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