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