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