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

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

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