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