Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / rndc / rndc-confgen.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: rndc-confgen.c,v 1.9.2.7 2004/03/09 06:09:26 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <stdarg.h>
24
25 #include <isc/assertions.h>
26 #include <isc/base64.h>
27 #include <isc/buffer.h>
28 #include <isc/commandline.h>
29 #include <isc/entropy.h>
30 #include <isc/file.h>
31 #include <isc/keyboard.h>
32 #include <isc/mem.h>
33 #include <isc/net.h>
34 #include <isc/print.h>
35 #include <isc/result.h>
36 #include <isc/string.h>
37 #include <isc/time.h>
38 #include <isc/util.h>
39
40 #include <dns/keyvalues.h>
41 #include <dns/name.h>
42
43 #include <dst/dst.h>
44 #include <rndc/os.h>
45
46 #include "util.h"
47
48 #define DEFAULT_KEYLENGTH       128             /* Bits. */
49 #define DEFAULT_KEYNAME         "rndc-key"
50 #define DEFAULT_SERVER          "127.0.0.1"
51 #define DEFAULT_PORT            953
52
53 static char program[256];
54 char *progname;
55
56 isc_boolean_t verbose = ISC_FALSE;
57
58 const char *keyfile, *keydef;
59
60 static void
61 usage(int status) {
62
63         fprintf(stderr, "\
64 Usage:\n\
65  %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
66 [-s addr] [-t chrootdir] [-u user]\n\
67   -a:           generate just the key clause and write it to keyfile (%s)\n\
68   -b bits:      from 1 through 512, default %d; total length of the secret\n\
69   -c keyfile:   specify an alternate key file (requires -a)\n\
70   -k keyname:   the name as it will be used  in named.conf and rndc.conf\n\
71   -p port:      the port named will listen on and rndc will connect to\n\
72   -r randomfile: a file containing random data\n\
73   -s addr:      the address to which rndc should connect\n\
74   -t chrootdir: write a keyfile in chrootdir as well (requires -a)\n\
75   -u user:      set the keyfile owner to \"user\" (requires -a)\n",
76                 progname, keydef, DEFAULT_KEYLENGTH);
77
78         exit (status);
79 }
80
81 /*
82  * Write an rndc.key file to 'keyfile'.  If 'user' is non-NULL,
83  * make that user the owner of the file.  The key will have
84  * the name 'keyname' and the secret in the buffer 'secret'.
85  */
86 static void
87 write_key_file(const char *keyfile, const char *user,
88                const char *keyname, isc_buffer_t *secret )
89 {
90         FILE *fd;
91
92         fd = safe_create(keyfile);
93         if (fd == NULL)
94                 fatal( "unable to create \"%s\"\n", keyfile);
95         if (user != NULL) {
96                 if (set_user(fd, user) == -1)
97                         fatal("unable to set file owner\n");
98         }
99         fprintf(fd, "key \"%s\" {\n\talgorithm hmac-md5;\n"
100                 "\tsecret \"%.*s\";\n};\n", keyname,
101                 (int)isc_buffer_usedlength(secret),
102                 (char *)isc_buffer_base(secret));
103         fflush(fd);
104         if (ferror(fd))
105                 fatal("write to %s failed\n", keyfile);
106         if (fclose(fd))
107                 fatal("fclose(%s) failed\n", keyfile);
108 }
109
110 int
111 main(int argc, char **argv) {
112         isc_boolean_t show_final_mem = ISC_FALSE;
113         isc_buffer_t key_rawbuffer;
114         isc_buffer_t key_txtbuffer;
115         isc_region_t key_rawregion;
116         isc_mem_t *mctx = NULL;
117         isc_entropy_t *ectx = NULL;
118         isc_entropysource_t *entropy_source = NULL;
119         isc_result_t result = ISC_R_SUCCESS;
120         dst_key_t *key = NULL;
121         const char *keyname = NULL;
122         const char *randomfile = NULL;
123         const char *serveraddr = NULL;
124         char key_rawsecret[64];
125         char key_txtsecret[256];
126         char *p;
127         int ch;
128         int port;
129         int keysize;
130         int entropy_flags = 0;
131         int open_keyboard = ISC_ENTROPY_KEYBOARDMAYBE;
132         struct in_addr addr4_dummy;
133         struct in6_addr addr6_dummy;
134         char *chrootdir = NULL;
135         char *user = NULL;
136         isc_boolean_t keyonly = ISC_FALSE;
137         int len;
138
139         keydef = keyfile = RNDC_KEYFILE;
140
141         result = isc_file_progname(*argv, program, sizeof(program));
142         if (result != ISC_R_SUCCESS)
143                 memcpy(program, "rndc-confgen", 13);
144         progname = program;
145
146         keyname = DEFAULT_KEYNAME;
147         keysize = DEFAULT_KEYLENGTH;
148         serveraddr = DEFAULT_SERVER;
149         port = DEFAULT_PORT;
150
151         while ((ch = isc_commandline_parse(argc, argv,
152                                            "ab:c:hk:Mmp:r:s:t:u:Vy")) != -1) {
153                 switch (ch) {
154                 case 'a':
155                         keyonly = ISC_TRUE;
156                         break;
157                 case 'b':
158                         keysize = strtol(isc_commandline_argument, &p, 10);
159                         if (*p != '\0' || keysize < 0)
160                                 fatal("-b requires a non-negative number");
161                         if (keysize < 1 || keysize > 512)
162                                 fatal("-b must be in the range 1 through 512");
163                         break;
164                 case 'c':
165                         keyfile = isc_commandline_argument;
166                         break;
167                 case 'h':
168                         usage(0);
169                 case 'k':
170                 case 'y':       /* Compatible with rndc -y. */
171                         keyname = isc_commandline_argument;
172                         break;
173                 case 'M':
174                         isc_mem_debugging = 1;
175                         break;
176
177                 case 'm':
178                         show_final_mem = ISC_TRUE;
179                         break;
180                 case 'p':
181                         port = strtol(isc_commandline_argument, &p, 10);
182                         if (*p != '\0' || port < 0 || port > 65535)
183                                 fatal("port '%s' out of range",
184                                       isc_commandline_argument);
185                         break;
186                 case 'r':
187                         randomfile = isc_commandline_argument;
188                         break;
189                 case 's':
190                         serveraddr = isc_commandline_argument;
191                         if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 &&
192                             inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1)
193                                 fatal("-s should be an IPv4 or IPv6 address");
194                         break;
195                 case 't':
196                         chrootdir = isc_commandline_argument;
197                         break;
198                 case 'u':
199                         user = isc_commandline_argument;
200                         break;
201                 case 'V':
202                         verbose = ISC_TRUE;
203                         break;
204                 case '?':
205                         usage(1);
206                         break;
207                 default:
208                         fatal("unexpected error parsing command arguments: "
209                               "got %c\n", ch);
210                         break;
211                 }
212         }
213
214         argc -= isc_commandline_index;
215         argv += isc_commandline_index;
216
217         if (argc > 0)
218                 usage(1);
219
220         DO("create memory context", isc_mem_create(0, 0, &mctx));
221
222         DO("create entropy context", isc_entropy_create(mctx, &ectx));
223
224         if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) {
225                 randomfile = NULL;
226                 open_keyboard = ISC_ENTROPY_KEYBOARDYES;
227         }
228         DO("start entropy source", isc_entropy_usebestsource(ectx,
229                                                              &entropy_source,
230                                                              randomfile,
231                                                              open_keyboard));
232
233         entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY;
234
235         DO("initialize dst library", dst_lib_init(mctx, ectx, entropy_flags));
236
237         DO("generate key", dst_key_generate(dns_rootname, DST_ALG_HMACMD5,
238                                             keysize, 0, 0,
239                                             DNS_KEYPROTO_ANY,
240                                             dns_rdataclass_in, mctx, &key));
241
242         isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret));
243
244         DO("dump key to buffer", dst_key_tobuffer(key, &key_rawbuffer));
245
246         isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
247         isc_buffer_usedregion(&key_rawbuffer, &key_rawregion);
248
249         DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "",
250                                                      &key_txtbuffer));
251
252         /*
253          * Shut down the entropy source now so the "stop typing" message
254          * does not muck with the output.
255          */
256         if (entropy_source != NULL)
257                 isc_entropy_destroysource(&entropy_source);
258
259         if (key != NULL)
260                 dst_key_free(&key);
261
262         isc_entropy_detach(&ectx);
263         dst_lib_destroy();
264
265         if (keyonly) {
266                 write_key_file(keyfile, chrootdir == NULL ? user : NULL,
267                                keyname, &key_txtbuffer);
268
269                 if (chrootdir != NULL) {
270                         char *buf;
271                         len = strlen(chrootdir) + strlen(keyfile) + 2;
272                         buf = isc_mem_get(mctx, len);
273                         if (buf == NULL)
274                                 fatal("isc_mem_get(%d) failed\n", len);
275                         snprintf(buf, len, "%s/%s", chrootdir, keyfile);
276                         
277                         write_key_file(buf, user, keyname, &key_txtbuffer);
278                         isc_mem_put(mctx, buf, len);
279                 }
280         } else {
281                 printf("\
282 # Start of rndc.conf\n\
283 key \"%s\" {\n\
284         algorithm hmac-md5;\n\
285         secret \"%.*s\";\n\
286 };\n\
287 \n\
288 options {\n\
289         default-key \"%s\";\n\
290         default-server %s;\n\
291         default-port %d;\n\
292 };\n\
293 # End of rndc.conf\n\
294 \n\
295 # Use with the following in named.conf, adjusting the allow list as needed:\n\
296 # key \"%s\" {\n\
297 #       algorithm hmac-md5;\n\
298 #       secret \"%.*s\";\n\
299 # };\n\
300 # \n\
301 # controls {\n\
302 #       inet %s port %d\n\
303 #               allow { %s; } keys { \"%s\"; };\n\
304 # };\n\
305 # End of named.conf\n",
306                        keyname,
307                        (int)isc_buffer_usedlength(&key_txtbuffer),
308                        (char *)isc_buffer_base(&key_txtbuffer),
309                        keyname, serveraddr, port,
310                        keyname,
311                        (int)isc_buffer_usedlength(&key_txtbuffer),
312                        (char *)isc_buffer_base(&key_txtbuffer),
313                        serveraddr, port, serveraddr, keyname);
314         }
315
316         if (show_final_mem)
317                 isc_mem_stats(mctx, stderr);
318
319         isc_mem_destroy(&mctx);
320
321         return (0);
322 }