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