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