xref: /titanic_50/usr/src/cmd/auditreduce/regex2.c (revision dfc7be02c381b7c61998fdb9ecbf069933d62381)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*dfc7be02SJan Friedel  * Common Development and Distribution License (the "License").
6*dfc7be02SJan Friedel  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*dfc7be02SJan Friedel  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Extend regular expression matching for the file objects to allow
287c478bd9Sstevel@tonic-gate  * multiple regular expressions (instead of just 1), and to not select
297c478bd9Sstevel@tonic-gate  * regular expressions starting with a "~".  This will allow adminstrator
307c478bd9Sstevel@tonic-gate  * to exclude uninteresting files from the audit trail.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate struct exp {
387c478bd9Sstevel@tonic-gate 	char *s;	/* The regular is expression */
397c478bd9Sstevel@tonic-gate 	int not;	/* Exclude if matched? */
407c478bd9Sstevel@tonic-gate 	char *comp;	/* The compiled regular expression */
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static char SEP = ',';		/* separator used between reg exprs */
447c478bd9Sstevel@tonic-gate static char NOT = '~';		/* Character used to exclude rex exprs */
457c478bd9Sstevel@tonic-gate static int compile = 1;		/* Must we compile the expressions */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static char *fexp = NULL;	/* full list of regular expressions */
487c478bd9Sstevel@tonic-gate static int nexp = 1;		/* number of regular expressions in fexp */
497c478bd9Sstevel@tonic-gate static struct exp *p_exp = NULL; /* list of individual expressions */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate char *
re_comp2(s)527c478bd9Sstevel@tonic-gate re_comp2(s)
537c478bd9Sstevel@tonic-gate 	char *s;
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	char *p;
567c478bd9Sstevel@tonic-gate 	int i;
577c478bd9Sstevel@tonic-gate 	static char *er = "regcmp: error";
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	compile = 1;
607c478bd9Sstevel@tonic-gate 	if (p_exp != NULL) {
617c478bd9Sstevel@tonic-gate 		for (i = 0; i < nexp; i++)
627c478bd9Sstevel@tonic-gate 			if (p_exp[i].comp != NULL)
637c478bd9Sstevel@tonic-gate 				free(p_exp[i].comp);
647c478bd9Sstevel@tonic-gate 		free(p_exp);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 	if (fexp != NULL) {
677c478bd9Sstevel@tonic-gate 		free(fexp);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	fexp = strdup(s);
707c478bd9Sstevel@tonic-gate 	for (p = fexp, nexp = 1; *p != '\0'; p++) {
717c478bd9Sstevel@tonic-gate 		if (*p == SEP) {
727c478bd9Sstevel@tonic-gate 			nexp++;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	p_exp = (struct exp *)malloc(nexp * sizeof (struct exp));
767c478bd9Sstevel@tonic-gate 	for (i = 0, p = fexp; *p != '\0'; i++) {
777c478bd9Sstevel@tonic-gate 		p_exp[i].comp = NULL;
787c478bd9Sstevel@tonic-gate 		if (*p == NOT) {
797c478bd9Sstevel@tonic-gate 			p++;
807c478bd9Sstevel@tonic-gate 			p_exp[i].not = 1;
817c478bd9Sstevel@tonic-gate 		} else {
827c478bd9Sstevel@tonic-gate 			p_exp[i].not = 0;
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 		p_exp[i].s = p;
857c478bd9Sstevel@tonic-gate 		while (*p != SEP && *p != '\0')
867c478bd9Sstevel@tonic-gate 			p++;
877c478bd9Sstevel@tonic-gate 		if (*p == SEP) {
887c478bd9Sstevel@tonic-gate 			*p = '\0';
897c478bd9Sstevel@tonic-gate 			p++;
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		if (regcmp(p_exp[i].s, NULL) == NULL)
927c478bd9Sstevel@tonic-gate 			return (er);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	return (NULL);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
re_exec2(s)987c478bd9Sstevel@tonic-gate re_exec2(s)
997c478bd9Sstevel@tonic-gate 	char *s;
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int i;
1027c478bd9Sstevel@tonic-gate 	char *ret;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (compile) {
1057c478bd9Sstevel@tonic-gate 		for (i = 0; i < nexp; i++) {
1067c478bd9Sstevel@tonic-gate 			if ((p_exp[i].comp = regcmp(p_exp[i].s, NULL)) == NULL)
1077c478bd9Sstevel@tonic-gate 				return (-1);
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 		compile = 0;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	for (i = 0; i < nexp; i++) {
1127c478bd9Sstevel@tonic-gate 		ret = regex(p_exp[i].comp, s);
1137c478bd9Sstevel@tonic-gate 		if (ret != NULL) {
1147c478bd9Sstevel@tonic-gate 			return (!p_exp[i].not);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* no match and no more to check */
1197c478bd9Sstevel@tonic-gate 	return (0);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate }
122