]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/bin/confgen/ddns-confgen.c
Update BIND to 9.9.6-P1
[FreeBSD/stable/9.git] / contrib / bind9 / bin / confgen / ddns-confgen.c
1 /*
2  * Copyright (C) 2009, 2011, 2014  Internet Systems Consortium, Inc. ("ISC")
3  *
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.
7  *
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.
15  */
16
17 /* $Id: ddns-confgen.c,v 1.11 2011/03/12 04:59:46 tbox Exp $ */
18
19 /*! \file */
20
21 /**
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.
25  */
26
27 #include <config.h>
28
29 #include <stdlib.h>
30 #include <stdarg.h>
31
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>
37 #include <isc/file.h>
38 #include <isc/keyboard.h>
39 #include <isc/mem.h>
40 #include <isc/net.h>
41 #include <isc/print.h>
42 #include <isc/result.h>
43 #include <isc/string.h>
44 #include <isc/time.h>
45 #include <isc/util.h>
46
47 #include <dns/keyvalues.h>
48 #include <dns/name.h>
49 #include <dns/result.h>
50
51 #include <dst/dst.h>
52 #include <confgen/os.h>
53
54 #include "util.h"
55 #include "keygen.h"
56
57 #define DEFAULT_KEYNAME         "ddns-key"
58
59 static char program[256];
60 const char *progname;
61
62 isc_boolean_t verbose = ISC_FALSE;
63
64 ISC_PLATFORM_NORETURN_PRE static void
65 usage(int status) ISC_PLATFORM_NORETURN_POST;
66
67 static void
68 usage(int status) {
69
70         fprintf(stderr, "\
71 Usage:\n\
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",
79                  progname);
80
81         exit (status);
82 }
83
84 int
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;
96         char *keybuf = NULL;
97         dns_secalg_t alg = DST_ALG_HMACSHA256;
98         const char *algname = alg_totext(alg);
99         int keysize = 256;
100         int len = 0;
101         int ch;
102
103         dns_result_register();
104
105         result = isc_file_progname(*argv, program, sizeof(program));
106         if (result != ISC_R_SUCCESS)
107                 memmove(program, "ddns-confgen", 13);
108         progname = program;
109
110         isc_commandline_errprint = ISC_FALSE;
111
112         while ((ch = isc_commandline_parse(argc, argv,
113                                            "a:hk:Mmr:qs:Vy:z:")) != -1) {
114                 switch (ch) {
115                 case 'a':
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);
121                         break;
122                 case 'h':
123                         usage(0);
124                 case 'k':
125                 case 'y':
126                         keyname = isc_commandline_argument;
127                         break;
128                 case 'M':
129                         isc_mem_debugging = ISC_MEM_DEBUGTRACE;
130                         break;
131                 case 'm':
132                         show_final_mem = ISC_TRUE;
133                         break;
134                 case 'q':
135                         quiet = ISC_TRUE;
136                         break;
137                 case 'r':
138                         randomfile = isc_commandline_argument;
139                         break;
140                 case 's':
141                         self_domain = isc_commandline_argument;
142                         break;
143                 case 'V':
144                         verbose = ISC_TRUE;
145                         break;
146                 case 'z':
147                         zone = isc_commandline_argument;
148                         break;
149                 case '?':
150                         if (isc_commandline_option != '?') {
151                                 fprintf(stderr, "%s: invalid argument -%c\n",
152                                         program, isc_commandline_option);
153                                 usage(1);
154                         } else
155                                 usage(0);
156                         break;
157                 default:
158                         fprintf(stderr, "%s: unhandled option -%c\n",
159                                 program, isc_commandline_option);
160                         exit(1);
161                 }
162         }
163
164         argc -= isc_commandline_index;
165         argv += isc_commandline_index;
166         POST(argv);
167
168         if (self_domain != NULL && zone != NULL)
169                 usage(1);       /* -s and -z cannot coexist */
170
171         if (argc > 0)
172                 usage(1);
173
174         DO("create memory context", isc_mem_create(0, 0, &mctx));
175
176         if (keyname == NULL) {
177                 const char *suffix = NULL;
178
179                 keyname = DEFAULT_KEYNAME;
180                 if (self_domain != NULL)
181                         suffix = self_domain;
182                 else if (zone != NULL)
183                         suffix = zone;
184                 if (suffix != NULL) {
185                         len = strlen(keyname) + strlen(suffix) + 2;
186                         keybuf = isc_mem_get(mctx, len);
187                         if (keybuf == NULL)
188                                 fatal("failed to allocate memory for keyname");
189                         snprintf(keybuf, len, "%s.%s", keyname, suffix);
190                         keyname = (const char *) keybuf;
191                 }
192         }
193
194         isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
195
196         generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
197
198
199         if (!quiet)
200                 printf("\
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\
203 # will be run:\n");
204
205         printf("\
206 key \"%s\" {\n\
207         algorithm %s;\n\
208         secret \"%.*s\";\n\
209 };\n",
210                keyname, algname,
211                (int)isc_buffer_usedlength(&key_txtbuffer),
212                (char *)isc_buffer_base(&key_txtbuffer));
213
214         if (!quiet) {
215                 if (self_domain != NULL) {
216                         printf("\n\
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\
220 update-policy {\n\
221           grant %s name %s ANY;\n\
222 };\n",
223                                self_domain, keyname, self_domain);
224                 } else if (zone != NULL) {
225                         printf("\n\
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\
229 update-policy {\n\
230           grant %s zonesub ANY;\n\
231 };\n",
232                                zone, keyname);
233                 } else {
234                         printf("\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\
239 update-policy {\n\
240         grant %s zonesub ANY;\n\
241 };\n",
242                                keyname);
243                 }
244
245                 printf("\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");
249
250         }
251
252         if (keybuf != NULL)
253                 isc_mem_put(mctx, keybuf, len);
254
255         if (show_final_mem)
256                 isc_mem_stats(mctx, stderr);
257
258         isc_mem_destroy(&mctx);
259
260         return (0);
261 }