2 * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
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>
29 #endif /* STDC_HEADERS */
33 #endif /* HAVE_STRING_H */
36 #endif /* HAVE_STRINGS_H */
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
48 #include "interfaces.h"
50 static int check_addr_printf(int msg_type, const char *fmt, ...);
52 /* for match_addr.c */
53 struct interface *interfaces;
54 sudo_printf_t sudo_printf = check_addr_printf;
57 check_addr(char *input)
59 int expected, matched;
63 while (isspace((unsigned char)*input))
66 /* input: "addr[/mask] 1/0" */
67 len = strcspn(input, " \t");
69 while (isspace((unsigned char)*cp))
74 matched = addr_matches(input);
75 if (matched != expected) {
76 warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
85 fprintf(stderr, "usage: check_addr datafile\n");
90 main(int argc, char *argv[])
92 int ntests = 0, errors = 0;
97 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
98 setprogname(argc > 0 ? argv[0] : "check_addr");
104 fp = fopen(argv[1], "r");
106 errorx(1, "unable to open %s", argv[1]);
109 * Input is in the following format. There are two types of
110 * lines: interfaces, which sets the address and mask of the
111 * locally connected ethernet interfaces for the lines that
112 * follow and, address lines that include and address (with
113 * optional netmask) to match, followed by expected match status
116 * interfaces: addr1/mask addr2/mask ...
117 * address: addr[/mask] 1/0
118 * address: addr[/mask] 1/0
119 * interfaces: addr3/mask addr4/mask ...
120 * address: addr[/mask] 1/0
123 while (fgets(line, sizeof(line), fp) != NULL) {
124 len = strcspn(line, "\n");
127 /* Ignore comments */
128 if ((cp = strchr(line, '#')) != NULL)
131 /* Skip blank lines. */
135 if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
136 set_interfaces(line + sizeof("interfaces:") - 1);
137 } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
138 errors += check_addr(line + sizeof("address:") - 1);
141 warningx("unexpected data line: %s\n", line);
146 printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
147 ntests, errors, (ntests - errors) * 100 / ntests);
160 check_addr_printf(int msg_type, const char *fmt, ...)
166 case SUDO_CONV_INFO_MSG:
169 case SUDO_CONV_ERROR_MSG:
178 vfprintf(fp, fmt, ap);