Annotation of suX/src/sux.c, revision 1.3.2.5

1.1       misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.3.2.5 ! misho       6:  * $Id: sux.c,v 1.3.2.4 2013/04/09 12:55:51 misho Exp $
1.1       misho       7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: 
                     11: 
1.3.2.1   misho      12: cfg_root_t cfg;
1.1       misho      13: int Verbose;
                     14: struct tagProc proc;
1.2       misho      15: FILE *lf;
1.1       misho      16: 
                     17: 
                     18: static inline void
                     19: Log(int lvl, const char *fmt, ...)
                     20: {
1.2       misho      21:        va_list lst, cp;
1.1       misho      22: 
1.3.2.3   misho      23:        EVERBS(lvl) {
1.1       misho      24:                va_start(lst, fmt);
1.2       misho      25:                va_copy(cp, lst);
                     26:                vfprintf(lf, fmt, lst);
1.1       misho      27:                va_end(lst);
1.2       misho      28:                fprintf(lf, "\n");
                     29:                vsyslog(LOG_WARNING, fmt, cp);
                     30:                va_end(cp);
1.1       misho      31:        }
                     32: }
                     33: 
                     34: static inline void
                     35: Err(const char *fmt, ...)
                     36: {
1.2       misho      37:        va_list lst, cp;
1.1       misho      38: 
                     39:        va_start(lst, fmt);
1.2       misho      40:        va_copy(cp, lst);
                     41:        vfprintf(lf, fmt, lst);
1.1       misho      42:        va_end(lst);
1.2       misho      43:        fprintf(lf, "\n");
                     44:        vsyslog(LOG_ERR, fmt, cp);
                     45:        va_end(cp);
1.1       misho      46: }
                     47: 
1.3.2.4   misho      48: static inline void
                     49: DumpProc(const char *txt)
                     50: {
                     51:        Log(0, "%s:: UID:GID=%d:%d Prio=%d Class=%s Name=%s Dir=%s Cmd=%s Script=%s From=%s:%s%s", 
                     52:                        txt ? txt : __func__, AIT_GET_I16(&proc.proc_uid), 
                     53:                        AIT_GET_I16(&proc.proc_gid), AIT_GET_I32(&proc.proc_prio), 
                     54:                        AIT_GET_STR(&proc.proc_class), AIT_GET_STR(&proc.proc_name), 
                     55:                        AIT_GET_STR(&proc.proc_dir), AIT_GET_STR(&proc.proc_cmd), 
                     56:                        getenv("PATH_TRANSLATED"), getenv("REMOTE_ADDR"), 
                     57:                        getenv("REMOTE_PORT"), getenv("REQUEST_URI"));
                     58: }
                     59: 
1.1       misho      60: static void
                     61: initProg()
                     62: {
1.3.2.1   misho      63:        char d[MAXPATHLEN];
                     64: 
1.3.2.2   misho      65:        AIT_SET_I16(&proc.proc_uid, getuid());
                     66:        AIT_SET_I16(&proc.proc_gid, getgid());
                     67:        AIT_SET_I32(&proc.proc_prio, getpriority(PRIO_PROCESS, 0));
1.3.2.4   misho      68:        AIT_INIT_VAL2(&proc.proc_class, string);
1.3.2.1   misho      69:        getcwd(d, sizeof d);
                     70:        AIT_SET_STR(&proc.proc_dir, d);
1.3.2.4   misho      71:        AIT_INIT_VAL2(&proc.proc_name, string);
                     72:        AIT_INIT_VAL2(&proc.proc_cmd, string);
1.1       misho      73: 
1.2       misho      74: #if 0
                     75:        lf = fopen(DEFAULT_LOG, "a");
                     76:        if (!lf)
                     77: #endif
                     78:                lf = stdout;
                     79: 
                     80:        openlog(PACKAGE_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
1.1       misho      81: }
                     82: 
                     83: static void
1.3.2.3   misho      84: endProg()
                     85: {
                     86:        AIT_FREE_VAL(&proc.proc_uid);
                     87:        AIT_FREE_VAL(&proc.proc_gid);
                     88:        AIT_FREE_VAL(&proc.proc_prio);
                     89:        AIT_FREE_VAL(&proc.proc_class);
                     90:        AIT_FREE_VAL(&proc.proc_dir);
                     91:        AIT_FREE_VAL(&proc.proc_name);
                     92:        AIT_FREE_VAL(&proc.proc_cmd);
                     93: }
                     94: 
                     95: static void
1.1       misho      96: Usage()
                     97: {
                     98:        printf( " -= suX =- suExecutor designed for web based applicaions\n"
                     99:                "(C)`11 AITNET ltd - Sofia/Bulgaria - <office@aitnet.org>\n\n"
                    100:                " Syntax: %s [options] <program> [arguments]\n"
                    101:                "\t-u <user>\t\t\tUser for suID\n"
                    102:                "\t-g <group>\t\t\tGroup for suID\n"
                    103:                "\t-p <priority (-20..20)>\t\tExecute with priority\n"
                    104:                "\t-d <directory>\t\t\tDirectory for suID\n"
1.3       misho     105:                "\t-C <directory>\t\t\tChroot to directory\n"
                    106:                "\t-c <cfgfile>\t\t\tConfig file\n"
1.2       misho     107:                "\t-l <logfile>\t\t\tLog file path (default:/var/log/suX.log)\n"
1.3.2.3   misho     108:                "\t-o\t\t\t\tForce set UID,GID and Priority for program\n"
1.1       misho     109:                "\t-v\t\t\t\tVerbose, (more -v, more verbosity)\n"
                    110:                "\t-h\t\t\t\tThis help screen!\n\n", PACKAGE_NAME);
                    111: }
                    112: 
                    113: static inline int
                    114: setUIDGID(char flg, const char *name)
                    115: {
                    116:        struct stat sb;
                    117: 
                    118:        if (stat(name, &sb) == -1) {
1.3.2.3   misho     119:                ESYSERR(0);
1.1       misho     120:                return -1;
                    121:        }
                    122: 
1.3.2.3   misho     123:        if (!(flg & SUX_GET_UID))
1.3.2.2   misho     124:                AIT_SET_I16(&proc.proc_uid, sb.st_uid);
1.3.2.3   misho     125:        if (!(flg & SUX_GET_GID))
1.3.2.2   misho     126:                AIT_SET_I16(&proc.proc_gid, sb.st_gid);
1.1       misho     127: 
                    128:        return 0;
                    129: }
                    130: 
                    131: static inline int
                    132: SetClass()
                    133: {
                    134:        struct passwd *pass;
1.3.2.5 ! misho     135:        int ret = 0;
1.1       misho     136: 
1.3.2.2   misho     137:        pass = getpwuid(AIT_GET_I16(&proc.proc_uid));
1.1       misho     138:        if (!pass) {
1.3.2.3   misho     139:                Err("Error:: User with this UID %d not found", AIT_GET_I16(&proc.proc_uid));
1.1       misho     140:                endpwent();
                    141:                return -1;
                    142:        } else 
1.3.2.1   misho     143:                AIT_SET_STR(&proc.proc_class, pass->pw_class);
1.1       misho     144: 
1.3.2.5 ! misho     145:        if (setusercontext(NULL, pass, AIT_GET_I16(&proc.proc_uid), 
        !           146:                                LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETLOGIN | 
        !           147:                                LOGIN_SETPRIORITY)) {
1.3.2.1   misho     148:                Err("Error:: Cant set login class %s", AIT_GET_STR(&proc.proc_class));
1.3.2.5 ! misho     149:                ret = -1;
1.1       misho     150:        }
                    151: 
                    152:        endpwent();
1.3.2.5 ! misho     153:        return ret;
1.1       misho     154: }
                    155: 
                    156: static int
                    157: LoadCfgData(char flg)
                    158: {
1.3.2.1   misho     159:        const char *str;
                    160:        char mode = 0;
1.1       misho     161: 
1.3.2.1   misho     162:        str = cfg_getAttribute(&cfg, "global", "mode");
1.1       misho     163:        if (!str) {
                    164:                Err("Error:: Unknown mode ...");
                    165:                return -1;
                    166:        }
                    167:        if (!strcasecmp(str, "SCRIPT")) {
                    168:                mode = 1;
1.3.2.1   misho     169:                if (setUIDGID(flg, AIT_GET_STR(&proc.proc_name)) == -1)
1.1       misho     170:                        return -1;
                    171:        } else if (!strcasecmp(str, "FILE")) {
                    172:                mode = 2;
1.3.2.1   misho     173:                if (setUIDGID(flg, AIT_GET_STR(&proc.proc_name)) == -1)
1.1       misho     174:                        return -1;
                    175:        } else if (!strcasecmp(str, "DIR") && 
1.3.2.1   misho     176:                        (str = cfg_getAttribute(&cfg, "global", "directory"))) {
1.1       misho     177:                mode = 3;
1.3.2.3   misho     178:                if (!(flg & SUX_GET_DIR))
1.3.2.1   misho     179:                        AIT_SET_STR(&proc.proc_dir, str);
1.1       misho     180: 
1.3.2.1   misho     181:                if (setUIDGID(flg, AIT_GET_STR(&proc.proc_dir)) == -1)
1.1       misho     182:                        return -1;
                    183:        } else {
                    184:                Err("Error:: Unknown mode %s", str);
                    185:                return -1;
                    186:        }
1.3.2.3   misho     187:        if (!(flg & SUX_GET_PRIO)) {
1.3.2.1   misho     188:                str = cfg_getAttribute(&cfg, "global", "priority");
1.1       misho     189:                if (str)
1.3.2.2   misho     190:                        AIT_SET_I32(&proc.proc_prio, strtol(str, NULL, 10));
1.1       misho     191:        }
                    192: 
                    193:        /* find associate extension */
1.3.2.1   misho     194:        str = strrchr(AIT_GET_STR(&proc.proc_name), '.');
1.1       misho     195:        if (str) {
                    196:                str++;
                    197:                str = (*str) ? str : "default";
1.3.2.1   misho     198:                switch (cfg_loadAttribute(&cfg, "associate", str, &proc.proc_cmd, NULL)) {
1.1       misho     199:                        case -1:
1.3.2.1   misho     200:                                ELIBERR(cfg);
1.1       misho     201:                                return -1;
                    202:                        case 0:
1.3.2.1   misho     203:                                cfg_loadAttribute(&cfg, "associate", "default", &proc.proc_cmd, DEFAULT_CMD);
1.1       misho     204:                }
                    205:        } else
1.3.2.1   misho     206:                AIT_SET_STR(&proc.proc_cmd, DEFAULT_CMD);
1.1       misho     207: 
                    208:        return 0;
                    209: }
                    210: 
                    211: static int
1.3.2.3   misho     212: Run(char **argv, char flg)
1.1       misho     213: {
                    214:        char **args, *cmd;
                    215:        array_t *acmd, *aarg;
                    216:        int n;
                    217: 
                    218:        if (!argv)
                    219:                return -1;
                    220: 
1.3.2.1   misho     221:        n = array_Args(AIT_GET_STR(&proc.proc_cmd), 0, " \t", &acmd);
1.1       misho     222:        if (n < 1)
                    223:                return -1;
1.3.2.1   misho     224:        if (!(aarg = array_From((const char***) &argv, 0))) {
                    225:                array_Destroy(&acmd);
1.1       misho     226:                return -1;
1.3.2.1   misho     227:        } else if (*array(acmd, 0, char*) == '!') {
                    228:                if (array_Grow(acmd, 0, 0)) {
                    229:                        array_Destroy(&aarg);
                    230:                        array_Destroy(&acmd);
1.1       misho     231:                        return -1;
                    232:                }
1.3.2.1   misho     233:                cmd = array(aarg, 0, char*);
1.1       misho     234:        } else
1.3.2.1   misho     235:                cmd = array(acmd, 0, char*);
1.1       misho     236: 
1.3.2.1   misho     237:        if (array_Concat(acmd, aarg) == -1) {
                    238:                array_Destroy(&aarg);
                    239:                array_Destroy(&acmd);
1.1       misho     240:                return -1;
                    241:        }
1.3.2.1   misho     242:        array_Destroy(&aarg);
                    243:        if (!(args = array_To(acmd))) {
                    244:                array_Destroy(&acmd);
1.1       misho     245:                return -1;
                    246:        }
1.3.2.1   misho     247:        array_Destroy(&acmd);
1.1       misho     248: 
                    249:        if (SetClass()) {
                    250:                if (args)
1.3.2.3   misho     251:                        e_free(args);
1.1       misho     252:                return -1;
                    253:        }
                    254: 
1.3.2.3   misho     255:        if (flg & SUX_GET_FORCE) {
1.3.2.5 ! misho     256:                if (setegid(AIT_GET_I16(&proc.proc_gid)) == -1) {
1.3.2.3   misho     257:                        ESYSERR(0);
                    258:                        if (args)
1.3.2.5 ! misho     259:                                e_free(args);
1.3.2.3   misho     260:                        return -1;
                    261:                }
1.3.2.5 ! misho     262:                if (seteuid(AIT_GET_I16(&proc.proc_uid)) == -1) {
1.3.2.3   misho     263:                        ESYSERR(0);
                    264:                        if (args)
1.3.2.5 ! misho     265:                                e_free(args);
1.3.2.3   misho     266:                        return -1;
                    267:                }
                    268:                if (setpriority(PRIO_PROCESS, 0, AIT_GET_I32(&proc.proc_prio)) == -1) {
                    269:                        ESYSERR(0);
                    270:                        if (args)
1.3.2.5 ! misho     271:                                e_free(args);
1.3.2.3   misho     272:                        return -1;
                    273:                }
1.1       misho     274:        }
1.2       misho     275: 
1.3.2.4   misho     276:        DumpProc(__func__);
1.1       misho     277: 
1.3.2.3   misho     278:        EVERBS(3) {
1.1       misho     279:                char **el = args - 1;
                    280:                while (*++el)
                    281:                        Log(3, "args: %s", *el);
                    282:        }
                    283: 
1.2       misho     284:        fflush(lf);
                    285: 
1.1       misho     286:        execve(cmd, args, environ);
                    287:        if (args)
1.3.2.5 ! misho     288:                e_free(args);
1.3.2.3   misho     289:        ESYSERR(0);
1.1       misho     290:        return -1;
                    291: }
                    292: 
                    293: 
                    294: int
                    295: main(int argc, char **argv)
                    296: {
1.3       misho     297:        char ch, *str, *wrk, szCfg[MAXPATHLEN], **pp, flg = 0;
1.1       misho     298:        struct passwd *pass;
                    299:        struct group *grp;
1.2       misho     300:        FILE *f;
1.1       misho     301: 
1.2       misho     302:        initProg();
1.1       misho     303:        strlcpy(szCfg, DEFAULT_CONFIG, sizeof szCfg);
                    304: 
1.3.2.3   misho     305:        while ((ch = getopt(argc, argv, "hvoC:c:u:g:p:d:l:")) != -1)
1.1       misho     306:                switch (ch) {
1.2       misho     307:                        case 'l':
                    308:                                f = fopen(optarg, "a");
                    309:                                if (!f) {
                    310:                                        Err("Error:: logfile #%d - %s", errno, strerror(errno));
                    311:                                        return 1;
                    312:                                } else
                    313:                                        if (fileno(lf) > 2)
                    314:                                                fclose(lf);
                    315:                                lf = f;
                    316:                                break;
1.1       misho     317:                        case 'd':
1.3.2.1   misho     318:                                AIT_SET_STR(&proc.proc_dir, optarg);
1.3.2.3   misho     319:                                flg |= SUX_GET_DIR;
1.1       misho     320:                                break;
                    321:                        case 'p':
1.3.2.2   misho     322:                                AIT_SET_I32(&proc.proc_prio, strtol(optarg, NULL, 0));
1.3.2.3   misho     323:                                flg |= SUX_GET_PRIO;
1.1       misho     324:                                break;
                    325:                        case 'g':
                    326:                                setgrent();
                    327:                                grp = getgrnam(optarg);
                    328:                                if (grp) {
1.3.2.2   misho     329:                                        AIT_SET_I16(&proc.proc_gid, grp->gr_gid);
1.3.2.3   misho     330:                                        flg |= SUX_GET_GID;
1.1       misho     331:                                } else
                    332:                                        Err("Error:: Group not found!");
                    333:                                endgrent();
                    334:                                break;
                    335:                        case 'u':
                    336:                                setpwent();
                    337:                                pass = getpwnam(optarg);
                    338:                                if (pass) {
1.3.2.2   misho     339:                                        AIT_SET_I16(&proc.proc_uid, pass->pw_uid);
1.3.2.3   misho     340:                                        flg |= SUX_GET_UID;
1.1       misho     341:                                } else
                    342:                                        Err("Error:: User not found!");
                    343:                                endpwent();
                    344:                                break;
                    345:                        case 'c':
                    346:                                strlcpy(szCfg, optarg, sizeof szCfg);
                    347:                                break;
1.3       misho     348:                        case 'C':
                    349:                                if (chroot(optarg) == -1)
                    350:                                        Err("Error:: chroot to dir");
                    351:                                if ((str = getenv("PATH_TRANSLATED")))
                    352:                                        if ((wrk = strstr(str, optarg)))
                    353:                                                setenv("PATH_TRANSLATED", str + strlen(optarg), 42);
                    354:                                break;
1.3.2.3   misho     355:                        case 'o':
                    356:                                flg |= SUX_GET_FORCE;
                    357:                                break;
1.1       misho     358:                        case 'v':
1.3.2.3   misho     359:                                e_incVerbose;
1.1       misho     360:                                break;
                    361:                        case 'h':
                    362:                        default:
                    363:                                Usage();
1.3.2.3   misho     364:                                endProg();
1.2       misho     365:                                if (fileno(lf) > 2)
                    366:                                        fclose(lf);
1.1       misho     367:                                return 1;
                    368:                }
                    369:        argc -= optind;
                    370:        argv += optind;
1.2       misho     371: 
1.3.2.3   misho     372:        EVERBS(2) {
1.2       misho     373:                for (pp = argv; *pp; pp++)
                    374:                        Log(2, "Args=%s\n", *pp);
                    375:                for (pp = environ; *pp; pp++)
                    376:                        Log(2, "Envs=%s\n", *pp);
                    377:        }
                    378: 
1.1       misho     379:        if (!argc) {
                    380:                if (!(str = getenv("PATH_TRANSLATED"))) {
                    381:                        Usage();
1.3.2.3   misho     382:                        endProg();
1.2       misho     383:                        if (fileno(lf) > 2)
                    384:                                fclose(lf);
1.1       misho     385:                        return 1;
                    386:                } else
1.3.2.1   misho     387:                        AIT_SET_STR(&proc.proc_name, str);
1.1       misho     388:        } else
1.3.2.1   misho     389:                AIT_SET_STR(&proc.proc_name, *argv);
1.1       misho     390:        Log(2, "Try to load config %s", szCfg);
1.3.2.1   misho     391:        if (cfgLoadConfig(szCfg, &cfg)) {
1.3.2.3   misho     392:                ELIBERR(cfg);
                    393:                endProg();
1.2       misho     394:                if (fileno(lf) > 2)
                    395:                        fclose(lf);
1.1       misho     396:                return 2;
                    397:        } else
                    398:                if (LoadCfgData(flg) == -1) {
1.3.2.1   misho     399:                        cfgUnloadConfig(&cfg);
1.3.2.3   misho     400:                        endProg();
1.2       misho     401:                        if (fileno(lf) > 2)
                    402:                                fclose(lf);
1.1       misho     403:                        closelog();
                    404:                        return 3;
                    405:                }
1.3.2.1   misho     406:        cfgUnloadConfig(&cfg);
1.1       misho     407: 
1.3.2.3   misho     408:        if (Run(argv, flg) == -1) {
                    409:                endProg();
1.2       misho     410:                if (fileno(lf) > 2)
                    411:                        fclose(lf);
1.1       misho     412:                closelog();
                    413:                return 4;
                    414:        }
                    415: 
1.3.2.3   misho     416:        endProg();
1.2       misho     417:        if (fileno(lf) > 2)
                    418:                fclose(lf);
1.1       misho     419:        closelog();
                    420:        return 0;
                    421: }

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