summaryrefslogtreecommitdiff
path: root/usr.sbin/bgpctl/irr_parser.c
blob: f9fe2e2f1493a6d75c4186651a6687b59985a034 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*	$OpenBSD: irr_parser.c,v 1.4 2007/03/04 12:37:07 henning Exp $ */

/*
 * Copyright (c) 2007 Henning Brauer <henning@openbsd.org>
 *
 * 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 THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, 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.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "irrfilter.h"

#define PARSEBUF_INCREMENT 4096

int	 lineno;
char	*parsebuf = NULL;
size_t	 parsebuflen = 0;

void	 grow_parsebuf(void);
char	*irr_getln(FILE *f);
int	 parse_policy(char *, char *);
int	 policy_additem(char *, struct policy_item *);
int	 parse_asset(char *, char *);
int	 parse_route(char *, char *);

/*
 * parse_response() return values:
 * -1	error
 * 0	object not found
 * >0	number of lines matched plus 1
 */
int
parse_response(FILE *f, enum qtype qtype)
{
	char	*key, *val;
	int	 cnt, n;

	lineno = 1;
	cnt = 1;
	while ((val = irr_getln(f)) != NULL) {
		if (!strncmp(val, "%ERROR:101:", 11))	/* no entries found */
			return (0);

		if (val[0] == '%') {
			warnx("message from whois server: %s", val);
			return (-1);
		}

		key = strsep(&val, ":");
		if (val == NULL) {
			warnx("%u: %s", lineno, key);
			warnx("no \":\" found!");
			return (-1);
		}
		while (ISWS(*val))
			val++;

		switch (qtype) {
		case QTYPE_OWNAS:
			if ((n = parse_policy(key, val)) == -1)
				return (-1);
			break;
		case QTYPE_ASSET:
			if ((n = parse_asset(key, val)) == -1)
				return (-1);
			break;
		case QTYPE_ROUTE:
			if ((n = parse_route(key, val)) == -1)
				return (-1);
			break;
		default:
			err(1, "king bula suffers from dementia");
		}
		cnt += n;
	}

	return (cnt);
}

void
grow_parsebuf(void)
{
	char	*p;
	size_t	 newlen;

	newlen = parsebuflen + PARSEBUF_INCREMENT;
	if ((p = realloc(parsebuf, newlen)) == NULL)
		err(1, "grow_parsebuf realloc");
	parsebuf = p;
	parsebuflen = newlen;

	if (0)
		fprintf(stderr, "parsebuf now %lu bytes\n", (ulong)parsebuflen);
}

char *
irr_getln(FILE *f)
{
	int	 c, next, last;
	char	*p;

	if (parsebuf == NULL)
		grow_parsebuf();
	p = parsebuf;
	last = -1;

	do {
		c = getc(f);

		if (p == parsebuf) {	/* beginning of new line */
			if (c == '%') {
				next = getc(f);
				switch (next) {
				case ' ':		/* comment. skip over */
					while ((c = getc(f)) != '\n' &&
					    c != EOF)
						; /* nothing */
					break;
				case '\n':
				case EOF:
					c = next;
					break;
				default:
					ungetc(next, f);
					break;
				}
			}
		}

		if (c == '#') /* skip until \n */
			while ((c = getc(f)) != '\n' && c != EOF)
				; /* nothing */

		if (c == '\n') {
			lineno++;
			next = getc(f);
			if (next == '+')	/* continuation, skip the + */
				c = getc(f);
			else if (ISWS(next))	/* continuation */
				c = next;
			else
				ungetc(next, f);
		}


		if (c == '\n' || c == EOF) {
			if (c == EOF)
				if (ferror(f))
					err(1, "ferror");
			if (p > parsebuf) {
				*p = '\0';
				return (parsebuf);
			}
		} else {
			if (!(ISWS(c) && ISWS(last))) {
				if (p + 1 >= parsebuf + parsebuflen - 1) {
					size_t	offset;

					offset = p - parsebuf;
					grow_parsebuf();
					p = parsebuf + offset;
				}
				if (ISWS(c)) /* equal opportunity whitespace */
					*p++ = ' ';
				else
					*p++ = (char)c;
			}
			last = c;
		}
	} while (c != EOF);

	return (NULL);
}

/*
 * parse the policy from an aut-num object
 */

enum policy_parser_st {
	PO_NONE,
	PO_PEER_KEY,
	PO_PEER_AS,
	PO_PEER_ADDR,
	PO_RTR_KEY,
	PO_RTR_ADDR,
	PO_ACTION_KEY,
	PO_ACTION_SPEC,
	PO_FILTER_KEY,
	PO_FILTER_SPEC
};

int
parse_policy(char *key, char *val)
{
	struct policy_item	*pi;
	enum pdir		 dir;
	enum policy_parser_st	 st = PO_NONE, nextst;
	char			*tok, *router = "", *p;

	if (!strcmp(key, "import"))
		dir = IMPORT;
	else if (!strcmp(key, "export"))
		dir = EXPORT;
	else				/* ignore! */
		return (0);

	if (dir == EXPORT && (irrflags & F_IMPORTONLY))
		return (0);

	if ((pi = calloc(1, sizeof(*pi))) == NULL)
		err(1, "parse_policy calloc");
	pi->dir = dir;

	while ((tok = strsep(&val, " ")) != NULL) {
		nextst = PO_NONE;
		if (dir == IMPORT) {
			if (!strcmp(tok, "from"))
				nextst = PO_PEER_KEY;
			else if (!strcmp(tok, "at"))
				nextst = PO_RTR_KEY;
			else if (!strcmp(tok, "action"))
				nextst = PO_ACTION_KEY;
			else if (!strcmp(tok, "accept"))
				nextst = PO_FILTER_KEY;
		} else if (dir == EXPORT) {
			if (!strcmp(tok, "to"))
				nextst = PO_PEER_KEY;
			else if (!strcmp(tok, "at"))
				nextst = PO_RTR_KEY;
			else if (!strcmp(tok, "announce"))
				nextst = PO_FILTER_KEY;
		}

		if (nextst == PO_FILTER_KEY) /* rest is filter spec */
			if ((pi->filter = strdup(val)) == NULL)
				err(1, NULL);

		if (nextst == PO_ACTION_KEY) {
			/* action list. ends after last ; */
			p = strrchr(val, ';');
			if (p == NULL || !ISWS(*++p))
				errx(1, "syntax error in action spec");
			*p = '\0';
			if ((pi->action = strdup(val)) == NULL)
				err(1, NULL);
			val = ++p;
			while (ISWS(*p))
				p++;
		}
			
		switch (st) {
		case PO_NONE:
			if (nextst != PO_PEER_KEY)
				goto ppoerr;
			st = nextst;
			break;
		case PO_PEER_KEY:
			if (pi->peer_as == NULL) {
				if (nextst != PO_NONE)
					goto ppoerr;
				if ((pi->peer_as = strdup(tok)) == NULL)
					err(1, NULL);
			} else {
				switch (nextst) {
				case PO_NONE:
					if (!strcasecmp(tok, "and") ||
					    !strcasecmp(tok, "or") ||
					    !strcasecmp(tok, "not"))
						fprintf(stderr, "compound peering "
						    "statements are not supported");
					 else	/* peer address */
						if ((pi->peer_addr = strdup(tok)) == NULL)
							err(1, NULL);
					break;
				case PO_RTR_KEY:
				case PO_ACTION_KEY:
				case PO_FILTER_KEY:
					st = nextst;
					break;
				default:
					goto ppoerr;
				}
			}
			break;
		case PO_PEER_AS:
		case PO_PEER_ADDR:
			err(1, "state error");
			break;
		case PO_RTR_KEY:
			if (nextst != PO_NONE)
				goto ppoerr;
			/* rtr address */
			if ((router = strdup(tok)) == NULL)
				err(1, NULL);
			st = PO_RTR_ADDR;
			break;
		case PO_RTR_ADDR:
			if (nextst != PO_ACTION_KEY &&
			    nextst != PO_FILTER_KEY)
				goto ppoerr;
			st = nextst;
			break;
		case PO_ACTION_KEY:
			/* already handled, next must be FILTER_KEY */
			if (nextst != PO_FILTER_KEY)
				goto ppoerr;
			st = nextst;
			break;
		case PO_FILTER_KEY:
			/* already handled */
			break;
		case PO_ACTION_SPEC:
		case PO_FILTER_SPEC:
			err(1, "state error");
			break;
		}
	}

	if (st != PO_FILTER_KEY)
		err(1, "state error");

	if (policy_additem(router, pi) == -1)
		return (-1);

	return (1);

ppoerr:
	free(pi);
	fprintf(stderr, "%u: parse error\n", lineno);
	return (-1);
}

int
policy_additem(char *router, struct policy_item *pi)
{
	struct router	*r;

	for (r = TAILQ_FIRST(&router_head); r != NULL &&
	    strcmp(r->address, router); r = TAILQ_NEXT(r, entry))
		; /* nothing */

	if (r == NULL) {
		if ((r = calloc(1, sizeof(*r))) == NULL ||
		    (r->address = strdup(router)) == NULL)
			err(1, NULL);
		TAILQ_INIT(&r->policy_h);
		TAILQ_INSERT_TAIL(&router_head, r, entry);
	}

	TAILQ_INSERT_TAIL(&r->policy_h, pi, entry);

	return (0);
}

/*
 * parse as-set: get members
 */

int
parse_asset(char *key, char *val)
{
	char	*tok;

	if (strcmp(key, "members"))	/* ignore everything else */
		return (0);

	while ((tok = strsep(&val, ",")) != NULL) {
		EATWS(tok);
		if (tok[0] != '\0')
			asset_addmember(tok);
	}

	return (1);
}

/*
 * parse route obj: just get the prefix
 */
int
parse_route(char *key, char *val)
{
	if (strcmp(key, "route"))	/* ignore everything else */
		return (0);

	return(prefixset_addmember(val));
}