2 * Copyright (C) 2009, 2011, 2014 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: ddns-confgen.c,v 1.11 2011/03/12 04:59:46 tbox Exp $ */
22 * ddns-confgen generates configuration files for dynamic DNS. It can
23 * be used as a convenient alternative to writing the ddns.key file
24 * and the corresponding key and update-policy statements in named.conf.
32 #include <isc/assertions.h>
33 #include <isc/base64.h>
34 #include <isc/buffer.h>
35 #include <isc/commandline.h>
36 #include <isc/entropy.h>
38 #include <isc/keyboard.h>
41 #include <isc/print.h>
42 #include <isc/result.h>
43 #include <isc/string.h>
47 #include <dns/keyvalues.h>
49 #include <dns/result.h>
52 #include <confgen/os.h>
57 #define DEFAULT_KEYNAME "ddns-key"
59 static char program[256];
62 isc_boolean_t verbose = ISC_FALSE;
64 ISC_PLATFORM_NORETURN_PRE static void
65 usage(int status) ISC_PLATFORM_NORETURN_POST;
72 %s [-a alg] [-k keyname] [-r randomfile] [-q] [-s name | -z zone]\n\
73 -a alg: algorithm (default hmac-sha256)\n\
74 -k keyname: name of the key as it will be used in named.conf\n\
75 -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
76 -s name: domain name to be updated using the created key\n\
77 -z zone: name of the zone as it will be used in named.conf\n\
78 -q: quiet mode: print the key, with no explanatory text\n",
85 main(int argc, char **argv) {
86 isc_boolean_t show_final_mem = ISC_FALSE;
87 isc_boolean_t quiet = ISC_FALSE;
88 isc_buffer_t key_txtbuffer;
89 char key_txtsecret[256];
90 isc_mem_t *mctx = NULL;
91 isc_result_t result = ISC_R_SUCCESS;
92 const char *randomfile = NULL;
93 const char *keyname = NULL;
94 const char *zone = NULL;
95 const char *self_domain = NULL;
97 dns_secalg_t alg = DST_ALG_HMACSHA256;
98 const char *algname = alg_totext(alg);
103 dns_result_register();
105 result = isc_file_progname(*argv, program, sizeof(program));
106 if (result != ISC_R_SUCCESS)
107 memmove(program, "ddns-confgen", 13);
110 isc_commandline_errprint = ISC_FALSE;
112 while ((ch = isc_commandline_parse(argc, argv,
113 "a:hk:Mmr:qs:Vy:z:")) != -1) {
116 algname = isc_commandline_argument;
117 alg = alg_fromtext(algname);
118 if (alg == DST_ALG_UNKNOWN)
119 fatal("Unsupported algorithm '%s'", algname);
120 keysize = alg_bits(alg);
126 keyname = isc_commandline_argument;
129 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
132 show_final_mem = ISC_TRUE;
138 randomfile = isc_commandline_argument;
141 self_domain = isc_commandline_argument;
147 zone = isc_commandline_argument;
150 if (isc_commandline_option != '?') {
151 fprintf(stderr, "%s: invalid argument -%c\n",
152 program, isc_commandline_option);
158 fprintf(stderr, "%s: unhandled option -%c\n",
159 program, isc_commandline_option);
164 argc -= isc_commandline_index;
165 argv += isc_commandline_index;
168 if (self_domain != NULL && zone != NULL)
169 usage(1); /* -s and -z cannot coexist */
174 DO("create memory context", isc_mem_create(0, 0, &mctx));
176 if (keyname == NULL) {
177 const char *suffix = NULL;
179 keyname = DEFAULT_KEYNAME;
180 if (self_domain != NULL)
181 suffix = self_domain;
182 else if (zone != NULL)
184 if (suffix != NULL) {
185 len = strlen(keyname) + strlen(suffix) + 2;
186 keybuf = isc_mem_get(mctx, len);
188 fatal("failed to allocate memory for keyname");
189 snprintf(keybuf, len, "%s.%s", keyname, suffix);
190 keyname = (const char *) keybuf;
194 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
196 generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
201 # To activate this key, place the following in named.conf, and\n\
202 # in a separate keyfile on the system or systems from which nsupdate\n\
211 (int)isc_buffer_usedlength(&key_txtbuffer),
212 (char *)isc_buffer_base(&key_txtbuffer));
215 if (self_domain != NULL) {
217 # Then, in the \"zone\" statement for the zone containing the\n\
218 # name \"%s\", place an \"update-policy\" statement\n\
219 # like this one, adjusted as needed for your preferred permissions:\n\
221 grant %s name %s ANY;\n\
223 self_domain, keyname, self_domain);
224 } else if (zone != NULL) {
226 # Then, in the \"zone\" definition statement for \"%s\",\n\
227 # place an \"update-policy\" statement like this one, adjusted as \n\
228 # needed for your preferred permissions:\n\
230 grant %s zonesub ANY;\n\
235 # Then, in the \"zone\" statement for each zone you wish to dynamically\n\
236 # update, place an \"update-policy\" statement granting update permission\n\
237 # to this key. For example, the following statement grants this key\n\
238 # permission to update any name within the zone:\n\
240 grant %s zonesub ANY;\n\
246 # After the keyfile has been placed, the following command will\n\
247 # execute nsupdate using this key:\n\
248 nsupdate -k <keyfile>\n");
253 isc_mem_put(mctx, keybuf, len);
256 isc_mem_stats(mctx, stderr);
258 isc_mem_destroy(&mctx);