summaryrefslogtreecommitdiffstats
path: root/include/sh_unix.h
blob: a5a4cebe0a4af3d2b2ef5d89cf4f65139795d23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* SAMHAIN file system integrity testing                                   */
/* Copyright (C) 1999 Rainer Wichmann                                      */
/*                                                                         */
/*  This program is free software; you can redistribute it                 */
/*  and/or modify                                                          */
/*  it under the terms of the GNU General Public License as                */
/*  published by                                                           */
/*  the Free Software Foundation; either version 2 of the License, or      */
/*  (at your option) any later version.                                    */
/*                                                                         */
/*  This program is distributed in the hope that it will be useful,        */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*  GNU General Public License for more details.                           */
/*                                                                         */
/*  You should have received a copy of the GNU General Public License      */
/*  along with this program; if not, write to the Free Software            */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */



#ifndef SH_UNIX_H
#define SH_UNIX_H

/* For PATH_MAX */
#include <limits.h>
#if !defined(PATH_MAX)
#define PATH_MAX 1024
#endif

#include <unistd.h>
#include "samhain.h"
#include "sh_error.h"


typedef enum {
  SH_ISLOG,
  SH_ISFILE,
  SH_ISDIR,
  SH_ISDATA
} ShOpenType;

typedef enum {
  SH_DATA_RAW,
  SH_DATA_LINE
} ShDataType;

typedef enum {
  SH_FILE_REGULAR,
  SH_FILE_SYMLINK,
  SH_FILE_DIRECTORY,
  SH_FILE_CDEV,
  SH_FILE_BDEV,
  SH_FILE_FIFO,
  SH_FILE_SOCKET,
  SH_FILE_DOOR,
  SH_FILE_PORT,
  SH_FILE_UNKNOWN
} ShFileType;

/* -- Attributes to check. --
 */

/* checksum     */
#define MODI_CHK (1 << 0)
/* link         */
#define MODI_LNK (1 << 1)
/* inode        */
#define MODI_INO (1 << 2)

/* user         */
#define MODI_USR (1 << 3)
/* group        */
#define MODI_GRP (1 << 4)
/* mtime        */
#define MODI_MTM (1 << 5)

/* ctime        */
#define MODI_CTM (1 << 6)
/* atime        */
#define MODI_ATM (1 << 7)
/* size         */
#define MODI_SIZ (1 << 8)

/* file mode    */
#define MODI_MOD (1 << 9)
/* hardlinks    */
#define MODI_HLN (1 << 10)
/* device type   */
#define MODI_RDEV (1 << 11)

/* size may grow   */
#define MODI_SGROW (1 << 12)
/* use prelink     */
#define MODI_PREL (1 << 13)
/* get content     */
#define MODI_TXT ((1 << 14)|MODI_CHK)
#define MODI_TXT_ENABLED(a) (((a)&(1 << 14))!=0)

/* get audit record  */
#define MODI_AUDIT (1 << 15)
#define MODI_AUDIT_ENABLED(a) (((a)&(1 << 15))!=0)
/* do not check  */
#define MODI_NOCHECK (1 << 16)
/* do not check  */
#define MODI_ALLIGNORE (1 << 17)

#define MODI_TIGER192  0x01000000UL
#define MODI_SHA1      0x02000000UL
#define MODI_MD5       0x03000000UL
#define MODI_SHA256    0x04000000UL
#define MODI_HASHTYPE  0x0F000000UL

#define MODI_INIT 0xD0000000UL
#define MODI_INITIALIZED(a) (((a) & 0xF0000000UL) == MODI_INIT)

#define MODI_SET(a, b) ((a) |= (b))
#define MODI_CLEAR(a, b) ((a) &= ~(b))
#define MODI_ISSET(a, b) (((a) & (b)) != 0)

#define SH_TXT_MAX 9200

#define MASK_ALLIGNORE_  0
extern  unsigned long mask_ALLIGNORE;
#define MASK_ATTRIBUTES_ (MODI_MOD|MODI_USR|MODI_GRP|MODI_RDEV)
extern  unsigned long mask_ATTRIBUTES;
#define MASK_LOGFILES_   (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_INO)
extern  unsigned long mask_LOGFILES;
#define MASK_LOGGROW_    (MASK_LOGFILES_|MODI_SIZ|MODI_SGROW|MODI_CHK) 
extern  unsigned long mask_LOGGROW;
#define MASK_READONLY_   (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_MTM|MODI_CTM)
extern  unsigned long mask_READONLY;
#define MASK_NOIGNORE_   (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_ATM|MODI_MTM)
extern  unsigned long mask_NOIGNORE;
#define MASK_USER_       (MASK_READONLY_|MODI_ATM)
extern  unsigned long mask_USER0;
extern  unsigned long mask_USER1;
extern  unsigned long mask_USER2;
extern  unsigned long mask_USER3;
extern  unsigned long mask_USER4;
/* like READONLY, but without MTM,CTM,SIZ,INO, and with PREL)
 */
#define MASK_PRELINK_   (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_CHK|MODI_PREL)
extern  unsigned long mask_PRELINK;

typedef struct file_struct {
  unsigned long    check_flags;
  int              file_reported;
  char             fullpath[PATH_MAX];
  ShFileType       type;
  dev_t            dev;
  ino_t            ino;
  mode_t           mode;
  nlink_t          hardlinks;
#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
  unsigned long    attributes;
  char             c_attributes[ATTRBUF_SIZE];
#endif
  char             c_mode[CMODE_SIZE];
  uid_t            owner;
  char             c_owner[USER_MAX+2];
  gid_t            group;
  char             c_group[GROUP_MAX+2];
  dev_t            rdev;
  off_t            size;
  unsigned long    blksize;
  unsigned long    blocks;
  time_t           atime;
  time_t           mtime;
  time_t           ctime;

  char           * link_path;
  mode_t           linkmode;
  char             link_c_mode[11];
  int              linkisok;
  char           * attr_string;
} file_type;

extern int sh_unix_check_selinux;
extern int sh_unix_check_acl;

/* destroy userid cache 
 */
void sh_userid_destroy ();

/* --- run a command, securely --- 
 */
int sh_unix_run_command (const char * str);

/* Ignore SIGPIPE
 */
void sh_unix_ign_sigpipe();

/* mlock utilities
 */
int sh_unix_mlock(const char * file, int line, void * addr, size_t len);
int sh_unix_munlock(void * addr, size_t len);
int sh_unix_count_mlock(void);
/* public for unit tests */
int sh_unix_pagesize(void);
unsigned long sh_unix_lookup_page(void * in_addr, size_t len, int * num_pages);

/* chroot directory
 */
int sh_unix_set_chroot(const char * str);

/* whether to use localtime for file timesatams in logs
 */
int sh_unix_uselocaltime (const char * c);

/* whether to perform selinux/acl checks
 */ 
#ifdef USE_XATTR
int sh_unix_setcheckselinux (const char * c);
#endif
#ifdef USE_ACL
int sh_unix_setcheckacl (const char * c);
#endif

/* set I/O limit
 */
int  sh_unix_set_io_limit (const char * c);
void sh_unix_io_pause (void);

/* get file type
 */
int sh_unix_get_ftype(char * fullpath);

/* reset masks for policies
 */
int sh_unix_maskreset(void);

/* return true if database is remote
 */
int file_is_remote (void);

/* return the path to the configuration/database file
 */
char * file_path(char what, char flag);

/* return current time as unsigned long
 */
unsigned long sh_unix_longtime (void);

/* close all files >= fd, except possibly one
 */
void sh_unix_closeall (int fd, int except, int inchild);

/* Check whether directory for pid file exists
 */
int sh_unix_check_piddir (char * pidpath);

/* write lock for filename
 */
int sh_unix_write_lock_file(char * filename);

/* rm lock(s) for log file(s)
 */
int sh_unix_rm_lock_file(char * filename);

/* write the PID file
 */
int sh_unix_write_pid_file(void);

/* rm the PID file
 */
int sh_unix_rm_pid_file(void);


/* checksum of own binary
 */
int sh_unix_self_hash (const char * c);

/* return BAD on failure
 */
int sh_unix_self_check (void);

/* add a trusted user to the list 
 */
int tf_add_trusted_user(const char *);

/* check a file 
 */
int tf_trust_check (const char * file, int mode);

/* initialize group vector
 */
#ifdef HOST_IS_OSF
int  sh_unix_initgroups  (      char * in_user, gid_t in_gid);
#else
int  sh_unix_initgroups  (const char * in_user, gid_t in_gid);
#endif
int  sh_unix_initgroups2 (uid_t         in_pid, gid_t in_gid);

/* set the timeserver address
 */
int sh_unix_settimeserver (const char * address);
void reset_count_dev_time(void);

/* lock the key
 */
void sh_unix_memlock(void);

/* deamon mode 
 */
int sh_unix_setdeamon  (const char * dummy);
int sh_unix_setnodeamon(const char * dummy);

/* Test whether file exists
 */
int sh_unix_file_exists(char * path);

/* test whether file exists with proper attributes
 */
int sh_unix_device_readable(int fd);

/* local host
 */
void sh_unix_localhost(void);

/* check whether /proc exists and is a proc filesystem
 */ 
int sh_unix_test_proc(void);

/* check whether a directory is secure 
 * (no symlink in path, not world-writeable)
 */
/* int sh_unix_is_secure_dir (ShErrLevel level, char * tmp); */

/* check whether there's a rotated log with the correct inode and checksum 
 */
int sh_check_rotated_log (const char * path,  
			  UINT64 old_size, UINT64 old_inode, const char * old_hash, unsigned long mask);

/* obtain file info
 */
int sh_unix_getinfo (int level, const char * filename, file_type * theFile, 
		     char * fileHash, int flagrel);

/* read file, return length read
 */
int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline);

/* call with goDaemon == 1 to make daemon process
 */
int  sh_unix_init(int goDaemon);

/* for local time use thetime = 0, returns pointer to buffer 
 */
char * sh_unix_time (time_t thetime, char * buffer, size_t len);

/* convert to GMT time, returns pointer to buffer
 */
char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len);

/* effective user info
 */
int  sh_unix_getUser (void);

/* get home directory, , returns pointer to out
 */
char *  sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len);

/* get a group GID 
 */
long sh_group_to_gid (const char * g, int * fail);

#ifdef HAVE_GETTIMEOFDAY
unsigned long sh_unix_notime (void);
#endif

/* check whether a directory
 */
int sh_unix_isdir (char * dirName, int level);

#ifdef SH_STEALTH
int  sh_unix_getline_stealth  (SL_TICKET fd, char * str, int len);
void sh_unix_xor_code (char * str, int len);
#endif

#if defined(SCREW_IT_UP)
/* for raise() 
 */
#include <signal.h>
#include <errno.h>

void   sh_sigtrap_handler (int signum);

#ifdef HAVE_GETTIMEOFDAY
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#endif

struct sh_sigtrap_variables {
  int not_traced;
#ifdef HAVE_GETTIMEOFDAY
  struct timeval save_tv;
#endif
};

struct sh_sigtrap_variables * sh_sigtrap_variables_get();

int sh_sigtrap_max_duration_set (const char * str);

static inline
int  sh_sigtrap_prepare()
{
  struct sigaction act_trap;
  int              val_retry;
  act_trap.sa_handler   = &sh_sigtrap_handler;   /* signal action     */
  act_trap.sa_flags     = 0;                     /* init sa_flags     */
  sigemptyset ( &act_trap.sa_mask );             /* set an empty mask */
  do {
    val_retry = sigaction(SIGTRAP, &act_trap, NULL);
  } while (val_retry < 0 && errno == EINTR);
  return 0;
}

/*@unused@*/ static inline 
int sh_derr(void)
{
  struct sh_sigtrap_variables * sigtrap_variables;
  sigtrap_variables = sh_sigtrap_variables_get();
  if (sigtrap_variables == NULL) {
    /* Perhaps, its better to not die, and to continue using Samhain,
       even if this part does not work. */
    return (0);
  }

  sigtrap_variables->not_traced = 0;

#ifdef HAVE_GETTIMEOFDAY
  gettimeofday(&sigtrap_variables->save_tv, NULL);
#endif

  /* raise() sends to same thread, like pthread_kill(pthread_self(), sig); 
   */
  raise(SIGTRAP);
  
  if (sigtrap_variables->not_traced == 0)
    _exit(5);

  sigtrap_variables->not_traced = 0;
  return (0);
}

#else

/*@unused@*/ static inline 
int sh_derr(void)
{
  return 0;
}
/* #if defined(SCREW_IT_UP) */
#endif

#endif