rndc-confgen.c 8.11 KB
Newer Older
1
/*
2
 * Copyright (C) 2001, 2003-2005, 2007-2009, 2011, 2013, 2014, 2016, 2017  Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
 */

Automatic Updater's avatar
Automatic Updater committed
9
/* $Id: rndc-confgen.c,v 1.7 2011/03/12 04:59:46 tbox Exp $ */
10
11
12
13
14
15
16
17
18
19
20

/*! \file */

/**
 * rndc-confgen generates configuration files for rndc. It can be used
 * as a convenient alternative to writing the rndc.conf file and the
 * corresponding controls and key statements in named.conf by hand.
 * Alternatively, it can be run with the -a option to set up a
 * rndc.key file and avoid the need for a rndc.conf file and a
 * controls statement altogether.
 */
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <config.h>

#include <stdlib.h>
#include <stdarg.h>

#include <isc/assertions.h>
#include <isc/base64.h>
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/keyboard.h>
#include <isc/mem.h>
#include <isc/net.h>
36
#include <isc/print.h>
37
38
39
40
41
#include <isc/result.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/util.h>

42
43
#include <pk11/site.h>

44
45
46
47
#include <dns/keyvalues.h>
#include <dns/name.h>

#include <dst/dst.h>
48
#include <confgen/os.h>
49
50

#include "util.h"
51
#include "keygen.h"
52
53
54
55
56

#define DEFAULT_KEYNAME		"rndc-key"
#define DEFAULT_SERVER		"127.0.0.1"
#define DEFAULT_PORT		953

57
static char program[256];
58
const char *progname;
59

60
61
isc_boolean_t verbose = ISC_FALSE;

Mark Andrews's avatar
Mark Andrews committed
62
63
const char *keyfile, *keydef;

Francis Dupont's avatar
Francis Dupont committed
64
65
66
ISC_PLATFORM_NORETURN_PRE static void
usage(int status) ISC_PLATFORM_NORETURN_POST;

67
68
static void
usage(int status) {
Mark Andrews's avatar
Mark Andrews committed
69

70
#ifndef PK11_MD5_DISABLE
71
72
	fprintf(stderr, "\
Usage:\n\
73
 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
Mark Andrews's avatar
Mark Andrews committed
74
[-s addr] [-t chrootdir] [-u user]\n\
75
  -a:		 generate just the key clause and write it to keyfile (%s)\n\
76
  -A alg:	 algorithm (default hmac-md5 (deprecated and will change))\n\
77
  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
78
79
80
81
82
83
84
  -c keyfile:	 specify an alternate key file (requires -a)\n\
  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
  -p port:	 the port named will listen on and rndc will connect to\n\
  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
  -s addr:	 the address to which rndc should connect\n\
  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
85
		 progname, keydef);
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#else
	fprintf(stderr, "\
Usage:\n\
 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
[-s addr] [-t chrootdir] [-u user]\n\
  -a:		 generate just the key clause and write it to keyfile (%s)\n\
  -A alg:	 algorithm (default hmac-sha256)\n\
  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
  -c keyfile:	 specify an alternate key file (requires -a)\n\
  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
  -p port:	 the port named will listen on and rndc will connect to\n\
  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
  -s addr:	 the address to which rndc should connect\n\
  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
		 progname, keydef);
#endif
103
104
105
106
107
108
109
110

	exit (status);
}

int
main(int argc, char **argv) {
	isc_boolean_t show_final_mem = ISC_FALSE;
	isc_buffer_t key_txtbuffer;
111
	char key_txtsecret[256];
112
113
114
115
116
	isc_mem_t *mctx = NULL;
	isc_result_t result = ISC_R_SUCCESS;
	const char *keyname = NULL;
	const char *randomfile = NULL;
	const char *serveraddr = NULL;
117
	dns_secalg_t alg;
118
	isc_boolean_t algset = ISC_FALSE;
119
	const char *algname;
120
121
122
	char *p;
	int ch;
	int port;
123
	int keysize = -1;
124
125
	struct in_addr addr4_dummy;
	struct in6_addr addr6_dummy;
Mark Andrews's avatar
Mark Andrews committed
126
127
128
129
130
	char *chrootdir = NULL;
	char *user = NULL;
	isc_boolean_t keyonly = ISC_FALSE;
	int len;

Automatic Updater's avatar
Automatic Updater committed
131
	keydef = keyfile = RNDC_KEYFILE;
132

133
	result = isc_file_progname(*argv, program, sizeof(program));
134
	if (result != ISC_R_SUCCESS)
135
		memmove(program, "rndc-confgen", 13);
136
	progname = program;
137
138

	keyname = DEFAULT_KEYNAME;
139
#ifndef PK11_MD5_DISABLE
140
	alg = DST_ALG_HMACMD5;
141
142
143
#else
	alg = DST_ALG_HMACSHA256;
#endif
144
145
146
	serveraddr = DEFAULT_SERVER;
	port = DEFAULT_PORT;

147
148
	isc_commandline_errprint = ISC_FALSE;

Mark Andrews's avatar
Mark Andrews committed
149
	while ((ch = isc_commandline_parse(argc, argv,
150
151
					   "aA:b:c:hk:Mmp:r:s:t:u:Vy")) != -1)
	{
152
		switch (ch) {
Mark Andrews's avatar
Mark Andrews committed
153
154
155
		case 'a':
			keyonly = ISC_TRUE;
			break;
156
		case 'A':
157
			algset = ISC_TRUE;
158
159
160
161
162
			algname = isc_commandline_argument;
			alg = alg_fromtext(algname);
			if (alg == DST_ALG_UNKNOWN)
				fatal("Unsupported algorithm '%s'", algname);
			break;
163
164
165
166
167
		case 'b':
			keysize = strtol(isc_commandline_argument, &p, 10);
			if (*p != '\0' || keysize < 0)
				fatal("-b requires a non-negative number");
			break;
Mark Andrews's avatar
Mark Andrews committed
168
169
170
		case 'c':
			keyfile = isc_commandline_argument;
			break;
171
172
173
174
175
176
177
		case 'h':
			usage(0);
		case 'k':
		case 'y':	/* Compatible with rndc -y. */
			keyname = isc_commandline_argument;
			break;
		case 'M':
178
			isc_mem_debugging = ISC_MEM_DEBUGTRACE;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
			break;

		case 'm':
			show_final_mem = ISC_TRUE;
			break;
		case 'p':
			port = strtol(isc_commandline_argument, &p, 10);
			if (*p != '\0' || port < 0 || port > 65535)
				fatal("port '%s' out of range",
				      isc_commandline_argument);
			break;
		case 'r':
			randomfile = isc_commandline_argument;
			break;
		case 's':
			serveraddr = isc_commandline_argument;
195
196
			if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 &&
			    inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1)
197
				fatal("-s should be an IPv4 or IPv6 address");
Mark Andrews's avatar
Mark Andrews committed
198
199
200
201
202
203
			break;
		case 't':
			chrootdir = isc_commandline_argument;
			break;
		case 'u':
			user = isc_commandline_argument;
204
205
206
207
208
			break;
		case 'V':
			verbose = ISC_TRUE;
			break;
		case '?':
209
210
211
212
213
214
			if (isc_commandline_option != '?') {
				fprintf(stderr, "%s: invalid argument -%c\n",
					program, isc_commandline_option);
				usage(1);
			} else
				usage(0);
215
216
			break;
		default:
217
218
219
			fprintf(stderr, "%s: unhandled option -%c\n",
				program, isc_commandline_option);
			exit(1);
220
221
222
223
224
		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;
225
	POST(argv);
226
227
228
229

	if (argc > 0)
		usage(1);

230
231
232
233
234
235
236
237
238
239
	if (alg == DST_ALG_HMACMD5) {
		if (algset) {
			fprintf(stderr,
				"warning: use of hmac-md5 for RNDC keys "
				"is deprecated; hmac-sha256 is now "
				"recommended.\n");
		} else {
			fprintf(stderr,
				"warning: the default algorithm hmac-md5 "
				"is deprecated and will be\n"
Mark Andrews's avatar
Mark Andrews committed
240
				"changed to hmac-sha256 in a future release\n");
241
242
243
		}
	}

244
245
246
247
	if (keysize < 0)
		keysize = alg_bits(alg);
	algname = alg_totext(alg);

248
249
250
	DO("create memory context", isc_mem_create(0, 0, &mctx));
	isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));

251
	generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
252

Mark Andrews's avatar
Mark Andrews committed
253
	if (keyonly) {
254
		write_key_file(keyfile, chrootdir == NULL ? user : NULL,
255
			       keyname, &key_txtbuffer, alg);
256
257
258
259
260

		if (chrootdir != NULL) {
			char *buf;
			len = strlen(chrootdir) + strlen(keyfile) + 2;
			buf = isc_mem_get(mctx, len);
261
262
			if (buf == NULL)
				fatal("isc_mem_get(%d) failed\n", len);
263
264
			snprintf(buf, len, "%s%s%s", chrootdir,
				 (*keyfile != '/') ? "/" : "", keyfile);
Automatic Updater's avatar
Automatic Updater committed
265

266
			write_key_file(buf, user, keyname, &key_txtbuffer, alg);
Mark Andrews's avatar
Mark Andrews committed
267
268
269
270
			isc_mem_put(mctx, buf, len);
		}
	} else {
		printf("\
271
272
# Start of rndc.conf\n\
key \"%s\" {\n\
273
	algorithm %s;\n\
274
275
276
277
278
279
280
281
282
283
284
285
	secret \"%.*s\";\n\
};\n\
\n\
options {\n\
	default-key \"%s\";\n\
	default-server %s;\n\
	default-port %d;\n\
};\n\
# End of rndc.conf\n\
\n\
# Use with the following in named.conf, adjusting the allow list as needed:\n\
# key \"%s\" {\n\
286
# 	algorithm %s;\n\
287
288
289
290
291
292
293
294
# 	secret \"%.*s\";\n\
# };\n\
# \n\
# controls {\n\
# 	inet %s port %d\n\
# 		allow { %s; } keys { \"%s\"; };\n\
# };\n\
# End of named.conf\n",
295
		       keyname, algname,
Mark Andrews's avatar
Mark Andrews committed
296
297
298
		       (int)isc_buffer_usedlength(&key_txtbuffer),
		       (char *)isc_buffer_base(&key_txtbuffer),
		       keyname, serveraddr, port,
299
		       keyname, algname,
Mark Andrews's avatar
Mark Andrews committed
300
301
302
303
		       (int)isc_buffer_usedlength(&key_txtbuffer),
		       (char *)isc_buffer_base(&key_txtbuffer),
		       serveraddr, port, serveraddr, keyname);
	}
304
305
306
307
308
309
310
311

	if (show_final_mem)
		isc_mem_stats(mctx, stderr);

	isc_mem_destroy(&mctx);

	return (0);
}