]> granicus.if.org Git - sudo/blob - plugins/sudoers/regress/parser/check_addr.c
5f67d4dca1a04c567f581574adf79e983ce3496f
[sudo] / plugins / sudoers / regress / parser / check_addr.c
1 /*
2  * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@sudo.ws>
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 #include <stdlib.h>
23 #include <stdarg.h>
24 #ifdef HAVE_STRING_H
25 # include <string.h>
26 #endif /* HAVE_STRING_H */
27 #ifdef HAVE_STRINGS_H
28 # include <strings.h>
29 #endif /* HAVE_STRINGS_H */
30 #include <ctype.h>
31 #include <errno.h>
32 #include <grp.h>
33 #include <pwd.h>
34
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #define SUDO_ERROR_WRAP 0
39
40 #include "sudoers.h"
41 #include "interfaces.h"
42
43 __dso_public int main(int argc, char *argv[]);
44
45 static int
46 check_addr(char *input)
47 {
48     int expected, matched;
49     const char *errstr;
50     size_t len;
51     char *cp;
52
53     while (isspace((unsigned char)*input))
54         input++;
55
56     /* input: "addr[/mask] 1/0" */
57     len = strcspn(input, " \t");
58     cp = input + len;
59     while (isspace((unsigned char)*cp))
60         cp++;
61     expected = strtonum(cp, 0, 1, &errstr);
62     if (errstr != NULL)
63         sudo_fatalx("expecting 0 or 1, got %s", cp);
64     input[len] = '\0';
65
66     matched = addr_matches(input);
67     if (matched != expected) {
68         sudo_warnx("%s %smatched: FAIL", input, matched ? "" : "not ");
69         return 1;
70     }
71     return 0;
72 }
73
74 static void
75 usage(void)
76 {
77     fprintf(stderr, "usage: %s datafile\n", getprogname());
78     exit(1);
79 }
80
81 int
82 main(int argc, char *argv[])
83 {
84     int ntests = 0, errors = 0;
85     char *cp, line[2048];
86     size_t len;
87     FILE *fp;
88
89     initprogname(argc > 0 ? argv[0] : "check_addr");
90
91     if (argc != 2)
92         usage();
93
94     fp = fopen(argv[1], "r");
95     if (fp == NULL)
96         sudo_fatalx("unable to open %s", argv[1]);
97
98     /*
99      * Input is in the following format.  There are two types of
100      * lines: interfaces, which sets the address and mask of the
101      * locally connected ethernet interfaces for the lines that
102      * follow and, address lines that include and address (with
103      * optional netmask) to match, followed by expected match status
104      * (1 or 0).  E.g.
105      *
106      * interfaces: addr1/mask addr2/mask ...
107      * address: addr[/mask] 1/0
108      * address: addr[/mask] 1/0
109      * interfaces: addr3/mask addr4/mask ...
110      * address: addr[/mask] 1/0
111      */
112
113     while (fgets(line, sizeof(line), fp) != NULL) {
114         len = strcspn(line, "\n");
115         line[len] = '\0';
116
117         /* Ignore comments */
118         if ((cp = strchr(line, '#')) != NULL)
119             *cp = '\0';
120
121         /* Skip blank lines. */
122         if (line[0] == '\0')
123             continue;
124
125         if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
126             if (!set_interfaces(line + sizeof("interfaces:") - 1)) {
127                 sudo_warn("unable to parse interfaces list");
128                 errors++;
129             }
130         } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
131             errors += check_addr(line + sizeof("address:") - 1);
132             ntests++;
133         } else {
134             sudo_warnx("unexpected data line: %s\n", line);
135             continue;
136         }
137     }
138
139     if (ntests != 0) {
140         printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
141             ntests, errors, (ntests - errors) * 100 / ntests);
142     }
143
144     exit(errors);
145 }