cscvs to tla changeset 93
[onak.git] / onak.c
1 /*
2  * onak.c - An OpenPGP keyserver.
3  *
4  * This is the main swiss army knife binary.
5  *
6  * Jonathan McDowell <noodles@earth.li>
7  * 
8  * Copyright 2002 Project Purple
9  *
10  * $Id: onak.c,v 1.15 2003/09/28 20:33:34 noodles Exp $
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "armor.h"
19 #include "keydb.h"
20 #include "keyid.h"
21 #include "keyindex.h"
22 #include "keystructs.h"
23 #include "log.h"
24 #include "mem.h"
25 #include "merge.h"
26 #include "onak-conf.h"
27 #include "parsekey.h"
28
29 int stdin_getchar(void *ctx, size_t count, unsigned char *c)
30 {
31         int ic = 0;
32
33         while ((count > 0) && (ic != EOF)) {
34                 ic = getchar();
35                 *c = ic;
36                 c++;
37                 count--;
38         }
39
40         return (ic == EOF);
41 }
42
43 int stdout_putchar(void *ctx, size_t count, unsigned char *c)
44 {
45         int i;
46
47         for (i = 0; i < count; i++) {
48                 putchar(c[i]);
49         }
50         return 0;
51 }
52
53 void find_keys(char *search, uint64_t keyid, bool ishex,
54                 bool fingerprint, bool exact, bool verbose)
55 {
56         struct openpgp_publickey *publickey = NULL;
57         int count = 0;
58
59         if (ishex) {
60                 count = fetch_key(keyid, &publickey, false);
61         } else {
62                 count = fetch_key_text(search, &publickey);
63         }
64         if (publickey != NULL) {
65                 key_index(publickey, verbose, fingerprint, false);
66                 free_publickey(publickey);
67         } else if (count == 0) {
68                 puts("Key not found.");
69         } else {
70                 printf("Found %d keys, but maximum number to return is %d.\n",
71                                 count,
72                                 config.maxkeys);
73                 puts("Try again with a more specific search.");
74         }
75 }
76
77 void usage(void) {
78         puts("onak " VERSION " - an OpenPGP keyserver.\n");
79         puts("Usage:\n");
80         puts("\tonak [options] <command> <parameters>\n");
81         puts("\tCommands:\n");
82         puts("\tadd    - read armored OpenPGP keys from stdin and add to the"
83                 " keyserver");
84         puts("\tdelete - delete a given key from the keyserver");
85         puts("\tdump   - dump all the keys from the keyserver to a file or"
86                 " files\n\t         starting keydump*");
87         puts("\tget    - retrieves the key requested from the keyserver");
88         puts("\tindex  - search for a key and list it");
89         puts("\tvindex - search for a key and list it and its signatures");
90 }
91
92 int main(int argc, char *argv[])
93 {
94         struct openpgp_packet_list      *packets = NULL;
95         struct openpgp_packet_list      *list_end = NULL;
96         struct openpgp_publickey        *keys = NULL;
97         int                              rc = EXIT_SUCCESS;
98         int                              result = 0;
99         char                            *search = NULL;
100         char                            *end = NULL;
101         uint64_t                         keyid = 0;
102         bool                             ishex = false;
103         bool                             verbose = false;
104         bool                             update = false;
105         bool                             binary = false;
106         bool                             fingerprint = false;
107         int                              optchar;
108
109         while ((optchar = getopt(argc, argv, "bfuv")) != -1 ) {
110                 switch (optchar) {
111                 case 'b': 
112                         binary = true;
113                         break;
114                 case 'f': 
115                         fingerprint = true;
116                         break;
117                 case 'u': 
118                         update = true;
119                         break;
120                 case 'v': 
121                         verbose = true;
122                         setlogthreshold(LOGTHING_INFO);
123                         break;
124                 }
125         }
126
127         readconfig();
128         initlogthing("onak", config.logfile);
129
130         if ((argc - optind) < 1) {
131                 usage();
132         } else if (!strcmp("dump", argv[optind])) {
133                 initdb();
134                 dumpdb("keydump");
135                 cleanupdb();
136         } else if (!strcmp("add", argv[optind])) {
137                 if (binary) {
138                         result = read_openpgp_stream(stdin_getchar, NULL,
139                                  &packets);
140                         logthing(LOGTHING_INFO,
141                                         "read_openpgp_stream: %d", result);
142                 } else {
143                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
144                 }
145                 if (packets != NULL) {
146                         result = parse_keys(packets, &keys);
147                         free_packet_list(packets);
148                         packets = NULL;
149                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
150                                         result);
151
152                         initdb();
153                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
154                                         update_keys(&keys));
155                         if (keys != NULL && update) {
156                                 flatten_publickey(keys,
157                                         &packets,
158                                         &list_end);
159                                 armor_openpgp_stream(stdout_putchar,
160                                         NULL,
161                                         packets);
162                                 free_packet_list(packets);
163                                 packets = NULL;
164                         }
165                         cleanupdb();
166                 } else {
167                         rc = 1;
168                         logthing(LOGTHING_NOTICE, "No keys read.");
169                 }
170
171                 if (keys != NULL) {
172                         free_publickey(keys);
173                         keys = NULL;
174                 } else {
175                         rc = 1;
176                         logthing(LOGTHING_NOTICE, "No changes.");
177                 }
178         } else if ((argc - optind) == 2) {
179                 search = argv[optind+1];
180                 if (search != NULL) {
181                         keyid = strtoul(search, &end, 16);
182                         if (*search != 0 &&
183                                         end != NULL &&
184                                         *end == 0) {
185                                 ishex = true;
186                         }
187                 }
188                 initdb();
189                 if (!strcmp("index", argv[optind])) {
190                         find_keys(search, keyid, ishex, fingerprint,
191                                         false, false);
192                 } else if (!strcmp("vindex", argv[optind])) {
193                         find_keys(search, keyid, ishex, fingerprint,
194                                         false, true);
195                 } else if (!strcmp("delete", argv[optind])) {
196                         delete_key(getfullkeyid(keyid), false);
197                 } else if (!strcmp("get", argv[optind])) {
198                         if (!ishex) {
199                                 puts("Can't get a key on uid text."
200                                         " You must supply a keyid.");
201                         } else if (fetch_key(keyid, &keys, false)) {
202                                 logthing(LOGTHING_INFO, "Got key.");
203                                 flatten_publickey(keys,
204                                                 &packets,
205                                                 &list_end);
206                                 free_publickey(keys);
207                                 armor_openpgp_stream(stdout_putchar,
208                                                 NULL,
209                                                 packets);
210                                 free_packet_list(packets);
211                                 packets = NULL;
212                         } else {
213                                 puts("Key not found");
214                         }
215                 }
216                 cleanupdb();
217         } else {
218                 usage();
219         }
220
221         cleanuplogthing();
222         cleanupconfig();
223
224         return rc;
225 }