source: trunk/include/sh_unix.h@ 550

Last change on this file since 550 was 481, checked in by katerina, 9 years ago

Enhancements and fixes for tickets #374, #375, #376, #377, #378, and #379.

File size: 11.3 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
21
22#ifndef SH_UNIX_H
23#define SH_UNIX_H
24
25/* For PATH_MAX */
26#include <limits.h>
27#if !defined(PATH_MAX)
28#define PATH_MAX 1024
29#endif
30
31#include <unistd.h>
32#include "samhain.h"
33#include "sh_error.h"
34
35
36typedef enum {
37 SH_ISLOG,
38 SH_ISFILE,
39 SH_ISDIR,
40 SH_ISDATA
41} ShOpenType;
42
43typedef enum {
44 SH_DATA_RAW,
45 SH_DATA_LINE
46} ShDataType;
47
48typedef enum {
49 SH_FILE_REGULAR,
50 SH_FILE_SYMLINK,
51 SH_FILE_DIRECTORY,
52 SH_FILE_CDEV,
53 SH_FILE_BDEV,
54 SH_FILE_FIFO,
55 SH_FILE_SOCKET,
56 SH_FILE_DOOR,
57 SH_FILE_PORT,
58 SH_FILE_UNKNOWN
59} ShFileType;
60
61/* -- Attributes to check. --
62 */
63
64/* checksum */
65#define MODI_CHK (1 << 0)
66/* link */
67#define MODI_LNK (1 << 1)
68/* inode */
69#define MODI_INO (1 << 2)
70
71/* user */
72#define MODI_USR (1 << 3)
73/* group */
74#define MODI_GRP (1 << 4)
75/* mtime */
76#define MODI_MTM (1 << 5)
77
78/* ctime */
79#define MODI_CTM (1 << 6)
80/* atime */
81#define MODI_ATM (1 << 7)
82/* size */
83#define MODI_SIZ (1 << 8)
84
85/* file mode */
86#define MODI_MOD (1 << 9)
87/* hardlinks */
88#define MODI_HLN (1 << 10)
89/* device type */
90#define MODI_RDEV (1 << 11)
91
92/* size may grow */
93#define MODI_SGROW (1 << 12)
94/* use prelink */
95#define MODI_PREL (1 << 13)
96/* get content */
97#define MODI_TXT ((1 << 14)|MODI_CHK)
98#define MODI_TXT_ENABLED(a) (((a)&(1 << 14))!=0)
99
100/* get audit record */
101#define MODI_AUDIT (1 << 15)
102#define MODI_AUDIT_ENABLED(a) (((a)&(1 << 15))!=0)
103/* do not check */
104#define MODI_NOCHECK (1 << 16)
105/* do not check */
106#define MODI_ALLIGNORE (1 << 17)
107
108#define MODI_TIGER192 0x01000000UL
109#define MODI_SHA1 0x02000000UL
110#define MODI_MD5 0x03000000UL
111#define MODI_SHA256 0x04000000UL
112#define MODI_HASHTYPE 0x0F000000UL
113
114#define MODI_INIT 0xD0000000UL
115#define MODI_INITIALIZED(a) (((a) & 0xF0000000UL) == MODI_INIT)
116
117#define MODI_SET(a, b) ((a) |= (b))
118#define MODI_CLEAR(a, b) ((a) &= ~(b))
119#define MODI_ISSET(a, b) (((a) & (b)) != 0)
120
121#define SH_TXT_MAX 9200
122
123#define MASK_ALLIGNORE_ 0
124extern unsigned long mask_ALLIGNORE;
125#define MASK_ATTRIBUTES_ (MODI_MOD|MODI_USR|MODI_GRP|MODI_RDEV)
126extern unsigned long mask_ATTRIBUTES;
127#define MASK_LOGFILES_ (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_INO)
128extern unsigned long mask_LOGFILES;
129#define MASK_LOGGROW_ (MASK_LOGFILES_|MODI_SIZ|MODI_SGROW|MODI_CHK)
130extern unsigned long mask_LOGGROW;
131#define MASK_READONLY_ (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_MTM|MODI_CTM)
132extern unsigned long mask_READONLY;
133#define MASK_NOIGNORE_ (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_ATM|MODI_MTM)
134extern unsigned long mask_NOIGNORE;
135#define MASK_USER_ (MASK_READONLY_|MODI_ATM)
136extern unsigned long mask_USER0;
137extern unsigned long mask_USER1;
138extern unsigned long mask_USER2;
139extern unsigned long mask_USER3;
140extern unsigned long mask_USER4;
141/* like READONLY, but without MTM,CTM,SIZ,INO, and with PREL)
142 */
143#define MASK_PRELINK_ (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_CHK|MODI_PREL)
144extern unsigned long mask_PRELINK;
145
146typedef struct file_struct {
147 unsigned long check_flags;
148 int file_reported;
149 char fullpath[PATH_MAX];
150 ShFileType type;
151 dev_t dev;
152 ino_t ino;
153 mode_t mode;
154 nlink_t hardlinks;
155#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
156 unsigned long attributes;
157 char c_attributes[ATTRBUF_SIZE];
158#endif
159 char c_mode[CMODE_SIZE];
160 uid_t owner;
161 char c_owner[USER_MAX+2];
162 gid_t group;
163 char c_group[GROUP_MAX+2];
164 dev_t rdev;
165 off_t size;
166 unsigned long blksize;
167 unsigned long blocks;
168 time_t atime;
169 time_t mtime;
170 time_t ctime;
171
172 char * link_path;
173 mode_t linkmode;
174 char link_c_mode[11];
175 int linkisok;
176 char * attr_string;
177} file_type;
178
179extern int sh_unix_check_selinux;
180extern int sh_unix_check_acl;
181
182/* destroy userid cache
183 */
184void sh_userid_destroy ();
185
186/* --- run a command, securely ---
187 */
188int sh_unix_run_command (const char * str);
189
190/* Ignore SIGPIPE
191 */
192void sh_unix_ign_sigpipe();
193
194/* mlock utilities
195 */
196int sh_unix_mlock(const char * file, int line, void * addr, size_t len);
197int sh_unix_munlock(void * addr, size_t len);
198int sh_unix_count_mlock(void);
199/* public for unit tests */
200int sh_unix_pagesize(void);
201unsigned long sh_unix_lookup_page(void * in_addr, size_t len, int * num_pages);
202
203/* chroot directory
204 */
205int sh_unix_set_chroot(const char * str);
206
207/* whether to use localtime for file timesatams in logs
208 */
209int sh_unix_uselocaltime (const char * c);
210
211/* whether to perform selinux/acl checks
212 */
213#ifdef USE_XATTR
214int sh_unix_setcheckselinux (const char * c);
215#endif
216#ifdef USE_ACL
217int sh_unix_setcheckacl (const char * c);
218#endif
219
220/* set I/O limit
221 */
222int sh_unix_set_io_limit (const char * c);
223void sh_unix_io_pause (void);
224
225/* get file type
226 */
227int sh_unix_get_ftype(char * fullpath);
228
229/* reset masks for policies
230 */
231int sh_unix_maskreset(void);
232
233/* return true if database is remote
234 */
235int file_is_remote (void);
236
237/* return the path to the configuration/database file
238 */
239char * file_path(char what, char flag);
240
241/* return current time as unsigned long
242 */
243unsigned long sh_unix_longtime (void);
244
245/* close all files >= fd, except possibly one
246 */
247void sh_unix_closeall (int fd, int except, int inchild);
248
249/* Check whether directory for pid file exists
250 */
251int sh_unix_check_piddir (char * pidpath);
252
253/* write lock for filename
254 */
255int sh_unix_write_lock_file(char * filename);
256
257/* rm lock(s) for log file(s)
258 */
259int sh_unix_rm_lock_file(char * filename);
260
261/* write the PID file
262 */
263int sh_unix_write_pid_file(void);
264
265/* rm the PID file
266 */
267int sh_unix_rm_pid_file(void);
268
269
270/* checksum of own binary
271 */
272int sh_unix_self_hash (const char * c);
273
274/* return BAD on failure
275 */
276int sh_unix_self_check (void);
277
278/* add a trusted user to the list
279 */
280int tf_add_trusted_user(const char *);
281
282/* check a file
283 */
284int tf_trust_check (const char * file, int mode);
285
286/* initialize group vector
287 */
288#ifdef HOST_IS_OSF
289int sh_unix_initgroups ( char * in_user, gid_t in_gid);
290#else
291int sh_unix_initgroups (const char * in_user, gid_t in_gid);
292#endif
293int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid);
294
295/* set the timeserver address
296 */
297int sh_unix_settimeserver (const char * address);
298void reset_count_dev_time(void);
299
300/* lock the key
301 */
302void sh_unix_memlock(void);
303
304/* deamon mode
305 */
306int sh_unix_setdeamon (const char * dummy);
307int sh_unix_setnodeamon(const char * dummy);
308
309/* Test whether file exists
310 */
311int sh_unix_file_exists(char * path);
312
313/* test whether file exists with proper attributes
314 */
315int sh_unix_device_readable(int fd);
316
317/* local host
318 */
319void sh_unix_localhost(void);
320
321/* check whether /proc exists and is a proc filesystem
322 */
323int sh_unix_test_proc(void);
324
325/* check whether a directory is secure
326 * (no symlink in path, not world-writeable)
327 */
328/* int sh_unix_is_secure_dir (ShErrLevel level, char * tmp); */
329
330/* check whether there's a rotated log with the correct inode and checksum
331 */
332int sh_check_rotated_log (const char * path,
333 UINT64 old_size, UINT64 old_inode, const char * old_hash, unsigned long mask);
334
335/* obtain file info
336 */
337int sh_unix_getinfo (int level, const char * filename, file_type * theFile,
338 char * fileHash, int flagrel);
339
340/* read file, return length read
341 */
342int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline);
343
344/* call with goDaemon == 1 to make daemon process
345 */
346int sh_unix_init(int goDaemon);
347
348/* for local time use thetime = 0, returns pointer to buffer
349 */
350char * sh_unix_time (time_t thetime, char * buffer, size_t len);
351
352/* convert to GMT time, returns pointer to buffer
353 */
354char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len);
355
356/* effective user info
357 */
358int sh_unix_getUser (void);
359
360/* get home directory, , returns pointer to out
361 */
362char * sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len);
363
364/* get a group GID
365 */
366long sh_group_to_gid (const char * g, int * fail);
367
368#ifdef HAVE_GETTIMEOFDAY
369unsigned long sh_unix_notime (void);
370#endif
371
372/* check whether a directory
373 */
374int sh_unix_isdir (char * dirName, int level);
375
376#ifdef SH_STEALTH
377int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len);
378void sh_unix_xor_code (char * str, int len);
379#endif
380
381#if defined(SCREW_IT_UP)
382/* for raise()
383 */
384#include <signal.h>
385#include <errno.h>
386
387void sh_sigtrap_handler (int signum);
388
389#ifdef HAVE_GETTIMEOFDAY
390#if TIME_WITH_SYS_TIME
391#include <sys/time.h>
392#include <time.h>
393#else
394#if HAVE_SYS_TIME_H
395#include <sys/time.h>
396#else
397#include <time.h>
398#endif
399#endif
400#endif
401
402struct sh_sigtrap_variables {
403 int not_traced;
404#ifdef HAVE_GETTIMEOFDAY
405 struct timeval save_tv;
406#endif
407};
408
409struct sh_sigtrap_variables * sh_sigtrap_variables_get();
410
411int sh_sigtrap_max_duration_set (const char * str);
412
413static inline
414int sh_sigtrap_prepare()
415{
416 struct sigaction act_trap;
417 int val_retry;
418 act_trap.sa_handler = &sh_sigtrap_handler; /* signal action */
419 act_trap.sa_flags = 0; /* init sa_flags */
420 sigemptyset ( &act_trap.sa_mask ); /* set an empty mask */
421 do {
422 val_retry = sigaction(SIGTRAP, &act_trap, NULL);
423 } while (val_retry < 0 && errno == EINTR);
424 return 0;
425}
426
427/*@unused@*/ static inline
428int sh_derr(void)
429{
430 struct sh_sigtrap_variables * sigtrap_variables;
431 sigtrap_variables = sh_sigtrap_variables_get();
432 if (sigtrap_variables == NULL) {
433 /* Perhaps, its better to not die, and to continue using Samhain,
434 even if this part does not work. */
435 return (0);
436 }
437
438 sigtrap_variables->not_traced = 0;
439
440#ifdef HAVE_GETTIMEOFDAY
441 gettimeofday(&sigtrap_variables->save_tv, NULL);
442#endif
443
444 /* raise() sends to same thread, like pthread_kill(pthread_self(), sig);
445 */
446 raise(SIGTRAP);
447
448 if (sigtrap_variables->not_traced == 0)
449 _exit(5);
450
451 sigtrap_variables->not_traced = 0;
452 return (0);
453}
454
455#else
456
457/*@unused@*/ static inline
458int sh_derr(void)
459{
460 return 0;
461}
462/* #if defined(SCREW_IT_UP) */
463#endif
464
465#endif
466
467
Note: See TracBrowser for help on using the repository browser.