]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
220483d196e211dc14ce4211cbaf122dbb384604
[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("\tdump   - dump all the keys from the keyserver to a file or"
82                 " files\n\t         starting keydump*");
83         puts("\tget    - retrieves the key requested from the keyserver");
84         puts("\tindex  - search for a key and list it");
85         puts("\tvindex - search for a key and list it and its signatures");
86 }
87
88 int main(int argc, char *argv[])
89 {
90         struct openpgp_packet_list      *packets = NULL;
91         struct openpgp_packet_list      *list_end = NULL;
92         struct openpgp_publickey        *keys = NULL;
93         int                              rc = EXIT_SUCCESS;
94         int                              result = 0;
95         char                            *search = NULL;
96         char                            *end = NULL;
97         uint64_t                         keyid = 0;
98         bool                             ishex = false;
99         bool                             verbose = false;
100         bool                             update = false;
101         bool                             binary = false;
102         bool                             fingerprint = false;
103         int                              optchar;
104
105         while ((optchar = getopt(argc, argv, "bfuv")) != -1 ) {
106                 switch (optchar) {
107                 case 'b': 
108                         binary = true;
109                         break;
110                 case 'f': 
111                         fingerprint = true;
112                         break;
113                 case 'u': 
114                         update = true;
115                         break;
116                 case 'v': 
117                         verbose = true;
118                         setlogthreshold(LOGTHING_INFO);
119                         break;
120                 }
121         }
122
123         readconfig();
124         initlogthing("onak", config.logfile);
125
126         if ((argc - optind) < 1) {
127                 usage();
128         } else if (!strcmp("dump", argv[optind])) {
129                 initdb();
130                 dumpdb("keydump");
131                 cleanupdb();
132         } else if (!strcmp("add", argv[optind])) {
133                 if (binary) {
134                         result = read_openpgp_stream(stdin_getchar, NULL,
135                                  &packets);
136                         logthing(LOGTHING_INFO,
137                                         "read_openpgp_stream: %d", result);
138                 } else {
139                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
140                 }
141                 if (packets != NULL) {
142                         result = parse_keys(packets, &keys);
143                         free_packet_list(packets);
144                         packets = NULL;
145                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
146                                         result);
147
148                         initdb();
149                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
150                                         update_keys(&keys));
151                         if (keys != NULL && update) {
152                                 flatten_publickey(keys,
153                                         &packets,
154                                         &list_end);
155                                 armor_openpgp_stream(stdout_putchar,
156                                         NULL,
157                                         packets);
158                                 free_packet_list(packets);
159                                 packets = NULL;
160                         }
161                         cleanupdb();
162                 } else {
163                         rc = 1;
164                         logthing(LOGTHING_NOTICE, "No keys read.");
165                 }
166
167                 if (keys != NULL) {
168                         free_publickey(keys);
169                         keys = NULL;
170                 } else {
171                         rc = 1;
172                         logthing(LOGTHING_NOTICE, "No changes.");
173                 }
174         } else if ((argc - optind) == 2) {
175                 search = argv[optind+1];
176                 if (search != NULL) {
177                         keyid = strtoul(search, &end, 16);
178                         if (*search != 0 &&
179                                         end != NULL &&
180                                         *end == 0) {
181                                 ishex = true;
182                         }
183                 }
184                 initdb();
185                 if (!strcmp("index", argv[optind])) {
186                         find_keys(search, keyid, ishex, fingerprint,
187                                         false, false);
188                 } else if (!strcmp("vindex", argv[optind])) {
189                         find_keys(search, keyid, ishex, fingerprint,
190                                         false, true);
191                 } else if (!strcmp("delete", argv[optind])) {
192                         delete_key(getfullkeyid(keyid), false);
193                 } else if (!strcmp("get", argv[optind])) {
194                         if (fetch_key(keyid, &keys, false)) {
195                                 logthing(LOGTHING_INFO, "Got key.");
196                                 flatten_publickey(keys,
197                                                 &packets,
198                                                 &list_end);
199                                 armor_openpgp_stream(stdout_putchar,
200                                                 NULL,
201                                                 packets);
202                                 free_packet_list(packets);
203                                 packets = NULL;
204                         } else {
205                                 puts("Key not found");
206                         }
207                 }
208                 cleanupdb();
209         } else {
210                 usage();
211         }
212
213         cleanuplogthing();
214         cleanupconfig();
215
216         return rc;
217 }