Annotation of suX/src/sux.c, revision 1.1.1.1.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.1.1.1.2.5! misho 6: * $Id: sux.c,v 1.1.1.1.2.4 2011/05/20 16:41:31 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: sl_config cfg;
13: int Verbose;
14: struct tagProc proc;
1.1.1.1.2.2 misho 15: FILE *lf;
1.1 misho 16:
17:
18: static inline void
19: Log(int lvl, const char *fmt, ...)
20: {
1.1.1.1.2.5! misho 21: va_list lst, cp;
1.1 misho 22:
23: if (lvl <= Verbose) {
24: va_start(lst, fmt);
1.1.1.1.2.5! misho 25: va_copy(cp, lst);
1.1.1.1.2.2 misho 26: vfprintf(lf, fmt, lst);
1.1 misho 27: va_end(lst);
1.1.1.1.2.5! 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.1.1.1.2.5! misho 37: va_list lst, cp;
1.1 misho 38:
39: va_start(lst, fmt);
1.1.1.1.2.5! misho 40: va_copy(cp, lst);
1.1.1.1.2.2 misho 41: vfprintf(lf, fmt, lst);
1.1 misho 42: va_end(lst);
1.1.1.1.2.5! misho 43: fprintf(lf, "\n");
! 44: vsyslog(LOG_ERR, fmt, cp);
! 45: va_end(cp);
1.1 misho 46: }
47:
48: static void
49: initProg()
50: {
51: proc.proc_uid = getuid();
52: proc.proc_gid = getgid();
53: proc.proc_prio = getpriority(PRIO_PROCESS, 0);
54: getcwd(proc.proc_dir, sizeof proc.proc_dir);
55:
1.1.1.1.2.3 misho 56: lf = fopen(DEFAULT_LOG, "a");
57: if (!lf)
58: lf = stdout;
1.1.1.1.2.2 misho 59:
1.1 misho 60: openlog(PACKAGE_NAME, LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_USER);
61: }
62:
63: static void
64: Usage()
65: {
66: printf( " -= suX =- suExecutor designed for web based applicaions\n"
67: "(C)`11 AITNET ltd - Sofia/Bulgaria - <office@aitnet.org>\n\n"
68: " Syntax: %s [options] <program> [arguments]\n"
69: "\t-u <user>\t\t\tUser for suID\n"
70: "\t-g <group>\t\t\tGroup for suID\n"
71: "\t-p <priority (-20..20)>\t\tExecute with priority\n"
72: "\t-d <directory>\t\t\tDirectory for suID\n"
1.1.1.1.2.2 misho 73: "\t-l <logfile>\t\t\tLog file path (default:/var/log/suX.log)\n"
1.1 misho 74: "\t-v\t\t\t\tVerbose, (more -v, more verbosity)\n"
75: "\t-h\t\t\t\tThis help screen!\n\n", PACKAGE_NAME);
76: }
77:
78: static inline int
79: setUIDGID(char flg, const char *name)
80: {
81: struct stat sb;
82:
83: if (stat(name, &sb) == -1) {
84: Err("Error:: %s stat #%d - %s", name, errno, strerror(errno));
85: return -1;
86: }
87:
88: if (!(flg & 1))
89: proc.proc_uid = sb.st_uid;
90: if (!(flg & 2))
91: proc.proc_gid = sb.st_gid;
92:
93: return 0;
94: }
95:
96: static inline int
97: SetClass()
98: {
99: login_cap_t *cap;
100: struct passwd *pass;
101:
102: pass = getpwuid(proc.proc_uid);
103: if (!pass) {
104: Err("Error:: User with this UID %d not found", proc.proc_uid);
105: endpwent();
106: return -1;
107: } else
108: strlcpy(proc.proc_class, pass->pw_class, sizeof proc.proc_class);
109:
110: cap = login_getclass(proc.proc_class);
111: if (!cap) {
112: Err("Error:: Cant get login class %s", proc.proc_class);
113: endpwent();
114: return -1;
115: }
116:
117: if (setusercontext(cap, pass, proc.proc_uid, LOGIN_SETALL)) {
118: Err("Error:: Cant set login class %s", proc.proc_class);
119: login_close(cap);
120: endpwent();
121: return -1;
122: }
123:
124: login_close(cap);
125: endpwent();
126: return 0;
127: }
128:
129: static int
130: LoadCfgData(char flg)
131: {
132: char *str, mode = 0;
133:
134: str = (char*) cfg_GetAttribute(&cfg, CFG("global"), CFG("mode"));
135: if (!str) {
136: Err("Error:: Unknown mode ...");
137: return -1;
138: }
139: if (!strcasecmp(str, "SCRIPT")) {
140: mode = 1;
141: if (setUIDGID(flg, proc.proc_name) == -1)
142: return -1;
143: } else if (!strcasecmp(str, "FILE")) {
144: mode = 2;
145: if (setUIDGID(flg, proc.proc_name) == -1)
146: return -1;
147: } else if (!strcasecmp(str, "DIR") &&
148: (str = (char*) cfg_GetAttribute(&cfg, CFG("global"), CFG("directory")))) {
149: mode = 3;
150: if (!(flg & 8))
151: strlcpy(proc.proc_dir, str, sizeof proc.proc_dir);
152:
153: if (setUIDGID(flg, proc.proc_dir) == -1)
154: return -1;
155: } else {
156: Err("Error:: Unknown mode %s", str);
157: return -1;
158: }
159: if (!(flg & 4)) {
160: str = (char*) cfg_GetAttribute(&cfg, CFG("global"), CFG("priority"));
161: if (str)
162: proc.proc_prio = strtol(str, NULL, 10);
163: }
164:
165: /* find associate extension */
166: str = strrchr(proc.proc_name, '.');
167: if (str) {
168: str++;
169: str = (*str) ? str : "default";
170: switch (cfg_LoadAttribute(&cfg, CFG("associate"), CFG(str),
171: CFG(proc.proc_cmd), sizeof proc.proc_cmd, NULL)) {
172: case -1:
173: Err("Error:: can`t load attribute #%d - %s\n", cfg_GetErrno(), cfg_GetError());
174: return -1;
175: case 0:
176: cfg_LoadAttribute(&cfg, CFG("associate"), CFG("default"),
177: CFG(proc.proc_cmd), sizeof proc.proc_cmd, DEFAULT_CMD);
178: }
179: } else
180: strlcpy(proc.proc_cmd, DEFAULT_CMD, sizeof proc.proc_cmd);
181:
182: return 0;
183: }
184:
185: static int
186: Run(char **argv)
187: {
188: char **args, *cmd;
189: array_t *acmd, *aarg;
190: int n;
191:
192: if (!argv)
193: return -1;
194:
195: n = io_arrayMake(proc.proc_cmd, 0, " \t", &acmd);
196: if (n < 1)
197: return -1;
198: if (!(aarg = io_arrayFrom((const char**) argv, 0))) {
199: io_arrayDestroy(&acmd);
200: return -1;
201: } else if (*io_arrayGet(acmd, 0, char*) == '!') {
202: if (io_arrayGrow(acmd, 0)) {
203: io_arrayDestroy(&aarg);
204: io_arrayDestroy(&acmd);
205: return -1;
206: }
207: cmd = io_arrayGet(aarg, 0, char*);
208: } else
209: cmd = io_arrayGet(acmd, 0, char*);
210:
211: if (io_arrayConcat(acmd, aarg) == -1) {
212: io_arrayDestroy(&aarg);
213: io_arrayDestroy(&acmd);
214: return -1;
215: }
216: io_arrayDestroy(&aarg);
217: if (!(args = io_arrayTo(acmd))) {
218: io_arrayDestroy(&acmd);
219: return -1;
220: }
221: io_arrayDestroy(&acmd);
222:
223: if (SetClass()) {
224: if (args)
225: free(args);
226: return -1;
227: }
228:
229: if (setgid(proc.proc_gid) == -1) {
230: Err("Error:: setgid #%d - %s\n", errno, strerror(errno));
231: if (args)
232: free(args);
233: return -1;
234: }
235: if (setuid(proc.proc_uid) == -1) {
236: Err("Error:: setuid #%d - %s\n", errno, strerror(errno));
237: if (args)
238: free(args);
239: return -1;
240: }
241: if (setpriority(PRIO_PROCESS, 0, proc.proc_prio) == -1) {
242: Err("Error:: setpriority #%d - %s\n", errno, strerror(errno));
243: if (args)
244: free(args);
245: return -1;
246: }
247:
1.1.1.1.2.4 misho 248: Log(0, "UID:GID=%d:%d Prio=%d Class=%s Name=%s Dir=%s Cmd=%s", proc.proc_uid, proc.proc_gid,
1.1.1.1.2.1 misho 249: proc.proc_prio, proc.proc_class, proc.proc_name, proc.proc_dir, proc.proc_cmd);
250:
1.1 misho 251: if (3 <= Verbose) {
252: char **el = args - 1;
253: while (*++el)
254: Log(3, "args: %s", *el);
255: }
256:
1.1.1.1.2.4 misho 257: fflush(lf);
258:
1.1 misho 259: execve(cmd, args, environ);
260: if (args)
261: free(args);
262: Err("Error:: in exec() #%d - %s", errno, strerror(errno));
263: return -1;
264: }
265:
266:
267: int
268: main(int argc, char **argv)
269: {
1.1.1.1.2.3 misho 270: char ch, *str, szCfg[MAXPATHLEN], flg = 0;
1.1 misho 271: struct passwd *pass;
272: struct group *grp;
1.1.1.1.2.2 misho 273: FILE *f;
1.1 misho 274:
1.1.1.1.2.2 misho 275: initProg();
1.1 misho 276: strlcpy(szCfg, DEFAULT_CONFIG, sizeof szCfg);
277:
1.1.1.1.2.2 misho 278: while ((ch = getopt(argc, argv, "hvc:u:g:p:d:l:")) != -1)
1.1 misho 279: switch (ch) {
1.1.1.1.2.2 misho 280: case 'l':
1.1.1.1.2.3 misho 281: f = fopen(optarg, "a");
1.1.1.1.2.2 misho 282: if (!f) {
283: Err("Error:: logfile #%d - %s", errno, strerror(errno));
284: return 1;
285: } else
1.1.1.1.2.3 misho 286: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 287: fclose(lf);
288: lf = f;
289: break;
1.1 misho 290: case 'd':
291: strlcpy(proc.proc_dir, optarg, sizeof proc.proc_dir);
292: flg |= 8;
293: break;
294: case 'p':
295: proc.proc_prio = strtol(optarg, NULL, 0);
296: flg |= 4;
297: break;
298: case 'g':
299: setgrent();
300: grp = getgrnam(optarg);
301: if (grp) {
302: proc.proc_gid = grp->gr_gid;
303: flg |= 2;
304: } else
305: Err("Error:: Group not found!");
306: endgrent();
307: break;
308: case 'u':
309: setpwent();
310: pass = getpwnam(optarg);
311: if (pass) {
312: proc.proc_uid = pass->pw_uid;
313: flg |= 1;
314: } else
315: Err("Error:: User not found!");
316: endpwent();
317: break;
318: case 'c':
319: strlcpy(szCfg, optarg, sizeof szCfg);
320: break;
321: case 'v':
322: Verbose++;
323: break;
324: case 'h':
325: default:
326: Usage();
1.1.1.1.2.3 misho 327: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 328: fclose(lf);
1.1 misho 329: return 1;
330: }
331: argc -= optind;
332: argv += optind;
333: if (!argc) {
334: if (!(str = getenv("PATH_TRANSLATED"))) {
335: Usage();
1.1.1.1.2.3 misho 336: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 337: fclose(lf);
1.1 misho 338: return 1;
339: } else
340: strlcpy(proc.proc_name, str, sizeof proc.proc_name);
341: } else
342: strlcpy(proc.proc_name, *argv, sizeof proc.proc_name);
343: Log(2, "Try to load config %s", szCfg);
344: if (LoadConfig(szCfg, &cfg)) {
345: Err("Error:: can`t load config #%d - %s\n", cfg_GetErrno(), cfg_GetError());
1.1.1.1.2.3 misho 346: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 347: fclose(lf);
1.1 misho 348: return 2;
349: } else
350: if (LoadCfgData(flg) == -1) {
351: UnloadConfig(&cfg);
1.1.1.1.2.3 misho 352: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 353: fclose(lf);
1.1 misho 354: closelog();
355: return 3;
356: }
357: UnloadConfig(&cfg);
358:
359: if (Run(argv) == -1) {
1.1.1.1.2.3 misho 360: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 361: fclose(lf);
1.1 misho 362: closelog();
363: return 4;
364: }
365:
1.1.1.1.2.3 misho 366: if (fileno(lf) > 2)
1.1.1.1.2.2 misho 367: fclose(lf);
1.1 misho 368: closelog();
369: return 0;
370: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>