00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <config.h>
00020
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <errno.h>
00024 #include <time.h>
00025
00026 #include <isc/buffer.h>
00027 #include <isc/commandline.h>
00028 #include <isc/entropy.h>
00029 #include <isc/file.h>
00030 #include <isc/hash.h>
00031 #include <isc/mem.h>
00032 #include <isc/print.h>
00033 #include <isc/string.h>
00034 #include <isc/util.h>
00035
00036 #include <dns/keyvalues.h>
00037 #include <dns/result.h>
00038 #include <dns/log.h>
00039
00040 #include <dst/dst.h>
00041
00042 #ifdef PKCS11CRYPTO
00043 #include <pk11/result.h>
00044 #endif
00045
00046 #include "dnssectool.h"
00047
00048 const char *program = "dnssec-settime";
00049 int verbose;
00050
00051 static isc_mem_t *mctx = NULL;
00052
00053 ISC_PLATFORM_NORETURN_PRE static void
00054 usage(void) ISC_PLATFORM_NORETURN_POST;
00055
00056 static void
00057 usage(void) {
00058 fprintf(stderr, "Usage:\n");
00059 fprintf(stderr, " %s [options] keyfile\n\n", program);
00060 fprintf(stderr, "Version: %s\n", VERSION);
00061 fprintf(stderr, "General options:\n");
00062 #if defined(PKCS11CRYPTO)
00063 fprintf(stderr, " -E engine: specify PKCS#11 provider "
00064 "(default: %s)\n", PK11_LIB_LOCATION);
00065 #elif defined(USE_PKCS11)
00066 fprintf(stderr, " -E engine: specify OpenSSL engine "
00067 "(default \"pkcs11\")\n");
00068 #else
00069 fprintf(stderr, " -E engine: specify OpenSSL engine\n");
00070 #endif
00071 fprintf(stderr, " -f: force update of old-style "
00072 "keys\n");
00073 fprintf(stderr, " -K directory: set key file location\n");
00074 fprintf(stderr, " -L ttl: set default key TTL\n");
00075 fprintf(stderr, " -v level: set level of verbosity\n");
00076 fprintf(stderr, " -V: print version information\n");
00077 fprintf(stderr, " -h: help\n");
00078 fprintf(stderr, "Timing options:\n");
00079 fprintf(stderr, " -P date/[+-]offset/none: set/unset key "
00080 "publication date\n");
00081 fprintf(stderr, " -A date/[+-]offset/none: set/unset key "
00082 "activation date\n");
00083 fprintf(stderr, " -R date/[+-]offset/none: set/unset key "
00084 "revocation date\n");
00085 fprintf(stderr, " -I date/[+-]offset/none: set/unset key "
00086 "inactivation date\n");
00087 fprintf(stderr, " -D date/[+-]offset/none: set/unset key "
00088 "deletion date\n");
00089 fprintf(stderr, "Printing options:\n");
00090 fprintf(stderr, " -p C/P/A/R/I/D/all: print a particular time "
00091 "value or values\n");
00092 fprintf(stderr, " -u: print times in unix epoch "
00093 "format\n");
00094 fprintf(stderr, "Output:\n");
00095 fprintf(stderr, " K<name>+<alg>+<new id>.key, "
00096 "K<name>+<alg>+<new id>.private\n");
00097
00098 exit (-1);
00099 }
00100
00101 static void
00102 printtime(dst_key_t *key, int type, const char *tag, isc_boolean_t epoch,
00103 FILE *stream)
00104 {
00105 isc_result_t result;
00106 const char *output = NULL;
00107 isc_stdtime_t when;
00108
00109 if (tag != NULL)
00110 fprintf(stream, "%s: ", tag);
00111
00112 result = dst_key_gettime(key, type, &when);
00113 if (result == ISC_R_NOTFOUND) {
00114 fprintf(stream, "UNSET\n");
00115 } else if (epoch) {
00116 fprintf(stream, "%d\n", (int) when);
00117 } else {
00118 time_t timet = when;
00119 output = ctime(&timet);
00120 fprintf(stream, "%s", output);
00121 }
00122 }
00123
00124 int
00125 main(int argc, char **argv) {
00126 isc_result_t result;
00127 #ifdef USE_PKCS11
00128 const char *engine = PKCS11_ENGINE;
00129 #else
00130 const char *engine = NULL;
00131 #endif
00132 char *filename = NULL, *directory = NULL;
00133 char newname[1024];
00134 char keystr[DST_KEY_FORMATSIZE];
00135 char *endp, *p;
00136 int ch;
00137 isc_entropy_t *ectx = NULL;
00138 const char *predecessor = NULL;
00139 dst_key_t *prevkey = NULL;
00140 dst_key_t *key = NULL;
00141 isc_buffer_t buf;
00142 dns_name_t *name = NULL;
00143 dns_secalg_t alg = 0;
00144 unsigned int size = 0;
00145 isc_uint16_t flags = 0;
00146 int prepub = -1;
00147 dns_ttl_t ttl = 0;
00148 isc_stdtime_t now;
00149 isc_stdtime_t pub = 0, act = 0, rev = 0, inact = 0, del = 0;
00150 isc_stdtime_t prevact = 0, previnact = 0, prevdel = 0;
00151 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
00152 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
00153 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
00154 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
00155 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
00156 isc_boolean_t unsetdel = ISC_FALSE;
00157 isc_boolean_t printcreate = ISC_FALSE, printpub = ISC_FALSE;
00158 isc_boolean_t printact = ISC_FALSE, printrev = ISC_FALSE;
00159 isc_boolean_t printinact = ISC_FALSE, printdel = ISC_FALSE;
00160 isc_boolean_t force = ISC_FALSE;
00161 isc_boolean_t epoch = ISC_FALSE;
00162 isc_boolean_t changed = ISC_FALSE;
00163 isc_log_t *log = NULL;
00164
00165 if (argc == 1)
00166 usage();
00167
00168 result = isc_mem_create(0, 0, &mctx);
00169 if (result != ISC_R_SUCCESS)
00170 fatal("Out of memory");
00171
00172 setup_logging(mctx, &log);
00173
00174 #ifdef PKCS11CRYPTO
00175 pk11_result_register();
00176 #endif
00177 dns_result_register();
00178
00179 isc_commandline_errprint = ISC_FALSE;
00180
00181 isc_stdtime_get(&now);
00182
00183 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:L:P:p:R:S:uv:V"
00184 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
00185 switch (ch) {
00186 case 'E':
00187 engine = isc_commandline_argument;
00188 break;
00189 case 'f':
00190 force = ISC_TRUE;
00191 break;
00192 case 'p':
00193 p = isc_commandline_argument;
00194 if (!strcasecmp(p, "all")) {
00195 printcreate = ISC_TRUE;
00196 printpub = ISC_TRUE;
00197 printact = ISC_TRUE;
00198 printrev = ISC_TRUE;
00199 printinact = ISC_TRUE;
00200 printdel = ISC_TRUE;
00201 break;
00202 }
00203
00204 do {
00205 switch (*p++) {
00206 case 'C':
00207 printcreate = ISC_TRUE;
00208 break;
00209 case 'P':
00210 printpub = ISC_TRUE;
00211 break;
00212 case 'A':
00213 printact = ISC_TRUE;
00214 break;
00215 case 'R':
00216 printrev = ISC_TRUE;
00217 break;
00218 case 'I':
00219 printinact = ISC_TRUE;
00220 break;
00221 case 'D':
00222 printdel = ISC_TRUE;
00223 break;
00224 case ' ':
00225 break;
00226 default:
00227 usage();
00228 break;
00229 }
00230 } while (*p != '\0');
00231 break;
00232 case 'u':
00233 epoch = ISC_TRUE;
00234 break;
00235 case 'K':
00236
00237
00238
00239
00240 directory = isc_mem_strdup(mctx,
00241 isc_commandline_argument);
00242 if (directory == NULL) {
00243 fatal("Failed to allocate memory for "
00244 "directory");
00245 }
00246 break;
00247 case 'L':
00248 ttl = strtottl(isc_commandline_argument);
00249 setttl = ISC_TRUE;
00250 break;
00251 case 'v':
00252 verbose = strtol(isc_commandline_argument, &endp, 0);
00253 if (*endp != '\0')
00254 fatal("-v must be followed by a number");
00255 break;
00256 case 'P':
00257 if (setpub || unsetpub)
00258 fatal("-P specified more than once");
00259
00260 changed = ISC_TRUE;
00261 pub = strtotime(isc_commandline_argument,
00262 now, now, &setpub);
00263 unsetpub = !setpub;
00264 break;
00265 case 'A':
00266 if (setact || unsetact)
00267 fatal("-A specified more than once");
00268
00269 changed = ISC_TRUE;
00270 act = strtotime(isc_commandline_argument,
00271 now, now, &setact);
00272 unsetact = !setact;
00273 break;
00274 case 'R':
00275 if (setrev || unsetrev)
00276 fatal("-R specified more than once");
00277
00278 changed = ISC_TRUE;
00279 rev = strtotime(isc_commandline_argument,
00280 now, now, &setrev);
00281 unsetrev = !setrev;
00282 break;
00283 case 'I':
00284 if (setinact || unsetinact)
00285 fatal("-I specified more than once");
00286
00287 changed = ISC_TRUE;
00288 inact = strtotime(isc_commandline_argument,
00289 now, now, &setinact);
00290 unsetinact = !setinact;
00291 break;
00292 case 'D':
00293 if (setdel || unsetdel)
00294 fatal("-D specified more than once");
00295
00296 changed = ISC_TRUE;
00297 del = strtotime(isc_commandline_argument,
00298 now, now, &setdel);
00299 unsetdel = !setdel;
00300 break;
00301 case 'S':
00302 predecessor = isc_commandline_argument;
00303 break;
00304 case 'i':
00305 prepub = strtottl(isc_commandline_argument);
00306 break;
00307 case '?':
00308 if (isc_commandline_option != '?')
00309 fprintf(stderr, "%s: invalid argument -%c\n",
00310 program, isc_commandline_option);
00311
00312 case 'h':
00313
00314 usage();
00315
00316 case 'V':
00317
00318 version(program);
00319
00320 default:
00321 fprintf(stderr, "%s: unhandled option -%c\n",
00322 program, isc_commandline_option);
00323 exit(1);
00324 }
00325 }
00326
00327 if (argc < isc_commandline_index + 1 ||
00328 argv[isc_commandline_index] == NULL)
00329 fatal("The key file name was not specified");
00330 if (argc > isc_commandline_index + 1)
00331 fatal("Extraneous arguments");
00332
00333 if (ectx == NULL)
00334 setup_entropy(mctx, NULL, &ectx);
00335 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
00336 if (result != ISC_R_SUCCESS)
00337 fatal("Could not initialize hash");
00338 result = dst_lib_init2(mctx, ectx, engine,
00339 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
00340 if (result != ISC_R_SUCCESS)
00341 fatal("Could not initialize dst: %s",
00342 isc_result_totext(result));
00343 isc_entropy_stopcallbacksources(ectx);
00344
00345 if (predecessor != NULL) {
00346 int major, minor;
00347
00348 if (prepub == -1)
00349 prepub = (30 * 86400);
00350
00351 if (setpub || unsetpub)
00352 fatal("-S and -P cannot be used together");
00353 if (setact || unsetact)
00354 fatal("-S and -A cannot be used together");
00355
00356 result = dst_key_fromnamedfile(predecessor, directory,
00357 DST_TYPE_PUBLIC |
00358 DST_TYPE_PRIVATE,
00359 mctx, &prevkey);
00360 if (result != ISC_R_SUCCESS)
00361 fatal("Invalid keyfile %s: %s",
00362 filename, isc_result_totext(result));
00363 if (!dst_key_isprivate(prevkey) && !dst_key_isexternal(prevkey))
00364 fatal("%s is not a private key", filename);
00365
00366 name = dst_key_name(prevkey);
00367 alg = dst_key_alg(prevkey);
00368 size = dst_key_size(prevkey);
00369 flags = dst_key_flags(prevkey);
00370
00371 dst_key_format(prevkey, keystr, sizeof(keystr));
00372 dst_key_getprivateformat(prevkey, &major, &minor);
00373 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
00374 fatal("Predecessor has incompatible format "
00375 "version %d.%d\n\t", major, minor);
00376
00377 result = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &prevact);
00378 if (result != ISC_R_SUCCESS)
00379 fatal("Predecessor has no activation date. "
00380 "You must set one before\n\t"
00381 "generating a successor.");
00382
00383 result = dst_key_gettime(prevkey, DST_TIME_INACTIVE,
00384 &previnact);
00385 if (result != ISC_R_SUCCESS)
00386 fatal("Predecessor has no inactivation date. "
00387 "You must set one before\n\t"
00388 "generating a successor.");
00389
00390 pub = prevact - prepub;
00391 if (pub < now && prepub != 0)
00392 fatal("Predecessor will become inactive before the\n\t"
00393 "prepublication period ends. Either change "
00394 "its inactivation date,\n\t"
00395 "or use the -i option to set a shorter "
00396 "prepublication interval.");
00397
00398 result = dst_key_gettime(prevkey, DST_TIME_DELETE, &prevdel);
00399 if (result != ISC_R_SUCCESS)
00400 fprintf(stderr, "%s: warning: Predecessor has no "
00401 "removal date;\n\t"
00402 "it will remain in the zone "
00403 "indefinitely after rollover.\n",
00404 program);
00405 else if (prevdel < previnact)
00406 fprintf(stderr, "%s: warning: Predecessor is "
00407 "scheduled to be deleted\n\t"
00408 "before it is scheduled to be "
00409 "inactive.\n", program);
00410
00411 changed = setpub = setact = ISC_TRUE;
00412 } else {
00413 if (prepub < 0)
00414 prepub = 0;
00415
00416 if (prepub > 0) {
00417 if (setpub && setact && (act - prepub) < pub)
00418 fatal("Activation and publication dates "
00419 "are closer together than the\n\t"
00420 "prepublication interval.");
00421
00422 if (setpub && !setact) {
00423 setact = ISC_TRUE;
00424 act = pub + prepub;
00425 } else if (setact && !setpub) {
00426 setpub = ISC_TRUE;
00427 pub = act - prepub;
00428 }
00429
00430 if ((act - prepub) < now)
00431 fatal("Time until activation is shorter "
00432 "than the\n\tprepublication interval.");
00433 }
00434 }
00435
00436 if (directory != NULL) {
00437 filename = argv[isc_commandline_index];
00438 } else {
00439 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
00440 &directory, &filename);
00441 if (result != ISC_R_SUCCESS)
00442 fatal("cannot process filename %s: %s",
00443 argv[isc_commandline_index],
00444 isc_result_totext(result));
00445 }
00446
00447 result = dst_key_fromnamedfile(filename, directory,
00448 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
00449 mctx, &key);
00450 if (result != ISC_R_SUCCESS)
00451 fatal("Invalid keyfile %s: %s",
00452 filename, isc_result_totext(result));
00453
00454 if (!dst_key_isprivate(key) && !dst_key_isexternal(key))
00455 fatal("%s is not a private key", filename);
00456
00457 dst_key_format(key, keystr, sizeof(keystr));
00458
00459 if (predecessor != NULL) {
00460 if (!dns_name_equal(name, dst_key_name(key)))
00461 fatal("Key name mismatch");
00462 if (alg != dst_key_alg(key))
00463 fatal("Key algorithm mismatch");
00464 if (size != dst_key_size(key))
00465 fatal("Key size mismatch");
00466 if (flags != dst_key_flags(key))
00467 fatal("Key flags mismatch");
00468 }
00469
00470 prevdel = previnact = 0;
00471 if ((setdel && setinact && del < inact) ||
00472 (dst_key_gettime(key, DST_TIME_INACTIVE,
00473 &previnact) == ISC_R_SUCCESS &&
00474 setdel && !setinact && del < previnact) ||
00475 (dst_key_gettime(key, DST_TIME_DELETE,
00476 &prevdel) == ISC_R_SUCCESS &&
00477 setinact && !setdel && prevdel < inact) ||
00478 (!setdel && !setinact && prevdel < previnact))
00479 fprintf(stderr, "%s: warning: Key is scheduled to "
00480 "be deleted before it is\n\t"
00481 "scheduled to be inactive.\n",
00482 program);
00483
00484 if (force)
00485 set_keyversion(key);
00486 else
00487 check_keyversion(key, keystr);
00488
00489 if (verbose > 2)
00490 fprintf(stderr, "%s: %s\n", program, keystr);
00491
00492
00493
00494
00495 if (setpub)
00496 dst_key_settime(key, DST_TIME_PUBLISH, pub);
00497 else if (unsetpub)
00498 dst_key_unsettime(key, DST_TIME_PUBLISH);
00499
00500 if (setact)
00501 dst_key_settime(key, DST_TIME_ACTIVATE, act);
00502 else if (unsetact)
00503 dst_key_unsettime(key, DST_TIME_ACTIVATE);
00504
00505 if (setrev) {
00506 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
00507 fprintf(stderr, "%s: warning: Key %s is already "
00508 "revoked; changing the revocation date "
00509 "will not affect this.\n",
00510 program, keystr);
00511 if ((dst_key_flags(key) & DNS_KEYFLAG_KSK) == 0)
00512 fprintf(stderr, "%s: warning: Key %s is not flagged as "
00513 "a KSK, but -R was used. Revoking a "
00514 "ZSK is legal, but undefined.\n",
00515 program, keystr);
00516 dst_key_settime(key, DST_TIME_REVOKE, rev);
00517 } else if (unsetrev) {
00518 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
00519 fprintf(stderr, "%s: warning: Key %s is already "
00520 "revoked; removing the revocation date "
00521 "will not affect this.\n",
00522 program, keystr);
00523 dst_key_unsettime(key, DST_TIME_REVOKE);
00524 }
00525
00526 if (setinact)
00527 dst_key_settime(key, DST_TIME_INACTIVE, inact);
00528 else if (unsetinact)
00529 dst_key_unsettime(key, DST_TIME_INACTIVE);
00530
00531 if (setdel)
00532 dst_key_settime(key, DST_TIME_DELETE, del);
00533 else if (unsetdel)
00534 dst_key_unsettime(key, DST_TIME_DELETE);
00535
00536 if (setttl)
00537 dst_key_setttl(key, ttl);
00538
00539
00540
00541
00542
00543 if (force && !changed) {
00544 dst_key_settime(key, DST_TIME_PUBLISH, now);
00545 dst_key_settime(key, DST_TIME_ACTIVATE, now);
00546 changed = ISC_TRUE;
00547 }
00548
00549 if (!changed && setttl)
00550 changed = ISC_TRUE;
00551
00552
00553
00554
00555 if (printcreate)
00556 printtime(key, DST_TIME_CREATED, "Created", epoch, stdout);
00557
00558 if (printpub)
00559 printtime(key, DST_TIME_PUBLISH, "Publish", epoch, stdout);
00560
00561 if (printact)
00562 printtime(key, DST_TIME_ACTIVATE, "Activate", epoch, stdout);
00563
00564 if (printrev)
00565 printtime(key, DST_TIME_REVOKE, "Revoke", epoch, stdout);
00566
00567 if (printinact)
00568 printtime(key, DST_TIME_INACTIVE, "Inactive", epoch, stdout);
00569
00570 if (printdel)
00571 printtime(key, DST_TIME_DELETE, "Delete", epoch, stdout);
00572
00573 if (changed) {
00574 isc_buffer_init(&buf, newname, sizeof(newname));
00575 result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory,
00576 &buf);
00577 if (result != ISC_R_SUCCESS) {
00578 fatal("Failed to build public key filename: %s",
00579 isc_result_totext(result));
00580 }
00581
00582 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
00583 directory);
00584 if (result != ISC_R_SUCCESS) {
00585 dst_key_format(key, keystr, sizeof(keystr));
00586 fatal("Failed to write key %s: %s", keystr,
00587 isc_result_totext(result));
00588 }
00589
00590 printf("%s\n", newname);
00591
00592 isc_buffer_clear(&buf);
00593 result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory,
00594 &buf);
00595 if (result != ISC_R_SUCCESS) {
00596 fatal("Failed to build private key filename: %s",
00597 isc_result_totext(result));
00598 }
00599 printf("%s\n", newname);
00600 }
00601
00602 if (prevkey != NULL)
00603 dst_key_free(&prevkey);
00604 dst_key_free(&key);
00605 dst_lib_destroy();
00606 isc_hash_destroy();
00607 cleanup_entropy(&ectx);
00608 if (verbose > 10)
00609 isc_mem_stats(mctx, stdout);
00610 cleanup_logging(&log);
00611 isc_mem_free(mctx, directory);
00612 isc_mem_destroy(&mctx);
00613
00614 return (0);
00615 }