"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/plugins/sudoers/regress/parser/check_addr.c" (12 Jun 2022, 3612 Bytes) of package /linux/misc/sudo-1.9.11p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "check_addr.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.9.10_vs_1.9.11rc1.

    1 /*
    2  * SPDX-License-Identifier: ISC
    3  *
    4  * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@sudo.ws>
    5  *
    6  * Permission to use, copy, modify, and distribute this software for any
    7  * purpose with or without fee is hereby granted, provided that the above
    8  * copyright notice and this permission notice appear in all copies.
    9  *
   10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   17  */
   18 
   19 #include <config.h>
   20 
   21 #include <sys/types.h>
   22 #include <sys/socket.h>
   23 #include <stdio.h>
   24 #include <stdlib.h>
   25 #include <stdarg.h>
   26 #include <string.h>
   27 #include <ctype.h>
   28 
   29 #include <netinet/in.h>
   30 #include <arpa/inet.h>
   31 
   32 #define SUDO_ERROR_WRAP 0
   33 
   34 #include "sudoers.h"
   35 #include "interfaces.h"
   36 
   37 sudo_dso_public int main(int argc, char *argv[]);
   38 
   39 static int
   40 check_addr(char *input)
   41 {
   42     int expected, matched;
   43     const char *errstr;
   44     size_t len;
   45     char *cp;
   46 
   47     while (isspace((unsigned char)*input))
   48     input++;
   49 
   50     /* input: "addr[/mask] 1/0" */
   51     len = strcspn(input, " \t");
   52     cp = input + len;
   53     while (isspace((unsigned char)*cp))
   54     cp++;
   55     expected = sudo_strtonum(cp, 0, 1, &errstr);
   56     if (errstr != NULL)
   57     sudo_fatalx("expecting 0 or 1, got %s", cp);
   58     input[len] = '\0';
   59 
   60     matched = addr_matches(input);
   61     if (matched != expected) {
   62     sudo_warnx("%s %smatched: FAIL", input, matched ? "" : "not ");
   63     return 1;
   64     }
   65     return 0;
   66 }
   67 
   68 static void
   69 usage(void)
   70 {
   71     fprintf(stderr, "usage: %s datafile\n", getprogname());
   72     exit(EXIT_FAILURE);
   73 }
   74 
   75 int
   76 main(int argc, char *argv[])
   77 {
   78     int ntests = 0, errors = 0;
   79     char *cp, line[2048];
   80     size_t len;
   81     FILE *fp;
   82 
   83     initprogname(argc > 0 ? argv[0] : "check_addr");
   84 
   85     if (argc != 2)
   86     usage();
   87 
   88     fp = fopen(argv[1], "r");
   89     if (fp == NULL)
   90     sudo_fatalx("unable to open %s", argv[1]);
   91 
   92     /*
   93      * Input is in the following format.  There are two types of
   94      * lines: interfaces, which sets the address and mask of the
   95      * locally connected ethernet interfaces for the lines that
   96      * follow and, address lines that include and address (with
   97      * optional netmask) to match, followed by expected match status
   98      * (1 or 0).  E.g.
   99      *
  100      * interfaces: addr1/mask addr2/mask ...
  101      * address: addr[/mask] 1/0
  102      * address: addr[/mask] 1/0
  103      * interfaces: addr3/mask addr4/mask ...
  104      * address: addr[/mask] 1/0
  105      */
  106 
  107     while (fgets(line, sizeof(line), fp) != NULL) {
  108     len = strcspn(line, "\n");
  109     line[len] = '\0';
  110 
  111     /* Ignore comments */
  112     if ((cp = strchr(line, '#')) != NULL)
  113         *cp = '\0';
  114 
  115     /* Skip blank lines. */
  116     if (line[0] == '\0')
  117         continue;
  118 
  119     if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
  120         if (!set_interfaces(line + sizeof("interfaces:") - 1)) {
  121         sudo_warn("unable to parse interfaces list");
  122         errors++;
  123         }
  124     } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
  125         errors += check_addr(line + sizeof("address:") - 1);
  126         ntests++;
  127     } else {
  128         sudo_warnx("unexpected data line: %s", line);
  129         continue;
  130     }
  131     }
  132 
  133     if (ntests != 0) {
  134     printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
  135         ntests, errors, (ntests - errors) * 100 / ntests);
  136     }
  137 
  138     exit(errors);
  139 }