]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
cscvs to tla changeset 122
[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.19 2004/03/23 12:33:47 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         char                            *configfile = NULL;
75         int                              rc = EXIT_SUCCESS;
76         int                              result = 0;
77         char                            *search = NULL;
78         char                            *end = NULL;
79         uint64_t                         keyid = 0;
80         bool                             ishex = false;
81         bool                             verbose = false;
82         bool                             update = false;
83         bool                             binary = false;
84         bool                             fingerprint = false;
85         int                              optchar;
86
87         while ((optchar = getopt(argc, argv, "bc:fuv")) != -1 ) {
88                 switch (optchar) {
89                 case 'b': 
90                         binary = true;
91                         break;
92                 case 'c':
93                         configfile = strdup(optarg);
94                         break;
95                 case 'f': 
96                         fingerprint = true;
97                         break;
98                 case 'u': 
99                         update = true;
100                         break;
101                 case 'v': 
102                         verbose = true;
103                         setlogthreshold(LOGTHING_INFO);
104                         break;
105                 }
106         }
107
108         readconfig(configfile);
109         initlogthing("onak", config.logfile);
110
111         if ((argc - optind) < 1) {
112                 usage();
113         } else if (!strcmp("dump", argv[optind])) {
114                 initdb(true);
115                 dumpdb("keydump");
116                 cleanupdb();
117         } else if (!strcmp("add", argv[optind])) {
118                 if (binary) {
119                         result = read_openpgp_stream(stdin_getchar, NULL,
120                                  &packets, 0);
121                         logthing(LOGTHING_INFO,
122                                         "read_openpgp_stream: %d", result);
123                 } else {
124                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
125                 }
126                 if (packets != NULL) {
127                         result = parse_keys(packets, &keys);
128                         free_packet_list(packets);
129                         packets = NULL;
130                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
131                                         result);
132
133                         initdb(false);
134                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
135                                         update_keys(&keys));
136                         if (keys != NULL && update) {
137                                 flatten_publickey(keys,
138                                         &packets,
139                                         &list_end);
140                                 armor_openpgp_stream(stdout_putchar,
141                                         NULL,
142                                         packets);
143                                 free_packet_list(packets);
144                                 packets = NULL;
145                         }
146                         cleanupdb();
147                 } else {
148                         rc = 1;
149                         logthing(LOGTHING_NOTICE, "No keys read.");
150                 }
151
152                 if (keys != NULL) {
153                         free_publickey(keys);
154                         keys = NULL;
155                 } else {
156                         rc = 1;
157                         logthing(LOGTHING_NOTICE, "No changes.");
158                 }
159         } else if ((argc - optind) == 2) {
160                 search = argv[optind+1];
161                 if (search != NULL) {
162                         keyid = strtoul(search, &end, 16);
163                         if (*search != 0 &&
164                                         end != NULL &&
165                                         *end == 0) {
166                                 ishex = true;
167                         }
168                 }
169                 initdb(false);
170                 if (!strcmp("index", argv[optind])) {
171                         find_keys(search, keyid, ishex, fingerprint,
172                                         false, false);
173                 } else if (!strcmp("vindex", argv[optind])) {
174                         find_keys(search, keyid, ishex, fingerprint,
175                                         false, true);
176                 } else if (!strcmp("delete", argv[optind])) {
177                         delete_key(getfullkeyid(keyid), false);
178                 } else if (!strcmp("get", argv[optind])) {
179                         if (!ishex) {
180                                 puts("Can't get a key on uid text."
181                                         " You must supply a keyid.");
182                         } else if (fetch_key(keyid, &keys, false)) {
183                                 logthing(LOGTHING_INFO, "Got key.");
184                                 flatten_publickey(keys,
185                                                 &packets,
186                                                 &list_end);
187                                 free_publickey(keys);
188                                 armor_openpgp_stream(stdout_putchar,
189                                                 NULL,
190                                                 packets);
191                                 free_packet_list(packets);
192                                 packets = NULL;
193                         } else {
194                                 puts("Key not found");
195                         }
196                 }
197                 cleanupdb();
198         } else {
199                 usage();
200         }
201
202         cleanuplogthing();
203         cleanupconfig();
204
205         return rc;
206 }