]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
cscvs to tla changeset 36
[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                             update = false;
96         bool                             binary = false;
97         int                              optchar;
98
99
100         while ((optchar = getopt(argc, argv, "buv")) != -1 ) {
101                 switch (optchar) {
102                 case 'b': 
103                         binary = true;
104                         break;
105                 case 'u': 
106                         update = true;
107                         break;
108                 case 'v': 
109                         verbose = true;
110                         break;
111                 }
112         }
113
114         readconfig();
115
116         if ((argc - optind) < 1) {
117                 usage();
118         } else if (!strcmp("add", argv[optind])) {
119                 if (binary) {
120                         read_openpgp_stream(stdin_getchar, NULL, &packets);
121                 } else {
122                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
123                 }
124                 if (packets != NULL) {
125                         parse_keys(packets, &keys);
126                         free_packet_list(packets);
127                         packets = NULL;
128                         if (verbose) {
129                                 fprintf(stderr, "Finished reading keys.\n");
130                         }
131         
132                         initdb();
133                         fprintf(stderr, "Got %d new keys.\n",
134                                         update_keys(&keys, verbose));
135                         if (keys != NULL && update) {
136                                 flatten_publickey(keys,
137                                         &packets,
138                                         &list_end);
139                                 armor_openpgp_stream(stdout_putchar,
140                                         NULL,
141                                         packets);
142                                 free_packet_list(packets);
143                                 packets = NULL;
144                         }
145                         cleanupdb();
146                 } else {
147                         rc = 1;
148                         fprintf(stderr, "No keys read.\n");
149                 }
150
151                 if (keys != NULL) {
152                         free_publickey(keys);
153                         keys = NULL;
154                 } else {
155                         rc = 1;
156                         fprintf(stderr, "No changes.\n");
157                 }
158         } else if ((argc - optind) == 2) {
159                 search = argv[optind+1];
160                 if (search != NULL) {
161                         keyid = strtoul(search, &end, 16);
162                         if (*search != 0 &&
163                                         end != NULL &&
164                                         *end == 0) {
165                                 ishex = true;
166                         }
167                 }
168                 initdb();
169                 if (!strcmp("index", argv[optind])) {
170                         find_keys(search, keyid, ishex, false, false, false);
171                 } else if (!strcmp("vindex", argv[optind])) {
172                         find_keys(search, keyid, ishex, false, false, true);
173                 } else if (!strcmp("delete", argv[optind])) {
174                         delete_key(getfullkeyid(keyid), false);
175                 } else if (!strcmp("get", argv[optind])) {
176                         if (fetch_key(keyid, &keys, false)) {
177                                 if (verbose) {
178                                         fprintf(stderr, "Got key.\n");
179                                 }
180                                 flatten_publickey(keys,
181                                                 &packets,
182                                                 &list_end);
183                                 armor_openpgp_stream(stdout_putchar,
184                                                 NULL,
185                                                 packets);
186                                 free_packet_list(packets);
187                                 packets = NULL;
188                         } else {
189                                 puts("Key not found");
190                         }
191                 }
192                 cleanupdb();
193         } else {
194                 usage();
195         }
196
197         return rc;
198 }