source: trunk/src/sh_calls.c@ 560

Last change on this file since 560 was 550, checked in by katerina, 5 years ago

Fix for ticket #442 (support for OpenBSD signify).

File size: 23.6 KB
Line 
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 <netdb.h>
37#include <netinet/in.h>
38
39#ifndef S_SPLINT_S
40#include <arpa/inet.h>
41#else
42#define AF_INET 2
43#endif
44
45#include <time.h>
46
47#ifndef HAVE_LSTAT
48#define lstat stat
49#endif
50
51#include "samhain.h"
52#include "sh_error.h"
53#include "sh_ipvx.h"
54#include "sh_sub.h"
55#include "sh_utils.h"
56
57#undef FIL__
58#define FIL__ _("sh_calls.c")
59
60extern int flag_err_debug;
61
62char aud_err_message[64];
63
64typedef struct cht_struct
65{
66 const char * str;
67 unsigned long val;
68} cht_type;
69
70static cht_type aud_tab[] =
71{
72 { N_("execve"), AUD_EXEC },
73 { N_("utime"), AUD_UTIME },
74 { N_("unlink"), AUD_UNLINK },
75 { N_("dup"), AUD_DUP },
76 { N_("chdir"), AUD_CHDIR },
77 { N_("open"), AUD_OPEN },
78 { N_("kill"), AUD_KILL },
79 { N_("exit"), AUD_EXIT },
80 { N_("fork"), AUD_FORK },
81 { N_("setuid"), AUD_SETUID },
82 { N_("setgid"), AUD_SETGID },
83 { N_("pipe"), AUD_PIPE },
84 { NULL, 0 }
85};
86
87/* Set aud functions
88 */
89int sh_aud_set_functions(const char * str_s)
90{
91 int i = 0;
92
93 SL_ENTER(_("sh_aud_set_functions"));
94
95 if (str_s == NULL)
96 return -1;
97
98 while (aud_tab[i].str != NULL)
99 {
100 if (NULL != sl_strstr (str_s, _(aud_tab[i].str)))
101 {
102 sh.flag.audit = 1;
103 sh.flag.aud_mask |= aud_tab[i].val;
104 }
105 ++i;
106 }
107
108 SL_RETURN(0,_("sh_aud_set_functions"));
109}
110
111
112
113
114/* Need to catch EINTR for these functions.
115 */
116long int retry_sigaction(const char * file, int line,
117 int signum, const struct sigaction *act,
118 struct sigaction *oldact)
119{
120 int error;
121 long int val_retry = -1;
122 char errbuf[SH_ERRBUF_SIZE];
123 errno = 0;
124
125 SL_ENTER(_("retry_sigaction"));
126
127 do {
128 val_retry = sigaction(signum, act, oldact);
129 } while (val_retry < 0 && errno == EINTR);
130
131 error = errno;
132 if (val_retry < 0) {
133 sh_error_handle ((-1), file, line, error, MSG_ERR_SIGACT,
134 sh_error_message(error, errbuf, sizeof(errbuf)),
135 (long) signum );
136 }
137 errno = error;
138 SL_RETURN(val_retry, _("retry_sigaction"));
139}
140
141static struct sh_sockaddr bind_addr;
142static int use_bind_addr = 0;
143
144int sh_calls_set_bind_addr (const char * str)
145{
146 static int reject = 0;
147
148 if (reject == 1)
149 return (0);
150
151 if (sh.flag.opts == S_TRUE)
152 reject = 1;
153
154 if (0 == sh_ipvx_aton(str, &bind_addr))
155 return -1;
156
157 use_bind_addr = 1;
158 return 0;
159}
160
161
162long int retry_connect(const char * file, int line, int sockfd,
163 struct sockaddr *serv_addr, int addrlen)
164{
165 int error;
166 int err_bind = 0;
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 (use_bind_addr)
175 {
176 int slen = SH_SS_LEN(bind_addr);
177 struct sockaddr *b_addr = sh_ipvx_sockaddr_cast(&bind_addr);
178
179 if (bind_addr.ss_family == AF_INET)
180 b_addr->sa_family = AF_INET;
181#if defined(USE_IPVX)
182 else
183 b_addr->sa_family = AF_INET6;
184#endif
185 val_retry = bind(sockfd, b_addr, slen);
186 }
187
188 if (val_retry == 0)
189 {
190 do {
191 val_retry = connect(sockfd, serv_addr, addrlen);
192 } while (val_retry < 0 && (errno == EINTR || errno == EINPROGRESS));
193 }
194 else
195 {
196 err_bind = 1;
197 }
198
199 error = errno;
200 if (val_retry != 0) {
201 long eport = 0;
202 char eaddr[SH_IP_BUF];
203 char emesg[SH_BUFSIZE];
204 struct sockaddr *err_addr = serv_addr;
205 struct sh_sockaddr ss;
206
207 if (err_bind)
208 err_addr = sh_ipvx_sockaddr_cast(&bind_addr);
209
210 sh_ipvx_save(&ss, err_addr->sa_family, err_addr);
211 sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
212
213 if (err_addr->sa_family == AF_INET)
214 eport = (long) ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
215#if defined(USE_IPVX)
216 else
217 eport = (long) ntohs(((struct sockaddr_in6 *)serv_addr)->sin6_port);
218#endif
219
220 sl_strlcpy(emesg, sh_error_message(error, errbuf, sizeof(errbuf)), sizeof(emesg));
221 sl_strlcat(emesg,
222 (err_addr->sa_family == AF_INET) ? _(", AF_INET") : _(", AF_INET6"),
223 sizeof(emesg));
224
225 sl_strlcat(emesg,
226 (err_bind) ? _(", bind") : _(", connect"),
227 sizeof(emesg));
228
229 sh_error_handle ((-1), file, line, error, MSG_ERR_CONNECT,
230 emesg,
231 (long) sockfd, eport, eaddr);
232 }
233 errno = error;
234 SL_RETURN(val_retry, _("retry_connect"));
235}
236
237long int retry_accept(const char * file, int line, int fd,
238 struct sh_sockaddr *serv_addr, int * addrlen)
239{
240 int error;
241 long int val_retry = -1;
242 char errbuf[SH_ERRBUF_SIZE];
243 struct sockaddr_storage ss;
244
245 ACCEPT_TYPE_ARG3 my_addrlen = sizeof(ss);
246
247 errno = 0;
248
249 SL_ENTER(_("retry_accept"));
250
251 do {
252 val_retry = accept(fd, (struct sockaddr *)&ss, &my_addrlen);
253 } while (val_retry < 0 && errno == EINTR);
254
255 error = errno;
256 if (val_retry < 0) {
257 sh_error_handle ((-1), file, line, error, MSG_ERR_ACCEPT,
258 sh_error_message(error, errbuf, sizeof(errbuf)),
259 (long) fd );
260 }
261 errno = error;
262
263 if (flag_err_debug == S_TRUE)
264 {
265 char ipbuf[SH_IP_BUF];
266 char buf[SH_BUFSIZE];
267#if defined(USE_IPVX)
268 sl_strlcpy(errbuf, _("Address family: "), sizeof(errbuf));
269 sl_strlcat(errbuf,
270 (ss.ss_family == AF_INET6) ? _("AF_INET6") : _("AF_INET"),
271 sizeof(errbuf));
272 getnameinfo((struct sockaddr *)&ss, my_addrlen,
273 ipbuf, sizeof(ipbuf), NULL, 0, NI_NUMERICHOST);
274#else
275 struct sockaddr_in sa;
276 char * p;
277 memcpy(&(sa), (struct sockaddr_in*)&ss, sizeof(struct sockaddr_in));
278 p = inet_ntoa(sa.sin_addr);
279 sl_strlcpy(ipbuf, p, sizeof(ipbuf));
280 sl_strlcpy(errbuf, _("Address family: AF_INET"), sizeof(errbuf));
281#endif
282 sl_strlcpy(buf, _("Address: "), sizeof(buf));
283 sl_strlcat(buf, ipbuf, sizeof(buf));
284 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
285 errbuf, _("retry_accept"));
286 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
287 buf, _("retry_accept"));
288 }
289
290 sh_ipvx_save(serv_addr, ss.ss_family, (struct sockaddr *) &ss);
291
292 if (flag_err_debug == S_TRUE)
293 {
294 char ipbuf[SH_IP_BUF];
295 char ipbuf2[SH_IP_BUF];
296 char buf[SH_BUFSIZE];
297#if defined(USE_IPVX)
298 int len = (serv_addr->ss_family == AF_INET) ?
299 sizeof(struct sockaddr_in) :
300 sizeof(struct sockaddr_in6);
301 getnameinfo(sh_ipvx_sockaddr_cast(serv_addr), len,
302 ipbuf2, sizeof(ipbuf2), NULL, 0, NI_NUMERICHOST);
303#else
304 char * p = inet_ntoa((serv_addr->sin).sin_addr);
305 sl_strlcpy(ipbuf2, p, sizeof(ipbuf2));
306#endif
307 sh_ipvx_ntoa (ipbuf, sizeof(ipbuf), serv_addr);
308 sl_snprintf(buf, sizeof(buf), _("Address: %s / %s"),
309 ipbuf, ipbuf2);
310 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
311 buf, _("retry_accept"));
312 }
313
314 *addrlen = (int) my_addrlen;
315 SL_RETURN(val_retry, _("retry_accept"));
316}
317
318static int sh_enable_use_sub = 0;
319
320#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
321#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
322static int sh_use_sub = 0;
323#else
324static int sh_use_sub = 1;
325#endif
326#else
327static int sh_use_sub = 0;
328#endif
329
330void sh_calls_enable_sub()
331{
332 sh_enable_use_sub = 1;
333 return;
334}
335
336int sh_calls_set_sub (const char * str)
337{
338 int ret = sh_util_flagval(str, &sh_use_sub);
339
340 if ((ret == 0) && (!sh_use_sub))
341 {
342 sh_kill_sub();
343 }
344 return ret;
345}
346
347long int retry_lstat_ns(const char * file, int line,
348 const char *file_name, struct stat *buf)
349{
350 int error;
351 long int val_retry = -1;
352 char errbuf[SH_ERRBUF_SIZE];
353
354 SL_ENTER(_("retry_lstat_ns"));
355
356 do {
357 val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
358 } while (val_retry < 0 && errno == EINTR);
359
360 error = errno;
361 if (val_retry < 0) {
362 (void) sh_error_message(error, aud_err_message, 64);
363 sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT,
364 sh_error_message(error, errbuf, sizeof(errbuf)),
365 file_name );
366 }
367 errno = error;
368
369 SL_RETURN(val_retry, _("retry_lstat_ns"));
370}
371
372long int retry_lstat(const char * file, int line,
373 const char *file_name, struct stat *buf)
374{
375 int error;
376 long int val_retry = -1;
377 char errbuf[SH_ERRBUF_SIZE];
378
379 SL_ENTER(_("retry_lstat"));
380
381 if (sh_use_sub && sh_enable_use_sub)
382 {
383 val_retry = sh_sub_lstat (file_name, buf);
384 }
385 else
386 {
387 do {
388 val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
389 } while (val_retry < 0 && errno == EINTR);
390 }
391
392 error = errno;
393 if (val_retry < 0) {
394 (void) sh_error_message(error, aud_err_message, 64);
395 sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT,
396 sh_error_message(error, errbuf, sizeof(errbuf)),
397 file_name );
398 }
399 errno = error;
400
401 SL_RETURN(val_retry, _("retry_lstat"));
402}
403
404long int retry_stat(const char * file, int line,
405 const char *file_name, struct stat *buf)
406{
407 int error;
408 long int val_retry = -1;
409 char errbuf[SH_ERRBUF_SIZE];
410
411 SL_ENTER(_("retry_stat"));
412
413 if (sh_use_sub && sh_enable_use_sub)
414 {
415 val_retry = sh_sub_stat (file_name, buf);
416 }
417 else
418 {
419 do {
420 val_retry = stat (file_name, buf);
421 } while (val_retry < 0 && errno == EINTR);
422 }
423
424 error = errno;
425 if (val_retry < 0) {
426 (void) sh_error_message(error, aud_err_message, 64);
427 sh_error_handle ((-1), file, line, error, MSG_ERR_STAT,
428 sh_error_message(error, errbuf, sizeof(errbuf)),
429 file_name );
430 }
431 errno = error;
432
433 SL_RETURN(val_retry, _("retry_stat"));
434}
435
436long int retry_fstat(const char * file, int line, int filed, struct stat *buf)
437{
438 int error;
439 long int val_retry = -1;
440 char errbuf[SH_ERRBUF_SIZE];
441
442 SL_ENTER(_("retry_fstat"));
443
444 do {
445 val_retry = fstat (filed, buf);
446 } while (val_retry < 0 && errno == EINTR);
447 error = errno;
448 if (val_retry < 0) {
449 sh_error_handle ((-1), file, line, error, MSG_ERR_FSTAT,
450 sh_error_message(error, errbuf, sizeof(errbuf)),
451 (long) filed );
452 }
453 errno = error;
454 SL_RETURN(val_retry, _("retry_fstat"));
455}
456
457long int retry_fcntl(const char * file, int line, int fd, int cmd, long arg)
458{
459 int error;
460 long int val_retry = -1;
461 char errbuf[SH_ERRBUF_SIZE];
462 errno = 0;
463
464 SL_ENTER(_("retry_fcntl"));
465
466 if (cmd == F_GETFD || cmd == F_GETFL)
467 {
468 do {
469 val_retry = fcntl(fd, cmd);
470 } while (val_retry < 0 && errno == EINTR);
471 }
472 else
473 {
474 do {
475 val_retry = fcntl(fd, cmd, arg);
476 } while (val_retry < 0 && errno == EINTR);
477 }
478 error = errno;
479 if (val_retry < 0) {
480 sh_error_handle ((-1), file, line, error, MSG_ERR_FCNTL,
481 sh_error_message(error, errbuf, sizeof(errbuf)),
482 (long) fd, (long) cmd, arg );
483 }
484 errno = error;
485 SL_RETURN(val_retry, _("retry_fcntl"));
486}
487
488long int retry_msleep (int sec, int millisec)
489{
490 int result = 0;
491#if defined(HAVE_NANOSLEEP)
492 struct timespec req, rem;
493#endif
494
495 errno = 0;
496 if (millisec > 999) millisec = 999;
497 if (millisec < 0) millisec = 0;
498 if (sec < 0) sec = 0;
499
500#if defined(HAVE_NANOSLEEP)
501 /*@-usedef@*/
502 req.tv_sec = sec; rem.tv_sec = 0;
503 req.tv_nsec = millisec * 1000000; rem.tv_nsec = 0;
504 /*@+usedef@*/
505 do {
506 result = /*@-unrecog@*/nanosleep(&req, &rem)/*@+unrecog@*/;
507
508 req.tv_sec = rem.tv_sec; rem.tv_sec = 0;
509 req.tv_nsec = rem.tv_nsec; rem.tv_nsec = 0;
510
511 } while ((result == -1) && (errno == EINTR));
512#else
513 if (sec > 0)
514 sleep (sec); /* nanosleep not available */
515 else {
516#ifdef HAVE_USLEEP
517 if (millisec > 0)
518 usleep(1000 * millisec);
519#else
520 if (millisec > 0)
521 sleep (1);
522#endif
523 }
524#endif
525 return result;
526}
527
528/***************************************************
529 *
530 * Audit these functions.
531 *
532 ***************************************************/
533
534long int retry_aud_execve (const char * file, int line,
535 const char *dateiname, char *const argv[],
536 char *const envp[])
537{
538 uid_t a = geteuid();
539 gid_t b = getegid();
540 int i;
541 int error;
542 char errbuf[SH_ERRBUF_SIZE];
543
544 SL_ENTER(_("retry_aud_execve"));
545
546 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXEC) != 0)
547 sh_error_handle ((-1), file, line, 0, MSG_AUD_EXEC,
548 dateiname, (long) a, (long) b );
549 do {
550 i = execve(dateiname, argv, envp);
551 } while (i < 0 && errno == EINTR);
552
553 error = errno;
554 if (i < 0) {
555 sh_error_handle ((-1), file, line, error, MSG_ERR_EXEC,
556 sh_error_message(error, errbuf, sizeof(errbuf)),
557 dateiname, (long) a, (long) b );
558 }
559 errno = error;
560 SL_RETURN(i, _("retry_aud_execve"));
561}
562
563
564long int retry_aud_utime (const char * file, int line,
565 char * path, struct utimbuf *buf)
566{
567 long int val_return;
568 int error;
569 char errbuf[SH_ERRBUF_SIZE];
570 errno = 0;
571
572 SL_ENTER(_("retry_aud_utime"));
573
574 do {
575 val_return = utime (path, buf);
576 } while (val_return < 0 && errno == EINTR);
577
578 error = errno;
579 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UTIME) != 0)
580 sh_error_handle ((-1), file, line, 0, MSG_AUD_UTIME,
581 path,
582 (unsigned long) buf->actime,
583 (unsigned long) buf->modtime);
584 if (val_return < 0) {
585 sh_error_handle ((-1), file, line, error, MSG_ERR_UTIME,
586 sh_error_message(error, errbuf, sizeof(errbuf)),
587 path,
588 (unsigned long) buf->actime,
589 (unsigned long) buf->modtime);
590 }
591 errno = error;
592 SL_RETURN(val_return, _("retry_aud_utime"));
593}
594
595long int retry_aud_unlink (const char * file, int line,
596 char * path)
597{
598 long int val_return;
599 int error;
600 char errbuf[SH_ERRBUF_SIZE];
601 errno = 0;
602
603 SL_ENTER(_("retry_aud_unlink"));
604
605 do {
606 val_return = unlink (path);
607 } while (val_return < 0 && errno == EINTR);
608
609 error = errno;
610 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UNLINK) != 0)
611 sh_error_handle ((-1), file, line, 0, MSG_AUD_UNLINK,
612 path);
613 if (val_return < 0) {
614 sh_error_handle ((-1), file, line, error, MSG_ERR_UNLINK,
615 sh_error_message(error, errbuf, sizeof(errbuf)),
616 path);
617 }
618 errno = error;
619 SL_RETURN(val_return, _("retry_aud_unlink"));
620}
621
622long int retry_aud_dup2 (const char * file, int line,
623 int fd, int fd2)
624{
625 long int val_return;
626 int error;
627 char errbuf[SH_ERRBUF_SIZE];
628 errno = 0;
629
630 SL_ENTER(_("retry_aud_dup2"));
631
632 do {
633 val_return = dup2 (fd, fd2);
634 } while (val_return < 0 && errno == EINTR);
635
636 error = errno;
637 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
638 sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
639 (long) fd, val_return);
640 if (val_return < 0) {
641 sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
642 sh_error_message(error, errbuf, sizeof(errbuf)),
643 (long) fd, val_return);
644 }
645 errno = error;
646 SL_RETURN(val_return, _("retry_aud_dup2"));
647}
648
649long int retry_aud_dup (const char * file, int line,
650 int fd)
651{
652 long int val_return;
653 int error;
654 char errbuf[SH_ERRBUF_SIZE];
655 errno = 0;
656
657 SL_ENTER(_("retry_aud_dup"));
658
659 do {
660 val_return = dup (fd);
661 } while (val_return < 0 && errno == EINTR);
662 error = errno;
663 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
664 sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
665 (long) fd, val_return);
666 if (val_return < 0) {
667 sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
668 sh_error_message(error, errbuf, sizeof(errbuf)),
669 (long) fd, val_return);
670 }
671 errno = error;
672 SL_RETURN(val_return, _("retry_aud_dup"));
673}
674
675
676
677long int retry_aud_chdir (const char * file, int line,
678 const char *path)
679{
680 long int val_return;
681 int error = 0;
682 char errbuf[SH_ERRBUF_SIZE];
683 errno = 0;
684
685 SL_ENTER(_("retry_aud_chdir"));
686
687 do {
688 val_return = chdir (path);
689 } while (val_return < 0 && errno == EINTR);
690
691 error = errno;
692 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_CHDIR) != 0)
693 sh_error_handle ((-1), file, line, 0, MSG_AUD_CHDIR,
694 path);
695 if (val_return < 0) {
696 sh_error_handle ((-1), file, line, error, MSG_ERR_CHDIR,
697 sh_error_message(error, errbuf, sizeof(errbuf)),
698 path);
699 }
700 errno = error;
701 SL_RETURN(val_return, _("retry_aud_chdir"));
702}
703
704
705long int aud_open_noatime (const char * file, int line, int privs,
706 const char *pathname, int flags, mode_t mode,
707 int * o_noatime)
708{
709 long int val_return;
710 int error;
711 char errbuf[SH_ERRBUF_SIZE];
712
713 SL_ENTER(_("aud_open"));
714
715#ifdef USE_SUID
716 if (0 == strcmp(pathname, "/usr/bin/sudo"))
717 {
718 uid_t ruid; uid_t euid; uid_t suid;
719 getresuid(&ruid, &euid, &suid);
720 }
721 if (privs == SL_YESPRIV)
722 sl_set_suid();
723#else
724 /*@-noeffect@*/
725 (void) privs; /* fix compiler warning */
726 /*@+noeffect@*/
727#endif
728
729 val_return = open (pathname, *o_noatime|flags, mode);
730
731#ifdef USE_SUID
732 if (privs == SL_YESPRIV)
733 sl_unset_suid();
734#endif
735
736 if ((val_return < 0) && (*o_noatime != 0))
737 {
738 /* cppcheck-suppress resourceLeak */
739 val_return = open (pathname, flags, mode);
740 if (val_return >= 0)
741 *o_noatime = 0;
742 }
743 error = errno;
744
745 if (val_return < 0)
746 {
747 (void) sh_error_message(error, aud_err_message, 64);
748 }
749
750 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
751 {
752 sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
753 pathname, (long) flags, (long) mode, val_return);
754 }
755 if (val_return < 0) {
756 sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
757 sh_error_message(error, errbuf, sizeof(errbuf)),
758 pathname, (long) flags, (long) mode, val_return);
759 }
760 errno = error;
761 SL_RETURN(val_return, _("aud_open"));
762}
763
764long int aud_open (const char * file, int line, int privs,
765 const char *pathname, int flags, mode_t mode)
766{
767 long int val_return;
768 int error;
769 char errbuf[SH_ERRBUF_SIZE];
770
771 SL_ENTER(_("aud_open"));
772
773#ifdef USE_SUID
774 if (privs == SL_YESPRIV)
775 sl_set_suid();
776#else
777 /*@-noeffect@*/
778 (void) privs; /* fix compiler warning */
779 /*@+noeffect@*/
780#endif
781
782 val_return = open (pathname, flags, mode);
783
784#ifdef USE_SUID
785 if (privs == SL_YESPRIV)
786 sl_unset_suid();
787#endif
788
789 error = errno;
790
791 if (val_return < 0)
792 {
793 (void) sh_error_message(error, aud_err_message, 64);
794 }
795
796 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
797 {
798 sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
799 pathname, (long) flags, (long) mode, val_return);
800 }
801 if (val_return < 0) {
802 sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
803 sh_error_message(error, errbuf, sizeof(errbuf)),
804 pathname, (long) flags, (long) mode, val_return);
805 }
806 errno = error;
807 SL_RETURN(val_return, _("aud_open"));
808}
809
810long int aud_kill (const char * file, int line, pid_t pid, int sig)
811{
812 int myerror;
813 long int val_return = kill (pid, sig);
814 char errbuf[SH_ERRBUF_SIZE];
815 myerror = errno;
816
817 SL_ENTER(_("aud_kill"));
818
819 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_KILL) != 0)
820 sh_error_handle ((-1), file, line, 0, MSG_AUD_KILL,
821 (long) pid, (long) sig);
822 if (val_return < 0) {
823 sh_error_handle ((-1), file, line, myerror, MSG_ERR_KILL,
824 sh_error_message(myerror, errbuf, sizeof(errbuf)),
825 (long) pid, (long) sig);
826 }
827 errno = myerror;
828 SL_RETURN(val_return, _("aud_kill"));
829}
830
831/*@noreturn@*/
832void aud_exit (const char * file, int line, int exitval)
833{
834 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
835 sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
836 (long) exitval);
837
838 SL_ENTER(_("aud_exit"));
839
840 sh.flag.exit = exitval;
841 exit(exitval);
842}
843
844/*@noreturn@*/
845void aud__exit (const char * file, int line, int exitval)
846{
847 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
848 sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
849 (long) exitval);
850
851 SL_ENTER(_("aud__exit"));
852
853 sh.flag.exit = exitval;
854 _exit(exitval);
855}
856
857pid_t aud_fork (const char * file, int line)
858{
859 int error;
860 pid_t i = fork();
861 char errbuf[SH_ERRBUF_SIZE];
862
863 error = errno;
864 SL_ENTER(_("aud_fork"));
865
866 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_FORK) != 0 && (i > 0))
867 sh_error_handle ((-1), file, line, 0, MSG_AUD_FORK,
868 (long) i);
869 if (i == (pid_t) -1) {
870 sh_error_handle ((-1), file, line, error, MSG_ERR_FORK,
871 sh_error_message(error, errbuf, sizeof(errbuf)),
872 (long) i);
873 }
874 errno = error;
875 SL_RETURN(i, _("aud_fork"));
876}
877
878int aud_setuid (const char * file, int line, uid_t uid)
879{
880 int error = 0;
881 int i = 0;
882 char errbuf[SH_ERRBUF_SIZE];
883
884 SL_ENTER(_("aud_setuid"));
885
886 if (uid != (uid_t) 0) {
887 i = setuid(uid);
888 error = errno;
889 }
890 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETUID) != 0)
891 sh_error_handle ((-1), file, line, 0, MSG_AUD_SETUID,
892 (long) uid);
893 if (uid == (uid_t) 0) {
894 i = setuid(uid);
895 error = errno;
896 }
897 if (i < 0) {
898 sh_error_handle ((-1), file, line, error, MSG_ERR_SETUID,
899 sh_error_message(error, errbuf, sizeof(errbuf)),
900 (long) uid);
901 }
902 errno = error;
903 SL_RETURN(i, _("aud_setuid"));
904}
905
906int aud_setgid (const char * file, int line, gid_t gid)
907{
908 int error = 0;
909 int i = 0;
910 char errbuf[SH_ERRBUF_SIZE];
911
912 SL_ENTER(_("aud_setgid"));
913
914 if (gid != (gid_t) 0) {
915 i = setgid(gid);
916 error = errno;
917 }
918
919 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETGID) != 0)
920 sh_error_handle ((-1), file, line, 0, MSG_AUD_SETGID,
921 (long) gid);
922 if (gid == (gid_t) 0) {
923 i = setgid(gid);
924 error = errno;
925 }
926 if (i < 0) {
927 sh_error_handle ((-1), file, line, error, MSG_ERR_SETGID,
928 sh_error_message(error, errbuf, sizeof(errbuf)),
929 (long) gid);
930 }
931 errno = error;
932 SL_RETURN(i, _("aud_setgid"));
933}
934
935int aud_pipe (const char * file, int line, int * modus)
936{
937 int error;
938 int i = pipe (modus);
939 char errbuf[SH_ERRBUF_SIZE];
940
941 SL_ENTER(_("aud_pipe"));
942
943 error = errno;
944 if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_PIPE) != 0)
945 {
946 if (i < 0)
947 sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
948 (long) 0, (long) 0);
949 else
950 sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
951 (long) modus[0], (long) modus[1]);
952 }
953 if (i < 0) {
954 if (i < 0)
955 sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
956 sh_error_message(error, errbuf, sizeof(errbuf)),
957 (long) 0, (long) 0);
958 else
959 sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
960 sh_error_message(error, errbuf, sizeof(errbuf)),
961 (long) modus[0], (long) modus[1]);
962 }
963 SL_RETURN(i, _("aud_pipe"));
964}
Note: See TracBrowser for help on using the repository browser.