cscvs to tla changeset 29
[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(void)
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);
90                 parse_keys(packets, publickey);
91                 close(fd);
92         }
93
94         return (fd > -1);
95 }
96
97 /**
98  *      store_key - Takes a key and stores it.
99  *      @publickey: A pointer to the public key to store.
100  *      @intrans: If we're already in a transaction.
101  *      @update: If true the key exists and should be updated.
102  *
103  *      Again we just use the hex representation of the keyid as the filename
104  *      to store the key to. We flatten the public key to a list of OpenPGP
105  *      packets and then use write_openpgp_stream() to write the stream out to
106  *      the file.
107  */
108 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
109 {
110         struct openpgp_packet_list *packets = NULL;
111         struct openpgp_packet_list *list_end = NULL;
112         struct openpgp_publickey *next = NULL;
113         char keyfile[1024];
114         int fd = -1;
115
116         snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
117                         get_keyid(publickey) & 0xFFFFFFFF);
118         fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
119
120         if (fd > -1) {
121                 next = publickey -> next;
122                 publickey -> next = NULL;
123                 flatten_publickey(publickey, &packets, &list_end);
124                 publickey -> next = next;
125                 
126                 write_openpgp_stream(file_putchar, &fd, packets);
127                 close(fd);
128         }
129
130         return (fd > -1);
131 }
132
133 /**
134  *      delete_key - Given a keyid delete the key from storage.
135  *      @keyid: The keyid to delete.
136  *      @intrans: If we're already in a transaction.
137  *
138  *      This function deletes a public key from whatever storage mechanism we
139  *      are using. Returns 0 if the key existed.
140  */
141 int delete_key(uint64_t keyid, bool intrans)
142 {
143         char keyfile[1024];
144
145         snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
146                         keyid & 0xFFFFFFFF);
147
148         return unlink(keyfile);
149 }
150
151 /**
152  *      fetch_key_text - Trys to find the keys that contain the supplied text.
153  *      @search: The text to search for.
154  *      @publickey: A pointer to a structure to return the key in.
155  *
156  *      This function searches for the supplied text and returns the keys that
157  *      contain it.
158  *
159  *      TODO: Write for flat file access. Some sort of grep?
160  */
161 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
162 {
163         return 0;
164 }
165
166 /*
167  * Include the basic keydb routines.
168  */
169 #define NEED_KEYID2UID 1
170 #define NEED_GETKEYSIGS 1
171 #define NEED_GETFULLKEYID 1
172 #include "keydb.c"