| 1 |
/* |
| 2 |
* SPDX-License-Identifier: ISC |
| 3 |
* |
| 4 |
* Copyright (c) 2021-2022 Todd C. Miller <[email protected]> |
| 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/stat.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#ifdef HAVE_STDBOOL_H |
| 26 |
# include <stdbool.h> |
| 27 |
#else |
| 28 |
# include <compat/stdbool.h> |
| 29 |
#endif /* HAVE_STDBOOL_H */ |
| 30 |
#include <string.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <limits.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include <sudo_compat.h> |
| 36 |
#include <sudo_util.h> |
| 37 |
|
| 38 |
sudo_dso_public int main(int argc, char *argv[]); |
| 39 |
|
| 40 |
extern int get_net_ifs(char **addrinfo); |
| 41 |
|
| 42 |
int |
| 43 |
main(int argc, char *argv[]) |
| 44 |
{ |
| 45 |
int ch, ninterfaces, errors = 0, ntests = 1; |
| 46 |
char *interfaces = NULL; |
| 47 |
bool verbose = false; |
| 48 |
|
| 49 |
initprogname(argc > 0 ? argv[0] : "check_net_ifs"); |
| 50 |
|
| 51 |
while ((ch = getopt(argc, argv, "v")) != -1) { |
| 52 |
switch (ch) { |
| 53 |
case 'v': |
| 54 |
verbose = true; |
| 55 |
break; |
| 56 |
default: |
| 57 |
fprintf(stderr, "usage: %s [-v]\n", getprogname()); |
| 58 |
return EXIT_FAILURE; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
ninterfaces = get_net_ifs(&interfaces); |
| 63 |
switch (ninterfaces) { |
| 64 |
case -1: |
| 65 |
printf("FAIL: unable to get network interfaces\n"); |
| 66 |
errors++; |
| 67 |
break; |
| 68 |
case 0: |
| 69 |
/* no interfaces or STUB_LOAD_INTERFACES defined. */ |
| 70 |
if (verbose) |
| 71 |
printf("OK: (0 interfaces)\n"); |
| 72 |
break; |
| 73 |
default: |
| 74 |
if (verbose) { |
| 75 |
printf("OK: (%d interface%s, %s)\n", ninterfaces, |
| 76 |
ninterfaces > 1 ? "s" : "", interfaces); |
| 77 |
} |
| 78 |
break; |
| 79 |
} |
| 80 |
free(interfaces); |
| 81 |
|
| 82 |
if (ntests != 0) { |
| 83 |
printf("%s: %d tests run, %d errors, %d%% success rate\n", |
| 84 |
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); |
| 85 |
} |
| 86 |
return errors; |
| 87 |
} |