No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / confgen / rndc-confgen.c
blob1373b312437dd7caf35d8436eef19a315350b7e9
1 /* $NetBSD$ */
3 /*
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 */
22 /*! \file */
24 /**
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.
33 #include <config.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
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>
43 #include <isc/file.h>
44 #include <isc/keyboard.h>
45 #include <isc/mem.h>
46 #include <isc/net.h>
47 #include <isc/print.h>
48 #include <isc/result.h>
49 #include <isc/string.h>
50 #include <isc/time.h>
51 #include <isc/util.h>
53 #include <dns/keyvalues.h>
54 #include <dns/name.h>
56 #include <dst/dst.h>
57 #include <confgen/os.h>
59 #include "util.h"
60 #include "keygen.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];
68 const char *progname;
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;
77 static void
78 usage(int status) {
80 fprintf(stderr, "\
81 Usage:\n\
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);
95 exit (status);
98 int
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);
110 char *p;
111 int ch;
112 int port;
113 int keysize;
114 struct in_addr addr4_dummy;
115 struct in6_addr addr6_dummy;
116 char *chrootdir = NULL;
117 char *user = NULL;
118 isc_boolean_t keyonly = ISC_FALSE;
119 int len;
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);
126 progname = program;
128 keyname = DEFAULT_KEYNAME;
129 keysize = DEFAULT_KEYLENGTH;
130 serveraddr = DEFAULT_SERVER;
131 port = DEFAULT_PORT;
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) {
137 switch (ch) {
138 case 'a':
139 keyonly = ISC_TRUE;
140 break;
141 case 'b':
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");
147 break;
148 case 'c':
149 keyfile = isc_commandline_argument;
150 break;
151 case 'h':
152 usage(0);
153 case 'k':
154 case 'y': /* Compatible with rndc -y. */
155 keyname = isc_commandline_argument;
156 break;
157 case 'M':
158 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
159 break;
161 case 'm':
162 show_final_mem = ISC_TRUE;
163 break;
164 case 'p':
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);
169 break;
170 case 'r':
171 randomfile = isc_commandline_argument;
172 break;
173 case 's':
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");
178 break;
179 case 't':
180 chrootdir = isc_commandline_argument;
181 break;
182 case 'u':
183 user = isc_commandline_argument;
184 break;
185 case 'V':
186 verbose = ISC_TRUE;
187 break;
188 case '?':
189 if (isc_commandline_option != '?') {
190 fprintf(stderr, "%s: invalid argument -%c\n",
191 program, isc_commandline_option);
192 usage(1);
193 } else
194 usage(0);
195 break;
196 default:
197 fprintf(stderr, "%s: unhandled option -%c\n",
198 program, isc_commandline_option);
199 exit(1);
203 argc -= isc_commandline_index;
204 argv += isc_commandline_index;
206 if (argc > 0)
207 usage(1);
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);
214 if (keyonly) {
215 write_key_file(keyfile, chrootdir == NULL ? user : NULL,
216 keyname, &key_txtbuffer, alg);
218 if (chrootdir != NULL) {
219 char *buf;
220 len = strlen(chrootdir) + strlen(keyfile) + 2;
221 buf = isc_mem_get(mctx, len);
222 if (buf == NULL)
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);
230 } else {
231 printf("\
232 # Start of rndc.conf\n\
233 key \"%s\" {\n\
234 algorithm %s;\n\
235 secret \"%.*s\";\n\
236 };\n\
238 options {\n\
239 default-key \"%s\";\n\
240 default-server %s;\n\
241 default-port %d;\n\
242 };\n\
243 # End of rndc.conf\n\
245 # Use with the following in named.conf, adjusting the allow list as needed:\n\
246 # key \"%s\" {\n\
247 # algorithm %s;\n\
248 # secret \"%.*s\";\n\
249 # };\n\
250 # \n\
251 # controls {\n\
252 # inet %s port %d\n\
253 # allow { %s; } keys { \"%s\"; };\n\
254 # };\n\
255 # End of named.conf\n",
256 keyname, algname,
257 (int)isc_buffer_usedlength(&key_txtbuffer),
258 (char *)isc_buffer_base(&key_txtbuffer),
259 keyname, serveraddr, port,
260 keyname, algname,
261 (int)isc_buffer_usedlength(&key_txtbuffer),
262 (char *)isc_buffer_base(&key_txtbuffer),
263 serveraddr, port, serveraddr, keyname);
266 if (show_final_mem)
267 isc_mem_stats(mctx, stderr);
269 isc_mem_destroy(&mctx);
271 return (0);