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)
|
---|
39 | static FILE * pdbg = NULL;
|
---|
40 | static 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 |
|
---|
101 | extern int get_the_fd (SL_TICKET ticket);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * -- generic safe popen
|
---|
105 | */
|
---|
106 |
|
---|
107 | int 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 | */
|
---|
512 | extern int flag_err_debug;
|
---|
513 |
|
---|
514 | int 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 |
|
---|
616 | void 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 |
|
---|
649 | int 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 | if (!sl_ok_adds(sk, sv))
|
---|
667 | {
|
---|
668 | SL_RETURN (-1, _("sh_ext_tas_add_envv"));
|
---|
669 | }
|
---|
670 | si = tas->envc;
|
---|
671 | tas->envv[si] = SH_ALLOC(sk + sv);
|
---|
672 |
|
---|
673 | if (key != NULL)
|
---|
674 | {
|
---|
675 | (void) sl_strlcpy(tas->envv[si], key, sk+sv);
|
---|
676 | (void) sl_strlcat(tas->envv[si], "=", sk+sv);
|
---|
677 | if (val != NULL)
|
---|
678 | (void) sl_strlcat(tas->envv[si], val, sk+sv);
|
---|
679 | }
|
---|
680 | else
|
---|
681 | (void) sl_strlcpy(tas->envv[si], val, sv);
|
---|
682 |
|
---|
683 | ++(tas->envc);
|
---|
684 | SL_RETURN ((tas->envc), _("sh_ext_tas_add_envv"));
|
---|
685 | }
|
---|
686 |
|
---|
687 | int sh_ext_tas_rm_argv(sh_tas_t * tas)
|
---|
688 | {
|
---|
689 | int last;
|
---|
690 |
|
---|
691 | SL_ENTER(_("sh_ext_tas_rm_argv"));
|
---|
692 | if (tas == NULL || tas->argc == 0)
|
---|
693 | {
|
---|
694 | SL_RETURN (-1, _("sh_ext_tas_rm_argv"));
|
---|
695 | }
|
---|
696 |
|
---|
697 | last = (tas->argc - 1);
|
---|
698 | --(tas->argc);
|
---|
699 | SH_FREE(tas->argv[last]);
|
---|
700 | tas->argv[last] = NULL;
|
---|
701 | SL_RETURN ((tas->argc), _("sh_ext_tas_rm_argv"));
|
---|
702 | }
|
---|
703 |
|
---|
704 | int sh_ext_tas_add_argv(sh_tas_t * tas, const char * val)
|
---|
705 | {
|
---|
706 | size_t sv = 0;
|
---|
707 | int si;
|
---|
708 |
|
---|
709 | SL_ENTER(_("sh_ext_tas_add_argv"));
|
---|
710 |
|
---|
711 | if (tas == NULL || val == NULL ||
|
---|
712 | tas->argc >= 30)
|
---|
713 | {
|
---|
714 | SL_RETURN (-1, _("sh_ext_tas_add_argv"));
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (val != NULL)
|
---|
718 | sv = strlen(val) + 1;
|
---|
719 |
|
---|
720 | si = tas->argc;
|
---|
721 | tas->argv[si] = SH_ALLOC(sv);
|
---|
722 |
|
---|
723 | (void) sl_strlcpy(tas->argv[si], val, sv);
|
---|
724 |
|
---|
725 | ++(tas->argc);
|
---|
726 | SL_RETURN ((tas->argc), _("sh_ext_tas_add_argv"));
|
---|
727 | }
|
---|
728 |
|
---|
729 | void sh_ext_tas_command(sh_tas_t * tas, const char * command)
|
---|
730 | {
|
---|
731 | size_t len = sl_strlen(command);
|
---|
732 | tas->command = SH_ALLOC(len+1);
|
---|
733 | (void) sl_strlcpy(tas->command, command, len+1);
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | void sh_ext_tas_free(sh_tas_t * tas)
|
---|
738 | {
|
---|
739 | int i;
|
---|
740 | if (NULL != tas->command) SH_FREE(tas->command);
|
---|
741 |
|
---|
742 | for (i = 0; i < 32; ++i)
|
---|
743 | {
|
---|
744 | if (NULL != tas->argv[i]) SH_FREE(tas->argv[i]);
|
---|
745 | if (NULL != tas->envv[i]) SH_FREE(tas->envv[i]);
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (tas->com_ti != (-1))
|
---|
749 | {
|
---|
750 | (void) sl_close(tas->com_ti);
|
---|
751 | tas->com_ti = -1;
|
---|
752 | tas->com_fd = -1;
|
---|
753 | }
|
---|
754 |
|
---|
755 | return;
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 | /* --------------- EXTERN STUFF ------------------- */
|
---|
760 |
|
---|
761 | #if defined(WITH_EXTERNAL)
|
---|
762 |
|
---|
763 | typedef struct _sh_com_t
|
---|
764 | {
|
---|
765 | char type[4];
|
---|
766 |
|
---|
767 | int for_c;
|
---|
768 | char * for_v[32];
|
---|
769 | int fand_c;
|
---|
770 | char * fand_v[32];
|
---|
771 | int fnot_c;
|
---|
772 | char * fnot_v[32];
|
---|
773 | time_t deadtime;
|
---|
774 | time_t last_run;
|
---|
775 |
|
---|
776 | sh_tas_t tas;
|
---|
777 |
|
---|
778 | struct _sh_com_t * next;
|
---|
779 |
|
---|
780 | } sh_com_t;
|
---|
781 |
|
---|
782 |
|
---|
783 | static
|
---|
784 | void set3 (char * pos, char c1, char c2, char c3)
|
---|
785 | {
|
---|
786 | pos[0] = c1;
|
---|
787 | pos[1] = c2;
|
---|
788 | pos[2] = c3;
|
---|
789 | pos[3] = '\0';
|
---|
790 | return;
|
---|
791 | }
|
---|
792 |
|
---|
793 |
|
---|
794 |
|
---|
795 | /* initialize the external command structure
|
---|
796 | */
|
---|
797 | static
|
---|
798 | sh_com_t * command_init(void)
|
---|
799 | {
|
---|
800 | int i;
|
---|
801 | uid_t ff_euid;
|
---|
802 | sh_com_t * ext_com = NULL;
|
---|
803 |
|
---|
804 | SL_ENTER(_("command_init"));
|
---|
805 |
|
---|
806 | ext_com = (sh_com_t *) SH_ALLOC(sizeof(sh_com_t));
|
---|
807 |
|
---|
808 | if (!ext_com)
|
---|
809 | {
|
---|
810 | SL_RETURN( NULL, ("command_init"));
|
---|
811 | }
|
---|
812 |
|
---|
813 | sh_ext_tas_init (&(ext_com->tas));
|
---|
814 |
|
---|
815 | (void) sl_get_euid(&ff_euid);
|
---|
816 | ext_com->tas.trusted_users[0] = (uid_t) 0;
|
---|
817 | ext_com->tas.trusted_users[1] = (uid_t) (ff_euid);
|
---|
818 |
|
---|
819 | /* ------------------------------------------------- */
|
---|
820 |
|
---|
821 | set3(ext_com->type, 'l', 'o', 'g');
|
---|
822 | ext_com->for_c = 0;
|
---|
823 | ext_com->fand_c = 0;
|
---|
824 | ext_com->fnot_c = 0;
|
---|
825 | ext_com->deadtime = 0;
|
---|
826 | ext_com->last_run = 0;
|
---|
827 |
|
---|
828 | for (i = 0; i < 32; ++i)
|
---|
829 | {
|
---|
830 | ext_com->for_v[i] = NULL;
|
---|
831 | ext_com->fand_v[i] = NULL;
|
---|
832 | ext_com->fnot_v[i] = NULL;
|
---|
833 | }
|
---|
834 | ext_com->next = NULL;
|
---|
835 |
|
---|
836 | SL_RETURN( ext_com, ("command_init"));
|
---|
837 | }
|
---|
838 |
|
---|
839 | /* the list of external commands
|
---|
840 | */
|
---|
841 | static sh_com_t * ext_coms = NULL;
|
---|
842 |
|
---|
843 | /* if -1, allocation of last command has failed,
|
---|
844 | * thus don't fill in options
|
---|
845 | */
|
---|
846 | static int ext_failed = -1;
|
---|
847 |
|
---|
848 | static
|
---|
849 | int sh_ext_add_envv(const char * key, const char * val)
|
---|
850 | {
|
---|
851 | int retval;
|
---|
852 |
|
---|
853 | SL_ENTER(_("sh_ext_add_envv"));
|
---|
854 |
|
---|
855 | if (ext_coms == NULL || ext_failed == (-1) ||
|
---|
856 | (key == NULL && val == NULL) ||
|
---|
857 | ext_coms->tas.envc >= 30)
|
---|
858 | {
|
---|
859 | SL_RETURN (-1, _("sh_ext_add_envv"));
|
---|
860 | }
|
---|
861 |
|
---|
862 | retval = sh_ext_tas_add_envv(&(ext_coms->tas), key, val);
|
---|
863 |
|
---|
864 | if (retval >= 0)
|
---|
865 | retval = 0;
|
---|
866 |
|
---|
867 | SL_RETURN (retval, _("sh_ext_add_envv"));
|
---|
868 | }
|
---|
869 |
|
---|
870 |
|
---|
871 |
|
---|
872 | static
|
---|
873 | int sh_ext_init(const char * command)
|
---|
874 | {
|
---|
875 | sh_com_t * retval;
|
---|
876 | size_t size;
|
---|
877 |
|
---|
878 | SL_ENTER(_("sh_ext_init"));
|
---|
879 |
|
---|
880 | if (command == NULL)
|
---|
881 | {
|
---|
882 | SL_RETURN (-1, _("sh_ext_init"));
|
---|
883 | }
|
---|
884 | size = strlen(command);
|
---|
885 | if (command[0] != '/' || size < 2)
|
---|
886 | {
|
---|
887 | SL_RETURN (-1, _("sh_ext_init"));
|
---|
888 | }
|
---|
889 |
|
---|
890 | if (NULL == (retval = command_init()))
|
---|
891 | {
|
---|
892 | SL_RETURN (-1, _("sh_ext_init"));
|
---|
893 | }
|
---|
894 |
|
---|
895 | sh_ext_tas_command(&(retval->tas), command);
|
---|
896 |
|
---|
897 | if (sh.timezone != NULL)
|
---|
898 | {
|
---|
899 | (void) sh_ext_add_envv( "TZ", sh.timezone);
|
---|
900 | }
|
---|
901 |
|
---|
902 | retval->next = ext_coms;
|
---|
903 | ext_coms = retval;
|
---|
904 | SL_RETURN (0, _("sh_ext_init"));
|
---|
905 | }
|
---|
906 |
|
---|
907 | static
|
---|
908 | int sh_ext_uid (const char * user, /*@out@*/uid_t * uid, /*@out@*/gid_t * gid)
|
---|
909 | {
|
---|
910 | struct passwd * tempres;
|
---|
911 |
|
---|
912 | SL_ENTER(_("sh_ext_uid"));
|
---|
913 |
|
---|
914 | *uid = (uid_t)-1; *gid = (gid_t)-1;
|
---|
915 |
|
---|
916 | if (user == NULL)
|
---|
917 | {
|
---|
918 | SL_RETURN (-1, _("sh_ext_uid"));
|
---|
919 | }
|
---|
920 | tempres = sh_getpwnam(user);
|
---|
921 |
|
---|
922 | if (NULL != tempres)
|
---|
923 | {
|
---|
924 | *uid = tempres->pw_uid;
|
---|
925 | *gid = tempres->pw_gid;
|
---|
926 | SL_RETURN (0, _("sh_ext_uid"));
|
---|
927 | }
|
---|
928 |
|
---|
929 | SL_RETURN (-1, _("sh_ext_uid"));
|
---|
930 | }
|
---|
931 |
|
---|
932 |
|
---|
933 | static
|
---|
934 | int sh_ext_add (const char * argstring, int * ntok, char * stok[])
|
---|
935 | {
|
---|
936 | int i = 0;
|
---|
937 | size_t s;
|
---|
938 | char * p;
|
---|
939 | char * new;
|
---|
940 | size_t len;
|
---|
941 |
|
---|
942 | SL_ENTER(_("sh_ext_add"));
|
---|
943 |
|
---|
944 | if (NULL == argstring)
|
---|
945 | {
|
---|
946 | SL_RETURN((-1), _("sh_ext_add"));
|
---|
947 | }
|
---|
948 |
|
---|
949 | len = strlen(argstring) + 1;
|
---|
950 | new = SH_ALLOC(len);
|
---|
951 | sl_strlcpy(new, argstring, len);
|
---|
952 |
|
---|
953 | do
|
---|
954 | {
|
---|
955 | if (i == 0)
|
---|
956 | p = strtok (new, ", \t");
|
---|
957 | else
|
---|
958 | p = strtok (NULL, ", \t");
|
---|
959 | if (p == NULL)
|
---|
960 | break;
|
---|
961 |
|
---|
962 | s = strlen(p) + 1;
|
---|
963 | if (stok[i] != NULL)
|
---|
964 | SH_FREE(stok[i]);
|
---|
965 | stok[i] = SH_ALLOC(s);
|
---|
966 | (void) sl_strlcpy(stok[i], p, s);
|
---|
967 |
|
---|
968 | ++i;
|
---|
969 | if (i == 30)
|
---|
970 | break;
|
---|
971 | }
|
---|
972 | while (p != NULL);
|
---|
973 |
|
---|
974 | *ntok = i;
|
---|
975 | SH_FREE(new);
|
---|
976 |
|
---|
977 | SL_RETURN (0, _("sh_ext_add"));
|
---|
978 | }
|
---|
979 |
|
---|
980 | /*********************************************************
|
---|
981 | *
|
---|
982 | * Public functions
|
---|
983 | *
|
---|
984 | *
|
---|
985 | *********************************************************/
|
---|
986 |
|
---|
987 | /*
|
---|
988 | * -- start a new external command, and add it to the list
|
---|
989 | */
|
---|
990 | int sh_ext_setcommand(const char * cmd)
|
---|
991 | {
|
---|
992 | int i;
|
---|
993 |
|
---|
994 | SL_ENTER(_("sh_ext_setcommand"));
|
---|
995 | if ( (i = sh_ext_init(cmd)) < 0)
|
---|
996 | ext_failed = -1;
|
---|
997 | else
|
---|
998 | ext_failed = 0;
|
---|
999 | SL_RETURN( i, _("sh_ext_setcommand"));
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 |
|
---|
1003 | /*
|
---|
1004 | * -- clean up the command list
|
---|
1005 | */
|
---|
1006 | int sh_ext_cleanup(void)
|
---|
1007 | {
|
---|
1008 | int i;
|
---|
1009 | sh_com_t * retval;
|
---|
1010 |
|
---|
1011 | SL_ENTER(_("sh_ext_cleanup"));
|
---|
1012 |
|
---|
1013 | while (ext_coms != NULL)
|
---|
1014 | {
|
---|
1015 | retval = ext_coms;
|
---|
1016 | ext_coms = retval->next;
|
---|
1017 |
|
---|
1018 | sh_ext_tas_free (&(retval->tas));
|
---|
1019 |
|
---|
1020 | for (i = 0; i < 32; ++i)
|
---|
1021 | {
|
---|
1022 | if (NULL != retval->for_v[i]) SH_FREE(retval->for_v[i]);
|
---|
1023 | if (NULL != retval->fand_v[i]) SH_FREE(retval->fand_v[i]);
|
---|
1024 | if (NULL != retval->fnot_v[i]) SH_FREE(retval->fnot_v[i]);
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | SH_FREE(retval);
|
---|
1028 |
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | SL_RETURN (0, _("sh_ext_cleanup"));
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * -- add keywords to the OR filter
|
---|
1036 | */
|
---|
1037 | int sh_ext_add_or (const char * str)
|
---|
1038 | {
|
---|
1039 | if (ext_coms == NULL || ext_failed == (-1))
|
---|
1040 | return (-1);
|
---|
1041 | return (sh_ext_add (str, &(ext_coms->for_c), ext_coms->for_v));
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /*
|
---|
1045 | * -- add keywords to the AND filter
|
---|
1046 | */
|
---|
1047 | int sh_ext_add_and (const char * str)
|
---|
1048 | {
|
---|
1049 | if (ext_coms == NULL || ext_failed == (-1))
|
---|
1050 | return (-1);
|
---|
1051 | return (sh_ext_add (str, &(ext_coms->fand_c), ext_coms->fand_v));
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * -- add keywords to the NOT filter
|
---|
1056 | */
|
---|
1057 | int sh_ext_add_not (const char * str)
|
---|
1058 | {
|
---|
1059 | if (ext_coms == NULL || ext_failed == (-1))
|
---|
1060 | return (-1);
|
---|
1061 | return (sh_ext_add (str, &(ext_coms->fnot_c), ext_coms->fnot_v));
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /*
|
---|
1065 | * -- add keywords to the CL argument list
|
---|
1066 | */
|
---|
1067 | int sh_ext_add_argv (const char * str)
|
---|
1068 | {
|
---|
1069 | if (ext_coms == NULL || ext_failed == (-1))
|
---|
1070 | return (-1);
|
---|
1071 | return (sh_ext_add (str, &(ext_coms->tas.argc), ext_coms->tas.argv));
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | /*
|
---|
1075 | * -- add a path to the environment
|
---|
1076 | */
|
---|
1077 | int sh_ext_add_default (const char * dummy)
|
---|
1078 | {
|
---|
1079 | (void) dummy;
|
---|
1080 | char * p = NULL;
|
---|
1081 | int i;
|
---|
1082 |
|
---|
1083 | SL_ENTER(_("sh_ext_add_default"));
|
---|
1084 | if (dummy[0] == 'n' || dummy[0] == 'N' ||
|
---|
1085 | dummy[0] == 'f' || dummy[0] == 'F' || dummy[0] == '0')
|
---|
1086 | {
|
---|
1087 | SL_RETURN(0, _("sh_ext_add_default"));
|
---|
1088 | }
|
---|
1089 | p = sh_unix_getUIDdir (SH_ERR_ERR, (uid_t) ext_coms->tas.run_user_uid);
|
---|
1090 | if (p)
|
---|
1091 | (void) sh_ext_add_envv (_("HOME"), p);
|
---|
1092 | (void) sh_ext_add_envv (_("SHELL"), _("/bin/sh"));
|
---|
1093 | (void) sh_ext_add_envv (_("PATH"), _("/sbin:/usr/sbin:/bin:/usr/bin"));
|
---|
1094 | i = (p == NULL ? (-1) : 0);
|
---|
1095 | SL_RETURN(i, _("sh_ext_add_default"));
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /*
|
---|
1099 | * -- add an environment variable
|
---|
1100 | */
|
---|
1101 | int sh_ext_add_environ (const char * str)
|
---|
1102 | {
|
---|
1103 | int i;
|
---|
1104 | SL_ENTER(_("sh_ext_add_environ"));
|
---|
1105 | i = sh_ext_add_envv (NULL, str);
|
---|
1106 | SL_RETURN(i, _("sh_ext_add_environ"));
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /*
|
---|
1110 | * -- set deadtime
|
---|
1111 | */
|
---|
1112 | int sh_ext_deadtime (const char * str)
|
---|
1113 | {
|
---|
1114 | long deadtime = 0;
|
---|
1115 | char * tail = NULL;
|
---|
1116 |
|
---|
1117 | SL_ENTER(_("sh_ext_deadtime"));
|
---|
1118 |
|
---|
1119 | if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
|
---|
1120 | {
|
---|
1121 | SL_RETURN (-1, ("sh_ext_deadtime"));
|
---|
1122 | }
|
---|
1123 | deadtime = strtol(str, &tail, 10);
|
---|
1124 | if (tail == str || deadtime < 0 || deadtime == LONG_MAX)
|
---|
1125 | {
|
---|
1126 | SL_RETURN (-1, ("sh_ext_deadtime"));
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | ext_coms->deadtime = (time_t) deadtime;
|
---|
1130 | SL_RETURN (0, ("sh_ext_deadtime"));
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /*
|
---|
1134 | * -- define type
|
---|
1135 | */
|
---|
1136 | int sh_ext_type (const char * str)
|
---|
1137 | {
|
---|
1138 | SL_ENTER(_("sh_ext_type"));
|
---|
1139 |
|
---|
1140 | if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
|
---|
1141 | {
|
---|
1142 | SL_RETURN((-1), _("sh_ext_type"));
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | if (strlen(str) != 3)
|
---|
1146 | {
|
---|
1147 | SL_RETURN((-1), _("sh_ext_type"));
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | set3(ext_coms->type, str[0], str[1], str[2]);
|
---|
1151 |
|
---|
1152 | if (str[0] == 'l' && str[1] == 'o' && str[2] == 'g')
|
---|
1153 | ext_coms->tas.rw = 'w';
|
---|
1154 | else if (str[0] == 's' && str[1] == 'r' && str[2] == 'v')
|
---|
1155 | ext_coms->tas.rw = 'w';
|
---|
1156 | else if (str[0] == 'm' && str[1] == 'o' && str[2] == 'n')
|
---|
1157 | ext_coms->tas.rw = 'r';
|
---|
1158 | else
|
---|
1159 | {
|
---|
1160 | SL_RETURN((-1), _("sh_ext_type"));
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | SL_RETURN(0, _("sh_ext_type"));
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | /*
|
---|
1169 | * -- define checksum
|
---|
1170 | */
|
---|
1171 | int sh_ext_checksum (const char * str)
|
---|
1172 | {
|
---|
1173 | SL_ENTER(_("sh_ext_checksum"));
|
---|
1174 | if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
|
---|
1175 | {
|
---|
1176 | SL_RETURN((-1), _("sh_ext_checksum"));
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | if (sl_strlen(str) != KEY_LEN)
|
---|
1180 | {
|
---|
1181 | SL_RETURN((-1), _("sh_ext_checksum"));
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | (void) sl_strlcpy (ext_coms->tas.checksum, str, KEY_LEN+1);
|
---|
1185 |
|
---|
1186 | SL_RETURN((0), _("sh_ext_checksum"));
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * -- choose privileges
|
---|
1191 | */
|
---|
1192 | int sh_ext_priv (const char * c)
|
---|
1193 | {
|
---|
1194 |
|
---|
1195 | uid_t me_uid;
|
---|
1196 | gid_t me_gid;
|
---|
1197 |
|
---|
1198 | SL_ENTER(_("sh_ext_priv"));
|
---|
1199 | if (0 == sh_ext_uid (c, &me_uid, &me_gid))
|
---|
1200 | {
|
---|
1201 | ext_coms->tas.run_user_uid = me_uid;
|
---|
1202 | ext_coms->tas.run_user_gid = me_gid;
|
---|
1203 | if (me_uid != (uid_t) 0)
|
---|
1204 | ext_coms->tas.privileged = 0;
|
---|
1205 | SL_RETURN((0), _("sh_ext_priv"));
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | SL_RETURN (-1, _("sh_ext_priv"));
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 |
|
---|
1212 |
|
---|
1213 |
|
---|
1214 | /*
|
---|
1215 | * -- check filters
|
---|
1216 | */
|
---|
1217 | static int sh_ext_filter (char * message, sh_com_t * task)
|
---|
1218 | {
|
---|
1219 | int i;
|
---|
1220 | int j = 0;
|
---|
1221 | time_t now_time;
|
---|
1222 |
|
---|
1223 | SL_ENTER(_("sh_ext_filter"));
|
---|
1224 |
|
---|
1225 | /* Presence of any of these keywords prevents execution.
|
---|
1226 | */
|
---|
1227 | if (task->fnot_c > 0)
|
---|
1228 | {
|
---|
1229 | for (i = 0; i < task->fnot_c; ++i)
|
---|
1230 | {
|
---|
1231 | if (NULL != sl_strstr(message, task->fnot_v[i]))
|
---|
1232 | {
|
---|
1233 | SL_RETURN ((-1), _("sh_ext_filter"));
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | /* Presence of all of these keywords is required for execution.
|
---|
1239 | */
|
---|
1240 | if (task->fand_c > 0)
|
---|
1241 | {
|
---|
1242 | j = 0;
|
---|
1243 |
|
---|
1244 | for (i = 0; i < task->fand_c; ++i)
|
---|
1245 | if (NULL != sl_strstr(message, task->fand_v[i]))
|
---|
1246 | ++j;
|
---|
1247 |
|
---|
1248 | if (j != task->fand_c)
|
---|
1249 | {
|
---|
1250 | SL_RETURN ((-1), _("sh_ext_filter"));
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /* Presence of at least one of these keywords is required for execution.
|
---|
1256 | */
|
---|
1257 | if (task->for_c > 0)
|
---|
1258 | {
|
---|
1259 | for (i = 0; i < task->for_c; ++i)
|
---|
1260 | {
|
---|
1261 | if (NULL != sl_strstr(message, task->for_v[i]))
|
---|
1262 | {
|
---|
1263 | goto checkdeadtime;
|
---|
1264 | }
|
---|
1265 | }
|
---|
1266 | SL_RETURN ((-1), _("sh_ext_filter"));
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | checkdeadtime:
|
---|
1270 | if (task->deadtime != (time_t) 0) /* deadtime */
|
---|
1271 | {
|
---|
1272 | now_time = time (NULL);
|
---|
1273 |
|
---|
1274 | if (task->last_run == (time_t) 0)
|
---|
1275 | {
|
---|
1276 | task->last_run = now_time;
|
---|
1277 | }
|
---|
1278 | else if ((time_t)(now_time-task->last_run) < task->deadtime)
|
---|
1279 | {
|
---|
1280 | SL_RETURN ((-1), _("sh_ext_filter"));
|
---|
1281 | }
|
---|
1282 | else
|
---|
1283 | {
|
---|
1284 | task->last_run = now_time;
|
---|
1285 | }
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | SL_RETURN ((0), _("sh_ext_filter"));
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 |
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * -- execute external script/program
|
---|
1295 | */
|
---|
1296 | int sh_ext_execute (char t1, char t2, char t3, /*@null@*/char * message,
|
---|
1297 | size_t msg_siz)
|
---|
1298 | {
|
---|
1299 | int caperr;
|
---|
1300 | sh_com_t * listval = ext_coms;
|
---|
1301 | int status = 0;
|
---|
1302 | char * tmp;
|
---|
1303 |
|
---|
1304 | static int some_error = 0;
|
---|
1305 |
|
---|
1306 | struct sigaction new_act;
|
---|
1307 | struct sigaction old_act;
|
---|
1308 |
|
---|
1309 | SL_ENTER(_("sh_ext_execute"));
|
---|
1310 |
|
---|
1311 | PDBG_OPEN;
|
---|
1312 |
|
---|
1313 | if (listval == NULL || message == NULL)
|
---|
1314 | {
|
---|
1315 | SL_RETURN ((-1), _("sh_ext_execute"));
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | PDBG(-1);
|
---|
1319 |
|
---|
1320 | if (msg_siz == 0)
|
---|
1321 | msg_siz = sl_strlen(message);
|
---|
1322 |
|
---|
1323 |
|
---|
1324 | /* ignore SIGPIPE (instead get EPIPE if connection is closed)
|
---|
1325 | */
|
---|
1326 | new_act.sa_handler = SIG_IGN;
|
---|
1327 | (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
|
---|
1328 |
|
---|
1329 | while (listval != NULL)
|
---|
1330 | {
|
---|
1331 | PDBG(-2);
|
---|
1332 | if (t1 == listval->type[0] &&
|
---|
1333 | t2 == listval->type[1] &&
|
---|
1334 | t3 == listval->type[2] &&
|
---|
1335 | 0 == sh_ext_filter (message, listval))
|
---|
1336 | {
|
---|
1337 | PDBG(-3);
|
---|
1338 |
|
---|
1339 | if (0 != (caperr = sl_get_cap_sub()))
|
---|
1340 | {
|
---|
1341 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
1342 | sh_error_message (caperr),
|
---|
1343 | _("sl_get_cap_sub"));
|
---|
1344 | }
|
---|
1345 | if (0 == sh_ext_popen (&(listval->tas)))
|
---|
1346 | {
|
---|
1347 | PDBG_OPEN;
|
---|
1348 | PDBG(-4);
|
---|
1349 | if (NULL != listval->tas.pipe && listval->tas.rw == 'w')
|
---|
1350 | {
|
---|
1351 | PDBG(-5);
|
---|
1352 | if (message != NULL)
|
---|
1353 | {
|
---|
1354 | PDBG(-6);
|
---|
1355 | status = (int) write (listval->tas.pipeFD,
|
---|
1356 | message, msg_siz);
|
---|
1357 | if (status >= 0)
|
---|
1358 | status = (int) write (listval->tas.pipeFD, "\n", 1);
|
---|
1359 | }
|
---|
1360 | PDBG_D(status);
|
---|
1361 | if (status >= 0)
|
---|
1362 | status = (int) write (listval->tas.pipeFD, "[", 1);
|
---|
1363 | PDBG_D(status);
|
---|
1364 | if (status >= 0)
|
---|
1365 | status = (int) write (listval->tas.pipeFD, "E", 1);
|
---|
1366 | PDBG_D(status);
|
---|
1367 | if (status >= 0)
|
---|
1368 | status = (int) write (listval->tas.pipeFD, "O", 1);
|
---|
1369 | PDBG_D(status);
|
---|
1370 | if (status >= 0)
|
---|
1371 | status = (int) write (listval->tas.pipeFD, "F", 1);
|
---|
1372 | PDBG_D(status);
|
---|
1373 | if (status >= 0)
|
---|
1374 | status = (int) write (listval->tas.pipeFD, "]", 1);
|
---|
1375 | PDBG_D(status);
|
---|
1376 | if (status >= 0)
|
---|
1377 | status = (int) write (listval->tas.pipeFD, "\n", 1);
|
---|
1378 | PDBG_D(status);
|
---|
1379 | if (status >= 0)
|
---|
1380 | {
|
---|
1381 | some_error = 0;
|
---|
1382 | }
|
---|
1383 | if ((status < 0) && (some_error == 0))
|
---|
1384 | {
|
---|
1385 | some_error = 1;
|
---|
1386 | PDBG_S("some error");
|
---|
1387 | PDBG_D(status);
|
---|
1388 | tmp = sh_util_safe_name (listval->tas.command);
|
---|
1389 |
|
---|
1390 | if (tmp)
|
---|
1391 | {
|
---|
1392 | if (listval->tas.privileged == 0 &&
|
---|
1393 | (0 == getuid() || 0 != sl_is_suid()) )
|
---|
1394 | sh_error_handle((-1), FIL__, __LINE__, 0,
|
---|
1395 | MSG_NOEXEC,
|
---|
1396 | (UID_CAST) listval->tas.run_user_uid,
|
---|
1397 | tmp);
|
---|
1398 | else
|
---|
1399 | sh_error_handle((-1), FIL__, __LINE__, 0,
|
---|
1400 | MSG_NOEXEC,
|
---|
1401 | (UID_CAST) getuid(), tmp);
|
---|
1402 |
|
---|
1403 | SH_FREE(tmp);
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | }
|
---|
1407 | PDBG(-7);
|
---|
1408 | (void) fflush(listval->tas.pipe);
|
---|
1409 | }
|
---|
1410 | PDBG(-8);
|
---|
1411 | (void) sh_ext_pclose(&(listval->tas));
|
---|
1412 | }
|
---|
1413 | else
|
---|
1414 | {
|
---|
1415 | PDBG_OPEN;
|
---|
1416 | PDBG_S("0 != sh_ext_popen()");
|
---|
1417 | }
|
---|
1418 | if (0 != (caperr = sl_drop_cap_sub()))
|
---|
1419 | {
|
---|
1420 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
1421 | sh_error_message (caperr),
|
---|
1422 | _("sl_drop_cap_sub"));
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | }
|
---|
1426 | listval = listval->next;
|
---|
1427 | }
|
---|
1428 | PDBG_OPEN;
|
---|
1429 | PDBG_S("no more commands");
|
---|
1430 |
|
---|
1431 | /* restore old signal handler
|
---|
1432 | */
|
---|
1433 | (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
|
---|
1434 | PDBG_S("return");
|
---|
1435 | PDBG_CLOSE;
|
---|
1436 |
|
---|
1437 | SL_RETURN ((0), _("sh_ext_execute"));
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 |
|
---|
1441 | /* #if defined(WITH_EXTERNAL) */
|
---|
1442 | #endif
|
---|