nsd-checkzone.c revision 1.3
1/* 2 * nsd-checkzone.c -- nsd-checkzone(8) checks zones for syntax errors 3 * 4 * Copyright (c) 2013, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 10#include "config.h" 11 12#include <assert.h> 13#include <stdio.h> 14#include <stdlib.h> 15#include <string.h> 16#include <time.h> 17#include <unistd.h> 18#include <errno.h> 19 20#include "nsd.h" 21#include "options.h" 22#include "util.h" 23#include "zonec.h" 24 25struct nsd nsd; 26 27/* 28 * Print the help text. 29 * 30 */ 31static void 32usage (void) 33{ 34 fprintf(stderr, "Usage: nsd-checkzone <zone name> <zone file>\n"); 35 fprintf(stderr, "Version %s. Report bugs to <%s>.\n", 36 PACKAGE_VERSION, PACKAGE_BUGREPORT); 37} 38 39static void 40check_zone(struct nsd* nsd, const char* name, const char* fname) 41{ 42 const dname_type* dname; 43 zone_options_type* zo; 44 zone_type* zone; 45 unsigned errors; 46 47 /* init*/ 48 nsd->db = namedb_open("", nsd->options); 49 dname = dname_parse(nsd->options->region, name); 50 if(!dname) { 51 /* parse failure */ 52 error("cannot parse zone name '%s'", name); 53 } 54 zo = zone_options_create(nsd->options->region); 55 memset(zo, 0, sizeof(*zo)); 56 zo->node.key = dname; 57 zo->name = name; 58 zone = namedb_zone_create(nsd->db, dname, zo); 59 60 /* read the zone */ 61 errors = zonec_read(name, fname, zone); 62 if(errors > 0) { 63 printf("zone %s file %s has %u errors\n", name, fname, errors); 64 exit(1); 65 } 66 printf("zone %s is ok\n", name); 67 namedb_close(nsd->db); 68} 69 70/* dummy functions to link */ 71int writepid(struct nsd * ATTR_UNUSED(nsd)) 72{ 73 return 0; 74} 75void unlinkpid(const char * ATTR_UNUSED(file)) 76{ 77} 78void bind8_stats(struct nsd * ATTR_UNUSED(nsd)) 79{ 80} 81 82void sig_handler(int ATTR_UNUSED(sig)) 83{ 84} 85 86extern char *optarg; 87extern int optind; 88 89int 90main(int argc, char *argv[]) 91{ 92 /* Scratch variables... */ 93 int c; 94 struct nsd nsd; 95 memset(&nsd, 0, sizeof(nsd)); 96 97 log_init("nsd-checkzone"); 98 99 /* Parse the command line... */ 100 while ((c = getopt(argc, argv, "h")) != -1) { 101 switch (c) { 102 case 'h': 103 usage(); 104 exit(0); 105 case '?': 106 default: 107 usage(); 108 exit(1); 109 } 110 } 111 argc -= optind; 112 argv += optind; 113 114 /* Commandline parse error */ 115 if (argc != 2) { 116 fprintf(stderr, "wrong number of arguments.\n"); 117 usage(); 118 exit(1); 119 } 120 121 nsd.options = nsd_options_create(region_create_custom(xalloc, free, 122 DEFAULT_CHUNK_SIZE, DEFAULT_LARGE_OBJECT_SIZE, 123 DEFAULT_INITIAL_CLEANUP_SIZE, 1)); 124 if (verbosity == 0) 125 verbosity = nsd.options->verbosity; 126 127 check_zone(&nsd, argv[0], argv[1]); 128 region_destroy(nsd.options->region); 129 /* yylex_destroy(); but, not available in all versions of flex */ 130 131 exit(0); 132} 133