]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
cscvs to tla changeset 55
[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         bool                             fingerprint = false;
100         int                              optchar;
101
102         while ((optchar = getopt(argc, argv, "bfuv")) != -1 ) {
103                 switch (optchar) {
104                 case 'b': 
105                         binary = true;
106                         break;
107                 case 'f': 
108                         fingerprint = true;
109                         break;
110                 case 'u': 
111                         update = true;
112                         break;
113                 case 'v': 
114                         verbose = true;
115                         break;
116                 }
117         }
118
119         readconfig();
120
121         if ((argc - optind) < 1) {
122                 usage();
123         } else if (!strcmp("add", argv[optind])) {
124                 if (binary) {
125                         result = read_openpgp_stream(stdin_getchar, NULL,
126                                  &packets);
127                         if (verbose) {
128                                 fprintf(stderr,
129                                         "read_openpgp_stream: %d\n", result);
130                         }
131                 } else {
132                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
133                 }
134                 if (packets != NULL) {
135                         result = parse_keys(packets, &keys);
136                         free_packet_list(packets);
137                         packets = NULL;
138                         if (verbose) {
139                                 fprintf(stderr, "Finished reading %d keys.\n",
140                                         result);
141                         }
142
143                         initdb();
144                         fprintf(stderr, "Got %d new keys.\n",
145                                         update_keys(&keys, verbose));
146                         if (keys != NULL && update) {
147                                 flatten_publickey(keys,
148                                         &packets,
149                                         &list_end);
150                                 armor_openpgp_stream(stdout_putchar,
151                                         NULL,
152                                         packets);
153                                 free_packet_list(packets);
154                                 packets = NULL;
155                         }
156                         cleanupdb();
157                 } else {
158                         rc = 1;
159                         fprintf(stderr, "No keys read.\n");
160                 }
161
162                 if (keys != NULL) {
163                         free_publickey(keys);
164                         keys = NULL;
165                 } else {
166                         rc = 1;
167                         fprintf(stderr, "No changes.\n");
168                 }
169         } else if ((argc - optind) == 2) {
170                 search = argv[optind+1];
171                 if (search != NULL) {
172                         keyid = strtoul(search, &end, 16);
173                         if (*search != 0 &&
174                                         end != NULL &&
175                                         *end == 0) {
176                                 ishex = true;
177                         }
178                 }
179                 initdb();
180                 if (!strcmp("index", argv[optind])) {
181                         find_keys(search, keyid, ishex, fingerprint,
182                                         false, false);
183                 } else if (!strcmp("vindex", argv[optind])) {
184                         find_keys(search, keyid, ishex, fingerprint,
185                                         false, true);
186                 } else if (!strcmp("delete", argv[optind])) {
187                         delete_key(getfullkeyid(keyid), false);
188                 } else if (!strcmp("get", argv[optind])) {
189                         if (fetch_key(keyid, &keys, false)) {
190                                 if (verbose) {
191                                         fprintf(stderr, "Got key.\n");
192                                 }
193                                 flatten_publickey(keys,
194                                                 &packets,
195                                                 &list_end);
196                                 armor_openpgp_stream(stdout_putchar,
197                                                 NULL,
198                                                 packets);
199                                 free_packet_list(packets);
200                                 packets = NULL;
201                         } else {
202                                 puts("Key not found");
203                         }
204                 }
205                 cleanupdb();
206         } else {
207                 usage();
208         }
209
210         cleanupconfig();
211
212         return rc;
213 }