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