2 * Copyright (C) 2004, 2005, 2007-2009, 2011, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001, 2003 Internet Software Consortium.
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.
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.
18 /* $Id: rndc-confgen.c,v 1.7 2011/03/12 04:59:46 tbox Exp $ */
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.
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>
42 #include <isc/keyboard.h>
45 #include <isc/print.h>
46 #include <isc/result.h>
47 #include <isc/string.h>
51 #include <dns/keyvalues.h>
55 #include <confgen/os.h>
60 #define DEFAULT_KEYLENGTH 128 /*% Bits. */
61 #define DEFAULT_KEYNAME "rndc-key"
62 #define DEFAULT_SERVER "127.0.0.1"
63 #define DEFAULT_PORT 953
65 static char program[256];
68 isc_boolean_t verbose = ISC_FALSE;
70 const char *keyfile, *keydef;
72 ISC_PLATFORM_NORETURN_PRE static void
73 usage(int status) ISC_PLATFORM_NORETURN_POST;
80 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
81 [-s addr] [-t chrootdir] [-u user]\n\
82 -a: generate just the key clause and write it to keyfile (%s)\n\
83 -b bits: from 1 through 512, default %d; total length of the secret\n\
84 -c keyfile: specify an alternate key file (requires -a)\n\
85 -k keyname: the name as it will be used in named.conf and rndc.conf\n\
86 -p port: the port named will listen on and rndc will connect to\n\
87 -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
88 -s addr: the address to which rndc should connect\n\
89 -t chrootdir: write a keyfile in chrootdir as well (requires -a)\n\
90 -u user: set the keyfile owner to \"user\" (requires -a)\n",
91 progname, keydef, DEFAULT_KEYLENGTH);
97 main(int argc, char **argv) {
98 isc_boolean_t show_final_mem = ISC_FALSE;
99 isc_buffer_t key_txtbuffer;
100 char key_txtsecret[256];
101 isc_mem_t *mctx = NULL;
102 isc_result_t result = ISC_R_SUCCESS;
103 const char *keyname = NULL;
104 const char *randomfile = NULL;
105 const char *serveraddr = NULL;
106 dns_secalg_t alg = DST_ALG_HMACMD5;
107 const char *algname = alg_totext(alg);
112 struct in_addr addr4_dummy;
113 struct in6_addr addr6_dummy;
114 char *chrootdir = NULL;
116 isc_boolean_t keyonly = ISC_FALSE;
119 keydef = keyfile = RNDC_KEYFILE;
121 result = isc_file_progname(*argv, program, sizeof(program));
122 if (result != ISC_R_SUCCESS)
123 memmove(program, "rndc-confgen", 13);
126 keyname = DEFAULT_KEYNAME;
127 keysize = DEFAULT_KEYLENGTH;
128 serveraddr = DEFAULT_SERVER;
131 isc_commandline_errprint = ISC_FALSE;
133 while ((ch = isc_commandline_parse(argc, argv,
134 "ab:c:hk:Mmp:r:s:t:u:Vy")) != -1) {
140 keysize = strtol(isc_commandline_argument, &p, 10);
141 if (*p != '\0' || keysize < 0)
142 fatal("-b requires a non-negative number");
145 keyfile = isc_commandline_argument;
150 case 'y': /* Compatible with rndc -y. */
151 keyname = isc_commandline_argument;
154 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
158 show_final_mem = ISC_TRUE;
161 port = strtol(isc_commandline_argument, &p, 10);
162 if (*p != '\0' || port < 0 || port > 65535)
163 fatal("port '%s' out of range",
164 isc_commandline_argument);
167 randomfile = isc_commandline_argument;
170 serveraddr = isc_commandline_argument;
171 if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 &&
172 inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1)
173 fatal("-s should be an IPv4 or IPv6 address");
176 chrootdir = isc_commandline_argument;
179 user = isc_commandline_argument;
185 if (isc_commandline_option != '?') {
186 fprintf(stderr, "%s: invalid argument -%c\n",
187 program, isc_commandline_option);
193 fprintf(stderr, "%s: unhandled option -%c\n",
194 program, isc_commandline_option);
199 argc -= isc_commandline_index;
200 argv += isc_commandline_index;
206 DO("create memory context", isc_mem_create(0, 0, &mctx));
207 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
209 generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
212 write_key_file(keyfile, chrootdir == NULL ? user : NULL,
213 keyname, &key_txtbuffer, alg);
215 if (chrootdir != NULL) {
217 len = strlen(chrootdir) + strlen(keyfile) + 2;
218 buf = isc_mem_get(mctx, len);
220 fatal("isc_mem_get(%d) failed\n", len);
221 snprintf(buf, len, "%s%s%s", chrootdir,
222 (*keyfile != '/') ? "/" : "", keyfile);
224 write_key_file(buf, user, keyname, &key_txtbuffer, alg);
225 isc_mem_put(mctx, buf, len);
229 # Start of rndc.conf\n\
236 default-key \"%s\";\n\
237 default-server %s;\n\
240 # End of rndc.conf\n\
242 # Use with the following in named.conf, adjusting the allow list as needed:\n\
245 # secret \"%.*s\";\n\
250 # allow { %s; } keys { \"%s\"; };\n\
252 # End of named.conf\n",
254 (int)isc_buffer_usedlength(&key_txtbuffer),
255 (char *)isc_buffer_base(&key_txtbuffer),
256 keyname, serveraddr, port,
258 (int)isc_buffer_usedlength(&key_txtbuffer),
259 (char *)isc_buffer_base(&key_txtbuffer),
260 serveraddr, port, serveraddr, keyname);
264 isc_mem_stats(mctx, stderr);
266 isc_mem_destroy(&mctx);