source: trunk/src/sh_extern.c@ 29

Last change on this file since 29 was 29, checked in by rainer, 19 years ago

Fixes for test suite, OpenBSD portability, self-resolving, and suidchk patch by Neil

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