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