source: trunk/src/sh_calls.c@ 515

Last change on this file since 515 was 509, checked in by katerina, 8 years ago

Fix for ticket #402 (memory leak in server).

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