Changeset 249

Show
Ignore:
Timestamp:
06/08/05 20:08:15 (3 years ago)
Author:
athomas
Message:
  • Fixed long standing bug where the incorrect user is reported in the logs.
  • Added fperms and fowner constraints thanks to Pierre.
  • Logging beautification.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • op/trunk/main.c

    r247 r249  
    7474cmd_t   *First = NULL; 
    7575var_t   *Variables = NULL; 
    76 struct passwd *realuser = NULL; 
     76char *realuser = NULL; 
    7777int gargc = -1; 
    7878char **gargv = NULL; 
     
    202202 
    203203        if (sigprocmask(SIG_BLOCK, &sig_mask, &old_sig_mask)) 
    204                 fatal(1, "Could not set signal mask"); 
     204                fatal(1, "could not set signal mask"); 
    205205 
    206206        gargv = argv; 
    207207        gargc = argc; 
    208         realuser = getpwuid(getuid()); 
    209208 
    210209        while (1) { 
     
    262261 
    263262        if (!read_conf && !read_conf_dir) 
    264                 fatal(1, "Could not open %s or any configuration files in %s", OP_ACCESS, OP_ACCESS_DIR); 
     263                fatal(1, "could not open %s or any configuration files in %s", OP_ACCESS, OP_ACCESS_DIR); 
    265264 
    266265        if ((pw = getpwuid(getuid())) == NULL)  
    267266                exit(1); 
    268         realuser = getpwuid(getuid()); 
     267        realuser = (char*)strdup(pw->pw_name); 
    269268        strncpy(user, pw->pw_name, MAXSTRLEN); 
    270269 
     
    277276                if (uptr != NULL) { 
    278277                        if (getuid() != 0)  
    279                                 fatal(1, "Permission denied for -u option"); 
     278                                fatal(1, "permission denied for -u option"); 
    280279                } 
    281280        } 
     
    296295                if (access(argv[argStart], F_OK) != 0) 
    297296                        if (access(argv[argStart], X_OK) != 0) 
    298                                 fatal(1, "Unknown or non executable command"); 
     297                                fatal(1, "unknown or non executable command"); 
    299298        } 
    300299        cmd = Find(uptr); 
    301300 
    302301        if (cmd == NULL)  
    303                 fatal(1, "No such command %s", argv[1]); 
     302                fatal(1, "no such command %s", argv[1]); 
    304303 
    305304        argc -= argStart; 
     
    311310 
    312311        if ((num < 0) && ((argc-1) < -num)) 
    313                 fatal(1, "%s: Improper number of arguments", cmd->name); 
     312                fatal(1, "%s: improper number of arguments", cmd->name); 
    314313        if ((num > 0) && ((argc-1) != num))  
    315                 fatal(1, "%s: Improper number of arguments", cmd->name); 
     314                fatal(1, "%s: improper number of arguments", cmd->name); 
    316315        if (num <0) 
    317316                num = -num; 
     
    319318        pcmd_s = format_cmd(argc, argv, cmd_s, MAXSTRLEN); 
    320319        if (Verify(new, num, argc, argv) < 0) 
    321                 fatal(0, "%s: Permission denied by op", cmd->name); 
     320                fatal(0, "%s: permission denied by op", cmd->name); 
    322321 
    323322        return Go(new, num, argc, argv); 
     
    697696                                        if (regexec(reg1,argv[j]) == 1) break; 
    698697                                } 
    699                                 if (cp == NULL) return logger(LOG_ERR, "%s: Argument %i (%s) did not pass wildcard constraint", cmd->name, j, argv[j]); 
     698                                if (cp == NULL) return logger(LOG_ERR, "%s: argument %i (%s) did not pass wildcard constraint", cmd->name, j, argv[j]); 
    700699                        } 
    701700                } 
     
    745744                        } 
    746745                } 
    747                 if (cp == NULL) return logger(LOG_ERR, "%s: Argument '%s' did not pass constraint '%s'", cmd->name, argv[val], np); 
     746                if (cp == NULL) return logger(LOG_ERR, "%s: argument '%s' did not pass constraint '%s'", cmd->name, argv[val], np); 
    748747        } 
    749748        return 0; 
     
    873872                        strcat(new_envp[curenv], xauth); 
    874873                        if (curenv + 1 >= MAXENV) 
    875                                 fatal(1, "%s: Environment length exceeded",cmd->name); 
     874                                fatal(1, "%s: environment length exceeded",cmd->name); 
    876875                        ++curenv; 
    877876                        /* Propagate $DISPLAY to new environment */ 
     
    880879                        strcat(new_envp[curenv], getenv("DISPLAY")); 
    881880                        if (curenv + 1 >= MAXENV) 
    882                                 fatal(1, "%s: Environment length exceeded",cmd->name); 
     881                                fatal(1, "%s: environment length exceeded",cmd->name); 
    883882                        ++curenv; 
    884883                } 
     
    972971                                if (strncmp(cmd->opts[i] + 1, environ[j], cp - environ[j]) == 0) { 
    973972                                        if (curenv + 1 >= MAXENV) 
    974                                                 fatal(1, "%s: Environment length exceeded",cmd->name); 
     973                                                fatal(1, "%s: environment length exceeded",cmd->name); 
    975974                                        new_envp[curenv++] = environ[j]; 
    976975                                        break; 
     
    981980                for (i = 0; environ[i] != NULL; i++) { 
    982981                        if (curenv + 1 >= MAXENV) 
    983                                 fatal(1, "%s: Environment length exceeded",cmd->name); 
     982                                fatal(1, "%s: environment length exceeded",cmd->name); 
    984983                        new_envp[curenv++] = environ[i]; 
    985984                } 
     
    999998                if (cmd->opts[i][0] == '$' && strchr(cmd->opts[i], '=') != NULL) { 
    1000999                        if (curenv + 1 >= MAXENV) 
    1001                                 fatal(1, "%s: Environment length exceeded",cmd->name); 
     1000                                fatal(1, "%s: environment length exceeded",cmd->name); 
    10021001                        new_envp[curenv++] = cmd->opts[i] + 1; 
    10031002                        continue; 
     
    10051004        } 
    10061005        new_envp[curenv] = NULL; 
     1006 
     1007        /* --------------------------------------------------- */ 
     1008        /* fowner constraint must respect the syntax :        */ 
     1009        /* fowner=user:group,...                              */ 
     1010        /* Notice : user and/or group are regular expressions  */ 
     1011        /* --------------------------------------------------- */ 
     1012 
     1013        if ((cp = FindOpt(cmd, "fowner")) != NULL) { 
     1014                struct passwd * pwbuf; 
     1015                struct group * grbuf; 
     1016                struct stat statbuf; 
     1017                char * ptr; 
     1018                char usergroup[MAXSTRLEN]; 
     1019 
     1020                /* Get user and group name of the owner of the file */ 
     1021                stat(cmd->args[0],&statbuf); 
     1022 
     1023                pwbuf = getpwuid(statbuf.st_uid); 
     1024                if (pwbuf == NULL) 
     1025                        fatal(1,"%s: no identified user for uid %d", cmd->name, statbuf.st_uid); 
     1026                grbuf = getgrgid(statbuf.st_gid); 
     1027                if (grbuf == NULL) 
     1028                        fatal(1,"%s: no identified group for gid %d", cmd->name, statbuf.st_gid); 
     1029 
     1030                if (strlen(pwbuf->pw_name) + strlen(grbuf->gr_name) + 1 >= MAXSTRLEN) 
     1031                        fatal(1, "%s: user/group string buffer length exceeded", cmd->name); 
     1032                strcpy(usergroup, pwbuf->pw_name); 
     1033                strcat(usergroup, ":"); 
     1034                strcat(usergroup, grbuf->gr_name); 
     1035 
     1036                /* check users,groups candidates */ 
     1037                 
     1038                for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL; cp = GetField(cp, str, MAXSTRLEN - 5)) { 
     1039                        regexp          *reg1 = NULL; 
     1040                        char regstr[MAXSTRLEN]; 
     1041 
     1042                        ptr=strchr(str,':'); 
     1043                        if (ptr == NULL) 
     1044                                fatal(1,"%s: fowner argument must respect the user:group format", cmd->name); 
     1045 
     1046                        strcpy(regstr, "^("); 
     1047                        strcat(regstr, str); 
     1048                        strcat(regstr, ")$"); 
     1049                 
     1050                        if ((reg1 = regcomp(regstr)) == NULL) 
     1051                                return logger(LOG_ERR, "Invalid regex '%s'", str); 
     1052         
     1053                        if ((regexec(reg1, usergroup) == 1)) 
     1054                                break; 
     1055                } 
     1056                if (cp == NULL) 
     1057                        fatal(1,"%s: file %s (%s) did not pass ownership constraints", 
     1058                          cmd->name, cmd->args[0], usergroup); 
     1059        } 
     1060 
     1061        /* ---------------------------------------------------------------------- */ 
     1062        /* fperms constraint must respect the syntax :                            */ 
     1063        /* fperms=NNNN,MMMM,... where NNNN and MMMM are octal representation of   */ 
     1064        /*                            the target requested authorised permissions */ 
     1065        /* Notice : NNNN and MMMM can be regular expressions                      */ 
     1066        /* ---------------------------------------------------------------------- */ 
     1067 
     1068        if ((cp = FindOpt(cmd, "fperms")) != NULL) { 
     1069                struct stat buf; 
     1070                char   mode[5]; 
     1071 
     1072                stat(cmd->args[0],&buf); 
     1073                snprintf(mode, 5, "%o", buf.st_mode & 07777); 
     1074 
     1075                for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL; cp = GetField(cp, str, MAXSTRLEN - 5)) { 
     1076                        regexp          *reg1 = NULL; 
     1077                        char regstr[MAXSTRLEN]; 
     1078 
     1079                        strcpy(regstr, "^("); 
     1080                        strcat(regstr, str); 
     1081                        strcat(regstr, ")$"); 
     1082                 
     1083                        if ((reg1 = regcomp(regstr)) == NULL) 
     1084                                return logger(LOG_ERR, "Invalid regex '%s'", str); 
     1085 
     1086                        if (regexec(reg1,mode) == 1) 
     1087                                break; 
     1088                } 
     1089                if (cp == NULL) 
     1090                        fatal(1,"%s: file %s (%s) did not pass permissions constraints", 
     1091                          cmd->name, cmd->args[0],mode); 
     1092        } 
    10071093 
    10081094        if (strcmp("MAGIC_SHELL", cmd->args[0]) == 0) { 
     
    10131099                if (environ[i] != NULL) { 
    10141100                        if (curarg >= MAXARG - 1) 
    1015                                 fatal(1, "%s: Argument length exceeded",cmd->name); 
     1101                                fatal(1, "%s: argument length exceeded",cmd->name); 
    10161102                        new_argv[curarg++] = environ[i] + 6; 
    10171103                } else { 
    1018                         fatal(1, "No shell"); 
     1104                        fatal(1, "%s: no shell", cmd->name); 
    10191105                } 
    10201106 
    10211107                if (argc != 1) { 
    10221108                        if (curarg >= MAXARG - 1) 
    1023                                 fatal(1, "%s: Argument length exceeded",cmd->name); 
     1109                                fatal(1, "%s: argument length exceeded",cmd->name); 
    10241110                        new_argv[curarg++] = "-c"; 
    10251111 
     
    10281114 
    10291115                        if ((cp = (char *)malloc(len + 10)) == NULL) 
    1030                                 fatal(1, "Unable to create buffer"); 
     1116                                fatal(1, "%s: unable to create buffer", cmd->name); 
    10311117 
    10321118                        len = 0; 
     
    10381124                        } 
    10391125                        if (curarg >= MAXARG - 1) 
    1040                                 fatal(1, "%s: Argument length exceeded",cmd->name); 
     1126                                fatal(1, "%s: argument length exceeded",cmd->name); 
    10411127                        new_argv[curarg++] = cp; 
    10421128                } 
     
    10491135                                if (np[1] == '*') { 
    10501136                                        if (curarg + argc >= MAXARG - 1) 
    1051                                                 fatal(1, "%s: Argument length exceeded",cmd->name); 
     1137                                                fatal(1, "%s: argument length exceeded",cmd->name); 
    10521138                                        for (j = 1; j < argc; j++) 
    10531139                                                new_argv[curarg++] = argv[j]; 
     
    10571143                                                fatal(1, "%s Referenced argument out of range",cmd->name); 
    10581144                                        if (curarg >= MAXARG - 1) 
    1059                                                 fatal(1, "%s: Argument length exceeded",cmd->name); 
     1145                                                fatal(1, "%s: argument length exceeded",cmd->name); 
    10601146                                        new_argv[curarg++] = argv[atoi(np + 1)]; 
    10611147                                } 
     
    11151201                        if (cp == NULL) { 
    11161202                                if (curarg >= MAXARG - 1) 
    1117                                         fatal(1, "%s: Argument length exceeded",cmd->name); 
     1203                                        fatal(1, "%s: argument length exceeded",cmd->name); 
    11181204                                new_argv[curarg++] = cmd->args[i]; 
    11191205                                continue; 
     
    11271213 
    11281214        if (sigprocmask(SIG_SETMASK, &old_sig_mask, NULL)) 
    1129                 fatal(1, "Could not restore signal mask"); 
     1215                fatal(1, "could not restore signal mask"); 
    11301216        if ((i = execve(new_argv[0], new_argv, new_envp)) < 0) { 
    11311217                perror("execve"); 
     
    11971283        if (level >= minimum_logging_level) return -1; 
    11981284 
    1199         if (realuser) username = realuser->pw_name
     1285        if (realuser) username = realuser
    12001286 
    12011287        vstrnprintf(buffer2, MAXSTRLEN, format, args); 
  • op/trunk/Makefile.am

    r248 r249  
    22op_SOURCES=atov.c defs.h lex.l main.c regexp.c regexp.h util.c 
    33man_MANS=op.1 
    4 EXTRA_DIST=op.conf op.conf.complex op.list.in op.pam op.paper 
     4EXTRA_DIST=op.conf op.conf.complex op.list.in op.pam op.paper op.1 
    55#README 
    66 
  • op/trunk/op.1

    r248 r249  
    184184still be logged. 
    185185.TP 
     186.B fowner 
     187Specifies the owner and group of the target command executable as a list of 
     188regular expression in the form 
     189.I user:group 
     190If the executable ownership does not match, the command will not be executed. 
     191.TP 
     192.B fperms 
     193As with 
     194.I fowner 
     195but matches against the octal permissions of the executable. 
     196.TP 
    186197.B xauth 
    187198Attempt to propagate the X authority entry for the current display to the new