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