source: trunk/src/sh_extern.c@ 422

Last change on this file since 422 was 415, checked in by katerina, 12 years ago

Fixes for tickets #314, #315, #316, #317, #318, #319, #320, and #321.

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