Annotation of embedaddon/sudo/plugins/sudoers/regress/parser/check_addr.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.3 ! misho       2:  * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       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: 
1.1.1.2   misho      45: #define SUDO_ERROR_WRAP 0
                     46: 
1.1       misho      47: #include "sudoers.h"
                     48: #include "parse.h"
                     49: #include "interfaces.h"
                     50: 
1.1.1.3 ! misho      51: __dso_public int main(int argc, char *argv[]);
1.1.1.2   misho      52: 
1.1       misho      53: static int
                     54: check_addr(char *input)
                     55: {
                     56:     int expected, matched;
                     57:     size_t len;
                     58:     char *cp;
                     59: 
                     60:     while (isspace((unsigned char)*input))
                     61:        input++;
                     62: 
                     63:     /* input: "addr[/mask] 1/0" */
                     64:     len = strcspn(input, " \t");
                     65:     cp = input + len;
                     66:     while (isspace((unsigned char)*cp))
                     67:        cp++;
                     68:     expected = atoi(cp);
                     69:     input[len] = '\0';
                     70: 
                     71:     matched = addr_matches(input);
                     72:     if (matched != expected) {
                     73:        warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
                     74:        return 1;
                     75:     }
                     76:     return 0;
                     77: }
                     78: 
                     79: static void
                     80: usage(void)
                     81: {
                     82:     fprintf(stderr, "usage: check_addr datafile\n");
                     83:     exit(1);
                     84: }
                     85: 
                     86: int
                     87: main(int argc, char *argv[])
                     88: {
                     89:     int ntests = 0, errors = 0;
                     90:     char *cp, line[2048];
                     91:     size_t len;
                     92:     FILE *fp;
                     93: 
                     94: #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
                     95:     setprogname(argc > 0 ? argv[0] : "check_addr");
                     96: #endif
                     97: 
                     98:     if (argc != 2)
                     99:        usage();
                    100: 
                    101:     fp = fopen(argv[1], "r");
                    102:     if (fp == NULL)
1.1.1.3 ! misho     103:        fatalx("unable to open %s", argv[1]);
1.1       misho     104: 
                    105:     /*
                    106:      * Input is in the following format.  There are two types of
                    107:      * lines: interfaces, which sets the address and mask of the
                    108:      * locally connected ethernet interfaces for the lines that
                    109:      * follow and, address lines that include and address (with
                    110:      * optional netmask) to match, followed by expected match status
                    111:      * (1 or 0).  E.g.
                    112:      *
                    113:      * interfaces: addr1/mask addr2/mask ...
                    114:      * address: addr[/mask] 1/0
                    115:      * address: addr[/mask] 1/0
                    116:      * interfaces: addr3/mask addr4/mask ...
                    117:      * address: addr[/mask] 1/0
                    118:      */
                    119: 
                    120:     while (fgets(line, sizeof(line), fp) != NULL) {
                    121:        len = strcspn(line, "\n");
                    122:        line[len] = '\0';
                    123: 
                    124:        /* Ignore comments */
                    125:        if ((cp = strchr(line, '#')) != NULL)
                    126:            *cp = '\0';
                    127: 
                    128:        /* Skip blank lines. */
                    129:        if (line[0] == '\0')
                    130:            continue;
                    131: 
                    132:        if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
                    133:            set_interfaces(line + sizeof("interfaces:") - 1);
                    134:        } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
                    135:            errors += check_addr(line + sizeof("address:") - 1);
                    136:            ntests++;
                    137:        } else {
                    138:            warningx("unexpected data line: %s\n", line);
                    139:            continue;
                    140:        }
                    141:     }
                    142: 
                    143:     printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
                    144:        ntests, errors, (ntests - errors) * 100 / ntests);
                    145: 
                    146:     exit(errors);
                    147: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>