No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / confgen / ddns-confgen.c
blob98c5209fca419707502ee2707f6c928d0273a843
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: ddns-confgen.c,v 1.9 2009/09/29 15:06:05 fdupont Exp */
21 /*! \file */
23 /**
24 * ddns-confgen generates configuration files for dynamic DNS. It can
25 * be used as a convenient alternative to writing the ddns.key file
26 * and the corresponding key and update-policy statements in named.conf.
29 #include <config.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
34 #include <isc/assertions.h>
35 #include <isc/base64.h>
36 #include <isc/buffer.h>
37 #include <isc/commandline.h>
38 #include <isc/entropy.h>
39 #include <isc/file.h>
40 #include <isc/keyboard.h>
41 #include <isc/mem.h>
42 #include <isc/net.h>
43 #include <isc/print.h>
44 #include <isc/result.h>
45 #include <isc/string.h>
46 #include <isc/time.h>
47 #include <isc/util.h>
49 #include <dns/keyvalues.h>
50 #include <dns/name.h>
52 #include <dst/dst.h>
53 #include <confgen/os.h>
55 #include "util.h"
56 #include "keygen.h"
58 #define DEFAULT_KEYNAME "ddns-key"
60 static char program[256];
61 const char *progname;
63 isc_boolean_t verbose = ISC_FALSE;
65 ISC_PLATFORM_NORETURN_PRE static void
66 usage(int status) ISC_PLATFORM_NORETURN_POST;
68 static void
69 usage(int status) {
71 fprintf(stderr, "\
72 Usage:\n\
73 %s [-a alg] [-k keyname] [-r randomfile] [-q] [-s name | -z zone]\n\
74 -a alg: algorithm (default hmac-sha256)\n\
75 -k keyname: name of the key as it will be used in named.conf\n\
76 -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
77 -s name: domain name to be updated using the created key\n\
78 -z zone: name of the zone as it will be used in named.conf\n\
79 -q: quiet mode: print the key, with no explanatory text\n",
80 progname);
82 exit (status);
85 int
86 main(int argc, char **argv) {
87 isc_boolean_t show_final_mem = ISC_FALSE;
88 isc_boolean_t quiet = ISC_FALSE;
89 isc_buffer_t key_txtbuffer;
90 char key_txtsecret[256];
91 isc_mem_t *mctx = NULL;
92 isc_result_t result = ISC_R_SUCCESS;
93 const char *randomfile = NULL;
94 const char *keyname = NULL;
95 const char *zone = NULL;
96 const char *self_domain = NULL;
97 char *keybuf = NULL;
98 dns_secalg_t alg = DST_ALG_HMACSHA256;
99 const char *algname = alg_totext(alg);
100 int keysize = 256;
101 int len = 0;
102 int ch;
104 result = isc_file_progname(*argv, program, sizeof(program));
105 if (result != ISC_R_SUCCESS)
106 memcpy(program, "ddns-confgen", 13);
107 progname = program;
109 isc_commandline_errprint = ISC_FALSE;
111 while ((ch = isc_commandline_parse(argc, argv,
112 "a:hk:Mmr:qs:Vy:z:")) != -1) {
113 switch (ch) {
114 case 'a':
115 algname = isc_commandline_argument;
116 alg = alg_fromtext(algname);
117 if (alg == DST_ALG_UNKNOWN)
118 fatal("Unsupported algorithm '%s'", algname);
119 keysize = alg_bits(alg);
120 break;
121 case 'h':
122 usage(0);
123 case 'k':
124 case 'y':
125 keyname = isc_commandline_argument;
126 break;
127 case 'M':
128 isc_mem_debugging = ISC_MEM_DEBUGTRACE;
129 break;
130 case 'm':
131 show_final_mem = ISC_TRUE;
132 break;
133 case 'q':
134 quiet = ISC_TRUE;
135 break;
136 case 'r':
137 randomfile = isc_commandline_argument;
138 break;
139 case 's':
140 self_domain = isc_commandline_argument;
141 break;
142 case 'V':
143 verbose = ISC_TRUE;
144 break;
145 case 'z':
146 zone = isc_commandline_argument;
147 break;
148 case '?':
149 if (isc_commandline_option != '?') {
150 fprintf(stderr, "%s: invalid argument -%c\n",
151 program, isc_commandline_option);
152 usage(1);
153 } else
154 usage(0);
155 break;
156 default:
157 fprintf(stderr, "%s: unhandled option -%c\n",
158 program, isc_commandline_option);
159 exit(1);
163 argc -= isc_commandline_index;
164 argv += isc_commandline_index;
166 if (self_domain != NULL && zone != NULL)
167 usage(1); /* -s and -z cannot coexist */
169 if (argc > 0)
170 usage(1);
172 DO("create memory context", isc_mem_create(0, 0, &mctx));
174 if (keyname == NULL) {
175 const char *suffix = NULL;
177 keyname = DEFAULT_KEYNAME;
178 if (self_domain != NULL)
179 suffix = self_domain;
180 else if (zone != NULL)
181 suffix = zone;
182 if (suffix != NULL) {
183 len = strlen(keyname) + strlen(suffix) + 2;
184 keybuf = isc_mem_get(mctx, len);
185 if (keybuf == NULL)
186 fatal("failed to allocate memory for keyname");
187 snprintf(keybuf, len, "%s.%s", keyname, suffix);
188 keyname = (const char *) keybuf;
192 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
194 generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
197 if (!quiet)
198 printf("\
199 # To activate this key, place the following in named.conf, and\n\
200 # in a separate keyfile on the system or systems from which nsupdate\n\
201 # will be run:\n");
203 printf("\
204 key \"%s\" {\n\
205 algorithm %s;\n\
206 secret \"%.*s\";\n\
207 };\n",
208 keyname, algname,
209 (int)isc_buffer_usedlength(&key_txtbuffer),
210 (char *)isc_buffer_base(&key_txtbuffer));
212 if (!quiet) {
213 if (self_domain != NULL) {
214 printf("\n\
215 # Then, in the \"zone\" statement for the zone containing the\n\
216 # name \"%s\", place an \"update-policy\" statement\n\
217 # like this one, adjusted as needed for your preferred permissions:\n\
218 update-policy {\n\
219 grant %s name %s ANY;\n\
220 };\n",
221 self_domain, keyname, self_domain);
222 } else if (zone != NULL) {
223 printf("\n\
224 # Then, in the \"zone\" definition statement for \"%s\",\n\
225 # place an \"update-policy\" statement like this one, adjusted as \n\
226 # needed for your preferred permissions:\n\
227 update-policy {\n\
228 grant %s zonesub ANY;\n\
229 };\n",
230 zone, keyname);
231 } else {
232 printf("\n\
233 # Then, in the \"zone\" statement for each zone you wish to dynamically\n\
234 # update, place an \"update-policy\" statement granting update permission\n\
235 # to this key. For example, the following statement grants this key\n\
236 # permission to update any name within the zone:\n\
237 update-policy {\n\
238 grant %s zonesub ANY;\n\
239 };\n",
240 keyname);
243 printf("\n\
244 # After the keyfile has been placed, the following command will\n\
245 # execute nsupdate using this key:\n\
246 nsupdate -k <keyfile>\n");
250 if (keybuf != NULL)
251 isc_mem_put(mctx, keybuf, len);
253 if (show_final_mem)
254 isc_mem_stats(mctx, stderr);
256 isc_mem_destroy(&mctx);
258 return (0);