4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: rndc-confgen.c,v 1.5 2009/09/29 15:06:05 fdupont Exp */
25 * rndc-confgen generates configuration files for rndc. It can be used
26 * as a convenient alternative to writing the rndc.conf file and the
27 * corresponding controls and key statements in named.conf by hand.
28 * Alternatively, it can be run with the -a option to set up a
29 * rndc.key file and avoid the need for a rndc.conf file and a
30 * controls statement altogether.
38 #include <isc/assertions.h>
39 #include <isc/base64.h>
40 #include <isc/buffer.h>
41 #include <isc/commandline.h>
42 #include <isc/entropy.h>
44 #include <isc/keyboard.h>
47 #include <isc/print.h>
48 #include <isc/result.h>
49 #include <isc/string.h>
53 #include <dns/keyvalues.h>
57 #include <confgen/os.h>
62 #define DEFAULT_KEYLENGTH 128 /*% Bits. */
63 #define DEFAULT_KEYNAME "rndc-key"
64 #define DEFAULT_SERVER "127.0.0.1"
65 #define DEFAULT_PORT 953
67 static char program
[256];
70 isc_boolean_t verbose
= ISC_FALSE
;
72 const char *keyfile
, *keydef
;
74 ISC_PLATFORM_NORETURN_PRE
static void
75 usage(int status
) ISC_PLATFORM_NORETURN_POST
;
82 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
83 [-s addr] [-t chrootdir] [-u user]\n\
84 -a: generate just the key clause and write it to keyfile (%s)\n\
85 -b bits: from 1 through 512, default %d; total length of the secret\n\
86 -c keyfile: specify an alternate key file (requires -a)\n\
87 -k keyname: the name as it will be used in named.conf and rndc.conf\n\
88 -p port: the port named will listen on and rndc will connect to\n\
89 -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
90 -s addr: the address to which rndc should connect\n\
91 -t chrootdir: write a keyfile in chrootdir as well (requires -a)\n\
92 -u user: set the keyfile owner to \"user\" (requires -a)\n",
93 progname
, keydef
, DEFAULT_KEYLENGTH
);
99 main(int argc
, char **argv
) {
100 isc_boolean_t show_final_mem
= ISC_FALSE
;
101 isc_buffer_t key_txtbuffer
;
102 char key_txtsecret
[256];
103 isc_mem_t
*mctx
= NULL
;
104 isc_result_t result
= ISC_R_SUCCESS
;
105 const char *keyname
= NULL
;
106 const char *randomfile
= NULL
;
107 const char *serveraddr
= NULL
;
108 dns_secalg_t alg
= DST_ALG_HMACMD5
;
109 const char *algname
= alg_totext(alg
);
114 struct in_addr addr4_dummy
;
115 struct in6_addr addr6_dummy
;
116 char *chrootdir
= NULL
;
118 isc_boolean_t keyonly
= ISC_FALSE
;
121 keydef
= keyfile
= RNDC_KEYFILE
;
123 result
= isc_file_progname(*argv
, program
, sizeof(program
));
124 if (result
!= ISC_R_SUCCESS
)
125 memcpy(program
, "rndc-confgen", 13);
128 keyname
= DEFAULT_KEYNAME
;
129 keysize
= DEFAULT_KEYLENGTH
;
130 serveraddr
= DEFAULT_SERVER
;
133 isc_commandline_errprint
= ISC_FALSE
;
135 while ((ch
= isc_commandline_parse(argc
, argv
,
136 "ab:c:hk:Mmp:r:s:t:u:Vy")) != -1) {
142 keysize
= strtol(isc_commandline_argument
, &p
, 10);
143 if (*p
!= '\0' || keysize
< 0)
144 fatal("-b requires a non-negative number");
145 if (keysize
< 1 || keysize
> 512)
146 fatal("-b must be in the range 1 through 512");
149 keyfile
= isc_commandline_argument
;
154 case 'y': /* Compatible with rndc -y. */
155 keyname
= isc_commandline_argument
;
158 isc_mem_debugging
= ISC_MEM_DEBUGTRACE
;
162 show_final_mem
= ISC_TRUE
;
165 port
= strtol(isc_commandline_argument
, &p
, 10);
166 if (*p
!= '\0' || port
< 0 || port
> 65535)
167 fatal("port '%s' out of range",
168 isc_commandline_argument
);
171 randomfile
= isc_commandline_argument
;
174 serveraddr
= isc_commandline_argument
;
175 if (inet_pton(AF_INET
, serveraddr
, &addr4_dummy
) != 1 &&
176 inet_pton(AF_INET6
, serveraddr
, &addr6_dummy
) != 1)
177 fatal("-s should be an IPv4 or IPv6 address");
180 chrootdir
= isc_commandline_argument
;
183 user
= isc_commandline_argument
;
189 if (isc_commandline_option
!= '?') {
190 fprintf(stderr
, "%s: invalid argument -%c\n",
191 program
, isc_commandline_option
);
197 fprintf(stderr
, "%s: unhandled option -%c\n",
198 program
, isc_commandline_option
);
203 argc
-= isc_commandline_index
;
204 argv
+= isc_commandline_index
;
209 DO("create memory context", isc_mem_create(0, 0, &mctx
));
210 isc_buffer_init(&key_txtbuffer
, &key_txtsecret
, sizeof(key_txtsecret
));
212 generate_key(mctx
, randomfile
, alg
, keysize
, &key_txtbuffer
);
215 write_key_file(keyfile
, chrootdir
== NULL
? user
: NULL
,
216 keyname
, &key_txtbuffer
, alg
);
218 if (chrootdir
!= NULL
) {
220 len
= strlen(chrootdir
) + strlen(keyfile
) + 2;
221 buf
= isc_mem_get(mctx
, len
);
223 fatal("isc_mem_get(%d) failed\n", len
);
224 snprintf(buf
, len
, "%s%s%s", chrootdir
,
225 (*keyfile
!= '/') ? "/" : "", keyfile
);
227 write_key_file(buf
, user
, keyname
, &key_txtbuffer
, alg
);
228 isc_mem_put(mctx
, buf
, len
);
232 # Start of rndc.conf\n\
239 default-key \"%s\";\n\
240 default-server %s;\n\
243 # End of rndc.conf\n\
245 # Use with the following in named.conf, adjusting the allow list as needed:\n\
248 # secret \"%.*s\";\n\
253 # allow { %s; } keys { \"%s\"; };\n\
255 # End of named.conf\n",
257 (int)isc_buffer_usedlength(&key_txtbuffer
),
258 (char *)isc_buffer_base(&key_txtbuffer
),
259 keyname
, serveraddr
, port
,
261 (int)isc_buffer_usedlength(&key_txtbuffer
),
262 (char *)isc_buffer_base(&key_txtbuffer
),
263 serveraddr
, port
, serveraddr
, keyname
);
267 isc_mem_stats(mctx
, stderr
);
269 isc_mem_destroy(&mctx
);