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>
46 #define SUDO_ERROR_WRAP 0
50 #include "interfaces.h"
52 static int check_addr_printf(int msg_type, const char *fmt, ...);
54 /* for match_addr.c */
55 struct interface *interfaces;
56 sudo_printf_t sudo_printf = check_addr_printf;
58 sudo_conv_t sudo_conv; /* NULL in non-plugin */
61 check_addr(char *input)
63 int expected, matched;
67 while (isspace((unsigned char)*input))
70 /* input: "addr[/mask] 1/0" */
71 len = strcspn(input, " \t");
73 while (isspace((unsigned char)*cp))
78 matched = addr_matches(input);
79 if (matched != expected) {
80 warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
89 fprintf(stderr, "usage: check_addr datafile\n");
94 main(int argc, char *argv[])
96 int ntests = 0, errors = 0;
101 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
102 setprogname(argc > 0 ? argv[0] : "check_addr");
108 fp = fopen(argv[1], "r");
110 errorx(1, "unable to open %s", argv[1]);
113 * Input is in the following format. There are two types of
114 * lines: interfaces, which sets the address and mask of the
115 * locally connected ethernet interfaces for the lines that
116 * follow and, address lines that include and address (with
117 * optional netmask) to match, followed by expected match status
120 * interfaces: addr1/mask addr2/mask ...
121 * address: addr[/mask] 1/0
122 * address: addr[/mask] 1/0
123 * interfaces: addr3/mask addr4/mask ...
124 * address: addr[/mask] 1/0
127 while (fgets(line, sizeof(line), fp) != NULL) {
128 len = strcspn(line, "\n");
131 /* Ignore comments */
132 if ((cp = strchr(line, '#')) != NULL)
135 /* Skip blank lines. */
139 if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
140 set_interfaces(line + sizeof("interfaces:") - 1);
141 } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
142 errors += check_addr(line + sizeof("address:") - 1);
145 warningx("unexpected data line: %s\n", line);
150 printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
151 ntests, errors, (ntests - errors) * 100 / ntests);
164 check_addr_printf(int msg_type, const char *fmt, ...)
170 case SUDO_CONV_INFO_MSG:
173 case SUDO_CONV_ERROR_MSG:
182 vfprintf(fp, fmt, ap);