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