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