xref: /freebsd/sbin/ipf/libipf/optname.c (revision 315ee00f)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include "ipf.h"
11 
12 
13 u_32_t
14 optname(char ***cp, u_short *sp, int linenum)
15 {
16 	struct ipopt_names *io, *so;
17 	u_long msk = 0;
18 	u_short smsk = 0;
19 	char *s;
20 	int sec = 0;
21 
22 	for (s = strtok(**cp, ","); s; s = strtok(NULL, ",")) {
23 		for (io = ionames; io->on_name; io++)
24 			if (!strcasecmp(s, io->on_name)) {
25 				msk |= io->on_bit;
26 				break;
27 			}
28 		if (!io->on_name) {
29 			fprintf(stderr, "%d: unknown IP option name %s\n",
30 				linenum, s);
31 			return (0);
32 		}
33 		if (!strcasecmp(s, "sec-class"))
34 			sec = 1;
35 	}
36 
37 	if (sec && !*(*cp + 1)) {
38 		fprintf(stderr, "%d: missing security level after sec-class\n",
39 			linenum);
40 		return (0);
41 	}
42 
43 	if (sec) {
44 		(*cp)++;
45 		for (s = strtok(**cp, ","); s; s = strtok(NULL, ",")) {
46 			for (so = secclass; so->on_name; so++)
47 				if (!strcasecmp(s, so->on_name)) {
48 					smsk |= so->on_bit;
49 					break;
50 				}
51 			if (!so->on_name) {
52 				fprintf(stderr,
53 					"%d: no such security level: %s\n",
54 					linenum, s);
55 				return (0);
56 			}
57 		}
58 		if (smsk)
59 			*sp = smsk;
60 	}
61 	return (msk);
62 }
63