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 | #include "config_xor.h"
|
---|
21 |
|
---|
22 | #ifdef HOST_IS_HPUX
|
---|
23 | #define _XOPEN_SOURCE_EXTENDED
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <errno.h>
|
---|
29 |
|
---|
30 | #include <sys/types.h>
|
---|
31 | #include <unistd.h>
|
---|
32 | #include <sys/stat.h>
|
---|
33 | #include <fcntl.h>
|
---|
34 | #include <signal.h>
|
---|
35 | #include <sys/socket.h>
|
---|
36 | #include <netdb.h>
|
---|
37 | #include <netinet/in.h>
|
---|
38 |
|
---|
39 | #ifndef S_SPLINT_S
|
---|
40 | #include <arpa/inet.h>
|
---|
41 | #else
|
---|
42 | #define AF_INET 2
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #include <time.h>
|
---|
46 |
|
---|
47 | #ifndef HAVE_LSTAT
|
---|
48 | #define lstat stat
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #include "samhain.h"
|
---|
52 | #include "sh_error.h"
|
---|
53 | #include "sh_calls.h"
|
---|
54 | #include "sh_ipvx.h"
|
---|
55 | #include "sh_sub.h"
|
---|
56 | #include "sh_utils.h"
|
---|
57 |
|
---|
58 | #undef FIL__
|
---|
59 | #define FIL__ _("sh_calls.c")
|
---|
60 |
|
---|
61 | extern int flag_err_debug;
|
---|
62 |
|
---|
63 | char aud_err_message[64];
|
---|
64 |
|
---|
65 | typedef struct cht_struct
|
---|
66 | {
|
---|
67 | const char * str;
|
---|
68 | unsigned long val;
|
---|
69 | } cht_type;
|
---|
70 |
|
---|
71 | static cht_type aud_tab[] =
|
---|
72 | {
|
---|
73 | { N_("execve"), AUD_EXEC },
|
---|
74 | { N_("utime"), AUD_UTIME },
|
---|
75 | { N_("unlink"), AUD_UNLINK },
|
---|
76 | { N_("dup"), AUD_DUP },
|
---|
77 | { N_("chdir"), AUD_CHDIR },
|
---|
78 | { N_("open"), AUD_OPEN },
|
---|
79 | { N_("kill"), AUD_KILL },
|
---|
80 | { N_("exit"), AUD_EXIT },
|
---|
81 | { N_("fork"), AUD_FORK },
|
---|
82 | { N_("setuid"), AUD_SETUID },
|
---|
83 | { N_("setgid"), AUD_SETGID },
|
---|
84 | { N_("pipe"), AUD_PIPE },
|
---|
85 | { NULL, 0 }
|
---|
86 | };
|
---|
87 |
|
---|
88 | /* Set aud functions
|
---|
89 | */
|
---|
90 | int sh_aud_set_functions(const char * str_s)
|
---|
91 | {
|
---|
92 | int i = 0;
|
---|
93 |
|
---|
94 | SL_ENTER(_("sh_aud_set_functions"));
|
---|
95 |
|
---|
96 | if (str_s == NULL)
|
---|
97 | return -1;
|
---|
98 |
|
---|
99 | while (aud_tab[i].str != NULL)
|
---|
100 | {
|
---|
101 | if (NULL != sl_strstr (str_s, _(aud_tab[i].str)))
|
---|
102 | {
|
---|
103 | sh.flag.audit = 1;
|
---|
104 | sh.flag.aud_mask |= aud_tab[i].val;
|
---|
105 | }
|
---|
106 | ++i;
|
---|
107 | }
|
---|
108 |
|
---|
109 | SL_RETURN(0,_("sh_aud_set_functions"));
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | /* Need to catch EINTR for these functions.
|
---|
116 | */
|
---|
117 | long int retry_sigaction(const char * file, int line,
|
---|
118 | int signum, const struct sigaction *act,
|
---|
119 | struct sigaction *oldact)
|
---|
120 | {
|
---|
121 | int error;
|
---|
122 | long int val_retry = -1;
|
---|
123 | char errbuf[SH_ERRBUF_SIZE];
|
---|
124 | errno = 0;
|
---|
125 |
|
---|
126 | SL_ENTER(_("retry_sigaction"));
|
---|
127 |
|
---|
128 | do {
|
---|
129 | val_retry = sigaction(signum, act, oldact);
|
---|
130 | } while (val_retry < 0 && errno == EINTR);
|
---|
131 |
|
---|
132 | error = errno;
|
---|
133 | if (val_retry < 0) {
|
---|
134 | sh_error_handle ((-1), file, line, error, MSG_ERR_SIGACT,
|
---|
135 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
136 | (long) signum );
|
---|
137 | }
|
---|
138 | errno = error;
|
---|
139 | SL_RETURN(val_retry, _("retry_sigaction"));
|
---|
140 | }
|
---|
141 |
|
---|
142 | static struct sh_sockaddr bind_addr;
|
---|
143 | static int use_bind_addr = 0;
|
---|
144 |
|
---|
145 | int sh_calls_set_bind_addr (const char * str)
|
---|
146 | {
|
---|
147 | static int reject = 0;
|
---|
148 |
|
---|
149 | if (reject == 1)
|
---|
150 | return (0);
|
---|
151 |
|
---|
152 | if (sh.flag.opts == S_TRUE)
|
---|
153 | reject = 1;
|
---|
154 |
|
---|
155 | if (0 == sh_ipvx_aton(str, &bind_addr))
|
---|
156 | return -1;
|
---|
157 |
|
---|
158 | use_bind_addr = 1;
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | long int retry_connect(const char * file, int line, int sockfd,
|
---|
164 | struct sockaddr *serv_addr, int addrlen)
|
---|
165 | {
|
---|
166 | int error;
|
---|
167 | int err_bind = 0;
|
---|
168 | long int val_retry = 0;
|
---|
169 | char errbuf[SH_ERRBUF_SIZE];
|
---|
170 |
|
---|
171 | SL_ENTER(_("retry_connect"));
|
---|
172 |
|
---|
173 | errno = 0;
|
---|
174 |
|
---|
175 | if (use_bind_addr)
|
---|
176 | {
|
---|
177 | int slen = SH_SS_LEN(bind_addr);
|
---|
178 | struct sockaddr *b_addr = sh_ipvx_sockaddr_cast(&bind_addr);
|
---|
179 |
|
---|
180 | if (bind_addr.ss_family == AF_INET)
|
---|
181 | b_addr->sa_family = AF_INET;
|
---|
182 | #if defined(USE_IPVX)
|
---|
183 | else
|
---|
184 | b_addr->sa_family = AF_INET6;
|
---|
185 | #endif
|
---|
186 | val_retry = bind(sockfd, b_addr, slen);
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (val_retry == 0)
|
---|
190 | {
|
---|
191 | do {
|
---|
192 | val_retry = connect(sockfd, serv_addr, addrlen);
|
---|
193 | } while (val_retry < 0 && (errno == EINTR || errno == EINPROGRESS));
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | err_bind = 1;
|
---|
198 | }
|
---|
199 |
|
---|
200 | error = errno;
|
---|
201 | if (val_retry != 0) {
|
---|
202 | long eport = 0;
|
---|
203 | char eaddr[SH_IP_BUF];
|
---|
204 | char emesg[SH_BUFSIZE];
|
---|
205 | struct sockaddr *err_addr = serv_addr;
|
---|
206 | struct sh_sockaddr ss;
|
---|
207 |
|
---|
208 | if (err_bind)
|
---|
209 | err_addr = sh_ipvx_sockaddr_cast(&bind_addr);
|
---|
210 |
|
---|
211 | sh_ipvx_save(&ss, err_addr->sa_family, err_addr);
|
---|
212 | sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
|
---|
213 |
|
---|
214 | if (err_addr->sa_family == AF_INET)
|
---|
215 | eport = (long) ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
|
---|
216 | #if defined(USE_IPVX)
|
---|
217 | else
|
---|
218 | eport = (long) ntohs(((struct sockaddr_in6 *)serv_addr)->sin6_port);
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | sl_strlcpy(emesg, sh_error_message(error, errbuf, sizeof(errbuf)), sizeof(emesg));
|
---|
222 | sl_strlcat(emesg,
|
---|
223 | (err_addr->sa_family == AF_INET) ? _(", AF_INET") : _(", AF_INET6"),
|
---|
224 | sizeof(emesg));
|
---|
225 |
|
---|
226 | sl_strlcat(emesg,
|
---|
227 | (err_bind) ? _(", bind") : _(", connect"),
|
---|
228 | sizeof(emesg));
|
---|
229 |
|
---|
230 | sh_error_handle ((-1), file, line, error, MSG_ERR_CONNECT,
|
---|
231 | emesg,
|
---|
232 | (long) sockfd, eport, eaddr);
|
---|
233 | }
|
---|
234 | errno = error;
|
---|
235 | SL_RETURN(val_retry, _("retry_connect"));
|
---|
236 | }
|
---|
237 |
|
---|
238 | long int retry_accept(const char * file, int line, int fd,
|
---|
239 | struct sh_sockaddr *serv_addr, int * addrlen)
|
---|
240 | {
|
---|
241 | int error;
|
---|
242 | long int val_retry = -1;
|
---|
243 | char errbuf[SH_ERRBUF_SIZE];
|
---|
244 | struct sockaddr_storage ss;
|
---|
245 |
|
---|
246 | ACCEPT_TYPE_ARG3 my_addrlen = sizeof(ss);
|
---|
247 |
|
---|
248 | errno = 0;
|
---|
249 |
|
---|
250 | SL_ENTER(_("retry_accept"));
|
---|
251 |
|
---|
252 | do {
|
---|
253 | val_retry = accept(fd, (struct sockaddr *)&ss, &my_addrlen);
|
---|
254 | } while (val_retry < 0 && errno == EINTR);
|
---|
255 |
|
---|
256 | error = errno;
|
---|
257 | if (val_retry < 0) {
|
---|
258 | sh_error_handle ((-1), file, line, error, MSG_ERR_ACCEPT,
|
---|
259 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
260 | (long) fd );
|
---|
261 | }
|
---|
262 | errno = error;
|
---|
263 |
|
---|
264 | if (flag_err_debug == S_TRUE)
|
---|
265 | {
|
---|
266 | char ipbuf[SH_IP_BUF];
|
---|
267 | char buf[SH_BUFSIZE];
|
---|
268 | #if defined(USE_IPVX)
|
---|
269 | sl_strlcpy(errbuf, _("Address family: "), sizeof(errbuf));
|
---|
270 | sl_strlcat(errbuf,
|
---|
271 | (ss.ss_family == AF_INET6) ? _("AF_INET6") : _("AF_INET"),
|
---|
272 | sizeof(errbuf));
|
---|
273 | getnameinfo((struct sockaddr *)&ss, my_addrlen,
|
---|
274 | ipbuf, sizeof(ipbuf), NULL, 0, NI_NUMERICHOST);
|
---|
275 | #else
|
---|
276 | struct sockaddr_in sa;
|
---|
277 | char * p;
|
---|
278 | memcpy(&(sa), (struct sockaddr_in*)&ss, sizeof(struct sockaddr_in));
|
---|
279 | p = inet_ntoa(sa.sin_addr);
|
---|
280 | sl_strlcpy(ipbuf, p, sizeof(ipbuf));
|
---|
281 | sl_strlcpy(errbuf, _("Address family: AF_INET"), sizeof(errbuf));
|
---|
282 | #endif
|
---|
283 | sl_strlcpy(buf, _("Address: "), sizeof(buf));
|
---|
284 | sl_strlcat(buf, ipbuf, sizeof(buf));
|
---|
285 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
286 | errbuf, _("retry_accept"));
|
---|
287 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
288 | buf, _("retry_accept"));
|
---|
289 | }
|
---|
290 |
|
---|
291 | sh_ipvx_save(serv_addr, ss.ss_family, (struct sockaddr *) &ss);
|
---|
292 |
|
---|
293 | if (flag_err_debug == S_TRUE)
|
---|
294 | {
|
---|
295 | char ipbuf[SH_IP_BUF];
|
---|
296 | char ipbuf2[SH_IP_BUF];
|
---|
297 | char buf[SH_BUFSIZE];
|
---|
298 | #if defined(USE_IPVX)
|
---|
299 | int len = (serv_addr->ss_family == AF_INET) ?
|
---|
300 | sizeof(struct sockaddr_in) :
|
---|
301 | sizeof(struct sockaddr_in6);
|
---|
302 | getnameinfo(sh_ipvx_sockaddr_cast(serv_addr), len,
|
---|
303 | ipbuf2, sizeof(ipbuf2), NULL, 0, NI_NUMERICHOST);
|
---|
304 | #else
|
---|
305 | char * p = inet_ntoa((serv_addr->sin).sin_addr);
|
---|
306 | sl_strlcpy(ipbuf2, p, sizeof(ipbuf2));
|
---|
307 | #endif
|
---|
308 | sh_ipvx_ntoa (ipbuf, sizeof(ipbuf), serv_addr);
|
---|
309 | sl_snprintf(buf, sizeof(buf), _("Address: %s / %s"),
|
---|
310 | ipbuf, ipbuf2);
|
---|
311 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
312 | buf, _("retry_accept"));
|
---|
313 | }
|
---|
314 |
|
---|
315 | *addrlen = (int) my_addrlen;
|
---|
316 | SL_RETURN(val_retry, _("retry_accept"));
|
---|
317 | }
|
---|
318 |
|
---|
319 | static int sh_enable_use_sub = 0;
|
---|
320 |
|
---|
321 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
322 | #if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
|
---|
323 | static int sh_use_sub = 0;
|
---|
324 | #else
|
---|
325 | static int sh_use_sub = 1;
|
---|
326 | #endif
|
---|
327 | #else
|
---|
328 | static int sh_use_sub = 0;
|
---|
329 | #endif
|
---|
330 |
|
---|
331 | void sh_calls_enable_sub()
|
---|
332 | {
|
---|
333 | sh_enable_use_sub = 1;
|
---|
334 | return;
|
---|
335 | }
|
---|
336 |
|
---|
337 | int sh_calls_set_sub (const char * str)
|
---|
338 | {
|
---|
339 | int ret = sh_util_flagval(str, &sh_use_sub);
|
---|
340 |
|
---|
341 | if ((ret == 0) && (!sh_use_sub))
|
---|
342 | {
|
---|
343 | sh_kill_sub();
|
---|
344 | }
|
---|
345 | return ret;
|
---|
346 | }
|
---|
347 |
|
---|
348 | long int retry_lstat_ns(const char * file, int line,
|
---|
349 | const char *file_name, struct stat *buf)
|
---|
350 | {
|
---|
351 | int error;
|
---|
352 | long int val_retry = -1;
|
---|
353 | char errbuf[SH_ERRBUF_SIZE];
|
---|
354 |
|
---|
355 | SL_ENTER(_("retry_lstat_ns"));
|
---|
356 |
|
---|
357 | do {
|
---|
358 | val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
|
---|
359 | } while (val_retry < 0 && errno == EINTR);
|
---|
360 |
|
---|
361 | error = errno;
|
---|
362 | if (val_retry < 0) {
|
---|
363 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
364 | sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT,
|
---|
365 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
366 | file_name );
|
---|
367 | }
|
---|
368 | errno = error;
|
---|
369 |
|
---|
370 | SL_RETURN(val_retry, _("retry_lstat_ns"));
|
---|
371 | }
|
---|
372 |
|
---|
373 | long int retry_lstat(const char * file, int line,
|
---|
374 | const char *file_name, struct stat *buf)
|
---|
375 | {
|
---|
376 | int error;
|
---|
377 | long int val_retry = -1;
|
---|
378 | char errbuf[SH_ERRBUF_SIZE];
|
---|
379 |
|
---|
380 | SL_ENTER(_("retry_lstat"));
|
---|
381 |
|
---|
382 | if (sh_use_sub && sh_enable_use_sub)
|
---|
383 | {
|
---|
384 | val_retry = sh_sub_lstat (file_name, buf);
|
---|
385 | }
|
---|
386 | else
|
---|
387 | {
|
---|
388 | do {
|
---|
389 | val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
|
---|
390 | } while (val_retry < 0 && errno == EINTR);
|
---|
391 | }
|
---|
392 |
|
---|
393 | error = errno;
|
---|
394 | if (val_retry < 0) {
|
---|
395 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
396 | sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT,
|
---|
397 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
398 | file_name );
|
---|
399 | }
|
---|
400 | errno = error;
|
---|
401 |
|
---|
402 | SL_RETURN(val_retry, _("retry_lstat"));
|
---|
403 | }
|
---|
404 |
|
---|
405 | long int retry_stat(const char * file, int line,
|
---|
406 | const char *file_name, struct stat *buf)
|
---|
407 | {
|
---|
408 | int error;
|
---|
409 | long int val_retry = -1;
|
---|
410 | char errbuf[SH_ERRBUF_SIZE];
|
---|
411 |
|
---|
412 | SL_ENTER(_("retry_stat"));
|
---|
413 |
|
---|
414 | if (sh_use_sub && sh_enable_use_sub)
|
---|
415 | {
|
---|
416 | val_retry = sh_sub_stat (file_name, buf);
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | do {
|
---|
421 | val_retry = stat (file_name, buf);
|
---|
422 | } while (val_retry < 0 && errno == EINTR);
|
---|
423 | }
|
---|
424 |
|
---|
425 | error = errno;
|
---|
426 | if (val_retry < 0) {
|
---|
427 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
428 | sh_error_handle ((-1), file, line, error, MSG_ERR_STAT,
|
---|
429 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
430 | file_name );
|
---|
431 | }
|
---|
432 | errno = error;
|
---|
433 |
|
---|
434 | SL_RETURN(val_retry, _("retry_stat"));
|
---|
435 | }
|
---|
436 |
|
---|
437 | long int retry_fstat(const char * file, int line, int filed, struct stat *buf)
|
---|
438 | {
|
---|
439 | int error;
|
---|
440 | long int val_retry = -1;
|
---|
441 | char errbuf[SH_ERRBUF_SIZE];
|
---|
442 |
|
---|
443 | SL_ENTER(_("retry_fstat"));
|
---|
444 |
|
---|
445 | do {
|
---|
446 | val_retry = fstat (filed, buf);
|
---|
447 | } while (val_retry < 0 && errno == EINTR);
|
---|
448 | error = errno;
|
---|
449 | if (val_retry < 0) {
|
---|
450 | sh_error_handle ((-1), file, line, error, MSG_ERR_FSTAT,
|
---|
451 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
452 | (long) filed );
|
---|
453 | }
|
---|
454 | errno = error;
|
---|
455 | SL_RETURN(val_retry, _("retry_fstat"));
|
---|
456 | }
|
---|
457 |
|
---|
458 | long int retry_fcntl(const char * file, int line, int fd, int cmd, long arg)
|
---|
459 | {
|
---|
460 | int error;
|
---|
461 | long int val_retry = -1;
|
---|
462 | char errbuf[SH_ERRBUF_SIZE];
|
---|
463 | errno = 0;
|
---|
464 |
|
---|
465 | SL_ENTER(_("retry_fcntl"));
|
---|
466 |
|
---|
467 | if (cmd == F_GETFD || cmd == F_GETFL)
|
---|
468 | {
|
---|
469 | do {
|
---|
470 | val_retry = fcntl(fd, cmd);
|
---|
471 | } while (val_retry < 0 && errno == EINTR);
|
---|
472 | }
|
---|
473 | else
|
---|
474 | {
|
---|
475 | do {
|
---|
476 | val_retry = fcntl(fd, cmd, arg);
|
---|
477 | } while (val_retry < 0 && errno == EINTR);
|
---|
478 | }
|
---|
479 | error = errno;
|
---|
480 | if (val_retry < 0) {
|
---|
481 | sh_error_handle ((-1), file, line, error, MSG_ERR_FCNTL,
|
---|
482 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
483 | (long) fd, (long) cmd, arg );
|
---|
484 | }
|
---|
485 | errno = error;
|
---|
486 | SL_RETURN(val_retry, _("retry_fcntl"));
|
---|
487 | }
|
---|
488 |
|
---|
489 | long int retry_msleep (int sec, int millisec)
|
---|
490 | {
|
---|
491 | int result = 0;
|
---|
492 | #if defined(HAVE_NANOSLEEP)
|
---|
493 | struct timespec req, rem;
|
---|
494 | #endif
|
---|
495 |
|
---|
496 | errno = 0;
|
---|
497 | if (millisec > 999) millisec = 999;
|
---|
498 | if (millisec < 0) millisec = 0;
|
---|
499 | if (sec < 0) sec = 0;
|
---|
500 |
|
---|
501 | #if defined(HAVE_NANOSLEEP)
|
---|
502 | /*@-usedef@*/
|
---|
503 | req.tv_sec = sec; rem.tv_sec = 0;
|
---|
504 | req.tv_nsec = millisec * 1000000; rem.tv_nsec = 0;
|
---|
505 | /*@+usedef@*/
|
---|
506 | do {
|
---|
507 | result = /*@-unrecog@*/nanosleep(&req, &rem)/*@+unrecog@*/;
|
---|
508 |
|
---|
509 | req.tv_sec = rem.tv_sec; rem.tv_sec = 0;
|
---|
510 | req.tv_nsec = rem.tv_nsec; rem.tv_nsec = 0;
|
---|
511 |
|
---|
512 | } while ((result == -1) && (errno == EINTR));
|
---|
513 | #else
|
---|
514 | if (sec > 0)
|
---|
515 | sleep (sec); /* nanosleep not available */
|
---|
516 | else {
|
---|
517 | #ifdef HAVE_USLEEP
|
---|
518 | if (millisec > 0)
|
---|
519 | usleep(1000 * millisec);
|
---|
520 | #else
|
---|
521 | if (millisec > 0)
|
---|
522 | sleep (1);
|
---|
523 | #endif
|
---|
524 | }
|
---|
525 | #endif
|
---|
526 | return result;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /***************************************************
|
---|
530 | *
|
---|
531 | * Audit these functions.
|
---|
532 | *
|
---|
533 | ***************************************************/
|
---|
534 |
|
---|
535 | long int retry_aud_execve (const char * file, int line,
|
---|
536 | const char *dateiname, char * argv[],
|
---|
537 | char * envp[])
|
---|
538 | {
|
---|
539 | uid_t a = geteuid();
|
---|
540 | gid_t b = getegid();
|
---|
541 | int i;
|
---|
542 | int error;
|
---|
543 | char errbuf[SH_ERRBUF_SIZE];
|
---|
544 |
|
---|
545 | SL_ENTER(_("retry_aud_execve"));
|
---|
546 |
|
---|
547 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXEC) != 0)
|
---|
548 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXEC,
|
---|
549 | dateiname, (long) a, (long) b );
|
---|
550 | do {
|
---|
551 | i = execve(dateiname, argv, envp);
|
---|
552 | } while (i < 0 && errno == EINTR);
|
---|
553 |
|
---|
554 | error = errno;
|
---|
555 | if (i < 0) {
|
---|
556 | sh_error_handle ((-1), file, line, error, MSG_ERR_EXEC,
|
---|
557 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
558 | dateiname, (long) a, (long) b );
|
---|
559 | }
|
---|
560 | errno = error;
|
---|
561 | SL_RETURN(i, _("retry_aud_execve"));
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|
565 | long int retry_aud_utime (const char * file, int line,
|
---|
566 | char * path, struct utimbuf *buf)
|
---|
567 | {
|
---|
568 | long int val_return;
|
---|
569 | int error;
|
---|
570 | char errbuf[SH_ERRBUF_SIZE];
|
---|
571 | errno = 0;
|
---|
572 |
|
---|
573 | SL_ENTER(_("retry_aud_utime"));
|
---|
574 |
|
---|
575 | do {
|
---|
576 | val_return = utime (path, buf);
|
---|
577 | } while (val_return < 0 && errno == EINTR);
|
---|
578 |
|
---|
579 | error = errno;
|
---|
580 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UTIME) != 0)
|
---|
581 | sh_error_handle ((-1), file, line, 0, MSG_AUD_UTIME,
|
---|
582 | path,
|
---|
583 | (unsigned long) buf->actime,
|
---|
584 | (unsigned long) buf->modtime);
|
---|
585 | if (val_return < 0) {
|
---|
586 | sh_error_handle ((-1), file, line, error, MSG_ERR_UTIME,
|
---|
587 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
588 | path,
|
---|
589 | (unsigned long) buf->actime,
|
---|
590 | (unsigned long) buf->modtime);
|
---|
591 | }
|
---|
592 | errno = error;
|
---|
593 | SL_RETURN(val_return, _("retry_aud_utime"));
|
---|
594 | }
|
---|
595 |
|
---|
596 | long int retry_aud_unlink (const char * file, int line,
|
---|
597 | char * path)
|
---|
598 | {
|
---|
599 | long int val_return;
|
---|
600 | int error;
|
---|
601 | char errbuf[SH_ERRBUF_SIZE];
|
---|
602 | errno = 0;
|
---|
603 |
|
---|
604 | SL_ENTER(_("retry_aud_unlink"));
|
---|
605 |
|
---|
606 | do {
|
---|
607 | val_return = unlink (path);
|
---|
608 | } while (val_return < 0 && errno == EINTR);
|
---|
609 |
|
---|
610 | error = errno;
|
---|
611 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_UNLINK) != 0)
|
---|
612 | sh_error_handle ((-1), file, line, 0, MSG_AUD_UNLINK,
|
---|
613 | path);
|
---|
614 | if (val_return < 0) {
|
---|
615 | sh_error_handle ((-1), file, line, error, MSG_ERR_UNLINK,
|
---|
616 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
617 | path);
|
---|
618 | }
|
---|
619 | errno = error;
|
---|
620 | SL_RETURN(val_return, _("retry_aud_unlink"));
|
---|
621 | }
|
---|
622 |
|
---|
623 | long int retry_aud_dup2 (const char * file, int line,
|
---|
624 | int fd, int fd2)
|
---|
625 | {
|
---|
626 | long int val_return;
|
---|
627 | int error;
|
---|
628 | char errbuf[SH_ERRBUF_SIZE];
|
---|
629 | errno = 0;
|
---|
630 |
|
---|
631 | SL_ENTER(_("retry_aud_dup2"));
|
---|
632 |
|
---|
633 | do {
|
---|
634 | val_return = dup2 (fd, fd2);
|
---|
635 | } while (val_return < 0 && errno == EINTR);
|
---|
636 |
|
---|
637 | error = errno;
|
---|
638 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
|
---|
639 | sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
|
---|
640 | (long) fd, val_return);
|
---|
641 | if (val_return < 0) {
|
---|
642 | sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
|
---|
643 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
644 | (long) fd, val_return);
|
---|
645 | }
|
---|
646 | errno = error;
|
---|
647 | SL_RETURN(val_return, _("retry_aud_dup2"));
|
---|
648 | }
|
---|
649 |
|
---|
650 | long int retry_aud_dup (const char * file, int line,
|
---|
651 | int fd)
|
---|
652 | {
|
---|
653 | long int val_return;
|
---|
654 | int error;
|
---|
655 | char errbuf[SH_ERRBUF_SIZE];
|
---|
656 | errno = 0;
|
---|
657 |
|
---|
658 | SL_ENTER(_("retry_aud_dup"));
|
---|
659 |
|
---|
660 | do {
|
---|
661 | val_return = dup (fd);
|
---|
662 | } while (val_return < 0 && errno == EINTR);
|
---|
663 | error = errno;
|
---|
664 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_DUP) != 0)
|
---|
665 | sh_error_handle ((-1), file, line, 0, MSG_AUD_DUP,
|
---|
666 | (long) fd, val_return);
|
---|
667 | if (val_return < 0) {
|
---|
668 | sh_error_handle ((-1), file, line, error, MSG_ERR_DUP,
|
---|
669 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
670 | (long) fd, val_return);
|
---|
671 | }
|
---|
672 | errno = error;
|
---|
673 | SL_RETURN(val_return, _("retry_aud_dup"));
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 |
|
---|
678 | long int retry_aud_chdir (const char * file, int line,
|
---|
679 | const char *path)
|
---|
680 | {
|
---|
681 | long int val_return;
|
---|
682 | int error = 0;
|
---|
683 | char errbuf[SH_ERRBUF_SIZE];
|
---|
684 | errno = 0;
|
---|
685 |
|
---|
686 | SL_ENTER(_("retry_aud_chdir"));
|
---|
687 |
|
---|
688 | do {
|
---|
689 | val_return = chdir (path);
|
---|
690 | } while (val_return < 0 && errno == EINTR);
|
---|
691 |
|
---|
692 | error = errno;
|
---|
693 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_CHDIR) != 0)
|
---|
694 | sh_error_handle ((-1), file, line, 0, MSG_AUD_CHDIR,
|
---|
695 | path);
|
---|
696 | if (val_return < 0) {
|
---|
697 | sh_error_handle ((-1), file, line, error, MSG_ERR_CHDIR,
|
---|
698 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
699 | path);
|
---|
700 | }
|
---|
701 | errno = error;
|
---|
702 | SL_RETURN(val_return, _("retry_aud_chdir"));
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 | long int aud_open_noatime (const char * file, int line, int privs,
|
---|
707 | const char *pathname, int flags, mode_t mode,
|
---|
708 | int * o_noatime)
|
---|
709 | {
|
---|
710 | long int val_return;
|
---|
711 | int error;
|
---|
712 | char errbuf[SH_ERRBUF_SIZE];
|
---|
713 |
|
---|
714 | SL_ENTER(_("aud_open"));
|
---|
715 |
|
---|
716 | #ifdef USE_SUID
|
---|
717 | if (0 == strcmp(pathname, "/usr/bin/sudo"))
|
---|
718 | {
|
---|
719 | uid_t ruid; uid_t euid; uid_t suid;
|
---|
720 | getresuid(&ruid, &euid, &suid);
|
---|
721 | }
|
---|
722 | if (privs == SL_YESPRIV)
|
---|
723 | sl_set_suid();
|
---|
724 | #else
|
---|
725 | /*@-noeffect@*/
|
---|
726 | (void) privs; /* fix compiler warning */
|
---|
727 | /*@+noeffect@*/
|
---|
728 | #endif
|
---|
729 |
|
---|
730 | val_return = open (pathname, *o_noatime|flags, mode);
|
---|
731 |
|
---|
732 | #ifdef USE_SUID
|
---|
733 | if (privs == SL_YESPRIV)
|
---|
734 | sl_unset_suid();
|
---|
735 | #endif
|
---|
736 |
|
---|
737 | if ((val_return < 0) && (*o_noatime != 0))
|
---|
738 | {
|
---|
739 | /* cppcheck-suppress resourceLeak */
|
---|
740 | val_return = open (pathname, flags, mode);
|
---|
741 | if (val_return >= 0)
|
---|
742 | *o_noatime = 0;
|
---|
743 | }
|
---|
744 | error = errno;
|
---|
745 |
|
---|
746 | if (val_return < 0)
|
---|
747 | {
|
---|
748 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
|
---|
752 | {
|
---|
753 | sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
|
---|
754 | pathname, (long) flags, (long) mode, val_return);
|
---|
755 | }
|
---|
756 | if (val_return < 0) {
|
---|
757 | sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
|
---|
758 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
759 | pathname, (long) flags, (long) mode, val_return);
|
---|
760 | }
|
---|
761 | errno = error;
|
---|
762 | SL_RETURN(val_return, _("aud_open"));
|
---|
763 | }
|
---|
764 |
|
---|
765 | long int aud_open (const char * file, int line, int privs,
|
---|
766 | const char *pathname, int flags, mode_t mode)
|
---|
767 | {
|
---|
768 | long int val_return;
|
---|
769 | int error;
|
---|
770 | char errbuf[SH_ERRBUF_SIZE];
|
---|
771 |
|
---|
772 | SL_ENTER(_("aud_open"));
|
---|
773 |
|
---|
774 | #ifdef USE_SUID
|
---|
775 | if (privs == SL_YESPRIV)
|
---|
776 | sl_set_suid();
|
---|
777 | #else
|
---|
778 | /*@-noeffect@*/
|
---|
779 | (void) privs; /* fix compiler warning */
|
---|
780 | /*@+noeffect@*/
|
---|
781 | #endif
|
---|
782 |
|
---|
783 | val_return = open (pathname, flags, mode);
|
---|
784 |
|
---|
785 | #ifdef USE_SUID
|
---|
786 | if (privs == SL_YESPRIV)
|
---|
787 | sl_unset_suid();
|
---|
788 | #endif
|
---|
789 |
|
---|
790 | error = errno;
|
---|
791 |
|
---|
792 | if (val_return < 0)
|
---|
793 | {
|
---|
794 | (void) sh_error_message(error, aud_err_message, 64);
|
---|
795 | }
|
---|
796 |
|
---|
797 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_OPEN) != 0)
|
---|
798 | {
|
---|
799 | sh_error_handle ((-1), file, line, 0, MSG_AUD_OPEN,
|
---|
800 | pathname, (long) flags, (long) mode, val_return);
|
---|
801 | }
|
---|
802 | if (val_return < 0) {
|
---|
803 | sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN,
|
---|
804 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
805 | pathname, (long) flags, (long) mode, val_return);
|
---|
806 | }
|
---|
807 | errno = error;
|
---|
808 | SL_RETURN(val_return, _("aud_open"));
|
---|
809 | }
|
---|
810 |
|
---|
811 | long int aud_kill (const char * file, int line, pid_t pid, int sig)
|
---|
812 | {
|
---|
813 | int myerror;
|
---|
814 | long int val_return = kill (pid, sig);
|
---|
815 | char errbuf[SH_ERRBUF_SIZE];
|
---|
816 | myerror = errno;
|
---|
817 |
|
---|
818 | SL_ENTER(_("aud_kill"));
|
---|
819 |
|
---|
820 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_KILL) != 0)
|
---|
821 | sh_error_handle ((-1), file, line, 0, MSG_AUD_KILL,
|
---|
822 | (long) pid, (long) sig);
|
---|
823 | if (val_return < 0) {
|
---|
824 | sh_error_handle ((-1), file, line, myerror, MSG_ERR_KILL,
|
---|
825 | sh_error_message(myerror, errbuf, sizeof(errbuf)),
|
---|
826 | (long) pid, (long) sig);
|
---|
827 | }
|
---|
828 | errno = myerror;
|
---|
829 | SL_RETURN(val_return, _("aud_kill"));
|
---|
830 | }
|
---|
831 |
|
---|
832 | /*@noreturn@*/
|
---|
833 | void aud_exit (const char * file, int line, int exitval)
|
---|
834 | {
|
---|
835 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
|
---|
836 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
|
---|
837 | (long) exitval);
|
---|
838 |
|
---|
839 | SL_ENTER(_("aud_exit"));
|
---|
840 |
|
---|
841 | sh.flag.exit = exitval;
|
---|
842 | exit(exitval);
|
---|
843 | }
|
---|
844 |
|
---|
845 | /*@noreturn@*/
|
---|
846 | void aud__exit (const char * file, int line, int exitval)
|
---|
847 | {
|
---|
848 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
|
---|
849 | sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
|
---|
850 | (long) exitval);
|
---|
851 |
|
---|
852 | SL_ENTER(_("aud__exit"));
|
---|
853 |
|
---|
854 | sh.flag.exit = exitval;
|
---|
855 | _exit(exitval);
|
---|
856 | }
|
---|
857 |
|
---|
858 | pid_t aud_fork (const char * file, int line)
|
---|
859 | {
|
---|
860 | int error;
|
---|
861 | pid_t i = fork();
|
---|
862 | char errbuf[SH_ERRBUF_SIZE];
|
---|
863 |
|
---|
864 | error = errno;
|
---|
865 | SL_ENTER(_("aud_fork"));
|
---|
866 |
|
---|
867 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_FORK) != 0 && (i > 0))
|
---|
868 | sh_error_handle ((-1), file, line, 0, MSG_AUD_FORK,
|
---|
869 | (long) i);
|
---|
870 | if (i == (pid_t) -1) {
|
---|
871 | sh_error_handle ((-1), file, line, error, MSG_ERR_FORK,
|
---|
872 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
873 | (long) i);
|
---|
874 | }
|
---|
875 | errno = error;
|
---|
876 | SL_RETURN(i, _("aud_fork"));
|
---|
877 | }
|
---|
878 |
|
---|
879 | int aud_setuid (const char * file, int line, uid_t uid)
|
---|
880 | {
|
---|
881 | int error = 0;
|
---|
882 | int i = 0;
|
---|
883 | char errbuf[SH_ERRBUF_SIZE];
|
---|
884 |
|
---|
885 | SL_ENTER(_("aud_setuid"));
|
---|
886 |
|
---|
887 | if (uid != (uid_t) 0) {
|
---|
888 | i = setuid(uid);
|
---|
889 | error = errno;
|
---|
890 | }
|
---|
891 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETUID) != 0)
|
---|
892 | sh_error_handle ((-1), file, line, 0, MSG_AUD_SETUID,
|
---|
893 | (long) uid);
|
---|
894 | if (uid == (uid_t) 0) {
|
---|
895 | i = setuid(uid);
|
---|
896 | error = errno;
|
---|
897 | }
|
---|
898 | if (i < 0) {
|
---|
899 | sh_error_handle ((-1), file, line, error, MSG_ERR_SETUID,
|
---|
900 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
901 | (long) uid);
|
---|
902 | }
|
---|
903 | errno = error;
|
---|
904 | SL_RETURN(i, _("aud_setuid"));
|
---|
905 | }
|
---|
906 |
|
---|
907 | int aud_setgid (const char * file, int line, gid_t gid)
|
---|
908 | {
|
---|
909 | int error = 0;
|
---|
910 | int i = 0;
|
---|
911 | char errbuf[SH_ERRBUF_SIZE];
|
---|
912 |
|
---|
913 | SL_ENTER(_("aud_setgid"));
|
---|
914 |
|
---|
915 | if (gid != (gid_t) 0) {
|
---|
916 | i = setgid(gid);
|
---|
917 | error = errno;
|
---|
918 | }
|
---|
919 |
|
---|
920 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_SETGID) != 0)
|
---|
921 | sh_error_handle ((-1), file, line, 0, MSG_AUD_SETGID,
|
---|
922 | (long) gid);
|
---|
923 | if (gid == (gid_t) 0) {
|
---|
924 | i = setgid(gid);
|
---|
925 | error = errno;
|
---|
926 | }
|
---|
927 | if (i < 0) {
|
---|
928 | sh_error_handle ((-1), file, line, error, MSG_ERR_SETGID,
|
---|
929 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
930 | (long) gid);
|
---|
931 | }
|
---|
932 | errno = error;
|
---|
933 | SL_RETURN(i, _("aud_setgid"));
|
---|
934 | }
|
---|
935 |
|
---|
936 | int aud_pipe (const char * file, int line, int * modus)
|
---|
937 | {
|
---|
938 | int error;
|
---|
939 | int i = pipe (modus);
|
---|
940 | char errbuf[SH_ERRBUF_SIZE];
|
---|
941 |
|
---|
942 | SL_ENTER(_("aud_pipe"));
|
---|
943 |
|
---|
944 | error = errno;
|
---|
945 | if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_PIPE) != 0)
|
---|
946 | {
|
---|
947 | if (i < 0)
|
---|
948 | sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
|
---|
949 | (long) 0, (long) 0);
|
---|
950 | else
|
---|
951 | sh_error_handle ((-1), file, line, 0, MSG_AUD_PIPE,
|
---|
952 | (long) modus[0], (long) modus[1]);
|
---|
953 | }
|
---|
954 | if (i < 0) {
|
---|
955 | if (i < 0)
|
---|
956 | sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
|
---|
957 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
958 | (long) 0, (long) 0);
|
---|
959 | else
|
---|
960 | sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE,
|
---|
961 | sh_error_message(error, errbuf, sizeof(errbuf)),
|
---|
962 | (long) modus[0], (long) modus[1]);
|
---|
963 | }
|
---|
964 | SL_RETURN(i, _("aud_pipe"));
|
---|
965 | }
|
---|