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