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 | #else
|
---|
3511 | (void) buf;
|
---|
3512 | #endif
|
---|
3513 |
|
---|
3514 | if (sh_unix_check_selinux == S_TRUE)
|
---|
3515 | {
|
---|
3516 | out = sh_unix_getinfo_xattr_int(path, fd, _("security.selinux"));
|
---|
3517 |
|
---|
3518 | if (out)
|
---|
3519 | {
|
---|
3520 | if (collect) {
|
---|
3521 | tmp = sh_util_strconcat(_("selinux:"), out, ":", collect, NULL);
|
---|
3522 | SH_FREE(collect);
|
---|
3523 | }
|
---|
3524 | else {
|
---|
3525 | tmp = sh_util_strconcat(_("selinux:"), out, NULL);
|
---|
3526 | }
|
---|
3527 | SH_FREE(out);
|
---|
3528 | collect = tmp;
|
---|
3529 | }
|
---|
3530 | }
|
---|
3531 |
|
---|
3532 | SL_RETURN((collect),_("sh_unix_getinfo_xattr"));
|
---|
3533 | }
|
---|
3534 | #endif
|
---|
3535 |
|
---|
3536 | #ifdef USE_XATTR
|
---|
3537 | int sh_unix_setcheckselinux (const char * c)
|
---|
3538 | {
|
---|
3539 | int i;
|
---|
3540 | SL_ENTER(_("sh_unix_setcheckselinux"));
|
---|
3541 | i = sh_util_flagval(c, &(sh_unix_check_selinux));
|
---|
3542 |
|
---|
3543 | SL_RETURN(i, _("sh_unix_setcheckselinux"));
|
---|
3544 | }
|
---|
3545 | #endif
|
---|
3546 |
|
---|
3547 | #ifdef USE_ACL
|
---|
3548 | int sh_unix_setcheckacl (const char * c)
|
---|
3549 | {
|
---|
3550 | int i;
|
---|
3551 | SL_ENTER(_("sh_unix_setcheckacl"));
|
---|
3552 | i = sh_util_flagval(c, &(sh_unix_check_acl));
|
---|
3553 |
|
---|
3554 | SL_RETURN(i, _("sh_unix_setcheckacl"));
|
---|
3555 | }
|
---|
3556 | #endif
|
---|
3557 |
|
---|
3558 | #ifdef HAVE_LIBZ
|
---|
3559 | #include <zlib.h>
|
---|
3560 | #endif
|
---|
3561 |
|
---|
3562 | int sh_unix_getinfo (int level, char * filename, file_type * theFile,
|
---|
3563 | char * fileHash, int policy)
|
---|
3564 | {
|
---|
3565 | char timestr[81];
|
---|
3566 | long runtim;
|
---|
3567 | struct stat buf;
|
---|
3568 | struct stat lbuf;
|
---|
3569 | struct stat fbuf;
|
---|
3570 | int stat_return;
|
---|
3571 | int stat_errno = 0;
|
---|
3572 |
|
---|
3573 | ShFileType type;
|
---|
3574 | unsigned int mode;
|
---|
3575 | char * tmp;
|
---|
3576 | char * tmp2;
|
---|
3577 |
|
---|
3578 | char * linknamebuf;
|
---|
3579 | int linksize;
|
---|
3580 |
|
---|
3581 | extern int get_the_fd (SL_TICKET ticket);
|
---|
3582 |
|
---|
3583 | SL_TICKET rval_open;
|
---|
3584 | int err_open = 0;
|
---|
3585 |
|
---|
3586 | int fd;
|
---|
3587 | int fstat_return;
|
---|
3588 | int fstat_errno = 0;
|
---|
3589 | int try = 0;
|
---|
3590 |
|
---|
3591 | sh_string * content = NULL;
|
---|
3592 |
|
---|
3593 | time_t tend;
|
---|
3594 | time_t tstart;
|
---|
3595 |
|
---|
3596 |
|
---|
3597 | char * path = NULL;
|
---|
3598 |
|
---|
3599 | int alert_timeout = 120;
|
---|
3600 |
|
---|
3601 | path = theFile->fullpath;
|
---|
3602 |
|
---|
3603 | SL_ENTER(_("sh_unix_getinfo"));
|
---|
3604 |
|
---|
3605 | /* --- Stat the file, and get checksum. ---
|
---|
3606 | */
|
---|
3607 | tstart = time(NULL);
|
---|
3608 |
|
---|
3609 | stat_return = retry_lstat (FIL__, __LINE__,
|
---|
3610 | path /* theFile->fullpath */, &buf);
|
---|
3611 |
|
---|
3612 | if (stat_return)
|
---|
3613 | stat_errno = errno;
|
---|
3614 |
|
---|
3615 | theFile->link_path = NULL;
|
---|
3616 |
|
---|
3617 | try_again:
|
---|
3618 |
|
---|
3619 | fd = -1;
|
---|
3620 | fstat_return = -1;
|
---|
3621 | rval_open = -1;
|
---|
3622 |
|
---|
3623 | if (stat_return == 0 && S_ISREG(buf.st_mode))
|
---|
3624 | {
|
---|
3625 | rval_open = sl_open_fastread (FIL__, __LINE__,
|
---|
3626 | path /* theFile->fullpath */, SL_YESPRIV);
|
---|
3627 | if (SL_ISERROR(rval_open))
|
---|
3628 | {
|
---|
3629 | char * stale = sl_check_stale();
|
---|
3630 |
|
---|
3631 | if (stale)
|
---|
3632 | {
|
---|
3633 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
|
---|
3634 | stale, _("sh_unix_getinfo_open"));
|
---|
3635 | }
|
---|
3636 |
|
---|
3637 | if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
|
---|
3638 | {
|
---|
3639 | ++try;
|
---|
3640 | goto try_again;
|
---|
3641 | }
|
---|
3642 | err_open = errno;
|
---|
3643 | }
|
---|
3644 |
|
---|
3645 | alert_timeout = 120; /* this is per 8K block now ! */
|
---|
3646 |
|
---|
3647 | if (path[1] == 'p' && path[5] == '/' && path[2] == 'r' &&
|
---|
3648 | path[3] == 'o' && path[4] == 'c' && path[0] == '/')
|
---|
3649 | {
|
---|
3650 | /* seven is magic */
|
---|
3651 | alert_timeout = 7;
|
---|
3652 | }
|
---|
3653 |
|
---|
3654 | fd = get_the_fd(rval_open);
|
---|
3655 | }
|
---|
3656 |
|
---|
3657 | tend = time(NULL);
|
---|
3658 |
|
---|
3659 | /* An unprivileged user may slow lstat/open to a crawl
|
---|
3660 | * with clever path/symlink setup
|
---|
3661 | */
|
---|
3662 | if ((tend - tstart) > (time_t) /* 60 */ 6)
|
---|
3663 | {
|
---|
3664 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
3665 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_TOOLATE,
|
---|
3666 | (long)(tend - tstart), tmp2);
|
---|
3667 | SH_FREE(tmp2);
|
---|
3668 | }
|
---|
3669 |
|
---|
3670 | if (fd >= 0)
|
---|
3671 | {
|
---|
3672 | fstat_return = retry_fstat (FIL__, __LINE__, fd, &fbuf);
|
---|
3673 |
|
---|
3674 | if (fstat_return)
|
---|
3675 | {
|
---|
3676 | char * stale;
|
---|
3677 |
|
---|
3678 | fstat_errno = errno;
|
---|
3679 |
|
---|
3680 | stale = sl_check_stale();
|
---|
3681 |
|
---|
3682 | if (stale)
|
---|
3683 | {
|
---|
3684 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, fstat_errno,
|
---|
3685 | MSG_E_SUBGEN,
|
---|
3686 | stale, _("sh_unix_getinfo_fstat"));
|
---|
3687 | }
|
---|
3688 |
|
---|
3689 | if (try == 0) /* obsolete, but we keep this, just in case */
|
---|
3690 | {
|
---|
3691 | ++try;
|
---|
3692 | sl_close(rval_open);
|
---|
3693 | goto try_again;
|
---|
3694 | }
|
---|
3695 | }
|
---|
3696 | }
|
---|
3697 | else
|
---|
3698 | {
|
---|
3699 | fd = -1;
|
---|
3700 | }
|
---|
3701 |
|
---|
3702 |
|
---|
3703 | /* --- case 1: lstat failed ---
|
---|
3704 | */
|
---|
3705 | if (stat_return != 0)
|
---|
3706 | {
|
---|
3707 | stat_return = errno;
|
---|
3708 | if (!SL_ISERROR(rval_open))
|
---|
3709 | sl_close(rval_open);
|
---|
3710 | if (sh.flag.checkSum == SH_CHECK_INIT ||
|
---|
3711 | (sh_hash_have_it (theFile->fullpath) >= 0 &&
|
---|
3712 | (!SH_FFLAG_REPORTED_SET(theFile->file_reported))))
|
---|
3713 | {
|
---|
3714 | if (S_FALSE == sh_ignore_chk_del(theFile->fullpath)) {
|
---|
3715 | char errbuf[SH_ERRBUF_SIZE];
|
---|
3716 | uid_t euid;
|
---|
3717 | (void) sl_get_euid(&euid);
|
---|
3718 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
3719 | sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
|
---|
3720 | _("lstat"),
|
---|
3721 | sh_error_message (stat_errno, errbuf, sizeof(errbuf)),
|
---|
3722 | (long) euid,
|
---|
3723 | tmp2);
|
---|
3724 | SH_FREE(tmp2);
|
---|
3725 | }
|
---|
3726 | }
|
---|
3727 | SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
3728 | }
|
---|
3729 |
|
---|
3730 | /* --- case 2: not a regular file ---
|
---|
3731 | */
|
---|
3732 | else if (! S_ISREG(buf.st_mode))
|
---|
3733 | {
|
---|
3734 | if (fileHash != NULL)
|
---|
3735 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | /* --- case 3a: a regular file, fstat ok ---
|
---|
3739 | */
|
---|
3740 | else if (fstat_return == 0 &&
|
---|
3741 | buf.st_mode == fbuf.st_mode &&
|
---|
3742 | buf.st_ino == fbuf.st_ino &&
|
---|
3743 | buf.st_uid == fbuf.st_uid &&
|
---|
3744 | buf.st_gid == fbuf.st_gid &&
|
---|
3745 | buf.st_dev == fbuf.st_dev )
|
---|
3746 | {
|
---|
3747 | if (fileHash != NULL)
|
---|
3748 | {
|
---|
3749 | if ((theFile->check_mask & MODI_CHK) == 0)
|
---|
3750 | {
|
---|
3751 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3752 | }
|
---|
3753 | else if ((theFile->check_mask & MODI_PREL) != 0 &&
|
---|
3754 | S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
|
---|
3755 | alert_timeout, theFile->fullpath))
|
---|
3756 | {
|
---|
3757 | if (0 != sh_prelink_run (theFile->fullpath,
|
---|
3758 | fileHash, alert_timeout))
|
---|
3759 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3760 | }
|
---|
3761 | else
|
---|
3762 | {
|
---|
3763 | char hashbuf[KEYBUF_SIZE];
|
---|
3764 | UINT64 length_nolim = TIGER_NOLIM;
|
---|
3765 |
|
---|
3766 | if (MODI_TXT_ENABLED(theFile->check_mask) && fbuf.st_size < (10 * SH_TXT_MAX))
|
---|
3767 | {
|
---|
3768 | sl_init_content (rval_open, fbuf.st_size);
|
---|
3769 | }
|
---|
3770 |
|
---|
3771 | sl_strlcpy(fileHash,
|
---|
3772 | sh_tiger_generic_hash (theFile->fullpath,
|
---|
3773 | rval_open, &length_nolim,
|
---|
3774 | alert_timeout,
|
---|
3775 | hashbuf, sizeof(hashbuf)),
|
---|
3776 | KEY_LEN+1);
|
---|
3777 |
|
---|
3778 | content = sl_get_content(rval_open);
|
---|
3779 | content = sh_string_copy(content);
|
---|
3780 |
|
---|
3781 | if ((theFile->check_mask & MODI_SGROW) != 0)
|
---|
3782 | {
|
---|
3783 | fbuf.st_size = (off_t) length_nolim;
|
---|
3784 | buf.st_size = fbuf.st_size;
|
---|
3785 | sl_rewind(rval_open);
|
---|
3786 | sh_unix_checksum_size (theFile->fullpath, &fbuf,
|
---|
3787 | &fileHash[KEY_LEN + 1],
|
---|
3788 | alert_timeout, rval_open);
|
---|
3789 | }
|
---|
3790 | }
|
---|
3791 | }
|
---|
3792 | }
|
---|
3793 |
|
---|
3794 | /* --- case 3b: a regular file, fstat ok, but different ---
|
---|
3795 | */
|
---|
3796 | else if (fstat_return == 0 && S_ISREG(fbuf.st_mode))
|
---|
3797 | {
|
---|
3798 | memcpy (&buf, &fbuf, sizeof( struct stat ));
|
---|
3799 |
|
---|
3800 | if (fileHash != NULL)
|
---|
3801 | {
|
---|
3802 | if ((theFile->check_mask & MODI_CHK) == 0)
|
---|
3803 | {
|
---|
3804 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3805 | }
|
---|
3806 | else if (policy == SH_LEVEL_PRELINK &&
|
---|
3807 | S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
|
---|
3808 | alert_timeout, theFile->fullpath))
|
---|
3809 | {
|
---|
3810 | if (0 != sh_prelink_run (theFile->fullpath,
|
---|
3811 | fileHash, alert_timeout))
|
---|
3812 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3813 | }
|
---|
3814 | else
|
---|
3815 | {
|
---|
3816 | char hashbuf[KEYBUF_SIZE];
|
---|
3817 | UINT64 length_nolim = TIGER_NOLIM;
|
---|
3818 |
|
---|
3819 | if (MODI_TXT_ENABLED(theFile->check_mask) && fbuf.st_size < (10 * SH_TXT_MAX))
|
---|
3820 | {
|
---|
3821 | sl_init_content (rval_open, fbuf.st_size);
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | sl_strlcpy(fileHash,
|
---|
3825 | sh_tiger_generic_hash (theFile->fullpath, rval_open,
|
---|
3826 | &length_nolim,
|
---|
3827 | alert_timeout,
|
---|
3828 | hashbuf, sizeof(hashbuf)),
|
---|
3829 | KEY_LEN + 1);
|
---|
3830 |
|
---|
3831 | content = sl_get_content(rval_open);
|
---|
3832 | content = sh_string_copy(content);
|
---|
3833 |
|
---|
3834 | if ((theFile->check_mask & MODI_SGROW) != 0)
|
---|
3835 | {
|
---|
3836 | fbuf.st_size = (off_t) length_nolim;
|
---|
3837 | buf.st_size = fbuf.st_size;
|
---|
3838 | sl_rewind(rval_open);
|
---|
3839 | sh_unix_checksum_size (theFile->fullpath, &fbuf,
|
---|
3840 | &fileHash[KEY_LEN + 1],
|
---|
3841 | alert_timeout, rval_open);
|
---|
3842 | }
|
---|
3843 | }
|
---|
3844 | }
|
---|
3845 | }
|
---|
3846 |
|
---|
3847 | /* --- case 4: a regular file, fstat failed ---
|
---|
3848 | */
|
---|
3849 |
|
---|
3850 | else /* fstat_return != 0 or !S_ISREG(fbuf.st_mode) or open() failed */
|
---|
3851 | {
|
---|
3852 | uid_t euid;
|
---|
3853 |
|
---|
3854 | if (fileHash != NULL)
|
---|
3855 | sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
|
---|
3856 |
|
---|
3857 | if ((theFile->check_mask & MODI_CHK) != 0)
|
---|
3858 | {
|
---|
3859 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
3860 |
|
---|
3861 |
|
---|
3862 | if (fd >= 0 && fstat_return != 0)
|
---|
3863 | {
|
---|
3864 | char errbuf[SH_ERRBUF_SIZE];
|
---|
3865 | (void) sl_get_euid(&euid);
|
---|
3866 |
|
---|
3867 | sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
|
---|
3868 | _("fstat"),
|
---|
3869 | sh_error_message (fstat_errno, errbuf, sizeof(errbuf)),
|
---|
3870 | (long) euid,
|
---|
3871 | tmp2);
|
---|
3872 | }
|
---|
3873 | else if (fd >= 0 && !S_ISREG(fbuf.st_mode))
|
---|
3874 | {
|
---|
3875 | sh_error_handle (level, FIL__, __LINE__, fstat_errno,
|
---|
3876 | MSG_E_NOTREG, tmp2);
|
---|
3877 | }
|
---|
3878 | else
|
---|
3879 | {
|
---|
3880 | char errbuf[SH_ERRBUF_SIZE];
|
---|
3881 | char errbuf2[SH_ERRBUF_SIZE];
|
---|
3882 | sl_strlcpy(errbuf, sl_error_string(rval_open), sizeof(errbuf));
|
---|
3883 | sh_error_message(err_open, errbuf2, sizeof(errbuf2));
|
---|
3884 | sh_error_handle (level, FIL__, __LINE__, err_open,
|
---|
3885 | MSG_E_READ, errbuf, errbuf2, tmp2);
|
---|
3886 | }
|
---|
3887 | SH_FREE(tmp2);
|
---|
3888 | }
|
---|
3889 | }
|
---|
3890 |
|
---|
3891 |
|
---|
3892 | /* --- Determine file type. ---
|
---|
3893 | */
|
---|
3894 | memset (theFile->c_mode, '-', CMODE_SIZE-1);
|
---|
3895 | theFile->c_mode[CMODE_SIZE-1] = '\0';
|
---|
3896 |
|
---|
3897 | memset (theFile->link_c_mode, '-', CMODE_SIZE-1);
|
---|
3898 | theFile->link_c_mode[CMODE_SIZE-1] = '\0';
|
---|
3899 |
|
---|
3900 | sh_unix_getinfo_type (&buf, &type, theFile->c_mode);
|
---|
3901 | theFile->type = type;
|
---|
3902 |
|
---|
3903 | #if defined(__linux__) || defined(HAVE_STAT_FLAGS)
|
---|
3904 |
|
---|
3905 | /* --- Determine file attributes. ---
|
---|
3906 | */
|
---|
3907 | memset (theFile->c_attributes, '-', ATTRBUF_SIZE);
|
---|
3908 | theFile->c_attributes[ATTRBUF_USED] = '\0';
|
---|
3909 | theFile->attributes = 0;
|
---|
3910 |
|
---|
3911 | #if (defined(__linux__) && (defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H))) || defined(HAVE_STAT_FLAGS)
|
---|
3912 | if (theFile->c_mode[0] != 'c' && theFile->c_mode[0] != 'b' &&
|
---|
3913 | theFile->c_mode[0] != 'l' )
|
---|
3914 | sh_unix_getinfo_attr(theFile->fullpath,
|
---|
3915 | &theFile->attributes, theFile->c_attributes,
|
---|
3916 | fd, &buf);
|
---|
3917 | #endif
|
---|
3918 | #endif
|
---|
3919 |
|
---|
3920 | #if defined(USE_XATTR) && defined(USE_ACL)
|
---|
3921 | if (sh_unix_check_selinux == S_TRUE || sh_unix_check_acl == S_TRUE)
|
---|
3922 | theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
|
---|
3923 | #elif defined(USE_XATTR)
|
---|
3924 | if (sh_unix_check_selinux == S_TRUE)
|
---|
3925 | theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
|
---|
3926 | #elif defined(USE_ACL)
|
---|
3927 | if (sh_unix_check_acl == S_TRUE)
|
---|
3928 | theFile->attr_string = sh_unix_getinfo_acl (theFile->fullpath, fd, &buf);
|
---|
3929 | #else
|
---|
3930 | theFile->attr_string = NULL;
|
---|
3931 | #endif
|
---|
3932 |
|
---|
3933 | if (!SL_ISERROR(rval_open))
|
---|
3934 | sl_close(rval_open);
|
---|
3935 |
|
---|
3936 |
|
---|
3937 | /* --- I/O limit. ---
|
---|
3938 | */
|
---|
3939 | if (IO_Limit > 0)
|
---|
3940 | {
|
---|
3941 | runtim = (long) (time(NULL) - sh.statistics.time_start);
|
---|
3942 |
|
---|
3943 | if (runtim > 0 && (long)(sh.statistics.bytes_hashed/runtim) > IO_Limit)
|
---|
3944 | retry_msleep(1, 0);
|
---|
3945 | }
|
---|
3946 |
|
---|
3947 | /* --- Determine permissions. ---
|
---|
3948 | */
|
---|
3949 | sh_unix_getinfo_mode (&buf, &mode, theFile->c_mode);
|
---|
3950 |
|
---|
3951 | /* --- Trivia. ---
|
---|
3952 | */
|
---|
3953 | theFile->dev = buf.st_dev;
|
---|
3954 | theFile->ino = buf.st_ino;
|
---|
3955 | theFile->mode = buf.st_mode;
|
---|
3956 | theFile->hardlinks = buf.st_nlink;
|
---|
3957 | theFile->owner = buf.st_uid;
|
---|
3958 | theFile->group = buf.st_gid;
|
---|
3959 | theFile->rdev = buf.st_rdev;
|
---|
3960 | theFile->size = buf.st_size;
|
---|
3961 | theFile->blksize = (unsigned long) buf.st_blksize;
|
---|
3962 | theFile->blocks = (unsigned long) buf.st_blocks;
|
---|
3963 | theFile->atime = buf.st_atime;
|
---|
3964 | theFile->mtime = buf.st_mtime;
|
---|
3965 | theFile->ctime = buf.st_ctime;
|
---|
3966 |
|
---|
3967 |
|
---|
3968 | /* --- Owner and group. ---
|
---|
3969 | */
|
---|
3970 |
|
---|
3971 | if (NULL == sh_unix_getGIDname(SH_ERR_ALL, buf.st_gid, theFile->c_group, GROUP_MAX+1)) {
|
---|
3972 |
|
---|
3973 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
3974 |
|
---|
3975 | if (policy == SH_LEVEL_ALLIGNORE)
|
---|
3976 | {
|
---|
3977 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
|
---|
3978 | MSG_FI_NOGRP,
|
---|
3979 | (long) buf.st_gid, tmp2);
|
---|
3980 | }
|
---|
3981 | else
|
---|
3982 | {
|
---|
3983 | sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
|
---|
3984 | MSG_FI_NOGRP,
|
---|
3985 | (long) buf.st_gid, tmp2);
|
---|
3986 | }
|
---|
3987 | SH_FREE(tmp2);
|
---|
3988 | sl_snprintf(theFile->c_group, GROUP_MAX+1, "%d", (long) buf.st_gid);
|
---|
3989 | }
|
---|
3990 |
|
---|
3991 |
|
---|
3992 | if (NULL == sh_unix_getUIDname(SH_ERR_ALL, buf.st_uid, theFile->c_owner, USER_MAX+1)) {
|
---|
3993 |
|
---|
3994 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
3995 |
|
---|
3996 | if (policy == SH_LEVEL_ALLIGNORE)
|
---|
3997 | {
|
---|
3998 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
|
---|
3999 | MSG_FI_NOUSR,
|
---|
4000 | (long) buf.st_uid, tmp2);
|
---|
4001 | }
|
---|
4002 | else
|
---|
4003 | {
|
---|
4004 | sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
|
---|
4005 | MSG_FI_NOUSR,
|
---|
4006 | (long) buf.st_uid, tmp2);
|
---|
4007 | }
|
---|
4008 | SH_FREE(tmp2);
|
---|
4009 | sl_snprintf(theFile->c_owner, USER_MAX+1, "%d", (long) buf.st_uid);
|
---|
4010 | }
|
---|
4011 |
|
---|
4012 | /* --- Output the file. ---
|
---|
4013 | */
|
---|
4014 | if (flag_err_debug == SL_TRUE)
|
---|
4015 | {
|
---|
4016 | tmp2 = sh_util_safe_name ((filename == NULL) ?
|
---|
4017 | theFile->fullpath : filename);
|
---|
4018 | (void) sh_unix_time(theFile->mtime, timestr, sizeof(timestr));
|
---|
4019 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LIST,
|
---|
4020 | theFile->c_mode,
|
---|
4021 | theFile->hardlinks,
|
---|
4022 | theFile->c_owner,
|
---|
4023 | theFile->c_group,
|
---|
4024 | (unsigned long) theFile->size,
|
---|
4025 | timestr,
|
---|
4026 | tmp2);
|
---|
4027 | SH_FREE(tmp2);
|
---|
4028 | }
|
---|
4029 |
|
---|
4030 | /* --- Check for links. ---
|
---|
4031 | */
|
---|
4032 | if (theFile->c_mode[0] == 'l')
|
---|
4033 | {
|
---|
4034 | linknamebuf = SH_ALLOC(PATH_MAX);
|
---|
4035 |
|
---|
4036 | /* flawfinder: ignore */
|
---|
4037 | linksize = readlink (theFile->fullpath, linknamebuf, PATH_MAX-1);
|
---|
4038 |
|
---|
4039 | if (linksize < (PATH_MAX-1) && linksize >= 0)
|
---|
4040 | linknamebuf[linksize] = '\0';
|
---|
4041 | else
|
---|
4042 | linknamebuf[PATH_MAX-1] = '\0';
|
---|
4043 |
|
---|
4044 | if (linksize < 0)
|
---|
4045 | {
|
---|
4046 | char errbuf[SH_ERRBUF_SIZE];
|
---|
4047 | linksize = errno;
|
---|
4048 | tmp2 = sh_util_safe_name (theFile->fullpath);
|
---|
4049 | sh_error_handle (level, FIL__, __LINE__, linksize, MSG_FI_RDLNK,
|
---|
4050 | sh_error_message (linksize, errbuf, sizeof(errbuf)), tmp2);
|
---|
4051 | SH_FREE(tmp2);
|
---|
4052 | SH_FREE(linknamebuf);
|
---|
4053 | theFile->link_path = sh_util_strdup("-");
|
---|
4054 | SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
4055 | }
|
---|
4056 |
|
---|
4057 | if (linknamebuf[0] == '/')
|
---|
4058 | {
|
---|
4059 | theFile->link_path = sh_util_strdup (linknamebuf);
|
---|
4060 | }
|
---|
4061 | else
|
---|
4062 | {
|
---|
4063 | tmp = sh_util_dirname(theFile->fullpath);
|
---|
4064 | if (tmp) {
|
---|
4065 | theFile->link_path = SH_ALLOC(PATH_MAX);
|
---|
4066 | sl_strlcpy (theFile->link_path, tmp, PATH_MAX);
|
---|
4067 | SH_FREE(tmp);
|
---|
4068 | } else {
|
---|
4069 | theFile->link_path = SH_ALLOC(PATH_MAX);
|
---|
4070 | theFile->link_path[0] = '\0';
|
---|
4071 | }
|
---|
4072 | /*
|
---|
4073 | * Only attach '/' if not root directory. Handle "//", which
|
---|
4074 | * according to POSIX is implementation-defined, and may be
|
---|
4075 | * different from "/" (however, three or more '/' will collapse
|
---|
4076 | * to one).
|
---|
4077 | */
|
---|
4078 | tmp = theFile->link_path; while (*tmp == '/') ++tmp;
|
---|
4079 | if (*tmp != '\0')
|
---|
4080 | {
|
---|
4081 | sl_strlcat (theFile->link_path, "/", PATH_MAX);
|
---|
4082 | }
|
---|
4083 | sl_strlcat (theFile->link_path, linknamebuf, PATH_MAX);
|
---|
4084 | }
|
---|
4085 |
|
---|
4086 | /* stat the link
|
---|
4087 | */
|
---|
4088 | stat_return = retry_lstat (FIL__, __LINE__, theFile->link_path, &lbuf);
|
---|
4089 |
|
---|
4090 | /* check for error
|
---|
4091 | */
|
---|
4092 | if (stat_return != 0)
|
---|
4093 | {
|
---|
4094 | stat_return = errno;
|
---|
4095 | tmp = sh_util_safe_name (theFile->fullpath);
|
---|
4096 | tmp2 = sh_util_safe_name (theFile->link_path);
|
---|
4097 | if (stat_return != ENOENT)
|
---|
4098 | {
|
---|
4099 | uid_t euid;
|
---|
4100 | char errbuf[SH_ERRBUF_SIZE];
|
---|
4101 |
|
---|
4102 | (void) sl_get_euid(&euid);
|
---|
4103 | sh_error_handle (level, FIL__, __LINE__, stat_return,
|
---|
4104 | MSG_FI_STAT,
|
---|
4105 | _("lstat"),
|
---|
4106 | sh_error_message (stat_return,errbuf, sizeof(errbuf)),
|
---|
4107 | (long) euid,
|
---|
4108 | tmp2);
|
---|
4109 | }
|
---|
4110 | else
|
---|
4111 | {
|
---|
4112 | /* a dangling link -- everybody seems to have plenty of them
|
---|
4113 | */
|
---|
4114 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DLNK,
|
---|
4115 | tmp, tmp2);
|
---|
4116 | }
|
---|
4117 | theFile->linkisok = BAD;
|
---|
4118 | SH_FREE(tmp);
|
---|
4119 | SH_FREE(tmp2);
|
---|
4120 | SH_FREE(linknamebuf);
|
---|
4121 | /*
|
---|
4122 | * changed Tue Feb 10 16:16:13 CET 2004:
|
---|
4123 | * add dangling symlinks into database
|
---|
4124 | * SL_RETURN((-1),_("sh_unix_getinfo"));
|
---|
4125 | */
|
---|
4126 | theFile->linkmode = 0;
|
---|
4127 | SL_RETURN((0),_("sh_unix_getinfo"));
|
---|
4128 | }
|
---|
4129 |
|
---|
4130 | theFile->linkisok = GOOD;
|
---|
4131 |
|
---|
4132 |
|
---|
4133 | /* --- Determine file type. ---
|
---|
4134 | */
|
---|
4135 | sh_unix_getinfo_type (&lbuf, &type, theFile->link_c_mode);
|
---|
4136 | theFile->type = type;
|
---|
4137 |
|
---|
4138 | /* --- Determine permissions. ---
|
---|
4139 | */
|
---|
4140 | sh_unix_getinfo_mode (&lbuf, &mode, theFile->link_c_mode);
|
---|
4141 | theFile->linkmode = lbuf.st_mode;
|
---|
4142 |
|
---|
4143 | /* --- Output the link. ---
|
---|
4144 | */
|
---|
4145 | if (theFile->linkisok == GOOD)
|
---|
4146 | {
|
---|
4147 | tmp2 = sh_util_safe_name (linknamebuf);
|
---|
4148 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LLNK,
|
---|
4149 | theFile->link_c_mode, tmp2);
|
---|
4150 | SH_FREE(tmp2);
|
---|
4151 | }
|
---|
4152 | SH_FREE(linknamebuf);
|
---|
4153 | }
|
---|
4154 | else /* not a link */
|
---|
4155 | {
|
---|
4156 | if (content)
|
---|
4157 | {
|
---|
4158 | #ifdef HAVE_LIBZ
|
---|
4159 | unsigned long clen;
|
---|
4160 | unsigned char * compressed;
|
---|
4161 | #ifdef HAVE_COMPRESSBOUND
|
---|
4162 | clen = compressBound(sh_string_len(content));
|
---|
4163 | #else
|
---|
4164 | if (sh_string_len(content) > 10*SH_TXT_MAX)
|
---|
4165 | clen = SH_TXT_MAX;
|
---|
4166 | else
|
---|
4167 | clen = 13 + (int)(1.0001*sh_string_len(content));
|
---|
4168 | #endif
|
---|
4169 | compressed = SH_ALLOC(clen);
|
---|
4170 | if (Z_OK == compress(compressed, &clen,
|
---|
4171 | (unsigned char *) sh_string_str(content),
|
---|
4172 | sh_string_len(content)))
|
---|
4173 | {
|
---|
4174 | if (clen < SH_TXT_MAX)
|
---|
4175 | {
|
---|
4176 | sh_util_base64_enc_alloc (&(theFile->link_path),
|
---|
4177 | (char *) compressed, clen);
|
---|
4178 | }
|
---|
4179 | else
|
---|
4180 | {
|
---|
4181 | char tmsg[128];
|
---|
4182 | char * tpath = sh_util_safe_name (theFile->fullpath);
|
---|
4183 | sl_snprintf(tmsg, sizeof(tmsg),
|
---|
4184 | _("compressed file too large (%lu bytes)"),
|
---|
4185 | clen);
|
---|
4186 | sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, -1,
|
---|
4187 | MSG_E_SUBGPATH, tmsg,
|
---|
4188 | _("sh_unix_getinfo"), tpath);
|
---|
4189 | SH_FREE(tpath);
|
---|
4190 | }
|
---|
4191 | }
|
---|
4192 | SH_FREE(compressed);
|
---|
4193 | #endif
|
---|
4194 | sh_string_destroy(&content);
|
---|
4195 | }
|
---|
4196 | }
|
---|
4197 | SL_RETURN((0),_("sh_unix_getinfo"));
|
---|
4198 | }
|
---|
4199 |
|
---|
4200 | /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
|
---|
4201 | #endif
|
---|
4202 |
|
---|
4203 | int sh_unix_unlock(char * lockfile, char * flag)
|
---|
4204 | {
|
---|
4205 | int error = 0;
|
---|
4206 |
|
---|
4207 | SL_ENTER(_("sh_unix_unlock"));
|
---|
4208 |
|
---|
4209 | if (sh.flag.isdaemon == S_FALSE && flag == NULL)
|
---|
4210 | SL_RETURN((0),_("sh_unix_unlock"));
|
---|
4211 |
|
---|
4212 | /* --- Logfile is not locked to us. ---
|
---|
4213 | */
|
---|
4214 | if (sh.flag.islocked == BAD && flag != NULL)
|
---|
4215 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
4216 |
|
---|
4217 | /* --- Check whether the directory is secure. ---
|
---|
4218 | */
|
---|
4219 | if (0 != tf_trust_check (lockfile, SL_YESPRIV))
|
---|
4220 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
4221 |
|
---|
4222 | /* --- Delete the lock file. ---
|
---|
4223 | */
|
---|
4224 | error = retry_aud_unlink (FIL__, __LINE__, lockfile);
|
---|
4225 |
|
---|
4226 | if (error == 0)
|
---|
4227 | {
|
---|
4228 | if (flag != NULL)
|
---|
4229 | sh.flag.islocked = BAD; /* not locked anymore */
|
---|
4230 | }
|
---|
4231 | else if (flag != NULL)
|
---|
4232 | {
|
---|
4233 | char errbuf[SH_ERRBUF_SIZE];
|
---|
4234 | error = errno;
|
---|
4235 | sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_UNLNK,
|
---|
4236 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
4237 | lockfile);
|
---|
4238 | SL_RETURN((-1),_("sh_unix_unlock"));
|
---|
4239 | }
|
---|
4240 | SL_RETURN((0),_("sh_unix_unlock"));
|
---|
4241 | }
|
---|
4242 |
|
---|
4243 | int sh_unix_check_piddir (char * pidpath)
|
---|
4244 | {
|
---|
4245 | static struct stat buf;
|
---|
4246 | int status = 0;
|
---|
4247 | char * pid_dir;
|
---|
4248 |
|
---|
4249 | SL_ENTER(_("sh_unix_check_piddir"));
|
---|
4250 |
|
---|
4251 | pid_dir = sh_util_dirname (pidpath);
|
---|
4252 |
|
---|
4253 | status = retry_lstat (FIL__, __LINE__, pid_dir, &buf);
|
---|
4254 |
|
---|
4255 | if (status < 0 && errno == ENOENT)
|
---|
4256 | {
|
---|
4257 | status = mkdir (pid_dir, 0777);
|
---|
4258 | if (status < 0)
|
---|
4259 | {
|
---|
4260 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4261 | MSG_E_SUBGEN,
|
---|
4262 | _("Cannot create PID directory"),
|
---|
4263 | _("sh_unix_check_piddir"));
|
---|
4264 | SH_FREE(pid_dir);
|
---|
4265 | SL_RETURN((-1),_("sh_unix_check_piddir"));
|
---|
4266 | }
|
---|
4267 | }
|
---|
4268 | else if (!S_ISDIR(buf.st_mode))
|
---|
4269 | {
|
---|
4270 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4271 | MSG_E_SUBGEN,
|
---|
4272 | _("Path of PID directory refers to a non-directory object"),
|
---|
4273 | _("sh_unix_check_piddir"));
|
---|
4274 | SH_FREE(pid_dir);
|
---|
4275 | SL_RETURN((-1),_("sh_unix_check_piddir"));
|
---|
4276 | }
|
---|
4277 | SH_FREE(pid_dir);
|
---|
4278 | SL_RETURN((0),_("sh_unix_check_piddir"));
|
---|
4279 | }
|
---|
4280 |
|
---|
4281 | int sh_unix_lock (char * lockfile, char * flag)
|
---|
4282 | {
|
---|
4283 | int filed;
|
---|
4284 | int errnum;
|
---|
4285 | char myPid[64];
|
---|
4286 | SL_TICKET fd;
|
---|
4287 | extern int get_the_fd (SL_TICKET ticket);
|
---|
4288 |
|
---|
4289 | SL_ENTER(_("sh_unix_lock"));
|
---|
4290 |
|
---|
4291 | sprintf (myPid, "%ld\n", (long) sh.pid); /* known to fit */
|
---|
4292 |
|
---|
4293 | if (flag == NULL) /* PID file, check for directory */
|
---|
4294 | {
|
---|
4295 | if (0 != sh_unix_check_piddir (lockfile))
|
---|
4296 | {
|
---|
4297 | SL_RETURN((-1),_("sh_unix_lock"));
|
---|
4298 | }
|
---|
4299 | }
|
---|
4300 |
|
---|
4301 | fd = sl_open_safe_rdwr (FIL__, __LINE__,
|
---|
4302 | lockfile, SL_YESPRIV); /* fails if file exists */
|
---|
4303 |
|
---|
4304 | if (!SL_ISERROR(fd))
|
---|
4305 | {
|
---|
4306 | errnum = sl_write (fd, myPid, sl_strlen(myPid));
|
---|
4307 | filed = get_the_fd(fd);
|
---|
4308 | fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
---|
4309 | sl_close (fd);
|
---|
4310 |
|
---|
4311 | if (!SL_ISERROR(errnum))
|
---|
4312 | {
|
---|
4313 | if (flag != NULL)
|
---|
4314 | sh.flag.islocked = GOOD;
|
---|
4315 | SL_RETURN((0),_("sh_unix_lock"));
|
---|
4316 | }
|
---|
4317 | }
|
---|
4318 |
|
---|
4319 | TPT((0, FIL__, __LINE__, _("msg=<open pid file failed>\n")));
|
---|
4320 | if (flag != NULL)
|
---|
4321 | sh.flag.islocked = BAD;
|
---|
4322 | SL_RETURN((-1),_("sh_unix_lock"));
|
---|
4323 |
|
---|
4324 | /* notreached */
|
---|
4325 | }
|
---|
4326 |
|
---|
4327 |
|
---|
4328 | /* check whether file is locked
|
---|
4329 | */
|
---|
4330 | int sh_unix_test_and_lock (char * filename, char * lockfile)
|
---|
4331 | {
|
---|
4332 | static struct stat buf;
|
---|
4333 | int status = 0;
|
---|
4334 |
|
---|
4335 |
|
---|
4336 | SL_TICKET fd;
|
---|
4337 | char line_in[128];
|
---|
4338 |
|
---|
4339 | SL_ENTER(_("sh_unix_test_and_lock"));
|
---|
4340 |
|
---|
4341 | status = retry_lstat (FIL__, __LINE__, lockfile, &buf);
|
---|
4342 |
|
---|
4343 | /* --- No lock file found, try to lock. ---
|
---|
4344 | */
|
---|
4345 |
|
---|
4346 | if (status < 0 && errno == ENOENT)
|
---|
4347 | {
|
---|
4348 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
4349 | {
|
---|
4350 | if (filename != NULL)
|
---|
4351 | sh.flag.islocked = GOOD;
|
---|
4352 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
4353 | }
|
---|
4354 | else
|
---|
4355 | {
|
---|
4356 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4357 | MSG_E_SUBGEN,
|
---|
4358 | (filename == NULL) ? _("Cannot create PID file (1)") : _("Cannot create lock file (1)"),
|
---|
4359 | _("sh_unix_test_and_lock"));
|
---|
4360 | SL_RETURN((-1),_("sh_unix_test_and_lock"));
|
---|
4361 | }
|
---|
4362 | }
|
---|
4363 | else if (status == 0 && buf.st_size == 0)
|
---|
4364 | {
|
---|
4365 | if (filename != NULL)
|
---|
4366 | sh.flag.islocked = GOOD;
|
---|
4367 | sh_unix_unlock (lockfile, filename);
|
---|
4368 | if (filename != NULL)
|
---|
4369 | sh.flag.islocked = BAD;
|
---|
4370 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
4371 | {
|
---|
4372 | if (filename != NULL)
|
---|
4373 | sh.flag.islocked = GOOD;
|
---|
4374 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
4375 | }
|
---|
4376 | else
|
---|
4377 | {
|
---|
4378 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4379 | MSG_E_SUBGEN,
|
---|
4380 | (filename == NULL) ? _("Cannot create PID file (2)") : _("Cannot create lock file (2)"),
|
---|
4381 | _("sh_unix_test_and_lock"));
|
---|
4382 | SL_RETURN((-1),_("sh_unix_test_and_lock"));
|
---|
4383 | }
|
---|
4384 | }
|
---|
4385 |
|
---|
4386 | /* --- Check on lock. ---
|
---|
4387 | */
|
---|
4388 |
|
---|
4389 | if (status >= 0)
|
---|
4390 | {
|
---|
4391 | fd = sl_open_read (FIL__, __LINE__, lockfile, SL_YESPRIV);
|
---|
4392 | if (SL_ISERROR(fd))
|
---|
4393 | sh_error_handle ((-1), FIL__, __LINE__, fd,
|
---|
4394 | MSG_E_SUBGEN,
|
---|
4395 | (filename == NULL) ? _("Cannot open PID file for read") : _("Cannot open lock file for read"),
|
---|
4396 | _("sh_unix_test_and_lock"));
|
---|
4397 | }
|
---|
4398 | else
|
---|
4399 | fd = -1;
|
---|
4400 |
|
---|
4401 | if (!SL_ISERROR(fd))
|
---|
4402 | {
|
---|
4403 | /* read the PID in the lock file
|
---|
4404 | */
|
---|
4405 | status = sl_read (fd, line_in, sizeof(line_in));
|
---|
4406 | line_in[sizeof(line_in)-1] = '\0';
|
---|
4407 |
|
---|
4408 | /* convert to numeric
|
---|
4409 | */
|
---|
4410 | if (status > 0)
|
---|
4411 | {
|
---|
4412 | errno = 0;
|
---|
4413 | status = strtol(line_in, (char **)NULL, 10);
|
---|
4414 | if (errno == ERANGE || status <= 0)
|
---|
4415 | {
|
---|
4416 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4417 | MSG_E_SUBGEN,
|
---|
4418 | (filename == NULL) ? _("Bad PID in PID file") : _("Bad PID in lock file"),
|
---|
4419 | _("sh_unix_test_and_lock"));
|
---|
4420 |
|
---|
4421 | status = -1;
|
---|
4422 | }
|
---|
4423 | }
|
---|
4424 | else
|
---|
4425 | {
|
---|
4426 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4427 | MSG_E_SUBGEN,
|
---|
4428 | (filename == NULL) ? _("Cannot read PID file") : _("Cannot read lock file"),
|
---|
4429 | _("sh_unix_test_and_lock"));
|
---|
4430 | }
|
---|
4431 | sl_close(fd);
|
---|
4432 |
|
---|
4433 | if (status > 0 && (unsigned int) status == sh.pid)
|
---|
4434 | {
|
---|
4435 | if (filename != NULL)
|
---|
4436 | sh.flag.islocked = GOOD;
|
---|
4437 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
4438 | }
|
---|
4439 |
|
---|
4440 |
|
---|
4441 | /* --- Check whether the process exists. ---
|
---|
4442 | */
|
---|
4443 | if (status > 0)
|
---|
4444 | {
|
---|
4445 | errno = 0;
|
---|
4446 | status = aud_kill (FIL__, __LINE__, status, 0);
|
---|
4447 |
|
---|
4448 | /* Does not exist, so remove the stale lock
|
---|
4449 | * and create a new one.
|
---|
4450 | */
|
---|
4451 | if (status < 0 && errno == ESRCH)
|
---|
4452 | {
|
---|
4453 | if (filename != NULL)
|
---|
4454 | sh.flag.islocked = GOOD;
|
---|
4455 | if (0 != sh_unix_unlock(lockfile, filename) && (filename !=NULL))
|
---|
4456 | sh.flag.islocked = BAD;
|
---|
4457 | else
|
---|
4458 | {
|
---|
4459 | if (0 == sh_unix_lock (lockfile, filename))
|
---|
4460 | {
|
---|
4461 | if (filename != NULL)
|
---|
4462 | sh.flag.islocked = GOOD;
|
---|
4463 | SL_RETURN((0),_("sh_unix_test_and_lock"));
|
---|
4464 | }
|
---|
4465 | else
|
---|
4466 | {
|
---|
4467 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4468 | MSG_E_SUBGEN,
|
---|
4469 | (filename == NULL) ? _("Cannot create PID file (3)") : _("Cannot create lock file (3)"),
|
---|
4470 | _("sh_unix_test_and_lock"));
|
---|
4471 | }
|
---|
4472 | if (filename != NULL)
|
---|
4473 | sh.flag.islocked = BAD;
|
---|
4474 | }
|
---|
4475 | }
|
---|
4476 | else
|
---|
4477 | {
|
---|
4478 | sh_error_handle ((-1), FIL__, __LINE__, status,
|
---|
4479 | MSG_E_SUBGEN,
|
---|
4480 | (filename == NULL) ? _("Cannot remove stale PID file, PID may be a running process") : _("Cannot remove stale lock file, PID may be a running process"),
|
---|
4481 | _("sh_unix_test_and_lock"));
|
---|
4482 | if (filename != NULL)
|
---|
4483 | sh.flag.islocked = BAD;
|
---|
4484 | }
|
---|
4485 | }
|
---|
4486 | }
|
---|
4487 | SL_RETURN((-1),_("sh_unix_testlock"));
|
---|
4488 | }
|
---|
4489 |
|
---|
4490 | /* write the PID file
|
---|
4491 | */
|
---|
4492 | int sh_unix_write_pid_file()
|
---|
4493 | {
|
---|
4494 | return sh_unix_test_and_lock(NULL, sh.srvlog.alt);
|
---|
4495 | }
|
---|
4496 |
|
---|
4497 | /* write lock for filename
|
---|
4498 | */
|
---|
4499 | int sh_unix_write_lock_file(char * filename)
|
---|
4500 | {
|
---|
4501 | size_t len;
|
---|
4502 | int res;
|
---|
4503 | char * lockfile;
|
---|
4504 |
|
---|
4505 | if (filename == NULL)
|
---|
4506 | return (-1);
|
---|
4507 |
|
---|
4508 | len = sl_strlen(filename);
|
---|
4509 | if (sl_ok_adds(len, 6))
|
---|
4510 | len += 6;
|
---|
4511 | lockfile = SH_ALLOC(len);
|
---|
4512 | sl_strlcpy(lockfile, filename, len);
|
---|
4513 | sl_strlcat(lockfile, _(".lock"), len);
|
---|
4514 | res = sh_unix_test_and_lock(filename, lockfile);
|
---|
4515 | SH_FREE(lockfile);
|
---|
4516 | return res;
|
---|
4517 | }
|
---|
4518 |
|
---|
4519 | /* rm lock for filename
|
---|
4520 | */
|
---|
4521 | int sh_unix_rm_lock_file(char * filename)
|
---|
4522 | {
|
---|
4523 | size_t len;
|
---|
4524 | int res;
|
---|
4525 | char * lockfile;
|
---|
4526 |
|
---|
4527 | if (filename == NULL)
|
---|
4528 | return (-1);
|
---|
4529 |
|
---|
4530 | len = sl_strlen(filename);
|
---|
4531 | if (sl_ok_adds(len, 6))
|
---|
4532 | len += 6;
|
---|
4533 | lockfile = SH_ALLOC(len);
|
---|
4534 | sl_strlcpy(lockfile, filename, len);
|
---|
4535 | sl_strlcat(lockfile, _(".lock"), len);
|
---|
4536 |
|
---|
4537 | res = sh_unix_unlock(lockfile, filename);
|
---|
4538 | SH_FREE(lockfile);
|
---|
4539 | return res;
|
---|
4540 | }
|
---|
4541 |
|
---|
4542 | /* rm lock for filename
|
---|
4543 | */
|
---|
4544 | int sh_unix_rm_pid_file()
|
---|
4545 | {
|
---|
4546 | return sh_unix_unlock(sh.srvlog.alt, NULL);
|
---|
4547 | }
|
---|
4548 |
|
---|
4549 | /* Test whether file exists
|
---|
4550 | */
|
---|
4551 | int sh_unix_file_exists(char * path)
|
---|
4552 | {
|
---|
4553 | struct stat buf;
|
---|
4554 |
|
---|
4555 | SL_ENTER(_("sh_unix_file_exists"));
|
---|
4556 |
|
---|
4557 | if (0 == retry_lstat(FIL__, __LINE__, path, &buf))
|
---|
4558 | SL_RETURN( S_TRUE, _("sh_unix_file_exists"));
|
---|
4559 | else
|
---|
4560 | SL_RETURN( S_FALSE, _("sh_unix_file_exists"));
|
---|
4561 | }
|
---|
4562 |
|
---|
4563 |
|
---|
4564 | /* Test whether file exists, is a character device, and allows read
|
---|
4565 | * access.
|
---|
4566 | */
|
---|
4567 | int sh_unix_device_readable(int fd)
|
---|
4568 | {
|
---|
4569 | struct stat buf;
|
---|
4570 |
|
---|
4571 | SL_ENTER(_("sh_unix_device_readable"));
|
---|
4572 |
|
---|
4573 | if (retry_fstat(FIL__, __LINE__, fd, &buf) == -1)
|
---|
4574 | SL_RETURN( (-1), _("sh_unix_device_readable"));
|
---|
4575 | else if ( S_ISCHR(buf.st_mode) && 0 != (S_IROTH & buf.st_mode) )
|
---|
4576 | SL_RETURN( (0), _("sh_unix_device_readable"));
|
---|
4577 | else
|
---|
4578 | SL_RETURN( (-1), _("sh_unix_device_readable"));
|
---|
4579 | }
|
---|
4580 |
|
---|
4581 | static char preq[16];
|
---|
4582 |
|
---|
4583 | /* return true if database is remote
|
---|
4584 | */
|
---|
4585 | int file_is_remote ()
|
---|
4586 | {
|
---|
4587 | static int init = 0;
|
---|
4588 | struct stat buf;
|
---|
4589 |
|
---|
4590 | SL_ENTER(_("file_is_remote"));
|
---|
4591 |
|
---|
4592 | if (init == 0)
|
---|
4593 | {
|
---|
4594 | sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
|
---|
4595 | ++init;
|
---|
4596 | }
|
---|
4597 | if (0 == sl_strncmp (sh.data.path, preq, 15))
|
---|
4598 | {
|
---|
4599 | if (sh.data.path[15] != '\0') /* should be start of path */
|
---|
4600 | {
|
---|
4601 | if (0 == stat(&(sh.data.path[15]), &buf))
|
---|
4602 | {
|
---|
4603 | SL_RETURN( S_FALSE, _("file_is_remote"));
|
---|
4604 | }
|
---|
4605 | }
|
---|
4606 | SL_RETURN( S_TRUE, _("file_is_remote"));
|
---|
4607 | }
|
---|
4608 | SL_RETURN( S_FALSE, _("file_is_remote"));
|
---|
4609 | }
|
---|
4610 |
|
---|
4611 | /* Return the path to the configuration/database file.
|
---|
4612 | */
|
---|
4613 | char * file_path(char what, char flag)
|
---|
4614 | {
|
---|
4615 | static int init = 0;
|
---|
4616 |
|
---|
4617 | SL_ENTER(_("file_path"));
|
---|
4618 |
|
---|
4619 | if (init == 0)
|
---|
4620 | {
|
---|
4621 | sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
|
---|
4622 | ++init;
|
---|
4623 | }
|
---|
4624 |
|
---|
4625 | switch (what)
|
---|
4626 | {
|
---|
4627 |
|
---|
4628 | case 'C':
|
---|
4629 | if (0 == sl_strncmp (sh.conf.path, preq, 15))
|
---|
4630 | {
|
---|
4631 | #if defined(SH_WITH_SERVER)
|
---|
4632 | if (sh.flag.isserver == S_TRUE && sl_strlen(sh.conf.path) == 15)
|
---|
4633 | SL_RETURN( NULL, _("file_path"));
|
---|
4634 | if (sh.flag.isserver == S_TRUE)
|
---|
4635 | SL_RETURN( &(sh.conf.path[15]), _("file_path"));
|
---|
4636 | #endif
|
---|
4637 | if (flag == 'R')
|
---|
4638 | SL_RETURN( preq, _("file_path"));
|
---|
4639 | if (flag == 'I')
|
---|
4640 | {
|
---|
4641 | if (sl_strlen(sh.conf.path) == 15)
|
---|
4642 | SL_RETURN( NULL, _("file_path"));
|
---|
4643 | else
|
---|
4644 | SL_RETURN( &(sh.conf.path[15]), _("file_path"));
|
---|
4645 | }
|
---|
4646 | SL_RETURN ( preq, _("file_path"));
|
---|
4647 | }
|
---|
4648 | else
|
---|
4649 | SL_RETURN( sh.conf.path, _("file_path"));
|
---|
4650 | /* break; *//* unreachable */
|
---|
4651 |
|
---|
4652 | case 'D':
|
---|
4653 | if (0 == sl_strncmp (sh.data.path, preq, 15))
|
---|
4654 | {
|
---|
4655 | if (flag == 'R')
|
---|
4656 | SL_RETURN( preq, _("file_path"));
|
---|
4657 | if (flag == 'W' && sl_strlen(sh.data.path) == 15)
|
---|
4658 | SL_RETURN (NULL, _("file_path"));
|
---|
4659 | if (flag == 'W')
|
---|
4660 | SL_RETURN( &(sh.data.path[15]), _("file_path"));
|
---|
4661 | }
|
---|
4662 | else
|
---|
4663 | SL_RETURN( sh.data.path, _("file_path"));
|
---|
4664 | break;
|
---|
4665 |
|
---|
4666 | default:
|
---|
4667 | SL_RETURN( NULL, _("file_path"));
|
---|
4668 | }
|
---|
4669 |
|
---|
4670 | return NULL; /* notreached */
|
---|
4671 | }
|
---|
4672 | /************************************************/
|
---|
4673 | /**** Mlock Utilities ****/
|
---|
4674 | /************************************************/
|
---|
4675 |
|
---|
4676 | #include <limits.h>
|
---|
4677 |
|
---|
4678 | int sh_unix_pagesize()
|
---|
4679 | {
|
---|
4680 | int pagesize = 4096;
|
---|
4681 | #if defined(_SC_PAGESIZE)
|
---|
4682 | pagesize = sysconf(_SC_PAGESIZE);
|
---|
4683 | #elif defined(_SC_PAGE_SIZE)
|
---|
4684 | pagesize = sysconf(_SC_PAGE_SIZE);
|
---|
4685 | #elif defined(HAVE_GETPAGESIZE)
|
---|
4686 | pagesize = getpagesize();
|
---|
4687 | #elif defined(PAGESIZE)
|
---|
4688 | pagesize = PAGESIZE;
|
---|
4689 | #endif
|
---|
4690 |
|
---|
4691 | return ((pagesize > 0) ? pagesize : 4096);
|
---|
4692 | }
|
---|
4693 |
|
---|
4694 | typedef struct sh_page_lt {
|
---|
4695 | unsigned long page_start;
|
---|
4696 | int page_refcount;
|
---|
4697 | char file[64];
|
---|
4698 | int line;
|
---|
4699 | struct sh_page_lt * next;
|
---|
4700 | } sh_page_l;
|
---|
4701 |
|
---|
4702 | sh_page_l * sh_page_locked = NULL;
|
---|
4703 | volatile int page_locking = 0;
|
---|
4704 |
|
---|
4705 | unsigned long sh_unix_lookup_page (void * in_addr, size_t len, int * num_pages)
|
---|
4706 | {
|
---|
4707 | int pagesize = sh_unix_pagesize();
|
---|
4708 | unsigned long addr = (unsigned long) in_addr;
|
---|
4709 |
|
---|
4710 | unsigned long pagebase;
|
---|
4711 | unsigned long pagediff;
|
---|
4712 | unsigned long pagenum = addr / pagesize;
|
---|
4713 |
|
---|
4714 | SL_ENTER(_("sh_unix_lookup_page"));
|
---|
4715 | #if 0
|
---|
4716 | fprintf(stderr, "mlock: --> base %ld, pagenum: %ld\n",
|
---|
4717 | addr, pagenum);
|
---|
4718 | #endif
|
---|
4719 |
|
---|
4720 | /* address of first page
|
---|
4721 | */
|
---|
4722 | pagebase = pagenum * pagesize;
|
---|
4723 |
|
---|
4724 | /* number of pages
|
---|
4725 | */
|
---|
4726 | pagediff = (addr + len) - pagebase;
|
---|
4727 | pagenum = pagediff / pagesize;
|
---|
4728 | if (pagenum * pagesize < pagediff)
|
---|
4729 | ++pagenum;
|
---|
4730 |
|
---|
4731 | #if 0
|
---|
4732 | fprintf(stderr, "mlock: --> pagebase %ld, pagediff %ld, (addr + len) %ld\n",
|
---|
4733 | pagebase, pagediff, (addr + len));
|
---|
4734 | #endif
|
---|
4735 |
|
---|
4736 | *num_pages = pagenum;
|
---|
4737 | SL_RETURN((pagebase), _("sh_unix_lookup_page"));
|
---|
4738 | }
|
---|
4739 |
|
---|
4740 |
|
---|
4741 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
4742 |
|
---|
4743 | SH_MUTEX_STATIC(mutex_mlock,PTHREAD_MUTEX_INITIALIZER);
|
---|
4744 |
|
---|
4745 | int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
|
---|
4746 | {
|
---|
4747 | int num_pages;
|
---|
4748 | int status = 0;
|
---|
4749 | int pagesize;
|
---|
4750 | sh_page_l * page_list;
|
---|
4751 | unsigned long addr;
|
---|
4752 | #ifdef TEST_MLOCK
|
---|
4753 | int i = 0;
|
---|
4754 | #endif
|
---|
4755 |
|
---|
4756 | SL_ENTER(_("sh_unix_mlock"));
|
---|
4757 |
|
---|
4758 | /* There's no cancellation point here, except if tracing is on
|
---|
4759 | */
|
---|
4760 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
4761 |
|
---|
4762 | page_list = sh_page_locked;
|
---|
4763 |
|
---|
4764 | if (0 != page_locking)
|
---|
4765 | {
|
---|
4766 | status = -1;
|
---|
4767 | goto exit_mlock;
|
---|
4768 | }
|
---|
4769 |
|
---|
4770 | page_locking = 1;
|
---|
4771 |
|
---|
4772 | pagesize = sh_unix_pagesize();
|
---|
4773 | addr = sh_unix_lookup_page (in_addr, len, &num_pages);
|
---|
4774 |
|
---|
4775 | #ifdef TEST_MLOCK
|
---|
4776 | fprintf(stderr, "mlock: addr %ld, base %ld, pages: %d, length %d\n",
|
---|
4777 | (unsigned long) in_addr, addr, num_pages, len);
|
---|
4778 | #endif
|
---|
4779 |
|
---|
4780 | /* increase refcount of locked pages
|
---|
4781 | * addr is first page; num_pages is #(consecutive pages) to lock
|
---|
4782 | */
|
---|
4783 |
|
---|
4784 | while ((page_list != NULL) && (num_pages > 0))
|
---|
4785 | {
|
---|
4786 | #ifdef TEST_MLOCK
|
---|
4787 | fprintf(stderr, "mlock: check page %d: %ld [%d]\n",
|
---|
4788 | i, page_list->page_start, page_list->page_refcount);
|
---|
4789 | #endif
|
---|
4790 | if (page_list->page_start == addr)
|
---|
4791 | {
|
---|
4792 | page_list->page_refcount += 1;
|
---|
4793 | num_pages -= 1;
|
---|
4794 | addr += pagesize;
|
---|
4795 | #ifdef TEST_MLOCK
|
---|
4796 | fprintf(stderr, "mlock: found page %d: %ld [%d], next page %ld\n",
|
---|
4797 | i, page_list->page_start, page_list->page_refcount, addr);
|
---|
4798 | #endif
|
---|
4799 | }
|
---|
4800 | #ifdef TEST_MLOCK
|
---|
4801 | ++i;
|
---|
4802 | #endif
|
---|
4803 | page_list = page_list->next;
|
---|
4804 | }
|
---|
4805 |
|
---|
4806 | /* mlock some more pages, if needed
|
---|
4807 | */
|
---|
4808 | while (num_pages > 0)
|
---|
4809 | {
|
---|
4810 | #ifdef TEST_MLOCK
|
---|
4811 | fprintf(stderr, "mlock: lock page %d: mlock %ld [num_pages %d]\n",
|
---|
4812 | i, addr, num_pages);
|
---|
4813 | ++i;
|
---|
4814 | #endif
|
---|
4815 | page_list = SH_ALLOC(sizeof(sh_page_l));
|
---|
4816 | page_list->page_start = addr;
|
---|
4817 | page_list->page_refcount = 1;
|
---|
4818 | sl_strlcpy(page_list->file, file, 64);
|
---|
4819 | page_list->line = line;
|
---|
4820 | status = mlock( (void *) addr, pagesize);
|
---|
4821 | if (status != 0)
|
---|
4822 | {
|
---|
4823 | #ifdef TEST_MLOCK
|
---|
4824 | char errbuf[SH_ERRBUF_SIZE];
|
---|
4825 | fprintf(stderr, "mlock: error: %s\n",
|
---|
4826 | sh_error_message(errno, errbuf, sizeof(errbuf)));
|
---|
4827 | #endif
|
---|
4828 | SH_FREE(page_list);
|
---|
4829 | page_locking = 0;
|
---|
4830 | goto exit_mlock;
|
---|
4831 | }
|
---|
4832 | page_list->next = sh_page_locked;
|
---|
4833 | sh_page_locked = page_list;
|
---|
4834 | num_pages -= 1;
|
---|
4835 | addr += pagesize;
|
---|
4836 | }
|
---|
4837 | page_locking = 0;
|
---|
4838 |
|
---|
4839 | exit_mlock:
|
---|
4840 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
4841 |
|
---|
4842 | SL_RETURN((status), _("sh_unix_mlock"));
|
---|
4843 | }
|
---|
4844 | #else
|
---|
4845 | int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
|
---|
4846 | {
|
---|
4847 | (void) file; (void) line;
|
---|
4848 | (void) in_addr; (void) len;
|
---|
4849 | return -1;
|
---|
4850 | }
|
---|
4851 | #endif
|
---|
4852 |
|
---|
4853 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
4854 | int sh_unix_munlock (void * in_addr, size_t len)
|
---|
4855 | {
|
---|
4856 | int num_pages;
|
---|
4857 | int unlocked;
|
---|
4858 | int status;
|
---|
4859 | int pagesize;
|
---|
4860 | sh_page_l * page_list;
|
---|
4861 | sh_page_l * page_last;
|
---|
4862 | unsigned long addr;
|
---|
4863 |
|
---|
4864 | int test_count;
|
---|
4865 | int test_status;
|
---|
4866 | int test_pages;
|
---|
4867 |
|
---|
4868 | #ifdef TEST_MLOCK
|
---|
4869 | int i = 0;
|
---|
4870 | #endif
|
---|
4871 |
|
---|
4872 | SL_ENTER(_("sh_unix_munlock"));
|
---|
4873 |
|
---|
4874 | /* There's no cancellation point here, except if tracing is on
|
---|
4875 | */
|
---|
4876 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
4877 |
|
---|
4878 | unlocked = 0;
|
---|
4879 | status = 0;
|
---|
4880 | page_list = sh_page_locked;
|
---|
4881 |
|
---|
4882 | if (0 != page_locking)
|
---|
4883 | {
|
---|
4884 | status = -1;
|
---|
4885 | goto exit_munlock;
|
---|
4886 | }
|
---|
4887 | page_locking = 1;
|
---|
4888 |
|
---|
4889 | pagesize = sh_unix_pagesize();
|
---|
4890 | addr = sh_unix_lookup_page (in_addr, len, &num_pages);
|
---|
4891 |
|
---|
4892 | #ifdef TEST_MLOCK
|
---|
4893 | fprintf(stderr, "munlock: in_addr %ld, addr %ld, pages: %d, length %d\n",
|
---|
4894 | (unsigned long) in_addr, addr, num_pages, len);
|
---|
4895 | #endif
|
---|
4896 |
|
---|
4897 | test_pages = num_pages;
|
---|
4898 |
|
---|
4899 | /* reduce refcount of locked pages
|
---|
4900 | * addr is first page; num_pages is #(consecutive pages) to lock
|
---|
4901 | */
|
---|
4902 | while ((page_list != NULL) && (num_pages > 0))
|
---|
4903 | {
|
---|
4904 | #ifdef TEST_MLOCK
|
---|
4905 | fprintf(stderr, "munlock: page %d: %ld [%d]\n",
|
---|
4906 | i, page_list->page_start, page_list->page_refcount);
|
---|
4907 | #endif
|
---|
4908 |
|
---|
4909 | test_status = 0;
|
---|
4910 | for (test_count = 0; test_count < test_pages; ++test_count)
|
---|
4911 | {
|
---|
4912 | if (page_list->page_start == (addr + (test_count * pagesize)))
|
---|
4913 | {
|
---|
4914 | test_status = 1;
|
---|
4915 | break;
|
---|
4916 | }
|
---|
4917 | }
|
---|
4918 |
|
---|
4919 | if (test_status == 1)
|
---|
4920 | {
|
---|
4921 | page_list->page_refcount -= 1;
|
---|
4922 | if (page_list->page_refcount == 0)
|
---|
4923 | {
|
---|
4924 | status = munlock ( (void *) addr, pagesize);
|
---|
4925 | ++unlocked;
|
---|
4926 | }
|
---|
4927 | num_pages -= 1;
|
---|
4928 | #ifdef TEST_MLOCK
|
---|
4929 | fprintf(stderr,
|
---|
4930 | "munlock: page %d: %ld [refcount %d], refcount reduced\n",
|
---|
4931 | i, page_list->page_start, page_list->page_refcount);
|
---|
4932 | #endif
|
---|
4933 | }
|
---|
4934 | #ifdef TEST_MLOCK
|
---|
4935 | ++i;
|
---|
4936 | #endif
|
---|
4937 | page_list = page_list->next;
|
---|
4938 | }
|
---|
4939 |
|
---|
4940 | #ifdef TEST_MLOCK
|
---|
4941 | i = 0;
|
---|
4942 | #endif
|
---|
4943 |
|
---|
4944 | if (unlocked > 0)
|
---|
4945 | {
|
---|
4946 | page_list = sh_page_locked;
|
---|
4947 | page_last = sh_page_locked;
|
---|
4948 |
|
---|
4949 | while ((page_list != NULL) && (unlocked > 0))
|
---|
4950 | {
|
---|
4951 | if (page_list->page_refcount == 0)
|
---|
4952 | {
|
---|
4953 | #ifdef TEST_MLOCK
|
---|
4954 | fprintf(stderr, "munlock: remove page %d: %ld [refcount %d]\n",
|
---|
4955 | i, page_list->page_start, page_list->page_refcount);
|
---|
4956 | #endif
|
---|
4957 | if (page_last != page_list)
|
---|
4958 | {
|
---|
4959 | page_last->next = page_list->next;
|
---|
4960 | SH_FREE(page_list);
|
---|
4961 | page_list = page_last->next;
|
---|
4962 | }
|
---|
4963 | else
|
---|
4964 | {
|
---|
4965 | page_last = page_list->next;
|
---|
4966 | if (page_list == sh_page_locked)
|
---|
4967 | sh_page_locked = page_list->next;
|
---|
4968 | SH_FREE(page_list);
|
---|
4969 | page_list = page_last;
|
---|
4970 | }
|
---|
4971 | --unlocked;
|
---|
4972 | }
|
---|
4973 | else
|
---|
4974 | {
|
---|
4975 | #ifdef TEST_MLOCK
|
---|
4976 | fprintf(stderr, "munlock: skip page %d: %ld [refcount %d]\n",
|
---|
4977 | i, page_list->page_start, page_list->page_refcount);
|
---|
4978 | #endif
|
---|
4979 |
|
---|
4980 | page_last = page_list;
|
---|
4981 | page_list = page_list->next;
|
---|
4982 | }
|
---|
4983 | #ifdef TEST_MLOCK
|
---|
4984 | ++i;
|
---|
4985 | #endif
|
---|
4986 | }
|
---|
4987 | }
|
---|
4988 |
|
---|
4989 | page_locking = 0;
|
---|
4990 |
|
---|
4991 | exit_munlock:
|
---|
4992 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
4993 | SL_RETURN((status), _("sh_unix_munlock"));
|
---|
4994 | }
|
---|
4995 | #else
|
---|
4996 | int sh_unix_munlock (void * in_addr, size_t len)
|
---|
4997 | {
|
---|
4998 | (void) in_addr; (void) len;
|
---|
4999 | return -1;
|
---|
5000 | }
|
---|
5001 | #endif
|
---|
5002 |
|
---|
5003 | int sh_unix_count_mlock()
|
---|
5004 | {
|
---|
5005 | int i = 0;
|
---|
5006 | char str[32][64];
|
---|
5007 | sh_page_l * page_list;
|
---|
5008 |
|
---|
5009 | SL_ENTER(_("sh_unix_count_mlock"));
|
---|
5010 |
|
---|
5011 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
5012 | /* There's no cancellation point here, except if tracing is on
|
---|
5013 | */
|
---|
5014 | SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
|
---|
5015 | #endif
|
---|
5016 |
|
---|
5017 | page_list = sh_page_locked;
|
---|
5018 |
|
---|
5019 | while (page_list != NULL)
|
---|
5020 | {
|
---|
5021 | #ifdef WITH_TPT
|
---|
5022 | if (i < 32)
|
---|
5023 | sl_snprintf(str[i], 64, _("file: %s line: %d page: %d"),
|
---|
5024 | page_list->file, page_list->line, i+1);
|
---|
5025 | #endif
|
---|
5026 | page_list = page_list->next;
|
---|
5027 | ++i;
|
---|
5028 | }
|
---|
5029 |
|
---|
5030 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
5031 | SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
|
---|
5032 | #endif
|
---|
5033 |
|
---|
5034 | #ifdef WITH_TPT
|
---|
5035 | {
|
---|
5036 | int j = 0;
|
---|
5037 | while (j < i && j < 32)
|
---|
5038 | {
|
---|
5039 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, j, MSG_E_SUBGEN,
|
---|
5040 | str[j], _("sh_unix_count_mlock"));
|
---|
5041 | ++j;
|
---|
5042 | }
|
---|
5043 | }
|
---|
5044 | #endif
|
---|
5045 |
|
---|
5046 | sl_snprintf(str[0], 64, _("%d pages locked"), i);
|
---|
5047 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, i, MSG_E_SUBGEN,
|
---|
5048 | str[0], _("sh_unix_count_mlock"));
|
---|
5049 | SL_RETURN((i), _("sh_unix_count_mlock"));
|
---|
5050 | }
|
---|
5051 |
|
---|
5052 | /************************************************/
|
---|
5053 | /************************************************/
|
---|
5054 | /**** Stealth Utilities ****/
|
---|
5055 | /************************************************/
|
---|
5056 | /************************************************/
|
---|
5057 | #ifdef SH_STEALTH
|
---|
5058 |
|
---|
5059 | void sh_unix_xor_code (char * str, int len)
|
---|
5060 | {
|
---|
5061 | register int i;
|
---|
5062 |
|
---|
5063 | for (i = 0; i < len; ++i) str[i] ^= (char) XOR_CODE;
|
---|
5064 | return;
|
---|
5065 | }
|
---|
5066 |
|
---|
5067 | #if !defined(SH_STEALTH_MICRO)
|
---|
5068 |
|
---|
5069 |
|
---|
5070 | int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
|
---|
5071 | unsigned long * bytes_read);
|
---|
5072 | unsigned long first_hex_block(SL_TICKET fd, unsigned long * max);
|
---|
5073 |
|
---|
5074 | /*
|
---|
5075 | * --- Get hidden data from a block of hex data. ---
|
---|
5076 | */
|
---|
5077 | int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len)
|
---|
5078 | {
|
---|
5079 | int add_off = 0, llen;
|
---|
5080 | static unsigned long off_data = 0;
|
---|
5081 | static unsigned long max_data = 0;
|
---|
5082 | static unsigned long bytes_read = 0;
|
---|
5083 | static int stealth_init = BAD;
|
---|
5084 |
|
---|
5085 | SL_ENTER(_("sh_unix_getline_stealth"));
|
---|
5086 |
|
---|
5087 | if (str == NULL)
|
---|
5088 | {
|
---|
5089 | off_data = 0;
|
---|
5090 | max_data = 0;
|
---|
5091 | bytes_read = 0;
|
---|
5092 | stealth_init = BAD;
|
---|
5093 | SL_RETURN(0, _("sh_unix_getline_stealth"));
|
---|
5094 | }
|
---|
5095 |
|
---|
5096 | /* --- Initialize. ---
|
---|
5097 | */
|
---|
5098 | if (stealth_init == BAD)
|
---|
5099 | {
|
---|
5100 | off_data = first_hex_block(fd, &max_data);
|
---|
5101 | if (off_data == 0)
|
---|
5102 | {
|
---|
5103 | dlog(1, FIL__, __LINE__,
|
---|
5104 | _("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"));
|
---|
5105 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
|
---|
5106 | _("Stealth config file."));
|
---|
5107 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
5108 | }
|
---|
5109 | stealth_init = GOOD;
|
---|
5110 | max_data += off_data;
|
---|
5111 | }
|
---|
5112 |
|
---|
5113 | /* --- Seek to proper position. ---
|
---|
5114 | */
|
---|
5115 | if (bytes_read >= max_data || add_off < 0)
|
---|
5116 | {
|
---|
5117 | dlog(1, FIL__, __LINE__,
|
---|
5118 | _("The capacity of the container image file for the stealth config file seems to be too small. Your config file is likely truncated.\n"));
|
---|
5119 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
|
---|
5120 | _("Stealth config file."));
|
---|
5121 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
5122 | }
|
---|
5123 | sl_seek(fd, off_data);
|
---|
5124 |
|
---|
5125 | /* --- Read one line. ---
|
---|
5126 | */
|
---|
5127 | add_off = hideout_hex_block(fd, (unsigned char *) str, len, &bytes_read);
|
---|
5128 | off_data += add_off;
|
---|
5129 |
|
---|
5130 | llen = sl_strlen(str);
|
---|
5131 | SL_RETURN(llen, _("sh_unix_getline_stealth"));
|
---|
5132 | }
|
---|
5133 |
|
---|
5134 | int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
|
---|
5135 | unsigned long * bytes_read)
|
---|
5136 | {
|
---|
5137 |
|
---|
5138 | register int i, j, k;
|
---|
5139 | unsigned char c, e;
|
---|
5140 | register int num;
|
---|
5141 | unsigned char mask[9] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
|
---|
5142 | unsigned long here = 0;
|
---|
5143 | unsigned long retval = 0;
|
---|
5144 | unsigned long bread = 0;
|
---|
5145 |
|
---|
5146 | SL_ENTER(_("hideout_hex_block"));
|
---|
5147 |
|
---|
5148 | ASSERT_RET((len > 1), _("len > 1"), (0));
|
---|
5149 |
|
---|
5150 | --len;
|
---|
5151 |
|
---|
5152 | i = 0;
|
---|
5153 | while (i < len)
|
---|
5154 | {
|
---|
5155 | for (j = 0; j < 8; ++j)
|
---|
5156 | {
|
---|
5157 |
|
---|
5158 | /* --- Get a low byte, modify, read back. ---
|
---|
5159 | */
|
---|
5160 | for (k = 0; k < 2; ++k)
|
---|
5161 | {
|
---|
5162 | /* -- Skip whitespace. ---
|
---|
5163 | */
|
---|
5164 | c = ' ';
|
---|
5165 | do {
|
---|
5166 | do {
|
---|
5167 | num = sl_read (fd, &c, 1);
|
---|
5168 | } while (num == 0 && errno == EINTR);
|
---|
5169 | if (num > 0)
|
---|
5170 | ++here;
|
---|
5171 | else if (num == 0)
|
---|
5172 | SL_RETURN((0), _("hideout_hex_block"));
|
---|
5173 | else
|
---|
5174 | SL_RETURN((-1), _("hideout_hex_block"));
|
---|
5175 | } while (c == '\n' || c == '\t' || c == '\r' ||
|
---|
5176 | c == ' ');
|
---|
5177 | }
|
---|
5178 |
|
---|
5179 |
|
---|
5180 | /* --- e is the value of the low byte. ---
|
---|
5181 | */
|
---|
5182 | e = (unsigned char) sh_util_hexchar( c );
|
---|
5183 | if ((e & mask[7]) != 0) /* bit is set */
|
---|
5184 | str[i] |= mask[j];
|
---|
5185 | else /* bit is not set */
|
---|
5186 | str[i] &= ~mask[j];
|
---|
5187 |
|
---|
5188 | bread += 1;
|
---|
5189 | }
|
---|
5190 | if (str[i] == '\n') break;
|
---|
5191 | ++i;
|
---|
5192 | }
|
---|
5193 |
|
---|
5194 | if (i != 0)
|
---|
5195 | str[i] = '\0';
|
---|
5196 | else
|
---|
5197 | str[i+1] = '\0'; /* keep newline and terminate */
|
---|
5198 | retval += here;
|
---|
5199 | *bytes_read += (bread/8);
|
---|
5200 |
|
---|
5201 | SL_RETURN(retval, _("hideout_hex_block"));
|
---|
5202 | }
|
---|
5203 |
|
---|
5204 | /* --- Get offset of first data block. ---
|
---|
5205 | */
|
---|
5206 | unsigned long first_hex_block(SL_TICKET fd, unsigned long * max)
|
---|
5207 | {
|
---|
5208 | unsigned int i;
|
---|
5209 | long num = 1;
|
---|
5210 | unsigned long lnum;
|
---|
5211 | char c;
|
---|
5212 | int nothex = 0;
|
---|
5213 | unsigned long retval = 0;
|
---|
5214 | unsigned int this_line = 0;
|
---|
5215 | char theline[SH_BUFSIZE];
|
---|
5216 |
|
---|
5217 | SL_ENTER(_("first_hex_block"));
|
---|
5218 |
|
---|
5219 | *max = 0;
|
---|
5220 |
|
---|
5221 | while (1)
|
---|
5222 | {
|
---|
5223 | theline[0] = '\0';
|
---|
5224 | this_line = 0;
|
---|
5225 | c = '\0';
|
---|
5226 | while (c != '\n' && num > 0 && this_line < (sizeof(theline)-1))
|
---|
5227 | {
|
---|
5228 | do {
|
---|
5229 | num = sl_read (fd, &c, 1);
|
---|
5230 | } while (num == 0 && errno == EINTR);
|
---|
5231 | if (num > 0)
|
---|
5232 | theline[this_line] = c;
|
---|
5233 | else
|
---|
5234 | SL_RETURN((0), _("first_hex_block"));
|
---|
5235 | ++this_line;
|
---|
5236 | }
|
---|
5237 | theline[this_line] = '\0';
|
---|
5238 |
|
---|
5239 | /* not only 'newline' */
|
---|
5240 | if (this_line > 60)
|
---|
5241 | {
|
---|
5242 | nothex = 0;
|
---|
5243 | i = 0;
|
---|
5244 | while (nothex == 0 && i < (this_line-1))
|
---|
5245 | {
|
---|
5246 | if (! isxdigit((int)theline[i])) nothex = 1;
|
---|
5247 | ++i;
|
---|
5248 | }
|
---|
5249 | if (nothex == 1) retval += this_line;
|
---|
5250 | }
|
---|
5251 | else
|
---|
5252 | {
|
---|
5253 | nothex = 1;
|
---|
5254 | retval += this_line;
|
---|
5255 | }
|
---|
5256 |
|
---|
5257 | if (nothex == 0)
|
---|
5258 | {
|
---|
5259 | *max = 0;
|
---|
5260 | do {
|
---|
5261 | do {
|
---|
5262 | num = sl_read (fd, theline, SH_BUFSIZE);
|
---|
5263 | } while (num == 0 && errno == EINTR);
|
---|
5264 | if (num > 0)
|
---|
5265 | {
|
---|
5266 | lnum = (unsigned long) num;
|
---|
5267 | for (i = 0; i < lnum; ++i)
|
---|
5268 | {
|
---|
5269 | c = theline[i];
|
---|
5270 | if (c == '\n' || c == '\t' || c == '\r' || c == ' ')
|
---|
5271 | ;
|
---|
5272 | else if (!isxdigit((int)c))
|
---|
5273 | break;
|
---|
5274 | else
|
---|
5275 | *max += 1;
|
---|
5276 | }
|
---|
5277 | }
|
---|
5278 | } while (num > 0);
|
---|
5279 |
|
---|
5280 | *max /= 16;
|
---|
5281 | SL_RETURN((retval), _("first_hex_block"));
|
---|
5282 | }
|
---|
5283 |
|
---|
5284 | }
|
---|
5285 | /* SL_RETURN((0), _("first_hex_block")); *//* unreachable */
|
---|
5286 | }
|
---|
5287 |
|
---|
5288 | /* if !defined(SH_STEALTH_MICRO) */
|
---|
5289 | #endif
|
---|
5290 |
|
---|
5291 | /* ifdef SH_STEALTH */
|
---|
5292 | #endif
|
---|
5293 |
|
---|
5294 | /*
|
---|
5295 | * anti-debugger code
|
---|
5296 | */
|
---|
5297 | #if defined(SCREW_IT_UP)
|
---|
5298 | volatile int sh_not_traced = 0;
|
---|
5299 |
|
---|
5300 | #ifdef HAVE_GETTIMEOFDAY
|
---|
5301 | struct timeval save_tv;
|
---|
5302 | #endif
|
---|
5303 |
|
---|
5304 | void sh_sigtrap_handler (int signum)
|
---|
5305 | {
|
---|
5306 | #ifdef HAVE_GETTIMEOFDAY
|
---|
5307 | struct timeval tv;
|
---|
5308 | long difftv;
|
---|
5309 |
|
---|
5310 | gettimeofday(&tv, NULL);
|
---|
5311 | difftv = (tv.tv_sec - save_tv.tv_sec) * 1000000 +
|
---|
5312 | (tv.tv_usec - save_tv.tv_usec);
|
---|
5313 | if (difftv > 500000)
|
---|
5314 | raise(SIGKILL);
|
---|
5315 | #endif
|
---|
5316 | sh_not_traced += signum;
|
---|
5317 | return;
|
---|
5318 | }
|
---|
5319 | #endif
|
---|