int main(int argc, char *argv[]) { char *tty_libc, *tty_sudo; int rval = 0; initprogname(argc > 0 ? argv[0] : "check_ttyname"); /* Lookup tty name via libc. */ if ((tty_libc = ttyname(STDIN_FILENO)) == NULL && (tty_libc = ttyname(STDOUT_FILENO)) == NULL && (tty_libc = ttyname(STDERR_FILENO)) == NULL) tty_libc = "none"; tty_libc = estrdup(tty_libc); /* Lookup tty name via sudo (using kernel info if possible). */ if ((tty_sudo = get_process_ttyname()) == NULL) tty_sudo = estrdup("none"); if (strcmp(tty_libc, "none") == 0) { printf("%s: SKIP (%s)\n", getprogname(), tty_sudo); } else if (strcmp(tty_libc, tty_sudo) == 0) { printf("%s: OK (%s)\n", getprogname(), tty_sudo); } else { printf("%s: FAIL %s (sudo) vs. %s (libc)\n", getprogname(), tty_sudo, tty_libc); rval = 1; } efree(tty_libc); efree(tty_sudo); exit(rval); }
int main(int argc, char *argv[]) { const int ntests = nitems(tests); int i, errors = 0; time_t result; initprogname(argc > 0 ? argv[0] : "check_gentime"); /* Do local time tests in Eastern Standard Time. */ putenv("TZ=EST5EST5"); tzset(); for (i = 0; i < ntests; i++) { result = parse_gentime(tests[i].gentime); if (result != tests[i].unixtime) { fprintf(stderr, "check_gentime[%d]: %s: expected %lld, got %lld\n", i, tests[i].gentime, (long long)tests[i].unixtime, (long long)result); errors++; } } printf("check_gentime: %d tests run, %d errors, %d%% success rate\n", ntests, errors, (ntests - errors) * 100 / ntests); exit(errors); }
int main(int argc, char *argv[]) { int errors = 0; #ifndef HAVE_GETGROUPLIST_2 GETGROUPS_T *groups = NULL; struct passwd *pw; struct group *grp; char *username; int i, j, ntests = 0; int ngroups; gid_t basegid; initprogname(argc > 0 ? argv[0] : "getgrouplist_test"); if ((pw = getpwuid(0)) == NULL) sudo_fatal_nodebug("getpwuid(0)"); basegid = pw->pw_gid; if ((username = strdup(pw->pw_name)) == NULL) sudo_fatal_nodebug(NULL); if (sudo_getgrouplist2(username, basegid, &groups, &ngroups) == -1) sudo_fatal_nodebug("sudo_getgroulist2"); for (i = 0; i < ngroups; i++) { ntests++; /* Verify group ID exists. */ if ((grp = getgrgid(groups[i])) == NULL) { sudo_warnx_nodebug("unable to look up group ID %u", (unsigned int)groups[i]); errors++; continue; } /* Check user's primary gid from the passwd file. */ if (grp->gr_gid == basegid) continue; /* Verify group membership. */ for (j = 0; grp->gr_mem[j] != NULL; j++) { if (strcmp(username, grp->gr_mem[j]) == 0) { /* match */ break; } } if (grp->gr_mem[j] == NULL) { sudo_warnx_nodebug("unable to find %s in group %s", username, grp->gr_name); errors++; continue; } } if (errors != 0) { printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); } #endif /* HAVE_GETGROUPLIST_2 */ exit(errors); }
/* * Simple test driver for sudo_conf(). * Parses the given configuration file and dumps the resulting * sudo_conf_data struct to the standard output. */ int main(int argc, char *argv[]) { initprogname(argc > 0 ? argv[0] : "conf_test"); if (argc != 2) { fprintf(stderr, "usage: %s conf_file\n", getprogname()); exit(1); } sudo_conf_read(argv[1], SUDO_CONF_ALL); sudo_conf_dump(); exit(0); }
/* * Simple test driver for sudo_conf(). * Parses the given configuration file and dumps the resulting * sudo_conf_data struct to the standard output. */ int main(int argc, char *argv[]) { initprogname(argc > 0 ? argv[0] : "conf_test"); if (argc != 2) { fprintf(stderr, "usage: %s conf_file\n", getprogname()); exit(EXIT_FAILURE); } sudo_conf_clear_paths(); if (sudo_conf_read(argv[1], SUDO_CONF_ALL) == -1) exit(EXIT_FAILURE); sudo_conf_dump(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int ntests = nitems(test_strings); int i, errors = 0; unsigned char buf[64]; size_t len; initprogname(argc > 0 ? argv[0] : "check_base64"); for (i = 0; i < ntests; i++) { /* Test decode. */ len = base64_decode(test_strings[i].encoded, buf, sizeof(buf)); if (len == (size_t)-1) { fprintf(stderr, "check_base64: failed to decode %s\n", test_strings[i].encoded); errors++; } else { buf[len] = '\0'; if (strcmp(test_strings[i].ascii, (char *)buf) != 0) { fprintf(stderr, "check_base64: expected %s, got %s\n", test_strings[i].ascii, buf); errors++; } } /* Test encode. */ len = base64_encode((unsigned char *)test_strings[i].ascii, strlen(test_strings[i].ascii), (char *)buf, sizeof(buf)); if (len == (size_t)-1) { fprintf(stderr, "check_base64: failed to encode %s\n", test_strings[i].ascii); errors++; } else { if (strcmp(test_strings[i].encoded, (char *)buf) != 0) { fprintf(stderr, "check_base64: expected %s, got %s\n", test_strings[i].encoded, buf); errors++; } } } ntests *= 2; /* we test in both directions */ printf("check_base64: %d tests run, %d errors, %d%% success rate\n", ntests, errors, (ntests - errors) * 100 / ntests); exit(errors); }
int main(int argc, char *argv[]) { int ntests, errors = 0; initprogname(argc > 0 ? argv[0] : "check_fill"); errors += do_tests(check_fill, txt_data, nitems(txt_data)); errors += do_tests(check_fill_cmnd, cmd_data, nitems(cmd_data)); errors += do_tests(check_fill_args, args_data, nitems(args_data)); ntests = nitems(txt_data) + nitems(cmd_data) + nitems(args_data); printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); exit(errors); }
int main(int argc, char *argv[]) { char *tty_libc = NULL, *tty_sudo = NULL; char pathbuf[PATH_MAX]; int ret = 1; initprogname(argc > 0 ? argv[0] : "check_ttyname"); /* Lookup tty name using kernel info if possible. */ if (get_process_ttyname(pathbuf, sizeof(pathbuf)) != NULL) tty_sudo = pathbuf; #if defined(HAVE_KINFO_PROC2_NETBSD) || \ defined(HAVE_KINFO_PROC_OPENBSD) || \ defined(HAVE_KINFO_PROC_FREEBSD) || \ defined(HAVE_KINFO_PROC_44BSD) || \ defined(HAVE__TTYNAME_DEV) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || \ defined(HAVE_PSTAT_GETPROC) || defined(__linux__) /* Lookup tty name attached to stdin via libc. */ tty_libc = ttyname(STDIN_FILENO); #endif /* Compare libc and kernel ttys. */ if (tty_libc != NULL && tty_sudo != NULL) { if (strcmp(tty_libc, tty_sudo) == 0) ret = 0; } else if (tty_libc == NULL && tty_sudo == NULL) { ret = 0; } if (ret == 0) { printf("%s: OK (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none"); } else if (tty_libc == NULL) { printf("%s: SKIP (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none"); ret = 0; } else { printf("%s: FAIL %s (sudo) vs. %s (libc)\n", getprogname(), tty_sudo ? tty_sudo : "none", tty_libc ? tty_libc : "none"); } return ret; }
int main(int argc, char *argv[]) { initprogname(argc > 0 ? argv[0] : NULL); doargs(argc, argv); correctsize = (off_t) (numkeys*sizeof(int)); setdir(); genkeys(); sort(); validate(); complainx("Succeeded."); unsetdir(); return 0; }
/* * Simple tests for sudo_strtobool(), sudo_strtoid(), sudo_strtomode(). */ int main(int argc, char *argv[]) { int errors = 0; int ntests = 0; initprogname(argc > 0 ? argv[0] : "atofoo"); errors += test_strtobool(&ntests); errors += test_strtoid(&ntests); errors += test_strtomode(&ntests); if (ntests != 0) { printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); } exit(errors); }
/* * Exit codes defined in sudo_exec.h: * SESH_SUCCESS (0) ... successful operation * SESH_ERR_FAILURE (1) ... unspecified error * SESH_ERR_INVALID (30) ... invalid -e arg value * SESH_ERR_BAD_PATHS (31) ... odd number of paths * SESH_ERR_NO_FILES (32) ... copy error, no files copied * SESH_ERR_SOME_FILES (33) ... copy error, no files copied */ int main(int argc, char *argv[], char *envp[]) { int ret; debug_decl(main, SUDO_DEBUG_MAIN) initprogname(argc > 0 ? argv[0] : "sesh"); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); if (argc < 2) sudo_fatalx(U_("requires at least one argument")); /* Read sudo.conf and initialize the debug subsystem. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG) == -1) exit(EXIT_FAILURE); sudo_debug_register(getprogname(), NULL, NULL, sudo_conf_debug_files(getprogname())); if (strcmp(argv[1], "-e") == 0) { ret = sesh_sudoedit(argc, argv); } else { bool login_shell, noexec = false; char *cp, *cmnd; int fd = -1; /* If the first char of argv[0] is '-', we are running a login shell. */ login_shell = argv[0][0] == '-'; /* If argv[0] ends in -noexec, pass the flag to sudo_execve() */ if ((cp = strrchr(argv[0], '-')) != NULL && cp != argv[0]) noexec = strcmp(cp, "-noexec") == 0; /* If argv[1] is --execfd=%d, extract the fd to exec with. */ if (strncmp(argv[1], "--execfd=", 9) == 0) { const char *errstr; cp = argv[1] + 9; fd = strtonum(cp, 0, INT_MAX, &errstr); if (errstr != NULL) sudo_fatalx(U_("invalid file descriptor number: %s"), cp); argv++; argc--; } /* Shift argv and make a copy of the command to execute. */ argv++; argc--; if ((cmnd = strdup(argv[0])) == NULL) sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); /* If invoked as a login shell, modify argv[0] accordingly. */ if (login_shell) { if ((cp = strrchr(argv[0], '/')) == NULL) sudo_fatal(U_("unable to run %s as a login shell"), argv[0]); *cp = '-'; argv[0] = cp; } sudo_execve(fd, cmnd, argv, envp, noexec); sudo_warn(U_("unable to execute %s"), cmnd); ret = SESH_ERR_FAILURE; } sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, ret); _exit(ret); }
int main(int argc, char *argv[]) { char cwd[PATH_MAX + 1]; char *p; size_t clen; long pg; int i; initprogname(argc > 0 ? argv[0] : "mktemp_test"); pg = sysconf(_SC_PAGESIZE); if (getcwd(cwd, sizeof cwd - 1) == NULL) sudo_fatal("getcwd"); clen = strlen(cwd); cwd[clen++] = '/'; cwd[clen] = '\0'; #ifdef MAP_ANON p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); #else i = open("/dev/zero", O_RDWR); if (i == -1) sudo_fatal("/dev/zero"); p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE, i, 0); #endif if (p == MAP_FAILED) sudo_fatal("mmap"); if (mprotect(p, pg, PROT_NONE) || mprotect(p + pg * 2, pg, PROT_NONE)) sudo_fatal("mprotect"); p += pg; i = MAX_TEMPLATE_LEN + 1; while (i-- > 0) { /* try first at the start of a page, no prefix */ try_mkdtemp(p, "", i); /* now at the end of the page, no prefix */ try_mkdtemp(p + pg - i - 1, "", i); /* start of the page, prefixed with the cwd */ try_mkdtemp(p, cwd, i); /* how about at the end of the page, prefixed with cwd? */ try_mkdtemp(p + pg - clen - i - 1, cwd, i); /* again, with mkstemps() and an empty suffix */ /* try first at the start of a page, no prefix */ try_mkstemps(p, "", i, ""); /* now at the end of the page, no prefix */ try_mkstemps(p + pg - i - 1, "", i, ""); /* start of the page, prefixed with the cwd */ try_mkstemps(p, cwd, i, ""); /* how about at the end of the page, prefixed with cwd? */ try_mkstemps(p + pg - clen - i - 1, cwd, i, ""); /* mkstemps() and a non-empty suffix */ /* try first at the start of a page, no prefix */ try_mkstemps(p, "", i, SUFFIX); /* now at the end of the page, no prefix */ try_mkstemps(p + pg - i - SLEN - 1, "", i, SUFFIX); /* start of the page, prefixed with the cwd */ try_mkstemps(p, cwd, i, SUFFIX); /* how about at the end of the page, prefixed with cwd? */ try_mkstemps(p + pg - clen - i - SLEN - 1, cwd, i, SUFFIX); } return 0; }
int main(int argc, char *argv[]) { struct passwd pw, rpw; size_t len; FILE *fp; char line[2048]; char *file_in = NULL, *file_out = NULL; char *dir_in = NULL, *dir_out = NULL; const char *errstr; int state = 0; int errors = 0; int tests = 0; initprogname(argc > 0 ? argv[0] : "check_iolog_path"); if (argc != 2) usage(); fp = fopen(argv[1], "r"); if (fp == NULL) fatalx("unable to open %s", argv[1]); memset(&pw, 0, sizeof(pw)); memset(&rpw, 0, sizeof(rpw)); sudo_user.pw = &pw; sudo_user._runas_pw = &rpw; /* * Input consists of 12 lines: * sequence number * user name * user gid * runas user name * runas gid * hostname [short form] * command * dir [with escapes] * file [with escapes] * expanded dir * expanded file * empty line */ while (fgets(line, sizeof(line), fp) != NULL) { len = strcspn(line, "\n"); line[len] = '\0'; switch (state) { case 0: strlcpy(sessid, line, sizeof(sessid)); break; case 1: if (user_name != NULL) free(user_name); user_name = strdup(line); break; case 2: user_gid = (gid_t)atoid(line, NULL, NULL, &errstr); if (errstr != NULL) fatalx("group ID %s: %s", line, errstr); break; case 3: if (runas_pw->pw_name != NULL) free(runas_pw->pw_name); runas_pw->pw_name = strdup(line); break; case 4: runas_pw->pw_gid = (gid_t)atoid(line, NULL, NULL, &errstr); if (errstr != NULL) fatalx("group ID %s: %s", line, errstr); break; case 5: user_shost = strdup(line); break; case 6: user_base = strdup(line); break; case 7: dir_in = strdup(line); break; case 8: file_in = strdup(line); break; case 9: dir_out = strdup(line); break; case 10: file_out = strdup(line); break; case 11: errors += do_check(dir_in, file_in, dir_out, file_out); tests++; break; default: fatalx("internal error, invalid state %d", state); } state = (state + 1) % MAX_STATE; } if (tests != 0) { printf("iolog_path: %d test%s run, %d errors, %d%% success rate\n", tests, tests == 1 ? "" : "s", errors, (tests - errors) * 100 / tests); } exit(errors); }