ldns-signzone.c
Go to the documentation of this file.
00001 /*
00002  * ldns-signzone signs a zone file
00003  * 
00004  * (c) NLnet Labs, 2005 - 2008
00005  * See the file LICENSE for the license
00006  */
00007 
00008 #include "config.h"
00009 #include <stdlib.h>
00010 #include <unistd.h>
00011 
00012 #include <errno.h>
00013 
00014 #include <time.h>
00015 
00016 #include <ldns/ldns.h>
00017 #include <ldns/keys.h>
00018 
00019 #ifdef HAVE_SSL
00020 #include <openssl/conf.h>
00021 #include <openssl/engine.h>
00022 #endif /* HAVE_SSL */
00023 
00024 #define MAX_FILENAME_LEN 250
00025 int verbosity = 1;
00026 
00027 #ifdef HAVE_SSL
00028 #include <openssl/err.h>
00029 
00030 static void
00031 usage(FILE *fp, const char *prog) {
00032         fprintf(fp, "%s [OPTIONS] zonefile key [key [key]]\n", prog);
00033         fprintf(fp, "  signs the zone with the given key(s)\n");
00034         fprintf(fp, "  -b\t\tuse layout in signed zone and print comments DNSSEC records\n");
00035         fprintf(fp, "  -d\t\tused keys are not added to the zone\n");
00036         fprintf(fp, "  -e <date>\texpiration date\n");
00037         fprintf(fp, "  -f <file>\toutput zone to file (default <name>.signed)\n");
00038         fprintf(fp, "  -i <date>\tinception date\n");
00039         fprintf(fp, "  -o <domain>\torigin for the zone\n");
00040         fprintf(fp, "  -v\t\tprint version and exit\n");
00041         fprintf(fp, "  -A\t\tsign DNSKEY with all keys instead of minimal\n");
00042         fprintf(fp, "  -E <name>\tuse <name> as the crypto engine for signing\n");
00043         fprintf(fp, "           \tThis can have a lot of extra options, see the manual page for more info\n");
00044         fprintf(fp, "  -k <id>,<int>\tuse key id with algorithm int from engine\n");
00045         fprintf(fp, "  -K <id>,<int>\tuse key id with algorithm int from engine as KSK\n");
00046         fprintf(fp, "\t\tif no key is given (but an external one is used through the engine support, it might be necessary to provide the right algorithm number.\n");
00047         fprintf(fp, "  -n\t\tuse NSEC3 instead of NSEC.\n");
00048         fprintf(fp, "\t\tIf you use NSEC3, you can specify the following extra options:\n");
00049         fprintf(fp, "\t\t-a [algorithm] hashing algorithm\n");
00050         fprintf(fp, "\t\t-t [number] number of hash iterations\n");
00051         fprintf(fp, "\t\t-s [string] salt\n");
00052         fprintf(fp, "\t\t-p set the opt-out flag on all nsec3 rrs\n");
00053         fprintf(fp, "\n");
00054         fprintf(fp, "  keys must be specified by their base name (usually K<name>+<alg>+<id>),\n");
00055         fprintf(fp, "  i.e. WITHOUT the .private extension.\n");
00056         fprintf(fp, "  If the public part of the key is not present in the zone, the DNSKEY RR\n");
00057         fprintf(fp, "  will be read from the file called <base name>.key. If that does not exist,\n");
00058         fprintf(fp, "  a default DNSKEY will be generated from the private key and added to the zone.\n");
00059         fprintf(fp, "  A date can be a timestamp (seconds since the epoch), or of\n  the form <YYYYMMdd[hhmmss]>\n");
00060 }
00061 
00062 static void check_tm(struct tm tm)
00063 {
00064         if (tm.tm_year < 70) {
00065                 fprintf(stderr, "You cannot specify dates before 1970\n");
00066                 exit(EXIT_FAILURE);
00067         }
00068         if (tm.tm_mon < 0 || tm.tm_mon > 11) {
00069                 fprintf(stderr, "The month must be in the range 1 to 12\n");
00070                 exit(EXIT_FAILURE);
00071         }
00072         if (tm.tm_mday < 1 || tm.tm_mday > 31) {
00073                 fprintf(stderr, "The day must be in the range 1 to 31\n");
00074                 exit(EXIT_FAILURE);
00075         }
00076         
00077         if (tm.tm_hour < 0 || tm.tm_hour > 23) {
00078                 fprintf(stderr, "The hour must be in the range 0-23\n");
00079                 exit(EXIT_FAILURE);
00080         }
00081 
00082         if (tm.tm_min < 0 || tm.tm_min > 59) {
00083                 fprintf(stderr, "The minute must be in the range 0-59\n");
00084                 exit(EXIT_FAILURE);
00085         }
00086 
00087         if (tm.tm_sec < 0 || tm.tm_sec > 59) {
00088                 fprintf(stderr, "The second must be in the range 0-59\n");
00089                 exit(EXIT_FAILURE);
00090         }
00091 
00092 }
00093 
00094 /*
00095  * if the ttls are different, make them equal
00096  * if one of the ttls equals LDNS_DEFAULT_TTL, that one is changed
00097  * otherwise, rr2 will get the ttl of rr1
00098  * 
00099  * prints a warning if a non-default TTL is changed
00100  */
00101 static void
00102 equalize_ttls(ldns_rr *rr1, ldns_rr *rr2, uint32_t default_ttl)
00103 {
00104         uint32_t ttl1, ttl2;
00105         
00106         ttl1 = ldns_rr_ttl(rr1);
00107         ttl2 = ldns_rr_ttl(rr2);
00108         
00109         if (ttl1 != ttl2) {
00110                 if (ttl1 == default_ttl) {
00111                         ldns_rr_set_ttl(rr1, ttl2);
00112                 } else if (ttl2 == default_ttl) {
00113                         ldns_rr_set_ttl(rr2, ttl1);
00114                 } else {
00115                         ldns_rr_set_ttl(rr2, ttl1);
00116                         fprintf(stderr, 
00117                                 "warning: changing non-default TTL %u to %u\n",
00118                                 (unsigned int) ttl2, (unsigned int)  ttl1);
00119                 }
00120         }
00121 }
00122 
00123 static void
00124 equalize_ttls_rr_list(ldns_rr_list *rr_list, ldns_rr *rr, uint32_t default_ttl)
00125 {
00126         size_t i;
00127         ldns_rr *cur_rr;
00128         
00129         for (i = 0; i < ldns_rr_list_rr_count(rr_list); i++) {
00130                 cur_rr = ldns_rr_list_rr(rr_list, i);
00131                 if (ldns_rr_compare_no_rdata(cur_rr, rr) == 0) {
00132                         equalize_ttls(cur_rr, rr, default_ttl);
00133                 }
00134         }
00135 }
00136 
00137 static ldns_rr *
00138 find_key_in_zone(ldns_rr *pubkey_gen, ldns_zone *zone) {
00139         size_t key_i;
00140         ldns_rr *pubkey;
00141         
00142         for (key_i = 0;
00143                 key_i < ldns_rr_list_rr_count(ldns_zone_rrs(zone));
00144                 key_i++) {
00145                 pubkey = ldns_rr_list_rr(ldns_zone_rrs(zone), key_i);
00146                 if (ldns_rr_get_type(pubkey) == LDNS_RR_TYPE_DNSKEY &&
00147                         (ldns_calc_keytag(pubkey)
00148                                 ==
00149                                 ldns_calc_keytag(pubkey_gen) ||
00150                                          /* KSK has gen-keytag + 1 */
00151                                          ldns_calc_keytag(pubkey)
00152                                          ==
00153                                          ldns_calc_keytag(pubkey_gen) + 1) 
00154                            ) {
00155                                 if (verbosity >= 2) {
00156                                         fprintf(stderr, "Found it in the zone!\n");
00157                                 }
00158                                 return pubkey;
00159                 }
00160         }
00161         return NULL;
00162 }
00163 
00164 static ldns_rr *
00165 find_key_in_file(const char *keyfile_name_base, ldns_key* ATTR_UNUSED(key),
00166         uint32_t zone_ttl)
00167 {
00168         char *keyfile_name;
00169         FILE *keyfile;
00170         int line_nr;
00171         uint32_t default_ttl = zone_ttl;
00172 
00173         ldns_rr *pubkey = NULL;
00174         keyfile_name = LDNS_XMALLOC(char,
00175                                     strlen(keyfile_name_base) + 5);
00176         snprintf(keyfile_name,
00177                  strlen(keyfile_name_base) + 5,
00178                  "%s.key",
00179                  keyfile_name_base);
00180         if (verbosity >= 2) {
00181                 fprintf(stderr, "Trying to read %s\n", keyfile_name);
00182         }
00183         keyfile = fopen(keyfile_name, "r");
00184         line_nr = 0;
00185         if (keyfile) {
00186                 if (ldns_rr_new_frm_fp_l(&pubkey,
00187                                          keyfile,
00188                                          &default_ttl,
00189                                          NULL,
00190                                          NULL,
00191                                          &line_nr) ==
00192                     LDNS_STATUS_OK) {
00193                         if (verbosity >= 2) {
00194                                 printf("Key found in file: %s\n", keyfile_name);
00195                         }
00196                 }
00197                 fclose(keyfile);
00198         }
00199         LDNS_FREE(keyfile_name);
00200         return pubkey;
00201 }
00202 
00203 /* this function tries to find the specified keys either in the zone that
00204  * has been read, or in a <basename>.key file. If the key is not found,
00205  * a public key is generated, and it is assumed the key is a ZSK
00206  * 
00207  * if add_keys is true; the DNSKEYs are added to the zone prior to signing
00208  * if it is false, they are not added.
00209  * Even if keys are not added, the function is still needed, to check
00210  * whether keys of which we only have key data are KSKs or ZSKS
00211  */
00212 static void
00213 find_or_create_pubkey(const char *keyfile_name_base, ldns_key *key, ldns_zone *orig_zone, bool add_keys, uint32_t default_ttl) {
00214         ldns_rr *pubkey_gen, *pubkey;
00215         int key_in_zone;
00216         
00217         if (default_ttl == LDNS_DEFAULT_TTL) {
00218                 default_ttl = ldns_rr_ttl(ldns_zone_soa(orig_zone));
00219         }
00220 
00221         if (!ldns_key_pubkey_owner(key)) {
00222                 ldns_key_set_pubkey_owner(key, ldns_rdf_clone(ldns_rr_owner(ldns_zone_soa(orig_zone))));
00223         }
00224 
00225         /* find the public key in the zone, or in a
00226          * seperate file
00227          * we 'generate' one anyway, 
00228          * then match that to any present in the zone,
00229          * if it matches, we drop our own. If not,
00230          * we try to see if there is a .key file present.
00231          * If not, we use our own generated one, with
00232          * some default values 
00233          *
00234          * Even if -d (do-not-add-keys) is specified, 
00235          * we still need to do this, because we need
00236          * to have any key flags that are set this way
00237          */
00238         pubkey_gen = ldns_key2rr(key);
00239         ldns_rr_set_ttl(pubkey_gen, default_ttl);
00240 
00241         if (verbosity >= 2) {
00242                 fprintf(stderr,
00243                            "Looking for key with keytag %u or %u\n",
00244                            (unsigned int) ldns_calc_keytag(pubkey_gen),
00245                            (unsigned int) ldns_calc_keytag(pubkey_gen)+1
00246                            );
00247         }
00248 
00249         pubkey = find_key_in_zone(pubkey_gen, orig_zone);
00250         key_in_zone = 1;
00251         if (!pubkey) {
00252                 key_in_zone = 0;
00253                 /* it was not in the zone, try to read a .key file */
00254                 pubkey = find_key_in_file(keyfile_name_base, key, default_ttl);
00255                 if (!pubkey && !(ldns_key_flags(key) & LDNS_KEY_SEP_KEY)) {
00256                         /* maybe it is a ksk? */
00257                         ldns_key_set_keytag(key, ldns_key_keytag(key) + 1);
00258                         pubkey = find_key_in_file(keyfile_name_base, key, default_ttl);
00259                         if (!pubkey) {
00260                                 /* ok, no file, set back to ZSK */
00261                                 ldns_key_set_keytag(key, ldns_key_keytag(key) - 1);
00262                         }
00263                 }
00264                 if(pubkey && ldns_dname_compare(ldns_rr_owner(pubkey), ldns_rr_owner(ldns_zone_soa(orig_zone))) != 0) {
00265                         fprintf(stderr, "Error %s.key has wrong name: %s\n",
00266                                 keyfile_name_base, ldns_rdf2str(ldns_rr_owner(pubkey)));
00267                         exit(EXIT_FAILURE); /* leak rdf2str, but we exit */
00268                 }
00269         }
00270         
00271         if (!pubkey) {
00272                 /* okay, no public key found,
00273                    just use our generated one */
00274                 pubkey = pubkey_gen;
00275                 if (verbosity >= 2) {
00276                         fprintf(stderr, "Not in zone, no .key file, generating ZSK DNSKEY from private key data\n");
00277                 }
00278         } else {
00279                 ldns_rr_free(pubkey_gen);
00280         }
00281         ldns_key_set_flags(key, ldns_rdf2native_int16(ldns_rr_rdf(pubkey, 0)));
00282         ldns_key_set_keytag(key, ldns_calc_keytag(pubkey));
00283         
00284         if (add_keys && !key_in_zone) {
00285                 equalize_ttls_rr_list(ldns_zone_rrs(orig_zone), pubkey, default_ttl);
00286                 ldns_zone_push_rr(orig_zone, pubkey);
00287         }
00288 }
00289 
00290 void
00291 strip_dnssec_records(ldns_zone *zone)
00292 {
00293         ldns_rr_list *new_list;
00294         ldns_rr *cur_rr;
00295         
00296         new_list = ldns_rr_list_new();
00297         
00298         while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(zone)))) {
00299                 if (ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_RRSIG ||
00300                     ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_NSEC ||
00301                     ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_NSEC3
00302                    ) {
00303                         
00304                         ldns_rr_free(cur_rr);
00305                 } else {
00306                         ldns_rr_list_push_rr(new_list, cur_rr);
00307                 }
00308         }
00309         ldns_rr_list_free(ldns_zone_rrs(zone));
00310         ldns_zone_set_rrs(zone, new_list);
00311 }
00312 
00313 int
00314 main(int argc, char *argv[])
00315 {
00316         const char *zonefile_name;
00317         FILE *zonefile = NULL;
00318         int line_nr = 0;
00319         int c;
00320         int argi;
00321         ENGINE *engine = NULL;
00322 
00323         ldns_zone *orig_zone;
00324         ldns_rr_list *orig_rrs = NULL;
00325         ldns_rr *orig_soa = NULL;
00326         ldns_dnssec_zone *signed_zone;
00327 
00328         char *keyfile_name_base;
00329         char *keyfile_name = NULL;
00330         FILE *keyfile = NULL;
00331         ldns_key *key = NULL;
00332         ldns_key_list *keys;
00333         ldns_status s;
00334         size_t i;
00335         ldns_rr_list *added_rrs;
00336 
00337         char *outputfile_name = NULL;
00338         FILE *outputfile;
00339         
00340         /* tmp vars for engine keys */
00341         char *eng_key_l;
00342         size_t eng_key_id_len;
00343         char *eng_key_id;
00344         int eng_key_algo;
00345         
00346         bool use_nsec3 = false;
00347         int signflags = 0;
00348 
00349         /* Add the given keys to the zone if they are not yet present */
00350         bool add_keys = true;
00351         uint8_t nsec3_algorithm = 1;
00352         uint8_t nsec3_flags = 0;
00353         size_t nsec3_iterations_cmd = 1;
00354         uint16_t nsec3_iterations = 1;
00355         uint8_t nsec3_salt_length = 0;
00356         uint8_t *nsec3_salt = NULL;
00357         
00358         /* we need to know the origin before reading ksk's,
00359          * so keep an array of filenames until we know it
00360          */
00361         struct tm tm;
00362         uint32_t inception;
00363         uint32_t expiration;
00364         ldns_rdf *origin = NULL;
00365         uint32_t ttl = LDNS_DEFAULT_TTL;
00366         ldns_rr_class class = LDNS_RR_CLASS_IN; 
00367         
00368         char *prog = strdup(argv[0]);
00369         ldns_status result;
00370 
00371         ldns_output_format fmt = { ldns_output_format_default->flags, NULL };
00372         void **hashmap = NULL;
00373 
00374         
00375         inception = 0;
00376         expiration = 0;
00377         
00378         keys = ldns_key_list_new();
00379 
00380         OPENSSL_config(NULL);
00381 
00382         while ((c = getopt(argc, argv, "a:bde:f:i:k:lno:ps:t:vAE:K:")) != -1) {
00383                 switch (c) {
00384                 case 'a':
00385                         nsec3_algorithm = (uint8_t) atoi(optarg);
00386                         if (nsec3_algorithm != 1) {
00387                                 fprintf(stderr, "Bad NSEC3 algorithm, only RSASHA1 allowed\n");
00388                                 exit(EXIT_FAILURE);
00389                         }
00390                         break;
00391                 case 'b':
00392                         fmt.flags |= LDNS_COMMENT_BUBBLEBABBLE;
00393                         fmt.flags |= LDNS_COMMENT_FLAGS;
00394                         fmt.flags |= LDNS_COMMENT_NSEC3_CHAIN;
00395                         fmt.flags |= LDNS_COMMENT_LAYOUT;
00396                         hashmap = &fmt.data;
00397                         break;
00398                 case 'd':
00399                         add_keys = false;
00400                         break;
00401                 case 'e':
00402                         /* try to parse YYYYMMDD first,
00403                          * if that doesn't work, it
00404                          * should be a timestamp (seconds since epoch)
00405                          */
00406                         memset(&tm, 0, sizeof(tm));
00407 
00408                         if (strlen(optarg) == 8 &&
00409                             sscanf(optarg, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday)
00410                             ) {
00411                                 tm.tm_year -= 1900;
00412                                 tm.tm_mon--;
00413                                 check_tm(tm);
00414                                 expiration = 
00415                                         (uint32_t) ldns_mktime_from_utc(&tm);
00416                         } else if (strlen(optarg) == 14 &&
00417                                          sscanf(optarg, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec)
00418                                          ) {
00419                                 tm.tm_year -= 1900;
00420                                 tm.tm_mon--;
00421                                 check_tm(tm);
00422                                 expiration = 
00423                                         (uint32_t) ldns_mktime_from_utc(&tm);
00424                         } else {
00425                                 expiration = (uint32_t) atol(optarg);
00426                         }
00427                         break;
00428                 case 'f':
00429                         outputfile_name = LDNS_XMALLOC(char, MAX_FILENAME_LEN);
00430                         strncpy(outputfile_name, optarg, MAX_FILENAME_LEN);
00431                         break;
00432                 case 'i':
00433                         memset(&tm, 0, sizeof(tm));
00434 
00435                         if (strlen(optarg) == 8 &&
00436                             sscanf(optarg, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday)
00437                             ) {
00438                                 tm.tm_year -= 1900;
00439                                 tm.tm_mon--;
00440                                 check_tm(tm);
00441                                 inception = 
00442                                         (uint32_t) ldns_mktime_from_utc(&tm);
00443                         } else if (strlen(optarg) == 14 &&
00444                                          sscanf(optarg, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec)
00445                                          ) {
00446                                 tm.tm_year -= 1900;
00447                                 tm.tm_mon--;
00448                                 check_tm(tm);
00449                                 inception = 
00450                                         (uint32_t) ldns_mktime_from_utc(&tm);
00451                         } else {
00452                                 inception = (uint32_t) atol(optarg);
00453                         }
00454                         break;
00455                 case 'n':
00456                         use_nsec3 = true;
00457                         break;
00458                 case 'o':
00459                         if (ldns_str2rdf_dname(&origin, optarg) != LDNS_STATUS_OK) {
00460                                 fprintf(stderr, "Bad origin, not a correct domain name\n");
00461                                 usage(stderr, prog);
00462                                 exit(EXIT_FAILURE);
00463                         }
00464                         break;
00465                 case 'p':
00466                         nsec3_flags = nsec3_flags | LDNS_NSEC3_VARS_OPTOUT_MASK;
00467                         break;
00468                 case 'v':
00469                         printf("zone signer version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
00470                         exit(EXIT_SUCCESS);
00471                         break;
00472                 case 'A':
00473                         signflags |= LDNS_SIGN_DNSKEY_WITH_ZSK;
00474                         break;
00475                 case 'E':
00476                         ENGINE_load_builtin_engines();
00477                         ENGINE_load_dynamic();
00478                         ENGINE_load_cryptodev();
00479                         engine = ENGINE_by_id(optarg);
00480                         if (!engine) {
00481                                 printf("No such engine: %s\n", optarg);
00482                                 engine = ENGINE_get_first();
00483                                 printf("Available engines:\n");
00484                                 while (engine) {
00485                                         printf("%s\n", ENGINE_get_id(engine));
00486                                         engine = ENGINE_get_next(engine);
00487                                 }
00488                                 exit(EXIT_FAILURE);
00489                         } else {
00490                                 if (!ENGINE_init(engine)) {
00491                                         printf("The engine couldn't initialize\n");
00492                                         exit(EXIT_FAILURE);
00493                                 }
00494                                 ENGINE_set_default_RSA(engine);
00495                                 ENGINE_set_default_DSA(engine);
00496                                 ENGINE_set_default(engine, 0);
00497                         }
00498                         break;
00499                 case 'k':
00500                         eng_key_l = strchr(optarg, ',');
00501                         if (eng_key_l && strlen(eng_key_l) > 1) {
00502                                 if (eng_key_l > optarg) {
00503                                         eng_key_id_len = (size_t) (eng_key_l - optarg);
00504                                         eng_key_id = malloc(eng_key_id_len + 1);
00505                                         memcpy(eng_key_id, optarg, eng_key_id_len);
00506                                         eng_key_id[eng_key_id_len] = '\0';
00507                                 } else {
00508                                         /* no id given, use default from engine */
00509                                         eng_key_id = NULL;
00510                                 }
00511 
00512                                 eng_key_algo = atoi(eng_key_l + 1);
00513 
00514                                 printf("Engine key id: %s, algo %d\n", eng_key_id, eng_key_algo);
00515 
00516                                 s = ldns_key_new_frm_engine(&key, engine, eng_key_id, eng_key_algo);
00517                                 if (s == LDNS_STATUS_OK) {
00518                                         /* must be dnssec key */
00519                                         switch (ldns_key_algorithm(key)) {
00520                                         case LDNS_SIGN_RSAMD5:
00521                                         case LDNS_SIGN_RSASHA1:
00522                                         case LDNS_SIGN_RSASHA1_NSEC3:
00523                                         case LDNS_SIGN_RSASHA256:
00524                                         case LDNS_SIGN_RSASHA512:
00525                                         case LDNS_SIGN_DSA:
00526                                         case LDNS_SIGN_DSA_NSEC3:
00527                                         case LDNS_SIGN_ECC_GOST:
00528 #ifdef USE_ECDSA
00529                                         case LDNS_SIGN_ECDSAP256SHA256:
00530                                         case LDNS_SIGN_ECDSAP384SHA384:
00531 #endif
00532                                                 ldns_key_list_push_key(keys, key);
00533                                                 /*printf("Added key at %p:\n", key);*/
00534                                                 /*ldns_key_print(stdout, key);*/
00535                                                 break;
00536                                         default:
00537                                                 fprintf(stderr, "Warning, key not suitable for signing, ignoring key with algorithm %u\n", ldns_key_algorithm(key));
00538                                                 break;
00539                                         }
00540                                         if (expiration != 0) {
00541                                                 ldns_key_set_expiration(key,
00542                                                                 expiration);
00543                                         }
00544                                         if (inception != 0) {
00545                                                 ldns_key_set_inception(key,
00546                                                                 inception);
00547                                         }
00548                                 } else {
00549                                         printf("Error reading key '%s' from engine: %s\n", eng_key_id, ldns_get_errorstr_by_id(s));
00550                                         #ifdef HAVE_SSL
00551                                                         if (ERR_peek_error()) {
00552                                                                 ERR_load_crypto_strings();
00553                                                                 ERR_print_errors_fp(stderr);
00554                                                                 ERR_free_strings();
00555                                                         }
00556                                         #endif
00557                                         exit(EXIT_FAILURE);
00558                                 }
00559 
00560                                 if (eng_key_id) {
00561                                         free(eng_key_id);
00562                                 }
00563                         } else {
00564                                 printf("Error: bad engine key specification (should be: -k <id>,<algorithm>)).\n");
00565                                 exit(EXIT_FAILURE);
00566                         }
00567                         break;
00568                 case 'K':
00569                         printf("Not implemented yet\n");
00570                         exit(EXIT_FAILURE);
00571                         break;
00572                 case 's':
00573                         if (strlen(optarg) % 2 != 0) {
00574                                 fprintf(stderr, "Salt value is not valid hex data, not a multiple of 2 characters\n");
00575                                 exit(EXIT_FAILURE);
00576                         }
00577                         nsec3_salt_length = (uint8_t) strlen(optarg) / 2;
00578                         nsec3_salt = LDNS_XMALLOC(uint8_t, nsec3_salt_length);
00579                         for (c = 0; c < (int) strlen(optarg); c += 2) {
00580                                 if (isxdigit((int) optarg[c]) && isxdigit((int) optarg[c+1])) {
00581                                         nsec3_salt[c/2] = (uint8_t) ldns_hexdigit_to_int(optarg[c]) * 16 +
00582                                                 ldns_hexdigit_to_int(optarg[c+1]);
00583                                 } else {
00584                                         fprintf(stderr, "Salt value is not valid hex data.\n");
00585                                         exit(EXIT_FAILURE);
00586                                 }
00587                         }
00588 
00589                         break;
00590                 case 't':
00591                         nsec3_iterations_cmd = (size_t) atol(optarg);
00592                         if (nsec3_iterations_cmd > LDNS_NSEC3_MAX_ITERATIONS) {
00593                                 fprintf(stderr, "Iterations count can not exceed %u, quitting\n", LDNS_NSEC3_MAX_ITERATIONS);
00594                                 exit(EXIT_FAILURE);
00595                         }
00596                         nsec3_iterations = (uint16_t) nsec3_iterations_cmd;
00597                         break;
00598                 default:
00599                         usage(stderr, prog);
00600                         exit(EXIT_SUCCESS);
00601                 }
00602         }
00603         
00604         argc -= optind;
00605         argv += optind;
00606 
00607         if (argc < 1) {
00608                 printf("Error: not enough arguments\n");
00609                 usage(stdout, prog);
00610                 exit(EXIT_FAILURE);
00611         } else {
00612                 zonefile_name = argv[0];
00613         }
00614 
00615         /* read zonefile first to find origin if not specified */
00616         
00617         if (strncmp(zonefile_name, "-", 2) == 0) {
00618                 s = ldns_zone_new_frm_fp_l(&orig_zone,
00619                                            stdin,
00620                                            origin,
00621                                            ttl,
00622                                            class,
00623                                            &line_nr);
00624                         if (s != LDNS_STATUS_OK) {
00625                                 fprintf(stderr, "Zone not read, error: %s at stdin line %d\n", 
00626                                            ldns_get_errorstr_by_id(s),
00627                                            line_nr);
00628                                 exit(EXIT_FAILURE);
00629                         } else {
00630                                 orig_soa = ldns_zone_soa(orig_zone);
00631                                 if (!orig_soa) {
00632                                         fprintf(stderr,
00633                                                    "Error reading zonefile: missing SOA record\n");
00634                                         exit(EXIT_FAILURE);
00635                                 }
00636                                 orig_rrs = ldns_zone_rrs(orig_zone);
00637                                 if (!orig_rrs) {
00638                                         fprintf(stderr,
00639                                                    "Error reading zonefile: no resource records\n");
00640                                         exit(EXIT_FAILURE);
00641                                 }
00642                         }
00643         } else {
00644                 zonefile = fopen(zonefile_name, "r");
00645                 
00646                 if (!zonefile) {
00647                         fprintf(stderr,
00648                                    "Error: unable to read %s (%s)\n",
00649                                    zonefile_name,
00650                                    strerror(errno));
00651                         exit(EXIT_FAILURE);
00652                 } else {
00653                         s = ldns_zone_new_frm_fp_l(&orig_zone,
00654                                                    zonefile,
00655                                                    origin,
00656                                                    ttl,
00657                                                    class,
00658                                                    &line_nr);
00659                         if (s != LDNS_STATUS_OK) {
00660                                 fprintf(stderr, "Zone not read, error: %s at %s line %d\n", 
00661                                            ldns_get_errorstr_by_id(s), 
00662                                            zonefile_name, line_nr);
00663                                 exit(EXIT_FAILURE);
00664                         } else {
00665                                 orig_soa = ldns_zone_soa(orig_zone);
00666                                 if (!orig_soa) {
00667                                         fprintf(stderr,
00668                                                    "Error reading zonefile: missing SOA record\n");
00669                                         exit(EXIT_FAILURE);
00670                                 }
00671                                 orig_rrs = ldns_zone_rrs(orig_zone);
00672                                 if (!orig_rrs) {
00673                                         fprintf(stderr,
00674                                                    "Error reading zonefile: no resource records\n");
00675                                         exit(EXIT_FAILURE);
00676                                 }
00677                         }
00678                         fclose(zonefile);
00679                 }
00680         }
00681 
00682         /* read the ZSKs */
00683         argi = 1;
00684         while (argi < argc) {
00685                 keyfile_name_base = argv[argi];
00686                 keyfile_name = LDNS_XMALLOC(char, strlen(keyfile_name_base) + 9);
00687                 snprintf(keyfile_name,
00688                             strlen(keyfile_name_base) + 9,
00689                             "%s.private",
00690                             keyfile_name_base);
00691                 keyfile = fopen(keyfile_name, "r");
00692                 line_nr = 0;
00693                 if (!keyfile) {
00694                         fprintf(stderr,
00695                                    "Error: unable to read %s: %s\n",
00696                                    keyfile_name,
00697                                    strerror(errno));
00698                 } else {
00699                         s = ldns_key_new_frm_fp_l(&key, keyfile, &line_nr);
00700                         fclose(keyfile);
00701                         if (s == LDNS_STATUS_OK) {
00702                                 /* set times in key? they will end up
00703                                    in the rrsigs
00704                                 */
00705                                 if (expiration != 0) {
00706                                         ldns_key_set_expiration(key, expiration);
00707                                 }
00708                                 if (inception != 0) {
00709                                         ldns_key_set_inception(key, inception);
00710                                 }
00711 
00712                                 LDNS_FREE(keyfile_name);
00713                                 
00714                                 ldns_key_list_push_key(keys, key);
00715                         } else {
00716                                 fprintf(stderr, "Error reading key from %s at line %d: %s\n", argv[argi], line_nr, ldns_get_errorstr_by_id(s));
00717                         }
00718                 }
00719                 /* and, if not unset by -p, find or create the corresponding DNSKEY record */
00720                 if (key) {
00721                         find_or_create_pubkey(keyfile_name_base, key,
00722                                               orig_zone, add_keys, ttl);
00723                 }
00724                 argi++;
00725         }
00726         
00727         if (ldns_key_list_key_count(keys) < 1) {
00728                 fprintf(stderr, "Error: no keys to sign with. Aborting.\n\n");
00729                 usage(stderr, prog);
00730                 exit(EXIT_FAILURE);
00731         }
00732 
00733         signed_zone = ldns_dnssec_zone_new();
00734         if (ldns_dnssec_zone_add_rr(signed_zone, ldns_zone_soa(orig_zone)) !=
00735             LDNS_STATUS_OK) {
00736                 fprintf(stderr,
00737                   "Error adding SOA to dnssec zone, skipping record\n");
00738         }
00739         
00740         for (i = 0;
00741              i < ldns_rr_list_rr_count(ldns_zone_rrs(orig_zone));
00742              i++) {
00743                 if (ldns_dnssec_zone_add_rr(signed_zone, 
00744                          ldns_rr_list_rr(ldns_zone_rrs(orig_zone), 
00745                          i)) !=
00746                     LDNS_STATUS_OK) {
00747                         fprintf(stderr,
00748                                 "Error adding RR to dnssec zone");
00749                         fprintf(stderr, ", skipping record:\n");
00750                         ldns_rr_print(stderr, 
00751                           ldns_rr_list_rr(ldns_zone_rrs(orig_zone), i));
00752                 }
00753         }
00754 
00755         /* list to store newly created rrs, so we can free them later */
00756         added_rrs = ldns_rr_list_new();
00757 
00758         if (use_nsec3) {
00759                 result = ldns_dnssec_zone_sign_nsec3_flg_mkmap(signed_zone,
00760                         added_rrs,
00761                         keys,
00762                         ldns_dnssec_default_replace_signatures,
00763                         NULL,
00764                         nsec3_algorithm,
00765                         nsec3_flags,
00766                         nsec3_iterations,
00767                         nsec3_salt_length,
00768                         nsec3_salt,
00769                         signflags,
00770                         (ldns_rbtree_t**) hashmap);
00771         } else {
00772                 result = ldns_dnssec_zone_sign_flg(signed_zone,
00773                                 added_rrs,
00774                                 keys,
00775                                 ldns_dnssec_default_replace_signatures,
00776                                 NULL,
00777                                 signflags);
00778         }
00779         if (result != LDNS_STATUS_OK) {
00780                 fprintf(stderr, "Error signing zone: %s\n",
00781                            ldns_get_errorstr_by_id(result));
00782         }
00783 
00784         if (!outputfile_name) {
00785                 outputfile_name = LDNS_XMALLOC(char, MAX_FILENAME_LEN);
00786                 snprintf(outputfile_name, MAX_FILENAME_LEN, "%s.signed", zonefile_name);
00787         }
00788 
00789         if (signed_zone) {
00790                 if (strncmp(outputfile_name, "-", 2) == 0) {
00791                         ldns_dnssec_zone_print(stdout, signed_zone);
00792                 } else {
00793                         outputfile = fopen(outputfile_name, "w");
00794                         if (!outputfile) {
00795                                 fprintf(stderr, "Unable to open %s for writing: %s\n",
00796                                            outputfile_name, strerror(errno));
00797                         } else {
00798                                 ldns_dnssec_zone_print_fmt(
00799                                                 outputfile, &fmt, signed_zone);
00800                                 fclose(outputfile);
00801                         }
00802                 }
00803         } else {
00804                 fprintf(stderr, "Error signing zone.\n");
00805 
00806 #ifdef HAVE_SSL
00807                 if (ERR_peek_error()) {
00808                         ERR_load_crypto_strings();
00809                         ERR_print_errors_fp(stderr);
00810                         ERR_free_strings();
00811                 }
00812 #endif
00813                 exit(EXIT_FAILURE);
00814         }
00815         
00816         ldns_key_list_free(keys);
00817         /* since the ldns_rr records are pointed to in both the ldns_zone
00818          * and the ldns_dnssec_zone, we can either deep_free the
00819          * dnssec_zone and 'shallow' free the original zone and added
00820          * records, or the other way around
00821          */
00822         ldns_dnssec_zone_free(signed_zone);
00823         ldns_zone_deep_free(orig_zone);
00824         ldns_rr_list_deep_free(added_rrs);
00825         
00826         LDNS_FREE(outputfile_name);
00827         
00828         CRYPTO_cleanup_all_ex_data();
00829 
00830         free(prog);
00831         exit(EXIT_SUCCESS);
00832 }
00833 #else
00834 int
00835 main(int argc, char **argv)
00836 {
00837         fprintf(stderr, "ldns-signzone needs OpenSSL support, which has not been compiled in\n");
00838         return 1;
00839 }
00840 #endif /* HAVE_SSL */