summaryrefslogtreecommitdiff
path: root/keama/keama.c
blob: 401ddf2c0bdfabf73940ef6034c893dab7bfee4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright(c) 2017-2019 by Internet Systems Consortium, Inc.("ISC")
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *   Internet Systems Consortium, Inc.
 *   PO Box 360
 *   Newmarket, NH 03857 USA
 *   <info@isc.org>
 *   https://www.isc.org/
 *
 */

#include <sys/errno.h>
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "keama.h"

#define KEAMA_USAGE	"Usage: keama [-4|-6] [-D] [-N]" \
			" [-r {perform|fatal|pass}\\n" \
			" [-l hook-library-path]" \
			" [-i input-file] [-o output-file]\n"

static void
usage(const char *sfmt, const char *sarg) {
	if (sfmt != NULL) {
		fprintf(stderr, sfmt, sarg);
		fprintf(stderr, "\n");
	}
	fputs(KEAMA_USAGE, stderr);
	exit(1);
}

enum resolve resolve;
struct parses parses;

int local_family = 0;
char *hook_library_path = NULL;
char *input_file = NULL;
char *output_file = NULL;
FILE *input = NULL;
FILE *output = NULL;
isc_boolean_t use_isc_lifetimes = ISC_FALSE;
isc_boolean_t global_hr = ISC_TRUE;
isc_boolean_t json = ISC_FALSE;

static const char use_noarg[] = "No argument for command: %s";
static const char bad_resolve[] = "Bad -r argument: %s";

int
main(int argc, char **argv) {
	int i, fd;
	char *inbuf = NULL;
	size_t oldsize = 0;
	size_t newsize = 0;
	ssize_t cc;
	struct parse *cfile;
	size_t cnt = 0;

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-4") == 0)
			local_family = AF_INET;
		else if (strcmp(argv[i], "-6") == 0)
			local_family = AF_INET6;
		else if (strcmp(argv[i], "-D") == 0)
			use_isc_lifetimes = ISC_TRUE;
		else if (strcmp(argv[i], "-N") == 0)
			global_hr = ISC_FALSE;
		else if (strcmp(argv[i], "-T") == 0)
			json = ISC_TRUE;
		else if (strcmp(argv[i], "-r") == 0) {
			if (++i == argc)
				usage(use_noarg, argv[i -  1]);
			if (strcmp(argv[i], "perform") == 0)
				resolve = perform;
			else if (strcmp(argv[i], "fatal") == 0)
				resolve = fatal;
			else if (strcmp(argv[i], "pass") == 0)
				resolve = pass;
			else
				usage(bad_resolve, argv[i]);
		} else if (strcmp(argv[i], "-l") == 0) {
			if (++i == argc)
				usage(use_noarg, argv[i -  1]);
			hook_library_path = argv[i];
		} else if (strcmp(argv[i], "-i") == 0) {
			if (++i == argc)
				usage(use_noarg, argv[i -  1]);
			input_file = argv[i];
		} else if (strcmp(argv[i], "-o") == 0) {
			if (++i == argc)
				usage(use_noarg, argv[i -  1]);
			output_file = argv[i];
 		} else
			usage("Unknown command: %s", argv[i]);
	}

	if (!json && (local_family == 0))
		usage("address family must be set using %s", "-4 or -6");

	if (input_file == NULL) {
		input_file = "--stdin--";
		fd = fileno(stdin);
		for (;;) {
			if (newsize == 0)
				newsize = 1024;
			else {
				oldsize = newsize;
				newsize *= 4;
			}
			inbuf = (char *)realloc(inbuf, newsize);
			if (inbuf == 0)
				usage("out of memory reading standard "
				      "input: %s", strerror(errno));
			cc = read(fd, inbuf + oldsize, newsize - oldsize);
			if (cc < 0)
				usage("error reading standard input: %s",
				      strerror(errno));
			if (cc + oldsize < newsize) {
				newsize = cc + oldsize;
				break;
			}
		}
	} else {
		fd = open(input_file, O_RDONLY);
		if (fd < 0)
			usage("Cannot open '%s' for reading", input_file);
	}

	if (output_file) {
		output = fopen(output_file, "w");
		if (output == NULL)
			usage("Cannot open '%s' for writing", output_file);
	} else
		output = stdout;

	TAILQ_INIT(&parses);
	cfile = new_parse(fd, inbuf, newsize, input_file, 0);
	assert(cfile != NULL);

	if (json) {
		struct element *elem;

		elem = json_parse(cfile);
		if (elem != NULL) {
			print(output, elem, 0, 0);
			fprintf(output, "\n");
		}
	} else {
		spaces_init();
		options_init();
		cnt = conf_file_parse(cfile);
		if (cfile->stack_top > 0) {
			print(output, cfile->stack[0], 0, 0);
			fprintf(output, "\n");
		}
	}

	end_parse(cfile);

	exit(cnt);
}

void
stackPush(struct parse *pc, struct element *elem)
{
	if (pc->stack_top + 2 >= pc->stack_size) {
		size_t new_size = pc->stack_size + 10;
		size_t amount = new_size * sizeof(struct element *);

		pc->stack = (struct element **)realloc(pc->stack, amount);
		if (pc->stack == NULL)
			parse_error(pc, "can't resize element stack");
		pc->stack_size = new_size;
	}
	pc->stack_top++;
	pc->stack[pc->stack_top] = elem;
}

void
parse_error(struct parse *cfile, const char *fmt, ...)
{
	va_list list;
	char lexbuf[256];
	char mbuf[1024];
	char fbuf[1024];
	unsigned i, lix;

	snprintf(fbuf, sizeof(fbuf), "%s line %d: %s",
		 cfile->tlname, cfile->lexline, fmt);

	va_start(list, fmt);
	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
	va_end(list);

	lix = 0;
	for (i = 0;
	     cfile->token_line[i] && i < (cfile->lexchar - 1); i++) {
		if (lix < sizeof(lexbuf) - 1)
			lexbuf[lix++] = ' ';
		if (cfile->token_line[i] == '\t') {
			for (; lix < (sizeof lexbuf) - 1 && (lix & 7); lix++)
				lexbuf[lix] = ' ';
		}
	}
	lexbuf[lix] = 0;

	fprintf(stderr, "%s\n%s\n", mbuf, cfile->token_line);
	if (cfile->lexchar < 81)
		fprintf(stderr, "%s^\n", lexbuf);
	exit(-1);
}