]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bind9/bin/confgen/ddns-confgen.c
Upgrade to version 9.8.0-P4
[FreeBSD/FreeBSD.git] / contrib / bind9 / bin / confgen / ddns-confgen.c
1 /*
2  * Copyright (C) 2009  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.9 2009-09-29 15:06:05 fdupont 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
50 #include <dst/dst.h>
51 #include <confgen/os.h>
52
53 #include "util.h"
54 #include "keygen.h"
55
56 #define DEFAULT_KEYNAME         "ddns-key"
57
58 static char program[256];
59 const char *progname;
60
61 isc_boolean_t verbose = ISC_FALSE;
62
63 ISC_PLATFORM_NORETURN_PRE static void
64 usage(int status) ISC_PLATFORM_NORETURN_POST;
65
66 static void
67 usage(int status) {
68
69         fprintf(stderr, "\
70 Usage:\n\
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",
78                  progname);
79
80         exit (status);
81 }
82
83 int
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;
95         char *keybuf = NULL;
96         dns_secalg_t alg = DST_ALG_HMACSHA256;
97         const char *algname = alg_totext(alg);
98         int keysize = 256;
99         int len = 0;
100         int ch;
101
102         result = isc_file_progname(*argv, program, sizeof(program));
103         if (result != ISC_R_SUCCESS)
104                 memcpy(program, "ddns-confgen", 13);
105         progname = program;
106
107         isc_commandline_errprint = ISC_FALSE;
108
109         while ((ch = isc_commandline_parse(argc, argv,
110                                            "a:hk:Mmr:qs:Vy:z:")) != -1) {
111                 switch (ch) {
112                 case 'a':
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);
118                         break;
119                 case 'h':
120                         usage(0);
121                 case 'k':
122                 case 'y':
123                         keyname = isc_commandline_argument;
124                         break;
125                 case 'M':
126                         isc_mem_debugging = ISC_MEM_DEBUGTRACE;
127                         break;
128                 case 'm':
129                         show_final_mem = ISC_TRUE;
130                         break;
131                 case 'q':
132                         quiet = ISC_TRUE;
133                         break;
134                 case 'r':
135                         randomfile = isc_commandline_argument;
136                         break;
137                 case 's':
138                         self_domain = isc_commandline_argument;
139                         break;
140                 case 'V':
141                         verbose = ISC_TRUE;
142                         break;
143                 case 'z':
144                         zone = isc_commandline_argument;
145                         break;
146                 case '?':
147                         if (isc_commandline_option != '?') {
148                                 fprintf(stderr, "%s: invalid argument -%c\n",
149                                         program, isc_commandline_option);
150                                 usage(1);
151                         } else
152                                 usage(0);
153                         break;
154                 default:
155                         fprintf(stderr, "%s: unhandled option -%c\n",
156                                 program, isc_commandline_option);
157                         exit(1);
158                 }
159         }
160
161         argc -= isc_commandline_index;
162         argv += isc_commandline_index;
163
164         if (self_domain != NULL && zone != NULL)
165                 usage(1);       /* -s and -z cannot coexist */
166
167         if (argc > 0)
168                 usage(1);
169
170         DO("create memory context", isc_mem_create(0, 0, &mctx));
171
172         if (keyname == NULL) {
173                 const char *suffix = NULL;
174
175                 keyname = DEFAULT_KEYNAME;
176                 if (self_domain != NULL)
177                         suffix = self_domain;
178                 else if (zone != NULL)
179                         suffix = zone;
180                 if (suffix != NULL) {
181                         len = strlen(keyname) + strlen(suffix) + 2;
182                         keybuf = isc_mem_get(mctx, len);
183                         if (keybuf == NULL)
184                                 fatal("failed to allocate memory for keyname");
185                         snprintf(keybuf, len, "%s.%s", keyname, suffix);
186                         keyname = (const char *) keybuf;
187                 }
188         }
189
190         isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
191
192         generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
193
194
195         if (!quiet)
196                 printf("\
197 # To activate this key, place the following in named.conf, and\n\
198 # in a separate keyfile on the system or systems from which nsupdate\n\
199 # will be run:\n");
200
201         printf("\
202 key \"%s\" {\n\
203         algorithm %s;\n\
204         secret \"%.*s\";\n\
205 };\n",
206                keyname, algname,
207                (int)isc_buffer_usedlength(&key_txtbuffer),
208                (char *)isc_buffer_base(&key_txtbuffer));
209
210         if (!quiet) {
211                 if (self_domain != NULL) {
212                         printf("\n\
213 # Then, in the \"zone\" statement for the zone containing the\n\
214 # name \"%s\", place an \"update-policy\" statement\n\
215 # like this one, adjusted as needed for your preferred permissions:\n\
216 update-policy {\n\
217           grant %s name %s ANY;\n\
218 };\n",
219                                self_domain, keyname, self_domain);
220                 } else if (zone != NULL) {
221                         printf("\n\
222 # Then, in the \"zone\" definition statement for \"%s\",\n\
223 # place an \"update-policy\" statement like this one, adjusted as \n\
224 # needed for your preferred permissions:\n\
225 update-policy {\n\
226           grant %s zonesub ANY;\n\
227 };\n",
228                                zone, keyname);
229                 } else {
230                         printf("\n\
231 # Then, in the \"zone\" statement for each zone you wish to dynamically\n\
232 # update, place an \"update-policy\" statement granting update permission\n\
233 # to this key.  For example, the following statement grants this key\n\
234 # permission to update any name within the zone:\n\
235 update-policy {\n\
236         grant %s zonesub ANY;\n\
237 };\n",
238                                keyname);
239                 }
240
241                 printf("\n\
242 # After the keyfile has been placed, the following command will\n\
243 # execute nsupdate using this key:\n\
244 nsupdate -k <keyfile>\n");
245
246         }
247
248         if (keybuf != NULL)
249                 isc_mem_put(mctx, keybuf, len);
250
251         if (show_final_mem)
252                 isc_mem_stats(mctx, stderr);
253
254         isc_mem_destroy(&mctx);
255
256         return (0);
257 }