2 * Copyright (c) 2004-2008 Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditreduce/auditreduce.c#31 $
33 * Tool used to merge and select audit records from audit trail files
37 * XXX Currently we do not support merging of records from multiple
38 * XXX audit trail files
39 * XXX We assume that records are sorted chronologically - both wrt to
40 * XXX the records present within the file and between the files themselves
43 #include <config/config.h>
45 #define _GNU_SOURCE /* Required for strptime() on glibc2. */
47 #ifdef HAVE_FULL_QUEUE_H
48 #include <sys/queue.h>
50 #include <compat/queue.h>
53 #include <bsm/libbsm.h>
68 #include <compat/strlcpy.h>
71 #include "auditreduce.h"
73 static TAILQ_HEAD(tailhead, re_entry) re_head =
74 TAILQ_HEAD_INITIALIZER(re_head);
77 extern int optind, optopt, opterr,optreset;
79 static au_mask_t maskp; /* Class. */
80 static time_t p_atime; /* Created after this time. */
81 static time_t p_btime; /* Created before this time. */
82 static int p_auid; /* Audit id. */
83 static int p_euid; /* Effective user id. */
84 static int p_egid; /* Effective group id. */
85 static int p_rgid; /* Real group id. */
86 static int p_ruid; /* Real user id. */
87 static int p_subid; /* Subject id. */
90 * Maintain a dynamically sized array of events for -m
92 static uint16_t *p_evec; /* Event type list */
93 static int p_evec_used; /* Number of events used */
94 static int p_evec_alloc; /* Number of events allocated */
97 * Following are the objects (-o option) that we can select upon.
99 static char *p_fileobj = NULL;
100 static char *p_msgqobj = NULL;
101 static char *p_pidobj = NULL;
102 static char *p_semobj = NULL;
103 static char *p_shmobj = NULL;
104 static char *p_sockobj = NULL;
106 static uint32_t opttochk = 0;
109 parse_regexp(char *re_string)
111 char *orig, *copy, re_error[64];
112 struct re_entry *rep;
113 int error, nstrs, i, len;
115 copy = strdup(re_string);
118 for (nstrs = 0, i = 0; i < len; i++) {
119 if (copy[i] == ',' && i > 0) {
120 if (copy[i - 1] == '\\')
121 strlcpy(©[i - 1], ©[i], len);
128 TAILQ_INIT(&re_head);
129 for (i = 0; i < nstrs + 1; i++) {
130 rep = calloc(1, sizeof(*rep));
132 (void) fprintf(stderr, "calloc: %s\n",
140 rep->re_pattern = strdup(copy);
141 error = regcomp(&rep->re_regexp, rep->re_pattern,
142 REG_EXTENDED | REG_NOSUB);
144 regerror(error, &rep->re_regexp, re_error, 64);
145 (void) fprintf(stderr, "regcomp: %s\n", re_error);
148 TAILQ_INSERT_TAIL(&re_head, rep, re_glue);
156 usage(const char *msg)
158 fprintf(stderr, "%s\n", msg);
159 fprintf(stderr, "Usage: auditreduce [options] [file ...]\n");
160 fprintf(stderr, "\tOptions are : \n");
161 fprintf(stderr, "\t-A : all records\n");
162 fprintf(stderr, "\t-a YYYYMMDD[HH[[MM[SS]]] : after date\n");
163 fprintf(stderr, "\t-b YYYYMMDD[HH[[MM[SS]]] : before date\n");
164 fprintf(stderr, "\t-c <flags> : matching class\n");
165 fprintf(stderr, "\t-d YYYYMMDD : on date\n");
166 fprintf(stderr, "\t-e <uid|name> : effective user\n");
167 fprintf(stderr, "\t-f <gid|group> : effective group\n");
168 fprintf(stderr, "\t-g <gid|group> : real group\n");
169 fprintf(stderr, "\t-j <pid> : subject id \n");
170 fprintf(stderr, "\t-m <evno|evname> : matching event\n");
171 fprintf(stderr, "\t-o objecttype=objectvalue\n");
172 fprintf(stderr, "\t\t file=<pathname>\n");
173 fprintf(stderr, "\t\t msgqid=<ID>\n");
174 fprintf(stderr, "\t\t pid=<ID>\n");
175 fprintf(stderr, "\t\t semid=<ID>\n");
176 fprintf(stderr, "\t\t shmid=<ID>\n");
177 fprintf(stderr, "\t-r <uid|name> : real user\n");
178 fprintf(stderr, "\t-u <uid|name> : audit user\n");
179 fprintf(stderr, "\t-v : select non-matching records\n");
184 * Check if the given auid matches the selection criteria.
190 /* Check if we want to select on auid. */
191 if (ISOPTSET(opttochk, OPT_u)) {
199 * Check if the given euid matches the selection criteria.
202 select_euid(int euser)
205 /* Check if we want to select on euid. */
206 if (ISOPTSET(opttochk, OPT_e)) {
214 * Check if the given egid matches the selection criteria.
217 select_egid(int egrp)
220 /* Check if we want to select on egid. */
221 if (ISOPTSET(opttochk, OPT_f)) {
229 * Check if the given rgid matches the selection criteria.
235 /* Check if we want to select on rgid. */
236 if (ISOPTSET(opttochk, OPT_g)) {
244 * Check if the given ruid matches the selection criteria.
247 select_ruid(int user)
250 /* Check if we want to select on rgid. */
251 if (ISOPTSET(opttochk, OPT_r)) {
259 * Check if the given subject id (pid) matches the selection criteria.
262 select_subid(int subid)
265 /* Check if we want to select on subject uid. */
266 if (ISOPTSET(opttochk, OPT_j)) {
267 if (subid != p_subid)
275 * Check if object's pid maches the given pid.
278 select_pidobj(uint32_t pid)
281 if (ISOPTSET(opttochk, OPT_op)) {
282 if (pid != (uint32_t)strtol(p_pidobj, (char **)NULL, 10))
289 * Check if the given ipc object with the given type matches the selection
293 select_ipcobj(u_char type, uint32_t id, uint32_t *optchkd)
296 if (type == AT_IPC_MSG) {
297 SETOPT((*optchkd), OPT_om);
298 if (ISOPTSET(opttochk, OPT_om)) {
299 if (id != (uint32_t)strtol(p_msgqobj, (char **)NULL,
304 } else if (type == AT_IPC_SEM) {
305 SETOPT((*optchkd), OPT_ose);
306 if (ISOPTSET(opttochk, OPT_ose)) {
307 if (id != (uint32_t)strtol(p_semobj, (char **)NULL, 10))
311 } else if (type == AT_IPC_SHM) {
312 SETOPT((*optchkd), OPT_osh);
313 if (ISOPTSET(opttochk, OPT_osh)) {
314 if (id != (uint32_t)strtol(p_shmobj, (char **)NULL, 10))
320 /* Unknown type -- filter if *any* ipc filtering is required. */
321 if (ISOPTSET(opttochk, OPT_om) || ISOPTSET(opttochk, OPT_ose)
322 || ISOPTSET(opttochk, OPT_osh))
330 * Check if the file name matches selection criteria.
333 select_filepath(char *path, uint32_t *optchkd)
335 struct re_entry *rep;
338 SETOPT((*optchkd), OPT_of);
340 if (ISOPTSET(opttochk, OPT_of)) {
342 TAILQ_FOREACH(rep, &re_head, re_glue) {
343 if (regexec(&rep->re_regexp, path, 0, NULL,
345 return (!rep->re_negate);
352 * Returns 1 if the following pass the selection rules:
361 select_hdr32(tokenstr_t tok, uint32_t *optchkd)
366 SETOPT((*optchkd), (OPT_A | OPT_a | OPT_b | OPT_c | OPT_m | OPT_v));
368 /* The A option overrides a, b and d. */
369 if (!ISOPTSET(opttochk, OPT_A)) {
370 if (ISOPTSET(opttochk, OPT_a)) {
371 if (difftime((time_t)tok.tt.hdr32.s, p_atime) < 0) {
372 /* Record was created before p_atime. */
377 if (ISOPTSET(opttochk, OPT_b)) {
378 if (difftime(p_btime, (time_t)tok.tt.hdr32.s) < 0) {
379 /* Record was created after p_btime. */
385 if (ISOPTSET(opttochk, OPT_c)) {
387 * Check if the classes represented by the event matches
390 if (au_preselect(tok.tt.hdr32.e_type, &maskp, AU_PRS_BOTH,
391 AU_PRS_USECACHE) != 1)
395 /* Check if event matches. */
396 if (ISOPTSET(opttochk, OPT_m)) {
398 for (ev = p_evec; ev < &p_evec[p_evec_used]; ev++)
399 if (tok.tt.hdr32.e_type == *ev)
409 select_return32(tokenstr_t tok_ret32, tokenstr_t tok_hdr32, uint32_t *optchkd)
413 SETOPT((*optchkd), (OPT_c));
414 if (tok_ret32.tt.ret32.status == 0)
415 sorf = AU_PRS_SUCCESS;
417 sorf = AU_PRS_FAILURE;
418 if (ISOPTSET(opttochk, OPT_c)) {
419 if (au_preselect(tok_hdr32.tt.hdr32.e_type, &maskp, sorf,
420 AU_PRS_USECACHE) != 1)
427 * Return 1 if checks for the the following succeed
436 select_proc32(tokenstr_t tok, uint32_t *optchkd)
439 SETOPT((*optchkd), (OPT_u | OPT_e | OPT_f | OPT_g | OPT_r | OPT_op));
441 if (!select_auid(tok.tt.proc32.auid))
443 if (!select_euid(tok.tt.proc32.euid))
445 if (!select_egid(tok.tt.proc32.egid))
447 if (!select_rgid(tok.tt.proc32.rgid))
449 if (!select_ruid(tok.tt.proc32.ruid))
451 if (!select_pidobj(tok.tt.proc32.pid))
457 * Return 1 if checks for the the following succeed
466 select_subj32(tokenstr_t tok, uint32_t *optchkd)
469 SETOPT((*optchkd), (OPT_u | OPT_e | OPT_f | OPT_g | OPT_r | OPT_j));
471 if (!select_auid(tok.tt.subj32.auid))
473 if (!select_euid(tok.tt.subj32.euid))
475 if (!select_egid(tok.tt.subj32.egid))
477 if (!select_rgid(tok.tt.subj32.rgid))
479 if (!select_ruid(tok.tt.subj32.ruid))
481 if (!select_subid(tok.tt.subj32.pid))
487 * Read each record from the audit trail. Check if it is selected after
488 * passing through each of the options
491 select_records(FILE *fp)
493 tokenstr_t tok_hdr32_copy;
503 while ((reclen = au_read_rec(fp, &buf)) != -1) {
507 while ((selected == 1) && (bytesread < reclen)) {
508 if (-1 == au_fetch_tok(&tok, buf + bytesread,
509 reclen - bytesread)) {
510 /* Is this an incomplete record? */
516 * For each token type we have have different
517 * selection criteria.
521 selected = select_hdr32(tok,
523 bcopy(&tok, &tok_hdr32_copy,
528 selected = select_proc32(tok,
533 selected = select_subj32(tok,
538 selected = select_ipcobj(
539 tok.tt.ipc.type, tok.tt.ipc.id,
544 selected = select_filepath(
545 tok.tt.path.path, &optchkd);
549 selected = select_return32(tok,
550 tok_hdr32_copy, &optchkd);
556 bytesread += tok.len;
558 /* Check if all the options were matched. */
559 print = ((selected == 1) && (!err) && (!(opttochk & ~optchkd)));
560 if (ISOPTSET(opttochk, OPT_v))
563 (void) fwrite(buf, 1, reclen, stdout);
570 * The -o option has the form object_type=object_value. Identify the object
574 parse_object_type(char *name, char *val)
579 if (!strcmp(name, FILEOBJ)) {
582 SETOPT(opttochk, OPT_of);
583 } else if (!strcmp(name, MSGQIDOBJ)) {
585 SETOPT(opttochk, OPT_om);
586 } else if (!strcmp(name, PIDOBJ)) {
588 SETOPT(opttochk, OPT_op);
589 } else if (!strcmp(name, SEMIDOBJ)) {
591 SETOPT(opttochk, OPT_ose);
592 } else if (!strcmp(name, SHMIDOBJ)) {
594 SETOPT(opttochk, OPT_osh);
595 } else if (!strcmp(name, SOCKOBJ)) {
597 SETOPT(opttochk, OPT_oso);
599 usage("unknown value for -o");
603 main(int argc, char **argv)
611 char *objval, *converr;
619 while ((ch = getopt(argc, argv, "Aa:b:c:d:e:f:g:j:m:o:r:u:v")) != -1) {
622 SETOPT(opttochk, OPT_A);
626 if (ISOPTSET(opttochk, OPT_a)) {
627 usage("d is exclusive with a and b");
629 SETOPT(opttochk, OPT_a);
630 bzero(&tm, sizeof(tm));
631 strptime(optarg, "%Y%m%d%H%M%S", &tm);
632 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S",
634 /* fprintf(stderr, "Time converted = %s\n", timestr); */
635 p_atime = mktime(&tm);
639 if (ISOPTSET(opttochk, OPT_b)) {
640 usage("d is exclusive with a and b");
642 SETOPT(opttochk, OPT_b);
643 bzero(&tm, sizeof(tm));
644 strptime(optarg, "%Y%m%d%H%M%S", &tm);
645 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S",
647 /* fprintf(stderr, "Time converted = %s\n", timestr); */
648 p_btime = mktime(&tm);
652 if (0 != getauditflagsbin(optarg, &maskp)) {
653 /* Incorrect class */
654 usage("Incorrect class");
656 SETOPT(opttochk, OPT_c);
660 if (ISOPTSET(opttochk, OPT_b) || ISOPTSET(opttochk,
662 usage("'d' is exclusive with 'a' and 'b'");
663 SETOPT(opttochk, OPT_d);
664 bzero(&tm, sizeof(tm));
665 strptime(optarg, "%Y%m%d", &tm);
666 strftime(timestr, sizeof(timestr), "%Y%m%d", &tm);
667 /* fprintf(stderr, "Time converted = %s\n", timestr); */
668 p_atime = mktime(&tm);
672 strftime(timestr, sizeof(timestr), "%Y%m%d", &tm);
673 /* fprintf(stderr, "Time converted = %s\n", timestr); */
674 p_btime = mktime(&tm);
678 p_euid = strtol(optarg, &converr, 10);
679 if (*converr != '\0') {
680 /* Try the actual name */
681 if ((pw = getpwnam(optarg)) == NULL)
685 SETOPT(opttochk, OPT_e);
689 p_egid = strtol(optarg, &converr, 10);
690 if (*converr != '\0') {
691 /* Try actual group name. */
692 if ((grp = getgrnam(optarg)) == NULL)
694 p_egid = grp->gr_gid;
696 SETOPT(opttochk, OPT_f);
700 p_rgid = strtol(optarg, &converr, 10);
701 if (*converr != '\0') {
702 /* Try actual group name. */
703 if ((grp = getgrnam(optarg)) == NULL)
705 p_rgid = grp->gr_gid;
707 SETOPT(opttochk, OPT_g);
711 p_subid = strtol(optarg, (char **)NULL, 10);
712 SETOPT(opttochk, OPT_j);
716 if (p_evec == NULL) {
718 p_evec = malloc(sizeof(*etp) * p_evec_alloc);
721 } else if (p_evec_alloc == p_evec_used) {
723 p_evec = realloc(p_evec,
724 sizeof(*p_evec) * p_evec_alloc);
728 etp = &p_evec[p_evec_used++];
729 *etp = strtol(optarg, (char **)NULL, 10);
731 /* Could be the string representation. */
732 n = getauevnonam(optarg);
734 usage("Incorrect event name");
737 SETOPT(opttochk, OPT_m);
741 objval = strchr(optarg, '=');
742 if (objval != NULL) {
745 parse_object_type(optarg, objval);
750 p_ruid = strtol(optarg, &converr, 10);
751 if (*converr != '\0') {
752 if ((pw = getpwnam(optarg)) == NULL)
756 SETOPT(opttochk, OPT_r);
760 p_auid = strtol(optarg, &converr, 10);
761 if (*converr != '\0') {
762 if ((pw = getpwnam(optarg)) == NULL)
766 SETOPT(opttochk, OPT_u);
770 SETOPT(opttochk, OPT_v);
775 usage("Unknown option");
782 if (select_records(stdin) == -1)
784 "Couldn't select records from stdin");
789 * XXX: We should actually be merging records here.
791 for (i = 0; i < argc; i++) {
793 fp = fopen(fname, "r");
795 errx(EXIT_FAILURE, "Couldn't open %s", fname);
796 if (select_records(fp) == -1) {
797 errx(EXIT_FAILURE, "Couldn't select records %s",