Blame bin/confgen/rndc-confgen.c

Packit 5ce601
/*
Packit 5ce601
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit 5ce601
 *
Packit 5ce601
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit 5ce601
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service 704ed8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
Packit 5ce601
 *
Packit 5ce601
 * See the COPYRIGHT file distributed with this work for additional
Packit 5ce601
 * information regarding copyright ownership.
Packit 5ce601
 */
Packit 5ce601
Packit 5ce601
/*! \file */
Packit 5ce601
Packit 5ce601
/**
Packit 5ce601
 * rndc-confgen generates configuration files for rndc. It can be used
Packit 5ce601
 * as a convenient alternative to writing the rndc.conf file and the
Packit 5ce601
 * corresponding controls and key statements in named.conf by hand.
Packit 5ce601
 * Alternatively, it can be run with the -a option to set up a
Packit 5ce601
 * rndc.key file and avoid the need for a rndc.conf file and a
Packit 5ce601
 * controls statement altogether.
Packit 5ce601
 */
Packit 5ce601
Packit 5ce601
#include <config.h>
Packit 5ce601
Packit 5ce601
#include <stdbool.h>
Packit 5ce601
#include <stdlib.h>
Packit 5ce601
#include <stdarg.h>
Packit 5ce601
Packit 5ce601
#include <isc/assertions.h>
Packit 5ce601
#include <isc/base64.h>
Packit 5ce601
#include <isc/buffer.h>
Packit 5ce601
#include <isc/commandline.h>
Packit 5ce601
#include <isc/entropy.h>
Packit 5ce601
#include <isc/file.h>
Packit 5ce601
#include <isc/keyboard.h>
Packit 5ce601
#include <isc/mem.h>
Packit 5ce601
#include <isc/net.h>
Packit 5ce601
#include <isc/print.h>
Packit 5ce601
#include <isc/result.h>
Packit 5ce601
#include <isc/string.h>
Packit 5ce601
#include <isc/time.h>
Packit 5ce601
#include <isc/util.h>
Packit 5ce601
Packit 5ce601
#include <pk11/site.h>
Packit 5ce601
Packit 5ce601
#include <dns/keyvalues.h>
Packit 5ce601
#include <dns/name.h>
Packit 5ce601
Packit 5ce601
#include <dst/dst.h>
Packit 5ce601
#include <confgen/os.h>
Packit 5ce601
Packit 5ce601
#include "util.h"
Packit 5ce601
#include "keygen.h"
Packit 5ce601
Packit 5ce601
#define DEFAULT_KEYNAME		"rndc-key"
Packit 5ce601
#define DEFAULT_SERVER		"127.0.0.1"
Packit 5ce601
#define DEFAULT_PORT		953
Packit 5ce601
Packit 5ce601
static char program[256];
Packit 5ce601
const char *progname;
Packit 5ce601
Packit 5ce601
bool verbose = false;
Packit 5ce601
Packit Service d3afd5
const char *keyfile, *keydef;
Packit 5ce601
Packit 5ce601
ISC_PLATFORM_NORETURN_PRE static void
Packit 5ce601
usage(int status) ISC_PLATFORM_NORETURN_POST;
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
usage(int status) {
Packit 5ce601
Packit Service d3afd5
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	fprintf(stderr, "\
Packit 5ce601
Usage:\n\
Packit 5ce601
 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
Packit 5ce601
[-s addr] [-t chrootdir] [-u user]\n\
Packit 5ce601
  -a:		 generate just the key clause and write it to keyfile (%s)\n\
Packit Service d3afd5
  -A alg:	 algorithm (default hmac-md5)\n\
Packit 5ce601
  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
Packit 5ce601
  -c keyfile:	 specify an alternate key file (requires -a)\n\
Packit 5ce601
  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
Packit 5ce601
  -p port:	 the port named will listen on and rndc will connect to\n\
Packit 5ce601
  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
Packit 5ce601
  -s addr:	 the address to which rndc should connect\n\
Packit 5ce601
  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
Packit 5ce601
  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
Packit Service d3afd5
		 progname, keydef);
Packit Service d3afd5
#else
Packit Service d3afd5
	fprintf(stderr, "\
Packit Service d3afd5
Usage:\n\
Packit Service d3afd5
 %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
Packit Service d3afd5
[-s addr] [-t chrootdir] [-u user]\n\
Packit Service d3afd5
  -a:		 generate just the key clause and write it to keyfile (%s)\n\
Packit Service d3afd5
  -A alg:	 algorithm (default hmac-sha256)\n\
Packit Service d3afd5
  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
Packit Service d3afd5
  -c keyfile:	 specify an alternate key file (requires -a)\n\
Packit Service d3afd5
  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
Packit Service d3afd5
  -p port:	 the port named will listen on and rndc will connect to\n\
Packit Service d3afd5
  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
Packit Service d3afd5
  -s addr:	 the address to which rndc should connect\n\
Packit Service d3afd5
  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
Packit Service d3afd5
  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
Packit Service d3afd5
		 progname, keydef);
Packit Service d3afd5
#endif
Packit 5ce601
Packit 5ce601
	exit (status);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
int
Packit 5ce601
main(int argc, char **argv) {
Packit 5ce601
	bool show_final_mem = false;
Packit 5ce601
	isc_buffer_t key_txtbuffer;
Packit 5ce601
	char key_txtsecret[256];
Packit 5ce601
	isc_mem_t *mctx = NULL;
Packit 5ce601
	isc_result_t result = ISC_R_SUCCESS;
Packit 5ce601
	const char *keyname = NULL;
Packit 5ce601
	const char *randomfile = NULL;
Packit 5ce601
	const char *serveraddr = NULL;
Packit 5ce601
	dns_secalg_t alg;
Packit 5ce601
	const char *algname;
Packit 5ce601
	char *p;
Packit 5ce601
	int ch;
Packit 5ce601
	int port;
Packit 5ce601
	int keysize = -1;
Packit 5ce601
	struct in_addr addr4_dummy;
Packit 5ce601
	struct in6_addr addr6_dummy;
Packit 5ce601
	char *chrootdir = NULL;
Packit 5ce601
	char *user = NULL;
Packit 5ce601
	bool keyonly = false;
Packit 5ce601
	int len;
Packit 5ce601
Packit 5ce601
	keydef = keyfile = RNDC_KEYFILE;
Packit 5ce601
Packit 5ce601
	result = isc_file_progname(*argv, program, sizeof(program));
Packit 5ce601
	if (result != ISC_R_SUCCESS)
Packit 5ce601
		memmove(program, "rndc-confgen", 13);
Packit 5ce601
	progname = program;
Packit 5ce601
Packit 5ce601
	keyname = DEFAULT_KEYNAME;
Packit Service c9a470
#ifndef PK11_MD5_DISABLE
Packit Service d3afd5
	alg = DST_ALG_HMACMD5;
Packit Service d3afd5
#else
Packit Service d3afd5
	alg = DST_ALG_HMACSHA256;
Packit 5ce601
#endif
Packit 5ce601
	serveraddr = DEFAULT_SERVER;
Packit 5ce601
	port = DEFAULT_PORT;
Packit 5ce601
Packit 5ce601
	isc_commandline_errprint = false;
Packit 5ce601
Packit 5ce601
	while ((ch = isc_commandline_parse(argc, argv,
Packit 5ce601
					   "aA:b:c:hk:Mmp:r:s:t:u:Vy")) != -1)
Packit 5ce601
	{
Packit 5ce601
		switch (ch) {
Packit 5ce601
		case 'a':
Packit 5ce601
			keyonly = true;
Packit 5ce601
			break;
Packit 5ce601
		case 'A':
Packit 5ce601
			algname = isc_commandline_argument;
Packit 5ce601
			alg = alg_fromtext(algname);
Packit 5ce601
			if (alg == DST_ALG_UNKNOWN)
Packit 5ce601
				fatal("Unsupported algorithm '%s'", algname);
Packit 5ce601
			break;
Packit 5ce601
		case 'b':
Packit 5ce601
			keysize = strtol(isc_commandline_argument, &p, 10);
Packit 5ce601
			if (*p != '\0' || keysize < 0)
Packit 5ce601
				fatal("-b requires a non-negative number");
Packit 5ce601
			break;
Packit 5ce601
		case 'c':
Packit 5ce601
			keyfile = isc_commandline_argument;
Packit 5ce601
			break;
Packit 5ce601
		case 'h':
Packit 5ce601
			usage(0);
Packit 5ce601
		case 'k':
Packit 5ce601
		case 'y':	/* Compatible with rndc -y. */
Packit 5ce601
			keyname = isc_commandline_argument;
Packit 5ce601
			break;
Packit 5ce601
		case 'M':
Packit 5ce601
			isc_mem_debugging = ISC_MEM_DEBUGTRACE;
Packit 5ce601
			break;
Packit 5ce601
Packit 5ce601
		case 'm':
Packit 5ce601
			show_final_mem = true;
Packit 5ce601
			break;
Packit 5ce601
		case 'p':
Packit 5ce601
			port = strtol(isc_commandline_argument, &p, 10);
Packit 5ce601
			if (*p != '\0' || port < 0 || port > 65535)
Packit 5ce601
				fatal("port '%s' out of range",
Packit 5ce601
				      isc_commandline_argument);
Packit 5ce601
			break;
Packit 5ce601
		case 'r':
Packit 5ce601
			randomfile = isc_commandline_argument;
Packit 5ce601
			break;
Packit 5ce601
		case 's':
Packit 5ce601
			serveraddr = isc_commandline_argument;
Packit 5ce601
			if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 &&
Packit 5ce601
			    inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1)
Packit 5ce601
				fatal("-s should be an IPv4 or IPv6 address");
Packit 5ce601
			break;
Packit 5ce601
		case 't':
Packit 5ce601
			chrootdir = isc_commandline_argument;
Packit 5ce601
			break;
Packit 5ce601
		case 'u':
Packit 5ce601
			user = isc_commandline_argument;
Packit 5ce601
			break;
Packit 5ce601
		case 'V':
Packit 5ce601
			verbose = true;
Packit 5ce601
			break;
Packit 5ce601
		case '?':
Packit 5ce601
			if (isc_commandline_option != '?') {
Packit 5ce601
				fprintf(stderr, "%s: invalid argument -%c\n",
Packit 5ce601
					program, isc_commandline_option);
Packit 5ce601
				usage(1);
Packit 5ce601
			} else
Packit 5ce601
				usage(0);
Packit 5ce601
			break;
Packit 5ce601
		default:
Packit 5ce601
			fprintf(stderr, "%s: unhandled option -%c\n",
Packit 5ce601
				program, isc_commandline_option);
Packit 5ce601
			exit(1);
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	argc -= isc_commandline_index;
Packit 5ce601
	argv += isc_commandline_index;
Packit 5ce601
	POST(argv);
Packit 5ce601
Packit 5ce601
	if (argc > 0)
Packit 5ce601
		usage(1);
Packit 5ce601
Packit 5ce601
	if (keysize < 0)
Packit 5ce601
		keysize = alg_bits(alg);
Packit 5ce601
	algname = alg_totext(alg);
Packit 5ce601
Packit 5ce601
	DO("create memory context", isc_mem_create(0, 0, &mctx));
Packit 5ce601
	isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
Packit 5ce601
Packit 5ce601
	generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
Packit 5ce601
Packit 5ce601
	if (keyonly) {
Packit 5ce601
		write_key_file(keyfile, chrootdir == NULL ? user : NULL,
Packit 5ce601
			       keyname, &key_txtbuffer, alg);
Packit 5ce601
Packit 5ce601
		if (chrootdir != NULL) {
Packit 5ce601
			char *buf;
Packit 5ce601
			len = strlen(chrootdir) + strlen(keyfile) + 2;
Packit 5ce601
			buf = isc_mem_get(mctx, len);
Packit 5ce601
			if (buf == NULL)
Packit 5ce601
				fatal("isc_mem_get(%d) failed\n", len);
Packit 5ce601
			snprintf(buf, len, "%s%s%s", chrootdir,
Packit 5ce601
				 (*keyfile != '/') ? "/" : "", keyfile);
Packit 5ce601
Packit 5ce601
			write_key_file(buf, user, keyname, &key_txtbuffer, alg);
Packit 5ce601
			isc_mem_put(mctx, buf, len);
Packit 5ce601
		}
Packit 5ce601
	} else {
Packit 5ce601
		printf("\
Packit 5ce601
# Start of rndc.conf\n\
Packit 5ce601
key \"%s\" {\n\
Packit 5ce601
	algorithm %s;\n\
Packit 5ce601
	secret \"%.*s\";\n\
Packit 5ce601
};\n\
Packit 5ce601
\n\
Packit 5ce601
options {\n\
Packit 5ce601
	default-key \"%s\";\n\
Packit 5ce601
	default-server %s;\n\
Packit 5ce601
	default-port %d;\n\
Packit 5ce601
};\n\
Packit 5ce601
# End of rndc.conf\n\
Packit 5ce601
\n\
Packit 5ce601
# Use with the following in named.conf, adjusting the allow list as needed:\n\
Packit 5ce601
# key \"%s\" {\n\
Packit 5ce601
# 	algorithm %s;\n\
Packit 5ce601
# 	secret \"%.*s\";\n\
Packit 5ce601
# };\n\
Packit 5ce601
# \n\
Packit 5ce601
# controls {\n\
Packit 5ce601
# 	inet %s port %d\n\
Packit 5ce601
# 		allow { %s; } keys { \"%s\"; };\n\
Packit 5ce601
# };\n\
Packit 5ce601
# End of named.conf\n",
Packit 5ce601
		       keyname, algname,
Packit 5ce601
		       (int)isc_buffer_usedlength(&key_txtbuffer),
Packit 5ce601
		       (char *)isc_buffer_base(&key_txtbuffer),
Packit 5ce601
		       keyname, serveraddr, port,
Packit 5ce601
		       keyname, algname,
Packit 5ce601
		       (int)isc_buffer_usedlength(&key_txtbuffer),
Packit 5ce601
		       (char *)isc_buffer_base(&key_txtbuffer),
Packit 5ce601
		       serveraddr, port, serveraddr, keyname);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (show_final_mem)
Packit 5ce601
		isc_mem_stats(mctx, stderr);
Packit 5ce601
Packit 5ce601
	isc_mem_destroy(&mctx);
Packit 5ce601
Packit 5ce601
	return (0);
Packit 5ce601
}