2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001, 2003 Internet Software Consortium.
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.
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.9.2.6.2.5 2004/09/28 07:14:57 marka Exp $ */
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>
31 #include <isc/keyboard.h>
34 #include <isc/print.h>
35 #include <isc/result.h>
36 #include <isc/string.h>
40 #include <dns/keyvalues.h>
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
53 static char program[256];
56 isc_boolean_t verbose = ISC_FALSE;
58 const char *keyfile, *keydef;
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);
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'.
87 write_key_file(const char *keyfile, const char *user,
88 const char *keyname, isc_buffer_t *secret )
92 fd = safe_create(keyfile);
94 fatal( "unable to create \"%s\"\n", keyfile);
96 if (set_user(fd, user) == -1)
97 fatal("unable to set file owner\n");
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));
105 fatal("write to %s failed\n", keyfile);
107 fatal("fclose(%s) failed\n", keyfile);
108 fprintf(stderr, "wrote key file \"%s\"\n", keyfile);
112 main(int argc, char **argv) {
113 isc_boolean_t show_final_mem = ISC_FALSE;
114 isc_buffer_t key_rawbuffer;
115 isc_buffer_t key_txtbuffer;
116 isc_region_t key_rawregion;
117 isc_mem_t *mctx = NULL;
118 isc_entropy_t *ectx = NULL;
119 isc_entropysource_t *entropy_source = NULL;
120 isc_result_t result = ISC_R_SUCCESS;
121 dst_key_t *key = NULL;
122 const char *keyname = NULL;
123 const char *randomfile = NULL;
124 const char *serveraddr = NULL;
125 char key_rawsecret[64];
126 char key_txtsecret[256];
131 int entropy_flags = 0;
132 int open_keyboard = ISC_ENTROPY_KEYBOARDMAYBE;
133 struct in_addr addr4_dummy;
134 struct in6_addr addr6_dummy;
135 char *chrootdir = NULL;
137 isc_boolean_t keyonly = ISC_FALSE;
140 keydef = keyfile = RNDC_KEYFILE;
142 result = isc_file_progname(*argv, program, sizeof(program));
143 if (result != ISC_R_SUCCESS)
144 memcpy(program, "rndc-confgen", 13);
147 keyname = DEFAULT_KEYNAME;
148 keysize = DEFAULT_KEYLENGTH;
149 serveraddr = DEFAULT_SERVER;
152 while ((ch = isc_commandline_parse(argc, argv,
153 "ab:c:hk:Mmp:r:s:t:u:Vy")) != -1) {
159 keysize = strtol(isc_commandline_argument, &p, 10);
160 if (*p != '\0' || keysize < 0)
161 fatal("-b requires a non-negative number");
162 if (keysize < 1 || keysize > 512)
163 fatal("-b must be in the range 1 through 512");
166 keyfile = isc_commandline_argument;
171 case 'y': /* Compatible with rndc -y. */
172 keyname = isc_commandline_argument;
175 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
179 show_final_mem = ISC_TRUE;
182 port = strtol(isc_commandline_argument, &p, 10);
183 if (*p != '\0' || port < 0 || port > 65535)
184 fatal("port '%s' out of range",
185 isc_commandline_argument);
188 randomfile = isc_commandline_argument;
191 serveraddr = isc_commandline_argument;
192 if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 &&
193 inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1)
194 fatal("-s should be an IPv4 or IPv6 address");
197 chrootdir = isc_commandline_argument;
200 user = isc_commandline_argument;
209 fatal("unexpected error parsing command arguments: "
215 argc -= isc_commandline_index;
216 argv += isc_commandline_index;
221 DO("create memory context", isc_mem_create(0, 0, &mctx));
223 DO("create entropy context", isc_entropy_create(mctx, &ectx));
225 if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) {
227 open_keyboard = ISC_ENTROPY_KEYBOARDYES;
229 DO("start entropy source", isc_entropy_usebestsource(ectx,
234 entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY;
236 DO("initialize dst library", dst_lib_init(mctx, ectx, entropy_flags));
238 DO("generate key", dst_key_generate(dns_rootname, DST_ALG_HMACMD5,
241 dns_rdataclass_in, mctx, &key));
243 isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret));
245 DO("dump key to buffer", dst_key_tobuffer(key, &key_rawbuffer));
247 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
248 isc_buffer_usedregion(&key_rawbuffer, &key_rawregion);
250 DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "",
254 * Shut down the entropy source now so the "stop typing" message
255 * does not muck with the output.
257 if (entropy_source != NULL)
258 isc_entropy_destroysource(&entropy_source);
263 isc_entropy_detach(&ectx);
267 write_key_file(keyfile, chrootdir == NULL ? user : NULL,
268 keyname, &key_txtbuffer);
270 if (chrootdir != NULL) {
272 len = strlen(chrootdir) + strlen(keyfile) + 2;
273 buf = isc_mem_get(mctx, len);
275 fatal("isc_mem_get(%d) failed\n", len);
276 snprintf(buf, len, "%s%s%s", chrootdir,
277 (*keyfile != '/') ? "/" : "", keyfile);
279 write_key_file(buf, user, keyname, &key_txtbuffer);
280 isc_mem_put(mctx, buf, len);
284 # Start of rndc.conf\n\
286 algorithm hmac-md5;\n\
291 default-key \"%s\";\n\
292 default-server %s;\n\
295 # End of rndc.conf\n\
297 # Use with the following in named.conf, adjusting the allow list as needed:\n\
299 # algorithm hmac-md5;\n\
300 # secret \"%.*s\";\n\
305 # allow { %s; } keys { \"%s\"; };\n\
307 # End of named.conf\n",
309 (int)isc_buffer_usedlength(&key_txtbuffer),
310 (char *)isc_buffer_base(&key_txtbuffer),
311 keyname, serveraddr, port,
313 (int)isc_buffer_usedlength(&key_txtbuffer),
314 (char *)isc_buffer_base(&key_txtbuffer),
315 serveraddr, port, serveraddr, keyname);
319 isc_mem_stats(mctx, stderr);
321 isc_mem_destroy(&mctx);