]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
cscvs to tla changeset 28
[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         if ((argc - optind) < 1) {
111                 usage();
112         } else if (!strcmp("add", argv[optind])) {
113                 if (binary) {
114                         read_openpgp_stream(stdin_getchar, NULL, &packets);
115                 } else {
116                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
117                 }
118                 if (packets != NULL) {
119                         parse_keys(packets, &keys);
120                         free_packet_list(packets);
121                         packets = NULL;
122                         if (verbose) {
123                                 fprintf(stderr, "Finished reading keys.\n");
124                         }
125         
126                         initdb();
127                         fprintf(stderr, "Got %d new keys.\n",
128                                         update_keys(&keys, verbose));
129                         cleanupdb();
130                 } else {
131                         rc = 1;
132                         fprintf(stderr, "No keys read.\n");
133                 }
134
135                 if (keys != NULL) {
136                         free_publickey(keys);
137                         keys = NULL;
138                 } else {
139                         rc = 1;
140                         fprintf(stderr, "No changes.\n");
141                 }
142         } else if ((argc - optind) == 2) {
143                 search = argv[optind+1];
144                 if (search != NULL) {
145                         keyid = strtoul(search, &end, 16);
146                         if (*search != 0 &&
147                                         end != NULL &&
148                                         *end == 0) {
149                                 ishex = true;
150                         }
151                 }
152                 initdb();
153                 if (!strcmp("index", argv[optind])) {
154                         find_keys(search, keyid, ishex, false, false, false);
155                 } else if (!strcmp("vindex", argv[optind])) {
156                         find_keys(search, keyid, ishex, false, false, true);
157                 } else if (!strcmp("delete", argv[optind])) {
158                         delete_key(getfullkeyid(keyid), false);
159                 } else if (!strcmp("get", argv[optind])) {
160                         if (fetch_key(keyid, &keys, false)) {
161                                 if (verbose) {
162                                         fprintf(stderr, "Got key.\n");
163                                 }
164                                 flatten_publickey(keys,
165                                                 &packets,
166                                                 &list_end);
167                                 armor_openpgp_stream(stdout_putchar,
168                                                 NULL,
169                                                 packets);
170                         } else {
171                                 puts("Key not found");
172                         }
173                 }
174                 cleanupdb();
175         } else {
176                 usage();
177         }
178
179         return rc;
180 }