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