[1] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 1999 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 | #include "config_xor.h"
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | #include <stdio.h>
|
---|
| 24 | #include <stdlib.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 | #include <ctype.h>
|
---|
[98] | 27 | #ifdef HAVE_LINUX_FS_H
|
---|
| 28 | #include <linux/fs.h>
|
---|
| 29 | #endif
|
---|
[1] | 30 |
|
---|
| 31 | #ifdef HAVE_MEMORY_H
|
---|
| 32 | #include <memory.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #ifdef HAVE_UNISTD_H
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <signal.h>
|
---|
| 38 | #include <pwd.h>
|
---|
| 39 | #include <grp.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include <sys/stat.h>
|
---|
| 42 | #include <sys/resource.h>
|
---|
| 43 | #include <fcntl.h>
|
---|
| 44 | #include <unistd.h>
|
---|
[169] | 45 | /* need to undef these, since the #define's may be picked up from
|
---|
| 46 | * linux/wait.h, and will clash with a typedef in sys/wait.h
|
---|
| 47 | */
|
---|
| 48 | #undef P_ALL
|
---|
| 49 | #undef P_PID
|
---|
| 50 | #undef P_PGID
|
---|
[1] | 51 | #include <sys/wait.h>
|
---|
| 52 |
|
---|
| 53 | /*********************
|
---|
| 54 | #ifdef HAVE_SYS_VFS_H
|
---|
| 55 | #include <sys/vfs.h>
|
---|
| 56 | #endif
|
---|
| 57 | **********************/
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
| 60 | #if TIME_WITH_SYS_TIME
|
---|
| 61 | #include <sys/time.h>
|
---|
| 62 | #include <time.h>
|
---|
| 63 | #else
|
---|
| 64 | #if HAVE_SYS_TIME_H
|
---|
| 65 | #include <sys/time.h>
|
---|
| 66 | #else
|
---|
| 67 | #include <time.h>
|
---|
| 68 | #endif
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | #ifdef HAVE_SYS_SELECT_H
|
---|
| 72 | #include <sys/select.h>
|
---|
| 73 | #endif
|
---|
| 74 |
|
---|
| 75 | #ifndef FD_SET
|
---|
| 76 | #define NFDBITS 32
|
---|
| 77 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
|
---|
| 78 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
|
---|
| 79 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
|
---|
| 80 | #endif /* !FD_SET */
|
---|
| 81 | #ifndef FD_SETSIZE
|
---|
| 82 | #define FD_SETSIZE 32
|
---|
| 83 | #endif
|
---|
| 84 | #ifndef FD_ZERO
|
---|
| 85 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
| 90 | #include <sys/mman.h>
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
| 93 | #include "samhain.h"
|
---|
| 94 | #include "sh_error.h"
|
---|
| 95 | #include "sh_unix.h"
|
---|
| 96 | #include "sh_utils.h"
|
---|
| 97 | #include "sh_mem.h"
|
---|
| 98 | #include "sh_hash.h"
|
---|
| 99 | #include "sh_tools.h"
|
---|
[310] | 100 | #include "sh_restrict.h"
|
---|
[295] | 101 | #include "sh_ipvx.h"
|
---|
[1] | 102 | #include "sh_tiger.h"
|
---|
| 103 | #include "sh_prelink.h"
|
---|
[137] | 104 | #include "sh_pthread.h"
|
---|
[481] | 105 | #include "sh_sem.h"
|
---|
[1] | 106 |
|
---|
| 107 | /* moved here from far below
|
---|
| 108 | */
|
---|
| 109 | #include <netdb.h>
|
---|
| 110 |
|
---|
| 111 | #define SH_NEED_PWD_GRP
|
---|
| 112 | #define SH_NEED_GETHOSTBYXXX
|
---|
| 113 | #include "sh_static.h"
|
---|
| 114 |
|
---|
| 115 | #ifndef HAVE_LSTAT
|
---|
| 116 | #define lstat stat
|
---|
| 117 | #endif
|
---|
| 118 |
|
---|
| 119 | #if defined(S_IFLNK) && !defined(S_ISLNK)
|
---|
| 120 | #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
---|
| 121 | #else
|
---|
| 122 | #if !defined(S_ISLNK)
|
---|
| 123 | #define S_ISLNK(mode) (0)
|
---|
| 124 | #endif
|
---|
| 125 | #endif
|
---|
| 126 |
|
---|
| 127 | #if defined(S_IFSOCK) && !defined(S_ISSOCK)
|
---|
| 128 | #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
|
---|
| 129 | #else
|
---|
| 130 | #if !defined(S_ISSOCK)
|
---|
| 131 | #define S_ISSOCK(mode) (0)
|
---|
| 132 | #endif
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
[40] | 135 | #if defined(S_IFDOOR) && !defined(S_ISDOOR)
|
---|
| 136 | #define S_ISDOOR(mode) (((mode) & S_IFMT) == S_IFDOOR)
|
---|
| 137 | #else
|
---|
| 138 | #if !defined(S_ISDOOR)
|
---|
| 139 | #define S_ISDOOR(mode) (0)
|
---|
| 140 | #endif
|
---|
| 141 | #endif
|
---|
| 142 |
|
---|
| 143 | #if defined(S_IFPORT) && !defined(S_ISPORT)
|
---|
| 144 | #define S_ISPORT(mode) (((mode) & S_IFMT) == S_IFPORT)
|
---|
| 145 | #else
|
---|
| 146 | #if !defined(S_ISPORT)
|
---|
| 147 | #define S_ISPORT(mode) (0)
|
---|
| 148 | #endif
|
---|
| 149 | #endif
|
---|
| 150 |
|
---|
[105] | 151 | #define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
|
---|
[40] | 152 |
|
---|
[1] | 153 | #undef FIL__
|
---|
| 154 | #define FIL__ _("sh_unix.c")
|
---|
| 155 |
|
---|
| 156 | unsigned long mask_PRELINK = MASK_PRELINK_;
|
---|
| 157 | unsigned long mask_USER0 = MASK_USER_;
|
---|
| 158 | unsigned long mask_USER1 = MASK_USER_;
|
---|
[27] | 159 | unsigned long mask_USER2 = MASK_USER_;
|
---|
| 160 | unsigned long mask_USER3 = MASK_USER_;
|
---|
| 161 | unsigned long mask_USER4 = MASK_USER_;
|
---|
[1] | 162 | unsigned long mask_ALLIGNORE = MASK_ALLIGNORE_;
|
---|
| 163 | unsigned long mask_ATTRIBUTES = MASK_ATTRIBUTES_;
|
---|
| 164 | unsigned long mask_LOGFILES = MASK_LOGFILES_;
|
---|
| 165 | unsigned long mask_LOGGROW = MASK_LOGGROW_;
|
---|
| 166 | unsigned long mask_READONLY = MASK_READONLY_;
|
---|
| 167 | unsigned long mask_NOIGNORE = MASK_NOIGNORE_;
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | extern char **environ;
|
---|
| 171 |
|
---|
| 172 | int sh_unix_maskreset()
|
---|
| 173 | {
|
---|
| 174 | mask_PRELINK = MASK_PRELINK_;
|
---|
| 175 | mask_USER0 = MASK_USER_;
|
---|
| 176 | mask_USER1 = MASK_USER_;
|
---|
[27] | 177 | mask_USER2 = MASK_USER_;
|
---|
| 178 | mask_USER3 = MASK_USER_;
|
---|
| 179 | mask_USER4 = MASK_USER_;
|
---|
[1] | 180 | mask_ALLIGNORE = MASK_ALLIGNORE_;
|
---|
| 181 | mask_ATTRIBUTES = MASK_ATTRIBUTES_;
|
---|
| 182 | mask_LOGFILES = MASK_LOGFILES_;
|
---|
| 183 | mask_LOGGROW = MASK_LOGGROW_;
|
---|
| 184 | mask_READONLY = MASK_READONLY_;
|
---|
| 185 | mask_NOIGNORE = MASK_NOIGNORE_;
|
---|
| 186 | return 0;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | #ifdef SYS_SIGLIST_DECLARED
|
---|
| 191 | /* extern const char * const sys_siglist[]; */
|
---|
| 192 | #else
|
---|
| 193 | char * sh_unix_siglist (int signum)
|
---|
| 194 | {
|
---|
| 195 | switch (signum)
|
---|
| 196 | {
|
---|
| 197 | #ifdef SIGHUP
|
---|
| 198 | case SIGHUP:
|
---|
| 199 | return _("Hangup");
|
---|
| 200 | #endif
|
---|
| 201 | #ifdef SIGINT
|
---|
| 202 | case SIGINT:
|
---|
| 203 | return _("Interrupt");
|
---|
| 204 | #endif
|
---|
| 205 | #ifdef SIGQUIT
|
---|
| 206 | case SIGQUIT:
|
---|
| 207 | return _("Quit");
|
---|
| 208 | #endif
|
---|
| 209 | #ifdef SIGILL
|
---|
| 210 | case SIGILL:
|
---|
| 211 | return _("Illegal instruction");
|
---|
| 212 | #endif
|
---|
| 213 | #ifdef SIGTRAP
|
---|
| 214 | case SIGTRAP:
|
---|
| 215 | return _("Trace/breakpoint trap");
|
---|
| 216 | #endif
|
---|
| 217 | #ifdef SIGABRT
|
---|
| 218 | case SIGABRT:
|
---|
| 219 | return _("IOT trap/Abort");
|
---|
| 220 | #endif
|
---|
| 221 | #ifdef SIGBUS
|
---|
| 222 | case SIGBUS:
|
---|
| 223 | return _("Bus error");
|
---|
| 224 | #endif
|
---|
| 225 | #ifdef SIGFPE
|
---|
| 226 | case SIGFPE:
|
---|
| 227 | return _("Floating point exception");
|
---|
| 228 | #endif
|
---|
| 229 | #ifdef SIGUSR1
|
---|
| 230 | case SIGUSR1:
|
---|
| 231 | return _("User defined signal 1");
|
---|
| 232 | #endif
|
---|
| 233 | #ifdef SIGSEGV
|
---|
| 234 | case SIGSEGV:
|
---|
| 235 | return _("Segmentation fault");
|
---|
| 236 | #endif
|
---|
| 237 | #ifdef SIGUSR2
|
---|
| 238 | case SIGUSR2:
|
---|
| 239 | return _("User defined signal 2");
|
---|
| 240 | #endif
|
---|
| 241 | #ifdef SIGPIPE
|
---|
| 242 | case SIGPIPE:
|
---|
| 243 | return _("Broken pipe");
|
---|
| 244 | #endif
|
---|
| 245 | #ifdef SIGALRM
|
---|
| 246 | case SIGALRM:
|
---|
| 247 | return _("Alarm clock");
|
---|
| 248 | #endif
|
---|
| 249 | #ifdef SIGTERM
|
---|
| 250 | case SIGTERM:
|
---|
| 251 | return _("Terminated");
|
---|
| 252 | #endif
|
---|
| 253 | #ifdef SIGSTKFLT
|
---|
| 254 | case SIGSTKFLT:
|
---|
| 255 | return _("Stack fault");
|
---|
| 256 | #endif
|
---|
| 257 | #ifdef SIGCHLD
|
---|
| 258 | case SIGCHLD:
|
---|
| 259 | return _("Child exited");
|
---|
| 260 | #endif
|
---|
| 261 | #ifdef SIGCONT
|
---|
| 262 | case SIGCONT:
|
---|
| 263 | return _("Continued");
|
---|
| 264 | #endif
|
---|
| 265 | #ifdef SIGSTOP
|
---|
| 266 | case SIGSTOP:
|
---|
| 267 | return _("Stopped");
|
---|
| 268 | #endif
|
---|
| 269 | #ifdef SIGTSTP
|
---|
| 270 | case SIGTSTP:
|
---|
| 271 | return _("Stop typed at tty");
|
---|
| 272 | #endif
|
---|
| 273 | #ifdef SIGTTIN
|
---|
| 274 | case SIGTTIN:
|
---|
| 275 | return _("Stopped (tty input)");
|
---|
| 276 | #endif
|
---|
| 277 | #ifdef SIGTTOU
|
---|
| 278 | case SIGTTOU:
|
---|
| 279 | return _("Stopped (tty output)");
|
---|
| 280 | #endif
|
---|
| 281 | #ifdef SIGURG
|
---|
| 282 | case SIGURG:
|
---|
| 283 | return _("Urgent condition");
|
---|
| 284 | #endif
|
---|
| 285 | #ifdef SIGXCPU
|
---|
| 286 | case SIGXCPU:
|
---|
| 287 | return _("CPU time limit exceeded");
|
---|
| 288 | #endif
|
---|
| 289 | #ifdef SIGXFSZ
|
---|
| 290 | case SIGXFSZ:
|
---|
| 291 | return _("File size limit exceeded");
|
---|
| 292 | #endif
|
---|
| 293 | #ifdef SIGVTALRM
|
---|
| 294 | case SIGVTALRM:
|
---|
| 295 | return _("Virtual time alarm");
|
---|
| 296 | #endif
|
---|
| 297 | #ifdef SIGPROF
|
---|
| 298 | case SIGPROF:
|
---|
| 299 | return _("Profile signal");
|
---|
| 300 | #endif
|
---|
| 301 | #ifdef SIGWINCH
|
---|
| 302 | case SIGWINCH:
|
---|
| 303 | return _("Window size changed");
|
---|
| 304 | #endif
|
---|
| 305 | #ifdef SIGIO
|
---|
| 306 | case SIGIO:
|
---|
| 307 | return _("Possible I/O");
|
---|
| 308 | #endif
|
---|
| 309 | #ifdef SIGPWR
|
---|
| 310 | case SIGPWR:
|
---|
| 311 | return _("Power failure");
|
---|
| 312 | #endif
|
---|
| 313 | #ifdef SIGUNUSED
|
---|
| 314 | case SIGUNUSED:
|
---|
| 315 | return _("Unused signal");
|
---|
| 316 | #endif
|
---|
| 317 | }
|
---|
| 318 | return _("Unknown");
|
---|
| 319 | }
|
---|
| 320 | #endif
|
---|
| 321 |
|
---|
| 322 |
|
---|
| 323 | /* Log from within a signal handler without using any
|
---|
| 324 | * functions that are not async signal safe.
|
---|
| 325 | *
|
---|
| 326 | * This is the safe_itoa helper function.
|
---|
| 327 | */
|
---|
| 328 | char * safe_itoa(int i, char * str, int size)
|
---|
| 329 | {
|
---|
| 330 | unsigned int u;
|
---|
| 331 | int iisneg = 0;
|
---|
| 332 | char *p = &str[size-1];
|
---|
| 333 |
|
---|
| 334 | *p = '\0';
|
---|
| 335 | if (i < 0) {
|
---|
| 336 | iisneg = 1;
|
---|
| 337 | u = ((unsigned int)(-(1+i))) + 1;
|
---|
| 338 | } else {
|
---|
| 339 | u = i;
|
---|
| 340 | }
|
---|
| 341 | do {
|
---|
| 342 | --p;
|
---|
| 343 | *p = '0' + (u % 10);
|
---|
| 344 | u /= 10;
|
---|
[22] | 345 | } while (u && (p != str));
|
---|
| 346 | if ((iisneg == 1) && (p != str)) {
|
---|
[1] | 347 | --p;
|
---|
| 348 | *p = '-';
|
---|
| 349 | }
|
---|
| 350 | return p;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /* Log from within a signal handler without using any
|
---|
| 354 | * functions that are not async signal safe.
|
---|
| 355 | *
|
---|
| 356 | * This is the safe_logger function.
|
---|
| 357 | * Arguments: signal (signal number), method (0=logger, 1=stderr), thepid (pid)
|
---|
| 358 | */
|
---|
| 359 | extern int OnlyStderr;
|
---|
| 360 |
|
---|
[170] | 361 | int safe_logger (int thesignal, int method, char * details)
|
---|
[1] | 362 | {
|
---|
[34] | 363 | unsigned int i = 0;
|
---|
[1] | 364 | int status = -1;
|
---|
| 365 | struct stat buf;
|
---|
| 366 | pid_t newpid;
|
---|
| 367 | char str[128];
|
---|
| 368 | char * p;
|
---|
[22] | 369 |
|
---|
[1] | 370 | char l0[64], l1[64], l2[64], l3[64];
|
---|
[486] | 371 | char a0[32];
|
---|
[1] | 372 | char e0[128];
|
---|
| 373 | char msg[128];
|
---|
[22] | 374 |
|
---|
[1] | 375 | char * locations[] = { NULL, NULL, NULL, NULL, NULL };
|
---|
| 376 | char * envp[] = { NULL, NULL };
|
---|
[486] | 377 | char * argp[] = { NULL, NULL, NULL };
|
---|
[22] | 378 |
|
---|
| 379 | pid_t thepid = getpid();
|
---|
| 380 |
|
---|
[1] | 381 | if ((sh.flag.isdaemon == S_FALSE) || (OnlyStderr == S_TRUE))
|
---|
| 382 | method = 1;
|
---|
[22] | 383 |
|
---|
[1] | 384 | /* seems that solaris cc needs this way of initializing ...
|
---|
| 385 | */
|
---|
| 386 | locations[0] = l0;
|
---|
| 387 | locations[1] = l1;
|
---|
| 388 | locations[2] = l2;
|
---|
| 389 | locations[3] = l3;
|
---|
[22] | 390 |
|
---|
[1] | 391 | envp[0] = e0;
|
---|
| 392 | argp[0] = a0;
|
---|
[22] | 393 |
|
---|
[1] | 394 | sl_strlcpy(msg, _("samhain["), 128);
|
---|
| 395 | p = safe_itoa((int) thepid, str, 128);
|
---|
| 396 | if (p && *p)
|
---|
| 397 | sl_strlcat(msg, p, 128);
|
---|
[170] | 398 | if (thesignal == 0)
|
---|
[1] | 399 | {
|
---|
[22] | 400 | if (details == NULL) {
|
---|
| 401 | sl_strlcat(msg, _("]: out of memory"), 128);
|
---|
| 402 | } else {
|
---|
| 403 | sl_strlcat(msg, _("]: "), 128);
|
---|
| 404 | sl_strlcat(msg, details, 128);
|
---|
| 405 | }
|
---|
[1] | 406 | }
|
---|
| 407 | else
|
---|
| 408 | {
|
---|
| 409 | sl_strlcat(msg, _("]: exit on signal "), 128);
|
---|
[170] | 410 | p = safe_itoa(thesignal, str, 128);
|
---|
[1] | 411 | if (p && *p)
|
---|
| 412 | sl_strlcat(msg, p, 128);
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | if (method == 1) {
|
---|
| 416 | #ifndef STDERR_FILENO
|
---|
| 417 | #define STDERR_FILENO 2
|
---|
| 418 | #endif
|
---|
[153] | 419 | int retval = 0;
|
---|
| 420 | do {
|
---|
| 421 | retval = write(STDERR_FILENO, msg, strlen(msg));
|
---|
| 422 | } while (retval < 0 && errno == EINTR);
|
---|
| 423 | do {
|
---|
| 424 | retval = write(STDERR_FILENO, "\n", 1);
|
---|
| 425 | } while (retval < 0 && errno == EINTR);
|
---|
[1] | 426 | return 0;
|
---|
| 427 | }
|
---|
[22] | 428 |
|
---|
| 429 | sl_strlcpy (l0, _("/usr/bin/logger"), 64);
|
---|
| 430 | sl_strlcpy (l1, _("/usr/sbin/logger"), 64);
|
---|
| 431 | sl_strlcpy (l2, _("/usr/ucb/logger"), 64);
|
---|
| 432 | sl_strlcpy (l3, _("/bin/logger"), 64);
|
---|
| 433 |
|
---|
| 434 | sl_strlcpy (a0, _("logger"), 32);
|
---|
| 435 | sl_strlcpy (e0,
|
---|
| 436 | _("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin"),
|
---|
| 437 | 128);
|
---|
| 438 |
|
---|
[1] | 439 | while (locations[i] != NULL) {
|
---|
| 440 | status = stat(locations[i], &buf);
|
---|
| 441 | if (status == 0)
|
---|
| 442 | break;
|
---|
| 443 | ++i;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | if (locations[i] != NULL) {
|
---|
[486] | 447 | argp[1] = msg;
|
---|
[1] | 448 | newpid = fork();
|
---|
| 449 | if (newpid == 0) {
|
---|
| 450 | execve(locations[i], argp, envp);
|
---|
| 451 | _exit(1);
|
---|
| 452 | }
|
---|
| 453 | else if (newpid > 0) {
|
---|
| 454 | waitpid(newpid, &status, WUNTRACED);
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 | return 0;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[170] | 460 | void safe_fatal (const char * details,
|
---|
| 461 | const char * file, int line)
|
---|
[22] | 462 | {
|
---|
| 463 | char msg[128];
|
---|
| 464 | char str[128];
|
---|
| 465 | char * p;
|
---|
[170] | 466 | int thesignal = 0;
|
---|
[34] | 467 | int method = 0;
|
---|
| 468 |
|
---|
[22] | 469 | p = safe_itoa((int) line, str, 128);
|
---|
| 470 | sl_strlcpy(msg, _("FATAL: "), 128);
|
---|
| 471 | sl_strlcat(msg, file, 128);
|
---|
| 472 | sl_strlcat(msg, ": ", 128);
|
---|
| 473 | if (p && (*p)) {
|
---|
| 474 | sl_strlcat(msg, p , 128);
|
---|
| 475 | sl_strlcat(msg, ": ", 128);
|
---|
| 476 | }
|
---|
| 477 | sl_strlcat(msg, details, 128);
|
---|
[170] | 478 | (void) safe_logger (thesignal, method, msg);
|
---|
[481] | 479 |
|
---|
| 480 | close_ipc ();
|
---|
[205] | 481 | raise(SIGKILL);
|
---|
[22] | 482 | }
|
---|
[1] | 483 |
|
---|
| 484 | extern char sh_sig_msg[64];
|
---|
| 485 |
|
---|
| 486 | volatile int immediate_exit_normal = 0;
|
---|
| 487 |
|
---|
| 488 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 489 | static
|
---|
| 490 | void sh_unix_sigexit (int mysignal, siginfo_t * signal_info, void * signal_add)
|
---|
| 491 | #else
|
---|
| 492 | static
|
---|
| 493 | void sh_unix_sigexit (int mysignal)
|
---|
| 494 | #endif
|
---|
| 495 | {
|
---|
| 496 |
|
---|
| 497 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 498 | if (signal_info != NULL && signal_info->si_code == SI_USER &&
|
---|
| 499 | mysignal != SIGTERM && mysignal != SIGINT)
|
---|
| 500 | {
|
---|
| 501 | return;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | /* avoid compiler warning (unused var)
|
---|
| 505 | */
|
---|
| 506 | (void) signal_add;
|
---|
| 507 | #endif
|
---|
| 508 |
|
---|
| 509 | /*
|
---|
| 510 | * Block re-entry
|
---|
| 511 | */
|
---|
| 512 | if (immediate_exit_normal > 0)
|
---|
| 513 | {
|
---|
| 514 | ++immediate_exit_normal;
|
---|
| 515 | if ((skey != NULL) && (immediate_exit_normal == 2))
|
---|
| 516 | memset (skey, '\0', sizeof(sh_key_t));
|
---|
| 517 | if (immediate_exit_normal == 2)
|
---|
| 518 | {
|
---|
[153] | 519 | int val_return;
|
---|
| 520 |
|
---|
| 521 | do {
|
---|
| 522 | val_return = chdir ("/");
|
---|
| 523 | } while (val_return < 0 && errno == EINTR);
|
---|
| 524 |
|
---|
[481] | 525 | close_ipc ();
|
---|
[22] | 526 | safe_logger (mysignal, 0, NULL);
|
---|
[1] | 527 | }
|
---|
[205] | 528 | raise(SIGKILL);
|
---|
[1] | 529 | }
|
---|
| 530 | else
|
---|
| 531 | {
|
---|
| 532 | immediate_exit_normal = 1;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | #ifdef SYS_SIGLIST_DECLARED
|
---|
| 536 | strncpy (sh_sig_msg, sys_siglist[mysignal], 40);
|
---|
| 537 | #else
|
---|
| 538 | strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
|
---|
| 539 | #endif
|
---|
| 540 | sh_sig_msg[63] = '\0';
|
---|
| 541 |
|
---|
| 542 | ++sig_raised;
|
---|
| 543 | ++sig_urgent;
|
---|
| 544 | sig_termfast = 1;
|
---|
| 545 | return;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | volatile int immediate_exit_fast = 0;
|
---|
| 549 |
|
---|
| 550 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 551 | static
|
---|
| 552 | void sh_unix_sigexit_fast (int mysignal, siginfo_t * signal_info,
|
---|
| 553 | void * signal_add)
|
---|
| 554 | #else
|
---|
| 555 | static
|
---|
| 556 | void sh_unix_sigexit_fast (int mysignal)
|
---|
| 557 | #endif
|
---|
| 558 | {
|
---|
[172] | 559 | #if defined(SL_DEBUG) && (defined(USE_SYSTEM_MALLOC) || !defined(USE_MALLOC_LOCK))
|
---|
[154] | 560 | int retval;
|
---|
| 561 | #endif
|
---|
| 562 |
|
---|
[1] | 563 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 564 | if (signal_info != NULL && signal_info->si_code == SI_USER)
|
---|
| 565 | {
|
---|
| 566 | return;
|
---|
| 567 | }
|
---|
| 568 | #endif
|
---|
| 569 |
|
---|
| 570 | /* avoid compiler warning (unused var)
|
---|
| 571 | */
|
---|
| 572 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 573 | (void) signal_add;
|
---|
| 574 | #endif
|
---|
| 575 |
|
---|
| 576 | /* Check whether the heap is ok; otherwise _exit
|
---|
| 577 | */
|
---|
[172] | 578 | #if !defined(SL_DEBUG) || (!defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK))
|
---|
[1] | 579 | ++immediate_exit_fast;
|
---|
| 580 | if (skey != NULL && immediate_exit_fast < 2)
|
---|
| 581 | memset (skey, '\0', sizeof(sh_key_t));
|
---|
| 582 | if (immediate_exit_fast < 2)
|
---|
[22] | 583 | safe_logger (mysignal, 0, NULL);
|
---|
[205] | 584 | raise(SIGKILL);
|
---|
[1] | 585 | #else
|
---|
| 586 |
|
---|
| 587 | /* debug code
|
---|
| 588 | */
|
---|
| 589 | if (immediate_exit_fast == 1)
|
---|
| 590 | {
|
---|
| 591 | ++immediate_exit_fast;
|
---|
| 592 | if (skey != NULL)
|
---|
| 593 | memset (skey, '\0', sizeof(sh_key_t));
|
---|
| 594 | close_ipc ();
|
---|
[22] | 595 | safe_logger (mysignal, 0, NULL);
|
---|
[154] | 596 | do {
|
---|
| 597 | retval = chdir ("/");
|
---|
| 598 | } while (retval < 0 && errno == EINTR);
|
---|
[1] | 599 | raise(SIGFPE);
|
---|
| 600 | }
|
---|
| 601 | else if (immediate_exit_fast == 2)
|
---|
| 602 | {
|
---|
[154] | 603 | do {
|
---|
| 604 | retval = chdir ("/");
|
---|
| 605 | } while (retval < 0 && errno == EINTR);
|
---|
[1] | 606 | raise(SIGFPE);
|
---|
| 607 | }
|
---|
| 608 | else if (immediate_exit_fast != 0)
|
---|
| 609 | {
|
---|
[205] | 610 | raise(SIGKILL);
|
---|
[1] | 611 | }
|
---|
| 612 |
|
---|
| 613 | ++immediate_exit_fast;
|
---|
| 614 |
|
---|
| 615 | /* The FPE|BUS|SEGV|ILL signals leave the system in an undefined
|
---|
| 616 | * state, thus it is best to exit immediately.
|
---|
| 617 | */
|
---|
| 618 | #ifdef SYS_SIGLIST_DECLARED
|
---|
| 619 | strncpy (sh_sig_msg, sys_siglist[mysignal], 40);
|
---|
| 620 | #else
|
---|
| 621 | strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
|
---|
| 622 | #endif
|
---|
[131] | 623 | sh_sig_msg[63] = '\0';
|
---|
[1] | 624 |
|
---|
| 625 | sl_stack_print();
|
---|
| 626 |
|
---|
| 627 | /* Try to push out an error message.
|
---|
| 628 | */
|
---|
| 629 | sh_error_handle ((-1), FIL__, __LINE__, mysignal, MSG_EXIT_NORMAL,
|
---|
| 630 | sh.prg_name, sh_sig_msg);
|
---|
| 631 |
|
---|
| 632 | if (skey != NULL)
|
---|
| 633 | memset (skey, '\0', sizeof(sh_key_t));
|
---|
| 634 | close_ipc ();
|
---|
| 635 |
|
---|
[154] | 636 | do {
|
---|
| 637 | retval = chdir ("/");
|
---|
| 638 | } while (retval < 0 && errno == EINTR);
|
---|
| 639 |
|
---|
[1] | 640 | raise(SIGFPE);
|
---|
| 641 | #endif
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 |
|
---|
| 645 | static
|
---|
| 646 | void sh_unix_sigaction (int mysignal)
|
---|
| 647 | {
|
---|
| 648 | ++sig_raised;
|
---|
| 649 | #ifdef SIGUSR1
|
---|
| 650 | if (mysignal == SIGUSR1)
|
---|
| 651 | sig_debug_switch = 1;
|
---|
| 652 | #endif
|
---|
| 653 | #ifdef SIGUSR2
|
---|
| 654 | if (mysignal == SIGUSR2)
|
---|
| 655 | {
|
---|
[19] | 656 | ++sig_suspend_switch;
|
---|
[1] | 657 | ++sig_urgent;
|
---|
| 658 | }
|
---|
| 659 | #endif
|
---|
| 660 | #ifdef SIGHUP
|
---|
| 661 | if (mysignal == SIGHUP)
|
---|
| 662 | sig_config_read_again = 1;
|
---|
| 663 | #endif
|
---|
| 664 | #ifdef SIGTTOU
|
---|
[481] | 665 | if (mysignal == SIGTTOU) {
|
---|
| 666 | sig_force_check = 1; sh_sem_trylock(); }
|
---|
[1] | 667 | #endif
|
---|
[488] | 668 | #ifdef SIGTSTP
|
---|
| 669 | if (mysignal == SIGTSTP) {
|
---|
| 670 | sig_force_check = 1; sig_force_silent = 1; sh_sem_trylock(); }
|
---|
| 671 | #endif
|
---|
[279] | 672 | #ifdef SIGTTIN
|
---|
| 673 | if (mysignal == SIGTTIN)
|
---|
| 674 | sig_fresh_trail = 1;
|
---|
| 675 | #endif
|
---|
[1] | 676 | #ifdef SIGABRT
|
---|
| 677 | if (mysignal == SIGABRT)
|
---|
| 678 | sig_fresh_trail = 1;
|
---|
| 679 | #endif
|
---|
| 680 | #ifdef SIGQUIT
|
---|
| 681 | if (mysignal == SIGQUIT)
|
---|
[481] | 682 | sig_terminate = 1;
|
---|
[1] | 683 | #endif
|
---|
| 684 | #ifdef SIGTERM
|
---|
| 685 | if (mysignal == SIGTERM)
|
---|
| 686 | {
|
---|
| 687 | strncpy (sh_sig_msg, _("Terminated"), 40);
|
---|
| 688 | sig_termfast = 1;
|
---|
| 689 | ++sig_urgent;
|
---|
| 690 | }
|
---|
| 691 | #endif
|
---|
| 692 |
|
---|
| 693 | return;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[379] | 696 | void sh_unix_ign_sigpipe()
|
---|
| 697 | {
|
---|
| 698 | struct sigaction ignact;
|
---|
| 699 |
|
---|
| 700 | ignact.sa_handler = SIG_IGN; /* signal action */
|
---|
| 701 | sigemptyset( &ignact.sa_mask ); /* set an empty mask */
|
---|
| 702 | ignact.sa_flags = 0; /* init sa_flags */
|
---|
| 703 |
|
---|
| 704 | #ifdef SIGPIPE
|
---|
| 705 | retry_sigaction(FIL__, __LINE__, SIGPIPE, &ignact, NULL);
|
---|
| 706 | #endif
|
---|
| 707 |
|
---|
| 708 | return;
|
---|
| 709 | }
|
---|
[1] | 710 | static
|
---|
| 711 | void sh_unix_siginstall (int goDaemon)
|
---|
| 712 | {
|
---|
| 713 | struct sigaction act, act_fast, act2, oldact, ignact;
|
---|
| 714 | #if defined (SH_WITH_SERVER)
|
---|
| 715 | (void) goDaemon;
|
---|
| 716 | #endif
|
---|
| 717 |
|
---|
| 718 | SL_ENTER(_("sh_unix_siginstall"));
|
---|
| 719 |
|
---|
| 720 | ignact.sa_handler = SIG_IGN; /* signal action */
|
---|
| 721 | sigemptyset( &ignact.sa_mask ); /* set an empty mask */
|
---|
| 722 | ignact.sa_flags = 0; /* init sa_flags */
|
---|
| 723 |
|
---|
| 724 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 725 | act.sa_sigaction = &sh_unix_sigexit; /* signal action */
|
---|
| 726 | #else
|
---|
| 727 | act.sa_handler = &sh_unix_sigexit; /* signal action */
|
---|
| 728 | #endif
|
---|
| 729 |
|
---|
| 730 | sigfillset ( &act.sa_mask ); /* set a full mask */
|
---|
| 731 |
|
---|
| 732 |
|
---|
| 733 | /* Block all but deadly signals.
|
---|
| 734 | */
|
---|
| 735 | #ifdef SIGILL
|
---|
| 736 | sigdelset ( &act.sa_mask, SIGILL );
|
---|
| 737 | #endif
|
---|
| 738 | #ifndef SL_DEBUG
|
---|
| 739 | #ifdef SIGFPE
|
---|
| 740 | sigdelset ( &act.sa_mask, SIGFPE );
|
---|
| 741 | #endif
|
---|
| 742 | #endif
|
---|
| 743 | #ifdef SIGSEGV
|
---|
| 744 | sigdelset ( &act.sa_mask, SIGSEGV );
|
---|
| 745 | #endif
|
---|
| 746 | #ifdef SIGBUS
|
---|
| 747 | sigdelset ( &act.sa_mask, SIGBUS );
|
---|
| 748 | #endif
|
---|
| 749 |
|
---|
| 750 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 751 | act_fast.sa_sigaction = &sh_unix_sigexit_fast; /* signal action */
|
---|
| 752 | #else
|
---|
| 753 | act_fast.sa_handler = &sh_unix_sigexit_fast; /* signal action */
|
---|
| 754 | #endif
|
---|
| 755 |
|
---|
| 756 | sigfillset ( &act_fast.sa_mask ); /* set a full mask */
|
---|
| 757 |
|
---|
| 758 | #ifdef SIGILL
|
---|
| 759 | sigdelset ( &act_fast.sa_mask, SIGILL );
|
---|
| 760 | #endif
|
---|
| 761 | #ifndef SL_DEBUG
|
---|
| 762 | #ifdef SIGFPE
|
---|
| 763 | sigdelset ( &act_fast.sa_mask, SIGFPE );
|
---|
| 764 | #endif
|
---|
| 765 | #endif
|
---|
| 766 | #ifdef SIGSEGV
|
---|
| 767 | sigdelset ( &act_fast.sa_mask, SIGSEGV );
|
---|
| 768 | #endif
|
---|
| 769 | #ifdef SIGBUS
|
---|
| 770 | sigdelset ( &act_fast.sa_mask, SIGBUS );
|
---|
| 771 | #endif
|
---|
| 772 |
|
---|
| 773 |
|
---|
| 774 | /* Use siginfo to verify origin of signal, if possible.
|
---|
| 775 | */
|
---|
| 776 | #if defined(SA_SIGACTION_WORKS)
|
---|
| 777 | act.sa_flags = SA_SIGINFO;
|
---|
| 778 | act_fast.sa_flags = SA_SIGINFO;
|
---|
| 779 | #else
|
---|
| 780 | act.sa_flags = 0;
|
---|
| 781 | act_fast.sa_flags = 0;
|
---|
| 782 | #endif
|
---|
| 783 |
|
---|
| 784 | /* Do not block the signal from being received in its handler ...
|
---|
| 785 | * (is this a good or a bad idea ??).
|
---|
| 786 | */
|
---|
| 787 | #if defined(SA_NOMASK)
|
---|
| 788 | act_fast.sa_flags |= SA_NOMASK;
|
---|
| 789 | #elif defined(SA_NODEFER)
|
---|
| 790 | act_fast.sa_flags |= SA_NODEFER;
|
---|
| 791 | #endif
|
---|
| 792 |
|
---|
| 793 |
|
---|
| 794 | act2.sa_handler = &sh_unix_sigaction; /* signal action */
|
---|
| 795 | sigemptyset( &act2.sa_mask ); /* set an empty mask */
|
---|
| 796 | act2.sa_flags = 0; /* init sa_flags */
|
---|
| 797 |
|
---|
| 798 | /* signals to control the daemon */
|
---|
| 799 |
|
---|
| 800 | #ifdef SIGHUP
|
---|
| 801 | retry_sigaction(FIL__, __LINE__, SIGHUP, &act2, &oldact);
|
---|
| 802 | #endif
|
---|
| 803 | #ifdef SIGABRT
|
---|
| 804 | retry_sigaction(FIL__, __LINE__, SIGABRT, &act2, &oldact);
|
---|
| 805 | #endif
|
---|
| 806 | #ifdef SIGUSR1
|
---|
| 807 | retry_sigaction(FIL__, __LINE__, SIGUSR1, &act2, &oldact);
|
---|
| 808 | #endif
|
---|
| 809 | #ifdef SIGUSR2
|
---|
| 810 | retry_sigaction(FIL__, __LINE__, SIGUSR2, &act2, &oldact);
|
---|
| 811 | #endif
|
---|
| 812 | #ifdef SIGQUIT
|
---|
| 813 | retry_sigaction(FIL__, __LINE__, SIGQUIT, &act2, &oldact);
|
---|
| 814 | #endif
|
---|
| 815 | #ifdef SIGTERM
|
---|
[131] | 816 | retry_sigaction(FIL__, __LINE__, SIGTERM, &act, &oldact);
|
---|
[1] | 817 | #endif
|
---|
| 818 |
|
---|
| 819 | /* fatal signals that may cause termination */
|
---|
| 820 |
|
---|
| 821 | #ifdef SIGILL
|
---|
| 822 | retry_sigaction(FIL__, __LINE__, SIGILL, &act_fast, &oldact);
|
---|
| 823 | #endif
|
---|
| 824 | #ifndef SL_DEBUG
|
---|
| 825 | #ifdef SIGFPE
|
---|
| 826 | retry_sigaction(FIL__, __LINE__, SIGFPE, &act_fast, &oldact);
|
---|
| 827 | #endif
|
---|
| 828 | #endif
|
---|
| 829 | #ifdef SIGSEGV
|
---|
| 830 | retry_sigaction(FIL__, __LINE__, SIGSEGV, &act_fast, &oldact);
|
---|
| 831 | #endif
|
---|
| 832 | #ifdef SIGBUS
|
---|
| 833 | retry_sigaction(FIL__, __LINE__, SIGBUS, &act_fast, &oldact);
|
---|
| 834 | #endif
|
---|
| 835 |
|
---|
| 836 | /* other signals */
|
---|
| 837 |
|
---|
| 838 | #ifdef SIGINT
|
---|
| 839 | retry_sigaction(FIL__, __LINE__, SIGINT, &act, &oldact);
|
---|
| 840 | #endif
|
---|
| 841 | #ifdef SIGPIPE
|
---|
[132] | 842 | retry_sigaction(FIL__, __LINE__, SIGPIPE, &ignact, &oldact);
|
---|
[1] | 843 | #endif
|
---|
| 844 | #ifdef SIGALRM
|
---|
| 845 | retry_sigaction(FIL__, __LINE__, SIGALRM, &ignact, &oldact);
|
---|
| 846 | #endif
|
---|
[279] | 847 |
|
---|
[1] | 848 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
| 849 | #ifdef SIGTTOU
|
---|
| 850 | if (goDaemon == 1)
|
---|
| 851 | retry_sigaction(FIL__, __LINE__, SIGTTOU, &act2, &oldact);
|
---|
| 852 | else
|
---|
| 853 | retry_sigaction(FIL__, __LINE__, SIGTTOU, &ignact, &oldact);
|
---|
| 854 | #endif
|
---|
[488] | 855 | #ifdef SIGTSTP
|
---|
| 856 | if (goDaemon == 1)
|
---|
| 857 | retry_sigaction(FIL__, __LINE__, SIGTSTP, &act2, &oldact);
|
---|
| 858 | else
|
---|
| 859 | retry_sigaction(FIL__, __LINE__, SIGTSTP, &ignact, &oldact);
|
---|
| 860 | #endif
|
---|
[279] | 861 | #ifdef SIGTTIN
|
---|
| 862 | if (goDaemon == 1)
|
---|
| 863 | retry_sigaction(FIL__, __LINE__, SIGTTIN, &act2, &oldact);
|
---|
| 864 | else
|
---|
| 865 | retry_sigaction(FIL__, __LINE__, SIGTTIN, &ignact, &oldact);
|
---|
| 866 | #endif
|
---|
[1] | 867 | #else
|
---|
[488] | 868 | #ifdef SIGTSTP
|
---|
| 869 | retry_sigaction(FIL__, __LINE__, SIGTSTP, &ignact, &oldact);
|
---|
| 870 | #endif
|
---|
[1] | 871 | #ifdef SIGTTOU
|
---|
| 872 | retry_sigaction(FIL__, __LINE__, SIGTTOU, &ignact, &oldact);
|
---|
| 873 | #endif
|
---|
[279] | 874 | #ifdef SIGTTIN
|
---|
| 875 | retry_sigaction(FIL__, __LINE__, SIGTTIN, &ignact, &oldact);
|
---|
[1] | 876 | #endif
|
---|
[279] | 877 | #endif
|
---|
[1] | 878 |
|
---|
| 879 | #ifdef SIGTRAP
|
---|
| 880 | #if !defined(SCREW_IT_UP)
|
---|
| 881 | retry_sigaction(FIL__, __LINE__, SIGTRAP, &act, &oldact);
|
---|
| 882 | #endif
|
---|
| 883 | #endif
|
---|
| 884 |
|
---|
| 885 | #ifdef SIGPOLL
|
---|
| 886 | retry_sigaction(FIL__, __LINE__, SIGPOLL, &ignact, &oldact);
|
---|
| 887 | #endif
|
---|
| 888 | #if defined(SIGPROF) && !defined(SH_PROFILE)
|
---|
| 889 | retry_sigaction(FIL__, __LINE__, SIGPROF, &ignact, &oldact);
|
---|
| 890 | #endif
|
---|
| 891 | #ifdef SIGSYS
|
---|
| 892 | retry_sigaction(FIL__, __LINE__, SIGSYS, &act, &oldact);
|
---|
| 893 | #endif
|
---|
| 894 | #ifdef SIGURG
|
---|
| 895 | retry_sigaction(FIL__, __LINE__, SIGURG, &ignact, &oldact);
|
---|
| 896 | #endif
|
---|
| 897 | #if defined(SIGVTALRM) && !defined(SH_PROFILE)
|
---|
| 898 | retry_sigaction(FIL__, __LINE__, SIGVTALRM, &ignact, &oldact);
|
---|
| 899 | #endif
|
---|
| 900 | #ifdef SIGXCPU
|
---|
| 901 | retry_sigaction(FIL__, __LINE__, SIGXCPU, &act, &oldact);
|
---|
| 902 | #endif
|
---|
| 903 | #ifdef SIGXFSZ
|
---|
| 904 | retry_sigaction(FIL__, __LINE__, SIGXFSZ, &act, &oldact);
|
---|
| 905 | #endif
|
---|
| 906 |
|
---|
| 907 | #ifdef SIGEMT
|
---|
| 908 | retry_sigaction(FIL__, __LINE__, SIGEMT, &ignact, &oldact);
|
---|
| 909 | #endif
|
---|
| 910 | #ifdef SIGSTKFLT
|
---|
| 911 | retry_sigaction(FIL__, __LINE__, SIGSTKFLT, &act, &oldact);
|
---|
| 912 | #endif
|
---|
| 913 | #ifdef SIGIO
|
---|
| 914 | retry_sigaction(FIL__, __LINE__, SIGIO, &ignact, &oldact);
|
---|
| 915 | #endif
|
---|
| 916 | #ifdef SIGPWR
|
---|
| 917 | retry_sigaction(FIL__, __LINE__, SIGPWR, &act, &oldact);
|
---|
| 918 | #endif
|
---|
| 919 |
|
---|
| 920 | #ifdef SIGLOST
|
---|
| 921 | retry_sigaction(FIL__, __LINE__, SIGLOST, &ignact, &oldact);
|
---|
| 922 | #endif
|
---|
| 923 | #ifdef SIGUNUSED
|
---|
| 924 | retry_sigaction(FIL__, __LINE__, SIGUNUSED, &ignact, &oldact);
|
---|
| 925 | #endif
|
---|
| 926 |
|
---|
| 927 | SL_RET0(_("sh_unix_siginstall"));
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | /* ---------------------------------------------------------------- */
|
---|
| 931 |
|
---|
| 932 | /* checksum the own binary
|
---|
| 933 | */
|
---|
[22] | 934 | int sh_unix_self_hash (const char * c)
|
---|
[1] | 935 | {
|
---|
| 936 | char message[512];
|
---|
[133] | 937 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 938 |
|
---|
| 939 | SL_ENTER(_("sh_unix_self_hash"));
|
---|
| 940 |
|
---|
| 941 | if (c == NULL)
|
---|
| 942 | {
|
---|
| 943 | sh.exec.path[0] = '\0';
|
---|
| 944 | SL_RETURN((0), _("sh_unix_self_hash"));
|
---|
| 945 | }
|
---|
| 946 | sl_strlcpy(sh.exec.path, c, SH_PATHBUF);
|
---|
| 947 |
|
---|
| 948 | sl_strlcpy(sh.exec.hash,
|
---|
[160] | 949 | sh_tiger_hash (c, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
|
---|
[1] | 950 | KEY_LEN+1);
|
---|
| 951 | sl_snprintf(message, 512, _("%s has checksum: %s"),
|
---|
| 952 | sh.exec.path, sh.exec.hash);
|
---|
| 953 | message[511] = '\0';
|
---|
| 954 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 955 | message, _("sh_unix_self_hash"));
|
---|
[93] | 956 | if (0 == sl_strcmp(sh.exec.hash, SH_KEY_NULL ))
|
---|
[1] | 957 | {
|
---|
| 958 | dlog(1, FIL__, __LINE__,
|
---|
| 959 | _("Could not checksum my own executable because of the\nfollowing error: %s: %s\n\nPossible reasons include:\n Wrong path in configure file option SamhainPath=/path/to/executable\n No read permission for the effective UID: %d\n"),
|
---|
| 960 | sh.exec.path, sl_get_errmsg(), (int) sl_ret_euid());
|
---|
| 961 | sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_NOACCESS,
|
---|
| 962 | (long) sh.real.uid, c);
|
---|
| 963 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 964 | }
|
---|
| 965 | SL_RETURN((0), _("sh_unix_self_hash"));
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | int sh_unix_self_check ()
|
---|
| 969 | {
|
---|
| 970 | char newhash[KEY_LEN+1];
|
---|
| 971 | char message[512];
|
---|
[133] | 972 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 973 |
|
---|
| 974 | SL_ENTER(_("sh_unix_self_check"));
|
---|
[474] | 975 | if (sh.exec.path[0] == '\0')
|
---|
[1] | 976 | SL_RETURN((0), _("sh_unix_self_check"));
|
---|
| 977 |
|
---|
[133] | 978 | sl_strlcpy(newhash,
|
---|
[160] | 979 | sh_tiger_hash (sh.exec.path, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
|
---|
[133] | 980 | KEY_LEN+1);
|
---|
[487] | 981 | if (0 == sl_strncmp(sh.exec.hash, newhash, KEY_LEN))
|
---|
| 982 | {
|
---|
| 983 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 984 | _("Checksum ok"), _("sh_unix_self_check"));
|
---|
| 985 | SL_RETURN((0), _("sh_unix_self_check"));
|
---|
| 986 | }
|
---|
[497] | 987 |
|
---|
| 988 | if (0 == sl_strncmp(SH_KEY_NULL, newhash, KEY_LEN))
|
---|
| 989 | {
|
---|
| 990 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 991 | _("Could not read samhain executable"), _("sh_unix_self_check"));
|
---|
| 992 | SL_RETURN((0), _("sh_unix_self_check"));
|
---|
| 993 | }
|
---|
[1] | 994 |
|
---|
| 995 | dlog(1, FIL__, __LINE__,
|
---|
| 996 | _("The checksum of the executable: %s has changed since startup (%s -> %s).\n"),
|
---|
| 997 | sh.exec.path, sh.exec.hash, newhash);
|
---|
| 998 |
|
---|
| 999 | sl_snprintf(message, 512,
|
---|
| 1000 | _("The checksum of %s has changed since startup (%s -> %s)"),
|
---|
| 1001 | sh.exec.path, sh.exec.hash, newhash);
|
---|
| 1002 | message[511] = '\0';
|
---|
| 1003 |
|
---|
| 1004 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 1005 | message, _("sh_unix_self_check"));
|
---|
| 1006 | sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_E_AUTH,
|
---|
| 1007 | sh.exec.path);
|
---|
| 1008 | SL_RETURN((-1), _("sh_unix_self_check"));
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 |
|
---|
| 1012 | /* ---------------------------------------------------------------- */
|
---|
| 1013 |
|
---|
[410] | 1014 | long sh_group_to_gid (const char * g, int * fail)
|
---|
| 1015 | {
|
---|
| 1016 | struct group * w;
|
---|
| 1017 | gid_t gid = 0;
|
---|
[428] | 1018 | int status = 0;
|
---|
[1] | 1019 |
|
---|
[410] | 1020 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1021 | struct group grp;
|
---|
| 1022 | char * buffer;
|
---|
[462] | 1023 | static size_t gbufsize = SH_GRBUF_SIZE;
|
---|
[410] | 1024 | #endif
|
---|
| 1025 |
|
---|
| 1026 | *fail = -1;
|
---|
| 1027 |
|
---|
| 1028 | if (g)
|
---|
| 1029 | {
|
---|
| 1030 | size_t i;
|
---|
| 1031 | size_t len = strlen(g);
|
---|
| 1032 |
|
---|
| 1033 | *fail = 0;
|
---|
| 1034 |
|
---|
| 1035 | for (i = 0; i < len; ++i)
|
---|
| 1036 | {
|
---|
| 1037 | char c = g[i];
|
---|
| 1038 |
|
---|
| 1039 | if (!isdigit((int) c))
|
---|
| 1040 | goto is_a_name;
|
---|
| 1041 | }
|
---|
| 1042 | return atol(g);
|
---|
| 1043 |
|
---|
| 1044 | is_a_name:
|
---|
| 1045 |
|
---|
| 1046 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
[462] | 1047 |
|
---|
| 1048 | buffer = SH_ALLOC(gbufsize);
|
---|
| 1049 | status = sh_getgrnam_r(g, &grp, buffer, gbufsize, &w);
|
---|
| 1050 |
|
---|
| 1051 | if ((status == ERANGE) && (w == NULL))
|
---|
| 1052 | {
|
---|
[481] | 1053 | if (S_TRUE == sl_ok_adds( gbufsize, SH_GRBUF_SIZE ))
|
---|
[462] | 1054 | {
|
---|
| 1055 | SH_FREE(buffer);
|
---|
| 1056 | gbufsize += SH_GRBUF_SIZE;
|
---|
| 1057 | goto is_a_name;
|
---|
| 1058 | }
|
---|
| 1059 | }
|
---|
| 1060 |
|
---|
[410] | 1061 | #else
|
---|
[462] | 1062 |
|
---|
[428] | 1063 | errno = 0;
|
---|
[410] | 1064 | w = sh_getgrnam(g);
|
---|
[428] | 1065 | status = errno;
|
---|
[462] | 1066 |
|
---|
[410] | 1067 | #endif
|
---|
| 1068 |
|
---|
[428] | 1069 | if ((status == ERANGE) && (w == NULL))
|
---|
[410] | 1070 | {
|
---|
[428] | 1071 | static int seen = 0;
|
---|
| 1072 |
|
---|
| 1073 | if (seen == 0)
|
---|
| 1074 | {
|
---|
| 1075 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 1076 |
|
---|
| 1077 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
|
---|
| 1078 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 1079 | _("sh_group_to_gid"), (long) -1, _("line too long in group entry"));
|
---|
| 1080 | ++seen;
|
---|
| 1081 | }
|
---|
| 1082 | *fail = -1;
|
---|
| 1083 | }
|
---|
| 1084 | else if (w == NULL)
|
---|
| 1085 | {
|
---|
[410] | 1086 | char * tmp = sh_util_strdup(g);
|
---|
| 1087 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1088 | _("sh_group_to_gid"), tmp);
|
---|
| 1089 | SH_FREE(tmp);
|
---|
| 1090 | *fail = -1;
|
---|
| 1091 | }
|
---|
| 1092 | else
|
---|
| 1093 | {
|
---|
| 1094 | gid = w->gr_gid;
|
---|
| 1095 | }
|
---|
| 1096 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1097 | SH_FREE(buffer);
|
---|
| 1098 | #endif
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | return gid;
|
---|
| 1102 | }
|
---|
| 1103 |
|
---|
| 1104 | /* ---------------------------------------------------------------- */
|
---|
| 1105 |
|
---|
| 1106 |
|
---|
[1] | 1107 | /* added Tue Feb 22 10:36:44 NFT 2000 Rainer Wichmann */
|
---|
[22] | 1108 | static int tf_add_trusted_user_int(const char * c)
|
---|
[1] | 1109 | {
|
---|
[137] | 1110 | struct passwd * w;
|
---|
[1] | 1111 | int count;
|
---|
| 1112 | uid_t pwid = (uid_t)-1;
|
---|
| 1113 |
|
---|
[131] | 1114 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1115 | struct passwd pwd;
|
---|
[227] | 1116 | char * buffer;
|
---|
[131] | 1117 | #endif
|
---|
| 1118 |
|
---|
[1] | 1119 | SL_ENTER(_("tf_add_trusted_user_int"));
|
---|
| 1120 |
|
---|
| 1121 | /* First check for a user name.
|
---|
| 1122 | */
|
---|
[131] | 1123 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
[227] | 1124 | buffer = SH_ALLOC(SH_PWBUF_SIZE);
|
---|
| 1125 | sh_getpwnam_r(c, &pwd, buffer, SH_PWBUF_SIZE, &w);
|
---|
[131] | 1126 | #else
|
---|
| 1127 | w = sh_getpwnam(c);
|
---|
| 1128 | #endif
|
---|
| 1129 |
|
---|
| 1130 | if ((w != NULL) && ((pwid = w->pw_uid) > 0))
|
---|
[1] | 1131 | goto succe;
|
---|
| 1132 |
|
---|
| 1133 | /* Failed, so check for a numerical value.
|
---|
| 1134 | */
|
---|
| 1135 | pwid = strtol(c, (char **)NULL, 10);
|
---|
| 1136 | if (pwid > 0 && pwid < 65535)
|
---|
| 1137 | goto succe;
|
---|
| 1138 |
|
---|
| 1139 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1140 | _("add trusted user"), c);
|
---|
[227] | 1141 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1142 | SH_FREE(buffer);
|
---|
| 1143 | #endif
|
---|
[1] | 1144 | SL_RETURN((-1), _("tf_add_trusted_user_int"));
|
---|
| 1145 |
|
---|
| 1146 | succe:
|
---|
| 1147 | count = sl_trust_add_user(pwid);
|
---|
[227] | 1148 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1149 | SH_FREE(buffer);
|
---|
| 1150 | #endif
|
---|
[1] | 1151 | SL_RETURN((count), _("tf_add_trusted_user_int"));
|
---|
| 1152 | }
|
---|
| 1153 |
|
---|
[22] | 1154 | int tf_add_trusted_user(const char * c)
|
---|
[1] | 1155 | {
|
---|
| 1156 | int i;
|
---|
| 1157 | char * q;
|
---|
| 1158 | char * p = sh_util_strdup (c);
|
---|
[131] | 1159 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
| 1160 | char * saveptr;
|
---|
| 1161 | #endif
|
---|
| 1162 |
|
---|
[1] | 1163 | SL_ENTER(_("tf_add_trusted_user"));
|
---|
| 1164 |
|
---|
[131] | 1165 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
| 1166 | q = strtok_r(p, ", \t", &saveptr);
|
---|
| 1167 | #else
|
---|
[1] | 1168 | q = strtok(p, ", \t");
|
---|
[131] | 1169 | #endif
|
---|
[1] | 1170 | if (!q)
|
---|
| 1171 | {
|
---|
| 1172 | SH_FREE(p);
|
---|
| 1173 | SL_RETURN((-1), _("tf_add_trusted_user"));
|
---|
| 1174 | }
|
---|
| 1175 | while (q)
|
---|
| 1176 | {
|
---|
| 1177 | i = tf_add_trusted_user_int(q);
|
---|
| 1178 | if (SL_ISERROR(i))
|
---|
| 1179 | {
|
---|
| 1180 | SH_FREE(p);
|
---|
| 1181 | SL_RETURN((i), _("tf_add_trusted_user"));
|
---|
| 1182 | }
|
---|
[131] | 1183 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
| 1184 | q = strtok_r(NULL, ", \t", &saveptr);
|
---|
| 1185 | #else
|
---|
[1] | 1186 | q = strtok(NULL, ", \t");
|
---|
[131] | 1187 | #endif
|
---|
[1] | 1188 | }
|
---|
| 1189 | SH_FREE(p);
|
---|
| 1190 | SL_RETURN((0), _("tf_add_trusted_user"));
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
[170] | 1193 | extern uid_t sl_trust_baduid(void);
|
---|
| 1194 | extern gid_t sl_trust_badgid(void);
|
---|
[1] | 1195 |
|
---|
| 1196 | #if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
|
---|
[183] | 1197 | int tf_trust_check (const char * file, int mode)
|
---|
[1] | 1198 | {
|
---|
| 1199 | (void) file;
|
---|
| 1200 | (void) mode;
|
---|
| 1201 | return 0;
|
---|
| 1202 | }
|
---|
| 1203 | #else
|
---|
[183] | 1204 | int tf_trust_check (const char * file, int mode)
|
---|
[1] | 1205 | {
|
---|
| 1206 | char * tmp;
|
---|
| 1207 | char * tmp2;
|
---|
| 1208 | char * p;
|
---|
| 1209 | int status;
|
---|
| 1210 | int level;
|
---|
[383] | 1211 | uid_t ff_euid = (uid_t) -1;
|
---|
[513] | 1212 | uid_t baduid;
|
---|
| 1213 | gid_t badgid;
|
---|
[1] | 1214 |
|
---|
| 1215 | SL_ENTER(_("tf_trust_check"));
|
---|
| 1216 |
|
---|
| 1217 | if (mode == SL_YESPRIV)
|
---|
| 1218 | sl_get_euid(&ff_euid);
|
---|
| 1219 | else
|
---|
| 1220 | sl_get_ruid(&ff_euid);
|
---|
| 1221 |
|
---|
| 1222 | #if defined(SH_WITH_SERVER)
|
---|
| 1223 | if (0 == sl_ret_euid()) /* privileges not dropped yet */
|
---|
| 1224 | {
|
---|
[131] | 1225 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1226 | struct passwd pwd;
|
---|
[227] | 1227 | char * buffer = SH_ALLOC(SH_PWBUF_SIZE);
|
---|
[131] | 1228 | struct passwd * tempres;
|
---|
[227] | 1229 | sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
|
---|
[131] | 1230 | #else
|
---|
[1] | 1231 | struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
|
---|
[131] | 1232 | #endif
|
---|
[1] | 1233 |
|
---|
| 1234 | if (!tempres)
|
---|
| 1235 | {
|
---|
| 1236 | dlog(1, FIL__, __LINE__,
|
---|
| 1237 | _("User %s does not exist. Please add the user to your system.\n"),
|
---|
| 1238 | DEFAULT_IDENT);
|
---|
| 1239 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 1240 | }
|
---|
[383] | 1241 | else
|
---|
| 1242 | {
|
---|
| 1243 | ff_euid = tempres->pw_uid;
|
---|
| 1244 | }
|
---|
[227] | 1245 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
| 1246 | SH_FREE(buffer);
|
---|
| 1247 | #endif
|
---|
[1] | 1248 | }
|
---|
| 1249 | #endif
|
---|
| 1250 |
|
---|
| 1251 | status = sl_trustfile_euid(file, ff_euid);
|
---|
| 1252 |
|
---|
| 1253 | if ( SL_ENONE != status)
|
---|
| 1254 | {
|
---|
| 1255 | if (status == SL_ESTAT)
|
---|
| 1256 | level = SH_ERR_ALL;
|
---|
| 1257 | else
|
---|
| 1258 | level = SH_ERR_ERR;
|
---|
| 1259 |
|
---|
[513] | 1260 | tmp = sh_util_safe_name (file);
|
---|
| 1261 | p = sh_util_strdup(sl_trust_errfile());
|
---|
| 1262 | baduid = sl_trust_baduid();
|
---|
| 1263 | badgid = sl_trust_badgid();
|
---|
| 1264 |
|
---|
[1] | 1265 | if (p && *p != '\0')
|
---|
| 1266 | {
|
---|
[513] | 1267 | tmp2 = sh_util_safe_name (p);
|
---|
[1] | 1268 | sh_error_handle(level, FIL__, __LINE__, status, MSG_E_TRUST2,
|
---|
| 1269 | sl_error_string(status), tmp, tmp2);
|
---|
| 1270 | SH_FREE(tmp2);
|
---|
| 1271 | }
|
---|
| 1272 | else
|
---|
| 1273 | {
|
---|
| 1274 | sh_error_handle(level, FIL__, __LINE__, status, MSG_E_TRUST1,
|
---|
| 1275 | sl_error_string(status), tmp);
|
---|
| 1276 | }
|
---|
| 1277 |
|
---|
| 1278 | if (status == SL_EBADUID || status == SL_EBADGID ||
|
---|
| 1279 | status == SL_EBADOTH || status == SL_ETRUNC ||
|
---|
| 1280 | status == SL_EINTERNAL )
|
---|
| 1281 | {
|
---|
| 1282 | switch (status) {
|
---|
| 1283 | case SL_EINTERNAL:
|
---|
| 1284 | dlog(1, FIL__, __LINE__,
|
---|
| 1285 | _("An internal error occured in the trustfile function.\n"));
|
---|
| 1286 | break;
|
---|
| 1287 | case SL_ETRUNC:
|
---|
| 1288 | dlog(1, FIL__, __LINE__,
|
---|
| 1289 | _("A filename truncation occured in the trustfile function.\nProbably the normalized filename for %s\nis too long. This may be due e.g. to deep or circular softlinks.\n"),
|
---|
| 1290 | tmp);
|
---|
| 1291 | break;
|
---|
| 1292 | case SL_EBADOTH:
|
---|
| 1293 | dlog(1, FIL__, __LINE__,
|
---|
| 1294 | _("The path element: %s\nin the filename: %s is world writeable.\n"),
|
---|
[513] | 1295 | (p) ? p : _("(null)"), tmp);
|
---|
[1] | 1296 | break;
|
---|
| 1297 | case SL_EBADUID:
|
---|
| 1298 | dlog(1, FIL__, __LINE__,
|
---|
| 1299 | _("The owner (UID = %ld) of the path element: %s\nin the filename: %s\nis not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
|
---|
[513] | 1300 | (UID_CAST)baduid, (p) ? p : _("(null)"), tmp);
|
---|
[1] | 1301 | break;
|
---|
| 1302 | case SL_EBADGID:
|
---|
| 1303 | dlog(1, FIL__, __LINE__,
|
---|
| 1304 | _("The path element: %s\nin the filename: %s\nis group writeable (GID = %ld), and at least one of the group\nmembers (UID = %ld) is not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
|
---|
[513] | 1305 | (p) ? p : _("(null)"), tmp, (UID_CAST)badgid,
|
---|
| 1306 | (UID_CAST)baduid);
|
---|
[1] | 1307 | break;
|
---|
| 1308 | default:
|
---|
| 1309 | break;
|
---|
| 1310 | }
|
---|
[513] | 1311 | SH_FREE(tmp);
|
---|
| 1312 | if (p) SH_FREE(p);
|
---|
[1] | 1313 | SL_RETURN((-1), _("tf_trust_check"));
|
---|
| 1314 | }
|
---|
[513] | 1315 | else {
|
---|
| 1316 | SH_FREE(tmp);
|
---|
| 1317 | if (p) SH_FREE(p);
|
---|
| 1318 | }
|
---|
[1] | 1319 | }
|
---|
| 1320 |
|
---|
| 1321 | SL_RETURN((0), _("tf_trust_check"));
|
---|
| 1322 | }
|
---|
| 1323 | #endif
|
---|
| 1324 |
|
---|
| 1325 | #ifdef HAVE_INITGROUPS
|
---|
| 1326 | #ifdef HOST_IS_OSF
|
---|
| 1327 | int sh_unix_initgroups ( char * in_user, gid_t in_gid)
|
---|
| 1328 | #else
|
---|
| 1329 | int sh_unix_initgroups (const char * in_user, gid_t in_gid)
|
---|
| 1330 | #endif
|
---|
| 1331 | {
|
---|
| 1332 | int status = -1;
|
---|
| 1333 | status = sh_initgroups (in_user, in_gid);
|
---|
| 1334 | if (status < 0)
|
---|
| 1335 | {
|
---|
| 1336 | if (errno == EPERM)
|
---|
| 1337 | return 0;
|
---|
| 1338 | if (errno == EINVAL)
|
---|
| 1339 | return 0;
|
---|
| 1340 | return -1;
|
---|
| 1341 | }
|
---|
| 1342 | return 0;
|
---|
| 1343 | }
|
---|
| 1344 | #else
|
---|
| 1345 | int sh_unix_initgroups (const char * in_user, gid_t in_gid)
|
---|
| 1346 | {
|
---|
| 1347 | (void) in_user;
|
---|
| 1348 | (void) in_gid;
|
---|
| 1349 | return 0;
|
---|
| 1350 | }
|
---|
| 1351 | #endif
|
---|
| 1352 |
|
---|
| 1353 | #ifdef HAVE_INITGROUPS
|
---|
[132] | 1354 | char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
|
---|
[1] | 1355 | int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid)
|
---|
| 1356 | {
|
---|
| 1357 | int status = -1;
|
---|
[132] | 1358 | char user[SH_MINIBUF];
|
---|
[1] | 1359 |
|
---|
| 1360 | SL_ENTER(_("sh_unix_initgroups2"));
|
---|
| 1361 |
|
---|
[132] | 1362 | if (NULL == sh_unix_getUIDname (SH_ERR_ERR, in_pid, user, sizeof(user)))
|
---|
[1] | 1363 | SL_RETURN((-1), _("sh_unix_initgroups2"));
|
---|
| 1364 | status = sh_initgroups (user, in_gid);
|
---|
| 1365 | if (status < 0)
|
---|
| 1366 | {
|
---|
| 1367 | if (errno == EPERM)
|
---|
| 1368 | status = 0;
|
---|
| 1369 | if (errno == EINVAL)
|
---|
| 1370 | status = 0;
|
---|
| 1371 | }
|
---|
| 1372 | SL_RETURN((status), _("sh_unix_initgroups2"));
|
---|
| 1373 | }
|
---|
| 1374 | #else
|
---|
| 1375 | int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid)
|
---|
| 1376 | {
|
---|
| 1377 | (void) in_pid;
|
---|
| 1378 | (void) in_gid;
|
---|
| 1379 | return 0;
|
---|
| 1380 | }
|
---|
| 1381 | #endif
|
---|
| 1382 |
|
---|
[174] | 1383 | void sh_unix_closeall (int fd, int except, int inchild)
|
---|
[1] | 1384 | {
|
---|
| 1385 | int fdx = fd;
|
---|
| 1386 | #ifdef _SC_OPEN_MAX
|
---|
| 1387 | int fdlimit = sysconf (_SC_OPEN_MAX);
|
---|
| 1388 | #else
|
---|
| 1389 | #ifdef OPEN_MAX
|
---|
| 1390 | int fdlimit = OPEN_MAX;
|
---|
| 1391 | #else
|
---|
| 1392 | int fdlimit = _POSIX_OPEN_MAX;
|
---|
| 1393 | #endif
|
---|
| 1394 | #endif
|
---|
| 1395 |
|
---|
| 1396 | SL_ENTER(_("sh_unix_closeall"));
|
---|
| 1397 |
|
---|
| 1398 | /* can't happen - so fix it :-(
|
---|
| 1399 | */
|
---|
| 1400 | if (fdlimit < 0)
|
---|
[84] | 1401 | fdlimit = 20; /* POSIX lower limit */
|
---|
[1] | 1402 |
|
---|
| 1403 | if (fdlimit > 65536)
|
---|
| 1404 | fdlimit = 65536;
|
---|
| 1405 |
|
---|
[247] | 1406 | if (!inchild)
|
---|
| 1407 | sl_dropall (fdx, except);
|
---|
| 1408 | else
|
---|
| 1409 | sl_dropall_dirty (fdx, except);
|
---|
| 1410 |
|
---|
[1] | 1411 | /* Close everything from fd (inclusive) up to fdlimit (exclusive).
|
---|
| 1412 | */
|
---|
| 1413 | while (fd < fdlimit)
|
---|
| 1414 | {
|
---|
| 1415 | if (fd == except)
|
---|
| 1416 | fd++;
|
---|
| 1417 | else if (slib_do_trace != 0 && fd == slib_trace_fd)
|
---|
| 1418 | fd++;
|
---|
| 1419 | else
|
---|
[252] | 1420 | sl_close_fd(FIL__, __LINE__, fd++);
|
---|
[1] | 1421 | }
|
---|
| 1422 |
|
---|
| 1423 | SL_RET0(_("sh_unix_closeall"));
|
---|
| 1424 | }
|
---|
| 1425 |
|
---|
| 1426 | static void sh_unix_setlimits(void)
|
---|
| 1427 | {
|
---|
| 1428 | struct rlimit limits;
|
---|
| 1429 |
|
---|
| 1430 | SL_ENTER(_("sh_unix_setlimits"));
|
---|
| 1431 |
|
---|
| 1432 | limits.rlim_cur = RLIM_INFINITY;
|
---|
| 1433 | limits.rlim_max = RLIM_INFINITY;
|
---|
| 1434 |
|
---|
| 1435 | #ifdef RLIMIT_CPU
|
---|
| 1436 | setrlimit (RLIMIT_CPU, &limits);
|
---|
| 1437 | #endif
|
---|
| 1438 | #ifdef RLIMIT_FSIZE
|
---|
| 1439 | setrlimit (RLIMIT_FSIZE, &limits);
|
---|
| 1440 | #endif
|
---|
| 1441 | #ifdef RLIMIT_DATA
|
---|
| 1442 | setrlimit (RLIMIT_DATA, &limits);
|
---|
| 1443 | #endif
|
---|
| 1444 | #ifdef RLIMIT_STACK
|
---|
| 1445 | setrlimit (RLIMIT_STACK, &limits);
|
---|
| 1446 | #endif
|
---|
| 1447 | #ifdef RLIMIT_RSS
|
---|
| 1448 | setrlimit (RLIMIT_RSS, &limits);
|
---|
| 1449 | #endif
|
---|
| 1450 | #ifdef RLIMIT_NPROC
|
---|
| 1451 | setrlimit (RLIMIT_NPROC, &limits);
|
---|
| 1452 | #endif
|
---|
| 1453 | #ifdef RLIMIT_MEMLOCK
|
---|
| 1454 | setrlimit (RLIMIT_MEMLOCK, &limits);
|
---|
| 1455 | #endif
|
---|
| 1456 |
|
---|
| 1457 | #if !defined(SL_DEBUG)
|
---|
| 1458 | /* no core dumps
|
---|
| 1459 | */
|
---|
| 1460 | limits.rlim_cur = 0;
|
---|
| 1461 | limits.rlim_max = 0;
|
---|
| 1462 | #ifdef RLIMIT_CORE
|
---|
| 1463 | setrlimit (RLIMIT_CORE, &limits);
|
---|
| 1464 | #endif
|
---|
| 1465 | #else
|
---|
| 1466 | #ifdef RLIMIT_CORE
|
---|
| 1467 | setrlimit (RLIMIT_CORE, &limits);
|
---|
| 1468 | #endif
|
---|
| 1469 | #endif
|
---|
| 1470 |
|
---|
[8] | 1471 | limits.rlim_cur = 1024;
|
---|
| 1472 | limits.rlim_max = 1024;
|
---|
[1] | 1473 |
|
---|
[8] | 1474 | #if defined(RLIMIT_NOFILE)
|
---|
| 1475 | setrlimit (RLIMIT_NOFILE, &limits);
|
---|
| 1476 | #elif defined(RLIMIT_OFILE)
|
---|
| 1477 | setrlimit (RLIMIT_OFILE, &limits);
|
---|
| 1478 | #endif
|
---|
| 1479 |
|
---|
[1] | 1480 | SL_RET0(_("sh_unix_setlimits"));
|
---|
| 1481 | }
|
---|
| 1482 |
|
---|
| 1483 | static void sh_unix_copyenv(void)
|
---|
| 1484 | {
|
---|
| 1485 | char ** env0 = environ;
|
---|
| 1486 | char ** env1;
|
---|
| 1487 | int envlen = 0;
|
---|
[22] | 1488 | size_t len;
|
---|
[1] | 1489 |
|
---|
| 1490 | SL_ENTER(_("sh_unix_copyenv"));
|
---|
| 1491 |
|
---|
| 1492 | while (env0 != NULL && env0[envlen] != NULL) {
|
---|
| 1493 | /* printf("%2d: %s\n", envlen, env0[envlen]); */
|
---|
| 1494 | ++envlen;
|
---|
| 1495 | }
|
---|
| 1496 | ++envlen;
|
---|
| 1497 |
|
---|
| 1498 | /* printf("-> %2d: slots allocated\n", envlen); */
|
---|
[454] | 1499 | env1 = calloc(1,sizeof(char *) * envlen); /* only once */
|
---|
[1] | 1500 | if (env1 == NULL)
|
---|
| 1501 | {
|
---|
| 1502 | fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
|
---|
| 1503 | SL_RET0(_("sh_unix_copyenv"));
|
---|
| 1504 | }
|
---|
| 1505 | env0 = environ;
|
---|
| 1506 | envlen = 0;
|
---|
| 1507 |
|
---|
[22] | 1508 | while (env0 != NULL && env0[envlen] != NULL) {
|
---|
| 1509 | len = strlen(env0[envlen]) + 1;
|
---|
[454] | 1510 | env1[envlen] = calloc(1,len); /* only once */
|
---|
[1] | 1511 | if (env1[envlen] == NULL)
|
---|
| 1512 | {
|
---|
[473] | 1513 | int i;
|
---|
[1] | 1514 | fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
|
---|
[473] | 1515 | for (i = 0; i < envlen; ++i) free(env1[len]);
|
---|
| 1516 | free(env1);
|
---|
[1] | 1517 | SL_RET0(_("sh_unix_copyenv"));
|
---|
| 1518 | }
|
---|
[22] | 1519 | sl_strlcpy(env1[envlen], env0[envlen], len);
|
---|
[1] | 1520 | ++envlen;
|
---|
| 1521 | }
|
---|
| 1522 | env1[envlen] = NULL;
|
---|
| 1523 |
|
---|
| 1524 | environ = env1;
|
---|
| 1525 | SL_RET0(_("sh_unix_copyenv"));
|
---|
| 1526 | }
|
---|
| 1527 |
|
---|
| 1528 | /* delete all environment variables
|
---|
| 1529 | */
|
---|
| 1530 | static void sh_unix_zeroenv(void)
|
---|
| 1531 | {
|
---|
| 1532 | char * c;
|
---|
| 1533 | char ** env;
|
---|
| 1534 |
|
---|
| 1535 | SL_ENTER(_("sh_unix_zeroenv"));
|
---|
| 1536 |
|
---|
| 1537 | sh_unix_copyenv();
|
---|
| 1538 | env = environ;
|
---|
| 1539 |
|
---|
| 1540 | while (env != NULL && *env != NULL) {
|
---|
| 1541 | c = strchr ((*env), '=');
|
---|
| 1542 | #ifdef WITH_MYSQL
|
---|
| 1543 | /*
|
---|
| 1544 | * Skip the MYSQL_UNIX_PORT environment variable; MySQL may need it.
|
---|
| 1545 | */
|
---|
| 1546 | if (0 == sl_strncmp((*env), _("MYSQL_UNIX_PORT="), 16))
|
---|
| 1547 | {
|
---|
| 1548 | ++(env);
|
---|
| 1549 | continue;
|
---|
| 1550 | }
|
---|
[18] | 1551 | if (0 == sl_strncmp((*env), _("MYSQL_TCP_PORT="), 15))
|
---|
| 1552 | {
|
---|
| 1553 | ++(env);
|
---|
| 1554 | continue;
|
---|
| 1555 | }
|
---|
| 1556 | if (0 == sl_strncmp((*env), _("MYSQL_HOME="), 11))
|
---|
| 1557 | {
|
---|
| 1558 | ++(env);
|
---|
| 1559 | continue;
|
---|
| 1560 | }
|
---|
[1] | 1561 | #endif
|
---|
| 1562 | #ifdef WITH_ORACLE
|
---|
| 1563 | /*
|
---|
[415] | 1564 | * Skip the ORACLE_HOME and TNS_ADMIN environment variables;
|
---|
| 1565 | * Oracle may need them.
|
---|
[1] | 1566 | */
|
---|
| 1567 | if (0 == sl_strncmp((*env), _("ORACLE_HOME="), 12))
|
---|
| 1568 | {
|
---|
| 1569 | ++(env);
|
---|
| 1570 | continue;
|
---|
| 1571 | }
|
---|
[415] | 1572 | if (0 == sl_strncmp((*env), _("TNS_ADMIN="), 10))
|
---|
| 1573 | {
|
---|
| 1574 | ++(env);
|
---|
| 1575 | continue;
|
---|
| 1576 | }
|
---|
[1] | 1577 | #endif
|
---|
| 1578 | /*
|
---|
| 1579 | * Skip the TZ environment variable.
|
---|
| 1580 | */
|
---|
| 1581 | if (0 == sl_strncmp((*env), _("TZ="), 3))
|
---|
| 1582 | {
|
---|
| 1583 | ++(env);
|
---|
| 1584 | continue;
|
---|
| 1585 | }
|
---|
| 1586 | ++(env);
|
---|
| 1587 | if (c != NULL)
|
---|
| 1588 | {
|
---|
| 1589 | ++c;
|
---|
| 1590 | while ((*c) != '\0') {
|
---|
| 1591 | (*c) = '\0';
|
---|
| 1592 | ++c;
|
---|
| 1593 | }
|
---|
| 1594 | }
|
---|
| 1595 | }
|
---|
| 1596 |
|
---|
[205] | 1597 | #ifdef HAVE_TZSET
|
---|
| 1598 | tzset();
|
---|
| 1599 | #endif
|
---|
| 1600 |
|
---|
[1] | 1601 | SL_RET0(_("sh_unix_zeroenv"));
|
---|
| 1602 | }
|
---|
| 1603 |
|
---|
| 1604 |
|
---|
| 1605 | static void sh_unix_resettimer(void)
|
---|
| 1606 | {
|
---|
| 1607 | struct itimerval this_timer;
|
---|
| 1608 |
|
---|
| 1609 | SL_ENTER(_("sh_unix_resettimer"));
|
---|
| 1610 |
|
---|
| 1611 | this_timer.it_value.tv_sec = 0;
|
---|
| 1612 | this_timer.it_value.tv_usec = 0;
|
---|
| 1613 |
|
---|
| 1614 | this_timer.it_interval.tv_sec = 0;
|
---|
| 1615 | this_timer.it_interval.tv_usec = 0;
|
---|
| 1616 |
|
---|
| 1617 | setitimer(ITIMER_REAL, &this_timer, NULL);
|
---|
| 1618 | #if !defined(SH_PROFILE)
|
---|
| 1619 | setitimer(ITIMER_VIRTUAL, &this_timer, NULL);
|
---|
| 1620 | setitimer(ITIMER_PROF, &this_timer, NULL);
|
---|
| 1621 | #endif
|
---|
| 1622 |
|
---|
| 1623 | SL_RET0(_("sh_unix_resettimer"));
|
---|
| 1624 | }
|
---|
| 1625 |
|
---|
| 1626 | static void sh_unix_resetsignals(void)
|
---|
| 1627 | {
|
---|
| 1628 | int sig_num;
|
---|
| 1629 | #ifdef NSIG
|
---|
| 1630 | int max_sig = NSIG;
|
---|
| 1631 | #else
|
---|
| 1632 | int max_sig = 255;
|
---|
| 1633 | #endif
|
---|
| 1634 | int test;
|
---|
| 1635 | int status;
|
---|
[264] | 1636 | struct sigaction act;
|
---|
| 1637 | #if !defined(SH_PROFILE)
|
---|
| 1638 | struct sigaction oldact;
|
---|
| 1639 | #endif
|
---|
[1] | 1640 |
|
---|
| 1641 | sigset_t set_proc;
|
---|
| 1642 |
|
---|
| 1643 | SL_ENTER(_("sh_unix_resetsignals"));
|
---|
| 1644 | /*
|
---|
| 1645 | * Reset the current signal mask (inherited from parent process).
|
---|
| 1646 | */
|
---|
| 1647 |
|
---|
| 1648 | sigfillset(&set_proc);
|
---|
| 1649 |
|
---|
| 1650 | do {
|
---|
| 1651 | errno = 0;
|
---|
[315] | 1652 | test = SH_SETSIGMASK(SIG_UNBLOCK, &set_proc, NULL);
|
---|
[1] | 1653 | } while (test < 0 && errno == EINTR);
|
---|
| 1654 |
|
---|
| 1655 | /*
|
---|
| 1656 | * Reset signal handling.
|
---|
| 1657 | */
|
---|
| 1658 |
|
---|
| 1659 | act.sa_handler = SIG_DFL; /* signal action */
|
---|
| 1660 | sigemptyset( &act.sa_mask ); /* set an empty mask */
|
---|
| 1661 | act.sa_flags = 0; /* init sa_flags */
|
---|
| 1662 |
|
---|
| 1663 | for (sig_num = 1; sig_num <= max_sig; ++sig_num)
|
---|
| 1664 | {
|
---|
| 1665 | #if !defined(SH_PROFILE)
|
---|
| 1666 | test = retry_sigaction(FIL__, __LINE__, sig_num, &act, &oldact);
|
---|
| 1667 | #else
|
---|
| 1668 | test = 0;
|
---|
| 1669 | #endif
|
---|
| 1670 | if ((test == -1) && (errno != EINVAL))
|
---|
| 1671 | {
|
---|
[132] | 1672 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 1673 | status = errno;
|
---|
| 1674 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_SIG,
|
---|
[132] | 1675 | sh_error_message (status, errbuf, sizeof(errbuf)), sig_num);
|
---|
[1] | 1676 | }
|
---|
| 1677 | }
|
---|
| 1678 |
|
---|
| 1679 | SL_RET0(_("sh_unix_resetsignals"));
|
---|
| 1680 | }
|
---|
| 1681 |
|
---|
| 1682 | /* Get the local hostname (FQDN)
|
---|
| 1683 | */
|
---|
[293] | 1684 | static char * sh_tolower (char * s)
|
---|
| 1685 | {
|
---|
| 1686 | char * ret = s;
|
---|
| 1687 | if (s)
|
---|
| 1688 | {
|
---|
| 1689 | for (; *s; ++s)
|
---|
| 1690 | {
|
---|
| 1691 | *s = tolower((unsigned char) *s);
|
---|
| 1692 | }
|
---|
| 1693 | }
|
---|
| 1694 | return ret;
|
---|
| 1695 | }
|
---|
| 1696 |
|
---|
| 1697 |
|
---|
[1] | 1698 | #include <sys/socket.h>
|
---|
| 1699 |
|
---|
| 1700 | /* Required for BSD
|
---|
| 1701 | */
|
---|
| 1702 | #ifdef HAVE_NETINET_IN_H
|
---|
| 1703 | #include <netinet/in.h>
|
---|
| 1704 | #endif
|
---|
| 1705 |
|
---|
| 1706 | #include <arpa/inet.h>
|
---|
| 1707 |
|
---|
[263] | 1708 | const char * sh_unix_h_name (struct hostent * host_entry)
|
---|
[29] | 1709 | {
|
---|
| 1710 | char ** p;
|
---|
| 1711 | if (strchr(host_entry->h_name, '.')) {
|
---|
| 1712 | return host_entry->h_name;
|
---|
| 1713 | } else {
|
---|
| 1714 | for (p = host_entry->h_aliases; *p; ++p) {
|
---|
| 1715 | if (strchr(*p, '.'))
|
---|
| 1716 | return *p;
|
---|
| 1717 | }
|
---|
| 1718 | }
|
---|
| 1719 | return host_entry->h_name;
|
---|
| 1720 | }
|
---|
| 1721 |
|
---|
[1] | 1722 | /* uname() on FreeBSD is broken, because the 'nodename' buf is too small
|
---|
| 1723 | * to hold a valid (leftmost) domain label.
|
---|
| 1724 | */
|
---|
| 1725 | #if defined(HAVE_UNAME) && !defined(HOST_IS_FREEBSD)
|
---|
| 1726 | #include <sys/utsname.h>
|
---|
| 1727 | void sh_unix_localhost()
|
---|
| 1728 | {
|
---|
| 1729 | struct utsname buf;
|
---|
| 1730 | int i;
|
---|
[481] | 1731 | unsigned int ddot;
|
---|
[1] | 1732 | int len;
|
---|
| 1733 | char * p;
|
---|
| 1734 | char hostname[256];
|
---|
[295] | 1735 | char numeric[SH_IP_BUF];
|
---|
| 1736 | char * canonical;
|
---|
[1] | 1737 |
|
---|
| 1738 |
|
---|
| 1739 | SL_ENTER(_("sh_unix_localhost"));
|
---|
| 1740 |
|
---|
| 1741 | (void) uname (&buf);
|
---|
[22] | 1742 | /* flawfinder: ignore */ /* ff bug, ff sees system() */
|
---|
[1] | 1743 | sl_strlcpy (sh.host.system, buf.sysname, SH_MINIBUF);
|
---|
| 1744 | sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
|
---|
| 1745 | sl_strlcpy (sh.host.machine, buf.machine, SH_MINIBUF);
|
---|
| 1746 |
|
---|
| 1747 | /* Workaround for cases where nodename could be
|
---|
| 1748 | * a truncated FQDN.
|
---|
| 1749 | */
|
---|
| 1750 | if (strlen(buf.nodename) == (sizeof(buf.nodename)-1))
|
---|
| 1751 | {
|
---|
| 1752 | p = strchr(buf.nodename, '.');
|
---|
| 1753 | if (NULL != p) {
|
---|
| 1754 | *p = '\0';
|
---|
| 1755 | sl_strlcpy(hostname, buf.nodename, 256);
|
---|
| 1756 | } else {
|
---|
| 1757 | #ifdef HAVE_GETHOSTNAME
|
---|
| 1758 | if (0 != gethostname(hostname, 256))
|
---|
| 1759 | {
|
---|
| 1760 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 1761 | _("nodename returned by uname may be truncated"),
|
---|
| 1762 | _("sh_unix_localhost"));
|
---|
| 1763 | sl_strlcpy (hostname, buf.nodename, 256);
|
---|
| 1764 | }
|
---|
| 1765 | else
|
---|
| 1766 | {
|
---|
| 1767 | hostname[255] = '\0';
|
---|
| 1768 | }
|
---|
| 1769 | #else
|
---|
| 1770 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 1771 | _("nodename returned by uname may be truncated"),
|
---|
| 1772 | _("sh_unix_localhost"));
|
---|
| 1773 | sl_strlcpy(hostname, buf.nodename, 256);
|
---|
| 1774 | #endif
|
---|
| 1775 | }
|
---|
| 1776 | }
|
---|
| 1777 | else
|
---|
| 1778 | {
|
---|
| 1779 | sl_strlcpy(hostname, buf.nodename, 256);
|
---|
| 1780 | }
|
---|
| 1781 |
|
---|
[295] | 1782 | canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
|
---|
[1] | 1783 |
|
---|
[295] | 1784 | if (canonical == NULL)
|
---|
[134] | 1785 | {
|
---|
[295] | 1786 | sl_strlcpy (sh.host.name, hostname, SH_PATHBUF);
|
---|
[293] | 1787 | sh_tolower (sh.host.name);
|
---|
[134] | 1788 | }
|
---|
[295] | 1789 | else
|
---|
[1] | 1790 | {
|
---|
[295] | 1791 | sl_strlcpy (sh.host.name, canonical, SH_PATHBUF);
|
---|
| 1792 | SH_FREE(canonical);
|
---|
[1] | 1793 | }
|
---|
| 1794 |
|
---|
| 1795 | /* check whether it looks like a FQDN
|
---|
| 1796 | */
|
---|
| 1797 | len = sl_strlen(sh.host.name);
|
---|
[170] | 1798 | ddot = 0;
|
---|
[1] | 1799 | for (i = 0; i < len; ++i)
|
---|
| 1800 | if (sh.host.name[i] == '.') ++ddot;
|
---|
| 1801 |
|
---|
[295] | 1802 | if (ddot == 0)
|
---|
[1] | 1803 | {
|
---|
| 1804 | dlog(1, FIL__, __LINE__,
|
---|
| 1805 | _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
|
---|
| 1806 | hostname, sh.host.name);
|
---|
[295] | 1807 | sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
|
---|
[1] | 1808 | SL_RET0(_("sh_unix_localhost"));
|
---|
| 1809 | }
|
---|
| 1810 |
|
---|
[295] | 1811 | if (sh_ipvx_is_numeric(sh.host.name))
|
---|
[1] | 1812 | {
|
---|
| 1813 | dlog(1, FIL__, __LINE__,
|
---|
| 1814 | _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
|
---|
| 1815 | hostname, sh.host.name);
|
---|
| 1816 | }
|
---|
| 1817 |
|
---|
| 1818 | SL_RET0(_("sh_unix_localhost"));
|
---|
| 1819 | }
|
---|
[217] | 1820 |
|
---|
[1] | 1821 | #else
|
---|
[217] | 1822 |
|
---|
| 1823 | /*
|
---|
| 1824 | * --FreeBSD code
|
---|
| 1825 | */
|
---|
| 1826 | #if defined(HAVE_UNAME)
|
---|
| 1827 | #include <sys/utsname.h>
|
---|
| 1828 | #endif
|
---|
[1] | 1829 | void sh_unix_localhost()
|
---|
| 1830 | {
|
---|
[217] | 1831 | #if defined(HAVE_UNAME)
|
---|
| 1832 | struct utsname buf;
|
---|
| 1833 | #endif
|
---|
[1] | 1834 | int i;
|
---|
[170] | 1835 | int ddot;
|
---|
[1] | 1836 | int len;
|
---|
| 1837 | char hostname[1024];
|
---|
[295] | 1838 | char numeric[SH_IP_BUF];
|
---|
| 1839 | char * canonical;
|
---|
[1] | 1840 |
|
---|
| 1841 | SL_ENTER(_("sh_unix_localhost"));
|
---|
| 1842 |
|
---|
[217] | 1843 | #if defined(HAVE_UNAME)
|
---|
| 1844 | (void) uname (&buf);
|
---|
| 1845 | /* flawfinder: ignore */ /* ff bug, ff sees system() */
|
---|
| 1846 | sl_strlcpy (sh.host.system, buf.sysname, SH_MINIBUF);
|
---|
| 1847 | sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
|
---|
| 1848 | sl_strlcpy (sh.host.machine, buf.machine, SH_MINIBUF);
|
---|
| 1849 | #endif
|
---|
| 1850 |
|
---|
[1] | 1851 | (void) gethostname (hostname, 1024);
|
---|
| 1852 | hostname[1023] = '\0';
|
---|
[134] | 1853 |
|
---|
[295] | 1854 | canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
|
---|
[1] | 1855 |
|
---|
[295] | 1856 | if (canonical == NULL)
|
---|
[1] | 1857 | {
|
---|
[295] | 1858 | sl_strlcpy (sh.host.name, hostname, SH_PATHBUF);
|
---|
[293] | 1859 | sh_tolower (sh.host.name);
|
---|
[1] | 1860 | }
|
---|
[295] | 1861 | else
|
---|
[1] | 1862 | {
|
---|
[295] | 1863 | sl_strlcpy (sh.host.name, canonical, SH_PATHBUF);
|
---|
| 1864 | SH_FREE(canonical);
|
---|
[1] | 1865 | }
|
---|
| 1866 |
|
---|
| 1867 | /* check whether it looks like a FQDN
|
---|
| 1868 | */
|
---|
| 1869 | len = sl_strlen(sh.host.name);
|
---|
[170] | 1870 | ddot = 0;
|
---|
[1] | 1871 | for (i = 0; i < len; ++i)
|
---|
| 1872 | if (sh.host.name[i] == '.') ++ddot;
|
---|
| 1873 | if (ddot == 0)
|
---|
| 1874 | {
|
---|
| 1875 | dlog(1, FIL__, __LINE__,
|
---|
| 1876 | _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
|
---|
| 1877 | hostname, sh.host.name);
|
---|
[295] | 1878 | sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
|
---|
[1] | 1879 | SL_RET0(_("sh_unix_localhost"));
|
---|
| 1880 | }
|
---|
| 1881 |
|
---|
[295] | 1882 | if (sh_ipvx_is_numeric(sh.host.name))
|
---|
[1] | 1883 | {
|
---|
| 1884 | dlog(1, FIL__, __LINE__,
|
---|
| 1885 | _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
|
---|
| 1886 | hostname, sh.host.name);
|
---|
| 1887 | }
|
---|
| 1888 |
|
---|
| 1889 | SL_RET0(_("sh_unix_localhost"));
|
---|
| 1890 | }
|
---|
| 1891 | #endif
|
---|
| 1892 |
|
---|
| 1893 |
|
---|
| 1894 | void sh_unix_memlock()
|
---|
| 1895 | {
|
---|
| 1896 | SL_ENTER(_("sh_unix_memlock"));
|
---|
| 1897 |
|
---|
| 1898 | /* do this before dropping privileges
|
---|
| 1899 | */
|
---|
| 1900 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
[481] | 1901 | if (skey->mlock_failed == S_FALSE)
|
---|
[1] | 1902 | {
|
---|
[19] | 1903 | if ( (-1) == sh_unix_mlock( FIL__, __LINE__,
|
---|
| 1904 | (char *) skey, sizeof (sh_key_t)) )
|
---|
[1] | 1905 | {
|
---|
[134] | 1906 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[481] | 1907 | skey->mlock_failed = S_TRUE;
|
---|
[134] | 1908 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1909 | }
|
---|
| 1910 | }
|
---|
| 1911 | #else
|
---|
[481] | 1912 | if (skey->mlock_failed == S_FALSE)
|
---|
[1] | 1913 | {
|
---|
[134] | 1914 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[481] | 1915 | skey->mlock_failed = S_TRUE;
|
---|
[134] | 1916 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1917 | }
|
---|
| 1918 | #endif
|
---|
| 1919 |
|
---|
| 1920 | SL_RET0(_("sh_unix_memlock"));
|
---|
| 1921 | }
|
---|
| 1922 |
|
---|
| 1923 | #ifdef SH_WITH_SERVER
|
---|
| 1924 | char * chroot_dir = NULL;
|
---|
| 1925 |
|
---|
[20] | 1926 | int sh_unix_set_chroot(const char * str)
|
---|
[1] | 1927 | {
|
---|
| 1928 | size_t len;
|
---|
| 1929 | static int block = 0;
|
---|
| 1930 |
|
---|
| 1931 | if (block == 1)
|
---|
| 1932 | return 0;
|
---|
| 1933 |
|
---|
| 1934 | if (str && *str == '/')
|
---|
| 1935 | {
|
---|
| 1936 | len = strlen(str) + 1;
|
---|
[454] | 1937 | chroot_dir = calloc(1,strlen(str) + 1); /* only once */
|
---|
[1] | 1938 | if (!chroot_dir)
|
---|
| 1939 | {
|
---|
| 1940 | fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
|
---|
| 1941 | return 1;
|
---|
| 1942 | }
|
---|
| 1943 | sl_strlcpy(chroot_dir, str, len);
|
---|
| 1944 | block = 1;
|
---|
| 1945 | return 0;
|
---|
| 1946 | }
|
---|
| 1947 | return 1;
|
---|
| 1948 | }
|
---|
| 1949 |
|
---|
[170] | 1950 | int sh_unix_chroot(void)
|
---|
[1] | 1951 | {
|
---|
| 1952 | int status;
|
---|
| 1953 |
|
---|
| 1954 | if (chroot_dir != NULL)
|
---|
| 1955 | {
|
---|
| 1956 | status = retry_aud_chdir(FIL__, __LINE__, chroot_dir);
|
---|
| 1957 | if ( (-1) == status )
|
---|
| 1958 | {
|
---|
[132] | 1959 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 1960 | status = errno;
|
---|
| 1961 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
|
---|
[132] | 1962 | sh_error_message (status, errbuf, sizeof(errbuf)), chroot_dir);
|
---|
[1] | 1963 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 1964 | }
|
---|
[22] | 1965 | /* flawfinder: ignore */
|
---|
[1] | 1966 | return (chroot(chroot_dir));
|
---|
| 1967 | }
|
---|
| 1968 | return 0;
|
---|
| 1969 | }
|
---|
| 1970 | /* #ifdef SH_WITH_SERVER */
|
---|
| 1971 | #else
|
---|
[170] | 1972 | int sh_unix_chroot(void) { return 0; }
|
---|
[1] | 1973 | #endif
|
---|
| 1974 |
|
---|
| 1975 | /* daemon mode
|
---|
| 1976 | */
|
---|
| 1977 | static int block_setdeamon = 0;
|
---|
| 1978 |
|
---|
[20] | 1979 | int sh_unix_setdeamon(const char * dummy)
|
---|
[1] | 1980 | {
|
---|
| 1981 | int res = 0;
|
---|
| 1982 |
|
---|
| 1983 | SL_ENTER(_("sh_unix_setdeamon"));
|
---|
| 1984 |
|
---|
| 1985 | if (block_setdeamon != 0)
|
---|
| 1986 | SL_RETURN((0),_("sh_unix_setdeamon"));
|
---|
| 1987 |
|
---|
| 1988 | if (dummy == NULL)
|
---|
[481] | 1989 | sh.flag.isdaemon = S_TRUE;
|
---|
[1] | 1990 | else
|
---|
| 1991 | res = sh_util_flagval (dummy, &sh.flag.isdaemon);
|
---|
| 1992 |
|
---|
| 1993 | if (sh.flag.opts == S_TRUE)
|
---|
| 1994 | block_setdeamon = 1;
|
---|
| 1995 |
|
---|
| 1996 | SL_RETURN(res, _("sh_unix_setdeamon"));
|
---|
| 1997 | }
|
---|
| 1998 | #if defined(HAVE_LIBPRELUDE)
|
---|
| 1999 | #include "sh_prelude.h"
|
---|
| 2000 | #endif
|
---|
| 2001 |
|
---|
[20] | 2002 | int sh_unix_setnodeamon(const char * dummy)
|
---|
[1] | 2003 | {
|
---|
| 2004 | int res = 0;
|
---|
[132] | 2005 |
|
---|
[1] | 2006 | SL_ENTER(_("sh_unix_setnodeamon"));
|
---|
| 2007 |
|
---|
| 2008 | if (block_setdeamon != 0)
|
---|
| 2009 | SL_RETURN((0),_("sh_unix_setmodeamon"));
|
---|
| 2010 |
|
---|
| 2011 | if (dummy == NULL)
|
---|
[481] | 2012 | sh.flag.isdaemon = S_FALSE;
|
---|
[1] | 2013 | else
|
---|
| 2014 | res = sh_util_flagval (dummy, &sh.flag.isdaemon);
|
---|
| 2015 |
|
---|
| 2016 | if (sh.flag.opts == S_TRUE)
|
---|
| 2017 | block_setdeamon = 1;
|
---|
| 2018 |
|
---|
| 2019 | SL_RETURN(res, _("sh_unix_setnodeamon"));
|
---|
| 2020 | }
|
---|
| 2021 |
|
---|
| 2022 | int sh_unix_init(int goDaemon)
|
---|
| 2023 | {
|
---|
| 2024 | int status;
|
---|
| 2025 | uid_t uid;
|
---|
| 2026 | pid_t oldpid = getpid();
|
---|
| 2027 | #if defined(SH_WITH_SERVER)
|
---|
[170] | 2028 | extern int sh_socket_open_int (void);
|
---|
[1] | 2029 | #endif
|
---|
[132] | 2030 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 2031 |
|
---|
[321] | 2032 | extern void sh_kill_sub();
|
---|
| 2033 |
|
---|
[1] | 2034 | SL_ENTER(_("sh_unix_init"));
|
---|
| 2035 |
|
---|
| 2036 | /* fork twice, exit the parent process
|
---|
| 2037 | */
|
---|
| 2038 | if (goDaemon == 1) {
|
---|
| 2039 |
|
---|
| 2040 | switch (aud_fork(FIL__, __LINE__)) {
|
---|
| 2041 | case 0: break; /* child process continues */
|
---|
| 2042 | case -1: SL_RETURN((-1),_("sh_unix_init")); /* error */
|
---|
[321] | 2043 | default: /* parent process exits */
|
---|
| 2044 | sh_kill_sub();
|
---|
| 2045 | aud__exit(FIL__, __LINE__, 0);
|
---|
[1] | 2046 | }
|
---|
| 2047 |
|
---|
| 2048 | /* Child processes do not inherit page locks across a fork.
|
---|
| 2049 | * Error in next fork would return in this (?) thread of execution.
|
---|
| 2050 | */
|
---|
| 2051 | sh_unix_memlock();
|
---|
| 2052 |
|
---|
| 2053 | setsid(); /* should not fail */
|
---|
[162] | 2054 | sh.pid = (UINT64) getpid();
|
---|
[1] | 2055 |
|
---|
| 2056 | switch (aud_fork(FIL__, __LINE__)) {
|
---|
| 2057 | case 0: break; /* child process continues */
|
---|
| 2058 | case -1: SL_RETURN((-1),_("sh_unix_init")); /* error */
|
---|
[321] | 2059 | default: /* parent process exits */
|
---|
| 2060 | sh_kill_sub();
|
---|
| 2061 | aud__exit(FIL__, __LINE__, 0);
|
---|
[1] | 2062 | }
|
---|
| 2063 |
|
---|
| 2064 | /* Child processes do not inherit page locks across a fork.
|
---|
| 2065 | */
|
---|
| 2066 | sh_unix_memlock();
|
---|
[162] | 2067 | sh.pid = (UINT64) getpid();
|
---|
[1] | 2068 |
|
---|
| 2069 | } else {
|
---|
| 2070 | setsid(); /* should not fail */
|
---|
| 2071 | }
|
---|
| 2072 |
|
---|
| 2073 | /* set working directory
|
---|
| 2074 | */
|
---|
| 2075 | #ifdef SH_PROFILE
|
---|
| 2076 | status = 0;
|
---|
| 2077 | #else
|
---|
| 2078 | status = retry_aud_chdir(FIL__, __LINE__, "/");
|
---|
| 2079 | #endif
|
---|
| 2080 | if ( (-1) == status )
|
---|
| 2081 | {
|
---|
| 2082 | status = errno;
|
---|
| 2083 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
|
---|
[132] | 2084 | sh_error_message (status, errbuf, sizeof(errbuf)), "/");
|
---|
[1] | 2085 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 2086 | }
|
---|
| 2087 |
|
---|
| 2088 | /* reset timers
|
---|
| 2089 | */
|
---|
| 2090 | sh_unix_resettimer();
|
---|
| 2091 |
|
---|
| 2092 | /* signal handlers
|
---|
| 2093 | */
|
---|
| 2094 | sh_unix_resetsignals();
|
---|
| 2095 | #if defined(SCREW_IT_UP)
|
---|
| 2096 | sh_sigtrap_prepare();
|
---|
| 2097 | #endif
|
---|
| 2098 | sh_unix_siginstall (goDaemon);
|
---|
| 2099 |
|
---|
| 2100 | /* set file creation mask
|
---|
| 2101 | */
|
---|
| 2102 | (void) umask (0); /* should not fail */
|
---|
| 2103 |
|
---|
| 2104 | /* set resource limits to maximum, and
|
---|
| 2105 | * core dump size to zero
|
---|
| 2106 | */
|
---|
| 2107 | sh_unix_setlimits();
|
---|
| 2108 |
|
---|
| 2109 | /* zero out the environment (like PATH='\0')
|
---|
| 2110 | */
|
---|
| 2111 | sh_unix_zeroenv();
|
---|
| 2112 |
|
---|
| 2113 | if (goDaemon == 1)
|
---|
| 2114 | {
|
---|
[92] | 2115 | /* Close first tree file descriptors
|
---|
[84] | 2116 | */
|
---|
[252] | 2117 | sl_close_fd (FIL__, __LINE__, 0); /* if running as daemon */
|
---|
| 2118 | sl_close_fd (FIL__, __LINE__, 1); /* if running as daemon */
|
---|
| 2119 | sl_close_fd (FIL__, __LINE__, 2); /* if running as daemon */
|
---|
[84] | 2120 |
|
---|
| 2121 | /* Enable full error logging
|
---|
| 2122 | */
|
---|
| 2123 | sh_error_only_stderr (S_FALSE);
|
---|
| 2124 |
|
---|
| 2125 | /* open first three streams to /dev/null
|
---|
| 2126 | */
|
---|
| 2127 | status = aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
|
---|
| 2128 | if (status < 0)
|
---|
| 2129 | {
|
---|
| 2130 | status = errno;
|
---|
| 2131 | sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
[132] | 2132 | sh_error_message(status, errbuf, sizeof(errbuf)), _("open"));
|
---|
[84] | 2133 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 2134 | }
|
---|
| 2135 |
|
---|
| 2136 | status = retry_aud_dup(FIL__, __LINE__, 0);
|
---|
| 2137 | if (status >= 0)
|
---|
| 2138 | retry_aud_dup(FIL__, __LINE__, 0);
|
---|
| 2139 |
|
---|
| 2140 | if (status < 0)
|
---|
| 2141 | {
|
---|
| 2142 | status = errno;
|
---|
| 2143 | sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
[132] | 2144 | sh_error_message(status, errbuf, sizeof(errbuf)), _("dup"));
|
---|
[84] | 2145 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 2146 | }
|
---|
| 2147 |
|
---|
[86] | 2148 | sh_error_enable_unsafe (S_TRUE);
|
---|
[1] | 2149 | #if defined(HAVE_LIBPRELUDE)
|
---|
| 2150 | sh_prelude_reset ();
|
---|
| 2151 | #endif
|
---|
| 2152 |
|
---|
| 2153 | /* --- wait until parent has exited ---
|
---|
| 2154 | */
|
---|
| 2155 | while (1 == 1)
|
---|
| 2156 | {
|
---|
| 2157 | errno = 0;
|
---|
| 2158 | if (0 > aud_kill (FIL__, __LINE__, oldpid, 0) && errno == ESRCH)
|
---|
| 2159 | {
|
---|
| 2160 | break;
|
---|
| 2161 | }
|
---|
| 2162 | retry_msleep(0, 1);
|
---|
| 2163 | }
|
---|
| 2164 |
|
---|
| 2165 | /* write PID file
|
---|
| 2166 | */
|
---|
| 2167 | status = sh_unix_write_pid_file();
|
---|
| 2168 | if (status < 0)
|
---|
| 2169 | {
|
---|
| 2170 | sl_get_euid(&uid);
|
---|
| 2171 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_PIDFILE,
|
---|
| 2172 | (long) uid, sh.srvlog.alt);
|
---|
| 2173 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 2174 | }
|
---|
| 2175 | #if defined(SH_WITH_SERVER)
|
---|
| 2176 | sh_socket_open_int ();
|
---|
| 2177 | #endif
|
---|
| 2178 | }
|
---|
| 2179 | else
|
---|
| 2180 | {
|
---|
[86] | 2181 | sh_error_enable_unsafe (S_TRUE);
|
---|
[1] | 2182 | #if defined(HAVE_LIBPRELUDE)
|
---|
| 2183 | sh_prelude_reset ();
|
---|
| 2184 | #endif
|
---|
| 2185 | #if defined(SH_WITH_SERVER)
|
---|
| 2186 | sh_socket_open_int ();
|
---|
| 2187 | #endif
|
---|
| 2188 | }
|
---|
| 2189 |
|
---|
| 2190 | /* chroot (this is a no-op if no chroot dir is specified
|
---|
| 2191 | */
|
---|
| 2192 | status = sh_unix_chroot();
|
---|
| 2193 | if (status < 0)
|
---|
| 2194 | {
|
---|
| 2195 | status = errno;
|
---|
| 2196 | sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
[132] | 2197 | sh_error_message(status, errbuf, sizeof(errbuf)), _("chroot"));
|
---|
[1] | 2198 | aud_exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 2199 | }
|
---|
| 2200 |
|
---|
| 2201 | /* drop capabilities
|
---|
| 2202 | */
|
---|
| 2203 | sl_drop_cap();
|
---|
| 2204 |
|
---|
| 2205 | SL_RETURN((0),_("sh_unix_init"));
|
---|
| 2206 | }
|
---|
| 2207 |
|
---|
[197] | 2208 | /* --- run a command, securely --- */
|
---|
| 2209 |
|
---|
| 2210 | int sh_unix_run_command (const char * str)
|
---|
| 2211 | {
|
---|
| 2212 | pid_t pid;
|
---|
| 2213 | char * arg[4];
|
---|
| 2214 | char * env[5];
|
---|
| 2215 | char * path = sh_util_strdup(_("/bin/sh"));
|
---|
| 2216 |
|
---|
| 2217 | int status = -1;
|
---|
| 2218 |
|
---|
| 2219 | arg[0] = sh_util_strdup(_("/bin/sh"));
|
---|
| 2220 | arg[1] = sh_util_strdup(_("-c"));
|
---|
| 2221 | arg[2] = sh_util_strdup(str);
|
---|
| 2222 | arg[3] = NULL;
|
---|
| 2223 |
|
---|
| 2224 | env[0] = sh_util_strdup(_("PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/ucb"));
|
---|
| 2225 | env[1] = sh_util_strdup(_("SHELL=/bin/sh"));
|
---|
| 2226 | env[2] = sh_util_strdup(_("IFS= \t\n"));
|
---|
[199] | 2227 | if (getenv("TZ")) { /* flawfinder: ignore */
|
---|
| 2228 | char * tz = sh_util_strdup(getenv("TZ")); /* flawfinder: ignore */
|
---|
| 2229 | size_t tzlen = strlen(tz);
|
---|
[481] | 2230 | if (S_TRUE == sl_ok_adds (4, tzlen)) {
|
---|
[199] | 2231 | env[3] = SH_ALLOC(4+tzlen);
|
---|
[197] | 2232 | sl_strlcpy(env[3], "TZ=", 4);
|
---|
[199] | 2233 | sl_strlcat(env[3], tz , 4+tzlen);
|
---|
[197] | 2234 | } else {
|
---|
| 2235 | env[3] = NULL;
|
---|
| 2236 | }
|
---|
| 2237 | } else {
|
---|
| 2238 | env[3] = NULL;
|
---|
| 2239 | }
|
---|
| 2240 | env[4] = NULL;
|
---|
| 2241 |
|
---|
| 2242 | pid = fork();
|
---|
| 2243 |
|
---|
| 2244 | if (pid == (pid_t)(-1))
|
---|
| 2245 | {
|
---|
| 2246 | return -1;
|
---|
| 2247 | }
|
---|
| 2248 |
|
---|
| 2249 | else if (pid == 0) /* child */
|
---|
| 2250 | {
|
---|
| 2251 | memset(skey, 0, sizeof(sh_key_t));
|
---|
| 2252 | (void) umask(S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
|
---|
[481] | 2253 | sh_unix_closeall (3, -1, S_TRUE); /* in child process */
|
---|
[197] | 2254 | execve(path, arg, env);
|
---|
| 2255 | _exit(EXIT_FAILURE);
|
---|
| 2256 | }
|
---|
| 2257 |
|
---|
| 2258 | else /* parent */
|
---|
| 2259 | {
|
---|
| 2260 | int r;
|
---|
| 2261 |
|
---|
[203] | 2262 | while((r = waitpid(pid, &status, WUNTRACED)) != pid && r != -1) ;
|
---|
[197] | 2263 |
|
---|
[199] | 2264 | #if !defined(USE_UNO)
|
---|
[197] | 2265 | if (r == -1 || !WIFEXITED(status))
|
---|
| 2266 | {
|
---|
| 2267 | status = -1;
|
---|
| 2268 | }
|
---|
| 2269 | else
|
---|
| 2270 | {
|
---|
| 2271 | status = WEXITSTATUS(status);
|
---|
| 2272 | }
|
---|
[199] | 2273 | #endif
|
---|
[197] | 2274 | }
|
---|
| 2275 |
|
---|
| 2276 | return status;
|
---|
| 2277 | }
|
---|
| 2278 |
|
---|
[1] | 2279 | /********************************************************
|
---|
| 2280 | *
|
---|
| 2281 | * TIME
|
---|
| 2282 | *
|
---|
| 2283 | ********************************************************/
|
---|
| 2284 |
|
---|
| 2285 | /* Figure out the time offset of the current timezone
|
---|
| 2286 | * in a portable way.
|
---|
| 2287 | */
|
---|
| 2288 | char * t_zone(const time_t * xx)
|
---|
| 2289 | {
|
---|
| 2290 | struct tm aa;
|
---|
| 2291 | struct tm bb;
|
---|
[362] | 2292 |
|
---|
[481] | 2293 | struct tm * aptr;
|
---|
| 2294 | struct tm * bptr;
|
---|
| 2295 |
|
---|
[1] | 2296 | int sign = 0;
|
---|
| 2297 | int diff = 0;
|
---|
| 2298 | int hh, mm;
|
---|
| 2299 | static char tz[64];
|
---|
| 2300 |
|
---|
| 2301 | SL_ENTER(_("t_zone"));
|
---|
| 2302 |
|
---|
[131] | 2303 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
|
---|
[481] | 2304 | aptr = gmtime_r (xx, &aa);
|
---|
[131] | 2305 | #else
|
---|
[481] | 2306 | aptr = gmtime(xx);
|
---|
| 2307 | if (aptr)
|
---|
| 2308 | memcpy (&aa, aptr, sizeof(struct tm));
|
---|
[131] | 2309 | #endif
|
---|
| 2310 |
|
---|
| 2311 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
|
---|
[481] | 2312 | bptr = localtime_r (xx, &bb);
|
---|
[131] | 2313 | #else
|
---|
[481] | 2314 | bptr = localtime(xx);
|
---|
| 2315 | if (bptr)
|
---|
| 2316 | memcpy (&bb, bptr, sizeof(struct tm));
|
---|
[131] | 2317 | #endif
|
---|
[1] | 2318 |
|
---|
[481] | 2319 | if (bptr && aptr)
|
---|
| 2320 | {
|
---|
| 2321 | /* Check for datum wrap-around.
|
---|
| 2322 | */
|
---|
[494] | 2323 | if ((aa.tm_mday == 1) && (aa.tm_mday < bb.tm_mday) && (aa.tm_hour < bb.tm_hour))
|
---|
[493] | 2324 | sign = ( 1);
|
---|
| 2325 | else if (aa.tm_year < bb.tm_year)
|
---|
[481] | 2326 | sign = (-1);
|
---|
| 2327 | else if (aa.tm_mon < bb.tm_mon)
|
---|
| 2328 | sign = (-1);
|
---|
| 2329 | else if (aa.tm_mday < bb.tm_mday)
|
---|
| 2330 | sign = (-1);
|
---|
| 2331 | else if (bb.tm_year < aa.tm_year)
|
---|
| 2332 | sign = ( 1);
|
---|
| 2333 | else if (bb.tm_mon < aa.tm_mon)
|
---|
| 2334 | sign = ( 1);
|
---|
| 2335 | else if (bb.tm_mday < aa.tm_mday)
|
---|
| 2336 | sign = ( 1);
|
---|
| 2337 |
|
---|
| 2338 | diff = aa.tm_hour * 60 + aa.tm_min;
|
---|
| 2339 | diff = (bb.tm_hour * 60 + bb.tm_min) - diff;
|
---|
| 2340 | diff = diff - (sign * 24 * 60); /* datum wrap-around correction */
|
---|
| 2341 | hh = diff / 60;
|
---|
| 2342 | mm = diff - (hh * 60);
|
---|
| 2343 | sprintf (tz, _("%+03d%02d"), hh, mm); /* known to fit */
|
---|
| 2344 | }
|
---|
| 2345 | else
|
---|
| 2346 | {
|
---|
| 2347 | sprintf (tz, _("%+03d%02d"), 0, 0);
|
---|
| 2348 | }
|
---|
[1] | 2349 | SL_RETURN(tz, _("t_zone"));
|
---|
| 2350 | }
|
---|
| 2351 |
|
---|
| 2352 | unsigned long sh_unix_longtime ()
|
---|
| 2353 | {
|
---|
| 2354 | return ((unsigned long)time(NULL));
|
---|
| 2355 | }
|
---|
| 2356 |
|
---|
| 2357 | #ifdef HAVE_GETTIMEOFDAY
|
---|
| 2358 | unsigned long sh_unix_notime ()
|
---|
| 2359 | {
|
---|
| 2360 | struct timeval tv;
|
---|
| 2361 |
|
---|
| 2362 | gettimeofday (&tv, NULL);
|
---|
| 2363 |
|
---|
| 2364 | return ((unsigned long)(tv.tv_sec + tv.tv_usec * 10835 + getpid() + getppid()));
|
---|
| 2365 |
|
---|
| 2366 | }
|
---|
| 2367 | #endif
|
---|
| 2368 |
|
---|
| 2369 | static int count_dev_time = 0;
|
---|
| 2370 |
|
---|
| 2371 | void reset_count_dev_time(void)
|
---|
| 2372 | {
|
---|
| 2373 | count_dev_time = 0;
|
---|
| 2374 | return;
|
---|
| 2375 | }
|
---|
| 2376 |
|
---|
[22] | 2377 | int sh_unix_settimeserver (const char * address)
|
---|
[1] | 2378 | {
|
---|
| 2379 |
|
---|
| 2380 | SL_ENTER(_("sh_unix_settimeserver"));
|
---|
| 2381 |
|
---|
| 2382 | if (address != NULL && count_dev_time < 2
|
---|
| 2383 | && sl_strlen(address) < SH_PATHBUF)
|
---|
| 2384 | {
|
---|
| 2385 | if (count_dev_time == 0)
|
---|
| 2386 | sl_strlcpy (sh.srvtime.name, address, SH_PATHBUF);
|
---|
| 2387 | else
|
---|
| 2388 | sl_strlcpy (sh.srvtime.alt, address, SH_PATHBUF);
|
---|
| 2389 |
|
---|
| 2390 | ++count_dev_time;
|
---|
| 2391 | SL_RETURN((0), _("sh_unix_settimeserver"));
|
---|
| 2392 | }
|
---|
| 2393 | SL_RETURN((-1), _("sh_unix_settimeserver"));
|
---|
| 2394 | }
|
---|
| 2395 |
|
---|
| 2396 |
|
---|
| 2397 | #ifdef HAVE_NTIME
|
---|
| 2398 | #define UNIXEPOCH 2208988800UL /* difference between Unix time and net time
|
---|
| 2399 | * The UNIX EPOCH starts in 1970.
|
---|
| 2400 | */
|
---|
| 2401 | #include <sys/socket.h>
|
---|
| 2402 | #include <netinet/in.h>
|
---|
| 2403 | #include <arpa/inet.h>
|
---|
| 2404 | #include <netdb.h>
|
---|
| 2405 | #include <ctype.h>
|
---|
| 2406 | #endif
|
---|
| 2407 |
|
---|
| 2408 | /* Timeserver service. */
|
---|
| 2409 | /* define is missing on HP-UX 10.20 */
|
---|
| 2410 | #ifndef IPPORT_TIMESERVER
|
---|
| 2411 | #define IPPORT_TIMESERVER 37
|
---|
| 2412 | #endif
|
---|
| 2413 |
|
---|
[132] | 2414 | char * sh_unix_time (time_t thetime, char * buffer, size_t len)
|
---|
[1] | 2415 | {
|
---|
| 2416 |
|
---|
| 2417 | int status;
|
---|
[132] | 2418 | char AsciiTime[81]; /* local time */
|
---|
[1] | 2419 | time_t time_now;
|
---|
| 2420 | struct tm * time_ptr;
|
---|
[131] | 2421 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
|
---|
| 2422 | struct tm time_tm;
|
---|
| 2423 | #endif
|
---|
[1] | 2424 | #ifdef SH_USE_XML
|
---|
| 2425 | static char deftime[] = N_("0000-00-00T00:00:00"); /* default time */
|
---|
| 2426 | #else
|
---|
| 2427 | static char deftime[] = N_("[0000-00-00T00:00:00]"); /* default time */
|
---|
| 2428 | #endif
|
---|
| 2429 |
|
---|
| 2430 | #ifdef HAVE_NTIME
|
---|
| 2431 | int fd; /* network file descriptor */
|
---|
| 2432 | u_char net_time[4]; /* remote time in network format */
|
---|
| 2433 | static int failerr = 0; /* no net time */
|
---|
| 2434 | int fail = 0; /* no net time */
|
---|
| 2435 | int errflag;
|
---|
| 2436 | char errmsg[256];
|
---|
| 2437 | char error_call[SH_MINIBUF];
|
---|
| 2438 | int error_num;
|
---|
| 2439 | #endif
|
---|
| 2440 |
|
---|
| 2441 | SL_ENTER(_("sh_unix_time"));
|
---|
| 2442 |
|
---|
| 2443 | #ifdef HAVE_NTIME
|
---|
| 2444 | if (thetime == 0)
|
---|
| 2445 | {
|
---|
| 2446 | if (sh.srvtime.name[0] == '\0')
|
---|
| 2447 | {
|
---|
| 2448 | fail = 1;
|
---|
| 2449 | (void) time (&time_now);
|
---|
| 2450 | }
|
---|
| 2451 | else /* have a timeserver address */
|
---|
| 2452 | {
|
---|
[343] | 2453 | /* don't call timeserver more than once per second */
|
---|
| 2454 | static time_t time_old = 0;
|
---|
| 2455 | time_t time_new;
|
---|
| 2456 | static time_t time_saved = 0;
|
---|
| 2457 | (void) time (&time_new);
|
---|
| 2458 | if ((time_new == time_old) && (time_saved != 0))
|
---|
| 2459 | {
|
---|
| 2460 | time_now = time_saved;
|
---|
| 2461 | goto end;
|
---|
| 2462 | }
|
---|
| 2463 | time_old = time_new;
|
---|
| 2464 |
|
---|
| 2465 |
|
---|
[1] | 2466 | fd = connect_port_2 (sh.srvtime.name, sh.srvtime.alt,
|
---|
| 2467 | IPPORT_TIMESERVER,
|
---|
| 2468 | error_call, &error_num, errmsg, sizeof(errmsg));
|
---|
| 2469 | if (fd >= 0)
|
---|
| 2470 | {
|
---|
| 2471 | if (4 != read_port (fd, (char *) net_time, 4, &errflag, 2))
|
---|
| 2472 | {
|
---|
| 2473 | fail = 1;
|
---|
| 2474 | sh_error_handle ((-1), FIL__, __LINE__, errflag,
|
---|
| 2475 | MSG_E_NLOST,
|
---|
| 2476 | _("time"), sh.srvtime.name);
|
---|
| 2477 | }
|
---|
[252] | 2478 | sl_close_fd(FIL__, __LINE__, fd);
|
---|
[1] | 2479 | }
|
---|
| 2480 | else
|
---|
| 2481 | {
|
---|
| 2482 | sh_error_handle ((-1), FIL__, __LINE__, error_num,
|
---|
| 2483 | MSG_E_NET, errmsg, error_call,
|
---|
| 2484 | _("time"), sh.srvtime.name);
|
---|
| 2485 | fail = 1;
|
---|
| 2486 | }
|
---|
| 2487 |
|
---|
| 2488 | if (fail == 0)
|
---|
| 2489 | {
|
---|
[230] | 2490 | unsigned long ltmp;
|
---|
| 2491 | UINT32 ttmp;
|
---|
| 2492 | memcpy(&ttmp, net_time, sizeof(UINT32)); ltmp = ttmp;
|
---|
| 2493 | time_now = ntohl(ltmp) - UNIXEPOCH;
|
---|
[343] | 2494 | time_saved = time_now;
|
---|
| 2495 |
|
---|
[1] | 2496 | if (failerr == 1) {
|
---|
| 2497 | failerr = 0;
|
---|
| 2498 | sh_error_handle ((-1), FIL__, __LINE__, 0,
|
---|
| 2499 | MSG_E_NEST,
|
---|
| 2500 | _("time"), sh.srvtime.name);
|
---|
| 2501 | }
|
---|
| 2502 | }
|
---|
| 2503 | else
|
---|
| 2504 | {
|
---|
| 2505 | (void) time (&time_now);
|
---|
[343] | 2506 | time_saved = 0;
|
---|
| 2507 |
|
---|
[1] | 2508 | if (failerr == 0)
|
---|
| 2509 | {
|
---|
| 2510 | failerr = 1;
|
---|
| 2511 | sh_error_handle ((-1), FIL__, __LINE__, errflag,
|
---|
| 2512 | MSG_SRV_FAIL,
|
---|
| 2513 | _("time"), sh.srvtime.name);
|
---|
| 2514 | }
|
---|
| 2515 | }
|
---|
[343] | 2516 | end:
|
---|
| 2517 | ; /* 'label at end of compound statement' */
|
---|
[1] | 2518 | }
|
---|
| 2519 | }
|
---|
| 2520 | else
|
---|
| 2521 | {
|
---|
| 2522 | time_now = thetime;
|
---|
| 2523 | }
|
---|
| 2524 |
|
---|
| 2525 | /* #ifdef HAVE_NTIME */
|
---|
| 2526 | #else
|
---|
| 2527 |
|
---|
| 2528 | if (thetime == 0)
|
---|
| 2529 | {
|
---|
| 2530 | (void) time (&time_now);
|
---|
| 2531 | }
|
---|
| 2532 | else
|
---|
| 2533 | {
|
---|
| 2534 | time_now = thetime;
|
---|
| 2535 | }
|
---|
| 2536 |
|
---|
| 2537 | /* #ifdef HAVE_NTIME */
|
---|
| 2538 | #endif
|
---|
| 2539 |
|
---|
[132] | 2540 | if (time_now == (-1) )
|
---|
| 2541 | {
|
---|
| 2542 | sl_strlcpy(buffer, _(deftime), len);
|
---|
| 2543 | SL_RETURN(buffer, _("sh_unix_time"));
|
---|
| 2544 | }
|
---|
[1] | 2545 | else
|
---|
[131] | 2546 | {
|
---|
| 2547 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
|
---|
| 2548 | time_ptr = localtime_r (&time_now, &time_tm);
|
---|
| 2549 | #else
|
---|
| 2550 | time_ptr = localtime (&time_now);
|
---|
| 2551 | #endif
|
---|
| 2552 | }
|
---|
[1] | 2553 | if (time_ptr != NULL)
|
---|
| 2554 | {
|
---|
[132] | 2555 | status = strftime (AsciiTime, sizeof(AsciiTime),
|
---|
[1] | 2556 | #ifdef SH_USE_XML
|
---|
| 2557 | _("%Y-%m-%dT%H:%M:%S%%s"),
|
---|
| 2558 | #else
|
---|
| 2559 | _("[%Y-%m-%dT%H:%M:%S%%s]"),
|
---|
| 2560 | #endif
|
---|
| 2561 | time_ptr);
|
---|
| 2562 |
|
---|
[132] | 2563 | sl_snprintf(buffer, len, AsciiTime, t_zone(&time_now));
|
---|
[1] | 2564 |
|
---|
[132] | 2565 | if ( (status == 0) || (status == sizeof(AsciiTime)) )
|
---|
| 2566 | {
|
---|
| 2567 | sl_strlcpy(buffer, _(deftime), len);
|
---|
| 2568 | SL_RETURN( buffer, _("sh_unix_time"));
|
---|
| 2569 | }
|
---|
[1] | 2570 | else
|
---|
[132] | 2571 | {
|
---|
| 2572 | SL_RETURN(buffer, _("sh_unix_time"));
|
---|
| 2573 | }
|
---|
[1] | 2574 | }
|
---|
| 2575 |
|
---|
| 2576 | /* last resort
|
---|
| 2577 | */
|
---|
[132] | 2578 | sl_strlcpy(buffer, _(deftime), len);
|
---|
| 2579 | SL_RETURN( buffer, _("sh_unix_time"));
|
---|
[1] | 2580 | }
|
---|
| 2581 |
|
---|
| 2582 | static int sh_unix_use_localtime = S_FALSE;
|
---|
| 2583 |
|
---|
[481] | 2584 | /* whether to use localtime for file timestamps in logs
|
---|
[1] | 2585 | */
|
---|
[22] | 2586 | int sh_unix_uselocaltime (const char * c)
|
---|
[1] | 2587 | {
|
---|
| 2588 | int i;
|
---|
| 2589 | SL_ENTER(_("sh_unix_uselocaltime"));
|
---|
| 2590 | i = sh_util_flagval(c, &(sh_unix_use_localtime));
|
---|
| 2591 |
|
---|
| 2592 | SL_RETURN(i, _("sh_unix_uselocaltime"));
|
---|
| 2593 | }
|
---|
| 2594 |
|
---|
[132] | 2595 | char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len)
|
---|
[1] | 2596 | {
|
---|
| 2597 |
|
---|
| 2598 | int status;
|
---|
| 2599 |
|
---|
| 2600 | struct tm * time_ptr;
|
---|
[131] | 2601 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS)
|
---|
| 2602 | struct tm time_tm;
|
---|
| 2603 | #endif
|
---|
[132] | 2604 | char AsciiTime[81]; /* GMT time */
|
---|
[1] | 2605 | #ifdef SH_USE_XML
|
---|
| 2606 | static char deftime[] = N_("0000-00-00T00:00:00"); /* default time */
|
---|
| 2607 | #else
|
---|
| 2608 | static char deftime[] = N_("[0000-00-00T00:00:00]"); /* default time */
|
---|
| 2609 | #endif
|
---|
| 2610 |
|
---|
| 2611 | SL_ENTER(_("sh_unix_gmttime"));
|
---|
| 2612 |
|
---|
| 2613 | if (sh_unix_use_localtime == S_FALSE)
|
---|
[131] | 2614 | {
|
---|
| 2615 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
|
---|
| 2616 | time_ptr = gmtime_r (&thetime, &time_tm);
|
---|
| 2617 | #else
|
---|
| 2618 | time_ptr = gmtime (&thetime);
|
---|
| 2619 | #endif
|
---|
| 2620 | }
|
---|
[1] | 2621 | else
|
---|
[131] | 2622 | {
|
---|
| 2623 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
|
---|
| 2624 | time_ptr = localtime_r (&thetime, &time_tm);
|
---|
| 2625 | #else
|
---|
| 2626 | time_ptr = localtime (&thetime);
|
---|
| 2627 | #endif
|
---|
| 2628 | }
|
---|
[1] | 2629 | if (time_ptr != NULL)
|
---|
| 2630 | {
|
---|
| 2631 | status = strftime (AsciiTime, 80,
|
---|
| 2632 | #ifdef SH_USE_XML
|
---|
| 2633 | _("%Y-%m-%dT%H:%M:%S"),
|
---|
| 2634 | #else
|
---|
| 2635 | _("[%Y-%m-%dT%H:%M:%S]"),
|
---|
| 2636 | #endif
|
---|
| 2637 | time_ptr);
|
---|
| 2638 |
|
---|
[132] | 2639 | if ( (status == 0) || (status == 80) )
|
---|
| 2640 | sl_strlcpy(buffer, _(deftime), len);
|
---|
[1] | 2641 | else
|
---|
[132] | 2642 | sl_strlcpy(buffer, AsciiTime, len);
|
---|
| 2643 | SL_RETURN( buffer, _("sh_unix_gmttime"));
|
---|
[1] | 2644 | }
|
---|
| 2645 |
|
---|
| 2646 | /* last resort
|
---|
| 2647 | */
|
---|
[132] | 2648 | sl_strlcpy(buffer, _(deftime), len);
|
---|
| 2649 | SL_RETURN( buffer, _("sh_unix_gmttime"));
|
---|
[1] | 2650 | }
|
---|
| 2651 |
|
---|
| 2652 |
|
---|
[132] | 2653 | char * sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len)
|
---|
[1] | 2654 | {
|
---|
| 2655 | struct passwd * tempres;
|
---|
| 2656 | int status = 0;
|
---|
[264] | 2657 |
|
---|
[131] | 2658 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
|
---|
| 2659 | struct passwd pwd;
|
---|
[227] | 2660 | char * buffer;
|
---|
[131] | 2661 | #endif
|
---|
[132] | 2662 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 2663 |
|
---|
| 2664 | SL_ENTER(_("sh_unix_getUIDdir"));
|
---|
| 2665 |
|
---|
[131] | 2666 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
|
---|
[227] | 2667 | buffer = SH_ALLOC(SH_PWBUF_SIZE);
|
---|
| 2668 | sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
|
---|
[131] | 2669 | #else
|
---|
[1] | 2670 | errno = 0;
|
---|
| 2671 | tempres = sh_getpwuid(uid);
|
---|
| 2672 | status = errno;
|
---|
[131] | 2673 | #endif
|
---|
[1] | 2674 |
|
---|
| 2675 | if (tempres == NULL) {
|
---|
| 2676 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
|
---|
[132] | 2677 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
[1] | 2678 | _("getpwuid"), (long) uid, _("completely missing"));
|
---|
[227] | 2679 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
| 2680 | SH_FREE(buffer);
|
---|
| 2681 | #endif
|
---|
[1] | 2682 | SL_RETURN( NULL, _("sh_unix_getUIDdir"));
|
---|
| 2683 | }
|
---|
| 2684 |
|
---|
| 2685 | if (tempres->pw_dir != NULL) {
|
---|
[132] | 2686 | sl_strlcpy(out, tempres->pw_dir, len);
|
---|
[227] | 2687 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
| 2688 | SH_FREE(buffer);
|
---|
| 2689 | #endif
|
---|
[132] | 2690 | SL_RETURN( out, _("sh_unix_getUIDdir"));
|
---|
[1] | 2691 | } else {
|
---|
| 2692 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
|
---|
[132] | 2693 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
[1] | 2694 | _("getpwuid"), (long) uid, _("pw_dir"));
|
---|
[227] | 2695 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
| 2696 | SH_FREE(buffer);
|
---|
| 2697 | #endif
|
---|
[1] | 2698 | SL_RETURN( NULL, _("sh_unix_getUIDdir"));
|
---|
| 2699 | }
|
---|
| 2700 | }
|
---|
| 2701 |
|
---|
[264] | 2702 | /* ------------------- Caching ----------------*/
|
---|
| 2703 | #include "zAVLTree.h"
|
---|
[132] | 2704 |
|
---|
[264] | 2705 | #define CACHE_GID 0
|
---|
| 2706 | #define CACHE_UID 1
|
---|
| 2707 |
|
---|
| 2708 | struct user_id {
|
---|
| 2709 | char * name;
|
---|
| 2710 | uid_t id;
|
---|
| 2711 | struct user_id * next;
|
---|
| 2712 | };
|
---|
| 2713 |
|
---|
| 2714 | static struct user_id * uid_list = NULL;
|
---|
| 2715 | static struct user_id * gid_list = NULL;
|
---|
| 2716 |
|
---|
| 2717 | SH_MUTEX_STATIC(mutex_cache, PTHREAD_MUTEX_INITIALIZER);
|
---|
| 2718 |
|
---|
| 2719 | static void sh_userid_free(struct user_id * item)
|
---|
| 2720 | {
|
---|
| 2721 | while (item)
|
---|
| 2722 | {
|
---|
| 2723 | struct user_id * user = item;
|
---|
| 2724 | item = item->next;
|
---|
| 2725 |
|
---|
| 2726 | SH_FREE(user->name);
|
---|
| 2727 | SH_FREE(user);
|
---|
| 2728 | }
|
---|
| 2729 | return;
|
---|
| 2730 | }
|
---|
| 2731 |
|
---|
| 2732 | void sh_userid_destroy ()
|
---|
| 2733 | {
|
---|
| 2734 | struct user_id * tmp_uid;
|
---|
| 2735 | struct user_id * tmp_gid;
|
---|
| 2736 |
|
---|
| 2737 | SH_MUTEX_LOCK_UNSAFE(mutex_cache);
|
---|
| 2738 | tmp_gid = gid_list;
|
---|
| 2739 | gid_list = NULL;
|
---|
| 2740 | tmp_uid = uid_list;
|
---|
| 2741 | uid_list = NULL;
|
---|
| 2742 | SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
|
---|
| 2743 |
|
---|
| 2744 | sh_userid_free(tmp_uid);
|
---|
| 2745 | sh_userid_free(tmp_gid);
|
---|
| 2746 | return;
|
---|
| 2747 | }
|
---|
| 2748 |
|
---|
| 2749 | static void sh_userid_additem(struct user_id * list, struct user_id * item)
|
---|
| 2750 | {
|
---|
[383] | 2751 | if (list)
|
---|
| 2752 | {
|
---|
| 2753 | while (list && list->next)
|
---|
| 2754 | list = list->next;
|
---|
| 2755 | list->next = item;
|
---|
| 2756 | }
|
---|
[264] | 2757 | return;
|
---|
| 2758 | }
|
---|
| 2759 |
|
---|
| 2760 | static void sh_userid_add(uid_t id, char * username, int which)
|
---|
| 2761 | {
|
---|
| 2762 | size_t len;
|
---|
| 2763 | struct user_id * user = SH_ALLOC(sizeof(struct user_id));
|
---|
| 2764 |
|
---|
| 2765 | if (username)
|
---|
| 2766 | len = strlen(username) + 1;
|
---|
| 2767 | else
|
---|
| 2768 | len = 1;
|
---|
| 2769 |
|
---|
| 2770 | user->name = SH_ALLOC(len);
|
---|
| 2771 | user->id = id;
|
---|
| 2772 | if (username)
|
---|
| 2773 | sl_strlcpy(user->name, username, len);
|
---|
| 2774 | else
|
---|
| 2775 | user->name[0] = '\0';
|
---|
| 2776 | user->next = NULL;
|
---|
| 2777 |
|
---|
| 2778 | SH_MUTEX_LOCK(mutex_cache);
|
---|
| 2779 | if (which == CACHE_UID)
|
---|
| 2780 | {
|
---|
| 2781 | if (!uid_list)
|
---|
| 2782 | uid_list = user;
|
---|
| 2783 | else
|
---|
| 2784 | sh_userid_additem(uid_list, user);
|
---|
| 2785 | }
|
---|
| 2786 | else
|
---|
| 2787 | {
|
---|
| 2788 | if (!gid_list)
|
---|
| 2789 | gid_list = user;
|
---|
| 2790 | else
|
---|
| 2791 | sh_userid_additem(gid_list, user);
|
---|
| 2792 | }
|
---|
| 2793 | SH_MUTEX_UNLOCK(mutex_cache);
|
---|
| 2794 |
|
---|
| 2795 | return;
|
---|
| 2796 | }
|
---|
| 2797 |
|
---|
| 2798 | static char * sh_userid_search(struct user_id * list, uid_t id)
|
---|
| 2799 | {
|
---|
| 2800 | while (list)
|
---|
| 2801 | {
|
---|
| 2802 | if (list->id == id)
|
---|
| 2803 | return list->name;
|
---|
| 2804 | list = list->next;
|
---|
| 2805 | }
|
---|
| 2806 | return NULL;
|
---|
| 2807 | }
|
---|
| 2808 |
|
---|
| 2809 | static char * sh_userid_get (uid_t id, int which, char * out, size_t len)
|
---|
| 2810 | {
|
---|
| 2811 | char * user = NULL;
|
---|
| 2812 |
|
---|
| 2813 | SH_MUTEX_LOCK_UNSAFE(mutex_cache);
|
---|
| 2814 | if (which == CACHE_UID)
|
---|
| 2815 | user = sh_userid_search(uid_list, id);
|
---|
| 2816 | else
|
---|
| 2817 | user = sh_userid_search(gid_list, id);
|
---|
| 2818 | if (user)
|
---|
| 2819 | {
|
---|
| 2820 | sl_strlcpy(out, user, len);
|
---|
| 2821 | user = out;
|
---|
| 2822 | }
|
---|
| 2823 | SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
|
---|
| 2824 |
|
---|
| 2825 | return user;
|
---|
| 2826 | }
|
---|
| 2827 |
|
---|
| 2828 | /* --------- end caching code --------- */
|
---|
| 2829 |
|
---|
[132] | 2830 | char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len)
|
---|
[1] | 2831 | {
|
---|
| 2832 | struct passwd * tempres;
|
---|
[131] | 2833 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
|
---|
| 2834 | struct passwd pwd;
|
---|
[227] | 2835 | char * buffer;
|
---|
[131] | 2836 | #endif
|
---|
[1] | 2837 | int status = 0;
|
---|
[132] | 2838 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[264] | 2839 | char * tmp;
|
---|
[1] | 2840 |
|
---|
| 2841 | SL_ENTER(_("sh_unix_getUIDname"));
|
---|
| 2842 |
|
---|
[264] | 2843 | tmp = sh_userid_get(uid, CACHE_UID, out, len);
|
---|
| 2844 |
|
---|
| 2845 | if (tmp)
|
---|
| 2846 | {
|
---|
| 2847 | if (tmp[0] != '\0')
|
---|
| 2848 | {
|
---|
| 2849 | SL_RETURN( out, _("sh_unix_getUIDname"));
|
---|
| 2850 | }
|
---|
| 2851 | else
|
---|
| 2852 | {
|
---|
| 2853 | SL_RETURN( NULL, _("sh_unix_getUIDname"));
|
---|
| 2854 | }
|
---|
[132] | 2855 | }
|
---|
[1] | 2856 |
|
---|
[131] | 2857 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
|
---|
[227] | 2858 | buffer = SH_ALLOC(SH_PWBUF_SIZE);
|
---|
| 2859 | sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
|
---|
[131] | 2860 | #else
|
---|
[1] | 2861 | errno = 0;
|
---|
| 2862 | tempres = sh_getpwuid(uid);
|
---|
| 2863 | status = errno;
|
---|
[131] | 2864 | #endif
|
---|
[1] | 2865 |
|
---|
[264] | 2866 | if (tempres == NULL)
|
---|
| 2867 | {
|
---|
| 2868 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
|
---|
| 2869 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 2870 | _("getpwuid"), (long) uid, _("completely missing"));
|
---|
[227] | 2871 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2872 | SH_FREE(buffer);
|
---|
[227] | 2873 | #endif
|
---|
[264] | 2874 | sh_userid_add(uid, NULL, CACHE_UID);
|
---|
| 2875 | SL_RETURN( NULL, _("sh_unix_getUIDname"));
|
---|
| 2876 | }
|
---|
[1] | 2877 |
|
---|
| 2878 |
|
---|
[264] | 2879 | if (tempres->pw_name != NULL)
|
---|
| 2880 | {
|
---|
| 2881 |
|
---|
| 2882 | sl_strlcpy(out, tempres->pw_name, len);
|
---|
| 2883 | sh_userid_add(uid, out, CACHE_UID);
|
---|
| 2884 |
|
---|
[227] | 2885 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2886 | SH_FREE(buffer);
|
---|
[227] | 2887 | #endif
|
---|
[264] | 2888 |
|
---|
| 2889 | SL_RETURN( out, _("sh_unix_getUIDname"));
|
---|
| 2890 | }
|
---|
| 2891 | else
|
---|
| 2892 | {
|
---|
| 2893 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
|
---|
| 2894 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 2895 | _("getpwuid"), (long) uid, _("pw_user"));
|
---|
[227] | 2896 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2897 | SH_FREE(buffer);
|
---|
[227] | 2898 | #endif
|
---|
[264] | 2899 | SL_RETURN( NULL, _("sh_unix_getUIDname"));
|
---|
| 2900 | }
|
---|
| 2901 | /* notreached */
|
---|
[1] | 2902 | }
|
---|
| 2903 |
|
---|
[132] | 2904 | char * sh_unix_getGIDname (int level, gid_t gid, char * out, size_t len)
|
---|
[1] | 2905 | {
|
---|
| 2906 | struct group * tempres;
|
---|
| 2907 | int status = 0;
|
---|
[264] | 2908 |
|
---|
[131] | 2909 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
| 2910 | struct group grp;
|
---|
[227] | 2911 | char * buffer;
|
---|
[131] | 2912 | #endif
|
---|
[132] | 2913 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[264] | 2914 | char * tmp;
|
---|
[131] | 2915 |
|
---|
[1] | 2916 | SL_ENTER(_("sh_unix_getGIDname"));
|
---|
| 2917 |
|
---|
[264] | 2918 | tmp = sh_userid_get((uid_t)gid, CACHE_GID, out, len);
|
---|
| 2919 |
|
---|
| 2920 | if (tmp)
|
---|
| 2921 | {
|
---|
| 2922 | if (tmp[0] != '\0')
|
---|
| 2923 | {
|
---|
| 2924 | SL_RETURN( out, _("sh_unix_getGIDname"));
|
---|
| 2925 | }
|
---|
| 2926 | else
|
---|
| 2927 | {
|
---|
| 2928 | SL_RETURN( NULL, _("sh_unix_getGIDname"));
|
---|
| 2929 | }
|
---|
[132] | 2930 | }
|
---|
| 2931 |
|
---|
[131] | 2932 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[227] | 2933 | buffer = SH_ALLOC(SH_GRBUF_SIZE);
|
---|
| 2934 | status = sh_getgrgid_r(gid, &grp, buffer, SH_GRBUF_SIZE, &tempres);
|
---|
[131] | 2935 | #else
|
---|
[1] | 2936 | errno = 0;
|
---|
| 2937 | tempres = sh_getgrgid(gid);
|
---|
| 2938 | status = errno;
|
---|
[131] | 2939 | #endif
|
---|
[1] | 2940 |
|
---|
[428] | 2941 | if (status == ERANGE)
|
---|
| 2942 | {
|
---|
| 2943 | static int seen = 0;
|
---|
| 2944 |
|
---|
| 2945 | if (seen == 0)
|
---|
| 2946 | {
|
---|
| 2947 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
|
---|
| 2948 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 2949 | _("getgrgid"), (long) gid, _("line too long in group entry"));
|
---|
| 2950 | ++seen;
|
---|
| 2951 | }
|
---|
| 2952 |
|
---|
| 2953 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
| 2954 | SH_FREE(buffer);
|
---|
| 2955 | #endif
|
---|
| 2956 |
|
---|
| 2957 | sh_userid_add(gid, NULL, CACHE_GID);
|
---|
| 2958 | SL_RETURN( NULL, _("sh_unix_getGIDname"));
|
---|
| 2959 | }
|
---|
| 2960 |
|
---|
[264] | 2961 | if (tempres == NULL)
|
---|
| 2962 | {
|
---|
| 2963 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
|
---|
| 2964 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 2965 | _("getgrgid"), (long) gid, _("completely missing"));
|
---|
[1] | 2966 |
|
---|
[227] | 2967 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2968 | SH_FREE(buffer);
|
---|
[227] | 2969 | #endif
|
---|
[1] | 2970 |
|
---|
[264] | 2971 | sh_userid_add(gid, NULL, CACHE_GID);
|
---|
| 2972 | SL_RETURN( NULL, _("sh_unix_getGIDname"));
|
---|
| 2973 | }
|
---|
| 2974 |
|
---|
| 2975 | if (tempres->gr_name != NULL)
|
---|
| 2976 | {
|
---|
| 2977 |
|
---|
| 2978 | sl_strlcpy(out, tempres->gr_name, len);
|
---|
| 2979 | sh_userid_add((uid_t)gid, out, CACHE_GID);
|
---|
| 2980 |
|
---|
[227] | 2981 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2982 | SH_FREE(buffer);
|
---|
[227] | 2983 | #endif
|
---|
[264] | 2984 |
|
---|
| 2985 | SL_RETURN( out, _("sh_unix_getGIDname"));
|
---|
| 2986 | }
|
---|
| 2987 | else
|
---|
| 2988 | {
|
---|
| 2989 | sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
|
---|
| 2990 | sh_error_message(status, errbuf, sizeof(errbuf)),
|
---|
| 2991 | _("getgrgid"), (long) gid, _("gr_name"));
|
---|
| 2992 |
|
---|
[227] | 2993 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
|
---|
[264] | 2994 | SH_FREE(buffer);
|
---|
[227] | 2995 | #endif
|
---|
[264] | 2996 |
|
---|
| 2997 | SL_RETURN( NULL, _("sh_unix_getGIDname"));
|
---|
| 2998 | }
|
---|
| 2999 | /* notreached */
|
---|
[1] | 3000 | }
|
---|
| 3001 |
|
---|
| 3002 | int sh_unix_getUser ()
|
---|
| 3003 | {
|
---|
| 3004 | char * p;
|
---|
| 3005 | uid_t seuid, sruid;
|
---|
[132] | 3006 | char user[USER_MAX];
|
---|
| 3007 | char dir[SH_PATHBUF];
|
---|
[1] | 3008 |
|
---|
| 3009 | SL_ENTER(_("sh_unix_getUser"));
|
---|
| 3010 |
|
---|
| 3011 | seuid = geteuid();
|
---|
| 3012 |
|
---|
| 3013 | sh.effective.uid = seuid;
|
---|
| 3014 |
|
---|
[132] | 3015 | p = sh_unix_getUIDdir (SH_ERR_ERR, seuid, dir, sizeof(dir));
|
---|
[1] | 3016 |
|
---|
| 3017 | if (p == NULL)
|
---|
| 3018 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3019 | else
|
---|
| 3020 | {
|
---|
| 3021 | if (sl_strlen(p) >= SH_PATHBUF) {
|
---|
| 3022 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
|
---|
| 3023 | _("getpwuid"), (long) seuid, _("pw_home"));
|
---|
| 3024 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3025 | } else {
|
---|
| 3026 | sl_strlcpy ( sh.effective.home, p, SH_PATHBUF);
|
---|
| 3027 | }
|
---|
| 3028 | }
|
---|
| 3029 |
|
---|
| 3030 | sruid = getuid();
|
---|
| 3031 |
|
---|
| 3032 | sh.real.uid = sruid;
|
---|
| 3033 |
|
---|
[132] | 3034 | p = sh_unix_getUIDname (SH_ERR_ERR, sruid, user, sizeof(user));
|
---|
[1] | 3035 | if (p == NULL)
|
---|
| 3036 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3037 | else
|
---|
| 3038 | {
|
---|
| 3039 | if (sl_strlen(p) >= USER_MAX) {
|
---|
| 3040 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
|
---|
| 3041 | _("getpwuid"), (long) sruid, _("pw_user"));
|
---|
| 3042 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3043 | } else {
|
---|
| 3044 | sl_strlcpy ( sh.real.user, p, USER_MAX);
|
---|
| 3045 | }
|
---|
| 3046 | }
|
---|
| 3047 |
|
---|
[132] | 3048 | p = sh_unix_getUIDdir (SH_ERR_ERR, sruid, dir, sizeof(dir));
|
---|
[1] | 3049 |
|
---|
| 3050 | if (p == NULL)
|
---|
| 3051 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3052 | else
|
---|
| 3053 | {
|
---|
| 3054 | if (sl_strlen(p) >= SH_PATHBUF) {
|
---|
| 3055 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
|
---|
| 3056 | _("getpwuid"), (long) sruid, _("pw_home"));
|
---|
| 3057 | SL_RETURN((-1), _("sh_unix_getUser"));
|
---|
| 3058 | } else {
|
---|
| 3059 | sl_strlcpy ( sh.real.home, p, SH_PATHBUF);
|
---|
| 3060 | }
|
---|
| 3061 | }
|
---|
| 3062 |
|
---|
| 3063 | SL_RETURN((0), _("sh_unix_getUser"));
|
---|
| 3064 |
|
---|
| 3065 | /* notreached */
|
---|
| 3066 | }
|
---|
| 3067 |
|
---|
| 3068 |
|
---|
| 3069 | int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline)
|
---|
| 3070 | {
|
---|
| 3071 | register int count;
|
---|
| 3072 | register int n = 0;
|
---|
| 3073 | char c;
|
---|
| 3074 |
|
---|
| 3075 | SL_ENTER(_("sh_unix_getline"));
|
---|
| 3076 |
|
---|
| 3077 | if (sizeofline < 2) {
|
---|
[34] | 3078 | line[0] = '\0';
|
---|
[1] | 3079 | SL_RETURN((0), _("sh_unix_getline"));
|
---|
| 3080 | }
|
---|
| 3081 |
|
---|
[34] | 3082 | --sizeofline;
|
---|
| 3083 |
|
---|
[1] | 3084 | while (n < sizeofline) {
|
---|
| 3085 |
|
---|
| 3086 | count = sl_read (fd, &c, 1);
|
---|
| 3087 |
|
---|
| 3088 | /* end of file
|
---|
| 3089 | */
|
---|
| 3090 | if (count < 1) {
|
---|
| 3091 | line[n] = '\0';
|
---|
| 3092 | n = -1;
|
---|
| 3093 | break;
|
---|
| 3094 | }
|
---|
| 3095 |
|
---|
| 3096 | if (/* c != '\0' && */ c != '\n') {
|
---|
| 3097 | line[n] = c;
|
---|
| 3098 | ++n;
|
---|
| 3099 | } else if (c == '\n') {
|
---|
| 3100 | if (n > 0) {
|
---|
| 3101 | line[n] = '\0';
|
---|
| 3102 | break;
|
---|
| 3103 | } else {
|
---|
| 3104 | line[n] = '\n'; /* get newline only if only char on line */
|
---|
| 3105 | ++n;
|
---|
| 3106 | line[n] = '\0';
|
---|
| 3107 | break;
|
---|
| 3108 | }
|
---|
| 3109 | } else {
|
---|
| 3110 | line[n] = '\0';
|
---|
| 3111 | break;
|
---|
| 3112 | }
|
---|
| 3113 |
|
---|
| 3114 | }
|
---|
| 3115 |
|
---|
| 3116 |
|
---|
| 3117 | line[sizeofline] = '\0'; /* make sure line is terminated */
|
---|
| 3118 | SL_RETURN((n), _("sh_unix_getline"));
|
---|
| 3119 | }
|
---|
| 3120 |
|
---|
| 3121 |
|
---|
| 3122 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
| 3123 |
|
---|
| 3124 | /**************************************************************
|
---|
| 3125 | *
|
---|
| 3126 | * --- FILE INFO ---
|
---|
| 3127 | *
|
---|
| 3128 | **************************************************************/
|
---|
| 3129 |
|
---|
[10] | 3130 | #if (defined(__linux__) && (defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H))) || defined(HAVE_STAT_FLAGS)
|
---|
[1] | 3131 |
|
---|
[10] | 3132 | #if defined(__linux__)
|
---|
[1] | 3133 |
|
---|
| 3134 | /* --- Determine ext2fs file attributes. ---
|
---|
| 3135 | */
|
---|
| 3136 | #include <sys/ioctl.h>
|
---|
| 3137 | #if defined(HAVE_EXT2FS_EXT2_FS_H)
|
---|
| 3138 | #include <ext2fs/ext2_fs.h>
|
---|
| 3139 | #else
|
---|
| 3140 | #include <linux/ext2_fs.h>
|
---|
| 3141 | #endif
|
---|
| 3142 |
|
---|
[10] | 3143 | /* __linux__ includes */
|
---|
| 3144 | #endif
|
---|
| 3145 |
|
---|
[1] | 3146 | static
|
---|
| 3147 | int sh_unix_getinfo_attr (char * name,
|
---|
| 3148 | unsigned long * flags,
|
---|
| 3149 | char * c_attr,
|
---|
[10] | 3150 | int fd, struct stat * buf)
|
---|
[1] | 3151 | {
|
---|
| 3152 |
|
---|
| 3153 | /* TAKEN FROM:
|
---|
| 3154 | *
|
---|
| 3155 | * lsattr.c - List file attributes on an ext2 file system
|
---|
| 3156 | *
|
---|
| 3157 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
|
---|
| 3158 | * Laboratoire MASI, Institut Blaise Pascal
|
---|
| 3159 | * Universite Pierre et Marie Curie (Paris VI)
|
---|
| 3160 | *
|
---|
| 3161 | * This file can be redistributed under the terms of the GNU General
|
---|
| 3162 | * Public License
|
---|
| 3163 | */
|
---|
| 3164 |
|
---|
| 3165 | #ifdef HAVE_STAT_FLAGS
|
---|
| 3166 |
|
---|
| 3167 | SL_ENTER(_("sh_unix_getinfo_attr"));
|
---|
| 3168 |
|
---|
| 3169 | *flags = 0;
|
---|
| 3170 |
|
---|
[10] | 3171 | /* cast to void to avoid compiler warning about unused parameters */
|
---|
| 3172 | (void) fd;
|
---|
| 3173 | (void) name;
|
---|
[1] | 3174 |
|
---|
[10] | 3175 | #ifdef UF_NODUMP
|
---|
| 3176 | if (buf->st_flags & UF_NODUMP) {
|
---|
| 3177 | *flags |= UF_NODUMP;
|
---|
| 3178 | c_attr[0] = 'd';
|
---|
| 3179 | }
|
---|
| 3180 | #endif
|
---|
[1] | 3181 | #ifdef UF_IMMUTABLE
|
---|
[10] | 3182 | if (buf->st_flags & UF_IMMUTABLE) {
|
---|
| 3183 | *flags |= UF_IMMUTABLE;
|
---|
| 3184 | c_attr[1] = 'i';
|
---|
| 3185 | }
|
---|
[1] | 3186 | #endif
|
---|
| 3187 | #ifdef UF_APPEND
|
---|
[10] | 3188 | if (buf->st_flags & UF_APPEND) {
|
---|
| 3189 | *flags |= UF_APPEND;
|
---|
| 3190 | c_attr[2] = 'a';
|
---|
| 3191 | }
|
---|
[1] | 3192 | #endif
|
---|
[10] | 3193 | #ifdef UF_NOUNLINK
|
---|
| 3194 | if (buf->st_flags & UF_NOUNLINK) {
|
---|
| 3195 | *flags |= UF_NOUNLINK;
|
---|
| 3196 | c_attr[3] = 'u';
|
---|
| 3197 | }
|
---|
[1] | 3198 | #endif
|
---|
[10] | 3199 | #ifdef UF_OPAQUE
|
---|
| 3200 | if (buf->st_flags & UF_OPAQUE) {
|
---|
| 3201 | *flags |= UF_OPAQUE;
|
---|
| 3202 | c_attr[4] = 'o';
|
---|
| 3203 | }
|
---|
| 3204 | #endif
|
---|
| 3205 | #ifdef SF_ARCHIVED
|
---|
| 3206 | if (buf->st_flags & SF_ARCHIVED) {
|
---|
| 3207 | *flags |= SF_ARCHIVED;
|
---|
| 3208 | c_attr[5] = 'R';
|
---|
| 3209 | }
|
---|
| 3210 |
|
---|
| 3211 | #endif
|
---|
| 3212 | #ifdef SF_IMMUTABLE
|
---|
| 3213 | if (buf->st_flags & SF_IMMUTABLE) {
|
---|
| 3214 | *flags |= SF_IMMUTABLE;
|
---|
| 3215 | c_attr[6] = 'I';
|
---|
| 3216 | }
|
---|
| 3217 | #endif
|
---|
| 3218 | #ifdef SF_APPEND
|
---|
| 3219 | if (buf->st_flags & SF_APPEND) {
|
---|
| 3220 | *flags |= SF_APPEND;
|
---|
| 3221 | c_attr[7] = 'A';
|
---|
| 3222 | }
|
---|
| 3223 | #endif
|
---|
| 3224 | #ifdef SF_NOUNLINK
|
---|
| 3225 | if (buf->st_flags & SF_NOUNLINK) {
|
---|
| 3226 | *flags |= SF_NOUNLINK;
|
---|
| 3227 | c_attr[8] = 'U';
|
---|
| 3228 | }
|
---|
| 3229 | #endif
|
---|
[1] | 3230 |
|
---|
[10] | 3231 | /* ! HAVE_STAT_FLAGS */
|
---|
[1] | 3232 | #else
|
---|
| 3233 |
|
---|
| 3234 | #ifdef HAVE_EXT2_IOCTLS
|
---|
| 3235 | int /* fd, */ r, f;
|
---|
| 3236 |
|
---|
| 3237 | SL_ENTER(_("sh_unix_getinfo_attr"));
|
---|
| 3238 |
|
---|
| 3239 | *flags = 0;
|
---|
[10] | 3240 | (void) buf;
|
---|
[1] | 3241 |
|
---|
| 3242 | /* open() -> aud_open() R.Wichmann
|
---|
| 3243 | fd = aud_open (FIL__, __LINE__, SL_YESPRIV, name, O_RDONLY|O_NONBLOCK, 0);
|
---|
| 3244 | */
|
---|
| 3245 |
|
---|
| 3246 | if (fd == -1 || name == NULL)
|
---|
| 3247 | SL_RETURN(-1, _("sh_unix_getinfo_attr"));
|
---|
| 3248 |
|
---|
| 3249 |
|
---|
| 3250 | r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
|
---|
[252] | 3251 | /* sl_close_fd (FIL__, __LINE__, fd); */
|
---|
[1] | 3252 |
|
---|
| 3253 | if (r == -1)
|
---|
| 3254 | SL_RETURN(-1, _("sh_unix_getinfo_attr"));
|
---|
| 3255 |
|
---|
[10] | 3256 | if (f == 0)
|
---|
| 3257 | SL_RETURN(0, _("sh_unix_getinfo_attr"));
|
---|
| 3258 |
|
---|
[1] | 3259 | *flags = f;
|
---|
| 3260 |
|
---|
| 3261 | /* ! HAVE_EXT2_IOCTLS */
|
---|
| 3262 | #else
|
---|
| 3263 |
|
---|
| 3264 | SL_ENTER(_("sh_unix_getinfo_attr"));
|
---|
| 3265 |
|
---|
| 3266 | *flags = 0; /* modified by R.Wichmann */
|
---|
| 3267 |
|
---|
| 3268 | /* ! HAVE_EXT2_IOCTLS */
|
---|
| 3269 | #endif
|
---|
| 3270 | /*
|
---|
| 3271 | * END
|
---|
| 3272 | *
|
---|
| 3273 | * lsattr.c - List file attributes on an ext2 file system
|
---|
| 3274 | */
|
---|
| 3275 |
|
---|
[10] | 3276 | if (*flags == 0)
|
---|
| 3277 | goto theend;
|
---|
| 3278 |
|
---|
[1] | 3279 | #ifdef EXT2_SECRM_FL
|
---|
| 3280 | if ( (*flags & EXT2_SECRM_FL) != 0 ) c_attr[0] = 's';
|
---|
| 3281 | #endif
|
---|
| 3282 | #ifdef EXT2_UNRM_FL
|
---|
| 3283 | if ( (*flags & EXT2_UNRM_FL) != 0 ) c_attr[1] = 'u';
|
---|
| 3284 | #endif
|
---|
| 3285 | #ifdef EXT2_SYNC_FL
|
---|
| 3286 | if ( (*flags & EXT2_SYNC_FL) != 0 ) c_attr[2] = 'S';
|
---|
| 3287 | #endif
|
---|
| 3288 | #ifdef EXT2_IMMUTABLE_FL
|
---|
| 3289 | if ( (*flags & EXT2_IMMUTABLE_FL) != 0) c_attr[3] = 'i';
|
---|
| 3290 | #endif
|
---|
| 3291 | #ifdef EXT2_APPEND_FL
|
---|
| 3292 | if ( (*flags & EXT2_APPEND_FL) != 0 ) c_attr[4] = 'a';
|
---|
| 3293 | #endif
|
---|
| 3294 | #ifdef EXT2_NODUMP_FL
|
---|
| 3295 | if ( (*flags & EXT2_NODUMP_FL) != 0 ) c_attr[5] = 'd';
|
---|
| 3296 | #endif
|
---|
| 3297 | #ifdef EXT2_NOATIME_FL
|
---|
| 3298 | if ( (*flags & EXT2_NOATIME_FL) != 0) c_attr[6] = 'A';
|
---|
| 3299 | #endif
|
---|
| 3300 | #ifdef EXT2_COMPR_FL
|
---|
| 3301 | if ( (*flags & EXT2_COMPR_FL) != 0 ) c_attr[7] = 'c';
|
---|
| 3302 | #endif
|
---|
[10] | 3303 |
|
---|
| 3304 | #ifdef EXT2_TOPDIR_FL
|
---|
| 3305 | if ( (*flags & EXT2_TOPDIR_FL) != 0 ) c_attr[8] = 'T';
|
---|
[1] | 3306 | #endif
|
---|
[10] | 3307 | #ifdef EXT2_DIRSYNC_FL
|
---|
| 3308 | if ( (*flags & EXT2_DIRSYNC_FL) != 0 ) c_attr[9] = 'D';
|
---|
[1] | 3309 | #endif
|
---|
[10] | 3310 | #ifdef EXT2_NOTAIL_FL
|
---|
| 3311 | if ( (*flags & EXT2_NOTAIL_FL) != 0 ) c_attr[10] = 't';
|
---|
[1] | 3312 | #endif
|
---|
[10] | 3313 | #ifdef EXT2_JOURNAL_DATA_FL
|
---|
| 3314 | if ( (*flags & EXT2_JOURNAL_DATA_FL) != 0) c_attr[11] = 'j';
|
---|
[1] | 3315 | #endif
|
---|
| 3316 |
|
---|
[10] | 3317 | theend:
|
---|
| 3318 | /* ext2 */
|
---|
| 3319 | #endif
|
---|
| 3320 |
|
---|
[1] | 3321 | c_attr[12] = '\0';
|
---|
| 3322 |
|
---|
| 3323 | SL_RETURN(0, _("sh_unix_getinfo_attr"));
|
---|
| 3324 | }
|
---|
| 3325 |
|
---|
[10] | 3326 | /* defined(__linux__) || defined(HAVE_STAT_FLAGS) */
|
---|
[1] | 3327 | #endif
|
---|
| 3328 |
|
---|
| 3329 | /* determine file type
|
---|
| 3330 | */
|
---|
| 3331 | static
|
---|
| 3332 | int sh_unix_getinfo_type (struct stat * buf,
|
---|
| 3333 | ShFileType * type,
|
---|
| 3334 | char * c_mode)
|
---|
| 3335 | {
|
---|
| 3336 | SL_ENTER(_("sh_unix_getinfo_type"));
|
---|
| 3337 |
|
---|
| 3338 | if ( S_ISREG(buf->st_mode) ) {
|
---|
| 3339 | (*type) = SH_FILE_REGULAR;
|
---|
| 3340 | c_mode[0] = '-';
|
---|
| 3341 | }
|
---|
| 3342 | else if ( S_ISLNK(buf->st_mode) ) {
|
---|
| 3343 | (*type) = SH_FILE_SYMLINK;
|
---|
| 3344 | c_mode[0] = 'l';
|
---|
| 3345 | }
|
---|
| 3346 | else if ( S_ISDIR(buf->st_mode) ) {
|
---|
| 3347 | (*type) = SH_FILE_DIRECTORY;
|
---|
| 3348 | c_mode[0] = 'd';
|
---|
| 3349 | }
|
---|
| 3350 | else if ( S_ISCHR(buf->st_mode) ) {
|
---|
| 3351 | (*type) = SH_FILE_CDEV;
|
---|
| 3352 | c_mode[0] = 'c';
|
---|
| 3353 | }
|
---|
| 3354 | else if ( S_ISBLK(buf->st_mode) ) {
|
---|
| 3355 | (*type) = SH_FILE_BDEV;
|
---|
| 3356 | c_mode[0] = 'b';
|
---|
| 3357 | }
|
---|
| 3358 | else if ( S_ISFIFO(buf->st_mode) ) {
|
---|
| 3359 | (*type) = SH_FILE_FIFO;
|
---|
| 3360 | c_mode[0] = '|';
|
---|
| 3361 | }
|
---|
| 3362 | else if ( S_ISSOCK(buf->st_mode) ) {
|
---|
| 3363 | (*type) = SH_FILE_SOCKET;
|
---|
| 3364 | c_mode[0] = 's';
|
---|
| 3365 | }
|
---|
[40] | 3366 | else if ( S_ISDOOR(buf->st_mode) ) {
|
---|
| 3367 | (*type) = SH_FILE_DOOR;
|
---|
| 3368 | c_mode[0] = 'D';
|
---|
| 3369 | }
|
---|
| 3370 | else if ( S_ISPORT(buf->st_mode) ) {
|
---|
| 3371 | (*type) = SH_FILE_PORT;
|
---|
| 3372 | c_mode[0] = 'P';
|
---|
| 3373 | }
|
---|
[1] | 3374 | else {
|
---|
| 3375 | (*type) = SH_FILE_UNKNOWN;
|
---|
| 3376 | c_mode[0] = '?';
|
---|
| 3377 | }
|
---|
| 3378 |
|
---|
| 3379 | SL_RETURN(0, _("sh_unix_getinfo_type"));
|
---|
| 3380 | }
|
---|
| 3381 |
|
---|
| 3382 | int sh_unix_get_ftype(char * fullpath)
|
---|
| 3383 | {
|
---|
[149] | 3384 | char c_mode[CMODE_SIZE];
|
---|
[1] | 3385 | struct stat buf;
|
---|
| 3386 | ShFileType type;
|
---|
| 3387 | int res;
|
---|
| 3388 |
|
---|
| 3389 | SL_ENTER(_("sh_unix_get_ftype"));
|
---|
| 3390 |
|
---|
| 3391 | res = retry_lstat(FIL__, __LINE__, fullpath, &buf);
|
---|
| 3392 |
|
---|
| 3393 | if (res < 0)
|
---|
| 3394 | SL_RETURN(SH_FILE_UNKNOWN, _("sh_unix_getinfo_type"));
|
---|
| 3395 |
|
---|
| 3396 | sh_unix_getinfo_type (&buf, &type, c_mode);
|
---|
| 3397 |
|
---|
| 3398 | SL_RETURN(type, _("sh_unix_get_ftype"));
|
---|
| 3399 | }
|
---|
| 3400 |
|
---|
| 3401 |
|
---|
| 3402 | static
|
---|
| 3403 | int sh_unix_getinfo_mode (struct stat *buf,
|
---|
| 3404 | unsigned int * mode,
|
---|
| 3405 | char * c_mode)
|
---|
| 3406 | {
|
---|
| 3407 |
|
---|
| 3408 | SL_ENTER(_("sh_unix_getinfo_mode"));
|
---|
| 3409 |
|
---|
| 3410 | (*mode) = buf->st_mode;
|
---|
| 3411 |
|
---|
| 3412 | /* make 'ls'-like string */
|
---|
| 3413 |
|
---|
| 3414 | if ( (buf->st_mode & S_IRUSR) != 0 ) c_mode[1] = 'r';
|
---|
| 3415 | if ( (buf->st_mode & S_IWUSR) != 0 ) c_mode[2] = 'w';
|
---|
| 3416 | if ( (buf->st_mode & S_IXUSR) != 0 ) {
|
---|
| 3417 | if ((buf->st_mode & S_ISUID) != 0 ) c_mode[3] = 's';
|
---|
| 3418 | else c_mode[3] = 'x';
|
---|
| 3419 | } else {
|
---|
| 3420 | if ((buf->st_mode & S_ISUID) != 0 ) c_mode[3] = 'S';
|
---|
| 3421 | }
|
---|
| 3422 |
|
---|
| 3423 | if ( (buf->st_mode & S_IRGRP) != 0 ) c_mode[4] = 'r';
|
---|
| 3424 | if ( (buf->st_mode & S_IWGRP) != 0 ) c_mode[5] = 'w';
|
---|
| 3425 | if ( (buf->st_mode & S_IXGRP) != 0 ) {
|
---|
| 3426 | if ((buf->st_mode & S_ISGID) != 0 ) c_mode[6] = 's';
|
---|
| 3427 | else c_mode[6] = 'x';
|
---|
| 3428 | } else {
|
---|
| 3429 | if ((buf->st_mode & S_ISGID) != 0 ) c_mode[6] = 'S';
|
---|
| 3430 | }
|
---|
| 3431 |
|
---|
| 3432 | if ( (buf->st_mode & S_IROTH) != 0 ) c_mode[7] = 'r';
|
---|
| 3433 | if ( (buf->st_mode & S_IWOTH) != 0 ) c_mode[8] = 'w';
|
---|
| 3434 | #ifdef S_ISVTX /* not POSIX */
|
---|
| 3435 | if ( (buf->st_mode & S_IXOTH) != 0 ) {
|
---|
| 3436 | if ((buf->st_mode & S_ISVTX) != 0 ) c_mode[9] = 't';
|
---|
| 3437 | else c_mode[9] = 'x';
|
---|
| 3438 | } else {
|
---|
| 3439 | if ((buf->st_mode & S_ISVTX) != 0 ) c_mode[9] = 'T';
|
---|
| 3440 | }
|
---|
| 3441 | #else
|
---|
| 3442 | if ( (buf->st_mode & S_IXOTH) != 0 ) c_mode[9] = 'x';
|
---|
| 3443 | #endif
|
---|
| 3444 |
|
---|
| 3445 | SL_RETURN(0, _("sh_unix_getinfo_mode"));
|
---|
| 3446 | }
|
---|
| 3447 |
|
---|
| 3448 |
|
---|
| 3449 | long IO_Limit = 0;
|
---|
| 3450 |
|
---|
| 3451 | void sh_unix_io_pause ()
|
---|
| 3452 | {
|
---|
| 3453 | long runtime;
|
---|
| 3454 | float someval;
|
---|
| 3455 | unsigned long sometime;
|
---|
| 3456 |
|
---|
| 3457 | if (IO_Limit == 0)
|
---|
| 3458 | {
|
---|
| 3459 | return;
|
---|
| 3460 | }
|
---|
| 3461 | else
|
---|
| 3462 | {
|
---|
| 3463 | runtime = (long) (time(NULL) - sh.statistics.time_start);
|
---|
| 3464 |
|
---|
| 3465 | if (runtime > 0 && (long)(sh.statistics.bytes_hashed/runtime) > IO_Limit)
|
---|
| 3466 | {
|
---|
| 3467 | someval = sh.statistics.bytes_hashed - (IO_Limit * runtime);
|
---|
| 3468 | someval /= (float) IO_Limit;
|
---|
| 3469 | if (someval < 1.0)
|
---|
| 3470 | {
|
---|
| 3471 | someval *= 1000; /* milliseconds in a second */
|
---|
| 3472 | sometime = (unsigned long) someval;
|
---|
| 3473 | retry_msleep(0, sometime);
|
---|
| 3474 | }
|
---|
| 3475 | else
|
---|
| 3476 | {
|
---|
| 3477 | sometime = (unsigned long) someval;
|
---|
| 3478 | retry_msleep (sometime, 0);
|
---|
| 3479 | }
|
---|
| 3480 | }
|
---|
| 3481 | }
|
---|
| 3482 | return;
|
---|
| 3483 | }
|
---|
| 3484 |
|
---|
[22] | 3485 | int sh_unix_set_io_limit (const char * c)
|
---|
[1] | 3486 | {
|
---|
| 3487 | long val;
|
---|
| 3488 |
|
---|
| 3489 | SL_ENTER(_("sh_unix_set_io_limit"));
|
---|
| 3490 |
|
---|
| 3491 | val = strtol (c, (char **)NULL, 10);
|
---|
| 3492 | if (val < 0)
|
---|
| 3493 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 3494 | _("set I/O limit"), c);
|
---|
| 3495 |
|
---|
| 3496 | val = (val < 0 ? 0 : val);
|
---|
| 3497 |
|
---|
| 3498 | IO_Limit = val * 1024;
|
---|
| 3499 | SL_RETURN( 0, _("sh_unix_set_io_limit"));
|
---|
| 3500 | }
|
---|
| 3501 |
|
---|
| 3502 | /* obtain file info
|
---|
| 3503 | */
|
---|
| 3504 | extern int flag_err_debug;
|
---|
| 3505 |
|
---|
| 3506 | #include "sh_ignore.h"
|
---|
| 3507 |
|
---|
[425] | 3508 | int sh_unix_checksum_size (char * filename, off_t size, int is_max_size,
|
---|
[481] | 3509 | char * fileHash, int alert_timeout, SL_TICKET fd, unsigned long mask)
|
---|
[19] | 3510 | {
|
---|
[227] | 3511 | file_type * tmpFile;
|
---|
[19] | 3512 | int status;
|
---|
| 3513 |
|
---|
| 3514 | SL_ENTER(_("sh_unix_checksum_size"));
|
---|
| 3515 |
|
---|
[227] | 3516 | tmpFile = SH_ALLOC(sizeof(file_type));
|
---|
| 3517 | tmpFile->link_path = NULL;
|
---|
[169] | 3518 |
|
---|
[59] | 3519 | if (sh.flag.checkSum != SH_CHECK_INIT)
|
---|
| 3520 | {
|
---|
| 3521 | /* lookup file in database */
|
---|
[425] | 3522 | if (is_max_size == S_TRUE) {
|
---|
| 3523 | status = sh_hash_get_it (filename, tmpFile, NULL);
|
---|
| 3524 | if ((status != 0) || (tmpFile->size > size)) {
|
---|
| 3525 | goto out;
|
---|
| 3526 | }
|
---|
| 3527 | } else {
|
---|
| 3528 | tmpFile->size = size;
|
---|
[59] | 3529 | }
|
---|
| 3530 | }
|
---|
| 3531 | else
|
---|
| 3532 | {
|
---|
[425] | 3533 | tmpFile->size = size;
|
---|
[59] | 3534 | }
|
---|
[19] | 3535 |
|
---|
[425] | 3536 | /* if last <= current get checksum */
|
---|
| 3537 | if (tmpFile->size <= size)
|
---|
[19] | 3538 | {
|
---|
[133] | 3539 | char hashbuf[KEYBUF_SIZE];
|
---|
[227] | 3540 | UINT64 local_length = (UINT64) (tmpFile->size < 0 ? 0 : tmpFile->size);
|
---|
[481] | 3541 | if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(mask);
|
---|
[22] | 3542 | sl_strlcpy(fileHash,
|
---|
[153] | 3543 | sh_tiger_generic_hash (filename, fd, &(local_length),
|
---|
[133] | 3544 | alert_timeout, hashbuf, sizeof(hashbuf)),
|
---|
[59] | 3545 | KEY_LEN+1);
|
---|
[68] | 3546 |
|
---|
[107] | 3547 | /* return */
|
---|
[227] | 3548 | if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
|
---|
| 3549 | SH_FREE(tmpFile);
|
---|
[19] | 3550 | SL_RETURN( 0, _("sh_unix_checksum_size"));
|
---|
| 3551 | }
|
---|
| 3552 |
|
---|
| 3553 | out:
|
---|
[227] | 3554 | if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
|
---|
| 3555 | SH_FREE(tmpFile);
|
---|
[93] | 3556 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[19] | 3557 | SL_RETURN( -1, _("sh_unix_checksum_size"));
|
---|
| 3558 | }
|
---|
| 3559 |
|
---|
[425] | 3560 | /********************************************************
|
---|
| 3561 | * Search rotated logfile
|
---|
| 3562 | */
|
---|
| 3563 | extern char * sh_rotated_log_search(const char * path, struct stat * buf);
|
---|
| 3564 |
|
---|
| 3565 | int sh_check_rotated_log (const char * path,
|
---|
[481] | 3566 | UINT64 old_size, UINT64 old_inode, const char * old_hash, unsigned long mask)
|
---|
[425] | 3567 | {
|
---|
| 3568 | struct stat obuf;
|
---|
| 3569 | UINT64 length_nolim = TIGER_NOLIM;
|
---|
| 3570 | int retval = S_FALSE;
|
---|
| 3571 |
|
---|
| 3572 | if (old_size != length_nolim)
|
---|
| 3573 | {
|
---|
| 3574 | char hashbuf[KEYBUF_SIZE];
|
---|
| 3575 | char * rotated_file;
|
---|
| 3576 |
|
---|
| 3577 | obuf.st_ino = old_inode;
|
---|
| 3578 | rotated_file = sh_rotated_log_search(path, &obuf);
|
---|
| 3579 |
|
---|
| 3580 | if (rotated_file && (0 != strcmp(path, rotated_file)))
|
---|
| 3581 | {
|
---|
| 3582 | SL_TICKET fd = sl_open_fastread (FIL__, __LINE__, rotated_file, SL_YESPRIV);
|
---|
| 3583 | if (!SL_ISERROR(fd))
|
---|
| 3584 | {
|
---|
[481] | 3585 | sh_unix_checksum_size (rotated_file, old_size, S_FALSE,
|
---|
| 3586 | hashbuf, 120 /* alert_timeout */, fd, mask);
|
---|
[425] | 3587 |
|
---|
| 3588 | sl_close(fd);
|
---|
| 3589 |
|
---|
| 3590 | if (strncmp (old_hash, hashbuf, KEY_LEN) == 0) {
|
---|
| 3591 | retval = S_TRUE;
|
---|
| 3592 | }
|
---|
| 3593 | }
|
---|
| 3594 | SH_FREE(rotated_file);
|
---|
| 3595 | }
|
---|
| 3596 | }
|
---|
| 3597 | return retval;
|
---|
| 3598 | }
|
---|
| 3599 |
|
---|
| 3600 |
|
---|
[68] | 3601 | int sh_unix_check_selinux = S_FALSE;
|
---|
| 3602 | int sh_unix_check_acl = S_FALSE;
|
---|
| 3603 |
|
---|
| 3604 | #ifdef USE_ACL
|
---|
| 3605 |
|
---|
| 3606 | #include <sys/acl.h>
|
---|
| 3607 | static char * sh_unix_getinfo_acl (char * path, int fd, struct stat * buf)
|
---|
| 3608 | {
|
---|
| 3609 | /* system.posix_acl_access, system.posix_acl_default
|
---|
| 3610 | */
|
---|
| 3611 | char * out = NULL;
|
---|
| 3612 | char * collect = NULL;
|
---|
| 3613 | char * tmp;
|
---|
| 3614 | char * out_compact;
|
---|
| 3615 | ssize_t len;
|
---|
| 3616 | acl_t result;
|
---|
| 3617 |
|
---|
| 3618 | SL_ENTER(_("sh_unix_getinfo_acl"));
|
---|
| 3619 |
|
---|
| 3620 | result = (fd == -1) ?
|
---|
| 3621 | acl_get_file (path, ACL_TYPE_ACCESS) :
|
---|
| 3622 | acl_get_fd (fd);
|
---|
| 3623 |
|
---|
| 3624 | if (result)
|
---|
| 3625 | {
|
---|
| 3626 | out = acl_to_text (result, &len);
|
---|
| 3627 | if (out && (len > 0)) {
|
---|
| 3628 | out_compact = sh_util_acl_compact (out, len);
|
---|
| 3629 | acl_free(out);
|
---|
| 3630 | if (out_compact)
|
---|
| 3631 | {
|
---|
| 3632 | collect = sh_util_strconcat (_("acl_access:"), out_compact, NULL);
|
---|
| 3633 | SH_FREE(out_compact);
|
---|
| 3634 | }
|
---|
| 3635 | }
|
---|
| 3636 | acl_free(result);
|
---|
| 3637 | }
|
---|
| 3638 |
|
---|
| 3639 |
|
---|
| 3640 | if ( S_ISDIR(buf->st_mode) )
|
---|
| 3641 | {
|
---|
| 3642 | result = acl_get_file (path, ACL_TYPE_DEFAULT);
|
---|
| 3643 |
|
---|
| 3644 | if (result)
|
---|
| 3645 | {
|
---|
| 3646 | out = acl_to_text (result, &len);
|
---|
| 3647 | if (out && (len > 0)) {
|
---|
| 3648 | out_compact = sh_util_acl_compact (out, len);
|
---|
| 3649 | acl_free(out);
|
---|
| 3650 | if (out_compact) {
|
---|
| 3651 | if (collect) {
|
---|
| 3652 | tmp = sh_util_strconcat (_("acl_default:"),
|
---|
| 3653 | out_compact, ":", collect, NULL);
|
---|
| 3654 | SH_FREE(collect);
|
---|
| 3655 | }
|
---|
| 3656 | else {
|
---|
| 3657 | tmp = sh_util_strconcat (_("acl_default:"), out_compact, NULL);
|
---|
| 3658 | }
|
---|
| 3659 | SH_FREE(out_compact);
|
---|
| 3660 | collect = tmp;
|
---|
| 3661 | }
|
---|
| 3662 | }
|
---|
| 3663 | acl_free(result);
|
---|
| 3664 | }
|
---|
| 3665 | }
|
---|
| 3666 |
|
---|
| 3667 | SL_RETURN((collect),_("sh_unix_getinfo_acl"));
|
---|
| 3668 | }
|
---|
| 3669 | #endif
|
---|
| 3670 |
|
---|
| 3671 | #ifdef USE_XATTR
|
---|
| 3672 |
|
---|
| 3673 | #include <attr/xattr.h>
|
---|
| 3674 | static char * sh_unix_getinfo_xattr_int (char * path, int fd, char * name)
|
---|
| 3675 | {
|
---|
| 3676 | char * out = NULL;
|
---|
| 3677 | char * tmp = NULL;
|
---|
| 3678 | size_t size = 256;
|
---|
| 3679 | ssize_t result;
|
---|
| 3680 |
|
---|
| 3681 | SL_ENTER(_("sh_unix_getinfo_xattr_int"));
|
---|
| 3682 |
|
---|
| 3683 | out = SH_ALLOC(size);
|
---|
| 3684 |
|
---|
| 3685 | result = (fd == -1) ?
|
---|
| 3686 | lgetxattr (path, name, out, size-1) :
|
---|
| 3687 | fgetxattr (fd, name, out, size-1);
|
---|
| 3688 |
|
---|
| 3689 | if (result == -1 && errno == ERANGE)
|
---|
| 3690 | {
|
---|
| 3691 | SH_FREE(out);
|
---|
| 3692 | result = (fd == -1) ?
|
---|
| 3693 | lgetxattr (path, name, NULL, 0) :
|
---|
| 3694 | fgetxattr (fd, name, NULL, 0);
|
---|
| 3695 | size = result + 1;
|
---|
| 3696 | out = SH_ALLOC(size);
|
---|
| 3697 | result = (fd == -1) ?
|
---|
| 3698 | lgetxattr (path, name, out, size-1) :
|
---|
| 3699 | fgetxattr (fd, name, out, size-1);
|
---|
| 3700 | }
|
---|
| 3701 |
|
---|
| 3702 | if ((result > 0) && ((size_t)result < size))
|
---|
| 3703 | {
|
---|
| 3704 | out[size-1] = '\0';
|
---|
| 3705 | tmp = out;
|
---|
| 3706 | }
|
---|
| 3707 | else
|
---|
| 3708 | {
|
---|
| 3709 | SH_FREE(out);
|
---|
| 3710 | }
|
---|
| 3711 |
|
---|
| 3712 | SL_RETURN((tmp),_("sh_unix_getinfo_xattr_int"));
|
---|
| 3713 | }
|
---|
| 3714 |
|
---|
| 3715 |
|
---|
| 3716 | static char * sh_unix_getinfo_xattr (char * path, int fd, struct stat * buf)
|
---|
| 3717 | {
|
---|
| 3718 | /* system.posix_acl_access, system.posix_acl_default, security.selinux
|
---|
| 3719 | */
|
---|
| 3720 | char * tmp;
|
---|
| 3721 | char * out = NULL;
|
---|
| 3722 | char * collect = NULL;
|
---|
| 3723 |
|
---|
| 3724 | SL_ENTER(_("sh_unix_getinfo_xattr"));
|
---|
| 3725 |
|
---|
| 3726 | #ifdef USE_ACL
|
---|
| 3727 | /*
|
---|
| 3728 | * we need the acl_get_fd/acl_get_file functions, getxattr will only
|
---|
| 3729 | * yield the raw bytes
|
---|
| 3730 | */
|
---|
| 3731 | if (sh_unix_check_acl == S_TRUE)
|
---|
| 3732 | {
|
---|
| 3733 | out = sh_unix_getinfo_acl(path, fd, buf);
|
---|
| 3734 |
|
---|
| 3735 | if (out)
|
---|
| 3736 | {
|
---|
| 3737 | collect = out;
|
---|
| 3738 | }
|
---|
| 3739 | }
|
---|
[290] | 3740 | #else
|
---|
| 3741 | (void) buf;
|
---|
[68] | 3742 | #endif
|
---|
| 3743 |
|
---|
[105] | 3744 | if (sh_unix_check_selinux == S_TRUE)
|
---|
| 3745 | {
|
---|
| 3746 | out = sh_unix_getinfo_xattr_int(path, fd, _("security.selinux"));
|
---|
[68] | 3747 |
|
---|
[105] | 3748 | if (out)
|
---|
| 3749 | {
|
---|
| 3750 | if (collect) {
|
---|
| 3751 | tmp = sh_util_strconcat(_("selinux:"), out, ":", collect, NULL);
|
---|
| 3752 | SH_FREE(collect);
|
---|
| 3753 | }
|
---|
| 3754 | else {
|
---|
| 3755 | tmp = sh_util_strconcat(_("selinux:"), out, NULL);
|
---|
| 3756 | }
|
---|
| 3757 | SH_FREE(out);
|
---|
| 3758 | collect = tmp;
|
---|
| 3759 | }
|
---|
[68] | 3760 | }
|
---|
| 3761 |
|
---|
| 3762 | SL_RETURN((collect),_("sh_unix_getinfo_xattr"));
|
---|
| 3763 | }
|
---|
| 3764 | #endif
|
---|
| 3765 |
|
---|
| 3766 | #ifdef USE_XATTR
|
---|
| 3767 | int sh_unix_setcheckselinux (const char * c)
|
---|
| 3768 | {
|
---|
| 3769 | int i;
|
---|
| 3770 | SL_ENTER(_("sh_unix_setcheckselinux"));
|
---|
| 3771 | i = sh_util_flagval(c, &(sh_unix_check_selinux));
|
---|
| 3772 |
|
---|
| 3773 | SL_RETURN(i, _("sh_unix_setcheckselinux"));
|
---|
| 3774 | }
|
---|
| 3775 | #endif
|
---|
| 3776 |
|
---|
| 3777 | #ifdef USE_ACL
|
---|
| 3778 | int sh_unix_setcheckacl (const char * c)
|
---|
| 3779 | {
|
---|
| 3780 | int i;
|
---|
| 3781 | SL_ENTER(_("sh_unix_setcheckacl"));
|
---|
| 3782 | i = sh_util_flagval(c, &(sh_unix_check_acl));
|
---|
| 3783 |
|
---|
| 3784 | SL_RETURN(i, _("sh_unix_setcheckacl"));
|
---|
| 3785 | }
|
---|
| 3786 | #endif
|
---|
| 3787 |
|
---|
[169] | 3788 | #ifdef HAVE_LIBZ
|
---|
| 3789 | #include <zlib.h>
|
---|
| 3790 | #endif
|
---|
| 3791 |
|
---|
[367] | 3792 |
|
---|
| 3793 | static void * sh_dummy_filename;
|
---|
[516] | 3794 | void * sh_dummy_tmp;
|
---|
| 3795 | void * sh_dummy_tmp2;
|
---|
[367] | 3796 |
|
---|
[373] | 3797 | int sh_unix_getinfo (int level, const char * filename, file_type * theFile,
|
---|
[1] | 3798 | char * fileHash, int policy)
|
---|
| 3799 | {
|
---|
| 3800 | char timestr[81];
|
---|
| 3801 | long runtim;
|
---|
| 3802 | struct stat buf;
|
---|
| 3803 | struct stat lbuf;
|
---|
| 3804 | struct stat fbuf;
|
---|
[367] | 3805 | volatile int stat_return;
|
---|
| 3806 | volatile int stat_errno = 0;
|
---|
[8] | 3807 |
|
---|
[1] | 3808 | ShFileType type;
|
---|
| 3809 | unsigned int mode;
|
---|
| 3810 | char * tmp;
|
---|
| 3811 | char * tmp2;
|
---|
[8] | 3812 |
|
---|
[1] | 3813 | char * linknamebuf;
|
---|
[367] | 3814 | volatile int linksize;
|
---|
[1] | 3815 |
|
---|
| 3816 | extern int get_the_fd (SL_TICKET ticket);
|
---|
| 3817 |
|
---|
[367] | 3818 | volatile SL_TICKET rval_open;
|
---|
| 3819 | volatile int err_open = 0;
|
---|
[192] | 3820 |
|
---|
[367] | 3821 | volatile int fd;
|
---|
| 3822 | volatile int fstat_return;
|
---|
| 3823 | volatile int fstat_errno = 0;
|
---|
| 3824 | volatile int try = 0;
|
---|
[192] | 3825 |
|
---|
[167] | 3826 | sh_string * content = NULL;
|
---|
| 3827 |
|
---|
[1] | 3828 | time_t tend;
|
---|
| 3829 | time_t tstart;
|
---|
| 3830 |
|
---|
| 3831 |
|
---|
| 3832 | char * path = NULL;
|
---|
| 3833 |
|
---|
[367] | 3834 | volatile int alert_timeout = 120;
|
---|
[1] | 3835 |
|
---|
| 3836 | path = theFile->fullpath;
|
---|
| 3837 |
|
---|
| 3838 | SL_ENTER(_("sh_unix_getinfo"));
|
---|
| 3839 |
|
---|
[481] | 3840 | if (!MODI_INITIALIZED(theFile->check_flags))
|
---|
[381] | 3841 | {
|
---|
| 3842 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
| 3843 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 3844 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
|
---|
| 3845 | _("Uninitialized check mask"), _("sh_unix_getinfo"),
|
---|
| 3846 | tmp2);
|
---|
| 3847 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 3848 | SH_FREE(tmp2);
|
---|
[481] | 3849 | SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
[381] | 3850 | }
|
---|
| 3851 |
|
---|
[367] | 3852 | /* Take the address to keep gcc from putting it into a register.
|
---|
| 3853 | * Avoids the 'clobbered by longjmp' warning.
|
---|
| 3854 | */
|
---|
| 3855 | sh_dummy_filename = (void *) &filename;
|
---|
| 3856 | sh_dummy_tmp = (void *) &tmp;
|
---|
| 3857 | sh_dummy_tmp2 = (void *) &tmp2;
|
---|
| 3858 |
|
---|
[1] | 3859 | /* --- Stat the file, and get checksum. ---
|
---|
| 3860 | */
|
---|
| 3861 | tstart = time(NULL);
|
---|
[8] | 3862 |
|
---|
[1] | 3863 | stat_return = retry_lstat (FIL__, __LINE__,
|
---|
| 3864 | path /* theFile->fullpath */, &buf);
|
---|
| 3865 |
|
---|
[192] | 3866 | if (stat_return)
|
---|
| 3867 | stat_errno = errno;
|
---|
| 3868 |
|
---|
[252] | 3869 | theFile->link_path = NULL;
|
---|
| 3870 |
|
---|
| 3871 | try_again:
|
---|
| 3872 |
|
---|
[8] | 3873 | fd = -1;
|
---|
| 3874 | fstat_return = -1;
|
---|
| 3875 | rval_open = -1;
|
---|
| 3876 |
|
---|
[1] | 3877 | if (stat_return == 0 && S_ISREG(buf.st_mode))
|
---|
| 3878 | {
|
---|
[248] | 3879 | rval_open = sl_open_fastread (FIL__, __LINE__,
|
---|
| 3880 | path /* theFile->fullpath */, SL_YESPRIV);
|
---|
[192] | 3881 | if (SL_ISERROR(rval_open))
|
---|
[252] | 3882 | {
|
---|
| 3883 | char * stale = sl_check_stale();
|
---|
| 3884 |
|
---|
| 3885 | if (stale)
|
---|
| 3886 | {
|
---|
[367] | 3887 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[252] | 3888 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
|
---|
| 3889 | stale, _("sh_unix_getinfo_open"));
|
---|
[367] | 3890 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[252] | 3891 | }
|
---|
[1] | 3892 |
|
---|
[252] | 3893 | if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
|
---|
| 3894 | {
|
---|
| 3895 | ++try;
|
---|
| 3896 | goto try_again;
|
---|
| 3897 | }
|
---|
| 3898 | err_open = errno;
|
---|
| 3899 | }
|
---|
[251] | 3900 |
|
---|
[1] | 3901 | alert_timeout = 120; /* this is per 8K block now ! */
|
---|
| 3902 |
|
---|
[8] | 3903 | if (path[1] == 'p' && path[5] == '/' && path[2] == 'r' &&
|
---|
| 3904 | path[3] == 'o' && path[4] == 'c' && path[0] == '/')
|
---|
[1] | 3905 | {
|
---|
| 3906 | /* seven is magic */
|
---|
| 3907 | alert_timeout = 7;
|
---|
| 3908 | }
|
---|
[8] | 3909 |
|
---|
| 3910 | fd = get_the_fd(rval_open);
|
---|
[1] | 3911 | }
|
---|
| 3912 |
|
---|
| 3913 | tend = time(NULL);
|
---|
| 3914 |
|
---|
| 3915 | /* An unprivileged user may slow lstat/open to a crawl
|
---|
| 3916 | * with clever path/symlink setup
|
---|
| 3917 | */
|
---|
| 3918 | if ((tend - tstart) > (time_t) /* 60 */ 6)
|
---|
| 3919 | {
|
---|
| 3920 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
[367] | 3921 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 3922 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_TOOLATE,
|
---|
| 3923 | (long)(tend - tstart), tmp2);
|
---|
[367] | 3924 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 3925 | SH_FREE(tmp2);
|
---|
| 3926 | }
|
---|
[8] | 3927 |
|
---|
[1] | 3928 | if (fd >= 0)
|
---|
[192] | 3929 | {
|
---|
| 3930 | fstat_return = retry_fstat (FIL__, __LINE__, fd, &fbuf);
|
---|
[251] | 3931 |
|
---|
[192] | 3932 | if (fstat_return)
|
---|
[251] | 3933 | {
|
---|
| 3934 | char * stale;
|
---|
| 3935 |
|
---|
| 3936 | fstat_errno = errno;
|
---|
| 3937 |
|
---|
| 3938 | stale = sl_check_stale();
|
---|
| 3939 |
|
---|
| 3940 | if (stale)
|
---|
| 3941 | {
|
---|
[367] | 3942 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[251] | 3943 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, fstat_errno,
|
---|
| 3944 | MSG_E_SUBGEN,
|
---|
| 3945 | stale, _("sh_unix_getinfo_fstat"));
|
---|
[367] | 3946 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[251] | 3947 | }
|
---|
[252] | 3948 |
|
---|
| 3949 | if (try == 0) /* obsolete, but we keep this, just in case */
|
---|
| 3950 | {
|
---|
| 3951 | ++try;
|
---|
| 3952 | sl_close(rval_open);
|
---|
| 3953 | goto try_again;
|
---|
| 3954 | }
|
---|
[251] | 3955 | }
|
---|
[192] | 3956 | }
|
---|
[1] | 3957 | else
|
---|
[192] | 3958 | {
|
---|
| 3959 | fd = -1;
|
---|
| 3960 | }
|
---|
[8] | 3961 |
|
---|
[1] | 3962 |
|
---|
| 3963 | /* --- case 1: lstat failed ---
|
---|
| 3964 | */
|
---|
| 3965 | if (stat_return != 0)
|
---|
| 3966 | {
|
---|
| 3967 | stat_return = errno;
|
---|
| 3968 | if (!SL_ISERROR(rval_open))
|
---|
| 3969 | sl_close(rval_open);
|
---|
| 3970 | if (sh.flag.checkSum == SH_CHECK_INIT ||
|
---|
| 3971 | (sh_hash_have_it (theFile->fullpath) >= 0 &&
|
---|
[114] | 3972 | (!SH_FFLAG_REPORTED_SET(theFile->file_reported))))
|
---|
[1] | 3973 | {
|
---|
| 3974 | if (S_FALSE == sh_ignore_chk_del(theFile->fullpath)) {
|
---|
[458] | 3975 | int flags = sh_hash_getflags (theFile->fullpath);
|
---|
| 3976 |
|
---|
| 3977 | if ((flags >= 0) && (flags & SH_FFLAG_ENOENT) == 0) {
|
---|
| 3978 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 3979 | uid_t euid;
|
---|
| 3980 | (void) sl_get_euid(&euid);
|
---|
| 3981 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
| 3982 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 3983 | sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
|
---|
| 3984 | _("lstat"),
|
---|
| 3985 | sh_error_message (stat_errno, errbuf, sizeof(errbuf)),
|
---|
| 3986 | (long) euid,
|
---|
| 3987 | tmp2);
|
---|
| 3988 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 3989 | SH_FREE(tmp2);
|
---|
| 3990 | sh_hash_set_flag (theFile->fullpath, SH_FFLAG_ENOENT);
|
---|
| 3991 | }
|
---|
[1] | 3992 | }
|
---|
| 3993 | }
|
---|
| 3994 | SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
| 3995 | }
|
---|
| 3996 |
|
---|
[8] | 3997 | /* --- case 2: not a regular file ---
|
---|
[1] | 3998 | */
|
---|
| 3999 | else if (! S_ISREG(buf.st_mode))
|
---|
| 4000 | {
|
---|
| 4001 | if (fileHash != NULL)
|
---|
[93] | 4002 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4003 | }
|
---|
| 4004 |
|
---|
| 4005 | /* --- case 3a: a regular file, fstat ok ---
|
---|
| 4006 | */
|
---|
| 4007 | else if (fstat_return == 0 &&
|
---|
| 4008 | buf.st_mode == fbuf.st_mode &&
|
---|
| 4009 | buf.st_ino == fbuf.st_ino &&
|
---|
| 4010 | buf.st_uid == fbuf.st_uid &&
|
---|
| 4011 | buf.st_gid == fbuf.st_gid &&
|
---|
| 4012 | buf.st_dev == fbuf.st_dev )
|
---|
| 4013 | {
|
---|
| 4014 | if (fileHash != NULL)
|
---|
| 4015 | {
|
---|
[481] | 4016 | if ((theFile->check_flags & MODI_CHK) == 0 ||
|
---|
[310] | 4017 | sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size,
|
---|
| 4018 | (UINT64) fbuf.st_mode, rval_open))
|
---|
[1] | 4019 | {
|
---|
[93] | 4020 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4021 | }
|
---|
[481] | 4022 | else if ((theFile->check_flags & MODI_PREL) != 0 &&
|
---|
[1] | 4023 | S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
|
---|
[102] | 4024 | alert_timeout, theFile->fullpath))
|
---|
[1] | 4025 | {
|
---|
| 4026 | if (0 != sh_prelink_run (theFile->fullpath,
|
---|
[481] | 4027 | fileHash, alert_timeout, theFile->check_flags))
|
---|
[93] | 4028 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4029 | }
|
---|
| 4030 | else
|
---|
| 4031 | {
|
---|
[133] | 4032 | char hashbuf[KEYBUF_SIZE];
|
---|
[425] | 4033 | UINT64 length_current = TIGER_NOLIM;
|
---|
[167] | 4034 |
|
---|
[481] | 4035 | if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
|
---|
[167] | 4036 | {
|
---|
| 4037 | sl_init_content (rval_open, fbuf.st_size);
|
---|
| 4038 | }
|
---|
| 4039 |
|
---|
[481] | 4040 | if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
|
---|
[22] | 4041 | sl_strlcpy(fileHash,
|
---|
| 4042 | sh_tiger_generic_hash (theFile->fullpath,
|
---|
[425] | 4043 | rval_open, &length_current,
|
---|
[133] | 4044 | alert_timeout,
|
---|
| 4045 | hashbuf, sizeof(hashbuf)),
|
---|
[22] | 4046 | KEY_LEN+1);
|
---|
[167] | 4047 |
|
---|
| 4048 | content = sl_get_content(rval_open);
|
---|
[169] | 4049 | content = sh_string_copy(content);
|
---|
[167] | 4050 |
|
---|
[481] | 4051 | if ((theFile->check_flags & MODI_SGROW) != 0)
|
---|
[19] | 4052 | {
|
---|
[436] | 4053 | /* Update size so it matches the one for which the checksum
|
---|
| 4054 | has been computed */
|
---|
| 4055 | fbuf.st_size = length_current;
|
---|
[151] | 4056 | buf.st_size = fbuf.st_size;
|
---|
[19] | 4057 | sl_rewind(rval_open);
|
---|
[425] | 4058 | sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
|
---|
[19] | 4059 | &fileHash[KEY_LEN + 1],
|
---|
[481] | 4060 | alert_timeout, rval_open, theFile->check_flags);
|
---|
[19] | 4061 | }
|
---|
[1] | 4062 | }
|
---|
| 4063 | }
|
---|
| 4064 | }
|
---|
| 4065 |
|
---|
| 4066 | /* --- case 3b: a regular file, fstat ok, but different ---
|
---|
| 4067 | */
|
---|
| 4068 | else if (fstat_return == 0 && S_ISREG(fbuf.st_mode))
|
---|
| 4069 | {
|
---|
| 4070 | memcpy (&buf, &fbuf, sizeof( struct stat ));
|
---|
| 4071 |
|
---|
| 4072 | if (fileHash != NULL)
|
---|
| 4073 | {
|
---|
[481] | 4074 | if ((theFile->check_flags & MODI_CHK) == 0 ||
|
---|
[310] | 4075 | sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size,
|
---|
| 4076 | (UINT64) fbuf.st_mode, rval_open))
|
---|
[1] | 4077 | {
|
---|
[93] | 4078 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4079 | }
|
---|
| 4080 | else if (policy == SH_LEVEL_PRELINK &&
|
---|
| 4081 | S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
|
---|
[102] | 4082 | alert_timeout, theFile->fullpath))
|
---|
[1] | 4083 | {
|
---|
| 4084 | if (0 != sh_prelink_run (theFile->fullpath,
|
---|
[481] | 4085 | fileHash, alert_timeout, theFile->check_flags))
|
---|
[93] | 4086 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4087 | }
|
---|
| 4088 | else
|
---|
| 4089 | {
|
---|
[133] | 4090 | char hashbuf[KEYBUF_SIZE];
|
---|
[425] | 4091 | UINT64 length_current = TIGER_NOLIM;
|
---|
[167] | 4092 |
|
---|
[481] | 4093 | if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
|
---|
[167] | 4094 | {
|
---|
| 4095 | sl_init_content (rval_open, fbuf.st_size);
|
---|
| 4096 | }
|
---|
| 4097 |
|
---|
[481] | 4098 | if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
|
---|
[1] | 4099 | sl_strlcpy(fileHash,
|
---|
[133] | 4100 | sh_tiger_generic_hash (theFile->fullpath, rval_open,
|
---|
[425] | 4101 | &length_current,
|
---|
[133] | 4102 | alert_timeout,
|
---|
| 4103 | hashbuf, sizeof(hashbuf)),
|
---|
[1] | 4104 | KEY_LEN + 1);
|
---|
[167] | 4105 |
|
---|
| 4106 | content = sl_get_content(rval_open);
|
---|
[169] | 4107 | content = sh_string_copy(content);
|
---|
[167] | 4108 |
|
---|
[481] | 4109 | if ((theFile->check_flags & MODI_SGROW) != 0)
|
---|
[19] | 4110 | {
|
---|
[436] | 4111 | /* Update size so it matches the one for which the checksum
|
---|
| 4112 | has been computed */
|
---|
| 4113 | fbuf.st_size = length_current;
|
---|
[151] | 4114 | buf.st_size = fbuf.st_size;
|
---|
[19] | 4115 | sl_rewind(rval_open);
|
---|
[425] | 4116 | sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
|
---|
[19] | 4117 | &fileHash[KEY_LEN + 1],
|
---|
[481] | 4118 | alert_timeout, rval_open, theFile->check_flags);
|
---|
[19] | 4119 | }
|
---|
[1] | 4120 | }
|
---|
| 4121 | }
|
---|
| 4122 | }
|
---|
| 4123 |
|
---|
| 4124 | /* --- case 4: a regular file, fstat failed ---
|
---|
| 4125 | */
|
---|
| 4126 |
|
---|
[192] | 4127 | else /* fstat_return != 0 or !S_ISREG(fbuf.st_mode) or open() failed */
|
---|
[1] | 4128 | {
|
---|
[185] | 4129 | uid_t euid;
|
---|
[192] | 4130 |
|
---|
[1] | 4131 | if (fileHash != NULL)
|
---|
[93] | 4132 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
[1] | 4133 |
|
---|
[481] | 4134 | if ((theFile->check_flags & MODI_CHK) != 0)
|
---|
[65] | 4135 | {
|
---|
| 4136 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
[185] | 4137 |
|
---|
[200] | 4138 |
|
---|
| 4139 | if (fd >= 0 && fstat_return != 0)
|
---|
[185] | 4140 | {
|
---|
[192] | 4141 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[185] | 4142 | (void) sl_get_euid(&euid);
|
---|
[192] | 4143 |
|
---|
[367] | 4144 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[192] | 4145 | sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
|
---|
| 4146 | _("fstat"),
|
---|
| 4147 | sh_error_message (fstat_errno, errbuf, sizeof(errbuf)),
|
---|
| 4148 | (long) euid,
|
---|
| 4149 | tmp2);
|
---|
[367] | 4150 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[185] | 4151 | }
|
---|
[200] | 4152 | else if (fd >= 0 && !S_ISREG(fbuf.st_mode))
|
---|
[185] | 4153 | {
|
---|
[367] | 4154 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[192] | 4155 | sh_error_handle (level, FIL__, __LINE__, fstat_errno,
|
---|
[185] | 4156 | MSG_E_NOTREG, tmp2);
|
---|
[367] | 4157 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[185] | 4158 | }
|
---|
| 4159 | else
|
---|
| 4160 | {
|
---|
[192] | 4161 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 4162 | char errbuf2[SH_ERRBUF_SIZE];
|
---|
| 4163 | sl_strlcpy(errbuf, sl_error_string(rval_open), sizeof(errbuf));
|
---|
| 4164 | sh_error_message(err_open, errbuf2, sizeof(errbuf2));
|
---|
[367] | 4165 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[192] | 4166 | sh_error_handle (level, FIL__, __LINE__, err_open,
|
---|
| 4167 | MSG_E_READ, errbuf, errbuf2, tmp2);
|
---|
[367] | 4168 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[185] | 4169 | }
|
---|
[65] | 4170 | SH_FREE(tmp2);
|
---|
| 4171 | }
|
---|
[1] | 4172 | }
|
---|
| 4173 |
|
---|
| 4174 |
|
---|
| 4175 | /* --- Determine file type. ---
|
---|
| 4176 | */
|
---|
[149] | 4177 | memset (theFile->c_mode, '-', CMODE_SIZE-1);
|
---|
| 4178 | theFile->c_mode[CMODE_SIZE-1] = '\0';
|
---|
[8] | 4179 |
|
---|
[149] | 4180 | memset (theFile->link_c_mode, '-', CMODE_SIZE-1);
|
---|
| 4181 | theFile->link_c_mode[CMODE_SIZE-1] = '\0';
|
---|
[1] | 4182 |
|
---|
| 4183 | sh_unix_getinfo_type (&buf, &type, theFile->c_mode);
|
---|
| 4184 | theFile->type = type;
|
---|
| 4185 |
|
---|
[10] | 4186 | #if defined(__linux__) || defined(HAVE_STAT_FLAGS)
|
---|
[1] | 4187 |
|
---|
| 4188 | /* --- Determine file attributes. ---
|
---|
| 4189 | */
|
---|
[149] | 4190 | memset (theFile->c_attributes, '-', ATTRBUF_SIZE);
|
---|
| 4191 | theFile->c_attributes[ATTRBUF_USED] = '\0';
|
---|
[1] | 4192 | theFile->attributes = 0;
|
---|
| 4193 |
|
---|
[279] | 4194 | #if (defined(__linux__) && (defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H))) || defined(HAVE_STAT_FLAGS)
|
---|
[1] | 4195 | if (theFile->c_mode[0] != 'c' && theFile->c_mode[0] != 'b' &&
|
---|
| 4196 | theFile->c_mode[0] != 'l' )
|
---|
| 4197 | sh_unix_getinfo_attr(theFile->fullpath,
|
---|
[10] | 4198 | &theFile->attributes, theFile->c_attributes,
|
---|
| 4199 | fd, &buf);
|
---|
[1] | 4200 | #endif
|
---|
[279] | 4201 | #endif
|
---|
[1] | 4202 |
|
---|
[105] | 4203 | #if defined(USE_XATTR) && defined(USE_ACL)
|
---|
| 4204 | if (sh_unix_check_selinux == S_TRUE || sh_unix_check_acl == S_TRUE)
|
---|
| 4205 | theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
|
---|
| 4206 | #elif defined(USE_XATTR)
|
---|
[68] | 4207 | if (sh_unix_check_selinux == S_TRUE)
|
---|
| 4208 | theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
|
---|
| 4209 | #elif defined(USE_ACL)
|
---|
| 4210 | if (sh_unix_check_acl == S_TRUE)
|
---|
| 4211 | theFile->attr_string = sh_unix_getinfo_acl (theFile->fullpath, fd, &buf);
|
---|
| 4212 | #else
|
---|
| 4213 | theFile->attr_string = NULL;
|
---|
| 4214 | #endif
|
---|
| 4215 |
|
---|
[1] | 4216 | if (!SL_ISERROR(rval_open))
|
---|
| 4217 | sl_close(rval_open);
|
---|
| 4218 |
|
---|
| 4219 |
|
---|
| 4220 | /* --- I/O limit. ---
|
---|
| 4221 | */
|
---|
| 4222 | if (IO_Limit > 0)
|
---|
| 4223 | {
|
---|
| 4224 | runtim = (long) (time(NULL) - sh.statistics.time_start);
|
---|
| 4225 |
|
---|
| 4226 | if (runtim > 0 && (long)(sh.statistics.bytes_hashed/runtim) > IO_Limit)
|
---|
| 4227 | retry_msleep(1, 0);
|
---|
| 4228 | }
|
---|
| 4229 |
|
---|
| 4230 | /* --- Determine permissions. ---
|
---|
| 4231 | */
|
---|
| 4232 | sh_unix_getinfo_mode (&buf, &mode, theFile->c_mode);
|
---|
| 4233 |
|
---|
| 4234 | /* --- Trivia. ---
|
---|
| 4235 | */
|
---|
| 4236 | theFile->dev = buf.st_dev;
|
---|
| 4237 | theFile->ino = buf.st_ino;
|
---|
| 4238 | theFile->mode = buf.st_mode;
|
---|
| 4239 | theFile->hardlinks = buf.st_nlink;
|
---|
| 4240 | theFile->owner = buf.st_uid;
|
---|
| 4241 | theFile->group = buf.st_gid;
|
---|
| 4242 | theFile->rdev = buf.st_rdev;
|
---|
| 4243 | theFile->size = buf.st_size;
|
---|
| 4244 | theFile->blksize = (unsigned long) buf.st_blksize;
|
---|
| 4245 | theFile->blocks = (unsigned long) buf.st_blocks;
|
---|
| 4246 | theFile->atime = buf.st_atime;
|
---|
| 4247 | theFile->mtime = buf.st_mtime;
|
---|
| 4248 | theFile->ctime = buf.st_ctime;
|
---|
| 4249 |
|
---|
| 4250 |
|
---|
| 4251 | /* --- Owner and group. ---
|
---|
| 4252 | */
|
---|
| 4253 |
|
---|
[132] | 4254 | if (NULL == sh_unix_getGIDname(SH_ERR_ALL, buf.st_gid, theFile->c_group, GROUP_MAX+1)) {
|
---|
[1] | 4255 |
|
---|
| 4256 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
| 4257 |
|
---|
| 4258 | if (policy == SH_LEVEL_ALLIGNORE)
|
---|
| 4259 | {
|
---|
[367] | 4260 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4261 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
|
---|
| 4262 | MSG_FI_NOGRP,
|
---|
| 4263 | (long) buf.st_gid, tmp2);
|
---|
[367] | 4264 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4265 | }
|
---|
| 4266 | else
|
---|
| 4267 | {
|
---|
[367] | 4268 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4269 | sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
|
---|
| 4270 | MSG_FI_NOGRP,
|
---|
| 4271 | (long) buf.st_gid, tmp2);
|
---|
[367] | 4272 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4273 | }
|
---|
| 4274 | SH_FREE(tmp2);
|
---|
| 4275 | sl_snprintf(theFile->c_group, GROUP_MAX+1, "%d", (long) buf.st_gid);
|
---|
| 4276 | }
|
---|
| 4277 |
|
---|
| 4278 |
|
---|
[137] | 4279 | if (NULL == sh_unix_getUIDname(SH_ERR_ALL, buf.st_uid, theFile->c_owner, USER_MAX+1)) {
|
---|
[1] | 4280 |
|
---|
| 4281 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
| 4282 |
|
---|
| 4283 | if (policy == SH_LEVEL_ALLIGNORE)
|
---|
| 4284 | {
|
---|
[367] | 4285 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4286 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
|
---|
| 4287 | MSG_FI_NOUSR,
|
---|
| 4288 | (long) buf.st_uid, tmp2);
|
---|
[367] | 4289 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4290 | }
|
---|
| 4291 | else
|
---|
| 4292 | {
|
---|
[367] | 4293 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4294 | sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
|
---|
| 4295 | MSG_FI_NOUSR,
|
---|
| 4296 | (long) buf.st_uid, tmp2);
|
---|
[367] | 4297 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4298 | }
|
---|
| 4299 | SH_FREE(tmp2);
|
---|
| 4300 | sl_snprintf(theFile->c_owner, USER_MAX+1, "%d", (long) buf.st_uid);
|
---|
| 4301 | }
|
---|
| 4302 |
|
---|
| 4303 | /* --- Output the file. ---
|
---|
| 4304 | */
|
---|
[481] | 4305 | if (flag_err_debug == S_TRUE)
|
---|
[1] | 4306 | {
|
---|
| 4307 | tmp2 = sh_util_safe_name ((filename == NULL) ?
|
---|
| 4308 | theFile->fullpath : filename);
|
---|
[137] | 4309 | (void) sh_unix_time(theFile->mtime, timestr, sizeof(timestr));
|
---|
[367] | 4310 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4311 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LIST,
|
---|
| 4312 | theFile->c_mode,
|
---|
| 4313 | theFile->hardlinks,
|
---|
| 4314 | theFile->c_owner,
|
---|
| 4315 | theFile->c_group,
|
---|
| 4316 | (unsigned long) theFile->size,
|
---|
| 4317 | timestr,
|
---|
| 4318 | tmp2);
|
---|
[367] | 4319 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4320 | SH_FREE(tmp2);
|
---|
| 4321 | }
|
---|
| 4322 |
|
---|
| 4323 | /* --- Check for links. ---
|
---|
| 4324 | */
|
---|
| 4325 | if (theFile->c_mode[0] == 'l')
|
---|
| 4326 | {
|
---|
| 4327 | linknamebuf = SH_ALLOC(PATH_MAX);
|
---|
| 4328 |
|
---|
[22] | 4329 | /* flawfinder: ignore */
|
---|
[1] | 4330 | linksize = readlink (theFile->fullpath, linknamebuf, PATH_MAX-1);
|
---|
| 4331 |
|
---|
| 4332 | if (linksize < (PATH_MAX-1) && linksize >= 0)
|
---|
| 4333 | linknamebuf[linksize] = '\0';
|
---|
| 4334 | else
|
---|
| 4335 | linknamebuf[PATH_MAX-1] = '\0';
|
---|
| 4336 |
|
---|
| 4337 | if (linksize < 0)
|
---|
| 4338 | {
|
---|
[132] | 4339 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 4340 | linksize = errno;
|
---|
| 4341 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
[367] | 4342 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[1] | 4343 | sh_error_handle (level, FIL__, __LINE__, linksize, MSG_FI_RDLNK,
|
---|
[132] | 4344 | sh_error_message (linksize, errbuf, sizeof(errbuf)), tmp2);
|
---|
[367] | 4345 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[1] | 4346 | SH_FREE(tmp2);
|
---|
| 4347 | SH_FREE(linknamebuf);
|
---|
[167] | 4348 | theFile->link_path = sh_util_strdup("-");
|
---|
[1] | 4349 | SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
| 4350 | }
|
---|
[138] | 4351 |
|
---|
[167] | 4352 | if (linknamebuf[0] == '/')
|
---|
| 4353 | {
|
---|
| 4354 | theFile->link_path = sh_util_strdup (linknamebuf);
|
---|
| 4355 | }
|
---|
| 4356 | else
|
---|
| 4357 | {
|
---|
| 4358 | tmp = sh_util_dirname(theFile->fullpath);
|
---|
| 4359 | if (tmp) {
|
---|
| 4360 | theFile->link_path = SH_ALLOC(PATH_MAX);
|
---|
| 4361 | sl_strlcpy (theFile->link_path, tmp, PATH_MAX);
|
---|
| 4362 | SH_FREE(tmp);
|
---|
| 4363 | } else {
|
---|
| 4364 | theFile->link_path = SH_ALLOC(PATH_MAX);
|
---|
| 4365 | theFile->link_path[0] = '\0';
|
---|
| 4366 | }
|
---|
| 4367 | /*
|
---|
| 4368 | * Only attach '/' if not root directory. Handle "//", which
|
---|
| 4369 | * according to POSIX is implementation-defined, and may be
|
---|
| 4370 | * different from "/" (however, three or more '/' will collapse
|
---|
| 4371 | * to one).
|
---|
| 4372 | */
|
---|
| 4373 | tmp = theFile->link_path; while (*tmp == '/') ++tmp;
|
---|
| 4374 | if (*tmp != '\0')
|
---|
| 4375 | {
|
---|
| 4376 | sl_strlcat (theFile->link_path, "/", PATH_MAX);
|
---|
| 4377 | }
|
---|
| 4378 | sl_strlcat (theFile->link_path, linknamebuf, PATH_MAX);
|
---|
| 4379 | }
|
---|
| 4380 |
|
---|
| 4381 | /* stat the link
|
---|
| 4382 | */
|
---|
| 4383 | stat_return = retry_lstat (FIL__, __LINE__, theFile->link_path, &lbuf);
|
---|
| 4384 |
|
---|
| 4385 | /* check for error
|
---|
| 4386 | */
|
---|
| 4387 | if (stat_return != 0)
|
---|
| 4388 | {
|
---|
| 4389 | stat_return = errno;
|
---|
| 4390 | tmp = sh_util_safe_name (theFile->fullpath);
|
---|
| 4391 | tmp2 = sh_util_safe_name (theFile->link_path);
|
---|
| 4392 | if (stat_return != ENOENT)
|
---|
| 4393 | {
|
---|
[192] | 4394 | uid_t euid;
|
---|
[167] | 4395 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[192] | 4396 |
|
---|
| 4397 | (void) sl_get_euid(&euid);
|
---|
[367] | 4398 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[167] | 4399 | sh_error_handle (level, FIL__, __LINE__, stat_return,
|
---|
[192] | 4400 | MSG_FI_STAT,
|
---|
[454] | 4401 | _("lstat (link target)"),
|
---|
[167] | 4402 | sh_error_message (stat_return,errbuf, sizeof(errbuf)),
|
---|
[192] | 4403 | (long) euid,
|
---|
[167] | 4404 | tmp2);
|
---|
[367] | 4405 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[167] | 4406 | }
|
---|
| 4407 | else
|
---|
| 4408 | {
|
---|
| 4409 | /* a dangling link -- everybody seems to have plenty of them
|
---|
| 4410 | */
|
---|
[367] | 4411 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[167] | 4412 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DLNK,
|
---|
| 4413 | tmp, tmp2);
|
---|
[367] | 4414 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[167] | 4415 | }
|
---|
| 4416 | theFile->linkisok = BAD;
|
---|
[93] | 4417 | SH_FREE(tmp);
|
---|
[167] | 4418 | SH_FREE(tmp2);
|
---|
| 4419 | SH_FREE(linknamebuf);
|
---|
| 4420 | /*
|
---|
| 4421 | * changed Tue Feb 10 16:16:13 CET 2004:
|
---|
| 4422 | * add dangling symlinks into database
|
---|
| 4423 | * SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
| 4424 | */
|
---|
| 4425 | theFile->linkmode = 0;
|
---|
| 4426 | SL_RETURN((0),_("sh_unix_getinfo"));
|
---|
[93] | 4427 | }
|
---|
[1] | 4428 |
|
---|
[167] | 4429 | theFile->linkisok = GOOD;
|
---|
| 4430 |
|
---|
| 4431 |
|
---|
| 4432 | /* --- Determine file type. ---
|
---|
| 4433 | */
|
---|
| 4434 | sh_unix_getinfo_type (&lbuf, &type, theFile->link_c_mode);
|
---|
| 4435 | theFile->type = type;
|
---|
| 4436 |
|
---|
| 4437 | /* --- Determine permissions. ---
|
---|
| 4438 | */
|
---|
| 4439 | sh_unix_getinfo_mode (&lbuf, &mode, theFile->link_c_mode);
|
---|
| 4440 | theFile->linkmode = lbuf.st_mode;
|
---|
| 4441 |
|
---|
| 4442 | /* --- Output the link. ---
|
---|
| 4443 | */
|
---|
| 4444 | if (theFile->linkisok == GOOD)
|
---|
| 4445 | {
|
---|
| 4446 | tmp2 = sh_util_safe_name (linknamebuf);
|
---|
[367] | 4447 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[167] | 4448 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LLNK,
|
---|
| 4449 | theFile->link_c_mode, tmp2);
|
---|
[367] | 4450 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[167] | 4451 | SH_FREE(tmp2);
|
---|
| 4452 | }
|
---|
| 4453 | SH_FREE(linknamebuf);
|
---|
| 4454 | }
|
---|
[402] | 4455 | else /* not a link, theFile->c_mode[0] != 'l' */
|
---|
[167] | 4456 | {
|
---|
| 4457 | if (content)
|
---|
| 4458 | {
|
---|
[169] | 4459 | #ifdef HAVE_LIBZ
|
---|
[171] | 4460 | unsigned long clen;
|
---|
| 4461 | unsigned char * compressed;
|
---|
| 4462 | #ifdef HAVE_COMPRESSBOUND
|
---|
| 4463 | clen = compressBound(sh_string_len(content));
|
---|
| 4464 | #else
|
---|
| 4465 | if (sh_string_len(content) > 10*SH_TXT_MAX)
|
---|
| 4466 | clen = SH_TXT_MAX;
|
---|
| 4467 | else
|
---|
| 4468 | clen = 13 + (int)(1.0001*sh_string_len(content));
|
---|
| 4469 | #endif
|
---|
| 4470 | compressed = SH_ALLOC(clen);
|
---|
[169] | 4471 | if (Z_OK == compress(compressed, &clen,
|
---|
| 4472 | (unsigned char *) sh_string_str(content),
|
---|
| 4473 | sh_string_len(content)))
|
---|
| 4474 | {
|
---|
| 4475 | if (clen < SH_TXT_MAX)
|
---|
| 4476 | {
|
---|
| 4477 | sh_util_base64_enc_alloc (&(theFile->link_path),
|
---|
| 4478 | (char *) compressed, clen);
|
---|
| 4479 | }
|
---|
| 4480 | else
|
---|
| 4481 | {
|
---|
| 4482 | char tmsg[128];
|
---|
| 4483 | char * tpath = sh_util_safe_name (theFile->fullpath);
|
---|
| 4484 | sl_snprintf(tmsg, sizeof(tmsg),
|
---|
| 4485 | _("compressed file too large (%lu bytes)"),
|
---|
| 4486 | clen);
|
---|
[367] | 4487 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[169] | 4488 | sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, -1,
|
---|
| 4489 | MSG_E_SUBGPATH, tmsg,
|
---|
| 4490 | _("sh_unix_getinfo"), tpath);
|
---|
[367] | 4491 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[169] | 4492 | SH_FREE(tpath);
|
---|
| 4493 | }
|
---|
| 4494 | }
|
---|
| 4495 | SH_FREE(compressed);
|
---|
| 4496 | #endif
|
---|
| 4497 | sh_string_destroy(&content);
|
---|
[167] | 4498 | }
|
---|
| 4499 | }
|
---|
[1] | 4500 | SL_RETURN((0),_("sh_unix_getinfo"));
|
---|
| 4501 | }
|
---|
| 4502 |
|
---|
| 4503 | /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
|
---|
| 4504 | #endif
|
---|
| 4505 |
|
---|
[169] | 4506 | int sh_unix_unlock(char * lockfile, char * flag)
|
---|
| 4507 | {
|
---|
| 4508 | int error = 0;
|
---|
| 4509 |
|
---|
| 4510 | SL_ENTER(_("sh_unix_unlock"));
|
---|
[1] | 4511 |
|
---|
[169] | 4512 | if (sh.flag.isdaemon == S_FALSE && flag == NULL)
|
---|
| 4513 | SL_RETURN((0),_("sh_unix_unlock"));
|
---|
| 4514 |
|
---|
| 4515 | /* --- Logfile is not locked to us. ---
|
---|
| 4516 | */
|
---|
| 4517 | if (sh.flag.islocked == BAD && flag != NULL)
|
---|
| 4518 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
| 4519 |
|
---|
| 4520 | /* --- Check whether the directory is secure. ---
|
---|
| 4521 | */
|
---|
| 4522 | if (0 != tf_trust_check (lockfile, SL_YESPRIV))
|
---|
| 4523 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
| 4524 |
|
---|
| 4525 | /* --- Delete the lock file. ---
|
---|
| 4526 | */
|
---|
| 4527 | error = retry_aud_unlink (FIL__, __LINE__, lockfile);
|
---|
| 4528 |
|
---|
| 4529 | if (error == 0)
|
---|
| 4530 | {
|
---|
| 4531 | if (flag != NULL)
|
---|
| 4532 | sh.flag.islocked = BAD; /* not locked anymore */
|
---|
| 4533 | }
|
---|
| 4534 | else if (flag != NULL)
|
---|
| 4535 | {
|
---|
| 4536 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 4537 | error = errno;
|
---|
| 4538 | sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_UNLNK,
|
---|
| 4539 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
| 4540 | lockfile);
|
---|
| 4541 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
| 4542 | }
|
---|
| 4543 | SL_RETURN((0),_("sh_unix_unlock"));
|
---|
| 4544 | }
|
---|
| 4545 |
|
---|
[265] | 4546 | int sh_unix_check_piddir (char * pidpath)
|
---|
[246] | 4547 | {
|
---|
| 4548 | static struct stat buf;
|
---|
| 4549 | int status = 0;
|
---|
| 4550 | char * pid_dir;
|
---|
| 4551 |
|
---|
| 4552 | SL_ENTER(_("sh_unix_check_piddir"));
|
---|
| 4553 |
|
---|
[265] | 4554 | pid_dir = sh_util_dirname (pidpath);
|
---|
[246] | 4555 |
|
---|
| 4556 | status = retry_lstat (FIL__, __LINE__, pid_dir, &buf);
|
---|
| 4557 |
|
---|
| 4558 | if (status < 0 && errno == ENOENT)
|
---|
| 4559 | {
|
---|
| 4560 | status = mkdir (pid_dir, 0777);
|
---|
| 4561 | if (status < 0)
|
---|
| 4562 | {
|
---|
| 4563 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4564 | MSG_E_SUBGEN,
|
---|
| 4565 | _("Cannot create PID directory"),
|
---|
| 4566 | _("sh_unix_check_piddir"));
|
---|
| 4567 | SH_FREE(pid_dir);
|
---|
| 4568 | SL_RETURN((-1),_("sh_unix_check_piddir"));
|
---|
| 4569 | }
|
---|
| 4570 | }
|
---|
| 4571 | else if (!S_ISDIR(buf.st_mode))
|
---|
| 4572 | {
|
---|
| 4573 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4574 | MSG_E_SUBGEN,
|
---|
| 4575 | _("Path of PID directory refers to a non-directory object"),
|
---|
| 4576 | _("sh_unix_check_piddir"));
|
---|
| 4577 | SH_FREE(pid_dir);
|
---|
| 4578 | SL_RETURN((-1),_("sh_unix_check_piddir"));
|
---|
| 4579 | }
|
---|
| 4580 | SH_FREE(pid_dir);
|
---|
| 4581 | SL_RETURN((0),_("sh_unix_check_piddir"));
|
---|
| 4582 | }
|
---|
| 4583 |
|
---|
[169] | 4584 | int sh_unix_lock (char * lockfile, char * flag)
|
---|
| 4585 | {
|
---|
| 4586 | int filed;
|
---|
| 4587 | int errnum;
|
---|
| 4588 | char myPid[64];
|
---|
| 4589 | SL_TICKET fd;
|
---|
| 4590 | extern int get_the_fd (SL_TICKET ticket);
|
---|
| 4591 |
|
---|
| 4592 | SL_ENTER(_("sh_unix_lock"));
|
---|
| 4593 |
|
---|
| 4594 | sprintf (myPid, "%ld\n", (long) sh.pid); /* known to fit */
|
---|
| 4595 |
|
---|
[246] | 4596 | if (flag == NULL) /* PID file, check for directory */
|
---|
| 4597 | {
|
---|
| 4598 | if (0 != sh_unix_check_piddir (lockfile))
|
---|
| 4599 | {
|
---|
| 4600 | SL_RETURN((-1),_("sh_unix_lock"));
|
---|
| 4601 | }
|
---|
| 4602 | }
|
---|
| 4603 |
|
---|
[248] | 4604 | fd = sl_open_safe_rdwr (FIL__, __LINE__,
|
---|
| 4605 | lockfile, SL_YESPRIV); /* fails if file exists */
|
---|
[169] | 4606 |
|
---|
| 4607 | if (!SL_ISERROR(fd))
|
---|
| 4608 | {
|
---|
| 4609 | errnum = sl_write (fd, myPid, sl_strlen(myPid));
|
---|
| 4610 | filed = get_the_fd(fd);
|
---|
| 4611 | fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
---|
| 4612 | sl_close (fd);
|
---|
| 4613 |
|
---|
| 4614 | if (!SL_ISERROR(errnum))
|
---|
| 4615 | {
|
---|
| 4616 | if (flag != NULL)
|
---|
| 4617 | sh.flag.islocked = GOOD;
|
---|
| 4618 | SL_RETURN((0),_("sh_unix_lock"));
|
---|
| 4619 | }
|
---|
| 4620 | }
|
---|
| 4621 |
|
---|
| 4622 | TPT((0, FIL__, __LINE__, _("msg=<open pid file failed>\n")));
|
---|
| 4623 | if (flag != NULL)
|
---|
| 4624 | sh.flag.islocked = BAD;
|
---|
| 4625 | SL_RETURN((-1),_("sh_unix_lock"));
|
---|
| 4626 |
|
---|
| 4627 | /* notreached */
|
---|
| 4628 | }
|
---|
| 4629 |
|
---|
| 4630 |
|
---|
[1] | 4631 | /* check whether file is locked
|
---|
| 4632 | */
|
---|
| 4633 | int sh_unix_test_and_lock (char * filename, char * lockfile)
|
---|
| 4634 | {
|
---|
| 4635 | static struct stat buf;
|
---|
| 4636 | int status = 0;
|
---|
| 4637 |
|
---|
| 4638 |
|
---|
| 4639 | SL_TICKET fd;
|
---|
| 4640 | char line_in[128];
|
---|
| 4641 |
|
---|
| 4642 | SL_ENTER(_("sh_unix_test_and_lock"));
|
---|
| 4643 |
|
---|
| 4644 | status = retry_lstat (FIL__, __LINE__, lockfile, &buf);
|
---|
| 4645 |
|
---|
| 4646 | /* --- No lock file found, try to lock. ---
|
---|
| 4647 | */
|
---|
| 4648 |
|
---|
| 4649 | if (status < 0 && errno == ENOENT)
|
---|
| 4650 | {
|
---|
| 4651 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
| 4652 | {
|
---|
| 4653 | if (filename != NULL)
|
---|
| 4654 | sh.flag.islocked = GOOD;
|
---|
| 4655 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
| 4656 | }
|
---|
[76] | 4657 | else
|
---|
| 4658 | {
|
---|
| 4659 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4660 | MSG_E_SUBGEN,
|
---|
[246] | 4661 | (filename == NULL) ? _("Cannot create PID file (1)") : _("Cannot create lock file (1)"),
|
---|
[76] | 4662 | _("sh_unix_test_and_lock"));
|
---|
| 4663 | SL_RETURN((-1),_("sh_unix_test_and_lock"));
|
---|
| 4664 | }
|
---|
[1] | 4665 | }
|
---|
| 4666 | else if (status == 0 && buf.st_size == 0)
|
---|
| 4667 | {
|
---|
| 4668 | if (filename != NULL)
|
---|
| 4669 | sh.flag.islocked = GOOD;
|
---|
| 4670 | sh_unix_unlock (lockfile, filename);
|
---|
| 4671 | if (filename != NULL)
|
---|
| 4672 | sh.flag.islocked = BAD;
|
---|
| 4673 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
| 4674 | {
|
---|
| 4675 | if (filename != NULL)
|
---|
| 4676 | sh.flag.islocked = GOOD;
|
---|
| 4677 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
| 4678 | }
|
---|
| 4679 | else
|
---|
| 4680 | {
|
---|
[76] | 4681 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4682 | MSG_E_SUBGEN,
|
---|
[246] | 4683 | (filename == NULL) ? _("Cannot create PID file (2)") : _("Cannot create lock file (2)"),
|
---|
[76] | 4684 | _("sh_unix_test_and_lock"));
|
---|
[1] | 4685 | SL_RETURN((-1),_("sh_unix_test_and_lock"));
|
---|
| 4686 | }
|
---|
| 4687 | }
|
---|
| 4688 |
|
---|
| 4689 | /* --- Check on lock. ---
|
---|
| 4690 | */
|
---|
| 4691 |
|
---|
| 4692 | if (status >= 0)
|
---|
| 4693 | {
|
---|
[248] | 4694 | fd = sl_open_read (FIL__, __LINE__, lockfile, SL_YESPRIV);
|
---|
[1] | 4695 | if (SL_ISERROR(fd))
|
---|
| 4696 | sh_error_handle ((-1), FIL__, __LINE__, fd,
|
---|
| 4697 | MSG_E_SUBGEN,
|
---|
| 4698 | (filename == NULL) ? _("Cannot open PID file for read") : _("Cannot open lock file for read"),
|
---|
| 4699 | _("sh_unix_test_and_lock"));
|
---|
| 4700 | }
|
---|
| 4701 | else
|
---|
| 4702 | fd = -1;
|
---|
| 4703 |
|
---|
| 4704 | if (!SL_ISERROR(fd))
|
---|
| 4705 | {
|
---|
| 4706 | /* read the PID in the lock file
|
---|
| 4707 | */
|
---|
[76] | 4708 | status = sl_read (fd, line_in, sizeof(line_in));
|
---|
| 4709 | line_in[sizeof(line_in)-1] = '\0';
|
---|
[1] | 4710 |
|
---|
| 4711 | /* convert to numeric
|
---|
| 4712 | */
|
---|
| 4713 | if (status > 0)
|
---|
| 4714 | {
|
---|
| 4715 | errno = 0;
|
---|
| 4716 | status = strtol(line_in, (char **)NULL, 10);
|
---|
| 4717 | if (errno == ERANGE || status <= 0)
|
---|
| 4718 | {
|
---|
| 4719 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4720 | MSG_E_SUBGEN,
|
---|
| 4721 | (filename == NULL) ? _("Bad PID in PID file") : _("Bad PID in lock file"),
|
---|
| 4722 | _("sh_unix_test_and_lock"));
|
---|
| 4723 |
|
---|
| 4724 | status = -1;
|
---|
| 4725 | }
|
---|
| 4726 | }
|
---|
| 4727 | else
|
---|
| 4728 | {
|
---|
| 4729 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4730 | MSG_E_SUBGEN,
|
---|
| 4731 | (filename == NULL) ? _("Cannot read PID file") : _("Cannot read lock file"),
|
---|
| 4732 | _("sh_unix_test_and_lock"));
|
---|
| 4733 | }
|
---|
| 4734 | sl_close(fd);
|
---|
| 4735 |
|
---|
[162] | 4736 | if (status > 0 && (unsigned int) status == sh.pid)
|
---|
[1] | 4737 | {
|
---|
| 4738 | if (filename != NULL)
|
---|
| 4739 | sh.flag.islocked = GOOD;
|
---|
| 4740 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
| 4741 | }
|
---|
| 4742 |
|
---|
| 4743 |
|
---|
| 4744 | /* --- Check whether the process exists. ---
|
---|
| 4745 | */
|
---|
| 4746 | if (status > 0)
|
---|
| 4747 | {
|
---|
| 4748 | errno = 0;
|
---|
| 4749 | status = aud_kill (FIL__, __LINE__, status, 0);
|
---|
| 4750 |
|
---|
| 4751 | /* Does not exist, so remove the stale lock
|
---|
| 4752 | * and create a new one.
|
---|
| 4753 | */
|
---|
| 4754 | if (status < 0 && errno == ESRCH)
|
---|
| 4755 | {
|
---|
| 4756 | if (filename != NULL)
|
---|
| 4757 | sh.flag.islocked = GOOD;
|
---|
| 4758 | if (0 != sh_unix_unlock(lockfile, filename) && (filename !=NULL))
|
---|
| 4759 | sh.flag.islocked = BAD;
|
---|
| 4760 | else
|
---|
| 4761 | {
|
---|
| 4762 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
| 4763 | {
|
---|
| 4764 | if (filename != NULL)
|
---|
| 4765 | sh.flag.islocked = GOOD;
|
---|
| 4766 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
| 4767 | }
|
---|
| 4768 | else
|
---|
| 4769 | {
|
---|
| 4770 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4771 | MSG_E_SUBGEN,
|
---|
[246] | 4772 | (filename == NULL) ? _("Cannot create PID file (3)") : _("Cannot create lock file (3)"),
|
---|
[1] | 4773 | _("sh_unix_test_and_lock"));
|
---|
| 4774 | }
|
---|
| 4775 | if (filename != NULL)
|
---|
| 4776 | sh.flag.islocked = BAD;
|
---|
| 4777 | }
|
---|
| 4778 | }
|
---|
| 4779 | else
|
---|
| 4780 | {
|
---|
| 4781 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
| 4782 | MSG_E_SUBGEN,
|
---|
| 4783 | (filename == NULL) ? _("Cannot remove stale PID file, PID may be a running process") : _("Cannot remove stale lock file, PID may be a running process"),
|
---|
| 4784 | _("sh_unix_test_and_lock"));
|
---|
| 4785 | if (filename != NULL)
|
---|
| 4786 | sh.flag.islocked = BAD;
|
---|
| 4787 | }
|
---|
| 4788 | }
|
---|
| 4789 | }
|
---|
| 4790 | SL_RETURN((-1),_("sh_unix_testlock"));
|
---|
| 4791 | }
|
---|
| 4792 |
|
---|
| 4793 | /* write the PID file
|
---|
| 4794 | */
|
---|
| 4795 | int sh_unix_write_pid_file()
|
---|
| 4796 | {
|
---|
| 4797 | return sh_unix_test_and_lock(NULL, sh.srvlog.alt);
|
---|
| 4798 | }
|
---|
| 4799 |
|
---|
| 4800 | /* write lock for filename
|
---|
| 4801 | */
|
---|
| 4802 | int sh_unix_write_lock_file(char * filename)
|
---|
| 4803 | {
|
---|
[34] | 4804 | size_t len;
|
---|
| 4805 | int res;
|
---|
[1] | 4806 | char * lockfile;
|
---|
| 4807 |
|
---|
| 4808 | if (filename == NULL)
|
---|
| 4809 | return (-1);
|
---|
| 4810 |
|
---|
[34] | 4811 | len = sl_strlen(filename);
|
---|
| 4812 | if (sl_ok_adds(len, 6))
|
---|
| 4813 | len += 6;
|
---|
| 4814 | lockfile = SH_ALLOC(len);
|
---|
| 4815 | sl_strlcpy(lockfile, filename, len);
|
---|
| 4816 | sl_strlcat(lockfile, _(".lock"), len);
|
---|
| 4817 | res = sh_unix_test_and_lock(filename, lockfile);
|
---|
[1] | 4818 | SH_FREE(lockfile);
|
---|
[34] | 4819 | return res;
|
---|
[1] | 4820 | }
|
---|
| 4821 |
|
---|
| 4822 | /* rm lock for filename
|
---|
| 4823 | */
|
---|
| 4824 | int sh_unix_rm_lock_file(char * filename)
|
---|
| 4825 | {
|
---|
[34] | 4826 | size_t len;
|
---|
| 4827 | int res;
|
---|
[1] | 4828 | char * lockfile;
|
---|
| 4829 |
|
---|
| 4830 | if (filename == NULL)
|
---|
| 4831 | return (-1);
|
---|
| 4832 |
|
---|
[34] | 4833 | len = sl_strlen(filename);
|
---|
| 4834 | if (sl_ok_adds(len, 6))
|
---|
| 4835 | len += 6;
|
---|
| 4836 | lockfile = SH_ALLOC(len);
|
---|
| 4837 | sl_strlcpy(lockfile, filename, len);
|
---|
| 4838 | sl_strlcat(lockfile, _(".lock"), len);
|
---|
| 4839 |
|
---|
| 4840 | res = sh_unix_unlock(lockfile, filename);
|
---|
[1] | 4841 | SH_FREE(lockfile);
|
---|
[34] | 4842 | return res;
|
---|
[1] | 4843 | }
|
---|
| 4844 |
|
---|
| 4845 | /* rm lock for filename
|
---|
| 4846 | */
|
---|
| 4847 | int sh_unix_rm_pid_file()
|
---|
| 4848 | {
|
---|
| 4849 | return sh_unix_unlock(sh.srvlog.alt, NULL);
|
---|
| 4850 | }
|
---|
| 4851 |
|
---|
| 4852 | /* Test whether file exists
|
---|
| 4853 | */
|
---|
[78] | 4854 | int sh_unix_file_exists(char * path)
|
---|
[1] | 4855 | {
|
---|
| 4856 | struct stat buf;
|
---|
| 4857 |
|
---|
[78] | 4858 | SL_ENTER(_("sh_unix_file_exists"));
|
---|
[1] | 4859 |
|
---|
[221] | 4860 | if (0 == retry_lstat(FIL__, __LINE__, path, &buf))
|
---|
| 4861 | SL_RETURN( S_TRUE, _("sh_unix_file_exists"));
|
---|
[1] | 4862 | else
|
---|
[221] | 4863 | SL_RETURN( S_FALSE, _("sh_unix_file_exists"));
|
---|
[1] | 4864 | }
|
---|
| 4865 |
|
---|
| 4866 |
|
---|
[78] | 4867 | /* Test whether file exists, is a character device, and allows read
|
---|
| 4868 | * access.
|
---|
[1] | 4869 | */
|
---|
[78] | 4870 | int sh_unix_device_readable(int fd)
|
---|
[1] | 4871 | {
|
---|
| 4872 | struct stat buf;
|
---|
| 4873 |
|
---|
[78] | 4874 | SL_ENTER(_("sh_unix_device_readable"));
|
---|
[1] | 4875 |
|
---|
| 4876 | if (retry_fstat(FIL__, __LINE__, fd, &buf) == -1)
|
---|
[78] | 4877 | SL_RETURN( (-1), _("sh_unix_device_readable"));
|
---|
| 4878 | else if ( S_ISCHR(buf.st_mode) && 0 != (S_IROTH & buf.st_mode) )
|
---|
| 4879 | SL_RETURN( (0), _("sh_unix_device_readable"));
|
---|
[1] | 4880 | else
|
---|
[78] | 4881 | SL_RETURN( (-1), _("sh_unix_device_readable"));
|
---|
[1] | 4882 | }
|
---|
| 4883 |
|
---|
| 4884 | static char preq[16];
|
---|
| 4885 |
|
---|
| 4886 | /* return true if database is remote
|
---|
| 4887 | */
|
---|
| 4888 | int file_is_remote ()
|
---|
| 4889 | {
|
---|
| 4890 | static int init = 0;
|
---|
[42] | 4891 | struct stat buf;
|
---|
[1] | 4892 |
|
---|
| 4893 | SL_ENTER(_("file_is_remote"));
|
---|
| 4894 |
|
---|
| 4895 | if (init == 0)
|
---|
| 4896 | {
|
---|
| 4897 | sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
|
---|
| 4898 | ++init;
|
---|
| 4899 | }
|
---|
| 4900 | if (0 == sl_strncmp (sh.data.path, preq, 15))
|
---|
| 4901 | {
|
---|
[42] | 4902 | if (sh.data.path[15] != '\0') /* should be start of path */
|
---|
| 4903 | {
|
---|
| 4904 | if (0 == stat(&(sh.data.path[15]), &buf))
|
---|
| 4905 | {
|
---|
| 4906 | SL_RETURN( S_FALSE, _("file_is_remote"));
|
---|
| 4907 | }
|
---|
[460] | 4908 | else
|
---|
| 4909 | {
|
---|
| 4910 | char * tmp = sh_util_safe_name (&(sh.data.path[15]));
|
---|
| 4911 | sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGPATH,
|
---|
| 4912 | _("No local baseline database at expected path"),
|
---|
| 4913 | _("file_is_remote"),
|
---|
| 4914 | tmp);
|
---|
| 4915 | SH_FREE(tmp);
|
---|
| 4916 | }
|
---|
[42] | 4917 | }
|
---|
[460] | 4918 | else
|
---|
| 4919 | {
|
---|
| 4920 | sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN,
|
---|
| 4921 | _("No local baseline database path known"),
|
---|
| 4922 | _("file_is_remote"));
|
---|
| 4923 | }
|
---|
[1] | 4924 | SL_RETURN( S_TRUE, _("file_is_remote"));
|
---|
| 4925 | }
|
---|
| 4926 | SL_RETURN( S_FALSE, _("file_is_remote"));
|
---|
| 4927 | }
|
---|
| 4928 |
|
---|
| 4929 | /* Return the path to the configuration/database file.
|
---|
| 4930 | */
|
---|
| 4931 | char * file_path(char what, char flag)
|
---|
| 4932 | {
|
---|
| 4933 | static int init = 0;
|
---|
| 4934 |
|
---|
| 4935 | SL_ENTER(_("file_path"));
|
---|
| 4936 |
|
---|
| 4937 | if (init == 0)
|
---|
| 4938 | {
|
---|
| 4939 | sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
|
---|
| 4940 | ++init;
|
---|
| 4941 | }
|
---|
| 4942 |
|
---|
| 4943 | switch (what)
|
---|
| 4944 | {
|
---|
| 4945 |
|
---|
| 4946 | case 'C':
|
---|
| 4947 | if (0 == sl_strncmp (sh.conf.path, preq, 15))
|
---|
| 4948 | {
|
---|
| 4949 | #if defined(SH_WITH_SERVER)
|
---|
| 4950 | if (sh.flag.isserver == S_TRUE && sl_strlen(sh.conf.path) == 15)
|
---|
| 4951 | SL_RETURN( NULL, _("file_path"));
|
---|
| 4952 | if (sh.flag.isserver == S_TRUE)
|
---|
| 4953 | SL_RETURN( &(sh.conf.path[15]), _("file_path"));
|
---|
| 4954 | #endif
|
---|
| 4955 | if (flag == 'R')
|
---|
| 4956 | SL_RETURN( preq, _("file_path"));
|
---|
| 4957 | if (flag == 'I')
|
---|
| 4958 | {
|
---|
| 4959 | if (sl_strlen(sh.conf.path) == 15)
|
---|
| 4960 | SL_RETURN( NULL, _("file_path"));
|
---|
| 4961 | else
|
---|
| 4962 | SL_RETURN( &(sh.conf.path[15]), _("file_path"));
|
---|
| 4963 | }
|
---|
| 4964 | SL_RETURN ( preq, _("file_path"));
|
---|
| 4965 | }
|
---|
| 4966 | else
|
---|
| 4967 | SL_RETURN( sh.conf.path, _("file_path"));
|
---|
| 4968 | /* break; *//* unreachable */
|
---|
| 4969 |
|
---|
| 4970 | case 'D':
|
---|
| 4971 | if (0 == sl_strncmp (sh.data.path, preq, 15))
|
---|
| 4972 | {
|
---|
| 4973 | if (flag == 'R')
|
---|
| 4974 | SL_RETURN( preq, _("file_path"));
|
---|
| 4975 | if (flag == 'W' && sl_strlen(sh.data.path) == 15)
|
---|
| 4976 | SL_RETURN (NULL, _("file_path"));
|
---|
| 4977 | if (flag == 'W')
|
---|
| 4978 | SL_RETURN( &(sh.data.path[15]), _("file_path"));
|
---|
| 4979 | }
|
---|
| 4980 | else
|
---|
| 4981 | SL_RETURN( sh.data.path, _("file_path"));
|
---|
| 4982 | break;
|
---|
| 4983 |
|
---|
| 4984 | default:
|
---|
| 4985 | SL_RETURN( NULL, _("file_path"));
|
---|
| 4986 | }
|
---|
| 4987 |
|
---|
| 4988 | return NULL; /* notreached */
|
---|
| 4989 | }
|
---|
| 4990 | /************************************************/
|
---|
| 4991 | /**** Mlock Utilities ****/
|
---|
| 4992 | /************************************************/
|
---|
| 4993 |
|
---|
| 4994 | #include <limits.h>
|
---|
| 4995 |
|
---|
| 4996 | int sh_unix_pagesize()
|
---|
| 4997 | {
|
---|
| 4998 | int pagesize = 4096;
|
---|
| 4999 | #if defined(_SC_PAGESIZE)
|
---|
| 5000 | pagesize = sysconf(_SC_PAGESIZE);
|
---|
| 5001 | #elif defined(_SC_PAGE_SIZE)
|
---|
| 5002 | pagesize = sysconf(_SC_PAGE_SIZE);
|
---|
| 5003 | #elif defined(HAVE_GETPAGESIZE)
|
---|
| 5004 | pagesize = getpagesize();
|
---|
| 5005 | #elif defined(PAGESIZE)
|
---|
| 5006 | pagesize = PAGESIZE;
|
---|
| 5007 | #endif
|
---|
| 5008 |
|
---|
| 5009 | return ((pagesize > 0) ? pagesize : 4096);
|
---|
| 5010 | }
|
---|
| 5011 |
|
---|
| 5012 | typedef struct sh_page_lt {
|
---|
| 5013 | unsigned long page_start;
|
---|
| 5014 | int page_refcount;
|
---|
[19] | 5015 | char file[64];
|
---|
| 5016 | int line;
|
---|
[1] | 5017 | struct sh_page_lt * next;
|
---|
| 5018 | } sh_page_l;
|
---|
| 5019 |
|
---|
| 5020 | sh_page_l * sh_page_locked = NULL;
|
---|
| 5021 | volatile int page_locking = 0;
|
---|
| 5022 |
|
---|
| 5023 | unsigned long sh_unix_lookup_page (void * in_addr, size_t len, int * num_pages)
|
---|
| 5024 | {
|
---|
| 5025 | int pagesize = sh_unix_pagesize();
|
---|
| 5026 | unsigned long addr = (unsigned long) in_addr;
|
---|
| 5027 |
|
---|
| 5028 | unsigned long pagebase;
|
---|
| 5029 | unsigned long pagediff;
|
---|
| 5030 | unsigned long pagenum = addr / pagesize;
|
---|
| 5031 |
|
---|
| 5032 | SL_ENTER(_("sh_unix_lookup_page"));
|
---|
| 5033 | #if 0
|
---|
| 5034 | fprintf(stderr, "mlock: --> base %ld, pagenum: %ld\n",
|
---|
| 5035 | addr, pagenum);
|
---|
| 5036 | #endif
|
---|
| 5037 |
|
---|
| 5038 | /* address of first page
|
---|
| 5039 | */
|
---|
| 5040 | pagebase = pagenum * pagesize;
|
---|
| 5041 |
|
---|
| 5042 | /* number of pages
|
---|
| 5043 | */
|
---|
| 5044 | pagediff = (addr + len) - pagebase;
|
---|
| 5045 | pagenum = pagediff / pagesize;
|
---|
| 5046 | if (pagenum * pagesize < pagediff)
|
---|
| 5047 | ++pagenum;
|
---|
| 5048 |
|
---|
| 5049 | #if 0
|
---|
| 5050 | fprintf(stderr, "mlock: --> pagebase %ld, pagediff %ld, (addr + len) %ld\n",
|
---|
| 5051 | pagebase, pagediff, (addr + len));
|
---|
| 5052 | #endif
|
---|
| 5053 |
|
---|
| 5054 | *num_pages = pagenum;
|
---|
| 5055 | SL_RETURN((pagebase), _("sh_unix_lookup_page"));
|
---|
| 5056 | }
|
---|
| 5057 |
|
---|
| 5058 |
|
---|
| 5059 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
[134] | 5060 |
|
---|
| 5061 | SH_MUTEX_STATIC(mutex_mlock,PTHREAD_MUTEX_INITIALIZER);
|
---|
| 5062 |
|
---|
[170] | 5063 | int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
|
---|
[1] | 5064 | {
|
---|
| 5065 | int num_pages;
|
---|
| 5066 | int status = 0;
|
---|
| 5067 | int pagesize;
|
---|
[253] | 5068 | sh_page_l * page_list;
|
---|
[1] | 5069 | unsigned long addr;
|
---|
| 5070 | #ifdef TEST_MLOCK
|
---|
| 5071 | int i = 0;
|
---|
| 5072 | #endif
|
---|
| 5073 |
|
---|
| 5074 | SL_ENTER(_("sh_unix_mlock"));
|
---|
| 5075 |
|
---|
[134] | 5076 | /* There's no cancellation point here, except if tracing is on
|
---|
| 5077 | */
|
---|
| 5078 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
| 5079 |
|
---|
[253] | 5080 | page_list = sh_page_locked;
|
---|
| 5081 |
|
---|
[1] | 5082 | if (0 != page_locking)
|
---|
| 5083 | {
|
---|
[143] | 5084 | status = -1;
|
---|
| 5085 | goto exit_mlock;
|
---|
[1] | 5086 | }
|
---|
[134] | 5087 |
|
---|
[1] | 5088 | page_locking = 1;
|
---|
| 5089 |
|
---|
| 5090 | pagesize = sh_unix_pagesize();
|
---|
| 5091 | addr = sh_unix_lookup_page (in_addr, len, &num_pages);
|
---|
| 5092 |
|
---|
| 5093 | #ifdef TEST_MLOCK
|
---|
| 5094 | fprintf(stderr, "mlock: addr %ld, base %ld, pages: %d, length %d\n",
|
---|
| 5095 | (unsigned long) in_addr, addr, num_pages, len);
|
---|
| 5096 | #endif
|
---|
| 5097 |
|
---|
| 5098 | /* increase refcount of locked pages
|
---|
| 5099 | * addr is first page; num_pages is #(consecutive pages) to lock
|
---|
| 5100 | */
|
---|
[134] | 5101 |
|
---|
[1] | 5102 | while ((page_list != NULL) && (num_pages > 0))
|
---|
| 5103 | {
|
---|
| 5104 | #ifdef TEST_MLOCK
|
---|
| 5105 | fprintf(stderr, "mlock: check page %d: %ld [%d]\n",
|
---|
| 5106 | i, page_list->page_start, page_list->page_refcount);
|
---|
| 5107 | #endif
|
---|
| 5108 | if (page_list->page_start == addr)
|
---|
| 5109 | {
|
---|
| 5110 | page_list->page_refcount += 1;
|
---|
| 5111 | num_pages -= 1;
|
---|
| 5112 | addr += pagesize;
|
---|
| 5113 | #ifdef TEST_MLOCK
|
---|
| 5114 | fprintf(stderr, "mlock: found page %d: %ld [%d], next page %ld\n",
|
---|
| 5115 | i, page_list->page_start, page_list->page_refcount, addr);
|
---|
| 5116 | #endif
|
---|
| 5117 | }
|
---|
| 5118 | #ifdef TEST_MLOCK
|
---|
| 5119 | ++i;
|
---|
| 5120 | #endif
|
---|
| 5121 | page_list = page_list->next;
|
---|
| 5122 | }
|
---|
| 5123 |
|
---|
| 5124 | /* mlock some more pages, if needed
|
---|
| 5125 | */
|
---|
| 5126 | while (num_pages > 0)
|
---|
| 5127 | {
|
---|
| 5128 | #ifdef TEST_MLOCK
|
---|
| 5129 | fprintf(stderr, "mlock: lock page %d: mlock %ld [num_pages %d]\n",
|
---|
| 5130 | i, addr, num_pages);
|
---|
| 5131 | ++i;
|
---|
| 5132 | #endif
|
---|
| 5133 | page_list = SH_ALLOC(sizeof(sh_page_l));
|
---|
| 5134 | page_list->page_start = addr;
|
---|
| 5135 | page_list->page_refcount = 1;
|
---|
[19] | 5136 | sl_strlcpy(page_list->file, file, 64);
|
---|
| 5137 | page_list->line = line;
|
---|
[1] | 5138 | status = mlock( (void *) addr, pagesize);
|
---|
| 5139 | if (status != 0)
|
---|
| 5140 | {
|
---|
| 5141 | #ifdef TEST_MLOCK
|
---|
[132] | 5142 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 5143 | fprintf(stderr, "mlock: error: %s\n",
|
---|
| 5144 | sh_error_message(errno, errbuf, sizeof(errbuf)));
|
---|
[1] | 5145 | #endif
|
---|
| 5146 | SH_FREE(page_list);
|
---|
| 5147 | page_locking = 0;
|
---|
[143] | 5148 | goto exit_mlock;
|
---|
[1] | 5149 | }
|
---|
| 5150 | page_list->next = sh_page_locked;
|
---|
| 5151 | sh_page_locked = page_list;
|
---|
| 5152 | num_pages -= 1;
|
---|
| 5153 | addr += pagesize;
|
---|
| 5154 | }
|
---|
[134] | 5155 | page_locking = 0;
|
---|
[143] | 5156 |
|
---|
| 5157 | exit_mlock:
|
---|
[134] | 5158 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
[1] | 5159 |
|
---|
| 5160 | SL_RETURN((status), _("sh_unix_mlock"));
|
---|
| 5161 | }
|
---|
| 5162 | #else
|
---|
[175] | 5163 | int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
|
---|
[1] | 5164 | {
|
---|
[49] | 5165 | (void) file; (void) line;
|
---|
[1] | 5166 | (void) in_addr; (void) len;
|
---|
| 5167 | return -1;
|
---|
| 5168 | }
|
---|
| 5169 | #endif
|
---|
| 5170 |
|
---|
| 5171 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
| 5172 | int sh_unix_munlock (void * in_addr, size_t len)
|
---|
| 5173 | {
|
---|
| 5174 | int num_pages;
|
---|
[253] | 5175 | int unlocked;
|
---|
| 5176 | int status;
|
---|
[1] | 5177 | int pagesize;
|
---|
[253] | 5178 | sh_page_l * page_list;
|
---|
[1] | 5179 | sh_page_l * page_last;
|
---|
| 5180 | unsigned long addr;
|
---|
| 5181 |
|
---|
| 5182 | int test_count;
|
---|
| 5183 | int test_status;
|
---|
| 5184 | int test_pages;
|
---|
| 5185 |
|
---|
| 5186 | #ifdef TEST_MLOCK
|
---|
| 5187 | int i = 0;
|
---|
| 5188 | #endif
|
---|
| 5189 |
|
---|
| 5190 | SL_ENTER(_("sh_unix_munlock"));
|
---|
| 5191 |
|
---|
[134] | 5192 | /* There's no cancellation point here, except if tracing is on
|
---|
| 5193 | */
|
---|
| 5194 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
| 5195 |
|
---|
[253] | 5196 | unlocked = 0;
|
---|
| 5197 | status = 0;
|
---|
| 5198 | page_list = sh_page_locked;
|
---|
| 5199 |
|
---|
[1] | 5200 | if (0 != page_locking)
|
---|
| 5201 | {
|
---|
[143] | 5202 | status = -1;
|
---|
| 5203 | goto exit_munlock;
|
---|
[1] | 5204 | }
|
---|
| 5205 | page_locking = 1;
|
---|
| 5206 |
|
---|
| 5207 | pagesize = sh_unix_pagesize();
|
---|
| 5208 | addr = sh_unix_lookup_page (in_addr, len, &num_pages);
|
---|
| 5209 |
|
---|
| 5210 | #ifdef TEST_MLOCK
|
---|
| 5211 | fprintf(stderr, "munlock: in_addr %ld, addr %ld, pages: %d, length %d\n",
|
---|
| 5212 | (unsigned long) in_addr, addr, num_pages, len);
|
---|
| 5213 | #endif
|
---|
| 5214 |
|
---|
| 5215 | test_pages = num_pages;
|
---|
| 5216 |
|
---|
| 5217 | /* reduce refcount of locked pages
|
---|
| 5218 | * addr is first page; num_pages is #(consecutive pages) to lock
|
---|
| 5219 | */
|
---|
| 5220 | while ((page_list != NULL) && (num_pages > 0))
|
---|
| 5221 | {
|
---|
| 5222 | #ifdef TEST_MLOCK
|
---|
| 5223 | fprintf(stderr, "munlock: page %d: %ld [%d]\n",
|
---|
| 5224 | i, page_list->page_start, page_list->page_refcount);
|
---|
| 5225 | #endif
|
---|
| 5226 |
|
---|
| 5227 | test_status = 0;
|
---|
| 5228 | for (test_count = 0; test_count < test_pages; ++test_count)
|
---|
| 5229 | {
|
---|
| 5230 | if (page_list->page_start == (addr + (test_count * pagesize)))
|
---|
| 5231 | {
|
---|
| 5232 | test_status = 1;
|
---|
| 5233 | break;
|
---|
| 5234 | }
|
---|
| 5235 | }
|
---|
| 5236 |
|
---|
| 5237 | if (test_status == 1)
|
---|
| 5238 | {
|
---|
| 5239 | page_list->page_refcount -= 1;
|
---|
| 5240 | if (page_list->page_refcount == 0)
|
---|
| 5241 | {
|
---|
| 5242 | status = munlock ( (void *) addr, pagesize);
|
---|
| 5243 | ++unlocked;
|
---|
| 5244 | }
|
---|
| 5245 | num_pages -= 1;
|
---|
| 5246 | #ifdef TEST_MLOCK
|
---|
| 5247 | fprintf(stderr,
|
---|
| 5248 | "munlock: page %d: %ld [refcount %d], refcount reduced\n",
|
---|
| 5249 | i, page_list->page_start, page_list->page_refcount);
|
---|
| 5250 | #endif
|
---|
| 5251 | }
|
---|
| 5252 | #ifdef TEST_MLOCK
|
---|
| 5253 | ++i;
|
---|
| 5254 | #endif
|
---|
| 5255 | page_list = page_list->next;
|
---|
| 5256 | }
|
---|
| 5257 |
|
---|
| 5258 | #ifdef TEST_MLOCK
|
---|
| 5259 | i = 0;
|
---|
| 5260 | #endif
|
---|
| 5261 |
|
---|
| 5262 | if (unlocked > 0)
|
---|
| 5263 | {
|
---|
| 5264 | page_list = sh_page_locked;
|
---|
| 5265 | page_last = sh_page_locked;
|
---|
| 5266 |
|
---|
| 5267 | while ((page_list != NULL) && (unlocked > 0))
|
---|
| 5268 | {
|
---|
| 5269 | if (page_list->page_refcount == 0)
|
---|
| 5270 | {
|
---|
| 5271 | #ifdef TEST_MLOCK
|
---|
| 5272 | fprintf(stderr, "munlock: remove page %d: %ld [refcount %d]\n",
|
---|
| 5273 | i, page_list->page_start, page_list->page_refcount);
|
---|
| 5274 | #endif
|
---|
| 5275 | if (page_last != page_list)
|
---|
| 5276 | {
|
---|
| 5277 | page_last->next = page_list->next;
|
---|
| 5278 | SH_FREE(page_list);
|
---|
| 5279 | page_list = page_last->next;
|
---|
| 5280 | }
|
---|
| 5281 | else
|
---|
| 5282 | {
|
---|
| 5283 | page_last = page_list->next;
|
---|
| 5284 | if (page_list == sh_page_locked)
|
---|
| 5285 | sh_page_locked = page_list->next;
|
---|
| 5286 | SH_FREE(page_list);
|
---|
| 5287 | page_list = page_last;
|
---|
| 5288 | }
|
---|
| 5289 | --unlocked;
|
---|
| 5290 | }
|
---|
| 5291 | else
|
---|
| 5292 | {
|
---|
| 5293 | #ifdef TEST_MLOCK
|
---|
| 5294 | fprintf(stderr, "munlock: skip page %d: %ld [refcount %d]\n",
|
---|
| 5295 | i, page_list->page_start, page_list->page_refcount);
|
---|
| 5296 | #endif
|
---|
| 5297 |
|
---|
| 5298 | page_last = page_list;
|
---|
| 5299 | page_list = page_list->next;
|
---|
| 5300 | }
|
---|
| 5301 | #ifdef TEST_MLOCK
|
---|
| 5302 | ++i;
|
---|
| 5303 | #endif
|
---|
| 5304 | }
|
---|
| 5305 | }
|
---|
| 5306 |
|
---|
| 5307 | page_locking = 0;
|
---|
[143] | 5308 |
|
---|
| 5309 | exit_munlock:
|
---|
[134] | 5310 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
[1] | 5311 | SL_RETURN((status), _("sh_unix_munlock"));
|
---|
| 5312 | }
|
---|
| 5313 | #else
|
---|
| 5314 | int sh_unix_munlock (void * in_addr, size_t len)
|
---|
| 5315 | {
|
---|
| 5316 | (void) in_addr; (void) len;
|
---|
| 5317 | return -1;
|
---|
| 5318 | }
|
---|
| 5319 | #endif
|
---|
| 5320 |
|
---|
| 5321 | int sh_unix_count_mlock()
|
---|
| 5322 | {
|
---|
[481] | 5323 | unsigned int i = 0;
|
---|
[253] | 5324 | char str[32][64];
|
---|
| 5325 | sh_page_l * page_list;
|
---|
[1] | 5326 |
|
---|
| 5327 | SL_ENTER(_("sh_unix_count_mlock"));
|
---|
[253] | 5328 |
|
---|
[263] | 5329 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
[253] | 5330 | /* There's no cancellation point here, except if tracing is on
|
---|
| 5331 | */
|
---|
| 5332 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
[263] | 5333 | #endif
|
---|
[253] | 5334 |
|
---|
| 5335 | page_list = sh_page_locked;
|
---|
| 5336 |
|
---|
[1] | 5337 | while (page_list != NULL)
|
---|
| 5338 | {
|
---|
[19] | 5339 | #ifdef WITH_TPT
|
---|
[253] | 5340 | if (i < 32)
|
---|
| 5341 | sl_snprintf(str[i], 64, _("file: %s line: %d page: %d"),
|
---|
| 5342 | page_list->file, page_list->line, i+1);
|
---|
[19] | 5343 | #endif
|
---|
[1] | 5344 | page_list = page_list->next;
|
---|
| 5345 | ++i;
|
---|
| 5346 | }
|
---|
[253] | 5347 |
|
---|
[263] | 5348 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
[253] | 5349 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
[263] | 5350 | #endif
|
---|
[253] | 5351 |
|
---|
| 5352 | #ifdef WITH_TPT
|
---|
| 5353 | {
|
---|
[481] | 5354 | unsigned int j = 0;
|
---|
[253] | 5355 | while (j < i && j < 32)
|
---|
| 5356 | {
|
---|
[481] | 5357 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, j, MSG_E_SUBGEN,
|
---|
[253] | 5358 | str[j], _("sh_unix_count_mlock"));
|
---|
| 5359 | ++j;
|
---|
| 5360 | }
|
---|
| 5361 | }
|
---|
| 5362 | #endif
|
---|
| 5363 |
|
---|
| 5364 | sl_snprintf(str[0], 64, _("%d pages locked"), i);
|
---|
[481] | 5365 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, i, MSG_E_SUBGEN,
|
---|
[253] | 5366 | str[0], _("sh_unix_count_mlock"));
|
---|
[1] | 5367 | SL_RETURN((i), _("sh_unix_count_mlock"));
|
---|
| 5368 | }
|
---|
| 5369 |
|
---|
| 5370 | /************************************************/
|
---|
| 5371 | /************************************************/
|
---|
| 5372 | /**** Stealth Utilities ****/
|
---|
| 5373 | /************************************************/
|
---|
| 5374 | /************************************************/
|
---|
| 5375 | #ifdef SH_STEALTH
|
---|
| 5376 |
|
---|
| 5377 | void sh_unix_xor_code (char * str, int len)
|
---|
| 5378 | {
|
---|
| 5379 | register int i;
|
---|
| 5380 |
|
---|
| 5381 | for (i = 0; i < len; ++i) str[i] ^= (char) XOR_CODE;
|
---|
| 5382 | return;
|
---|
| 5383 | }
|
---|
| 5384 |
|
---|
| 5385 | #if !defined(SH_STEALTH_MICRO)
|
---|
| 5386 |
|
---|
| 5387 |
|
---|
[89] | 5388 | int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
|
---|
| 5389 | unsigned long * bytes_read);
|
---|
[1] | 5390 | unsigned long first_hex_block(SL_TICKET fd, unsigned long * max);
|
---|
| 5391 |
|
---|
| 5392 | /*
|
---|
| 5393 | * --- Get hidden data from a block of hex data. ---
|
---|
| 5394 | */
|
---|
| 5395 | int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len)
|
---|
| 5396 | {
|
---|
[89] | 5397 | int add_off = 0, llen;
|
---|
| 5398 | static unsigned long off_data = 0;
|
---|
| 5399 | static unsigned long max_data = 0;
|
---|
| 5400 | static unsigned long bytes_read = 0;
|
---|
[52] | 5401 | static int stealth_init = BAD;
|
---|
[1] | 5402 |
|
---|
| 5403 | SL_ENTER(_("sh_unix_getline_stealth"));
|
---|
| 5404 |
|
---|
[194] | 5405 | if (str == NULL)
|
---|
| 5406 | {
|
---|
| 5407 | off_data = 0;
|
---|
| 5408 | max_data = 0;
|
---|
| 5409 | bytes_read = 0;
|
---|
| 5410 | stealth_init = BAD;
|
---|
| 5411 | SL_RETURN(0, _("sh_unix_getline_stealth"));
|
---|
| 5412 | }
|
---|
[34] | 5413 |
|
---|
[1] | 5414 | /* --- Initialize. ---
|
---|
| 5415 | */
|
---|
| 5416 | if (stealth_init == BAD)
|
---|
| 5417 | {
|
---|
| 5418 | off_data = first_hex_block(fd, &max_data);
|
---|
| 5419 | if (off_data == 0)
|
---|
| 5420 | {
|
---|
| 5421 | dlog(1, FIL__, __LINE__,
|
---|
| 5422 | _("The stealth config file does not contain any steganographically\nhidden data. This file must be an image file in _uncompressed_\npostscript format.\nTo hide data in it, use:\n samhain_stealth -s postscript_file orig_config_file\n mv postscript_file /path/to/config/file\n"));
|
---|
| 5423 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
|
---|
| 5424 | _("Stealth config file."));
|
---|
| 5425 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 5426 | }
|
---|
| 5427 | stealth_init = GOOD;
|
---|
[52] | 5428 | max_data += off_data;
|
---|
[1] | 5429 | }
|
---|
| 5430 |
|
---|
| 5431 | /* --- Seek to proper position. ---
|
---|
| 5432 | */
|
---|
[89] | 5433 | if (bytes_read >= max_data || add_off < 0)
|
---|
[52] | 5434 | {
|
---|
| 5435 | dlog(1, FIL__, __LINE__,
|
---|
| 5436 | _("The capacity of the container image file for the stealth config file seems to be too small. Your config file is likely truncated.\n"));
|
---|
| 5437 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
|
---|
| 5438 | _("Stealth config file."));
|
---|
| 5439 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 5440 | }
|
---|
[1] | 5441 | sl_seek(fd, off_data);
|
---|
| 5442 |
|
---|
| 5443 | /* --- Read one line. ---
|
---|
| 5444 | */
|
---|
[89] | 5445 | add_off = hideout_hex_block(fd, (unsigned char *) str, len, &bytes_read);
|
---|
[1] | 5446 | off_data += add_off;
|
---|
| 5447 |
|
---|
| 5448 | llen = sl_strlen(str);
|
---|
| 5449 | SL_RETURN(llen, _("sh_unix_getline_stealth"));
|
---|
| 5450 | }
|
---|
| 5451 |
|
---|
[89] | 5452 | int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
|
---|
| 5453 | unsigned long * bytes_read)
|
---|
[1] | 5454 | {
|
---|
| 5455 |
|
---|
| 5456 | register int i, j, k;
|
---|
| 5457 | unsigned char c, e;
|
---|
| 5458 | register int num;
|
---|
| 5459 | unsigned char mask[9] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
|
---|
| 5460 | unsigned long here = 0;
|
---|
| 5461 | unsigned long retval = 0;
|
---|
[89] | 5462 | unsigned long bread = 0;
|
---|
[1] | 5463 |
|
---|
| 5464 | SL_ENTER(_("hideout_hex_block"));
|
---|
| 5465 |
|
---|
[34] | 5466 | ASSERT_RET((len > 1), _("len > 1"), (0));
|
---|
| 5467 |
|
---|
| 5468 | --len;
|
---|
| 5469 |
|
---|
[1] | 5470 | i = 0;
|
---|
| 5471 | while (i < len)
|
---|
| 5472 | {
|
---|
| 5473 | for (j = 0; j < 8; ++j)
|
---|
| 5474 | {
|
---|
| 5475 |
|
---|
| 5476 | /* --- Get a low byte, modify, read back. ---
|
---|
| 5477 | */
|
---|
| 5478 | for (k = 0; k < 2; ++k)
|
---|
| 5479 | {
|
---|
| 5480 | /* -- Skip whitespace. ---
|
---|
| 5481 | */
|
---|
| 5482 | c = ' ';
|
---|
| 5483 | do {
|
---|
| 5484 | do {
|
---|
| 5485 | num = sl_read (fd, &c, 1);
|
---|
| 5486 | } while (num == 0 && errno == EINTR);
|
---|
[89] | 5487 | if (num > 0)
|
---|
| 5488 | ++here;
|
---|
| 5489 | else if (num == 0)
|
---|
[34] | 5490 | SL_RETURN((0), _("hideout_hex_block"));
|
---|
[89] | 5491 | else
|
---|
| 5492 | SL_RETURN((-1), _("hideout_hex_block"));
|
---|
[1] | 5493 | } while (c == '\n' || c == '\t' || c == '\r' ||
|
---|
| 5494 | c == ' ');
|
---|
| 5495 | }
|
---|
| 5496 |
|
---|
| 5497 |
|
---|
| 5498 | /* --- e is the value of the low byte. ---
|
---|
| 5499 | */
|
---|
| 5500 | e = (unsigned char) sh_util_hexchar( c );
|
---|
| 5501 | if ((e & mask[7]) != 0) /* bit is set */
|
---|
| 5502 | str[i] |= mask[j];
|
---|
| 5503 | else /* bit is not set */
|
---|
| 5504 | str[i] &= ~mask[j];
|
---|
| 5505 |
|
---|
[89] | 5506 | bread += 1;
|
---|
[1] | 5507 | }
|
---|
| 5508 | if (str[i] == '\n') break;
|
---|
| 5509 | ++i;
|
---|
| 5510 | }
|
---|
| 5511 |
|
---|
| 5512 | if (i != 0)
|
---|
| 5513 | str[i] = '\0';
|
---|
| 5514 | else
|
---|
[34] | 5515 | str[i+1] = '\0'; /* keep newline and terminate */
|
---|
[1] | 5516 | retval += here;
|
---|
[89] | 5517 | *bytes_read += (bread/8);
|
---|
[1] | 5518 |
|
---|
| 5519 | SL_RETURN(retval, _("hideout_hex_block"));
|
---|
| 5520 | }
|
---|
| 5521 |
|
---|
| 5522 | /* --- Get offset of first data block. ---
|
---|
| 5523 | */
|
---|
| 5524 | unsigned long first_hex_block(SL_TICKET fd, unsigned long * max)
|
---|
| 5525 | {
|
---|
[34] | 5526 | unsigned int i;
|
---|
| 5527 | long num = 1;
|
---|
| 5528 | unsigned long lnum;
|
---|
[1] | 5529 | char c;
|
---|
| 5530 | int nothex = 0;
|
---|
| 5531 | unsigned long retval = 0;
|
---|
[34] | 5532 | unsigned int this_line = 0;
|
---|
[1] | 5533 | char theline[SH_BUFSIZE];
|
---|
| 5534 |
|
---|
| 5535 | SL_ENTER(_("first_hex_block"));
|
---|
| 5536 |
|
---|
| 5537 | *max = 0;
|
---|
| 5538 |
|
---|
| 5539 | while (1)
|
---|
| 5540 | {
|
---|
| 5541 | theline[0] = '\0';
|
---|
| 5542 | this_line = 0;
|
---|
| 5543 | c = '\0';
|
---|
[481] | 5544 | while (c != '\n' && this_line < (sizeof(theline)-1))
|
---|
[1] | 5545 | {
|
---|
| 5546 | do {
|
---|
| 5547 | num = sl_read (fd, &c, 1);
|
---|
| 5548 | } while (num == 0 && errno == EINTR);
|
---|
| 5549 | if (num > 0)
|
---|
| 5550 | theline[this_line] = c;
|
---|
| 5551 | else
|
---|
| 5552 | SL_RETURN((0), _("first_hex_block"));
|
---|
[34] | 5553 | ++this_line;
|
---|
[1] | 5554 | }
|
---|
| 5555 | theline[this_line] = '\0';
|
---|
| 5556 |
|
---|
| 5557 | /* not only 'newline' */
|
---|
| 5558 | if (this_line > 60)
|
---|
| 5559 | {
|
---|
| 5560 | nothex = 0;
|
---|
| 5561 | i = 0;
|
---|
| 5562 | while (nothex == 0 && i < (this_line-1))
|
---|
| 5563 | {
|
---|
| 5564 | if (! isxdigit((int)theline[i])) nothex = 1;
|
---|
| 5565 | ++i;
|
---|
| 5566 | }
|
---|
| 5567 | if (nothex == 1) retval += this_line;
|
---|
| 5568 | }
|
---|
| 5569 | else
|
---|
| 5570 | {
|
---|
| 5571 | nothex = 1;
|
---|
| 5572 | retval += this_line;
|
---|
| 5573 | }
|
---|
| 5574 |
|
---|
| 5575 | if (nothex == 0)
|
---|
| 5576 | {
|
---|
| 5577 | *max = 0;
|
---|
| 5578 | do {
|
---|
| 5579 | do {
|
---|
| 5580 | num = sl_read (fd, theline, SH_BUFSIZE);
|
---|
| 5581 | } while (num == 0 && errno == EINTR);
|
---|
[34] | 5582 | if (num > 0)
|
---|
| 5583 | {
|
---|
| 5584 | lnum = (unsigned long) num;
|
---|
| 5585 | for (i = 0; i < lnum; ++i)
|
---|
| 5586 | {
|
---|
| 5587 | c = theline[i];
|
---|
| 5588 | if (c == '\n' || c == '\t' || c == '\r' || c == ' ')
|
---|
| 5589 | ;
|
---|
| 5590 | else if (!isxdigit((int)c))
|
---|
| 5591 | break;
|
---|
| 5592 | else
|
---|
| 5593 | *max += 1;
|
---|
| 5594 | }
|
---|
[1] | 5595 | }
|
---|
| 5596 | } while (num > 0);
|
---|
| 5597 |
|
---|
| 5598 | *max /= 16;
|
---|
| 5599 | SL_RETURN((retval), _("first_hex_block"));
|
---|
| 5600 | }
|
---|
| 5601 |
|
---|
| 5602 | }
|
---|
| 5603 | /* SL_RETURN((0), _("first_hex_block")); *//* unreachable */
|
---|
| 5604 | }
|
---|
| 5605 |
|
---|
| 5606 | /* if !defined(SH_STEALTH_MICRO) */
|
---|
| 5607 | #endif
|
---|
| 5608 |
|
---|
| 5609 | /* ifdef SH_STEALTH */
|
---|
| 5610 | #endif
|
---|
| 5611 |
|
---|
| 5612 | /*
|
---|
| 5613 | * anti-debugger code
|
---|
| 5614 | */
|
---|
| 5615 | #if defined(SCREW_IT_UP)
|
---|
[408] | 5616 |
|
---|
| 5617 | #if defined(HAVE_PTHREAD)
|
---|
| 5618 |
|
---|
[411] | 5619 | static pthread_key_t gSigtrapVariables_key;
|
---|
| 5620 | static pthread_once_t gSigtrapVariables_key_once = PTHREAD_ONCE_INIT;
|
---|
[408] | 5621 |
|
---|
[411] | 5622 | static inline void make_gSigtrapVariables_key()
|
---|
[408] | 5623 | {
|
---|
[411] | 5624 | (void) pthread_key_create(&gSigtrapVariables_key, free);
|
---|
[408] | 5625 | }
|
---|
| 5626 |
|
---|
[411] | 5627 | struct sh_sigtrap_variables * sh_sigtrap_variables_get()
|
---|
[408] | 5628 | {
|
---|
| 5629 | void * ptr;
|
---|
| 5630 |
|
---|
[411] | 5631 | (void) pthread_once(&gSigtrapVariables_key_once, make_gSigtrapVariables_key);
|
---|
[408] | 5632 |
|
---|
[411] | 5633 | ptr = pthread_getspecific(gSigtrapVariables_key);
|
---|
| 5634 | if (ptr == NULL) {
|
---|
[454] | 5635 | ptr = calloc(1,sizeof(struct sh_sigtrap_variables));
|
---|
[411] | 5636 | if (ptr == NULL) {
|
---|
| 5637 | return NULL;
|
---|
[408] | 5638 | }
|
---|
[411] | 5639 | (void) pthread_setspecific(gSigtrapVariables_key, ptr);
|
---|
| 5640 | }
|
---|
[408] | 5641 |
|
---|
[411] | 5642 | return (struct sh_sigtrap_variables *) ptr;
|
---|
[408] | 5643 | }
|
---|
| 5644 |
|
---|
| 5645 | /* !defined(HAVE_PTHREAD) */
|
---|
| 5646 | #else
|
---|
| 5647 |
|
---|
[411] | 5648 | static struct sh_sigtrap_variables global_sigtrap_variables;
|
---|
| 5649 | struct sh_sigtrap_variables * sh_sigtrap_variables_get()
|
---|
[408] | 5650 | {
|
---|
[411] | 5651 | return &global_sigtrap_variables;
|
---|
[408] | 5652 | }
|
---|
[411] | 5653 |
|
---|
[1] | 5654 | #endif
|
---|
| 5655 |
|
---|
[411] | 5656 | int sh_sigtrap_max_duration_set (const char * str)
|
---|
[409] | 5657 | {
|
---|
[411] | 5658 | /* For security (prevent reloading with larger value)
|
---|
| 5659 | * this value can only be set once.
|
---|
| 5660 | */
|
---|
| 5661 | static int once = 0;
|
---|
| 5662 | int i;
|
---|
[409] | 5663 |
|
---|
[411] | 5664 | SL_ENTER(_("sh_sigtrap_max_duration_set"));
|
---|
[408] | 5665 |
|
---|
[411] | 5666 | i = atoi (str);
|
---|
[409] | 5667 |
|
---|
[411] | 5668 | if (i >= 0 && once == 0)
|
---|
| 5669 | {
|
---|
| 5670 | sh.sigtrap_max_duration = i;
|
---|
| 5671 | once = 1;
|
---|
| 5672 | }
|
---|
| 5673 | else
|
---|
| 5674 | {
|
---|
| 5675 | SL_RETURN ((-1), _("sh_sigtrap_max_duration_set"));
|
---|
| 5676 | }
|
---|
| 5677 | SL_RETURN( (0), _("sh_sigtrap_max_duration_set"));
|
---|
| 5678 | }
|
---|
| 5679 |
|
---|
[1] | 5680 | void sh_sigtrap_handler (int signum)
|
---|
| 5681 | {
|
---|
[411] | 5682 | struct sh_sigtrap_variables * sigtrap_variables;
|
---|
| 5683 | sigtrap_variables = sh_sigtrap_variables_get();
|
---|
| 5684 | if (sigtrap_variables == NULL) {
|
---|
| 5685 | /* Perhaps, it's better to not die, and to continue using Samhain,
|
---|
| 5686 | even if this part does not work. */
|
---|
| 5687 | return;
|
---|
| 5688 | }
|
---|
| 5689 |
|
---|
[1] | 5690 | #ifdef HAVE_GETTIMEOFDAY
|
---|
[411] | 5691 | {
|
---|
| 5692 | struct timeval tv;
|
---|
| 5693 | long difftv;
|
---|
| 5694 |
|
---|
| 5695 | gettimeofday(&tv, NULL);
|
---|
| 5696 | difftv = (tv.tv_sec - sigtrap_variables->save_tv.tv_sec) * 1000000 +
|
---|
| 5697 | (tv.tv_usec - sigtrap_variables->save_tv.tv_usec);
|
---|
| 5698 | if (difftv > sh.sigtrap_max_duration)
|
---|
| 5699 | raise(SIGKILL);
|
---|
| 5700 | }
|
---|
| 5701 | #endif
|
---|
[408] | 5702 |
|
---|
[411] | 5703 | sigtrap_variables->not_traced = signum;
|
---|
[473] | 5704 | /* cppcheck-suppress memleak */
|
---|
[1] | 5705 | return;
|
---|
| 5706 | }
|
---|
| 5707 | #endif
|
---|