Annotation of embedaddon/sudo/plugins/sudoers/regress/iolog_path/check_iolog_path.c, revision 1.1.1.1

1.1       misho       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 <stdio.h>
                     21: #ifdef STDC_HEADERS
                     22: # include <stdlib.h>
                     23: # include <stddef.h>
                     24: #else
                     25: # ifdef HAVE_STDLIB_H
                     26: #  include <stdlib.h>
                     27: # endif
                     28: #endif /* STDC_HEADERS */
                     29: #ifdef HAVE_STRING_H
                     30: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
                     31: #  include <memory.h>
                     32: # endif
                     33: # include <string.h>
                     34: #endif /* HAVE_STRING_H */
                     35: #ifdef HAVE_STRINGS_H
                     36: # include <strings.h>
                     37: #endif /* HAVE_STRINGS_H */
                     38: #ifdef HAVE_SETLOCALE
                     39: # include <locale.h>
                     40: #endif
                     41: #include <pwd.h>
                     42: #include <grp.h>
                     43: #include <time.h>
                     44: 
                     45: #define _SUDO_MAIN
                     46: #include "sudoers.h"
                     47: #include "def_data.c"
                     48: 
                     49: struct sudo_user sudo_user;
                     50: struct passwd *list_pw;
                     51: 
                     52: static char sessid[7];
                     53: 
                     54: static void
                     55: usage(void)
                     56: {
                     57:     fprintf(stderr, "usage: check_iolog_path datafile\n");
                     58:     exit(1);
                     59: }
                     60: 
                     61: static int
                     62: do_check(char *dir_in, char *file_in, char *tdir_out, char *tfile_out)
                     63: {
                     64:     char *path, *slash;
                     65:     char dir_out[4096], file_out[4096];
                     66:     struct tm *timeptr;
                     67:     time_t now;
                     68:     int error = 0;
                     69: 
                     70:     /*
                     71:      * Expand any strftime(3) escapes
                     72:      * XXX - want to pass timeptr to expand_iolog_path
                     73:      */
                     74:     time(&now);
                     75:     timeptr = localtime(&now);
                     76:     strftime(dir_out, sizeof(dir_out), tdir_out, timeptr);
                     77:     strftime(file_out, sizeof(file_out), tfile_out, timeptr);
                     78: 
                     79:     path = expand_iolog_path(NULL, dir_in, file_in, &slash);
                     80:     *slash = '\0';
                     81:     if (strcmp(path, dir_out) != 0) {
                     82:        warningx("%s: expected %s, got %s", dir_in, dir_out, path);
                     83:        error = 1;
                     84:     }
                     85:     if (strcmp(slash + 1, file_out) != 0) {
                     86:        warningx("%s: expected %s, got %s", file_in, file_out, slash + 1);
                     87:        error = 1;
                     88:     }
                     89: 
                     90:     return error;
                     91: }
                     92: 
                     93: #define MAX_STATE      12
                     94: 
                     95: int
                     96: main(int argc, char *argv[])
                     97: {
                     98:     struct passwd pw, rpw;
                     99:     size_t len;
                    100:     FILE *fp;
                    101:     char line[2048];
                    102:     char *file_in = NULL, *file_out = NULL;
                    103:     char *dir_in = NULL, *dir_out = NULL;
                    104:     int state = 0;
                    105:     int errors = 0;
                    106:     int tests = 0;
                    107: 
                    108: #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
                    109:     setprogname(argc > 0 ? argv[0] : "check_iolog_path");
                    110: #endif
                    111: 
                    112:     if (argc != 2)
                    113:        usage();
                    114: 
                    115:     fp = fopen(argv[1], "r");
                    116:     if (fp == NULL)
                    117:        errorx(1, "unable to open %s", argv[1]);
                    118: 
                    119:     memset(&pw, 0, sizeof(pw));
                    120:     memset(&rpw, 0, sizeof(rpw));
                    121:     sudo_user.pw = &pw;
                    122:     sudo_user._runas_pw = &rpw;
                    123: 
                    124:     /*
                    125:      * Input consists of 12 lines:
                    126:      * sequence number
                    127:      * user name
                    128:      * user gid
                    129:      * runas user name
                    130:      * runas gid
                    131:      * hostname [short form]
                    132:      * command
                    133:      * dir [with escapes]
                    134:      * file [with escapes]
                    135:      * expanded dir
                    136:      * expanded file
                    137:      * empty line
                    138:      */
                    139:     while (fgets(line, sizeof(line), fp) != NULL) {
                    140:        len = strcspn(line, "\n");
                    141:        line[len] = '\0';
                    142: 
                    143:        switch (state) {
                    144:        case 0:
                    145:            strlcpy(sessid, line, sizeof(sessid));
                    146:            break;
                    147:        case 1:
                    148:            if (user_name != NULL)
                    149:                free(user_name);
                    150:            user_name = strdup(line);
                    151:            break;
                    152:        case 2:
                    153:            user_gid = atoi(line);
                    154:            break;
                    155:        case 3:
                    156:            if (runas_pw->pw_name != NULL)
                    157:                free(runas_pw->pw_name);
                    158:            runas_pw->pw_name = strdup(line);
                    159:            break;
                    160:        case 4:
                    161:            runas_pw->pw_gid = atoi(line);
                    162:            break;
                    163:        case 5:
                    164:            user_shost = strdup(line);
                    165:            break;
                    166:        case 6:
                    167:            user_base = strdup(line);
                    168:            break;
                    169:        case 7:
                    170:            dir_in = strdup(line);
                    171:            break;
                    172:        case 8:
                    173:            file_in = strdup(line);
                    174:            break;
                    175:        case 9:
                    176:            dir_out = strdup(line);
                    177:            break;
                    178:        case 10:
                    179:            file_out = strdup(line);
                    180:            break;
                    181:        case 11:
                    182:            errors += do_check(dir_in, file_in, dir_out, file_out);
                    183:            tests++;
                    184:            break;
                    185:        default:
                    186:            errorx(1, "internal error, invalid state %d", state);
                    187:        }
                    188:        state = (state + 1) % MAX_STATE;
                    189:     }
                    190: 
                    191:     if (tests != 0) {
                    192:        printf("iolog_path: %d test%s run, %d errors, %d%% success rate\n",
                    193:            tests, tests == 1 ? "" : "s", errors,
                    194:            (tests - errors) * 100 / tests);
                    195:     }
                    196: 
                    197:     exit(errors);
                    198: }
                    199: 
                    200: void io_nextid(char *iolog_dir, char id[7])
                    201: {
                    202:     memcpy(id, sessid, sizeof(sessid));
                    203: }
                    204: 
                    205: void
                    206: cleanup(int gotsig)
                    207: {
                    208:     return;
                    209: }

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