source: trunk/include/samhain.h@ 231

Last change on this file since 231 was 207, checked in by katerina, 16 years ago

Fix for ticket #134 (size may get truncated to INT_MAX)

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