Imported Upstream version 1.8.4p4
[debian/sudo] / plugins / sudoers / regress / parser / check_addr.c
1 /*
2  * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
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 #include <netdb.h>
45
46 #define SUDO_ERROR_WRAP 0
47
48 #include "sudoers.h"
49 #include "parse.h"
50 #include "interfaces.h"
51
52 static int check_addr_printf(int msg_type, const char *fmt, ...);
53
54 /* for match_addr.c */
55 struct interface *interfaces;
56 sudo_printf_t sudo_printf = check_addr_printf;
57
58 sudo_conv_t sudo_conv;          /* NULL in non-plugin */
59
60 static int
61 check_addr(char *input)
62 {
63     int expected, matched;
64     size_t len;
65     char *cp;
66
67     while (isspace((unsigned char)*input))
68         input++;
69
70     /* input: "addr[/mask] 1/0" */
71     len = strcspn(input, " \t");
72     cp = input + len;
73     while (isspace((unsigned char)*cp))
74         cp++;
75     expected = atoi(cp);
76     input[len] = '\0';
77
78     matched = addr_matches(input);
79     if (matched != expected) {
80         warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
81         return 1;
82     }
83     return 0;
84 }
85
86 static void
87 usage(void)
88 {
89     fprintf(stderr, "usage: check_addr datafile\n");
90     exit(1);
91 }
92
93 int
94 main(int argc, char *argv[])
95 {
96     int ntests = 0, errors = 0;
97     char *cp, line[2048];
98     size_t len;
99     FILE *fp;
100
101 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
102     setprogname(argc > 0 ? argv[0] : "check_addr");
103 #endif
104
105     if (argc != 2)
106         usage();
107
108     fp = fopen(argv[1], "r");
109     if (fp == NULL)
110         errorx(1, "unable to open %s", argv[1]);
111
112     /*
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
118      * (1 or 0).  E.g.
119      *
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
125      */
126
127     while (fgets(line, sizeof(line), fp) != NULL) {
128         len = strcspn(line, "\n");
129         line[len] = '\0';
130
131         /* Ignore comments */
132         if ((cp = strchr(line, '#')) != NULL)
133             *cp = '\0';
134
135         /* Skip blank lines. */
136         if (line[0] == '\0')
137             continue;
138
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);
143             ntests++;
144         } else {
145             warningx("unexpected data line: %s\n", line);
146             continue;
147         }
148     }
149
150     printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
151         ntests, errors, (ntests - errors) * 100 / ntests);
152
153     exit(errors);
154 }
155
156 /* STUB */
157 void
158 cleanup(int gotsig)
159 {
160     return;
161 }
162
163 static int
164 check_addr_printf(int msg_type, const char *fmt, ...)
165 {
166     va_list ap;
167     FILE *fp;
168             
169     switch (msg_type) {
170     case SUDO_CONV_INFO_MSG:
171         fp = stdout;
172         break;
173     case SUDO_CONV_ERROR_MSG:
174         fp = stderr;
175         break;
176     default:
177         errno = EINVAL;
178         return -1;
179     }
180    
181     va_start(ap, fmt);
182     vfprintf(fp, fmt, ap);
183     va_end(ap);
184    
185     return 0;
186 }