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