cscvs to tla changeset 5
[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 "keydb.h"
19 #include "keyid.h"
20 #include "keystructs.h"
21 #include "ll.h"
22 #include "mem.h"
23 #include "onak-conf.h"
24 #include "parsekey.h"
25
26 /**
27  *      keydb_fetchchar - Fetches a char from a file.
28  */
29 static int keydb_fetchchar(void *fd, size_t count, unsigned char *c)
30 {
31         return !(read( *(int *) fd, c, count));
32 }
33
34 /**
35  *      keydb_putchar - Puts a char to a file.
36  */
37 static int keydb_putchar(void *fd, size_t count, unsigned char *c)
38 {
39         return !(write( *(int *) fd, c, count));
40 }
41
42 /**
43  *      initdb - Initialize the key database.
44  *
45  *      This is just a no-op for flat file access.
46  */
47 void initdb(void)
48 {
49 }
50
51 /**
52  *      cleanupdb - De-initialize the key database.
53  *
54  *      This is just a no-op for flat file access.
55  */
56 void cleanupdb(void)
57 {
58 }
59
60 /**
61  *      starttrans - Start a transaction.
62  *
63  *      This is just a no-op for flat file access.
64  */
65 bool starttrans(void)
66 {
67         return true;
68 }
69
70 /**
71  *      endtrans - End a transaction.
72  *
73  *      This is just a no-op for flat file access.
74  */
75 void endtrans(void)
76 {
77         return;
78 }
79
80 /**
81  *      fetch_key - Given a keyid fetch the key from storage.
82  *      @keyid: The keyid to fetch.
83  *      @publickey: A pointer to a structure to return the key in.
84  *      @intrans: If we're already in a transaction.
85  *
86  *      We use the hex representation of the keyid as the filename to fetch the
87  *      key from. The key is stored in the file as a binary OpenPGP stream of
88  *      packets, so we can just use read_openpgp_stream() to read the packets
89  *      in and then parse_keys() to parse the packets into a publickey
90  *      structure.
91  */
92 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
93                 bool intrans)
94 {
95         struct openpgp_packet_list *packets = NULL;
96         char keyfile[1024];
97         int fd = -1;
98
99         snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
100                         keyid & 0xFFFFFFFF);
101         fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
102
103         if (fd > -1) {
104                 read_openpgp_stream(keydb_fetchchar, &fd, &packets);
105                 parse_keys(packets, publickey);
106                 close(fd);
107         }
108
109         return (fd > -1);
110 }
111
112 /**
113  *      store_key - Takes a key and stores it.
114  *      @publickey: A pointer to the public key to store.
115  *      @intrans: If we're already in a transaction.
116  *      @update: If true the key exists and should be updated.
117  *
118  *      Again we just use the hex representation of the keyid as the filename
119  *      to store the key to. We flatten the public key to a list of OpenPGP
120  *      packets and then use write_openpgp_stream() to write the stream out to
121  *      the file.
122  */
123 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
124 {
125         struct openpgp_packet_list *packets = NULL;
126         struct openpgp_packet_list *list_end = NULL;
127         struct openpgp_publickey *next = NULL;
128         char keyfile[1024];
129         int fd = -1;
130
131         snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
132                         get_keyid(publickey) & 0xFFFFFFFF);
133         fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
134
135         if (fd > -1) {
136                 next = publickey -> next;
137                 publickey -> next = NULL;
138                 flatten_publickey(publickey, &packets, &list_end);
139                 publickey -> next = next;
140                 
141                 write_openpgp_stream(keydb_putchar, &fd, packets);
142                 close(fd);
143         }
144
145         return (fd > -1);
146 }
147
148 /**
149  *      delete_key - Given a keyid delete the key from storage.
150  *      @keyid: The keyid to delete.
151  *      @intrans: If we're already in a transaction.
152  *
153  *      This function deletes a public key from whatever storage mechanism we
154  *      are using. Returns 0 if the key existed.
155  */
156 int delete_key(uint64_t keyid, bool intrans)
157 {
158         char keyfile[1024];
159
160         snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
161                         keyid & 0xFFFFFFFF);
162
163         return unlink(keyfile);
164 }
165
166 /**
167  *      fetch_key_text - Trys to find the keys that contain the supplied text.
168  *      @search: The text to search for.
169  *      @publickey: A pointer to a structure to return the key in.
170  *
171  *      This function searches for the supplied text and returns the keys that
172  *      contain it.
173  *
174  *      TODO: Write for flat file access. Some sort of grep?
175  */
176 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
177 {
178         return 0;
179 }
180
181 /*
182  * Include the basic keydb routines.
183  */
184 #define NEED_KEYID2UID 1
185 #define NEED_GETKEYSIGS 1
186 #define NEED_GETFULLKEYID 1
187 #include "keydb.c"