cscvs to tla changeset 29
[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 keyserver");
78         puts("\tdelete - delete a given key from the keyserver");
79         puts("\tget    - retrieves the key requested from the keyserver");
80         puts("\tindex  - search for a key and list it");
81         puts("\tvindex - search for a key and list it and its signatures");
82 }
83
84 int main(int argc, char *argv[])
85 {
86         struct openpgp_packet_list      *packets = NULL;
87         struct openpgp_packet_list      *list_end = NULL;
88         struct openpgp_publickey        *keys = NULL;
89         int                              rc = EXIT_SUCCESS;
90         char                            *search = NULL;
91         char                            *end = NULL;
92         uint64_t                         keyid = 0;
93         bool                             ishex = false;
94         bool                             verbose = false;
95         bool                             binary = false;
96         int                              optchar;
97
98
99         while ((optchar = getopt(argc, argv, "bv")) != -1 ) {
100                 switch (optchar) {
101                 case 'b': 
102                         binary = true;
103                         break;
104                 case 'v': 
105                         verbose = true;
106                         break;
107                 }
108         }
109
110         readconfig();
111
112         if ((argc - optind) < 1) {
113                 usage();
114         } else if (!strcmp("add", argv[optind])) {
115                 if (binary) {
116                         read_openpgp_stream(stdin_getchar, NULL, &packets);
117                 } else {
118                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
119                 }
120                 if (packets != NULL) {
121                         parse_keys(packets, &keys);
122                         free_packet_list(packets);
123                         packets = NULL;
124                         if (verbose) {
125                                 fprintf(stderr, "Finished reading keys.\n");
126                         }
127         
128                         initdb();
129                         fprintf(stderr, "Got %d new keys.\n",
130                                         update_keys(&keys, verbose));
131                         cleanupdb();
132                 } else {
133                         rc = 1;
134                         fprintf(stderr, "No keys read.\n");
135                 }
136
137                 if (keys != NULL) {
138                         free_publickey(keys);
139                         keys = NULL;
140                 } else {
141                         rc = 1;
142                         fprintf(stderr, "No changes.\n");
143                 }
144         } else if ((argc - optind) == 2) {
145                 search = argv[optind+1];
146                 if (search != NULL) {
147                         keyid = strtoul(search, &end, 16);
148                         if (*search != 0 &&
149                                         end != NULL &&
150                                         *end == 0) {
151                                 ishex = true;
152                         }
153                 }
154                 initdb();
155                 if (!strcmp("index", argv[optind])) {
156                         find_keys(search, keyid, ishex, false, false, false);
157                 } else if (!strcmp("vindex", argv[optind])) {
158                         find_keys(search, keyid, ishex, false, false, true);
159                 } else if (!strcmp("delete", argv[optind])) {
160                         delete_key(getfullkeyid(keyid), false);
161                 } else if (!strcmp("get", argv[optind])) {
162                         if (fetch_key(keyid, &keys, false)) {
163                                 if (verbose) {
164                                         fprintf(stderr, "Got key.\n");
165                                 }
166                                 flatten_publickey(keys,
167                                                 &packets,
168                                                 &list_end);
169                                 armor_openpgp_stream(stdout_putchar,
170                                                 NULL,
171                                                 packets);
172                         } else {
173                                 puts("Key not found");
174                         }
175                 }
176                 cleanupdb();
177         } else {
178                 usage();
179         }
180
181         return rc;
182 }