[1] | 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 |
|
---|
[156] | 25 | /* For PATH_MAX */
|
---|
[1] | 26 | #include <limits.h>
|
---|
[156] | 27 | #if !defined(PATH_MAX)
|
---|
| 28 | #define PATH_MAX 1024
|
---|
| 29 | #endif
|
---|
| 30 |
|
---|
[1] | 31 | #include <unistd.h>
|
---|
| 32 | #include "samhain.h"
|
---|
| 33 | #include "sh_error.h"
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | typedef enum {
|
---|
| 37 | SH_ISLOG,
|
---|
| 38 | SH_ISFILE,
|
---|
| 39 | SH_ISDIR,
|
---|
| 40 | SH_ISDATA
|
---|
| 41 | } ShOpenType;
|
---|
| 42 |
|
---|
| 43 | typedef enum {
|
---|
| 44 | SH_DATA_RAW,
|
---|
| 45 | SH_DATA_LINE
|
---|
| 46 | } ShDataType;
|
---|
| 47 |
|
---|
| 48 | typedef 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,
|
---|
[40] | 56 | SH_FILE_DOOR,
|
---|
| 57 | SH_FILE_PORT,
|
---|
[1] | 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 | /* user */
|
---|
| 71 | #define MODI_USR (1 << 3)
|
---|
| 72 | /* group */
|
---|
| 73 | #define MODI_GRP (1 << 4)
|
---|
| 74 | /* mtime */
|
---|
| 75 | #define MODI_MTM (1 << 5)
|
---|
| 76 | /* ctime */
|
---|
| 77 | #define MODI_CTM (1 << 6)
|
---|
| 78 | /* atime */
|
---|
| 79 | #define MODI_ATM (1 << 7)
|
---|
| 80 | /* size */
|
---|
| 81 | #define MODI_SIZ (1 << 8)
|
---|
| 82 | /* file mode */
|
---|
| 83 | #define MODI_MOD (1 << 9)
|
---|
| 84 | /* hardlinks */
|
---|
| 85 | #define MODI_HLN (1 << 10)
|
---|
[40] | 86 | /* device type */
|
---|
[1] | 87 | #define MODI_RDEV (1 << 11)
|
---|
[19] | 88 | /* size may grow */
|
---|
| 89 | #define MODI_SGROW (1 << 12)
|
---|
| 90 | /* use prelink */
|
---|
| 91 | #define MODI_PREL (1 << 13)
|
---|
[167] | 92 | /* get content */
|
---|
[169] | 93 | #define MODI_TXT ((1 << 14)|MODI_CHK)
|
---|
[1] | 94 |
|
---|
[170] | 95 | #define MODI_TXT_ENABLED(a) (((a)&(1 << 14))!=0)
|
---|
| 96 |
|
---|
[167] | 97 | #define SH_TXT_MAX 9200
|
---|
| 98 |
|
---|
[1] | 99 | #define MASK_ALLIGNORE_ 0
|
---|
| 100 | extern unsigned long mask_ALLIGNORE;
|
---|
| 101 | #define MASK_ATTRIBUTES_ (MODI_MOD|MODI_USR|MODI_GRP|MODI_RDEV)
|
---|
| 102 | extern unsigned long mask_ATTRIBUTES;
|
---|
| 103 | #define MASK_LOGFILES_ (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_INO)
|
---|
| 104 | extern unsigned long mask_LOGFILES;
|
---|
[19] | 105 | #define MASK_LOGGROW_ (MASK_LOGFILES_|MODI_SIZ|MODI_SGROW|MODI_CHK)
|
---|
[1] | 106 | extern unsigned long mask_LOGGROW;
|
---|
| 107 | #define MASK_READONLY_ (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_MTM|MODI_CTM)
|
---|
| 108 | extern unsigned long mask_READONLY;
|
---|
| 109 | #define MASK_NOIGNORE_ (MASK_LOGFILES_|MODI_CHK|MODI_SIZ|MODI_ATM|MODI_MTM)
|
---|
| 110 | extern unsigned long mask_NOIGNORE;
|
---|
| 111 | #define MASK_USER_ (MASK_READONLY_|MODI_ATM)
|
---|
| 112 | extern unsigned long mask_USER0;
|
---|
| 113 | extern unsigned long mask_USER1;
|
---|
[27] | 114 | extern unsigned long mask_USER2;
|
---|
| 115 | extern unsigned long mask_USER3;
|
---|
| 116 | extern unsigned long mask_USER4;
|
---|
[19] | 117 | /* like READONLY, but without MTM,CTM,SIZ,INO, abd with PREL)
|
---|
[1] | 118 | */
|
---|
[19] | 119 | #define MASK_PRELINK_ (MASK_ATTRIBUTES_|MODI_HLN|MODI_LNK|MODI_CHK|MODI_PREL)
|
---|
[1] | 120 | extern unsigned long mask_PRELINK;
|
---|
| 121 |
|
---|
| 122 | typedef struct file_struct {
|
---|
| 123 | unsigned long check_mask;
|
---|
[114] | 124 | int file_reported;
|
---|
[1] | 125 | char fullpath[PATH_MAX];
|
---|
| 126 | ShFileType type;
|
---|
| 127 | dev_t dev;
|
---|
| 128 | ino_t ino;
|
---|
| 129 | mode_t mode;
|
---|
| 130 | nlink_t hardlinks;
|
---|
[10] | 131 | #if defined(__linux__) || defined(HAVE_STAT_FLAGS)
|
---|
[1] | 132 | unsigned long attributes;
|
---|
[149] | 133 | char c_attributes[ATTRBUF_SIZE];
|
---|
[1] | 134 | #endif
|
---|
[149] | 135 | char c_mode[CMODE_SIZE];
|
---|
[1] | 136 | uid_t owner;
|
---|
| 137 | char c_owner[USER_MAX+2];
|
---|
| 138 | gid_t group;
|
---|
| 139 | char c_group[GROUP_MAX+2];
|
---|
| 140 | dev_t rdev;
|
---|
| 141 | off_t size;
|
---|
| 142 | unsigned long blksize;
|
---|
| 143 | unsigned long blocks;
|
---|
| 144 | time_t atime;
|
---|
| 145 | time_t mtime;
|
---|
| 146 | time_t ctime;
|
---|
| 147 |
|
---|
[167] | 148 | char * link_path;
|
---|
[1] | 149 | mode_t linkmode;
|
---|
| 150 | char link_c_mode[11];
|
---|
| 151 | int linkisok;
|
---|
[68] | 152 | char * attr_string;
|
---|
[1] | 153 | } file_type;
|
---|
| 154 |
|
---|
[68] | 155 | extern int sh_unix_check_selinux;
|
---|
| 156 | extern int sh_unix_check_acl;
|
---|
| 157 |
|
---|
[1] | 158 | /* mlock utilities
|
---|
| 159 | */
|
---|
[170] | 160 | int sh_unix_mlock(const char * file, int line, void * addr, size_t len);
|
---|
[1] | 161 | int sh_unix_munlock(void * addr, size_t len);
|
---|
[170] | 162 | int sh_unix_count_mlock(void);
|
---|
[25] | 163 | /* public for unit tests */
|
---|
[170] | 164 | int sh_unix_pagesize(void);
|
---|
[25] | 165 | unsigned long sh_unix_lookup_page(void * in_addr, size_t len, int * num_pages);
|
---|
[1] | 166 |
|
---|
| 167 | /* chroot directory
|
---|
| 168 | */
|
---|
[20] | 169 | int sh_unix_set_chroot(const char * str);
|
---|
[1] | 170 |
|
---|
| 171 | /* whether to use localtime for file timesatams in logs
|
---|
| 172 | */
|
---|
[22] | 173 | int sh_unix_uselocaltime (const char * c);
|
---|
[1] | 174 |
|
---|
[68] | 175 | /* whether to perform selinux/acl checks
|
---|
| 176 | */
|
---|
| 177 | #ifdef USE_XATTR
|
---|
| 178 | int sh_unix_setcheckselinux (const char * c);
|
---|
| 179 | #endif
|
---|
| 180 | #ifdef USE_ACL
|
---|
| 181 | int sh_unix_setcheckacl (const char * c);
|
---|
| 182 | #endif
|
---|
| 183 |
|
---|
[1] | 184 | /* set I/O limit
|
---|
| 185 | */
|
---|
[22] | 186 | int sh_unix_set_io_limit (const char * c);
|
---|
[170] | 187 | void sh_unix_io_pause (void);
|
---|
[1] | 188 |
|
---|
| 189 | /* get file type
|
---|
| 190 | */
|
---|
| 191 | int sh_unix_get_ftype(char * fullpath);
|
---|
| 192 |
|
---|
| 193 | /* reset masks for policies
|
---|
| 194 | */
|
---|
[170] | 195 | int sh_unix_maskreset(void);
|
---|
[1] | 196 |
|
---|
| 197 | /* return true if database is remote
|
---|
| 198 | */
|
---|
[170] | 199 | int file_is_remote (void);
|
---|
[1] | 200 |
|
---|
| 201 | /* return the path to the configuration/database file
|
---|
| 202 | */
|
---|
| 203 | char * file_path(char what, char flag);
|
---|
| 204 |
|
---|
| 205 | /* return current time as unsigned long
|
---|
| 206 | */
|
---|
| 207 | unsigned long sh_unix_longtime (void);
|
---|
| 208 |
|
---|
| 209 | /* close all files >= fd, except possibly one
|
---|
| 210 | */
|
---|
[174] | 211 | void sh_unix_closeall (int fd, int except, int inchild);
|
---|
[1] | 212 |
|
---|
| 213 |
|
---|
| 214 | /* write lock for filename
|
---|
| 215 | */
|
---|
| 216 | int sh_unix_write_lock_file(char * filename);
|
---|
| 217 |
|
---|
| 218 | /* rm lock(s) for log file(s)
|
---|
| 219 | */
|
---|
| 220 | int sh_unix_rm_lock_file(char * filename);
|
---|
| 221 |
|
---|
| 222 | /* write the PID file
|
---|
| 223 | */
|
---|
[170] | 224 | int sh_unix_write_pid_file(void);
|
---|
[1] | 225 |
|
---|
| 226 | /* rm the PID file
|
---|
| 227 | */
|
---|
[170] | 228 | int sh_unix_rm_pid_file(void);
|
---|
[1] | 229 |
|
---|
| 230 |
|
---|
| 231 | /* checksum of own binary
|
---|
| 232 | */
|
---|
[22] | 233 | int sh_unix_self_hash (const char * c);
|
---|
[1] | 234 |
|
---|
| 235 | /* return BAD on failure
|
---|
| 236 | */
|
---|
| 237 | int sh_unix_self_check (void);
|
---|
| 238 |
|
---|
| 239 | /* add a trusted user to the list
|
---|
| 240 | */
|
---|
[22] | 241 | int tf_add_trusted_user(const char *);
|
---|
[1] | 242 |
|
---|
| 243 | /* check a file
|
---|
| 244 | */
|
---|
[183] | 245 | int tf_trust_check (const char * file, int mode);
|
---|
[1] | 246 |
|
---|
| 247 | /* initialize group vector
|
---|
| 248 | */
|
---|
| 249 | #ifdef HOST_IS_OSF
|
---|
| 250 | int sh_unix_initgroups ( char * in_user, gid_t in_gid);
|
---|
| 251 | #else
|
---|
| 252 | int sh_unix_initgroups (const char * in_user, gid_t in_gid);
|
---|
| 253 | #endif
|
---|
| 254 | int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid);
|
---|
| 255 |
|
---|
| 256 | /* set the timeserver address
|
---|
| 257 | */
|
---|
[22] | 258 | int sh_unix_settimeserver (const char * address);
|
---|
[1] | 259 | void reset_count_dev_time(void);
|
---|
| 260 |
|
---|
| 261 | /* lock the key
|
---|
| 262 | */
|
---|
| 263 | void sh_unix_memlock(void);
|
---|
| 264 |
|
---|
| 265 | /* deamon mode
|
---|
| 266 | */
|
---|
[20] | 267 | int sh_unix_setdeamon (const char * dummy);
|
---|
| 268 | int sh_unix_setnodeamon(const char * dummy);
|
---|
[1] | 269 |
|
---|
| 270 | /* Test whether file exists
|
---|
| 271 | */
|
---|
[78] | 272 | int sh_unix_file_exists(char * path);
|
---|
[1] | 273 |
|
---|
| 274 | /* test whether file exists with proper attributes
|
---|
| 275 | */
|
---|
[78] | 276 | int sh_unix_device_readable(int fd);
|
---|
[1] | 277 |
|
---|
| 278 | /* local host
|
---|
| 279 | */
|
---|
| 280 | void sh_unix_localhost(void);
|
---|
| 281 |
|
---|
| 282 | /* check whether /proc exists and is a proc filesystem
|
---|
| 283 | */
|
---|
| 284 | int sh_unix_test_proc(void);
|
---|
| 285 |
|
---|
| 286 | /* check whether a directory is secure
|
---|
| 287 | * (no symlink in path, not world-writeable)
|
---|
| 288 | */
|
---|
| 289 | /* int sh_unix_is_secure_dir (ShErrLevel level, char * tmp); */
|
---|
| 290 |
|
---|
| 291 | /* obtain file info
|
---|
| 292 | */
|
---|
| 293 | int sh_unix_getinfo (int level, char * filename, file_type * theFile,
|
---|
| 294 | char * fileHash, int flagrel);
|
---|
| 295 |
|
---|
| 296 | /* read file, return length read
|
---|
| 297 | */
|
---|
| 298 | int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline);
|
---|
| 299 |
|
---|
| 300 | /* call with goDaemon == 1 to make daemon process
|
---|
| 301 | */
|
---|
| 302 | int sh_unix_init(int goDaemon);
|
---|
| 303 |
|
---|
[132] | 304 | /* for local time use thetime = 0, returns pointer to buffer
|
---|
[1] | 305 | */
|
---|
[132] | 306 | char * sh_unix_time (time_t thetime, char * buffer, size_t len);
|
---|
[1] | 307 |
|
---|
[132] | 308 | /* convert to GMT time, returns pointer to buffer
|
---|
[1] | 309 | */
|
---|
[132] | 310 | char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len);
|
---|
[1] | 311 |
|
---|
| 312 | /* effective user info
|
---|
| 313 | */
|
---|
| 314 | int sh_unix_getUser (void);
|
---|
| 315 |
|
---|
[132] | 316 | /* get home directory, , returns pointer to out
|
---|
[1] | 317 | */
|
---|
[132] | 318 | char * sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len);
|
---|
[1] | 319 |
|
---|
| 320 |
|
---|
| 321 | #ifdef HAVE_GETTIMEOFDAY
|
---|
| 322 | unsigned long sh_unix_notime (void);
|
---|
| 323 | #endif
|
---|
| 324 |
|
---|
| 325 | /* check whether a directory
|
---|
| 326 | */
|
---|
| 327 | int sh_unix_isdir (char * dirName, int level);
|
---|
| 328 |
|
---|
| 329 | #ifdef SH_STEALTH
|
---|
| 330 | int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len);
|
---|
| 331 | void sh_unix_xor_code (char * str, int len);
|
---|
| 332 | #endif
|
---|
| 333 |
|
---|
| 334 | #if defined(SCREW_IT_UP)
|
---|
| 335 | /* for raise()
|
---|
| 336 | */
|
---|
| 337 | #include <signal.h>
|
---|
| 338 | #include <errno.h>
|
---|
| 339 |
|
---|
| 340 | void sh_sigtrap_handler (int signum);
|
---|
| 341 | extern volatile int sh_not_traced;
|
---|
| 342 |
|
---|
| 343 | #ifdef HAVE_GETTIMEOFDAY
|
---|
| 344 | #if TIME_WITH_SYS_TIME
|
---|
| 345 | #include <sys/time.h>
|
---|
| 346 | #include <time.h>
|
---|
| 347 | #else
|
---|
| 348 | #if HAVE_SYS_TIME_H
|
---|
| 349 | #include <sys/time.h>
|
---|
| 350 | #else
|
---|
| 351 | #include <time.h>
|
---|
| 352 | #endif
|
---|
| 353 | #endif
|
---|
| 354 | extern struct timeval save_tv;
|
---|
| 355 | #endif
|
---|
| 356 |
|
---|
| 357 | static inline
|
---|
| 358 | int sh_sigtrap_prepare()
|
---|
| 359 | {
|
---|
| 360 | struct sigaction act_trap;
|
---|
| 361 | int val_retry;
|
---|
| 362 | act_trap.sa_handler = &sh_sigtrap_handler; /* signal action */
|
---|
| 363 | act_trap.sa_flags = 0; /* init sa_flags */
|
---|
| 364 | sigemptyset ( &act_trap.sa_mask ); /* set an empty mask */
|
---|
| 365 | do {
|
---|
| 366 | val_retry = sigaction(SIGTRAP, &act_trap, NULL);
|
---|
| 367 | } while (val_retry < 0 && errno == EINTR);
|
---|
| 368 | return 0;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /*@unused@*/ static inline
|
---|
| 372 | int sh_derr(void)
|
---|
| 373 | {
|
---|
| 374 | sh_not_traced = 0;
|
---|
| 375 |
|
---|
| 376 | #ifdef HAVE_GETTIMEOFDAY
|
---|
| 377 | gettimeofday(&save_tv, NULL);
|
---|
| 378 | #endif
|
---|
| 379 |
|
---|
| 380 | #if defined(__linux__) && defined(__GNUC__) && defined(__i386__)
|
---|
[103] | 381 | __asm__ __volatile__ (".byte 0xf1");
|
---|
[1] | 382 | #else
|
---|
| 383 | raise(SIGTRAP);
|
---|
| 384 | #endif
|
---|
| 385 |
|
---|
| 386 | if (sh_not_traced == 0)
|
---|
| 387 | _exit(5);
|
---|
| 388 | sh_not_traced = 0;
|
---|
| 389 | return (0);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | #else
|
---|
| 393 |
|
---|
| 394 | /*@unused@*/ static inline
|
---|
| 395 | int sh_derr(void)
|
---|
| 396 | {
|
---|
| 397 | return 0;
|
---|
| 398 | }
|
---|
| 399 | /* #if defined(SCREW_IT_UP) */
|
---|
| 400 | #endif
|
---|
| 401 |
|
---|
| 402 | #endif
|
---|
| 403 |
|
---|
| 404 |
|
---|