1 /*  Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 
3     This program is free software: you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation, either version 3 of the License, or
6     (at your option) any later version.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
15  */
16 
17 #include <getopt.h>
18 #include <stdio.h>
19 
20 #include "contrib/strtonum.h"
21 #include "knot/common/log.h"
22 #include "utils/common/params.h"
23 #include "utils/knotc/commands.h"
24 #include "utils/knotc/interactive.h"
25 #include "utils/knotc/process.h"
26 
27 #define PROGRAM_NAME		"knotc"
28 #define SPACE			"  "
29 
print_help(void)30 static void print_help(void)
31 {
32 	printf("Usage: %s [parameters] <action> [action_args]\n"
33 	       "\n"
34 	       "Parameters:\n"
35 	       " -c, --config <file>      "SPACE"Use a textual configuration file.\n"
36 	       "                          "SPACE" (default %s)\n"
37 	       " -C, --confdb <dir>       "SPACE"Use a binary configuration database directory.\n"
38 	       "                          "SPACE" (default %s)\n"
39 	       " -m, --max-conf-size <MiB>"SPACE"Set maximum size of the configuration database (max 10000 MiB).\n"
40 	       "                          "SPACE" (default %d MiB)\n"
41 	       " -s, --socket <path>      "SPACE"Use a control UNIX socket path.\n"
42 	       "                          "SPACE" (default %s)\n"
43 	       " -t, --timeout <sec>      "SPACE"Use a control socket timeout (max 86400 seconds).\n"
44 	       "                          "SPACE" (default %u seconds)\n"
45 	       " -b, --blocking	          "SPACE"Zone event trigger commands wait until the event is finished.\n"
46 	       " -f, --force              "SPACE"Forced operation. Overrides some checks.\n"
47 	       " -v, --verbose            "SPACE"Enable debug output.\n"
48 	       " -h, --help               "SPACE"Print the program help.\n"
49 	       " -V, --version            "SPACE"Print the program version.\n",
50 	       PROGRAM_NAME, CONF_DEFAULT_FILE, CONF_DEFAULT_DBDIR,
51 	       CONF_MAPSIZE, RUN_DIR "/knot.sock", DEFAULT_CTL_TIMEOUT_MS / 1000);
52 
53 	print_commands();
54 }
55 
56 params_t params = {
57 	.max_conf_size = (size_t)CONF_MAPSIZE * 1024 * 1024,
58 	.timeout = -1
59 };
60 
main(int argc,char ** argv)61 int main(int argc, char **argv)
62 {
63 	/* Long options. */
64 	struct option opts[] = {
65 		{ "config",        required_argument, NULL, 'c' },
66 		{ "confdb",        required_argument, NULL, 'C' },
67 		{ "max-conf-size", required_argument, NULL, 'm' },
68 		{ "socket",        required_argument, NULL, 's' },
69 		{ "timeout",       required_argument, NULL, 't' },
70 		{ "blocking",      no_argument,       NULL, 'b' },
71 		{ "force",         no_argument,       NULL, 'f' },
72 		{ "verbose",       no_argument,       NULL, 'v' },
73 		{ "help",          no_argument,       NULL, 'h' },
74 		{ "version",       no_argument,       NULL, 'V' },
75 		{ NULL }
76 	};
77 
78 	/* Set the time zone. */
79 	tzset();
80 
81 	/* Parse command line arguments */
82 	int opt = 0;
83 	while ((opt = getopt_long(argc, argv, "+c:C:m:s:t:bfvhV", opts, NULL)) != -1) {
84 		switch (opt) {
85 		case 'c':
86 			params.config = optarg;
87 			break;
88 		case 'C':
89 			params.confdb = optarg;
90 			break;
91 		case 'm':
92 			if (str_to_size(optarg, &params.max_conf_size, 1, 10000) != KNOT_EOK) {
93 				print_help();
94 				return EXIT_FAILURE;
95 			}
96 			/* Convert to bytes. */
97 			params.max_conf_size *= 1024 * 1024;
98 			break;
99 		case 's':
100 			params.socket = optarg;
101 			break;
102 		case 't':
103 			if (str_to_int(optarg, &params.timeout, 0, 86400) != KNOT_EOK) {
104 				print_help();
105 				return EXIT_FAILURE;
106 			}
107 			/* Convert to milliseconds. */
108 			params.timeout *= 1000;
109 			break;
110 		case 'b':
111 			params.blocking = true;
112 			break;
113 		case 'f':
114 			params.force = true;
115 			break;
116 		case 'v':
117 			params.verbose = true;
118 			break;
119 		case 'h':
120 			print_help();
121 			return EXIT_SUCCESS;
122 		case 'V':
123 			print_version(PROGRAM_NAME);
124 			return EXIT_SUCCESS;
125 		default:
126 			print_help();
127 			return EXIT_FAILURE;
128 		}
129 	}
130 
131 	/* Set up simplified logging just to stdout/stderr. */
132 	log_init();
133 	log_levels_set(LOG_TARGET_STDOUT, LOG_SOURCE_ANY,
134 	               LOG_MASK(LOG_INFO) | LOG_MASK(LOG_NOTICE));
135 	log_levels_set(LOG_TARGET_STDERR, LOG_SOURCE_ANY, LOG_UPTO(LOG_WARNING));
136 	log_levels_set(LOG_TARGET_SYSLOG, LOG_SOURCE_ANY, 0);
137 	log_flag_set(LOG_FLAG_NOTIMESTAMP | LOG_FLAG_NOINFO);
138 	if (params.verbose) {
139 		log_levels_add(LOG_TARGET_STDOUT, LOG_SOURCE_ANY, LOG_MASK(LOG_DEBUG));
140 	}
141 
142 	int ret;
143 	if (argc - optind < 1) {
144 		ret = interactive_loop(&params);
145 	} else {
146 		ret = process_cmd(argc - optind, (const char **)argv + optind, &params);
147 	}
148 
149 	log_close();
150 
151 	return (ret == KNOT_EOK) ? EXIT_SUCCESS : EXIT_FAILURE;
152 }
153