Move update_keys to keydb rather than merge.
[onak.git] / keydb_file.c
1 /*
2  * keydb.c - Routines to store and fetch keys.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "charfuncs.h"
19 #include "keydb.h"
20 #include "keyid.h"
21 #include "keystructs.h"
22 #include "ll.h"
23 #include "mem.h"
24 #include "onak-conf.h"
25 #include "parsekey.h"
26
27 /**
28  *      initdb - Initialize the key database.
29  *
30  *      This is just a no-op for flat file access.
31  */
32 void initdb(bool readonly)
33 {
34 }
35
36 /**
37  *      cleanupdb - De-initialize the key database.
38  *
39  *      This is just a no-op for flat file access.
40  */
41 void cleanupdb(void)
42 {
43 }
44
45 /**
46  *      starttrans - Start a transaction.
47  *
48  *      This is just a no-op for flat file access.
49  */
50 bool starttrans(void)
51 {
52         return true;
53 }
54
55 /**
56  *      endtrans - End a transaction.
57  *
58  *      This is just a no-op for flat file access.
59  */
60 void endtrans(void)
61 {
62         return;
63 }
64
65 /**
66  *      fetch_key - Given a keyid fetch the key from storage.
67  *      @keyid: The keyid to fetch.
68  *      @publickey: A pointer to a structure to return the key in.
69  *      @intrans: If we're already in a transaction.
70  *
71  *      We use the hex representation of the keyid as the filename to fetch the
72  *      key from. The key is stored in the file as a binary OpenPGP stream of
73  *      packets, so we can just use read_openpgp_stream() to read the packets
74  *      in and then parse_keys() to parse the packets into a publickey
75  *      structure.
76  */
77 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
78                 bool intrans)
79 {
80         struct openpgp_packet_list *packets = NULL;
81         char keyfile[1024];
82         int fd = -1;
83
84         snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
85                         keyid & 0xFFFFFFFF);
86         fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
87
88         if (fd > -1) {
89                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
90                 parse_keys(packets, publickey);
91                 free_packet_list(packets);
92                 packets = NULL;
93                 close(fd);
94         }
95
96         return (fd > -1);
97 }
98
99 /**
100  *      store_key - Takes a key and stores it.
101  *      @publickey: A pointer to the public key to store.
102  *      @intrans: If we're already in a transaction.
103  *      @update: If true the key exists and should be updated.
104  *
105  *      Again we just use the hex representation of the keyid as the filename
106  *      to store the key to. We flatten the public key to a list of OpenPGP
107  *      packets and then use write_openpgp_stream() to write the stream out to
108  *      the file.
109  */
110 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
111 {
112         struct openpgp_packet_list *packets = NULL;
113         struct openpgp_packet_list *list_end = NULL;
114         struct openpgp_publickey *next = NULL;
115         char keyfile[1024];
116         int fd = -1;
117
118         snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
119                         get_keyid(publickey) & 0xFFFFFFFF);
120         fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
121
122         if (fd > -1) {
123                 next = publickey -> next;
124                 publickey -> next = NULL;
125                 flatten_publickey(publickey, &packets, &list_end);
126                 publickey -> next = next;
127                 
128                 write_openpgp_stream(file_putchar, &fd, packets);
129                 close(fd);
130                 free_packet_list(packets);
131                 packets = NULL;
132         }
133
134         return (fd > -1);
135 }
136
137 /**
138  *      delete_key - Given a keyid delete the key from storage.
139  *      @keyid: The keyid to delete.
140  *      @intrans: If we're already in a transaction.
141  *
142  *      This function deletes a public key from whatever storage mechanism we
143  *      are using. Returns 0 if the key existed.
144  */
145 int delete_key(uint64_t keyid, bool intrans)
146 {
147         char keyfile[1024];
148
149         snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
150                         keyid & 0xFFFFFFFF);
151
152         return unlink(keyfile);
153 }
154
155 /**
156  *      fetch_key_text - Trys to find the keys that contain the supplied text.
157  *      @search: The text to search for.
158  *      @publickey: A pointer to a structure to return the key in.
159  *
160  *      This function searches for the supplied text and returns the keys that
161  *      contain it.
162  *
163  *      TODO: Write for flat file access. Some sort of grep?
164  */
165 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
166 {
167         return 0;
168 }
169
170 /**
171  *      dumpdb - dump the key database
172  *      @filenamebase: The base filename to use for the dump.
173  *
174  *      Dumps the database into one or more files, which contain pure OpenPGP
175  *      that can be reimported into onak or gpg. filenamebase provides a base
176  *      file name for the dump; several files may be created, all of which will
177  *      begin with this string and then have a unique number and a .pgp
178  *      extension.
179  *          */
180 int dumpdb(char *filenamebase)
181 {
182         return 0;
183 }
184
185 /*
186  * Include the basic keydb routines.
187  */
188 #define NEED_KEYID2UID 1
189 #define NEED_GETKEYSIGS 1
190 #define NEED_GETFULLKEYID 1
191 #define NEED_UPDATEKEYS 1
192 #include "keydb.c"