source: trunk/src/sh_calls.c@ 1

Last change on this file since 1 was 1, checked in by katerina, 19 years ago

Initial import

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