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