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