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