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