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