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