Debian Codegraph @

src/util.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2002, 2004-2006, 2010-2011, 2013, 2016, 2019-2023
5 * Rami Lehti, Pablo Virolainen, Mike Markley,
6 * Richard van den Berg, Hannes von Haugwitz
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
29 #include <syslog.h>
30 #include <stdbool.h>
31 #include <pthread.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <signal.h>
35 #include <time.h>
36 #include "url.h"
37 /*for locale support*/
38 #include "locale-aide.h"
39 /*for locale support*/
40
41
42 #ifndef MAXHOSTNAMELEN
43 #define MAXHOSTNAMELEN 256
44 #endif
45
46 #include "log.h"
47 #include "db_config.h"
48 #include "util.h"
49 #include "errorcodes.h"
50
51 #define URL_UNSAFE " <>\"#%{}|\\^~[]`@:\033',"
52 #define ISPRINT(c) (isascii(c) && isprint(c))
53
54 pthread_mutex_t stderr_mutex = PTHREAD_MUTEX_INITIALIZER;
55 bool stderr_erase_line = false;
56
57 void stderr_msg(const char* format, ...) {
58 pthread_mutex_lock(&stderr_mutex);
59 if (stderr_erase_line) {
60 fprintf(stderr, "\33[2K");
61 }
62 va_list ap;
63 va_start(ap, format);
64 vfprintf(stderr, format, ap);
65 va_end(ap);
66 pthread_mutex_unlock(&stderr_mutex);
67 }
68
69 void vstderr_prefix_line(const char* prefix, const char* format, va_list ap) {
70 pthread_mutex_lock(&stderr_mutex);
71 if (stderr_erase_line) {
72 fprintf(stderr, "\33[2K");
73 }
74 fprintf(stderr, "%s: ", prefix);
75 vfprintf(stderr, format, ap);
76 fprintf(stderr, "\n");
77 pthread_mutex_unlock(&stderr_mutex);
78 }
79
80 void stderr_set_line_erasure(bool erase_line) {
81 pthread_mutex_lock(&stderr_mutex);
82 if (stderr_erase_line) {
83 fprintf(stderr, "\33[2K");
84 }
85 stderr_erase_line = erase_line;
86 pthread_mutex_unlock(&stderr_mutex);
87 }
88
89 const char* btoa(bool b) {
90 return b?"true":"false";
91 }
92
93 void* checked_malloc(size_t size) {
94 void * p = malloc(size);
95 if (p == NULL) {
96 log_msg(LOG_LEVEL_ERROR, "malloc: failed to allocate %lu bytes of memory", (unsigned long) size);
97 exit(MEMORY_ALLOCATION_FAILURE);
98 }
99 return p;
100 }
101 void* checked_calloc(size_t nmemb, size_t size) {
102 void * p = calloc(nmemb, size);
103 if (p == NULL) {
104 log_msg(LOG_LEVEL_ERROR, "calloc: failed to allocate %lu bytes of memory", (unsigned long) size);
105 exit(MEMORY_ALLOCATION_FAILURE);
106 }
107 return p;
108 }
109 void* checked_strdup(const char *s) {
110 void * p = strdup(s);
111 if (p == NULL) {
112 log_msg(LOG_LEVEL_ERROR, "strdup: failed to allocate memory");
113 exit(MEMORY_ALLOCATION_FAILURE);
114 }
115 return p;
116 }
117 void* checked_strndup(const char *s, size_t size) {
118 void * p = strndup(s, size);
119 if (p == NULL) {
120 log_msg(LOG_LEVEL_ERROR, "strndup: failed to allocate memory");
121 exit(MEMORY_ALLOCATION_FAILURE);
122 }
123 return p;
124 }
125 void* checked_realloc(void *ptr, size_t size) {
126 void * p = realloc(ptr,size);
127 if (p == NULL) {
128 log_msg(LOG_LEVEL_ERROR, "realloc: failed to re-allocate %lu bytes of memory", (unsigned long) size);
129 exit(MEMORY_ALLOCATION_FAILURE);
130 }
131 return p;
132 }
133
134 int cmpurl(url_t* u1,url_t* u2)
135 {
136 if(u1->type!= u2->type){
137 return RETFAIL;
138 };
139 if(strcmp(u1->value,u2->value)!=0){
140 return RETFAIL;
141 }
142
143 return RETOK;
144 }
145
146 static size_t escape_str(const char *unescaped_str, char *str, size_t s) {
147 size_t n = 0;
148 size_t i = 0;
149 char c;
150 while (i < s && (c = unescaped_str[i])) {
151 if ((c >= 0 && (c < 0x1f || c == 0x7f)) ||
152 (c == '\\' && isdigit(unescaped_str[i+1])
153 && isdigit(unescaped_str[i+2])
154 && isdigit(unescaped_str[i+3])
155 ) ) {
156 if (str) { snprintf(&str[n], 5, "\\%03o", c); }
157 n += 4;
158 } else {
159 if (str) { str[n] = c; }
160 n++;
161 }
162 i++;
163 }
164 if (str) { str[n] = '\0'; }
165 n++;
166 return n;
167 }
168
169 char *strnesc(const char *unescaped_str, size_t s) {
170 int n = escape_str(unescaped_str, NULL, s);
171 char *str = checked_malloc(n);
172 escape_str(unescaped_str, str, s);
173 return str;
174 }
175
176 char *stresc(const char *unescaped_str) {
177 return strnesc(unescaped_str, strlen(unescaped_str));
178 }
179
180 /* Returns 1 if the string contains unsafe characters, 0 otherwise. */
181 int contains_unsafe (const char *s)
182 {
183 for (; *s; s++)
184 if (strchr (URL_UNSAFE,(int) *s)||!ISPRINT((int)*s))
185 return 1;
186 return 0;
187 }
188
189 /* Decodes the forms %xy in a URL to the character the hexadecimal
190 code of which is xy. xy are hexadecimal digits from
191 [0123456789ABCDEF] (case-insensitive). If x or y are not
192 hex-digits or `%' precedes `\0', the sequence is inserted
193 literally. */
194
195 void decode_string (char* s)
196 {
197 char *p = s;
198
199 for (; *s; s++, p++)
200 {
201 if (*s != '%')
202 *p = *s;
203 else
204 {
205 /* Do nothing if at the end of the string, or if the chars
206 are not hex-digits. */
207 if (!*(s + 1) || !*(s + 2)
208 || !(ISXDIGIT (*(s + 1)) && ISXDIGIT (*(s + 2))))
209 {
210 *p = *s;
211 continue;
212 }
213 *p = (ASC2HEXD (*(s + 1)) << 4) + ASC2HEXD (*(s + 2));
214 s += 2;
215 }
216 }
217 *p = '\0';
218 }
219
220 /* Encodes the unsafe characters (listed in URL_UNSAFE) in a given
221 string, returning a malloc-ed %XX encoded string. */
222 char* encode_string (const char* s)
223 {
224 const char *b;
225 char *p, *res;
226 int i;
227
228 b = s;
229 for (i = 0; *s; s++, i++){
230 if (strchr (URL_UNSAFE,(int) *s)||!ISPRINT((int)*s)){
231 i += 2; /* Two more characters (hex digits) */
232 }
233 }
234
235 res = (char *)checked_malloc (i + 1);
236 s = b;
237 for (p = res; *s; s++){
238 if (strchr (URL_UNSAFE, *s)||!ISPRINT((int)*s))
239 {
240 const unsigned char c = *s;
241 *p++ = '%';
242 *p++ = HEXD2ASC (c >> 4);
243 *p++ = HEXD2ASC (c & 0xf);
244 }
245 else {
246 *p++ = *s;
247 }
248 }
249 *p = '\0';
250 return res;
251 }
252
253 char* byte_to_base16(const byte* src, size_t ssize) {
254 char* str = checked_malloc((2*ssize+1) * sizeof (char));
255 for(size_t i=0; i < ssize; ++i) {
256 snprintf(&str[2*i], 3, "%02x", src[i]);
257 }
258 return str;
259 }
260
261 char *get_progress_bar_string(const char* state_str, const char* path, long unsigned num_entries, long unsigned num_skipped, int elapsed, int length) {
262 char *progress_bar = checked_malloc(length+1);
263 int n = 0;
264 int left = length;
265 n += snprintf(&progress_bar[n], left+1, "[%02d:%02d] %s> %lu %s", elapsed/60, elapsed%60, state_str, num_entries, num_entries == 1?"file":"files");
266 left = length-n;
267 if (num_skipped && left > 0) {
268 n += snprintf(&progress_bar[n], left+1, " (%lu skipped)", num_skipped);
269 left = length-n;
270 }
271 if (path && left > 0) {
272 const char *ellipsis = "/...";
273 const char *last_str = ", last ";
274 int last_str_len = strlen(last_str);
275 int ellipsis_len = 0;
276 int prefix_len = 0;
277
278 const char *suffix_path = path;
279 if ((long) strlen(path) > (left-last_str_len) ) {
280 const char *first_slash = strchr(path+1, '/');
281 if (first_slash) {
282 ellipsis_len = strlen(ellipsis);
283 prefix_len = first_slash - path;
284
285 suffix_path = first_slash+1;
286
287 int path_left = left - last_str_len - prefix_len - ellipsis_len;
288 while ((long) strlen(suffix_path) > path_left) {
289 char *slash = strchr(suffix_path+1, '/');
290 if (slash) {
291 suffix_path = slash;
292 } else {
293 break;
294 }
295 }
296 }
297 }
298 snprintf(&progress_bar[n], left+1, "%s%.*s%.*s%s", last_str, prefix_len, path, ellipsis_len, ellipsis, suffix_path);
299 }
300 return progress_bar;
301 }
302
303 char* perm_to_char(mode_t perm)
304 {
305 char*pc=NULL;
306 int i=0;
307
308 pc=(char*)checked_malloc(sizeof(char)*11);
309 for(i=0;i<10;i++){
310 pc[i]='-';
311 }
312 pc[10]='\0';
313
314 if(S_ISDIR(perm))
315 pc[0]='d';
316 #ifdef S_ISFIFO
317 if(S_ISFIFO(perm))
318 pc[0]='p';
319 #endif
320 if(S_ISLNK(perm))
321 pc[0]='l';
322 if(S_ISBLK(perm))
323 pc[0]='b';
324 if(S_ISCHR(perm))
325 pc[0]='c';
326 #ifdef S_ISDOOR
327 if(S_ISDOOR(perm))
328 pc[0]='|';
329 #endif
330 #ifdef S_ISSOCK
331 if(S_ISSOCK(perm))
332 pc[0]='s';
333 #endif
334
335 if((S_IRUSR&perm)==S_IRUSR){
336 pc[1]='r';
337 }
338 if((S_IWUSR&perm)==S_IWUSR){
339 pc[2]='w';
340 }
341 if((S_IXUSR&perm)==S_IXUSR){
342 pc[3]='x';
343 }
344 if((S_IRGRP&perm)==S_IRGRP){
345 pc[4]='r';
346 }
347 if((S_IWGRP&perm)==S_IWGRP){
348 pc[5]='w';
349 }
350 if((S_IXGRP&perm)==S_IXGRP){
351 pc[6]='x';
352 }
353 if((S_IROTH&perm)==S_IROTH){
354 pc[7]='r';
355 }
356 if((S_IWOTH&perm)==S_IWOTH){
357 pc[8]='w';
358 }
359 if((S_IXOTH&perm)==S_IXOTH){
360 pc[9]='x';
361 }
362
363 if((S_ISUID&perm)==S_ISUID){
364 if((S_IXUSR&perm)==S_IXUSR){
365 pc[3]='s';
366 } else {
367 pc[3]='S';
368 }
369 }
370 if((S_ISGID&perm)==S_ISGID){
371 if((S_IXGRP&perm)==S_IXGRP){
372 pc[6]='s';
373 } else {
374 pc[6]='l';
375 }
376 }
377 #if defined (S_ISVTX) && defined (S_IXOTH)
378 if((S_ISVTX&perm)==S_ISVTX){
379 if((S_IXOTH&perm)==S_IXOTH){
380 pc[9]='t';
381 } else {
382 pc[9]='T';
383 }
384 }
385 #endif
386
387 log_msg(LOG_LEVEL_TRACE, "perm_to_char: %i -> %s",perm,pc);
388
389 return pc;
390 }
391
392 char *expand_tilde(char *path) {
393 char *homedir = NULL;
394 char *full = NULL;
395
396 if (path != NULL) {
397 if (path[0] == '~') {
398 if((homedir=getenv("HOME")) != NULL) {
399 size_t path_len, homedir_len, full_len;
400 path_len = strlen(path+sizeof(char));
401 homedir_len = strlen(homedir);
402 full_len = (homedir_len+path_len+1) * sizeof(char);
403 full = checked_malloc(full_len);
404 snprintf(full, full_len, "%s%s", homedir, path+sizeof(char));
405 log_msg(LOG_LEVEL_DEBUG, "expanded '~' in '%s' to '%s'", path, full);
406 free(path);
407 /* Don't free(homedir); because it is not safe on some platforms */
408 path = full;
409 } else {
410 log_msg(LOG_LEVEL_WARNING, _("Variable name 'HOME' not found in environment. '~' cannot be expanded"));
411 }
412 } else if (path[0] == '\\' && path[1] == '~') {
413 path += sizeof(char);
414 }
415 }
416 return path;
417 }
418
419 char* pipe2string(int fd) {
420 int nbytes;
421 char buffer[1024];
422
423 char* str = NULL;
424 while ((nbytes = read(fd, buffer, sizeof(buffer))) > 0) {
425 int str_len = str?strlen(str):0U;
426 int len = str_len+nbytes;
427 str = checked_realloc(str, (len+1)*sizeof(char));
428 strncpy(str+str_len, buffer, nbytes);
429 str[len] = '\0';
430 }
431 return str;
432 }
433
434 char* get_time_string(const time_t *tm) {
435 const char time_format[] = "%Y-%m-%d %H:%M:%S %z";
436 int time_string_len = strlen("1979-01-01 01:00:00 +0100")+1;
437
438 char *time = checked_malloc(time_string_len * sizeof (char));
439 strftime(time, time_string_len, time_format, localtime(tm));
440
441 return time;
442 }
443
444 void mask_sig(const char* whoami) {
445 sigset_t set;
446
447 sigemptyset(&set);
448 sigaddset(&set, SIGINT);
449 sigaddset(&set, SIGTERM);
450 sigaddset(&set, SIGHUP);
451 sigaddset(&set, SIGUSR1);
452 sigaddset(&set, SIGWINCH);
453
454 if(pthread_sigmask(SIG_BLOCK, &set, NULL)) {
455 log_msg(LOG_LEVEL_ERROR, "%10s: pthread_sigmask failed to set mask of blocked signals", whoami);
456 exit(THREAD_ERROR);
457 }
458 }
459
460 /* Like strstr but only do search for maximum of n chars.
461 haystack does not have to be NULL terminated
462 needle has to be NULL terminated. NULL in needle is not used in compare.
463 NULLs in haystack are ignored.
464 */
465 #ifndef HAVE_STRNSTR
466 char* strnstr(char* haystack,char* needle,int n)
467 {
468 char* h=haystack;
469 char* s=needle;
470 int slen=strlen(s);
471 int i=0;
472
473 for(i=0;i<n;i++){
474 /* If terminating NULL is reached in needle string
475 then we have a match */
476 if(*s=='\0'){
477 return &haystack[i-slen];
478 }
479 if(*s==*h){
480 s++;
481 }else{
482 s=needle;
483 }
484 h++;
485 }
486 /* Handle the special case that we are at the end of haystack
487 and match is right at the end
488 */
489 if(*s=='\0'){
490 return &haystack[i-slen];
491 }
492
493 /* If we get this far no match was found so we return NULL */
494 return NULL;
495 }
496 #endif
497
498 #ifndef HAVE_STRNLEN
499 size_t strnlen(const char *s, size_t maxlen)
500 {
501 size_t l;
502 l=strlen(s);
503 if(l>maxlen)
504 return maxlen;
505 return l;
506 }
507 #endif
508
509 #ifdef HAVE_SYSLOG
510 /* Lookup syslog facilities by name */
511 int syslog_facility_lookup(char *s)
512 {
513 if(!s || strlen(s)<1)
514 return(AIDE_SYSLOG_FACILITY);
515 #ifdef LOG_KERN
516 if(strcasecmp(s,"LOG_KERN")==0)
517 return(LOG_KERN);
518 #endif
519 #ifdef LOG_USER
520 if(strcasecmp(s,"LOG_USER")==0)
521 return(LOG_USER);
522 #endif
523 #ifdef LOG_MAIL
524 if(strcasecmp(s,"LOG_MAIL")==0)
525 return(LOG_MAIL);
526 #endif
527 #ifdef LOG_DAEMON
528 if(strcasecmp(s,"LOG_DAEMON")==0)
529 return(LOG_DAEMON);
530 #endif
531 #ifdef LOG_AUTH
532 if(strcasecmp(s,"LOG_AUTH")==0)
533 return(LOG_AUTH);
534 #endif
535 #ifdef LOG_SYSLOG
536 if(strcasecmp(s,"LOG_SYSLOG")==0)
537 return(LOG_SYSLOG);
538 #endif
539 #ifdef LOG_LPR
540 if(strcasecmp(s,"LOG_LPR")==0)
541 return(LOG_LPR);
542 #endif
543 #ifdef LOG_NEWS
544 if(strcasecmp(s,"LOG_NEWS")==0)
545 return(LOG_NEWS);
546 #endif
547 #ifdef LOG_UUCP
548 if(strcasecmp(s,"LOG_UUCP")==0)
549 return(LOG_UUCP);
550 #endif
551 #ifdef LOG_CRON
552 if(strcasecmp(s,"LOG_CRON")==0)
553 return(LOG_CRON);
554 #endif
555 #ifdef LOG_LOCAL0
556 if(strcasecmp(s,"LOG_LOCAL0")==0)
557 return(LOG_LOCAL0);
558 #endif
559 #ifdef LOG_LOCAL1
560 if(strcasecmp(s,"LOG_LOCAL1")==0)
561 return(LOG_LOCAL1);
562 #endif
563 #ifdef LOG_LOCAL2
564 if(strcasecmp(s,"LOG_LOCAL2")==0)
565 return(LOG_LOCAL2);
566 #endif
567 #ifdef LOG_LOCAL3
568 if(strcasecmp(s,"LOG_LOCAL3")==0)
569 return(LOG_LOCAL3);
570 #endif
571 #ifdef LOG_LOCAL4
572 if(strcasecmp(s,"LOG_LOCAL4")==0)
573 return(LOG_LOCAL4);
574 #endif
575 #ifdef LOG_LOCAL5
576 if(strcasecmp(s,"LOG_LOCAL5")==0)
577 return(LOG_LOCAL5);
578 #endif
579 #ifdef LOG_LOCAL6
580 if(strcasecmp(s,"LOG_LOCAL6")==0)
581 return(LOG_LOCAL6);
582 #endif
583 #ifdef LOG_LOCAL7
584 if(strcasecmp(s,"LOG_LOCAL7")==0)
585 return(LOG_LOCAL7);
586 #endif
587
588 log_msg(LOG_LEVEL_WARNING, "Syslog facility \"%s\" is unknown, using default",s);
589 return(AIDE_SYSLOG_FACILITY);
590 }
591 #endif