xref: /onnv-gate/usr/src/cmd/auditreduce/regex2.c (revision 11706:cd830a066051)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11706SJan.Friedel@Sun.COM  * Common Development and Distribution License (the "License").
6*11706SJan.Friedel@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11706SJan.Friedel@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Extend regular expression matching for the file objects to allow
280Sstevel@tonic-gate  * multiple regular expressions (instead of just 1), and to not select
290Sstevel@tonic-gate  * regular expressions starting with a "~".  This will allow adminstrator
300Sstevel@tonic-gate  * to exclude uninteresting files from the audit trail.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <libgen.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate struct exp {
380Sstevel@tonic-gate 	char *s;	/* The regular is expression */
390Sstevel@tonic-gate 	int not;	/* Exclude if matched? */
400Sstevel@tonic-gate 	char *comp;	/* The compiled regular expression */
410Sstevel@tonic-gate };
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static char SEP = ',';		/* separator used between reg exprs */
440Sstevel@tonic-gate static char NOT = '~';		/* Character used to exclude rex exprs */
450Sstevel@tonic-gate static int compile = 1;		/* Must we compile the expressions */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static char *fexp = NULL;	/* full list of regular expressions */
480Sstevel@tonic-gate static int nexp = 1;		/* number of regular expressions in fexp */
490Sstevel@tonic-gate static struct exp *p_exp = NULL; /* list of individual expressions */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate char *
re_comp2(s)520Sstevel@tonic-gate re_comp2(s)
530Sstevel@tonic-gate 	char *s;
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	char *p;
560Sstevel@tonic-gate 	int i;
570Sstevel@tonic-gate 	static char *er = "regcmp: error";
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	compile = 1;
600Sstevel@tonic-gate 	if (p_exp != NULL) {
610Sstevel@tonic-gate 		for (i = 0; i < nexp; i++)
620Sstevel@tonic-gate 			if (p_exp[i].comp != NULL)
630Sstevel@tonic-gate 				free(p_exp[i].comp);
640Sstevel@tonic-gate 		free(p_exp);
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 	if (fexp != NULL) {
670Sstevel@tonic-gate 		free(fexp);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	fexp = strdup(s);
700Sstevel@tonic-gate 	for (p = fexp, nexp = 1; *p != '\0'; p++) {
710Sstevel@tonic-gate 		if (*p == SEP) {
720Sstevel@tonic-gate 			nexp++;
730Sstevel@tonic-gate 		}
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	p_exp = (struct exp *)malloc(nexp * sizeof (struct exp));
760Sstevel@tonic-gate 	for (i = 0, p = fexp; *p != '\0'; i++) {
770Sstevel@tonic-gate 		p_exp[i].comp = NULL;
780Sstevel@tonic-gate 		if (*p == NOT) {
790Sstevel@tonic-gate 			p++;
800Sstevel@tonic-gate 			p_exp[i].not = 1;
810Sstevel@tonic-gate 		} else {
820Sstevel@tonic-gate 			p_exp[i].not = 0;
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 		p_exp[i].s = p;
850Sstevel@tonic-gate 		while (*p != SEP && *p != '\0')
860Sstevel@tonic-gate 			p++;
870Sstevel@tonic-gate 		if (*p == SEP) {
880Sstevel@tonic-gate 			*p = '\0';
890Sstevel@tonic-gate 			p++;
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 		if (regcmp(p_exp[i].s, NULL) == NULL)
920Sstevel@tonic-gate 			return (er);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	return (NULL);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate int
re_exec2(s)980Sstevel@tonic-gate re_exec2(s)
990Sstevel@tonic-gate 	char *s;
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int i;
1020Sstevel@tonic-gate 	char *ret;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (compile) {
1050Sstevel@tonic-gate 		for (i = 0; i < nexp; i++) {
1060Sstevel@tonic-gate 			if ((p_exp[i].comp = regcmp(p_exp[i].s, NULL)) == NULL)
1070Sstevel@tonic-gate 				return (-1);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		compile = 0;
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	for (i = 0; i < nexp; i++) {
1120Sstevel@tonic-gate 		ret = regex(p_exp[i].comp, s);
1130Sstevel@tonic-gate 		if (ret != NULL) {
1140Sstevel@tonic-gate 			return (!p_exp[i].not);
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/* no match and no more to check */
1190Sstevel@tonic-gate 	return (0);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate }
122