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