xref: /illumos-gate/usr/src/cmd/auditreduce/regex2.c (revision c0dd49bdd68c0d758a67d56f07826f3b45cfc664)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Extend regular expression matching for the file objects to allow
28  * multiple regular expressions (instead of just 1), and to not select
29  * regular expressions starting with a "~".  This will allow adminstrator
30  * to exclude uninteresting files from the audit trail.
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <libgen.h>
36 
37 struct exp {
38 	char *s;	/* The regular is expression */
39 	int not;	/* Exclude if matched? */
40 	char *comp;	/* The compiled regular expression */
41 };
42 
43 static char SEP = ',';		/* separator used between reg exprs */
44 static char NOT = '~';		/* Character used to exclude rex exprs */
45 static int compile = 1;		/* Must we compile the expressions */
46 
47 static char *fexp = NULL;	/* full list of regular expressions */
48 static int nexp = 1;		/* number of regular expressions in fexp */
49 static struct exp *p_exp = NULL; /* list of individual expressions */
50 
51 char *
52 re_comp2(s)
53 	char *s;
54 {
55 	char *p;
56 	int i;
57 	static char *er = "regcmp: error";
58 
59 	compile = 1;
60 	if (p_exp != NULL) {
61 		for (i = 0; i < nexp; i++)
62 			if (p_exp[i].comp != NULL)
63 				free(p_exp[i].comp);
64 		free(p_exp);
65 	}
66 	if (fexp != NULL) {
67 		free(fexp);
68 	}
69 	fexp = strdup(s);
70 	for (p = fexp, nexp = 1; *p != '\0'; p++) {
71 		if (*p == SEP) {
72 			nexp++;
73 		}
74 	}
75 	p_exp = (struct exp *)malloc(nexp * sizeof (struct exp));
76 	for (i = 0, p = fexp; *p != '\0'; i++) {
77 		p_exp[i].comp = NULL;
78 		if (*p == NOT) {
79 			p++;
80 			p_exp[i].not = 1;
81 		} else {
82 			p_exp[i].not = 0;
83 		}
84 		p_exp[i].s = p;
85 		while (*p != SEP && *p != '\0')
86 			p++;
87 		if (*p == SEP) {
88 			*p = '\0';
89 			p++;
90 		}
91 		if (regcmp(p_exp[i].s, NULL) == NULL)
92 			return (er);
93 	}
94 	return (NULL);
95 }
96 
97 int
98 re_exec2(s)
99 	char *s;
100 {
101 	int i;
102 	char *ret;
103 
104 	if (compile) {
105 		for (i = 0; i < nexp; i++) {
106 			if ((p_exp[i].comp = regcmp(p_exp[i].s, NULL)) == NULL)
107 				return (-1);
108 		}
109 		compile = 0;
110 	}
111 	for (i = 0; i < nexp; i++) {
112 		ret = regex(p_exp[i].comp, s);
113 		if (ret != NULL) {
114 			return (!p_exp[i].not);
115 		}
116 	}
117 
118 	/* no match and no more to check */
119 	return (0);
120 
121 }
122