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