-
Notifications
You must be signed in to change notification settings - Fork 201
/
auditd-config.c
2103 lines (1913 loc) · 53.2 KB
/
auditd-config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* auditd-config.c --
* Copyright 2004-2011,2013-14,2016,2018,2020-21 Red Hat Inc.
* All Rights Reserved.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <stdlib.h>
#include <netdb.h>
#include <fcntl.h> /* O_NOFOLLOW needs gnu defined */
#include <libgen.h>
#include <arpa/inet.h>
#include <limits.h> /* INT_MAX */
#include <sys/vfs.h>
#include "auditd-config.h"
#include "libaudit.h"
#include "private.h"
#include "common.h"
#define TCP_PORT_MAX 65535
/* Local prototypes */
struct nv_pair
{
const char *name;
const char *value;
const char *option;
};
struct kw_pair
{
const char *name;
int (*parser)(const struct nv_pair *, int, struct daemon_conf *);
int max_options;
};
struct nv_list
{
const char *name;
int option;
};
static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
const char *file);
static int nv_split(char *buf, struct nv_pair *nv);
static const struct kw_pair *kw_lookup(const char *val);
static int local_events_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int write_logs_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int log_file_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int num_logs_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int log_group_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int qos_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int dispatch_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int name_format_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int name_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int max_log_size_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int max_log_size_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int log_format_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int flush_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int freq_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int space_left_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int space_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int action_mail_acct_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int verify_email_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int admin_space_left_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int admin_space_left_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int disk_full_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int disk_error_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int priority_boost_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int tcp_listen_port_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int tcp_listen_queue_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int tcp_max_per_addr_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int use_libwrap_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int tcp_client_ports_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int tcp_client_max_idle_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int transport_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int enable_krb5_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int krb5_principal_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int krb5_key_file_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int distribute_network_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int q_depth_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int overflow_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int max_restarts_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int plugin_dir_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int eoe_timeout_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config);
static int sanity_check(struct daemon_conf *config);
static const struct kw_pair keywords[] =
{
{"local_events", local_events_parser, 0},
{"write_logs", write_logs_parser, 0 },
{"log_file", log_file_parser, 0 },
{"log_format", log_format_parser, 0 },
{"log_group", log_group_parser, 0 },
{"flush", flush_parser, 0 },
{"freq", freq_parser, 0 },
{"num_logs", num_logs_parser, 0 },
{"dispatcher", dispatch_parser, 0 },
{"name_format", name_format_parser, 0 },
{"name", name_parser, 0 },
{"disp_qos", qos_parser, 0 },
{"max_log_file", max_log_size_parser, 0 },
{"max_log_file_action", max_log_size_action_parser, 0 },
{"space_left", space_left_parser, 0 },
{"space_left_action", space_action_parser, 1 },
{"action_mail_acct", action_mail_acct_parser, 0 },
{"verify_email", verify_email_parser, 0 },
{"admin_space_left", admin_space_left_parser, 0 },
{"admin_space_left_action", admin_space_left_action_parser, 1 },
{"disk_full_action", disk_full_action_parser, 1 },
{"disk_error_action", disk_error_action_parser, 1 },
{"priority_boost", priority_boost_parser, 0 },
{"tcp_listen_port", tcp_listen_port_parser, 0 },
{"tcp_listen_queue", tcp_listen_queue_parser, 0 },
{"tcp_max_per_addr", tcp_max_per_addr_parser, 0 },
{"use_libwrap", use_libwrap_parser, 0 },
{"tcp_client_ports", tcp_client_ports_parser, 0 },
{"tcp_client_max_idle", tcp_client_max_idle_parser, 0 },
{"transport", transport_parser, 0 },
{"enable_krb5", enable_krb5_parser, 0 },
{"krb5_principal", krb5_principal_parser, 0 },
{"krb5_key_file", krb5_key_file_parser, 0 },
{"distribute_network", distribute_network_parser, 0 },
{"q_depth", q_depth_parser, 0 },
{"overflow_action", overflow_action_parser, 0 },
{"max_restarts", max_restarts_parser, 0 },
{"plugin_dir", plugin_dir_parser, 0 },
{"end_of_event_timeout", eoe_timeout_parser, 0 },
{ NULL, NULL, 0 }
};
static const struct nv_list log_formats[] =
{
{"raw", LF_RAW },
{"nolog", LF_NOLOG },
{"enriched", LF_ENRICHED },
{ NULL, 0 }
};
static const struct nv_list flush_techniques[] =
{
{"none", FT_NONE },
{"incremental", FT_INCREMENTAL },
{"incremental_async", FT_INCREMENTAL_ASYNC },
{"data", FT_DATA },
{"sync", FT_SYNC },
{ NULL, 0 }
};
static const struct nv_list failure_actions[] =
{
{"ignore", FA_IGNORE },
{"syslog", FA_SYSLOG },
{"rotate", FA_ROTATE },
{"email", FA_EMAIL },
{"exec", FA_EXEC },
{"suspend", FA_SUSPEND },
{"single", FA_SINGLE },
{"halt", FA_HALT },
{ NULL, 0 }
};
// Future ideas: e-mail, run command
static const struct nv_list size_actions[] =
{
{"ignore", SZ_IGNORE },
{"syslog", SZ_SYSLOG },
{"suspend", SZ_SUSPEND },
{"rotate", SZ_ROTATE },
{"keep_logs", SZ_KEEP_LOGS},
{ NULL, 0 }
};
static const struct nv_list node_name_formats[] =
{
{"none", N_NONE },
{"hostname", N_HOSTNAME },
{"fqd", N_FQD },
{"numeric", N_NUMERIC },
{"user", N_USER },
{ NULL, 0 }
};
static const struct nv_list yes_no_values[] =
{
{"yes", 1 },
{"no", 0 },
{ NULL, 0 }
};
static const struct nv_list overflow_actions[] =
{
{"ignore", O_IGNORE },
{"syslog", O_SYSLOG },
{"suspend", O_SUSPEND },
{"single", O_SINGLE },
{"halt", O_HALT },
{ NULL, 0 }
};
static const struct nv_list transport_words[] =
{
{"tcp", T_TCP },
#ifdef USE_GSSAPI
{"krb5", T_KRB5 },
#endif
{ NULL, 0 }
};
const char *email_command = "/usr/lib/sendmail";
static int allow_links = 0;
static const char *config_dir = NULL;
static char *config_file = NULL;
void set_allow_links(int allow)
{
allow_links = allow;
}
int set_config_dir(const char *val)
{
config_dir = strdup(val);
if (config_dir == NULL)
return 1;
if (asprintf(&config_file, "%s/auditd.conf", config_dir) < 0)
return 1;
return 0;
}
/*
* Set everything to its default value
*/
void clear_config(struct daemon_conf *config)
{
config->local_events = 1;
config->sender_uid = 0;
config->sender_pid = 0;
config->sender_ctx = NULL;
config->write_logs = 1;
config->log_file = strdup("/var/log/audit/audit.log");
config->log_format = LF_ENRICHED;
config->log_group = 0;
config->priority_boost = 4;
config->flush = FT_NONE;
config->freq = 0;
config->num_logs = 0L;
config->node_name_format = N_NONE;
config->node_name = NULL;
config->max_log_size = 0L;
config->max_log_size_action = SZ_IGNORE;
config->space_left = 0L;
config->space_left_percent = 0;
config->space_left_action = FA_IGNORE;
config->space_left_exe = NULL;
config->action_mail_acct = strdup("root");
config->verify_email = 1;
config->admin_space_left= 0L;
config->admin_space_left_percent = 0;
config->admin_space_left_action = FA_IGNORE;
config->admin_space_left_exe = NULL;
config->disk_full_action = FA_IGNORE;
config->disk_full_exe = NULL;
config->disk_error_action = FA_SYSLOG;
config->disk_error_exe = NULL;
config->tcp_listen_port = 0;
config->tcp_listen_queue = 5;
config->tcp_max_per_addr = 1;
config->use_libwrap = 1;
config->tcp_client_min_port = 0;
config->tcp_client_max_port = TCP_PORT_MAX;
config->tcp_client_max_idle = 0;
config->transport = T_TCP;
config->krb5_principal = NULL;
config->krb5_key_file = NULL;
config->distribute_network_events = 0;
config->q_depth = 2000;
config->overflow_action = O_SYSLOG;
config->max_restarts = 10;
config->plugin_dir = strdup("/etc/audit/plugins.d");
config->config_dir = NULL;
config->end_of_event_timeout = EOE_TIMEOUT;
}
static log_test_t log_test = TEST_AUDITD;
int load_config(struct daemon_conf *config, log_test_t lt)
{
int fd, rc, mode, lineno = 1;
struct stat st;
FILE *f;
char buf[160];
clear_config(config);
log_test = lt;
if (config_file == NULL)
config_file = strdup(CONFIG_FILE);
/* open the file */
mode = O_RDONLY;
if (allow_links == 0)
mode |= O_NOFOLLOW;
rc = open(config_file, mode);
if (rc < 0) {
if (errno != ENOENT) {
audit_msg(LOG_ERR, "Error opening config file (%s)",
strerror(errno));
return 1;
}
audit_msg(LOG_WARNING,
"Config file %s doesn't exist, skipping", config_file);
return 0;
}
fd = rc;
/* check the file's permissions: owned by root, not world writable,
* not symlink.
*/
audit_msg(LOG_DEBUG, "Config file %s opened for parsing",
config_file);
if (fstat(fd, &st) < 0) {
audit_msg(LOG_ERR, "Error fstat'ing config file (%s)",
strerror(errno));
close(fd);
return 1;
}
if (st.st_uid != 0) {
audit_msg(LOG_ERR, "Error - %s isn't owned by root",
config_file);
close(fd);
return 1;
}
if ((st.st_mode & S_IWOTH) == S_IWOTH) {
audit_msg(LOG_ERR, "Error - %s is world writable",
config_file);
close(fd);
return 1;
}
if (!S_ISREG(st.st_mode)) {
audit_msg(LOG_ERR, "Error - %s is not a regular file",
config_file);
close(fd);
return 1;
}
/* it's ok, read line by line */
f = fdopen(fd, "rm");
if (f == NULL) {
audit_msg(LOG_ERR, "Error - fdopen failed (%s)",
strerror(errno));
close(fd);
return 1;
}
while (get_line(f, buf, sizeof(buf), &lineno, config_file)) {
// convert line into name-value pair
const struct kw_pair *kw;
struct nv_pair nv;
rc = nv_split(buf, &nv);
switch (rc) {
case 0: // fine
break;
case 1: // not the right number of tokens.
audit_msg(LOG_ERR,
"Wrong number of arguments for line %d in %s",
lineno, config_file);
break;
case 2: // no '=' sign
audit_msg(LOG_ERR,
"Missing equal sign for line %d in %s",
lineno, config_file);
break;
default: // something else went wrong...
audit_msg(LOG_ERR,
"Unknown error for line %d in %s",
lineno, config_file);
break;
}
if (nv.name == NULL) {
lineno++;
continue;
}
if (nv.value == NULL) {
fclose(f);
audit_msg(LOG_ERR,
"Not processing any more lines in %s",
config_file);
return 1;
}
/* identify keyword or error */
kw = kw_lookup(nv.name);
if (kw->name == NULL) {
audit_msg(LOG_ERR,
"Unknown keyword \"%s\" in line %d of %s",
nv.name, lineno, config_file);
fclose(f);
return 1;
}
/* Check number of options */
if (kw->max_options == 0 && nv.option != NULL) {
audit_msg(LOG_ERR,
"Keyword \"%s\" has invalid option "
"\"%s\" in line %d of %s",
nv.name, nv.option, lineno, config_file);
fclose(f);
return 1;
}
/* dispatch to keyword's local parser */
rc = kw->parser(&nv, lineno, config);
if (rc != 0) {
fclose(f);
return 1; // local parser puts message out
}
lineno++;
}
fclose(f);
if (lineno > 1)
return sanity_check(config);
return 0;
}
static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
const char *file)
{
int too_long = 0;
while (fgets_unlocked(buf, size, f)) {
/* remove newline */
char *ptr = strchr(buf, 0x0a);
if (ptr) {
if (!too_long) {
*ptr = 0;
return buf;
}
// Reset and start with the next line
too_long = 0;
*lineno = *lineno + 1;
} else {
// If a line is too long skip it.
// Only output 1 warning
if (!too_long)
audit_msg(LOG_ERR,
"Skipping line %d in %s: too long",
*lineno, file);
too_long = 1;
}
}
return NULL;
}
static int nv_split(char *buf, struct nv_pair *nv)
{
/* Get the name part */
char *ptr;
nv->name = NULL;
nv->value = NULL;
nv->option = NULL;
ptr = audit_strsplit(buf);
if (ptr == NULL)
return 0; /* If there's nothing, go to next line */
if (ptr[0] == '#')
return 0; /* If there's a comment, go to next line */
nv->name = ptr;
/* Check for a '=' */
ptr = audit_strsplit(NULL);
if (ptr == NULL)
return 1;
if (strcmp(ptr, "=") != 0)
return 2;
/* get the value */
ptr = audit_strsplit(NULL);
if (ptr == NULL)
return 1;
nv->value = ptr;
/* See if there's an option */
ptr = audit_strsplit(NULL);
if (ptr) {
nv->option = ptr;
/* Make sure there's nothing else */
ptr = audit_strsplit(NULL);
if (ptr)
return 1;
}
/* Everything is OK */
return 0;
}
static const struct kw_pair *kw_lookup(const char *val)
{
int i = 0;
while (keywords[i].name != NULL) {
if (strcasecmp(keywords[i].name, val) == 0)
break;
i++;
}
return &keywords[i];
}
static int local_events_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
unsigned long i;
audit_msg(LOG_DEBUG, "local_events_parser called with: %s",
nv->value);
for (i=0; yes_no_values[i].name != NULL; i++) {
if (strcasecmp(nv->value, yes_no_values[i].name) == 0) {
config->local_events = yes_no_values[i].option;
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int write_logs_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
unsigned long i;
audit_msg(LOG_DEBUG, "write_logs_parser called with: %s",
nv->value);
for (i=0; yes_no_values[i].name != NULL; i++) {
if (strcasecmp(nv->value, yes_no_values[i].name) == 0) {
config->write_logs = yes_no_values[i].option;
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int log_file_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
char *dir = NULL, *tdir;
DIR *d;
int fd, mode;
struct stat buf;
audit_msg(LOG_DEBUG, "log_file_parser called with: %s", nv->value);
/* get dir from name. */
tdir = strdup(nv->value);
if (tdir)
dir = dirname(tdir);
if (dir == NULL || strlen(dir) < 4) { // '/var' is shortest dirname
audit_msg(LOG_ERR,
"The directory name: %s is too short - line %d",
dir, line);
free((void *)tdir);
return 1;
}
/* verify the directory path exists */
d = opendir(dir);
if (d == NULL) {
audit_msg(LOG_ERR, "Could not open dir %s (%s)", dir,
strerror(errno));
free((void *)tdir);
return 1;
}
free((void *)tdir);
closedir(d);
/* if the file exists, see that its regular, owned by root,
* and not world anything */
if (log_test == TEST_AUDITD)
mode = O_APPEND;
else
mode = O_RDONLY;
fd = open(nv->value, mode);
if (fd < 0) {
if (errno == ENOENT)
goto finish_up; // Will create the log later
else {
audit_msg(LOG_ERR, "Unable to open %s (%s)", nv->value,
strerror(errno));
return 1;
}
}
if (fstat(fd, &buf) < 0) {
audit_msg(LOG_ERR, "Unable to stat %s (%s)",
nv->value, strerror(errno));
close(fd);
return 1;
}
close(fd);
if (!S_ISREG(buf.st_mode)) {
audit_msg(LOG_ERR, "%s is not a regular file", nv->value);
return 1;
}
if (buf.st_uid != 0) {
audit_msg(LOG_ERR, "%s is not owned by root", nv->value);
return 1;
}
if ( (buf.st_mode & (S_IXUSR|S_IWGRP|S_IXGRP|S_IRWXO)) ) {
audit_msg(LOG_WARNING, "%s permissions should be 0600 or 0640",
nv->value);
}
if ( !(buf.st_mode & S_IWUSR) ) {
audit_msg(LOG_WARNING, "audit log is not writable by owner");
}
finish_up:
free((void *)config->log_file);
config->log_file = strdup(nv->value);
if (config->log_file == NULL)
return 1;
return 0;
}
static int num_logs_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
const char *ptr = nv->value;
unsigned long i;
audit_msg(LOG_DEBUG, "num_logs_parser called with: %s", nv->value);
/* check that all chars are numbers */
for (i=0; ptr[i]; i++) {
if (!isdigit((unsigned char)ptr[i])) {
audit_msg(LOG_ERR,
"Value %s should only be numbers - line %d",
nv->value, line);
return 1;
}
}
/* convert to unsigned long */
errno = 0;
i = strtoul(nv->value, NULL, 10);
if (errno) {
audit_msg(LOG_ERR,
"Error converting string to a number (%s) - line %d",
strerror(errno), line);
return 1;
}
if (i > 999) {
audit_msg(LOG_ERR, "num_logs must be 999 or less");
return 1;
}
config->num_logs = i;
return 0;
}
static int qos_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
audit_msg(LOG_WARNING, "The disp_qos option is deprecated - line %d",
line);
return 0;
}
static int dispatch_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
audit_msg(LOG_DEBUG, "dispatch_parser called with: %s", nv->value);
audit_msg(LOG_WARNING, "The dispatcher option is deprecated - line %d",
line);
return 0;
}
static int name_format_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
int i;
audit_msg(LOG_DEBUG, "name_format_parser called with: %s", nv->value);
for (i=0; node_name_formats[i].name != NULL; i++) {
if (strcasecmp(nv->value, node_name_formats[i].name) == 0) {
config->node_name_format = node_name_formats[i].option;
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int name_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
audit_msg(LOG_DEBUG, "name_parser called with: %s", nv->value);
if (nv->value == NULL)
config->node_name = NULL;
else
config->node_name = strdup(nv->value);
return 0;
}
static int max_log_size_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
const char *ptr = nv->value;
unsigned long i;
audit_msg(LOG_DEBUG, "max_log_size_parser called with: %s", nv->value);
/* check that all chars are numbers */
for (i=0; ptr[i]; i++) {
if (!isdigit((unsigned char)ptr[i])) {
audit_msg(LOG_ERR,
"Value %s should only be numbers - line %d",
nv->value, line);
return 1;
}
}
/* convert to unsigned long */
errno = 0;
i = strtoul(nv->value, NULL, 10);
if (errno) {
audit_msg(LOG_ERR,
"Error converting string to a number (%s) - line %d",
strerror(errno), line);
return 1;
}
config->max_log_size = i;
return 0;
}
static int max_log_size_action_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
int i;
audit_msg(LOG_DEBUG, "max_log_size_action_parser called with: %s",
nv->value);
for (i=0; size_actions[i].name != NULL; i++) {
if (strcasecmp(nv->value, size_actions[i].name) == 0) {
config->max_log_size_action = size_actions[i].option;
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int log_format_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
int i;
audit_msg(LOG_DEBUG, "log_format_parser called with: %s", nv->value);
for (i=0; log_formats[i].name != NULL; i++) {
if (strcasecmp(nv->value, log_formats[i].name) == 0) {
config->log_format = log_formats[i].option;
if (config->log_format == LF_NOLOG) {
audit_msg(LOG_WARNING,
"The NOLOG option to log_format is deprecated. Please use the write_logs option.");
if (config->write_logs != 0)
audit_msg(LOG_WARNING,
"The NOLOG option is overriding the write_logs current setting.");
config->write_logs = 0;
}
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int log_group_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
gid_t gid = 0;
audit_msg(LOG_DEBUG, "log_group_parser called with: %s",
nv->value);
if (isdigit((unsigned char)nv->value[0])) {
errno = 0;
gid = strtoul(nv->value,NULL,10);
if (errno) {
audit_msg(LOG_ERR,
"Numeric group ID conversion error (%s) for %s - line %d\n",
strerror(errno), nv->value, line);
return 1;
}
} else {
struct group *gr ;
gr = getgrnam(nv->value);
if (gr == NULL) {
audit_msg(LOG_ERR,
"Group ID is non-numeric and unknown (%s) - line %d\n",
nv->value, line);
return 1;
}
gid = gr->gr_gid;
}
config->log_group = gid;
return 0;
}
static int flush_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
int i;
audit_msg(LOG_DEBUG, "flush_parser called with: %s", nv->value);
for (i=0; flush_techniques[i].name != NULL; i++) {
if (strcasecmp(nv->value, flush_techniques[i].name) == 0) {
config->flush = flush_techniques[i].option;
return 0;
}
}
audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
return 1;
}
static int freq_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
const char *ptr = nv->value;
unsigned long i;
audit_msg(LOG_DEBUG, "freq_parser called with: %s", nv->value);
/* check that all chars are numbers */
for (i=0; ptr[i]; i++) {
if (!isdigit((unsigned char)ptr[i])) {
audit_msg(LOG_ERR,
"Value %s should only be numbers - line %d",
nv->value, line);
return 1;
}
}
/* convert to unsigned int */
errno = 0;
i = strtoul(nv->value, NULL, 10);
if (errno) {
audit_msg(LOG_ERR,
"Error converting string to a number (%s) - line %d",
strerror(errno), line);
return 1;
}
/* Check its range */
if (i > INT_MAX) {
audit_msg(LOG_ERR,
"Error - converted number (%s) is too large - line %d",
nv->value, line);
return 1;
}
config->freq = (unsigned int)i;
return 0;
}
static int space_left_parser(const struct nv_pair *nv, int line,
struct daemon_conf *config)
{
char *p, *ptr = (char *)nv->value;
unsigned long i;
int percent = 0;
audit_msg(LOG_DEBUG, "space_left_parser called with: %s", nv->value);
config->space_left_percent = 0;
/* check that all chars are numbers */
for (i=0; ptr[i]; i++) {
if (!isdigit((unsigned char)ptr[i]) && ptr[i] != '%') {
audit_msg(LOG_ERR,
"Value %c %s should only be numbers or percent - line %d",
ptr[i],nv->value, line);
return 1;
}
}
/* Check for percent sign */
p = strchr(ptr, '%');
if (p) {
percent = 1;
*p = ' ';
}
/* convert to unsigned long */
errno = 0;
i = strtoul(nv->value, NULL, 10);
if (errno) {
audit_msg(LOG_ERR,
"Error converting string to a number (%s) - line %d",
strerror(errno), line);
return 1;
}
if (percent) {
if (i > 99) {
audit_msg(LOG_ERR,
"Percentages should be less than 100 - line %d", line);
return 1;
}
config->space_left_percent = i;
config->space_left = 0L;
} else
config->space_left = i;
return 0;
}
static int check_exe_name(const char *val, int line)
{
struct stat buf;
if (val == NULL) {
audit_msg(LOG_ERR, "Executable path needed for line %d", line);
return -1;
}
if (*val != '/') {
audit_msg(LOG_ERR, "Absolute path needed for %s - line %d",
val, line);
return -1;
}
if (stat(val, &buf) < 0) {
audit_msg(LOG_ERR, "Unable to stat %s (%s) - line %d", val,
strerror(errno), line);
return -1;
}
if (!S_ISREG(buf.st_mode)) {
audit_msg(LOG_ERR, "%s is not a regular file - line %d", val,
line);
return -1;
}
if (buf.st_uid != 0) {
audit_msg(LOG_ERR, "%s is not owned by root - line %d", val,
line);