source: trunk/include/samhain.h@ 162

Last change on this file since 162 was 162, checked in by katerina, 17 years ago

Fix for ticket #89, #90, and #91 (locking,compile failure).

File size: 13.2 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#ifndef SAMHAIN_H
21#define SAMHAIN_H
22
23#include <sys/types.h>
24#include "slib.h"
25
26#ifdef SH_ENCRYPT
27#include "rijndael-api-fst.h"
28#endif
29
30/**************************************************
31 *
32 * STANDARD DEFINES
33 *
34 **************************************************/
35
36#define REPLACE_OLD
37
38/* Standard buffer sizes.
39 */
40#define SH_MINIBUF 64
41#define SH_BUFSIZE 1024
42#define SH_MAXBUF 4096
43#define SH_PATHBUF 256
44
45#define SH_ERRBUF_SIZE 64
46
47/* Sizes for arrays (user, group, timestamp).
48 */
49#define SOCKPASS_MAX 14
50#define USER_MAX 20
51#define GROUP_MAX 20
52#define TIM_MAX 32
53
54#define CMODE_SIZE 11
55
56#define ATTRBUF_SIZE 16
57#define ATTRBUF_USED 12
58
59/* The number of bytes in a key,
60 * the number of chars in its hex repesentation,
61 * and the block size of the hash algorithm.
62 */
63#define KEY_BYT 24
64#define KEY_LEN 48
65#define KEY_BLOCK 24
66#define KEYBUF_SIZE (KEY_LEN+1)
67
68/* The length of the compiled-in password.
69 */
70#define PW_LEN 8
71
72#undef GOOD
73#define GOOD 1
74#undef BAD
75#define BAD 0
76#undef ON
77#define ON 1
78#undef OFF
79#define OFF 0
80#undef S_TRUE
81#define S_TRUE 1
82#undef S_FALSE
83#define S_FALSE 0
84
85/* An unsigned integer guaranteed to be 32 bit.
86 */
87#if defined(HAVE_INT_32)
88#define UINT32 unsigned int
89#define SINT32 int
90#elif defined(HAVE_LONG_32)
91#define UINT32 unsigned long
92#define SINT32 long
93#elif defined(HAVE_SHORT_32)
94#define UINT32 unsigned short
95#define SINT32 short
96#endif
97
98#ifdef HAVE_INTTYPES_H
99#include <inttypes.h>
100#endif
101#ifdef HAVE_STDINT_H
102#include <stdint.h>
103#endif
104
105#if !defined(HAVE_UINT64_T)
106
107#ifdef HAVE_LONG_LONG_64
108#define UINT64 unsigned long long
109#else
110#ifdef HAVE_LONG_64
111#define UINT64 unsigned long
112#else
113#error "no 64bit type found"
114#endif
115#endif
116
117#else
118#define UINT64 uint64_t
119#endif
120
121
122
123#define UBYTE unsigned char
124
125
126enum {
127 SH_CHECK_NONE = 0,
128 SH_CHECK_INIT = 1,
129 SH_CHECK_CHECK = 2
130};
131
132#define SH_MOD_THREAD 1
133#define SH_MOD_ACTIVE 0
134#define SH_MOD_FAILED -1
135
136/* Flags for file status
137 */
138#define SH_FFLAG_ALLIGNORE (1<<0)
139#define SH_FFLAG_VISITED (1<<1)
140#define SH_FFLAG_CHECKED (1<<3)
141#define SH_FFLAG_REPORTED (1<<3)
142#define SH_FFLAG_SUIDCHK (1<<4)
143
144#define SH_FFLAG_ALLIGNORE_SET(a) (((a) & SH_FFLAG_ALLIGNORE) != 0)
145#define SET_SH_FFLAG_ALLIGNORE(a) ((a) |= SH_FFLAG_ALLIGNORE)
146#define CLEAR_SH_FFLAG_ALLIGNORE(a) ((a) &= ~SH_FFLAG_ALLIGNORE)
147
148#define SH_FFLAG_VISITED_SET(a) (((a) & SH_FFLAG_VISITED) != 0)
149#define SET_SH_FFLAG_VISITED(a) ((a) |= SH_FFLAG_VISITED)
150#define CLEAR_SH_FFLAG_VISITED(a) ((a) &= ~SH_FFLAG_VISITED)
151
152#define SH_FFLAG_CHECKED_SET(a) (((a) & SH_FFLAG_VISITED) != 0)
153#define SET_SH_FFLAG_CHECKED(a) ((a) |= SH_FFLAG_VISITED)
154#define CLEAR_SH_FFLAG_CHECKED(a) ((a) &= ~SH_FFLAG_VISITED)
155
156#define SH_FFLAG_REPORTED_SET(a) (((a) & SH_FFLAG_REPORTED) != 0)
157#define SET_SH_FFLAG_REPORTED(a) ((a) |= SH_FFLAG_REPORTED)
158#define CLEAR_SH_FFLAG_REPORTED(a) ((a) &= ~SH_FFLAG_REPORTED)
159
160#define SH_FFLAG_SUIDCHK_SET(a) (((a) & SH_FFLAG_SUIDCHK) != 0)
161#define SET_SH_FFLAG_SUIDCHK(a) ((a) |= SH_FFLAG_SUIDCHK)
162#define CLEAR_SH_FFLAG_SUIDCHK(a) ((a) &= ~SH_FFLAG_SUIDCHK)
163
164
165/**************************************************
166 *
167 * TYPEDEFS
168 *
169 **************************************************/
170
171enum {
172 SH_LEVEL_READONLY = 1,
173 SH_LEVEL_LOGFILES = 2,
174 SH_LEVEL_LOGGROW = 3,
175 SH_LEVEL_NOIGNORE = 4,
176 SH_LEVEL_ALLIGNORE = 5,
177 SH_LEVEL_ATTRIBUTES = 6,
178 SH_LEVEL_USER0 = 7,
179 SH_LEVEL_USER1 = 8,
180 SH_LEVEL_USER2 = 9,
181 SH_LEVEL_USER3 = 10,
182 SH_LEVEL_USER4 = 11,
183 SH_LEVEL_PRELINK = 12
184};
185
186typedef struct {
187 time_t alarm_interval;
188 time_t alarm_last;
189} sh_timer_t;
190
191typedef struct {
192 char path[SH_PATHBUF];
193 char hash[KEY_LEN+1];
194} sh_sh_df;
195
196typedef struct {
197 char user[USER_MAX];
198 char group[GROUP_MAX];
199 char home[SH_PATHBUF];
200 uid_t uid;
201 gid_t gid;
202} sh_sh_user;
203
204typedef struct {
205 char name[SH_PATHBUF]; /* local hostname */
206 char system[SH_MINIBUF]; /* system */
207 char release[SH_MINIBUF]; /* release */
208 char machine[SH_MINIBUF]; /* machine */
209} sh_sh_local;
210
211typedef struct {
212 char name[SH_PATHBUF];
213 char alt[SH_PATHBUF];
214} sh_sh_remote;
215
216typedef struct {
217 unsigned long bytes_hashed; /* bytes last check */
218 unsigned long bytes_speed; /* bytes/sec last check */
219 unsigned long mail_success; /* mails sent */
220 unsigned long mail_failed; /* mails not sent */
221 time_t time_start; /* start last check */
222 time_t time_check; /* time last check */
223 unsigned long dirs_checked; /* #dirs last check */
224 unsigned long files_checked; /* #files last check */
225} sh_sh_stat;
226
227typedef struct {
228 int exit; /* exit value */
229 int checkSum; /* whether to init/check checksums */
230 int update; /* update db */
231 int opts; /* reading cl options */
232 int isdaemon; /* daemon or not */
233 int loop; /* go in loop even if not daemon */
234 int nice; /* desired nicety */
235 int isserver; /* server or not */
236 int islocked; /* BAD if logfile not locked */
237 int smsg; /* GOOD if end message sent */
238 int log_start; /* TRUE if new audit trail */
239 int reportonce; /* TRUE if bad files only once rep.*/
240 int fulldetail; /* TRUE if full details requested */
241 int client_severity; /* TRUE if client severity used */
242 int client_class; /* TRUE if client class used */
243 int audit;
244 unsigned long aud_mask;
245 int hidefile; /* TRUE if file not reveled in log */
246} sh_sh_flag;
247
248typedef struct {
249
250 char prg_name[8];
251
252 UINT64 pid;
253
254 sh_sh_df exec;
255 sh_sh_df conf;
256 sh_sh_df data;
257
258 sh_sh_user real;
259 sh_sh_user effective;
260 sh_sh_user run;
261
262 sh_sh_local host;
263
264 sh_sh_remote srvtime;
265 sh_sh_remote srvmail;
266 sh_sh_remote srvexport;
267 sh_sh_remote srvcons;
268 sh_sh_remote srvlog;
269
270 sh_sh_stat statistics;
271 sh_sh_flag flag;
272
273#ifdef SH_STEALTH
274 unsigned long off_data;
275#endif
276
277 sh_timer_t mailNum;
278 sh_timer_t mailTime;
279 sh_timer_t fileCheck;
280
281 int looptime; /* timing for main loop */
282 /*@null@*//*@out@*/ char * timezone;
283} sh_struct;
284
285
286extern volatile int sig_raised;
287extern volatile int sig_urgent;
288extern volatile int sig_debug_switch; /* SIGUSR1 */
289extern volatile int sig_suspend_switch; /* SIGUSR2 */
290extern volatile int sh_global_suspend_flag;
291extern volatile int sig_fresh_trail; /* SIGIOT */
292extern volatile int sh_thread_pause_flag;
293extern volatile int sig_config_read_again; /* SIGHUP */
294extern volatile int sig_terminate; /* SIGQUIT */
295extern volatile int sig_termfast; /* SIGTERM */
296extern volatile int sig_force_check; /* SIGTTOU */
297
298extern long int eintr__result;
299
300extern int sh_argc_store;
301extern char ** sh_argv_store;
302
303#include "sh_calls.h"
304
305
306typedef struct {
307 char sh_sockpass[2*SOCKPASS_MAX+2];
308 char sigkey_old[KEY_LEN+1];
309 char sigkey_new[KEY_LEN+1];
310 char mailkey_old[KEY_LEN+1];
311 char mailkey_new[KEY_LEN+1];
312 char crypt[KEY_LEN+1];
313 char session[KEY_LEN+1];
314 char vernam[KEY_LEN+1];
315 int mlock_failed;
316
317 char pw[PW_LEN];
318
319 char poolv[KEY_BYT];
320 int poolc;
321
322 int rngI;
323 UINT32 rng0[3];
324 UINT32 rng1[3];
325 UINT32 rng2[3];
326
327 UINT32 res_vec[6];
328
329 UINT32 ErrFlag[2];
330
331#ifdef SH_ENCRYPT
332 /*@out@*/ keyInstance keyInstE;
333 /*@out@*/ keyInstance keyInstD;
334#endif
335} sh_key_t;
336
337extern sh_struct sh;
338/*@null@*/ extern sh_key_t *skey;
339
340/**************************************************
341 *
342 * macros
343 *
344 **************************************************/
345
346#if defined(__GNUC__) && (__GNUC__ >= 4)
347#define SH_GNUC_SENTINEL __attribute__((__sentinel__))
348#else
349#define SH_GNUC_SENTINEL
350#endif
351
352#if defined(__GNUC__) && (__GNUC__ >= 3)
353#undef SH_GNUC_PURE
354#define SH_GNUC_PURE __attribute__((pure))
355#undef SH_GNUC_CONST
356#define SH_GNUC_CONST __attribute__((const))
357#undef SH_GNUC_NORETURN
358#define SH_GNUC_NORETURN __attribute__((noreturn))
359#undef SH_GNUC_MALLOC
360#define SH_GNUC_MALLOC __attribute__((malloc))
361#else
362#undef SH_GNUC_PURE
363#define SH_GNUC_PURE
364#undef SH_GNUC_CONST
365#define SH_GNUC_CONST
366#undef SH_GNUC_NORETURN
367#define SH_GNUC_NORETURN
368#undef SH_GNUC_MALLOC
369#define SH_GNUC_MALLOC
370#endif
371
372
373/* The semantics of the built-in are that it is expected that expr == const
374 * for __builtin_expect ((expr), const)
375 */
376#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
377#define SH_LIKELY(expr) (__builtin_expect((expr), 1))
378#define SH_UNLIKELY(expr) (__builtin_expect((expr), 0))
379#else
380#define SH_LIKELY(expr) (expr)
381#define SH_UNLIKELY(expr) (expr)
382#endif
383
384/* signal-safe log function
385 */
386int safe_logger (int signal, int method, char * details);
387void safe_fatal (char * details, char *f, int l);
388
389#define SH_VALIDATE_EQ(a,b) \
390 do { \
391 if ((a) != (b)) safe_fatal(#a " != " #b, FIL__, __LINE__);\
392 } while (0)
393
394#define SH_VALIDATE_NE(a,b) \
395 do { \
396 if ((a) == (b)) safe_fatal(#a " == " #b, FIL__, __LINE__);\
397 } while (0)
398
399#define SH_VALIDATE_GE(a,b) \
400 do { \
401 if ((a) < (b)) safe_fatal(#a " < " #b, FIL__, __LINE__);\
402 } while (0)
403
404#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
405#define MLOCK(a, b) \
406 if ((skey != NULL) && skey->mlock_failed == SL_FALSE){ \
407 (void) sl_set_suid(); \
408 if (sh_unix_mlock(FIL__, __LINE__, a, b) < 0) skey->mlock_failed = SL_TRUE; \
409 (void) sl_unset_suid(); }
410#else
411#define MLOCK(a, b) \
412 ;
413#endif
414
415#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
416#define MUNLOCK(a, b) \
417 if ((skey != NULL) && skey->mlock_failed == SL_FALSE){ \
418 (void) sl_set_suid(); \
419 (void) sh_unix_munlock( a, b );\
420 (void) sl_unset_suid(); }
421#else
422#define MUNLOCK(a, b) \
423 ;
424#endif
425
426#ifdef SH_STEALTH
427void sh_do_encode (char * str, int len);
428#define sh_do_decode sh_do_encode
429#endif
430
431/* #if defined(SCREW_IT_UP)
432 * extern volatile int sh_not_traced;
433 * inline int sh_sigtrap_prepare();
434 * inline int sh_derr();
435 * #endif
436 */
437
438#if defined(SCREW_IT_UP) && (defined(__FreeBSD__) || defined(__linux__)) && defined(__i386__)
439#define BREAKEXIT(expr) \
440 do { \
441 int ixi; \
442 for (ixi = 0; ixi < 8; ++ixi) { \
443 if ((*(volatile unsigned *)((unsigned) expr + ixi) & 0xff) == 0xcc) \
444 _exit(EXIT_FAILURE); \
445 } \
446 } \
447 while (1 == 0)
448#else
449#define BREAKEXIT(expr)
450#endif
451
452
453
454#include "sh_cat.h"
455#include "sh_trace.h"
456#include "sh_mem.h"
457
458#endif
459
460/* CRIT: */
461/* NEW_CLIENT <client> */
462/* BAD_CLIENT <client> -- <details> */
463/* ERR_CLIENT <client> -- <details> */
464
465/* ALERT: */
466/* LOG_KEY samhain|yule <key> */
467/* STARTUP samhain|yule -- user <username> */
468/* EXIT samhain|yule */
469/* GOODSIG <file> <user> */
470/* FP_KEY <fingerprint> */
471/* GOODSIG_DAT <file> <user> */
472/* FP_KEY_DAT <fingerprint> */
473/* TIGER_CFG <file> <checksum> */
474/* TIGER_DAT <file> <checksum> */
475
476/* PANIC -- <details> */
477/* ERROR -- <details> */
478
479/* Policy */
480/* POLICY <code> <file> */
481/* <code> = MISSING || ADDED || NOT_A_DIRECTORY || <policy> */
482
483
484
Note: See TracBrowser for help on using the repository browser.