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