Annotation of embedaddon/sudo/plugins/sudoers/regress/parser/check_addr.c, revision 1.1.1.4
1.1 misho 1: /*
1.1.1.3 misho 2: * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 misho 3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
17: #include <config.h>
18:
19: #include <sys/types.h>
20: #include <sys/socket.h>
21: #include <stdio.h>
22: #ifdef STDC_HEADERS
23: # include <stdlib.h>
24: # include <stddef.h>
25: #else
26: # ifdef HAVE_STDLIB_H
27: # include <stdlib.h>
28: # endif
29: #endif /* STDC_HEADERS */
30: #include <stdarg.h>
31: #ifdef HAVE_STRING_H
32: # include <string.h>
33: #endif /* HAVE_STRING_H */
34: #ifdef HAVE_STRINGS_H
35: # include <strings.h>
36: #endif /* HAVE_STRINGS_H */
37: #include <ctype.h>
38: #include <errno.h>
39: #include <grp.h>
40: #include <pwd.h>
41:
42: #include <netinet/in.h>
43: #include <arpa/inet.h>
44:
1.1.1.2 misho 45: #define SUDO_ERROR_WRAP 0
46:
1.1 misho 47: #include "sudoers.h"
48: #include "parse.h"
49: #include "interfaces.h"
50:
1.1.1.3 misho 51: __dso_public int main(int argc, char *argv[]);
1.1.1.2 misho 52:
1.1 misho 53: static int
54: check_addr(char *input)
55: {
56: int expected, matched;
1.1.1.4 ! misho 57: const char *errstr;
1.1 misho 58: size_t len;
59: char *cp;
60:
61: while (isspace((unsigned char)*input))
62: input++;
63:
64: /* input: "addr[/mask] 1/0" */
65: len = strcspn(input, " \t");
66: cp = input + len;
67: while (isspace((unsigned char)*cp))
68: cp++;
1.1.1.4 ! misho 69: expected = strtonum(cp, 0, 1, &errstr);
! 70: if (errstr != NULL)
! 71: fatalx("expecting 0 or 1, got %s", cp);
1.1 misho 72: input[len] = '\0';
73:
74: matched = addr_matches(input);
75: if (matched != expected) {
76: warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
77: return 1;
78: }
79: return 0;
80: }
81:
82: static void
83: usage(void)
84: {
1.1.1.4 ! misho 85: fprintf(stderr, "usage: %s datafile\n", getprogname());
1.1 misho 86: exit(1);
87: }
88:
89: int
90: main(int argc, char *argv[])
91: {
92: int ntests = 0, errors = 0;
93: char *cp, line[2048];
94: size_t len;
95: FILE *fp;
96:
1.1.1.4 ! misho 97: initprogname(argc > 0 ? argv[0] : "check_addr");
1.1 misho 98:
99: if (argc != 2)
100: usage();
101:
102: fp = fopen(argv[1], "r");
103: if (fp == NULL)
1.1.1.3 misho 104: fatalx("unable to open %s", argv[1]);
1.1 misho 105:
106: /*
107: * Input is in the following format. There are two types of
108: * lines: interfaces, which sets the address and mask of the
109: * locally connected ethernet interfaces for the lines that
110: * follow and, address lines that include and address (with
111: * optional netmask) to match, followed by expected match status
112: * (1 or 0). E.g.
113: *
114: * interfaces: addr1/mask addr2/mask ...
115: * address: addr[/mask] 1/0
116: * address: addr[/mask] 1/0
117: * interfaces: addr3/mask addr4/mask ...
118: * address: addr[/mask] 1/0
119: */
120:
121: while (fgets(line, sizeof(line), fp) != NULL) {
122: len = strcspn(line, "\n");
123: line[len] = '\0';
124:
125: /* Ignore comments */
126: if ((cp = strchr(line, '#')) != NULL)
127: *cp = '\0';
128:
129: /* Skip blank lines. */
130: if (line[0] == '\0')
131: continue;
132:
133: if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
134: set_interfaces(line + sizeof("interfaces:") - 1);
135: } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
136: errors += check_addr(line + sizeof("address:") - 1);
137: ntests++;
138: } else {
139: warningx("unexpected data line: %s\n", line);
140: continue;
141: }
142: }
143:
144: printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
145: ntests, errors, (ntests - errors) * 100 / ntests);
146:
147: exit(errors);
148: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>