]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/bin/dnssec/dnssec-settime.c
Update BIND to 9.9.6-P1
[FreeBSD/stable/9.git] / contrib / bind9 / bin / dnssec / dnssec-settime.c
1 /*
2  * Copyright (C) 2009-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 /*! \file */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <time.h>
25
26 #include <isc/buffer.h>
27 #include <isc/commandline.h>
28 #include <isc/entropy.h>
29 #include <isc/file.h>
30 #include <isc/hash.h>
31 #include <isc/mem.h>
32 #include <isc/print.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35
36 #include <dns/keyvalues.h>
37 #include <dns/result.h>
38 #include <dns/log.h>
39
40 #include <dst/dst.h>
41
42 #include "dnssectool.h"
43
44 const char *program = "dnssec-settime";
45 int verbose;
46
47 static isc_mem_t        *mctx = NULL;
48
49 ISC_PLATFORM_NORETURN_PRE static void
50 usage(void) ISC_PLATFORM_NORETURN_POST;
51
52 static void
53 usage(void) {
54         fprintf(stderr, "Usage:\n");
55         fprintf(stderr, "    %s [options] keyfile\n\n", program);
56         fprintf(stderr, "Version: %s\n", VERSION);
57         fprintf(stderr, "General options:\n");
58 #ifdef USE_PKCS11
59         fprintf(stderr, "    -E engine:          specify OpenSSL engine "
60                                                  "(default \"pkcs11\")\n");
61 #else
62         fprintf(stderr, "    -E engine:          specify OpenSSL engine\n");
63 #endif
64         fprintf(stderr, "    -f:                 force update of old-style "
65                                                  "keys\n");
66         fprintf(stderr, "    -K directory:       set key file location\n");
67         fprintf(stderr, "    -L ttl:             set default key TTL\n");
68         fprintf(stderr, "    -v level:           set level of verbosity\n");
69         fprintf(stderr, "    -V:                 print version information\n");
70         fprintf(stderr, "    -h:                 help\n");
71         fprintf(stderr, "Timing options:\n");
72         fprintf(stderr, "    -P date/[+-]offset/none: set/unset key "
73                                                      "publication date\n");
74         fprintf(stderr, "    -A date/[+-]offset/none: set/unset key "
75                                                      "activation date\n");
76         fprintf(stderr, "    -R date/[+-]offset/none: set/unset key "
77                                                      "revocation date\n");
78         fprintf(stderr, "    -I date/[+-]offset/none: set/unset key "
79                                                      "inactivation date\n");
80         fprintf(stderr, "    -D date/[+-]offset/none: set/unset key "
81                                                      "deletion date\n");
82         fprintf(stderr, "Printing options:\n");
83         fprintf(stderr, "    -p C/P/A/R/I/D/all: print a particular time "
84                                                 "value or values\n");
85         fprintf(stderr, "    -u:                 print times in unix epoch "
86                                                 "format\n");
87         fprintf(stderr, "Output:\n");
88         fprintf(stderr, "     K<name>+<alg>+<new id>.key, "
89                              "K<name>+<alg>+<new id>.private\n");
90
91         exit (-1);
92 }
93
94 static void
95 printtime(dst_key_t *key, int type, const char *tag, isc_boolean_t epoch,
96           FILE *stream)
97 {
98         isc_result_t result;
99         const char *output = NULL;
100         isc_stdtime_t when;
101
102         if (tag != NULL)
103                 fprintf(stream, "%s: ", tag);
104
105         result = dst_key_gettime(key, type, &when);
106         if (result == ISC_R_NOTFOUND) {
107                 fprintf(stream, "UNSET\n");
108         } else if (epoch) {
109                 fprintf(stream, "%d\n", (int) when);
110         } else {
111                 time_t time = when;
112                 output = ctime(&time);
113                 fprintf(stream, "%s", output);
114         }
115 }
116
117 int
118 main(int argc, char **argv) {
119         isc_result_t    result;
120 #ifdef USE_PKCS11
121         const char      *engine = "pkcs11";
122 #else
123         const char      *engine = NULL;
124 #endif
125         char            *filename = NULL, *directory = NULL;
126         char            newname[1024];
127         char            keystr[DST_KEY_FORMATSIZE];
128         char            *endp, *p;
129         int             ch;
130         isc_entropy_t   *ectx = NULL;
131         const char      *predecessor = NULL;
132         dst_key_t       *prevkey = NULL;
133         dst_key_t       *key = NULL;
134         isc_buffer_t    buf;
135         dns_name_t      *name = NULL;
136         dns_secalg_t    alg = 0;
137         unsigned int    size = 0;
138         isc_uint16_t    flags = 0;
139         int             prepub = -1;
140         dns_ttl_t       ttl = 0;
141         isc_stdtime_t   now;
142         isc_stdtime_t   pub = 0, act = 0, rev = 0, inact = 0, del = 0;
143         isc_stdtime_t   prevact = 0, previnact = 0, prevdel = 0;
144         isc_boolean_t   setpub = ISC_FALSE, setact = ISC_FALSE;
145         isc_boolean_t   setrev = ISC_FALSE, setinact = ISC_FALSE;
146         isc_boolean_t   setdel = ISC_FALSE, setttl = ISC_FALSE;
147         isc_boolean_t   unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
148         isc_boolean_t   unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
149         isc_boolean_t   unsetdel = ISC_FALSE;
150         isc_boolean_t   printcreate = ISC_FALSE, printpub = ISC_FALSE;
151         isc_boolean_t   printact = ISC_FALSE,  printrev = ISC_FALSE;
152         isc_boolean_t   printinact = ISC_FALSE, printdel = ISC_FALSE;
153         isc_boolean_t   force = ISC_FALSE;
154         isc_boolean_t   epoch = ISC_FALSE;
155         isc_boolean_t   changed = ISC_FALSE;
156         isc_log_t       *log = NULL;
157
158         if (argc == 1)
159                 usage();
160
161         result = isc_mem_create(0, 0, &mctx);
162         if (result != ISC_R_SUCCESS)
163                 fatal("Out of memory");
164
165         setup_logging(verbose, mctx, &log);
166
167         dns_result_register();
168
169         isc_commandline_errprint = ISC_FALSE;
170
171         isc_stdtime_get(&now);
172
173 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:L:P:p:R:S:uv:V"
174         while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
175                 switch (ch) {
176                 case 'E':
177                         engine = isc_commandline_argument;
178                         break;
179                 case 'f':
180                         force = ISC_TRUE;
181                         break;
182                 case 'p':
183                         p = isc_commandline_argument;
184                         if (!strcasecmp(p, "all")) {
185                                 printcreate = ISC_TRUE;
186                                 printpub = ISC_TRUE;
187                                 printact = ISC_TRUE;
188                                 printrev = ISC_TRUE;
189                                 printinact = ISC_TRUE;
190                                 printdel = ISC_TRUE;
191                                 break;
192                         }
193
194                         do {
195                                 switch (*p++) {
196                                 case 'C':
197                                         printcreate = ISC_TRUE;
198                                         break;
199                                 case 'P':
200                                         printpub = ISC_TRUE;
201                                         break;
202                                 case 'A':
203                                         printact = ISC_TRUE;
204                                         break;
205                                 case 'R':
206                                         printrev = ISC_TRUE;
207                                         break;
208                                 case 'I':
209                                         printinact = ISC_TRUE;
210                                         break;
211                                 case 'D':
212                                         printdel = ISC_TRUE;
213                                         break;
214                                 case ' ':
215                                         break;
216                                 default:
217                                         usage();
218                                         break;
219                                 }
220                         } while (*p != '\0');
221                         break;
222                 case 'u':
223                         epoch = ISC_TRUE;
224                         break;
225                 case 'K':
226                         /*
227                          * We don't have to copy it here, but do it to
228                          * simplify cleanup later
229                          */
230                         directory = isc_mem_strdup(mctx,
231                                                    isc_commandline_argument);
232                         if (directory == NULL) {
233                                 fatal("Failed to allocate memory for "
234                                       "directory");
235                         }
236                         break;
237                 case 'L':
238                         ttl = strtottl(isc_commandline_argument);
239                         setttl = ISC_TRUE;
240                         break;
241                 case 'v':
242                         verbose = strtol(isc_commandline_argument, &endp, 0);
243                         if (*endp != '\0')
244                                 fatal("-v must be followed by a number");
245                         break;
246                 case 'P':
247                         if (setpub || unsetpub)
248                                 fatal("-P specified more than once");
249
250                         changed = ISC_TRUE;
251                         pub = strtotime(isc_commandline_argument,
252                                         now, now, &setpub);
253                         unsetpub = !setpub;
254                         break;
255                 case 'A':
256                         if (setact || unsetact)
257                                 fatal("-A specified more than once");
258
259                         changed = ISC_TRUE;
260                         act = strtotime(isc_commandline_argument,
261                                         now, now, &setact);
262                         unsetact = !setact;
263                         break;
264                 case 'R':
265                         if (setrev || unsetrev)
266                                 fatal("-R specified more than once");
267
268                         changed = ISC_TRUE;
269                         rev = strtotime(isc_commandline_argument,
270                                         now, now, &setrev);
271                         unsetrev = !setrev;
272                         break;
273                 case 'I':
274                         if (setinact || unsetinact)
275                                 fatal("-I specified more than once");
276
277                         changed = ISC_TRUE;
278                         inact = strtotime(isc_commandline_argument,
279                                         now, now, &setinact);
280                         unsetinact = !setinact;
281                         break;
282                 case 'D':
283                         if (setdel || unsetdel)
284                                 fatal("-D specified more than once");
285
286                         changed = ISC_TRUE;
287                         del = strtotime(isc_commandline_argument,
288                                         now, now, &setdel);
289                         unsetdel = !setdel;
290                         break;
291                 case 'S':
292                         predecessor = isc_commandline_argument;
293                         break;
294                 case 'i':
295                         prepub = strtottl(isc_commandline_argument);
296                         break;
297                 case '?':
298                         if (isc_commandline_option != '?')
299                                 fprintf(stderr, "%s: invalid argument -%c\n",
300                                         program, isc_commandline_option);
301                         /* Falls into */
302                 case 'h':
303                         /* Does not return. */
304                         usage();
305
306                 case 'V':
307                         /* Does not return. */
308                         version(program);
309
310                 default:
311                         fprintf(stderr, "%s: unhandled option -%c\n",
312                                 program, isc_commandline_option);
313                         exit(1);
314                 }
315         }
316
317         if (argc < isc_commandline_index + 1 ||
318             argv[isc_commandline_index] == NULL)
319                 fatal("The key file name was not specified");
320         if (argc > isc_commandline_index + 1)
321                 fatal("Extraneous arguments");
322
323         if (ectx == NULL)
324                 setup_entropy(mctx, NULL, &ectx);
325         result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
326         if (result != ISC_R_SUCCESS)
327                 fatal("Could not initialize hash");
328         result = dst_lib_init2(mctx, ectx, engine,
329                                ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
330         if (result != ISC_R_SUCCESS)
331                 fatal("Could not initialize dst: %s",
332                       isc_result_totext(result));
333         isc_entropy_stopcallbacksources(ectx);
334
335         if (predecessor != NULL) {
336                 char keystr[DST_KEY_FORMATSIZE];
337                 int major, minor;
338
339                 if (prepub == -1)
340                         prepub = (30 * 86400);
341
342                 if (setpub || unsetpub)
343                         fatal("-S and -P cannot be used together");
344                 if (setact || unsetact)
345                         fatal("-S and -A cannot be used together");
346
347                 result = dst_key_fromnamedfile(predecessor, directory,
348                                                DST_TYPE_PUBLIC |
349                                                DST_TYPE_PRIVATE,
350                                                mctx, &prevkey);
351                 if (result != ISC_R_SUCCESS)
352                         fatal("Invalid keyfile %s: %s",
353                               filename, isc_result_totext(result));
354                 if (!dst_key_isprivate(prevkey) && !dst_key_isexternal(prevkey))
355                         fatal("%s is not a private key", filename);
356
357                 name = dst_key_name(prevkey);
358                 alg = dst_key_alg(prevkey);
359                 size = dst_key_size(prevkey);
360                 flags = dst_key_flags(prevkey);
361
362                 dst_key_format(prevkey, keystr, sizeof(keystr));
363                 dst_key_getprivateformat(prevkey, &major, &minor);
364                 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
365                         fatal("Predecessor has incompatible format "
366                               "version %d.%d\n\t", major, minor);
367
368                 result = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &prevact);
369                 if (result != ISC_R_SUCCESS)
370                         fatal("Predecessor has no activation date. "
371                               "You must set one before\n\t"
372                               "generating a successor.");
373
374                 result = dst_key_gettime(prevkey, DST_TIME_INACTIVE,
375                                          &previnact);
376                 if (result != ISC_R_SUCCESS)
377                         fatal("Predecessor has no inactivation date. "
378                               "You must set one before\n\t"
379                               "generating a successor.");
380
381                 pub = prevact - prepub;
382                 if (pub < now && prepub != 0)
383                         fatal("Predecessor will become inactive before the\n\t"
384                               "prepublication period ends.  Either change "
385                               "its inactivation date,\n\t"
386                               "or use the -i option to set a shorter "
387                               "prepublication interval.");
388
389                 result = dst_key_gettime(prevkey, DST_TIME_DELETE, &prevdel);
390                 if (result != ISC_R_SUCCESS)
391                         fprintf(stderr, "%s: warning: Predecessor has no "
392                                         "removal date;\n\t"
393                                         "it will remain in the zone "
394                                         "indefinitely after rollover.\n",
395                                         program);
396                 else if (prevdel < previnact)
397                         fprintf(stderr, "%s: warning: Predecessor is "
398                                         "scheduled to be deleted\n\t"
399                                         "before it is scheduled to be "
400                                         "inactive.\n", program);
401
402                 changed = setpub = setact = ISC_TRUE;
403                 dst_key_free(&prevkey);
404         } else {
405                 if (prepub < 0)
406                         prepub = 0;
407
408                 if (prepub > 0) {
409                         if (setpub && setact && (act - prepub) < pub)
410                                 fatal("Activation and publication dates "
411                                       "are closer together than the\n\t"
412                                       "prepublication interval.");
413
414                         if (setpub && !setact) {
415                                 setact = ISC_TRUE;
416                                 act = pub + prepub;
417                         } else if (setact && !setpub) {
418                                 setpub = ISC_TRUE;
419                                 pub = act - prepub;
420                         }
421
422                         if ((act - prepub) < now)
423                                 fatal("Time until activation is shorter "
424                                       "than the\n\tprepublication interval.");
425                 }
426         }
427
428         if (directory != NULL) {
429                 filename = argv[isc_commandline_index];
430         } else {
431                 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
432                                             &directory, &filename);
433                 if (result != ISC_R_SUCCESS)
434                         fatal("cannot process filename %s: %s",
435                               argv[isc_commandline_index],
436                               isc_result_totext(result));
437         }
438
439         result = dst_key_fromnamedfile(filename, directory,
440                                        DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
441                                        mctx, &key);
442         if (result != ISC_R_SUCCESS)
443                 fatal("Invalid keyfile %s: %s",
444                       filename, isc_result_totext(result));
445
446         if (!dst_key_isprivate(key) && !dst_key_isexternal(key))
447                 fatal("%s is not a private key", filename);
448
449         dst_key_format(key, keystr, sizeof(keystr));
450
451         if (predecessor != NULL) {
452                 if (!dns_name_equal(name, dst_key_name(key)))
453                         fatal("Key name mismatch");
454                 if (alg != dst_key_alg(key))
455                         fatal("Key algorithm mismatch");
456                 if (size != dst_key_size(key))
457                         fatal("Key size mismatch");
458                 if (flags != dst_key_flags(key))
459                         fatal("Key flags mismatch");
460         }
461
462         prevdel = previnact = 0;
463         if ((setdel && setinact && del < inact) ||
464             (dst_key_gettime(key, DST_TIME_INACTIVE,
465                              &previnact) == ISC_R_SUCCESS &&
466              setdel && !setinact && del < previnact) ||
467             (dst_key_gettime(key, DST_TIME_DELETE,
468                              &prevdel) == ISC_R_SUCCESS &&
469              setinact && !setdel && prevdel < inact) ||
470             (!setdel && !setinact && prevdel < previnact))
471                 fprintf(stderr, "%s: warning: Key is scheduled to "
472                                 "be deleted before it is\n\t"
473                                 "scheduled to be inactive.\n",
474                         program);
475
476         if (force)
477                 set_keyversion(key);
478         else
479                 check_keyversion(key, keystr);
480
481         if (verbose > 2)
482                 fprintf(stderr, "%s: %s\n", program, keystr);
483
484         /*
485          * Set time values.
486          */
487         if (setpub)
488                 dst_key_settime(key, DST_TIME_PUBLISH, pub);
489         else if (unsetpub)
490                 dst_key_unsettime(key, DST_TIME_PUBLISH);
491
492         if (setact)
493                 dst_key_settime(key, DST_TIME_ACTIVATE, act);
494         else if (unsetact)
495                 dst_key_unsettime(key, DST_TIME_ACTIVATE);
496
497         if (setrev) {
498                 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
499                         fprintf(stderr, "%s: warning: Key %s is already "
500                                         "revoked; changing the revocation date "
501                                         "will not affect this.\n",
502                                         program, keystr);
503                 if ((dst_key_flags(key) & DNS_KEYFLAG_KSK) == 0)
504                         fprintf(stderr, "%s: warning: Key %s is not flagged as "
505                                         "a KSK, but -R was used.  Revoking a "
506                                         "ZSK is legal, but undefined.\n",
507                                         program, keystr);
508                 dst_key_settime(key, DST_TIME_REVOKE, rev);
509         } else if (unsetrev) {
510                 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
511                         fprintf(stderr, "%s: warning: Key %s is already "
512                                         "revoked; removing the revocation date "
513                                         "will not affect this.\n",
514                                         program, keystr);
515                 dst_key_unsettime(key, DST_TIME_REVOKE);
516         }
517
518         if (setinact)
519                 dst_key_settime(key, DST_TIME_INACTIVE, inact);
520         else if (unsetinact)
521                 dst_key_unsettime(key, DST_TIME_INACTIVE);
522
523         if (setdel)
524                 dst_key_settime(key, DST_TIME_DELETE, del);
525         else if (unsetdel)
526                 dst_key_unsettime(key, DST_TIME_DELETE);
527
528         if (setttl)
529                 dst_key_setttl(key, ttl);
530
531         /*
532          * No metadata changes were made but we're forcing an upgrade
533          * to the new format anyway: use "-P now -A now" as the default
534          */
535         if (force && !changed) {
536                 dst_key_settime(key, DST_TIME_PUBLISH, now);
537                 dst_key_settime(key, DST_TIME_ACTIVATE, now);
538                 changed = ISC_TRUE;
539         }
540
541         if (!changed && setttl)
542                 changed = ISC_TRUE;
543
544         /*
545          * Print out time values, if -p was used.
546          */
547         if (printcreate)
548                 printtime(key, DST_TIME_CREATED, "Created", epoch, stdout);
549
550         if (printpub)
551                 printtime(key, DST_TIME_PUBLISH, "Publish", epoch, stdout);
552
553         if (printact)
554                 printtime(key, DST_TIME_ACTIVATE, "Activate", epoch, stdout);
555
556         if (printrev)
557                 printtime(key, DST_TIME_REVOKE, "Revoke", epoch, stdout);
558
559         if (printinact)
560                 printtime(key, DST_TIME_INACTIVE, "Inactive", epoch, stdout);
561
562         if (printdel)
563                 printtime(key, DST_TIME_DELETE, "Delete", epoch, stdout);
564
565         if (changed) {
566                 isc_buffer_init(&buf, newname, sizeof(newname));
567                 result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory,
568                                                &buf);
569                 if (result != ISC_R_SUCCESS) {
570                         fatal("Failed to build public key filename: %s",
571                               isc_result_totext(result));
572                 }
573
574                 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
575                                         directory);
576                 if (result != ISC_R_SUCCESS) {
577                         dst_key_format(key, keystr, sizeof(keystr));
578                         fatal("Failed to write key %s: %s", keystr,
579                               isc_result_totext(result));
580                 }
581
582                 printf("%s\n", newname);
583
584                 isc_buffer_clear(&buf);
585                 result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory,
586                                                &buf);
587                 if (result != ISC_R_SUCCESS) {
588                         fatal("Failed to build private key filename: %s",
589                               isc_result_totext(result));
590                 }
591                 printf("%s\n", newname);
592         }
593
594         dst_key_free(&key);
595         dst_lib_destroy();
596         isc_hash_destroy();
597         cleanup_entropy(&ectx);
598         if (verbose > 10)
599                 isc_mem_stats(mctx, stdout);
600         cleanup_logging(&log);
601         isc_mem_free(mctx, directory);
602         isc_mem_destroy(&mctx);
603
604         return (0);
605 }