2 * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@sudo.ws>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
26 #endif /* HAVE_STRING_H */
29 #endif /* HAVE_STRINGS_H */
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #define SUDO_ERROR_WRAP 0
41 #include "interfaces.h"
43 __dso_public int main(int argc, char *argv[]);
46 check_addr(char *input)
48 int expected, matched;
53 while (isspace((unsigned char)*input))
56 /* input: "addr[/mask] 1/0" */
57 len = strcspn(input, " \t");
59 while (isspace((unsigned char)*cp))
61 expected = strtonum(cp, 0, 1, &errstr);
63 sudo_fatalx("expecting 0 or 1, got %s", cp);
66 matched = addr_matches(input);
67 if (matched != expected) {
68 sudo_warnx("%s %smatched: FAIL", input, matched ? "" : "not ");
77 fprintf(stderr, "usage: %s datafile\n", getprogname());
82 main(int argc, char *argv[])
84 int ntests = 0, errors = 0;
89 initprogname(argc > 0 ? argv[0] : "check_addr");
94 fp = fopen(argv[1], "r");
96 sudo_fatalx("unable to open %s", argv[1]);
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
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
113 while (fgets(line, sizeof(line), fp) != NULL) {
114 len = strcspn(line, "\n");
117 /* Ignore comments */
118 if ((cp = strchr(line, '#')) != NULL)
121 /* Skip blank lines. */
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");
130 } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
131 errors += check_addr(line + sizeof("address:") - 1);
134 sudo_warnx("unexpected data line: %s\n", line);
140 printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
141 ntests, errors, (ntests - errors) * 100 / ntests);