source: trunk/src/sh_extern.c@ 200

Last change on this file since 200 was 174, checked in by katerina, 16 years ago

Fix for tickets #112, #113 (dnmalloc deadlock on fork, hostname portability in test script).

File size: 32.7 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2000,2004 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config_xor.h"
22
23
24#include <stdio.h>
25#include <string.h>
26#ifdef HAVE_MEMORY_H
27#include <memory.h>
28#endif
29
30/* replace #if 0 by #if 1 and set an appropriate path in front of '/pdbg.'
31 * for debugging
32 */
33#if 0
34#define PDGBFILE "/pdbg."
35#endif
36
37
38#if defined(PDGBFILE)
39static FILE * pdbg = NULL;
40static FILE * pdbgc = NULL;
41#define PDBG_OPEN if (pdbg == NULL) pdbg = fopen(PDGBFILE"main", "a")
42#define PDBG_CLOSE fclose (pdbg); pdbg = NULL
43#define PDBG(arg) fprintf(pdbg, "PDBG: step %d\n", arg); fflush(pdbg)
44#define PDBG_D(arg) fprintf(pdbg, "PDBG: %d\n", arg); fflush(pdbg)
45#define PDBG_S(arg) fprintf(pdbg, "PDBG: %s\n", arg); fflush(pdbg)
46
47#define PDBGC_OPEN if (pdbgc == NULL) pdbgc = fopen(PDGBFILE"child", "a")
48#define PDBGC_CLOSE fclose (pdbgc); pdbgc = NULL
49#define PDBGC(arg) fprintf(pdbgc, "PDBGC: step %d\n", arg); fflush(pdbgc)
50#define PDBGC_D(arg) fprintf(pdbgc, "PDBGC: %d\n", arg); fflush(pdbgc)
51#define PDBGC_S(arg) fprintf(pdbgc, "PDBGC: %s\n", arg); fflush(pdbgc)
52#else
53#define PDBG_OPEN
54#define PDBG_CLOSE
55#define PDBG(arg)
56#define PDBG_D(arg)
57#define PDBG_S(arg)
58#define PDBGC_OPEN
59#define PDBGC_CLOSE
60#define PDBGC(arg)
61#define PDBGC_D(arg)
62#define PDBGC_S(arg)
63#endif
64
65
66#include <stdlib.h>
67#include <pwd.h>
68#include <unistd.h>
69#include <fcntl.h>
70#include <signal.h>
71#include <sys/stat.h>
72#include <sys/types.h>
73#include <errno.h>
74#include <sys/wait.h>
75
76#if TIME_WITH_SYS_TIME
77#include <sys/time.h>
78#include <time.h>
79#else
80#if HAVE_SYS_TIME_H
81#include <sys/time.h>
82#else
83#include <time.h>
84#endif
85#endif
86
87
88#include "samhain.h"
89#include "sh_utils.h"
90#include "sh_unix.h"
91#include "sh_tiger.h"
92#include "sh_extern.h"
93#include "sh_calls.h"
94#define SH_NEED_PWD_GRP 1
95#include "sh_static.h"
96
97
98#undef FIL__
99#define FIL__ _("sh_extern.c")
100
101extern int get_the_fd (SL_TICKET ticket);
102
103/*
104 * -- generic safe popen
105 */
106
107int sh_ext_popen (sh_tas_t * task)
108{
109 long status = 0;
110 int flags;
111 char * tmp;
112 char * tmp2;
113 int errnum;
114 int pipedes[2];
115 FILE * outf = NULL;
116 char * envp[1];
[29]117 char * argp[2];
[1]118
119 char * errfile;
[132]120 char errbuf[SH_ERRBUF_SIZE];
[1]121
122 static int some_error = 0;
123
124#if defined (__linux__)
125 SL_TICKET fd = -1;
126 char pname[128];
127 int pfd = -1;
128#endif
[102]129
[1]130 SL_ENTER(_("sh_ext_popen"));
131
132 /* Linux, HP-UX and FreeBSD will happily accept envp = argp = NULL
133 * Solaris (and probably some other Unices)
134 * needs a valid *envp[] with envp[0] = NULL;
135 * and similarly for argp
[29]136 * OpenBSD finally needs non-null argp[0] ...
[1]137 */
[29]138 argp[0] = task->command;
139 argp[1] = NULL;
[1]140 envp[0] = NULL;
141
142 /*
143 * -- check whether path is trustworthy
144 */
[169]145 status = sl_trustfile(task->command, NULL, NULL);
146#if 0
[1]147 if ((uid_t) -1 != task->trusted_users[0])
148 {
149 status = sl_trustfile(task->command, task->trusted_users, NULL);
150 }
[169]151#endif
[1]152
153 PDBG_OPEN;
154 PDBG_D( (int) status);
155
156 if ( SL_ENONE != status)
157 {
158 PDBG_S("SL_ENONE != status");
159 if (some_error == 0)
160 {
161 tmp = sh_util_safe_name (task->command);
162 errfile = sl_trust_errfile();
163 if (errfile[0] != '\0')
164 {
165 tmp2 = sh_util_safe_name (sl_trust_errfile());
166 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_TRUST2,
167 sl_error_string((int)status), tmp, tmp2);
168 SH_FREE(tmp2);
169 }
170 else
171 {
172 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_TRUST1,
173 sl_error_string((int)status), tmp);
174 }
175 SH_FREE(tmp);
176 }
177 some_error = 1;
178 SL_RETURN ((-1), _("sh_ext_popen"));
179 }
180
181 PDBG(1);
182
183 /*
184 * -- check whether the checksum is correct; with linux emulate fdexec
185 */
186#if !defined(__linux__) && !defined(SL_DEBUG)
187 if (task->checksum[0] != '\0')
188 {
[133]189 char hashbuf[KEYBUF_SIZE];
[1]190 PDBG_S("checksum test");
191 if (0 != sl_strcmp(task->checksum,
[160]192 sh_tiger_hash (task->command, TIGER_FILE, TIGER_NOLIM,
[133]193 hashbuf, sizeof(hashbuf))
194 )
[1]195 )
196 {
197 PDBG_S("checksum mismatch");
198 if (some_error == 0)
199 {
200 tmp = sh_util_safe_name (task->command);
201 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HASH, tmp);
202 SH_FREE(tmp);
203 }
204 some_error = 1;
205 SL_RETURN ((-1), _("sh_ext_popen"));
206 }
207 }
208#endif
209
210 some_error = 0;
211
212 PDBG(2);
213
214 /*
215 * -- Create the pipe
216 */
217 if (aud_pipe(FIL__, __LINE__, pipedes) < 0)
218 {
219 PDBG_S("pipe() failure");
220 errnum = errno;
221 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
[132]222 sh_error_message(errnum, errbuf, sizeof(errbuf)), _("pipe"));
[1]223 SL_RETURN ((-1), _("sh_ext_popen"));
224 }
225
226 PDBG(3);
[102]227
[1]228 /*
[102]229 * -- Flush streams and fork
[1]230 */
[102]231 fflush (NULL);
232
[1]233 task->pid = aud_fork(FIL__, __LINE__);
234
235 if (task->pid == (pid_t) - 1)
236 {
237 PDBG_S("fork() failure");
238 /*@-usedef@*/
239 (void) close(pipedes[0]);
240 (void) close(pipedes[1]);
241 /*@+usedef@*/
242 errnum = errno;
243 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
[132]244 sh_error_message(errnum, errbuf, sizeof(errbuf)), _("fork"));
[1]245 SL_RETURN ((-1), _("sh_ext_popen"));
246 }
247
248 PDBG(4);
249
250 if (task->pid == (pid_t) 0)
251 {
252 /*
253 * -- fork again, if requested
254 */
255 if (S_TRUE == task->fork_twice)
256 {
257 task->pid = aud_fork(FIL__, __LINE__);
258
259 if (task->pid == (pid_t) - 1)
260 {
261 aud__exit (FIL__, __LINE__, EXIT_FAILURE);
262 }
263 }
264
265 if (task->pid == (pid_t) 0)
266 {
267 PDBGC_OPEN;
268 PDBGC(1);
269
270 /*
271 * -- grandchild - make write side of the pipe stdin
272 */
273 if (task->rw == 'w')
274 {
275 if (retry_aud_dup2(FIL__, __LINE__,
276 pipedes[STDIN_FILENO], STDIN_FILENO) < 0)
277 aud__exit(FIL__, __LINE__,EXIT_FAILURE);
278 }
279 else
280 {
281 if (retry_aud_dup2(FIL__, __LINE__,
282 pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
283 aud__exit(FIL__, __LINE__,EXIT_FAILURE);
284 }
285 PDBGC(2);
286
287
288 /* close the pipe descriptors
289 */
290 (void) close (pipedes[STDIN_FILENO]);
291 (void) close (pipedes[STDOUT_FILENO]);
292
293 /* don't leak file descriptors
294 */
295#if !defined(PDGBFILE)
[174]296 sh_unix_closeall (3, task->com_fd, SL_TRUE); /* in child process */
[1]297#endif
298
299 /* drop root privileges, if possible && requested
300 */
301 if (task->privileged == 0 && 0 == getuid())
302 {
303 PDBGC_S("privileged");
304
305 /* zero priv info
306 */
307 memset(skey, 0, sizeof(sh_key_t));
308
309 (void) aud_setgid(FIL__, __LINE__,(gid_t) task->run_user_gid);
310 (void) aud_setuid(FIL__, __LINE__,(uid_t) task->run_user_uid);
311 /* make sure we cannot get root again
312 */
313 if (aud_setuid(FIL__, __LINE__,0) >= 0)
314 aud__exit(FIL__, __LINE__,EXIT_FAILURE);
315 }
316
317 PDBGC(3);
318 (void) fflush(NULL);
319
320 if (task->rw == 'w')
321 {
322 PDBGC_S("w");
323 (void) fcntl (STDOUT_FILENO, F_SETFD, FD_CLOEXEC);
324 (void) fcntl (STDERR_FILENO, F_SETFD, FD_CLOEXEC);
325 /*
326 freopen(_("/dev/null"), "r+", stderr);
327 freopen(_("/dev/null"), "r+", stdout);
328 */
329 }
330 else
331 {
332 PDBGC_S("r");
333 (void) retry_aud_dup2 (FIL__, __LINE__,
334 STDOUT_FILENO, STDERR_FILENO);
335 (void) fcntl (STDIN_FILENO, F_SETFD, FD_CLOEXEC);
336 /*
337 freopen(_("/dev/null"), "r+", stdin);
338 */
339 }
340
341 PDBGC(4);
342
343
344#if defined(__linux__)
345 /*
346 * -- emulate an fdexec with checksum testing
347 */
348 if (task->checksum[0] != '\0')
349 {
350 PDBGC_S("fexecve");
351 if (task->com_fd != (-1))
352 {
353 pfd = retry_aud_dup(FIL__, __LINE__, task->com_fd);
354 if (pfd < 0)
355 {
356 PDBGC_S("fexecve: dup2 failed");
357 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
358 }
359 }
360 else
361 {
[133]362 char hashbuf[KEYBUF_SIZE];
363
[1]364 fd =
365 sl_open_read(task->command,
366 task->privileged==0 ? SL_NOPRIV : SL_YESPRIV);
[133]367
[1]368 if (0 != sl_strcmp(task->checksum,
369 sh_tiger_hash (task->command,
[160]370 fd, TIGER_NOLIM, hashbuf, sizeof(hashbuf))))
[1]371 {
372 PDBGC_S("fexecve: checksum mismatch");
373 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
374 }
375 pfd = get_the_fd(fd);
376 }
377
378 PDBGC(5);
[22]379 sl_snprintf(pname, sizeof(pname), _("/proc/self/fd/%d"), pfd);
380 if (access(pname, R_OK|X_OK) == 0) /* flawfinder: ignore */
[1]381 {
382 PDBGC(6);
383 PDBGC_CLOSE;
384 fcntl (pfd, F_SETFD, FD_CLOEXEC);
385 retry_aud_execve (FIL__, __LINE__,
386 pname,
387 (task->argc == 0) ? NULL : task->argv,
388 (task->envc == 0) ? NULL : task->envv
389 );
390
391 errnum = errno;
392 PDBGC_OPEN;
393 PDBGC_S(strerror(errnum));
394 PDBGC_S(task->command);
395 PDBGC_S("fexecve: failed");
396 PDBGC_CLOSE;
397 /* failed
398 */
399 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
400 }
401 PDBGC_S("fexecve: not working");
402 /*
403 * procfs not working, go ahead; checksum is tested already
404 */
405 if (fd != -1)
406 sl_close(fd);
407 else if (pfd != -1)
408 close(fd);
409 }
410#endif
411
412 PDBGC_S(" -- non fexecve --");
413 /*
414 * -- execute path if executable
415 */
[22]416 if (0 == access(task->command, R_OK|X_OK)) /* flawfinder: ignore */
[1]417 {
418 PDBGC(5);
419 PDBGC_CLOSE;
420 (void) retry_aud_execve (FIL__, __LINE__,
421 task->command,
422 (task->argc == 0) ? argp : task->argv,
423 (task->envc == 0) ? envp : task->envv
424 );
425 }
426 errnum = errno;
427 PDBGC_OPEN;
428 PDBGC_S(strerror(errnum));
429 PDBGC_S(task->command);
430 PDBGC_S("execve: failed");
431 PDBGC_CLOSE;
432 /* failed
433 */
434 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
435 }
436 /*
437 * if we have forked twice, this is parent::detached_subprocess
438 */
439 if (S_TRUE == task->fork_twice)
440 {
441 aud__exit (FIL__, __LINE__, 0);
442 }
443 }
444
445
446 /*
447 * -- parent; task->pid is child pid; exit status is status of
448 * grandchild if exited
449 */
450 if (S_TRUE == task->fork_twice)
451 {
452 (void) waitpid (task->pid, NULL, 0);
453 }
454
455 PDBG(5);
456 /* open an output stream on top of the write side of the pipe
457 */
458 if (task->rw == 'w')
459 {
460 PDBG_S("is w");
461 (void) close (pipedes[STDIN_FILENO]);
462 (void) retry_fcntl (FIL__, __LINE__, pipedes[STDOUT_FILENO],
463 F_SETFD, FD_CLOEXEC);
464 outf = fdopen (pipedes[STDOUT_FILENO], "w");
465 }
466 else
467 {
468 PDBG_S("is r");
469 (void) close (pipedes[STDOUT_FILENO]);
470 (void) retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO],
471 F_SETFD, FD_CLOEXEC);
472 outf = fdopen (pipedes[STDIN_FILENO], "r");
473 }
474
475 if (outf == NULL)
476 {
477 errnum = errno;
478 PDBG_S("outf == NULL");
479 tmp = sh_util_safe_name (task->command);
480
481 if (task->privileged == 0 && 0 == getuid())
482 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_NOEXEC,
483 (UID_CAST) task->run_user_uid, tmp);
484 else
485 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_NOEXEC,
486 (UID_CAST) getuid(), tmp);
487
488 SH_FREE(tmp);
489
490 (void) aud_kill (FIL__, __LINE__, task->pid, SIGKILL);
491 (void) close (pipedes[STDOUT_FILENO]);
492 (void) close (pipedes[STDIN_FILENO]);
493 (void) waitpid (task->pid, NULL, 0);
494 task->pid = 0;
495
496 SL_RETURN ((-1), _("sh_ext_popen"));
497 }
498
499 if (task->rw == 'w')
500 task->pipeFD = pipedes[STDOUT_FILENO];
501 else
502 task->pipeFD = pipedes[STDIN_FILENO];
503
504 PDBG_D(task->pipeFD);
505
506 task->pipeTI = sl_make_ticket(task->pipeFD, _("pipe"));
507
508 flags = (int) retry_fcntl (FIL__, __LINE__, task->pipeFD, F_GETFL, 0);
509 if (flags != (-1))
510 (void) retry_fcntl (FIL__, __LINE__, task->pipeFD,
511 F_SETFL, flags|O_NONBLOCK);
512 task->pipe = outf;
513
514 PDBG_S("return from popen");
515 PDBG_CLOSE;
516
517 SL_RETURN (0, _("sh_ext_popen"));
518}
519
520/*
521 * -- close the pipe
522 */
523extern int flag_err_debug;
524
525int sh_ext_pclose (sh_tas_t * task)
526{
527 int status = 0;
528 int retry = 0;
529 pid_t retval;
530 char infomsg[256];
531
532 SL_ENTER(_("sh_ext_pclose"));
533
534 PDBGC_OPEN;
535 PDBG_S(" -> pclose");
536 (void) fflush(task->pipe);
537 (void) fclose(task->pipe);
538 if (!SL_ISERROR(task->pipeTI))
539 (void) sl_close(task->pipeTI);
540
541 task->pipe = NULL;
542 task->pipeFD = (-1);
543 task->pipeTI = SL_ETICKET;
544
545 if (S_FALSE == task->fork_twice)
546 {
547 infomsg[0] = '\0';
548
549 nochmal:
550 retval = waitpid(task->pid, &(task->exit_status), WNOHANG|WUNTRACED);
551 /*@-bufferoverflowhigh@*/
552 if (task->pid == retval)
553 {
[96]554#ifndef USE_UNO
[1]555 if (WIFEXITED(task->exit_status) != 0)
556 {
557 task->exit_status = WEXITSTATUS(task->exit_status);
558 if ((flag_err_debug == SL_TRUE) || (task->exit_status != 0))
[22]559 sl_snprintf(infomsg, sizeof(infomsg),
560 _("Subprocess exited normally with status %d"),
561 task->exit_status);
[1]562 }
563 else if (WIFSIGNALED(task->exit_status) != 0)
564 {
[22]565 sl_snprintf(infomsg, sizeof(infomsg),
566 _("Subprocess terminated by signal %d"),
567 WTERMSIG(task->exit_status));
[1]568 task->exit_status = EXIT_FAILURE;
569 }
570 else if (WIFSTOPPED(task->exit_status) != 0)
571 {
[22]572 sl_snprintf(infomsg, sizeof(infomsg),
573 _("Subprocess stopped by signal %d, killing"),
574 WSTOPSIG(task->exit_status));
[1]575 task->exit_status = EXIT_FAILURE;
576 (void) aud_kill (FIL__, __LINE__, task->pid, 9);
577 (void) retry_msleep (0, 30);
578 (void) waitpid (task->pid, NULL, WNOHANG|WUNTRACED);
579 }
580 else
581 {
[22]582 sl_snprintf(infomsg, sizeof(infomsg),
583 _("Subprocess exit status unknown"));
[1]584 task->exit_status = EXIT_FAILURE;
585 }
[96]586#else
587 task->exit_status = EXIT_FAILURE;
588#endif
[1]589 }
590 else if (0 == retval)
591 {
592 if (retry < 3)
593 {
594 ++retry;
595 (void) retry_msleep(0, (retry * 30));
596 goto nochmal;
597 }
598 (void) aud_kill (FIL__, __LINE__, task->pid, 9);
[22]599 sl_snprintf(infomsg, sizeof(infomsg),
600 _("Subprocess not yet exited, killing"));
[1]601 task->exit_status = EXIT_FAILURE;
602 (void) waitpid (task->pid, NULL, 0);
603 }
604 else
605 {
[22]606 sl_snprintf(infomsg, sizeof(infomsg),
607 _("Waitpid returned error %d\n"), errno);
[1]608 task->exit_status = EXIT_FAILURE;
609 }
610 /*@+bufferoverflowhigh@*/
611 status = task->exit_status;
612 if (flag_err_debug == SL_TRUE)
613 {
614 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, task->exit_status,
615 MSG_E_SUBGEN, infomsg, _("sh_ext_pclose"));
616 }
617 else if (status != 0)
618 {
619 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, task->exit_status,
620 MSG_E_SUBGEN, infomsg, _("sh_ext_pclose"));
621 }
622 }
623
624 task->pid = 0;
625 task->exit_status = 0;
626 PDBG_S(" <--");
627 PDBG_CLOSE;
628 SL_RETURN (status, _("sh_ext_pclose"));
629}
630
631void sh_ext_tas_init (sh_tas_t * tas)
632{
633 int i;
634
635 tas->command = NULL;
636 tas->argc = 0;
637 tas->envc = 0;
638 tas->checksum[0] = '\0';
639 tas->pipeFD = (-1);
640 tas->pipeTI = SL_ETICKET;
641 tas->pid = (pid_t) -1;
642 tas->privileged = 1;
643 tas->pipe = NULL;
644 tas->rw = 'w';
645 tas->exit_status = 0;
646 tas->fork_twice = S_TRUE;
647
648 for (i = 0; i < 32; ++i)
649 {
650 tas->argv[i] = NULL;
651 tas->envv[i] = NULL;
[169]652#if 0
[1]653 tas->trusted_users[i] = (uid_t) -1;
[169]654#endif
[1]655 }
656
657 tas->run_user_uid = (uid_t) getuid();
658 tas->run_user_gid = (gid_t) getgid();
659
660 tas->com_fd = -1;
661 tas->com_ti = -1;
662 return;
663}
664
665
[22]666int sh_ext_tas_add_envv(sh_tas_t * tas, const char * key, const char * val)
[1]667{
668 size_t sk = 0, sv = 0;
669 int si;
670
671 SL_ENTER(_("sh_ext_tas_add_envv"));
672
673 if (tas == NULL || (key == NULL && val == NULL) ||
674 tas->envc >= 30)
675 {
676 SL_RETURN (-1, _("sh_ext_tas_add_envv"));
677 }
678 if (key != NULL)
679 sk = strlen(key) + 1;
680 if (val != NULL)
681 sv = strlen(val) + 1;
682
[34]683 if (!sl_ok_adds(sk, sv))
684 {
685 SL_RETURN (-1, _("sh_ext_tas_add_envv"));
686 }
[1]687 si = tas->envc;
688 tas->envv[si] = SH_ALLOC(sk + sv);
689
690 if (key != NULL)
691 {
692 (void) sl_strlcpy(tas->envv[si], key, sk+sv);
693 (void) sl_strlcat(tas->envv[si], "=", sk+sv);
694 if (val != NULL)
695 (void) sl_strlcat(tas->envv[si], val, sk+sv);
696 }
697 else
698 (void) sl_strlcpy(tas->envv[si], val, sv);
699
700 ++(tas->envc);
701 SL_RETURN ((tas->envc), _("sh_ext_tas_add_envv"));
702}
703
704int sh_ext_tas_rm_argv(sh_tas_t * tas)
705{
706 int last;
707
708 SL_ENTER(_("sh_ext_tas_rm_argv"));
709 if (tas == NULL || tas->argc == 0)
710 {
711 SL_RETURN (-1, _("sh_ext_tas_rm_argv"));
712 }
713
714 last = (tas->argc - 1);
715 --(tas->argc);
716 SH_FREE(tas->argv[last]);
717 tas->argv[last] = NULL;
718 SL_RETURN ((tas->argc), _("sh_ext_tas_rm_argv"));
719}
720
[22]721int sh_ext_tas_add_argv(sh_tas_t * tas, const char * val)
[1]722{
723 size_t sv = 0;
724 int si;
725
726 SL_ENTER(_("sh_ext_tas_add_argv"));
727
728 if (tas == NULL || val == NULL ||
729 tas->argc >= 30)
730 {
731 SL_RETURN (-1, _("sh_ext_tas_add_argv"));
732 }
733
734 if (val != NULL)
735 sv = strlen(val) + 1;
736
737 si = tas->argc;
738 tas->argv[si] = SH_ALLOC(sv);
739
740 (void) sl_strlcpy(tas->argv[si], val, sv);
741
742 ++(tas->argc);
743 SL_RETURN ((tas->argc), _("sh_ext_tas_add_argv"));
744}
745
[22]746void sh_ext_tas_command(sh_tas_t * tas, const char * command)
[1]747{
748 size_t len = sl_strlen(command);
749 tas->command = SH_ALLOC(len+1);
750 (void) sl_strlcpy(tas->command, command, len+1);
751 return;
752}
753
754void sh_ext_tas_free(sh_tas_t * tas)
755{
756 int i;
757 if (NULL != tas->command) SH_FREE(tas->command);
758
759 for (i = 0; i < 32; ++i)
760 {
761 if (NULL != tas->argv[i]) SH_FREE(tas->argv[i]);
762 if (NULL != tas->envv[i]) SH_FREE(tas->envv[i]);
763 }
764
765 if (tas->com_ti != (-1))
766 {
767 (void) sl_close(tas->com_ti);
768 tas->com_ti = -1;
769 tas->com_fd = -1;
770 }
771
772 return;
773}
774
775
776/* --------------- EXTERN STUFF ------------------- */
777
778#if defined(WITH_EXTERNAL)
779
780typedef struct _sh_com_t
781{
782 char type[4];
783
784 int for_c;
785 char * for_v[32];
786 int fand_c;
787 char * fand_v[32];
788 int fnot_c;
789 char * fnot_v[32];
790 time_t deadtime;
791 time_t last_run;
792
793 sh_tas_t tas;
794
795 struct _sh_com_t * next;
796
797} sh_com_t;
798
799
800static
801void set3 (char * pos, char c1, char c2, char c3)
802{
803 pos[0] = c1;
804 pos[1] = c2;
805 pos[2] = c3;
806 pos[3] = '\0';
807 return;
808}
809
810
811
812/* initialize the external command structure
813 */
814static
815sh_com_t * command_init(void)
816{
817 int i;
818 uid_t ff_euid;
819 sh_com_t * ext_com = NULL;
820
821 SL_ENTER(_("command_init"));
822
823 ext_com = (sh_com_t *) SH_ALLOC(sizeof(sh_com_t));
824
825 if (!ext_com)
826 {
[170]827 SL_RETURN( NULL, _("command_init"));
[1]828 }
829
830 sh_ext_tas_init (&(ext_com->tas));
831
832 (void) sl_get_euid(&ff_euid);
[169]833#if 0
[1]834 ext_com->tas.trusted_users[0] = (uid_t) 0;
835 ext_com->tas.trusted_users[1] = (uid_t) (ff_euid);
[169]836#endif
[1]837
838 /* ------------------------------------------------- */
839
840 set3(ext_com->type, 'l', 'o', 'g');
841 ext_com->for_c = 0;
842 ext_com->fand_c = 0;
843 ext_com->fnot_c = 0;
844 ext_com->deadtime = 0;
845 ext_com->last_run = 0;
846
847 for (i = 0; i < 32; ++i)
848 {
849 ext_com->for_v[i] = NULL;
850 ext_com->fand_v[i] = NULL;
851 ext_com->fnot_v[i] = NULL;
852 }
853 ext_com->next = NULL;
854
[170]855 SL_RETURN( ext_com, _("command_init"));
[1]856}
857
858/* the list of external commands
859 */
860static sh_com_t * ext_coms = NULL;
861
862/* if -1, allocation of last command has failed,
863 * thus don't fill in options
864 */
865static int ext_failed = -1;
866
867static
[22]868int sh_ext_add_envv(const char * key, const char * val)
[1]869{
[34]870 int retval;
871
[1]872 SL_ENTER(_("sh_ext_add_envv"));
873
874 if (ext_coms == NULL || ext_failed == (-1) ||
875 (key == NULL && val == NULL) ||
876 ext_coms->tas.envc >= 30)
877 {
878 SL_RETURN (-1, _("sh_ext_add_envv"));
879 }
880
[34]881 retval = sh_ext_tas_add_envv(&(ext_coms->tas), key, val);
[1]882
[34]883 if (retval >= 0)
884 retval = 0;
885
886 SL_RETURN (retval, _("sh_ext_add_envv"));
[1]887}
888
889
890
891static
[22]892int sh_ext_init(const char * command)
[1]893{
894 sh_com_t * retval;
895 size_t size;
896
897 SL_ENTER(_("sh_ext_init"));
898
899 if (command == NULL)
900 {
901 SL_RETURN (-1, _("sh_ext_init"));
902 }
903 size = strlen(command);
904 if (command[0] != '/' || size < 2)
905 {
906 SL_RETURN (-1, _("sh_ext_init"));
907 }
908
909 if (NULL == (retval = command_init()))
910 {
911 SL_RETURN (-1, _("sh_ext_init"));
912 }
913
914 sh_ext_tas_command(&(retval->tas), command);
915
916 if (sh.timezone != NULL)
917 {
918 (void) sh_ext_add_envv( "TZ", sh.timezone);
919 }
920
921 retval->next = ext_coms;
922 ext_coms = retval;
923 SL_RETURN (0, _("sh_ext_init"));
924}
925
926static
[22]927int sh_ext_uid (const char * user, /*@out@*/uid_t * uid, /*@out@*/gid_t * gid)
[1]928{
[131]929 struct passwd * tempres;
930#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
931 struct passwd pwd;
932 char buffer[SH_PWBUF_SIZE];
933#endif
[1]934
935 SL_ENTER(_("sh_ext_uid"));
936
937 *uid = (uid_t)-1; *gid = (gid_t)-1;
938
939 if (user == NULL)
940 {
941 SL_RETURN (-1, _("sh_ext_uid"));
942 }
[131]943
944#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
945 sh_getpwnam_r(user, &pwd, buffer, sizeof(buffer), &tempres);
946#else
[1]947 tempres = sh_getpwnam(user);
[131]948#endif
[1]949
950 if (NULL != tempres)
951 {
952 *uid = tempres->pw_uid;
953 *gid = tempres->pw_gid;
954 SL_RETURN (0, _("sh_ext_uid"));
955 }
956
957 SL_RETURN (-1, _("sh_ext_uid"));
958}
959
960
961static
[22]962int sh_ext_add (const char * argstring, int * ntok, char * stok[])
[1]963{
964 int i = 0;
965 size_t s;
966 char * p;
[22]967 char * new;
968 size_t len;
[1]969
970 SL_ENTER(_("sh_ext_add"));
971
972 if (NULL == argstring)
973 {
974 SL_RETURN((-1), _("sh_ext_add"));
975 }
976
[22]977 len = strlen(argstring) + 1;
978 new = SH_ALLOC(len);
979 sl_strlcpy(new, argstring, len);
980
[1]981 do
982 {
[131]983#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
984 char * saveptr;
[1]985 if (i == 0)
[131]986 p = strtok_r (new, ", \t", &saveptr);
987 else
988 p = strtok_r (NULL, ", \t", &saveptr);
989#else
990 if (i == 0)
[22]991 p = strtok (new, ", \t");
[1]992 else
993 p = strtok (NULL, ", \t");
[131]994#endif
995
[1]996 if (p == NULL)
997 break;
998
999 s = strlen(p) + 1;
1000 if (stok[i] != NULL)
1001 SH_FREE(stok[i]);
1002 stok[i] = SH_ALLOC(s);
1003 (void) sl_strlcpy(stok[i], p, s);
1004
1005 ++i;
1006 if (i == 30)
1007 break;
1008 }
1009 while (p != NULL);
1010
1011 *ntok = i;
[22]1012 SH_FREE(new);
[1]1013
1014 SL_RETURN (0, _("sh_ext_add"));
1015}
1016
1017/*********************************************************
1018 *
1019 * Public functions
1020 *
1021 *
1022 *********************************************************/
1023
1024/*
1025 * -- start a new external command, and add it to the list
1026 */
[22]1027int sh_ext_setcommand(const char * cmd)
[1]1028{
1029 int i;
1030
1031 SL_ENTER(_("sh_ext_setcommand"));
1032 if ( (i = sh_ext_init(cmd)) < 0)
1033 ext_failed = -1;
1034 else
1035 ext_failed = 0;
1036 SL_RETURN( i, _("sh_ext_setcommand"));
1037}
1038
1039
1040/*
1041 * -- clean up the command list
1042 */
1043int sh_ext_cleanup(void)
1044{
1045 int i;
1046 sh_com_t * retval;
1047
1048 SL_ENTER(_("sh_ext_cleanup"));
1049
1050 while (ext_coms != NULL)
1051 {
1052 retval = ext_coms;
1053 ext_coms = retval->next;
1054
1055 sh_ext_tas_free (&(retval->tas));
1056
1057 for (i = 0; i < 32; ++i)
1058 {
1059 if (NULL != retval->for_v[i]) SH_FREE(retval->for_v[i]);
1060 if (NULL != retval->fand_v[i]) SH_FREE(retval->fand_v[i]);
1061 if (NULL != retval->fnot_v[i]) SH_FREE(retval->fnot_v[i]);
1062 }
1063
1064 SH_FREE(retval);
1065
1066 }
1067
1068 SL_RETURN (0, _("sh_ext_cleanup"));
1069}
1070
1071/*
[164]1072 * -- explicitely close a command
1073 */
1074int sh_ext_close_command (const char * str)
1075{
1076 (void) str;
1077 if (ext_coms == NULL || ext_failed == (-1))
1078 return (-1);
1079 ext_failed = (-1);
1080 return 0;
1081}
1082
1083/*
[1]1084 * -- add keywords to the OR filter
1085 */
[22]1086int sh_ext_add_or (const char * str)
[1]1087{
1088 if (ext_coms == NULL || ext_failed == (-1))
1089 return (-1);
1090 return (sh_ext_add (str, &(ext_coms->for_c), ext_coms->for_v));
1091}
1092
1093/*
1094 * -- add keywords to the AND filter
1095 */
[22]1096int sh_ext_add_and (const char * str)
[1]1097{
1098 if (ext_coms == NULL || ext_failed == (-1))
1099 return (-1);
1100 return (sh_ext_add (str, &(ext_coms->fand_c), ext_coms->fand_v));
1101}
1102
1103/*
1104 * -- add keywords to the NOT filter
1105 */
[22]1106int sh_ext_add_not (const char * str)
[1]1107{
1108 if (ext_coms == NULL || ext_failed == (-1))
1109 return (-1);
1110 return (sh_ext_add (str, &(ext_coms->fnot_c), ext_coms->fnot_v));
1111}
1112
1113/*
1114 * -- add keywords to the CL argument list
1115 */
[22]1116int sh_ext_add_argv (const char * str)
[1]1117{
1118 if (ext_coms == NULL || ext_failed == (-1))
1119 return (-1);
1120 return (sh_ext_add (str, &(ext_coms->tas.argc), ext_coms->tas.argv));
1121}
1122
1123/*
1124 * -- add a path to the environment
1125 */
[22]1126int sh_ext_add_default (const char * dummy)
[1]1127{
[22]1128 char * p = NULL;
[1]1129 int i;
[132]1130 char dir[SH_PATHBUF];
[1]1131
1132 SL_ENTER(_("sh_ext_add_default"));
1133 if (dummy[0] == 'n' || dummy[0] == 'N' ||
1134 dummy[0] == 'f' || dummy[0] == 'F' || dummy[0] == '0')
1135 {
1136 SL_RETURN(0, _("sh_ext_add_default"));
1137 }
[132]1138 p = sh_unix_getUIDdir (SH_ERR_ERR, (uid_t) ext_coms->tas.run_user_uid,
1139 dir, sizeof(dir));
[1]1140 if (p)
1141 (void) sh_ext_add_envv (_("HOME"), p);
1142 (void) sh_ext_add_envv (_("SHELL"), _("/bin/sh"));
1143 (void) sh_ext_add_envv (_("PATH"), _("/sbin:/usr/sbin:/bin:/usr/bin"));
1144 i = (p == NULL ? (-1) : 0);
1145 SL_RETURN(i, _("sh_ext_add_default"));
1146}
1147
1148/*
1149 * -- add an environment variable
1150 */
[22]1151int sh_ext_add_environ (const char * str)
[1]1152{
1153 int i;
[49]1154
[1]1155 SL_ENTER(_("sh_ext_add_environ"));
1156 i = sh_ext_add_envv (NULL, str);
1157 SL_RETURN(i, _("sh_ext_add_environ"));
1158}
1159
1160/*
1161 * -- set deadtime
1162 */
[22]1163int sh_ext_deadtime (const char * str)
[1]1164{
1165 long deadtime = 0;
1166 char * tail = NULL;
1167
1168 SL_ENTER(_("sh_ext_deadtime"));
1169
1170 if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
1171 {
[170]1172 SL_RETURN (-1, _("sh_ext_deadtime"));
[1]1173 }
1174 deadtime = strtol(str, &tail, 10);
1175 if (tail == str || deadtime < 0 || deadtime == LONG_MAX)
1176 {
[170]1177 SL_RETURN (-1, _("sh_ext_deadtime"));
[1]1178 }
1179
1180 ext_coms->deadtime = (time_t) deadtime;
[170]1181 SL_RETURN (0, _("sh_ext_deadtime"));
[1]1182}
1183
1184/*
1185 * -- define type
1186 */
[22]1187int sh_ext_type (const char * str)
[1]1188{
1189 SL_ENTER(_("sh_ext_type"));
1190
1191 if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
1192 {
1193 SL_RETURN((-1), _("sh_ext_type"));
1194 }
1195
1196 if (strlen(str) != 3)
1197 {
1198 SL_RETURN((-1), _("sh_ext_type"));
1199 }
1200
1201 set3(ext_coms->type, str[0], str[1], str[2]);
1202
1203 if (str[0] == 'l' && str[1] == 'o' && str[2] == 'g')
1204 ext_coms->tas.rw = 'w';
1205 else if (str[0] == 's' && str[1] == 'r' && str[2] == 'v')
1206 ext_coms->tas.rw = 'w';
1207 else if (str[0] == 'm' && str[1] == 'o' && str[2] == 'n')
1208 ext_coms->tas.rw = 'r';
1209 else
1210 {
1211 SL_RETURN((-1), _("sh_ext_type"));
1212 }
1213
1214 SL_RETURN(0, _("sh_ext_type"));
1215}
1216
1217
1218
1219/*
1220 * -- define checksum
1221 */
[22]1222int sh_ext_checksum (const char * str)
[1]1223{
1224 SL_ENTER(_("sh_ext_checksum"));
1225 if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
1226 {
1227 SL_RETURN((-1), _("sh_ext_checksum"));
1228 }
1229
1230 if (sl_strlen(str) != KEY_LEN)
1231 {
1232 SL_RETURN((-1), _("sh_ext_checksum"));
1233 }
1234
1235 (void) sl_strlcpy (ext_coms->tas.checksum, str, KEY_LEN+1);
1236
1237 SL_RETURN((0), _("sh_ext_checksum"));
1238}
1239
1240/*
1241 * -- choose privileges
1242 */
[22]1243int sh_ext_priv (const char * c)
[1]1244{
1245
1246 uid_t me_uid;
1247 gid_t me_gid;
1248
1249 SL_ENTER(_("sh_ext_priv"));
1250 if (0 == sh_ext_uid (c, &me_uid, &me_gid))
1251 {
1252 ext_coms->tas.run_user_uid = me_uid;
1253 ext_coms->tas.run_user_gid = me_gid;
1254 if (me_uid != (uid_t) 0)
1255 ext_coms->tas.privileged = 0;
1256 SL_RETURN((0), _("sh_ext_priv"));
1257 }
1258
1259 SL_RETURN (-1, _("sh_ext_priv"));
1260}
1261
1262
1263
1264
1265/*
1266 * -- check filters
1267 */
1268static int sh_ext_filter (char * message, sh_com_t * task)
1269{
1270 int i;
1271 int j = 0;
1272 time_t now_time;
1273
1274 SL_ENTER(_("sh_ext_filter"));
1275
1276 /* Presence of any of these keywords prevents execution.
1277 */
1278 if (task->fnot_c > 0)
1279 {
1280 for (i = 0; i < task->fnot_c; ++i)
1281 {
1282 if (NULL != sl_strstr(message, task->fnot_v[i]))
1283 {
1284 SL_RETURN ((-1), _("sh_ext_filter"));
1285 }
1286 }
1287 }
1288
1289 /* Presence of all of these keywords is required for execution.
1290 */
1291 if (task->fand_c > 0)
1292 {
1293 j = 0;
1294
1295 for (i = 0; i < task->fand_c; ++i)
1296 if (NULL != sl_strstr(message, task->fand_v[i]))
1297 ++j;
1298
1299 if (j != task->fand_c)
1300 {
1301 SL_RETURN ((-1), _("sh_ext_filter"));
1302 }
1303
1304 }
1305
1306 /* Presence of at least one of these keywords is required for execution.
1307 */
1308 if (task->for_c > 0)
1309 {
1310 for (i = 0; i < task->for_c; ++i)
1311 {
1312 if (NULL != sl_strstr(message, task->for_v[i]))
1313 {
1314 goto checkdeadtime;
1315 }
1316 }
1317 SL_RETURN ((-1), _("sh_ext_filter"));
1318 }
1319
1320 checkdeadtime:
1321 if (task->deadtime != (time_t) 0) /* deadtime */
1322 {
1323 now_time = time (NULL);
1324
1325 if (task->last_run == (time_t) 0)
1326 {
1327 task->last_run = now_time;
1328 }
1329 else if ((time_t)(now_time-task->last_run) < task->deadtime)
1330 {
1331 SL_RETURN ((-1), _("sh_ext_filter"));
1332 }
1333 else
1334 {
1335 task->last_run = now_time;
1336 }
1337 }
1338
1339 SL_RETURN ((0), _("sh_ext_filter"));
1340}
1341
1342
1343
1344/*
1345 * -- execute external script/program
1346 */
1347int sh_ext_execute (char t1, char t2, char t3, /*@null@*/char * message,
1348 size_t msg_siz)
1349{
1350 int caperr;
1351 sh_com_t * listval = ext_coms;
1352 int status = 0;
1353 char * tmp;
[132]1354 char errbuf[SH_ERRBUF_SIZE];
[1]1355
1356 static int some_error = 0;
1357
1358 struct sigaction new_act;
1359 struct sigaction old_act;
1360
1361 SL_ENTER(_("sh_ext_execute"));
1362
1363 PDBG_OPEN;
1364
1365 if (listval == NULL || message == NULL)
1366 {
1367 SL_RETURN ((-1), _("sh_ext_execute"));
1368 }
1369
1370 PDBG(-1);
1371
1372 if (msg_siz == 0)
1373 msg_siz = sl_strlen(message);
1374
1375
1376 /* ignore SIGPIPE (instead get EPIPE if connection is closed)
1377 */
1378 new_act.sa_handler = SIG_IGN;
1379 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
1380
1381 while (listval != NULL)
1382 {
1383 PDBG(-2);
1384 if (t1 == listval->type[0] &&
1385 t2 == listval->type[1] &&
1386 t3 == listval->type[2] &&
1387 0 == sh_ext_filter (message, listval))
1388 {
1389 PDBG(-3);
1390
1391 if (0 != (caperr = sl_get_cap_sub()))
1392 {
1393 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
[132]1394 sh_error_message (caperr, errbuf, sizeof(errbuf)),
[1]1395 _("sl_get_cap_sub"));
1396 }
1397 if (0 == sh_ext_popen (&(listval->tas)))
1398 {
1399 PDBG_OPEN;
1400 PDBG(-4);
1401 if (NULL != listval->tas.pipe && listval->tas.rw == 'w')
1402 {
1403 PDBG(-5);
1404 if (message != NULL)
1405 {
1406 PDBG(-6);
1407 status = (int) write (listval->tas.pipeFD,
1408 message, msg_siz);
1409 if (status >= 0)
1410 status = (int) write (listval->tas.pipeFD, "\n", 1);
1411 }
1412 PDBG_D(status);
1413 if (status >= 0)
1414 status = (int) write (listval->tas.pipeFD, "[", 1);
1415 PDBG_D(status);
1416 if (status >= 0)
1417 status = (int) write (listval->tas.pipeFD, "E", 1);
1418 PDBG_D(status);
1419 if (status >= 0)
1420 status = (int) write (listval->tas.pipeFD, "O", 1);
1421 PDBG_D(status);
1422 if (status >= 0)
1423 status = (int) write (listval->tas.pipeFD, "F", 1);
1424 PDBG_D(status);
1425 if (status >= 0)
1426 status = (int) write (listval->tas.pipeFD, "]", 1);
1427 PDBG_D(status);
1428 if (status >= 0)
1429 status = (int) write (listval->tas.pipeFD, "\n", 1);
1430 PDBG_D(status);
1431 if (status >= 0)
1432 {
1433 some_error = 0;
1434 }
1435 if ((status < 0) && (some_error == 0))
1436 {
1437 some_error = 1;
1438 PDBG_S("some error");
1439 PDBG_D(status);
1440 tmp = sh_util_safe_name (listval->tas.command);
1441
1442 if (tmp)
1443 {
1444 if (listval->tas.privileged == 0 &&
1445 (0 == getuid() || 0 != sl_is_suid()) )
1446 sh_error_handle((-1), FIL__, __LINE__, 0,
1447 MSG_NOEXEC,
1448 (UID_CAST) listval->tas.run_user_uid,
1449 tmp);
1450 else
1451 sh_error_handle((-1), FIL__, __LINE__, 0,
1452 MSG_NOEXEC,
1453 (UID_CAST) getuid(), tmp);
1454
1455 SH_FREE(tmp);
1456 }
1457
1458 }
1459 PDBG(-7);
1460 (void) fflush(listval->tas.pipe);
1461 }
1462 PDBG(-8);
1463 (void) sh_ext_pclose(&(listval->tas));
1464 }
1465 else
1466 {
1467 PDBG_OPEN;
1468 PDBG_S("0 != sh_ext_popen()");
1469 }
1470 if (0 != (caperr = sl_drop_cap_sub()))
1471 {
1472 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
[132]1473 sh_error_message (caperr, errbuf, sizeof(errbuf)),
[1]1474 _("sl_drop_cap_sub"));
1475 }
1476
1477 }
1478 listval = listval->next;
1479 }
1480 PDBG_OPEN;
1481 PDBG_S("no more commands");
1482
1483 /* restore old signal handler
1484 */
1485 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
1486 PDBG_S("return");
1487 PDBG_CLOSE;
1488
1489 SL_RETURN ((0), _("sh_ext_execute"));
1490}
1491
1492
1493/* #if defined(WITH_EXTERNAL) */
1494#endif
Note: See TracBrowser for help on using the repository browser.