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