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 | #ifdef HOST_IS_HPUX
|
---|
23 | #define _XOPEN_SOURCE_EXTENDED
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <errno.h>
|
---|
29 |
|
---|
30 | #include <sys/types.h>
|
---|
31 | #include <unistd.h>
|
---|
32 | #include <sys/stat.h>
|
---|
33 | #include <fcntl.h>
|
---|
34 | #include <signal.h>
|
---|
35 | #include <sys/socket.h>
|
---|
36 | #include <netinet/in.h>
|
---|
37 |
|
---|
38 | #ifndef S_SPLINT_S
|
---|
39 | #include <arpa/inet.h>
|
---|
40 | #else
|
---|
41 | #define AF_INET 2
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <time.h>
|
---|
45 |
|
---|
46 | #ifndef HAVE_LSTAT
|
---|
47 | #define lstat stat
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #include "samhain.h"
|
---|
51 | #include "sh_error.h"
|
---|
52 | #include "sh_calls.h"
|
---|
53 | #include "sh_ipvx.h"
|
---|
54 |
|
---|
55 | #undef FIL__
|
---|
56 | #define FIL__ _("sh_calls.c")
|
---|
57 |
|
---|
58 | char aud_err_message[64];
|
---|
59 |
|
---|
60 | typedef struct cht_struct
|
---|
61 | {
|
---|
62 | const char * str;
|
---|
63 | unsigned long val;
|
---|
64 | } cht_type;
|
---|
65 |
|
---|
66 | static cht_type aud_tab[] =
|
---|
67 | {
|
---|
68 | { N_("execve"), AUD_EXEC },
|
---|
69 | { N_("utime"), AUD_UTIME },
|
---|
70 | { N_("unlink"), AUD_UNLINK },
|
---|
71 | { N_("dup"), AUD_DUP },
|
---|
72 | { N_("chdir"), AUD_CHDIR },
|
---|
73 | { N_("open"), AUD_OPEN },
|
---|
74 | { N_("kill"), AUD_KILL },
|
---|
75 | { N_("exit"), AUD_EXIT },
|
---|
76 | { N_("fork"), AUD_FORK },
|
---|
77 | { N_("setuid"), AUD_SETUID },
|
---|
78 | { N_("setgid"), AUD_SETGID },
|
---|
79 | { N_("pipe"), AUD_PIPE },
|
---|
80 | { NULL, 0 }
|
---|
81 | };
|
---|
82 |
|
---|
83 | /* Set aud functions
|
---|
84 | */
|
---|
85 | int sh_aud_set_functions(const char * str_s)
|
---|
86 | {
|
---|
87 | int i = 0;
|
---|
88 |
|
---|
89 | SL_ENTER(_("sh_aud_set_functions"));
|
---|
90 |
|
---|
91 | if (str_s == NULL)
|
---|
92 | return -1;
|
---|
93 |
|
---|
94 | while (aud_tab[i].str != NULL)
|
---|
95 | {
|
---|
96 | if (NULL != sl_strstr (str_s, _(aud_tab[i].str)))
|
---|
97 | {
|
---|
98 | sh.flag.audit = 1;
|
---|
99 | sh.flag.aud_mask |= aud_tab[i].val;
|
---|
100 | }
|
---|
101 | ++i;
|
---|
102 | }
|
---|
103 |
|
---|
104 | SL_RETURN(0,_("sh_aud_set_functions"));
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | /* Need to catch EINTR for these functions.
|
---|
111 | */
|
---|
112 | long int retry_sigaction(const char * file, int line,
|
---|
113 | int signum, const struct sigaction *act,
|
---|
114 | struct sigaction *oldact)
|
---|
115 | {
|
---|
116 | int error;
|
---|
117 | long int val_retry = -1;
|
---|
118 | char errbuf[SH_ERRBUF_SIZE];
|
---|
119 | errno = 0;
|
---|
120 |
|
---|
121 | SL_ENTER(_("retry_sigaction"));
|
---|
122 |
|
---|
123 | do {
|
---|
124 | val_retry = sigaction(signum, act, oldact);
|
---|
125 | } while (val_retry < 0 && errno == EINTR);
|
---|
126 |
|
---|
127 | error = errno;
|
---|
128 | if (val_retry < 0) {
|
---|
129 | sh_error_handle ((-1), file, line, error, MSG_ERR_SIGACT,
|
---|
130 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
131 | (long) signum );
|
---|
132 | }
|
---|
133 | errno = error;
|
---|
134 | SL_RETURN(val_retry, _("retry_sigaction"));
|
---|
135 | }
|
---|
136 |
|
---|
137 | static struct sh_sockaddr bind_addr;
|
---|
138 | static int use_bind_addr = 0;
|
---|
139 |
|
---|
140 | int sh_calls_set_bind_addr (const char * str)
|
---|
141 | {
|
---|
142 | static int reject = 0;
|
---|
143 |
|
---|
144 | if (reject == 1)
|
---|
145 | return (0);
|
---|
146 |
|
---|
147 | if (sh.flag.opts == S_TRUE)
|
---|
148 | reject = 1;
|
---|
149 |
|
---|
150 | #if defined(USE_IPVX)
|
---|
151 | if (0 == sh_ipvx_aton(str, &bind_addr))
|
---|
152 | return -1;
|
---|
153 | #else
|
---|
154 | if (0 == inet_aton(str, &(bind_addr.sin.sin_addr)))
|
---|
155 | return -1;
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | use_bind_addr = 1;
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | long int retry_connect(const char * file, int line, int sockfd,
|
---|
164 | struct sockaddr *serv_addr, int addrlen)
|
---|
165 | {
|
---|
166 | int error;
|
---|
167 | long int val_retry = 0;
|
---|
168 | char errbuf[SH_ERRBUF_SIZE];
|
---|
169 |
|
---|
170 | SL_ENTER(_("retry_connect"));
|
---|
171 |
|
---|
172 | errno = 0;
|
---|
173 |
|
---|
174 | if (0 != use_bind_addr)
|
---|
175 | {
|
---|
176 | int slen = SH_SS_LEN(bind_addr);
|
---|
177 |
|
---|
178 | val_retry = bind(sockfd, sh_ipvx_sockaddr_cast(&bind_addr), slen);
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (val_retry == 0)
|
---|
182 | {
|
---|
183 | do {
|
---|
184 | val_retry = connect(sockfd, serv_addr, addrlen);
|
---|
185 | } while (val_retry < 0 && (errno == EINTR || errno == EINPROGRESS));
|
---|
186 | }
|
---|
187 |
|
---|
188 | error = errno;
|
---|
189 | if (val_retry != 0) {
|
---|
190 | long eport;
|
---|
191 | char eaddr[SH_IP_BUF];
|
---|
192 |
|
---|
193 | struct sh_sockaddr ss;
|
---|
194 | sh_ipvx_save(&ss, serv_addr->sa_family, serv_addr);
|
---|
195 | sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
|
---|
196 |
|
---|
197 | if (serv_addr->sa_family == AF_INET)
|
---|
198 | eport = (long) ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
|
---|
199 | else
|
---|
200 | eport = (long) ntohs(((struct sockaddr_in6 *)serv_addr)->sin6_port);
|
---|
201 |
|
---|
202 | sh_error_handle ((-1), file, line, error, MSG_ERR_CONNECT,
|
---|
203 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
204 | (long) sockfd, eport, eaddr);
|
---|
205 | }
|
---|
206 | errno = error;
|
---|
207 | SL_RETURN(val_retry, _("retry_connect"));
|
---|
208 | }
|
---|
209 |
|
---|
210 | long int retry_accept(const char * file, int line, int fd,
|
---|
211 | struct sh_sockaddr *serv_addr, int * addrlen)
|
---|
212 | {
|
---|
213 | int error;
|
---|
214 | long int val_retry = -1;
|
---|
215 | char errbuf[SH_ERRBUF_SIZE];
|
---|
216 | struct sockaddr_storage ss;
|
---|
217 |
|
---|
218 | ACCEPT_TYPE_ARG3 my_addrlen = sizeof(ss);
|
---|
219 |
|
---|
220 | errno = 0;
|
---|
221 |
|
---|
222 | SL_ENTER(_("retry_accept"));
|
---|
223 |
|
---|
224 | do {
|
---|
225 | val_retry = accept(fd, (struct sockaddr *)&ss, &my_addrlen);
|
---|
226 | } while (val_retry < 0 && errno == EINTR);
|
---|
227 |
|
---|
228 | error = errno;
|
---|
229 | if (val_retry < 0) {
|
---|
230 | sh_error_handle ((-1), file, line, error, MSG_ERR_ACCEPT,
|
---|
231 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
232 | (long) fd );
|
---|
233 | }
|
---|
234 | errno = error;
|
---|
235 |
|
---|
236 | sh_ipvx_save(serv_addr, ss.ss_family, (struct sockaddr *) &ss);
|
---|
237 |
|
---|
238 | *addrlen = (int) my_addrlen;
|
---|
239 | SL_RETURN(val_retry, _("retry_accept"));
|
---|
240 | }
|
---|
241 |
|
---|
242 | long int retry_lstat(const char * file, int line,
|
---|
243 | const char *file_name, struct stat *buf)
|
---|
244 | {
|
---|
245 | int error;
|
---|
246 | long int val_retry = -1;
|
---|
247 | char errbuf[SH_ERRBUF_SIZE];
|
---|
248 |
|
---|
249 | SL_ENTER(_("retry_lstat"));
|
---|
250 |
|
---|
251 | do {
|
---|
252 | val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
|
---|
253 | } while (val_retry < 0 && errno == EINTR);
|
---|
254 | error = errno;
|
---|
255 | if (val_retry < 0) {
|
---|
256 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
257 | sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT,
|
---|
258 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
259 | file_name );
|
---|
260 | }
|
---|
261 | errno = error;
|
---|
262 | SL_RETURN(val_retry, _("retry_lstat"));
|
---|
263 | }
|
---|
264 |
|
---|
265 | long int retry_stat(const char * file, int line,
|
---|
266 | const char *file_name, struct stat *buf)
|
---|
267 | {
|
---|
268 | int error;
|
---|
269 | long int val_retry = -1;
|
---|
270 | char errbuf[SH_ERRBUF_SIZE];
|
---|
271 |
|
---|
272 | SL_ENTER(_("retry_stat"));
|
---|
273 |
|
---|
274 | do {
|
---|
275 | val_retry = stat (file_name, buf);
|
---|
276 | } while (val_retry < 0 && errno == EINTR);
|
---|
277 | error = errno;
|
---|
278 | if (val_retry < 0) {
|
---|
279 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
280 | sh_error_handle ((-1), file, line, error, MSG_ERR_STAT,
|
---|
281 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
282 | file_name );
|
---|
283 | }
|
---|
284 | errno = error;
|
---|
285 | SL_RETURN(val_retry, _("retry_stat"));
|
---|
286 | }
|
---|
287 |
|
---|
288 | long int retry_fstat(const char * file, int line, int filed, struct stat *buf)
|
---|
289 | {
|
---|
290 | int error;
|
---|
291 | long int val_retry = -1;
|
---|
292 | char errbuf[SH_ERRBUF_SIZE];
|
---|
293 |
|
---|
294 | SL_ENTER(_("retry_fstat"));
|
---|
295 |
|
---|
296 | do {
|
---|
297 | val_retry = fstat (filed, buf);
|
---|
298 | } while (val_retry < 0 && errno == EINTR);
|
---|
299 | error = errno;
|
---|
300 | if (val_retry < 0) {
|
---|
301 | sh_error_handle ((-1), file, line, error, MSG_ERR_FSTAT,
|
---|
302 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
303 | (long) filed );
|
---|
304 | }
|
---|
305 | errno = error;
|
---|
306 | SL_RETURN(val_retry, _("retry_fstat"));
|
---|
307 | }
|
---|
308 |
|
---|
309 | long int retry_fcntl(const char * file, int line, int fd, int cmd, long arg)
|
---|
310 | {
|
---|
311 | int error;
|
---|
312 | long int val_retry = -1;
|
---|
313 | char errbuf[SH_ERRBUF_SIZE];
|
---|
314 | errno = 0;
|
---|
315 |
|
---|
316 | SL_ENTER(_("retry_fcntl"));
|
---|
317 |
|
---|
318 | if (cmd == F_GETFD || cmd == F_GETFL)
|
---|
319 | {
|
---|
320 | do {
|
---|
321 | val_retry = fcntl(fd, cmd);
|
---|
322 | } while (val_retry < 0 && errno == EINTR);
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | do {
|
---|
327 | val_retry = fcntl(fd, cmd, arg);
|
---|
328 | } while (val_retry < 0 && errno == EINTR);
|
---|
329 | }
|
---|
330 | error = errno;
|
---|
331 | if (val_retry < 0) {
|
---|
332 | sh_error_handle ((-1), file, line, error, MSG_ERR_FCNTL,
|
---|
333 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
334 | (long) fd, (long) cmd, arg );
|
---|
335 | }
|
---|
336 | errno = error;
|
---|
337 | SL_RETURN(val_retry, _("retry_fcntl"));
|
---|
338 | }
|
---|
339 |
|
---|
340 | long int retry_msleep (int sec, int millisec)
|
---|
341 | {
|
---|
342 | int result = 0;
|
---|
343 | #if defined(HAVE_NANOSLEEP)
|
---|
344 | struct timespec req, rem;
|
---|
345 | #endif
|
---|
346 |
|
---|
347 | SL_ENTER(_("retry_msleep"));
|
---|
348 |
|
---|
349 | errno = 0;
|
---|
350 | if (millisec > 999) millisec = 999;
|
---|
351 | if (millisec < 0) millisec = 0;
|
---|
352 | if (sec < 0) sec = 0;
|
---|
353 |
|
---|
354 | #if defined(HAVE_NANOSLEEP)
|
---|
355 | /*@-usedef@*/
|
---|
356 | req.tv_sec = sec; rem.tv_sec = 0;
|
---|
357 | req.tv_nsec = millisec * 1000000; rem.tv_nsec = 0;
|
---|
358 | /*@+usedef@*/
|
---|
359 | do {
|
---|
360 | result = /*@-unrecog@*/nanosleep(&req, &rem)/*@+unrecog@*/;
|
---|
361 |
|
---|
362 | req.tv_sec = rem.tv_sec; rem.tv_sec = 0;
|
---|
363 | req.tv_nsec = rem.tv_nsec; rem.tv_nsec = 0;
|
---|
364 |
|
---|
365 | } while ((result == -1) && (errno == EINTR));
|
---|
366 | #else
|
---|
367 | if (sec > 0)
|
---|
368 | {
|
---|
369 | sleep (sec); /* nanosleep not available */
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | #ifdef HAVE_USLEEP
|
---|
374 | if (millisec > 0)
|
---|
375 | {
|
---|
376 | usleep(1000 * millisec);
|
---|
377 | }
|
---|
378 | #else
|
---|
379 | if (millisec > 0)
|
---|
380 | {
|
---|
381 | sleep (1);
|
---|
382 | }
|
---|
383 | #endif
|
---|
384 | }
|
---|
385 | #endif
|
---|
386 | SL_RETURN(result, _("retry_msleep"));
|
---|
387 | }
|
---|
388 |
|
---|
389 | /***************************************************
|
---|
390 | *
|
---|
391 | * Audit these functions.
|
---|
392 | *
|
---|
393 | ***************************************************/
|
---|
394 |
|
---|
395 | long int retry_aud_execve (const char * file, int line,
|
---|
396 | const char *dateiname, char * argv[],
|
---|
397 | char * envp[])
|
---|
398 | {
|
---|
399 | uid_t a = geteuid();
|
---|
400 | gid_t b = getegid();
|
---|
401 | int i;
|
---|
402 | int error;
|
---|
403 | char errbuf[SH_ERRBUF_SIZE];
|
---|
404 |
|
---|
405 | SL_ENTER(_("retry_aud_execve"));
|
---|
406 |
|
---|
407 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXEC) != 0)
|
---|
408 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXEC,
|
---|
409 | dateiname, (long) a, (long) b );
|
---|
410 | do {
|
---|
411 | i = execve(dateiname, argv, envp);
|
---|
412 | } while (i < 0 && errno == EINTR);
|
---|
413 |
|
---|
414 | error = errno;
|
---|
415 | if (i < 0) {
|
---|
416 | sh_error_handle ((-1), file, line, error, MSG_ERR_EXEC,
|
---|
417 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
418 | dateiname, (long) a, (long) b );
|
---|
419 | }
|
---|
420 | errno = error;
|
---|
421 | SL_RETURN(i, _("retry_aud_execve"));
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | long int retry_aud_utime (const char * file, int line,
|
---|
426 | char * path, struct utimbuf *buf)
|
---|
427 | {
|
---|
428 | long int val_return;
|
---|
429 | int error;
|
---|
430 | char errbuf[SH_ERRBUF_SIZE];
|
---|
431 | errno = 0;
|
---|
432 |
|
---|
433 | SL_ENTER(_("retry_aud_utime"));
|
---|
434 |
|
---|
435 | do {
|
---|
436 | val_return = utime (path, buf);
|
---|
437 | } while (val_return < 0 && errno == EINTR);
|
---|
438 |
|
---|
439 | error = errno;
|
---|
440 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UTIME) != 0)
|
---|
441 | sh_error_handle ((-1), file, line, 0, MSG_AUD_UTIME,
|
---|
442 | path,
|
---|
443 | (unsigned long) buf->actime,
|
---|
444 | (unsigned long) buf->modtime);
|
---|
445 | if (val_return < 0) {
|
---|
446 | sh_error_handle ((-1), file, line, error, MSG_ERR_UTIME,
|
---|
447 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
448 | path,
|
---|
449 | (unsigned long) buf->actime,
|
---|
450 | (unsigned long) buf->modtime);
|
---|
451 | }
|
---|
452 | errno = error;
|
---|
453 | SL_RETURN(val_return, _("retry_aud_utime"));
|
---|
454 | }
|
---|
455 |
|
---|
456 | long int retry_aud_unlink (const char * file, int line,
|
---|
457 | char * path)
|
---|
458 | {
|
---|
459 | long int val_return;
|
---|
460 | int error;
|
---|
461 | char errbuf[SH_ERRBUF_SIZE];
|
---|
462 | errno = 0;
|
---|
463 |
|
---|
464 | SL_ENTER(_("retry_aud_unlink"));
|
---|
465 |
|
---|
466 | do {
|
---|
467 | val_return = unlink (path);
|
---|
468 | } while (val_return < 0 && errno == EINTR);
|
---|
469 |
|
---|
470 | error = errno;
|
---|
471 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UNLINK) != 0)
|
---|
472 | sh_error_handle ((-1), file, line, 0, MSG_AUD_UNLINK,
|
---|
473 | path);
|
---|
474 | if (val_return < 0) {
|
---|
475 | sh_error_handle ((-1), file, line, error, MSG_ERR_UNLINK,
|
---|
476 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
477 | path);
|
---|
478 | }
|
---|
479 | errno = error;
|
---|
480 | SL_RETURN(val_return, _("retry_aud_unlink"));
|
---|
481 | }
|
---|
482 |
|
---|
483 | long int retry_aud_dup2 (const char * file, int line,
|
---|
484 | int fd, int fd2)
|
---|
485 | {
|
---|
486 | long int val_return;
|
---|
487 | int error;
|
---|
488 | char errbuf[SH_ERRBUF_SIZE];
|
---|
489 | errno = 0;
|
---|
490 |
|
---|
491 | SL_ENTER(_("retry_aud_dup2"));
|
---|
492 |
|
---|
493 | do {
|
---|
494 | val_return = dup2 (fd, fd2);
|
---|
495 | } while (val_return < 0 && errno == EINTR);
|
---|
496 |
|
---|
497 | error = errno;
|
---|
498 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
|
---|
499 | sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
|
---|
500 | (long) fd, val_return);
|
---|
501 | if (val_return < 0) {
|
---|
502 | sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
|
---|
503 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
504 | (long) fd, val_return);
|
---|
505 | }
|
---|
506 | errno = error;
|
---|
507 | SL_RETURN(val_return, _("retry_aud_dup2"));
|
---|
508 | }
|
---|
509 |
|
---|
510 | long int retry_aud_dup (const char * file, int line,
|
---|
511 | int fd)
|
---|
512 | {
|
---|
513 | long int val_return;
|
---|
514 | int error;
|
---|
515 | char errbuf[SH_ERRBUF_SIZE];
|
---|
516 | errno = 0;
|
---|
517 |
|
---|
518 | SL_ENTER(_("retry_aud_dup"));
|
---|
519 |
|
---|
520 | do {
|
---|
521 | val_return = dup (fd);
|
---|
522 | } while (val_return < 0 && errno == EINTR);
|
---|
523 | error = errno;
|
---|
524 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
|
---|
525 | sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
|
---|
526 | (long) fd, val_return);
|
---|
527 | if (val_return < 0) {
|
---|
528 | sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
|
---|
529 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
530 | (long) fd, val_return);
|
---|
531 | }
|
---|
532 | errno = error;
|
---|
533 | SL_RETURN(val_return, _("retry_aud_dup"));
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 |
|
---|
538 | long int retry_aud_chdir (const char * file, int line,
|
---|
539 | const char *path)
|
---|
540 | {
|
---|
541 | long int val_return;
|
---|
542 | int error = 0;
|
---|
543 | char errbuf[SH_ERRBUF_SIZE];
|
---|
544 | errno = 0;
|
---|
545 |
|
---|
546 | SL_ENTER(_("retry_aud_chdir"));
|
---|
547 |
|
---|
548 | do {
|
---|
549 | val_return = chdir (path);
|
---|
550 | } while (val_return < 0 && errno == EINTR);
|
---|
551 |
|
---|
552 | error = errno;
|
---|
553 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_CHDIR) != 0)
|
---|
554 | sh_error_handle ((-1), file, line, 0, MSG_AUD_CHDIR,
|
---|
555 | path);
|
---|
556 | if (val_return < 0) {
|
---|
557 | sh_error_handle ((-1), file, line, error, MSG_ERR_CHDIR,
|
---|
558 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
559 | path);
|
---|
560 | }
|
---|
561 | errno = error;
|
---|
562 | SL_RETURN(val_return, _("retry_aud_chdir"));
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | long int aud_open_noatime (const char * file, int line, int privs,
|
---|
567 | const char *pathname, int flags, mode_t mode,
|
---|
568 | int * o_noatime)
|
---|
569 | {
|
---|
570 | long int val_return;
|
---|
571 | int error;
|
---|
572 | char errbuf[SH_ERRBUF_SIZE];
|
---|
573 |
|
---|
574 | SL_ENTER(_("aud_open"));
|
---|
575 |
|
---|
576 | val_return = open (pathname, *o_noatime|flags, mode);
|
---|
577 | if ((val_return < 0) && (*o_noatime != 0))
|
---|
578 | {
|
---|
579 | val_return = open (pathname, flags, mode);
|
---|
580 | if (val_return >= 0)
|
---|
581 | *o_noatime = 0;
|
---|
582 | }
|
---|
583 | error = errno;
|
---|
584 | /*@-noeffect@*/
|
---|
585 | (void) privs; /* fix compiler warning */
|
---|
586 | /*@+noeffect@*/
|
---|
587 |
|
---|
588 | if (val_return < 0)
|
---|
589 | {
|
---|
590 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
|
---|
594 | {
|
---|
595 | sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
|
---|
596 | pathname, (long) flags, (long) mode, val_return);
|
---|
597 | }
|
---|
598 | if (val_return < 0) {
|
---|
599 | sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
|
---|
600 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
601 | pathname, (long) flags, (long) mode, val_return);
|
---|
602 | }
|
---|
603 | errno = error;
|
---|
604 | SL_RETURN(val_return, _("aud_open"));
|
---|
605 | }
|
---|
606 |
|
---|
607 | long int aud_open (const char * file, int line, int privs,
|
---|
608 | const char *pathname, int flags, mode_t mode)
|
---|
609 | {
|
---|
610 | long int val_return;
|
---|
611 | int error;
|
---|
612 | char errbuf[SH_ERRBUF_SIZE];
|
---|
613 |
|
---|
614 | SL_ENTER(_("aud_open"));
|
---|
615 |
|
---|
616 | val_return = open (pathname, flags, mode);
|
---|
617 | error = errno;
|
---|
618 | /*@-noeffect@*/
|
---|
619 | (void) privs; /* fix compiler warning */
|
---|
620 | /*@+noeffect@*/
|
---|
621 |
|
---|
622 | if (val_return < 0)
|
---|
623 | {
|
---|
624 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
625 | }
|
---|
626 |
|
---|
627 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
|
---|
628 | {
|
---|
629 | sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
|
---|
630 | pathname, (long) flags, (long) mode, val_return);
|
---|
631 | }
|
---|
632 | if (val_return < 0) {
|
---|
633 | sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
|
---|
634 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
635 | pathname, (long) flags, (long) mode, val_return);
|
---|
636 | }
|
---|
637 | errno = error;
|
---|
638 | SL_RETURN(val_return, _("aud_open"));
|
---|
639 | }
|
---|
640 |
|
---|
641 | long int aud_kill (const char * file, int line, pid_t pid, int sig)
|
---|
642 | {
|
---|
643 | int myerror;
|
---|
644 | long int val_return = kill (pid, sig);
|
---|
645 | char errbuf[SH_ERRBUF_SIZE];
|
---|
646 | myerror = errno;
|
---|
647 |
|
---|
648 | SL_ENTER(_("aud_kill"));
|
---|
649 |
|
---|
650 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_KILL) != 0)
|
---|
651 | sh_error_handle ((-1), file, line, 0, MSG_AUD_KILL,
|
---|
652 | (long) pid, (long) sig);
|
---|
653 | if (val_return < 0) {
|
---|
654 | sh_error_handle ((-1), file, line, myerror, MSG_ERR_KILL,
|
---|
655 | sh_error_message(myerror, errbuf, sizeof(errbuf)),
|
---|
656 | (long) pid, (long) sig);
|
---|
657 | }
|
---|
658 | errno = myerror;
|
---|
659 | SL_RETURN(val_return, _("aud_kill"));
|
---|
660 | }
|
---|
661 |
|
---|
662 | /*@noreturn@*/
|
---|
663 | void aud_exit (const char * file, int line, int fd)
|
---|
664 | {
|
---|
665 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
|
---|
666 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
|
---|
667 | (long) fd);
|
---|
668 |
|
---|
669 | SL_ENTER(_("aud_exit"));
|
---|
670 |
|
---|
671 | sh.flag.exit = fd;
|
---|
672 | exit(fd);
|
---|
673 | }
|
---|
674 |
|
---|
675 | /*@noreturn@*/
|
---|
676 | void aud__exit (const char * file, int line, int fd)
|
---|
677 | {
|
---|
678 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
|
---|
679 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
|
---|
680 | (long) fd);
|
---|
681 |
|
---|
682 | SL_ENTER(_("aud__exit"));
|
---|
683 |
|
---|
684 | sh.flag.exit = fd;
|
---|
685 | _exit(fd);
|
---|
686 | }
|
---|
687 |
|
---|
688 | pid_t aud_fork (const char * file, int line)
|
---|
689 | {
|
---|
690 | int error;
|
---|
691 | pid_t i = fork();
|
---|
692 | char errbuf[SH_ERRBUF_SIZE];
|
---|
693 |
|
---|
694 | error = errno;
|
---|
695 | SL_ENTER(_("aud_fork"));
|
---|
696 |
|
---|
697 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_FORK) != 0 && (i > 0))
|
---|
698 | sh_error_handle ((-1), file, line, 0, MSG_AUD_FORK,
|
---|
699 | (long) i);
|
---|
700 | if (i == (pid_t) -1) {
|
---|
701 | sh_error_handle ((-1), file, line, error, MSG_ERR_FORK,
|
---|
702 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
703 | (long) i);
|
---|
704 | }
|
---|
705 | errno = error;
|
---|
706 | SL_RETURN(i, _("aud_fork"));
|
---|
707 | }
|
---|
708 |
|
---|
709 | int aud_setuid (const char * file, int line, uid_t uid)
|
---|
710 | {
|
---|
711 | int error = 0;
|
---|
712 | int i = 0;
|
---|
713 | char errbuf[SH_ERRBUF_SIZE];
|
---|
714 |
|
---|
715 | SL_ENTER(_("aud_setuid"));
|
---|
716 |
|
---|
717 | if (uid != (uid_t) 0) {
|
---|
718 | i = setuid(uid);
|
---|
719 | error = errno;
|
---|
720 | }
|
---|
721 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETUID) != 0)
|
---|
722 | sh_error_handle ((-1), file, line, 0, MSG_AUD_SETUID,
|
---|
723 | (long) uid);
|
---|
724 | if (uid == (uid_t) 0) {
|
---|
725 | i = setuid(uid);
|
---|
726 | error = errno;
|
---|
727 | }
|
---|
728 | if (i < 0) {
|
---|
729 | sh_error_handle ((-1), file, line, error, MSG_ERR_SETUID,
|
---|
730 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
731 | (long) uid);
|
---|
732 | }
|
---|
733 | errno = error;
|
---|
734 | SL_RETURN(i, _("aud_setuid"));
|
---|
735 | }
|
---|
736 |
|
---|
737 | int aud_setgid (const char * file, int line, gid_t gid)
|
---|
738 | {
|
---|
739 | int error = 0;
|
---|
740 | int i = 0;
|
---|
741 | char errbuf[SH_ERRBUF_SIZE];
|
---|
742 |
|
---|
743 | SL_ENTER(_("aud_setgid"));
|
---|
744 |
|
---|
745 | if (gid != (gid_t) 0) {
|
---|
746 | i = setgid(gid);
|
---|
747 | error = errno;
|
---|
748 | }
|
---|
749 |
|
---|
750 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETGID) != 0)
|
---|
751 | sh_error_handle ((-1), file, line, 0, MSG_AUD_SETGID,
|
---|
752 | (long) gid);
|
---|
753 | if (gid == (gid_t) 0) {
|
---|
754 | i = setgid(gid);
|
---|
755 | error = errno;
|
---|
756 | }
|
---|
757 | if (i < 0) {
|
---|
758 | sh_error_handle ((-1), file, line, error, MSG_ERR_SETGID,
|
---|
759 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
760 | (long) gid);
|
---|
761 | }
|
---|
762 | errno = error;
|
---|
763 | SL_RETURN(i, _("aud_setgid"));
|
---|
764 | }
|
---|
765 |
|
---|
766 | int aud_pipe (const char * file, int line, int * modus)
|
---|
767 | {
|
---|
768 | int error;
|
---|
769 | int i = pipe (modus);
|
---|
770 | char errbuf[SH_ERRBUF_SIZE];
|
---|
771 |
|
---|
772 | SL_ENTER(_("aud_pipe"));
|
---|
773 |
|
---|
774 | error = errno;
|
---|
775 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_PIPE) != 0)
|
---|
776 | {
|
---|
777 | if (i < 0)
|
---|
778 | sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
|
---|
779 | (long) 0, (long) 0);
|
---|
780 | else
|
---|
781 | sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
|
---|
782 | (long) modus[0], (long) modus[1]);
|
---|
783 | }
|
---|
784 | if (i < 0) {
|
---|
785 | if (i < 0)
|
---|
786 | sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
|
---|
787 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
788 | (long) 0, (long) 0);
|
---|
789 | else
|
---|
790 | sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
|
---|
791 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
792 | (long) modus[0], (long) modus[1]);
|
---|
793 | }
|
---|
794 | SL_RETURN(i, _("aud_pipe"));
|
---|
795 | }
|
---|