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