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