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