2 * Copyright (C) 2009, 2011 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.9.308.2 2011-03-12 04:59:13 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>
51 #include <confgen/os.h>
56 #define DEFAULT_KEYNAME "ddns-key"
58 static char program[256];
61 isc_boolean_t verbose = ISC_FALSE;
63 ISC_PLATFORM_NORETURN_PRE static void
64 usage(int status) ISC_PLATFORM_NORETURN_POST;
71 %s [-a alg] [-k keyname] [-r randomfile] [-q] [-s name | -z zone]\n\
72 -a alg: algorithm (default hmac-sha256)\n\
73 -k keyname: name of the key as it will be used in named.conf\n\
74 -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
75 -s name: domain name to be updated using the created key\n\
76 -z zone: name of the zone as it will be used in named.conf\n\
77 -q: quiet mode: print the key, with no explanatory text\n",
84 main(int argc, char **argv) {
85 isc_boolean_t show_final_mem = ISC_FALSE;
86 isc_boolean_t quiet = ISC_FALSE;
87 isc_buffer_t key_txtbuffer;
88 char key_txtsecret[256];
89 isc_mem_t *mctx = NULL;
90 isc_result_t result = ISC_R_SUCCESS;
91 const char *randomfile = NULL;
92 const char *keyname = NULL;
93 const char *zone = NULL;
94 const char *self_domain = NULL;
96 dns_secalg_t alg = DST_ALG_HMACSHA256;
97 const char *algname = alg_totext(alg);
102 result = isc_file_progname(*argv, program, sizeof(program));
103 if (result != ISC_R_SUCCESS)
104 memcpy(program, "ddns-confgen", 13);
107 isc_commandline_errprint = ISC_FALSE;
109 while ((ch = isc_commandline_parse(argc, argv,
110 "a:hk:Mmr:qs:Vy:z:")) != -1) {
113 algname = isc_commandline_argument;
114 alg = alg_fromtext(algname);
115 if (alg == DST_ALG_UNKNOWN)
116 fatal("Unsupported algorithm '%s'", algname);
117 keysize = alg_bits(alg);
123 keyname = isc_commandline_argument;
126 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
129 show_final_mem = ISC_TRUE;
135 randomfile = isc_commandline_argument;
138 self_domain = isc_commandline_argument;
144 zone = isc_commandline_argument;
147 if (isc_commandline_option != '?') {
148 fprintf(stderr, "%s: invalid argument -%c\n",
149 program, isc_commandline_option);
155 fprintf(stderr, "%s: unhandled option -%c\n",
156 program, isc_commandline_option);
161 argc -= isc_commandline_index;
162 argv += isc_commandline_index;
165 if (self_domain != NULL && zone != NULL)
166 usage(1); /* -s and -z cannot coexist */
171 DO("create memory context", isc_mem_create(0, 0, &mctx));
173 if (keyname == NULL) {
174 const char *suffix = NULL;
176 keyname = DEFAULT_KEYNAME;
177 if (self_domain != NULL)
178 suffix = self_domain;
179 else if (zone != NULL)
181 if (suffix != NULL) {
182 len = strlen(keyname) + strlen(suffix) + 2;
183 keybuf = isc_mem_get(mctx, len);
185 fatal("failed to allocate memory for keyname");
186 snprintf(keybuf, len, "%s.%s", keyname, suffix);
187 keyname = (const char *) keybuf;
191 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
193 generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
198 # To activate this key, place the following in named.conf, and\n\
199 # in a separate keyfile on the system or systems from which nsupdate\n\
208 (int)isc_buffer_usedlength(&key_txtbuffer),
209 (char *)isc_buffer_base(&key_txtbuffer));
212 if (self_domain != NULL) {
214 # Then, in the \"zone\" statement for the zone containing the\n\
215 # name \"%s\", place an \"update-policy\" statement\n\
216 # like this one, adjusted as needed for your preferred permissions:\n\
218 grant %s name %s ANY;\n\
220 self_domain, keyname, self_domain);
221 } else if (zone != NULL) {
223 # Then, in the \"zone\" definition statement for \"%s\",\n\
224 # place an \"update-policy\" statement like this one, adjusted as \n\
225 # needed for your preferred permissions:\n\
227 grant %s zonesub ANY;\n\
232 # Then, in the \"zone\" statement for each zone you wish to dynamically\n\
233 # update, place an \"update-policy\" statement granting update permission\n\
234 # to this key. For example, the following statement grants this key\n\
235 # permission to update any name within the zone:\n\
237 grant %s zonesub ANY;\n\
243 # After the keyfile has been placed, the following command will\n\
244 # execute nsupdate using this key:\n\
245 nsupdate -k <keyfile>\n");
250 isc_mem_put(mctx, keybuf, len);
253 isc_mem_stats(mctx, stderr);
255 isc_mem_destroy(&mctx);