OpenDNSSEC-enforcer  2.0.4
kaspcheck.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <syslog.h>
31 
32 #include "config.h"
33 
34 #include "kaspcheck.h"
35 #include "kc_helper.h"
36 
37 #include <libxml/tree.h>
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40 #include <libxml/xpathInternals.h>
41 #include <libxml/relaxng.h>
42 
43 const char *progname = NULL;
44 
45 #define StrFree(ptr) {if(ptr != NULL) {free(ptr); (ptr) = NULL;}}
46 
47 /*
48  * Display usage
49  */
50 static void usage()
51 {
52  fprintf(stderr,
53  "usage: %s [options]\n\n"
54  "Options:\n"
55  " -c, --conf [PATH_TO_CONF_FILE] Path to OpenDNSSEC configuration file\n"
56  " (defaults to %s)\n"
57  " -k, --kasp [PATH_TO_KASP_FILE] Path to KASP policy file\n"
58  " (defaults to the path from the conf.xml file)\n",
59  progname, OPENDNSSEC_CONFIG_FILE);
60  fprintf(stderr,
61  " -z, --zonelist [PATH_TO_ZONELIST_FILE] Path to zonelist file\n"
62  " (defaults to the path from the conf.xml file)\n"
63  " -V, --version Display the version information\n"
64  " -v, --verbose Print extra DEBUG messages\n"
65  " -h, --help Show this message\n");
66 }
67 
68 /*
69  * Fairly basic main.
70  */
71 int main (int argc, char *argv[])
72 {
73  extern int kc_helper_printto_stdout;
74  char *conffile = NULL, *kaspfile = NULL, *zonelistfile = NULL;
75  int status = 0; /* Will be non-zero on error (NOT warning) */
76  char **repo_list = NULL;
77  int repo_count = 0;
78  int ch, i, verbose = 0, option_index = 0;
79  static struct option long_options[] =
80  {
81  {"config", required_argument, 0, 'c'},
82  {"help", no_argument, 0, 'h'},
83  {"kasp", required_argument, 0, 'k'},
84  {"zonelist", required_argument, 0, 'z'},
85  {"version", no_argument, 0, 'V'},
86  {"verbose", no_argument, 0, 'v'},
87  {0,0,0,0}
88  };
89  char **policy_names = NULL;
90  int policy_count = 0;
91 
92  /* The program name is the last component of the program file name */
93  if ((progname = strrchr(argv[0], '/'))) { /* EQUALS */
94  ++progname; /* Point to character after last "/" */
95  } else {
96  progname = argv[0];
97  }
98 
99  while ((ch = getopt_long(argc, argv, "c:hk:Vvz:", long_options, &option_index)) != -1)
100  {
101  switch (ch)
102  {
103  case 'c':
104  conffile = StrStrdup(optarg);
105  break;
106  case 'h':
107  usage();
108  exit(0);
109  break;
110  case 'k':
111  kaspfile = StrStrdup(optarg);
112  break;
113  case 'z':
114  zonelistfile = StrStrdup(optarg);
115  break;
116  case 'V':
117  printf("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
118  exit(0);
119  break;
120  case 'v':
121  verbose = 1;
122  break;
123  }
124  }
125 
126  kc_helper_printto_stdout = 1;
127 
128  if (!conffile)
129  conffile = StrStrdup((char *)OPENDNSSEC_CONFIG_FILE);
130 
131  /* 0) Some basic setup */
133  /* 1) Check on conf.xml - set kasp.xml (if -k flag not given) */
134  status = check_conf(conffile, &kaspfile, &zonelistfile, &repo_list,
135  &repo_count, verbose);
136  /* 2) Checks on kasp.xml */
137  status += check_kasp(kaspfile, repo_list, repo_count, verbose,
138  &policy_names, &policy_count);
139  /* 3) Checks on zonelist.xml */
140  status += check_zonelist(zonelistfile, verbose, policy_names, policy_count);
141 
142  for (i = 0; i < policy_count; i++) {
143  free(policy_names[i]);
144  }
145  free(policy_names);
146 
147  xmlCleanupParser();
148  for (i = 0; i < repo_count; i++) StrFree(repo_list[i]);
149  StrFree(repo_list);
150  StrFree(conffile);
151  StrFree(kaspfile);
152  StrFree(zonelistfile);
153 
154  if (verbose)
155  dual_log("DEBUG: finished %d", status);
156  return status;
157 }
char * StrStrdup(const char *string)
Definition: kc_helper.c:1266
int check_conf(const char *conf, char **kasp, char **zonelist, char ***repo_listout, int *repo_countout, int verbose)
Definition: kc_helper.c:1395
int check_kasp(const char *kasp, char **repo_list, int repo_count, int verbose, char ***policy_names_out, int *policy_count_out)
Definition: kc_helper.c:1755
int check_zonelist(const char *zonelist, int verbose, char **policy_names, int policy_count)
Definition: kc_helper.c:1679
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
int main(int argc, char *argv[])
Definition: kaspcheck.c:71
#define DEFAULT_LOG_FACILITY
Definition: kc_helper.h:33
int kc_helper_printto_stdout
Definition: kc_helper.c:49
const char * progname
Definition: kaspcheck.c:43
void log_init(int facility, const char *program_name)
Definition: kc_helper.c:51
void dual_log(const char *format,...)
Definition: kc_helper.c:59
#define StrFree(ptr)
Definition: kaspcheck.c:45