xref: /titanic_50/usr/src/cmd/praudit/main.c (revision 0cfb4e8b28ab16b0cb224619362e8135673714c3)
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*0cfb4e8bSjf206706  * Common Development and Distribution License (the "License").
6*0cfb4e8bSjf206706  * 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*0cfb4e8bSjf206706  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <dirent.h>
297c478bd9Sstevel@tonic-gate #include <locale.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
407c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
417c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "praudit.h"
447c478bd9Sstevel@tonic-gate #include "toktable.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int	process_options(int *argc, char *argv[], char *names[]);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int	input_mode;	/* audit file source */
497c478bd9Sstevel@tonic-gate static int	format = PRF_DEFAULTM;	/* output mode */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char	SEPARATOR[SEP_SIZE] = ",";	/* field separator */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
567c478bd9Sstevel@tonic-gate  * praudit  -  display contents of audit trail file
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * main() - main control
597c478bd9Sstevel@tonic-gate  * input:    - command line input:   praudit -r|s -l -x -ddelim. -c filename(s)
607c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate int
647c478bd9Sstevel@tonic-gate main(int argc, char **argv)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	int	i = 0, retstat;
677c478bd9Sstevel@tonic-gate 	char	*names[MAXFILENAMES];
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/* Internationalization */
707c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
717c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * get audit file names
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 	if ((retstat = process_options(&argc, argv, names)) == 0) {
767c478bd9Sstevel@tonic-gate 		if (format & PRF_XMLM)
777c478bd9Sstevel@tonic-gate 			print_audit_xml_prolog();
787c478bd9Sstevel@tonic-gate 		do {
797c478bd9Sstevel@tonic-gate 			retstat = 0;
807c478bd9Sstevel@tonic-gate 			/*
817c478bd9Sstevel@tonic-gate 			 * process each audit file
827c478bd9Sstevel@tonic-gate 			 */
837c478bd9Sstevel@tonic-gate 			if (input_mode == FILEMODE) {
847c478bd9Sstevel@tonic-gate 				if (freopen(names[i], "r", stdin) == NULL) {
857c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
867c478bd9Sstevel@tonic-gate 					    gettext("praudit: Can't assign %s "
877c478bd9Sstevel@tonic-gate 					    "to stdin.\n"), names[i]);
88*0cfb4e8bSjf206706 					exit(1);
897c478bd9Sstevel@tonic-gate 				}
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 			/*
937c478bd9Sstevel@tonic-gate 			 * Call the library routine to format the
947c478bd9Sstevel@tonic-gate 			 * audit data from stdin and print to stdout
957c478bd9Sstevel@tonic-gate 			 */
967c478bd9Sstevel@tonic-gate 			retstat = print_audit(format, SEPARATOR);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		} while ((++i < argc) && retstat >= 0);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	if ((retstat == 0) && (format & PRF_XMLM))
1017c478bd9Sstevel@tonic-gate 		print_audit_xml_ending();
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (retstat == -2) {
1047c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\nusage: praudit [-r/-s] [-l] [-x] "
1057c478bd9Sstevel@tonic-gate 		    "[-ddel] [-c] filename...\n"));
106*0cfb4e8bSjf206706 		exit(1);
107*0cfb4e8bSjf206706 	} else if (retstat < 0) {
108*0cfb4e8bSjf206706 		exit(1);
1097c478bd9Sstevel@tonic-gate 	}
110*0cfb4e8bSjf206706 	return (0);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
1167c478bd9Sstevel@tonic-gate  * process_options() - get command line flags and file names
1177c478bd9Sstevel@tonic-gate  * input:    - praudit [-r]/[-s] [-l] [-x] [-ddel] [-c] {audit file names}
1187c478bd9Sstevel@tonic-gate  * output:   - {audit file names}
1197c478bd9Sstevel@tonic-gate  * globals set:	format:		RAWM / SHORTM / XML / ONELINE or DEFAULTM
1207c478bd9Sstevel@tonic-gate  *			SEPARATOR:  default, ",", set here if
1217c478bd9Sstevel@tonic-gate  *				user specified
1227c478bd9Sstevel@tonic-gate  * NOTE: no changes required here for new audit record format
1237c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate int
1267c478bd9Sstevel@tonic-gate process_options(int *argc, char **argv, char **names)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	int	c, returnstat = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * check for flags
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	while ((c = getopt(*argc, argv, "crslxd:")) != -1) {
1357c478bd9Sstevel@tonic-gate 		switch (c) {
1367c478bd9Sstevel@tonic-gate 		case 'c':
1377c478bd9Sstevel@tonic-gate 			format |= PRF_NOCACHE;	/* turn off cache */
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case 'r':
1407c478bd9Sstevel@tonic-gate 			if (format & PRF_SHORTM)
1417c478bd9Sstevel@tonic-gate 				returnstat = -2;
1427c478bd9Sstevel@tonic-gate 			else
1437c478bd9Sstevel@tonic-gate 				format |= PRF_RAWM;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 's':
1467c478bd9Sstevel@tonic-gate 			if (format & PRF_RAWM)
1477c478bd9Sstevel@tonic-gate 				returnstat = -2;
1487c478bd9Sstevel@tonic-gate 			else
1497c478bd9Sstevel@tonic-gate 				format |= PRF_SHORTM;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'l':
1527c478bd9Sstevel@tonic-gate 			format |= PRF_ONELINE;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'x':
1557c478bd9Sstevel@tonic-gate 			format |= PRF_XMLM;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case 'd':
1587c478bd9Sstevel@tonic-gate 			if (strlen(optarg) < sizeof (SEPARATOR))
1597c478bd9Sstevel@tonic-gate 				(void) strlcpy(SEPARATOR, optarg,
1607c478bd9Sstevel@tonic-gate 				    sizeof (SEPARATOR));
1617c478bd9Sstevel@tonic-gate 			else {
1627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1637c478bd9Sstevel@tonic-gate 				    gettext("praudit: Delimiter too "
1647c478bd9Sstevel@tonic-gate 				    "long.  Using default.\n"));
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		default:
1687c478bd9Sstevel@tonic-gate 			returnstat = -2;
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	argv = &argv[optind - 1];
1747c478bd9Sstevel@tonic-gate 	*argc -= optind;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (*argc > MAXFILENAMES) {
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("praudit: Too many file "
1787c478bd9Sstevel@tonic-gate 		    "names.\n"));
1797c478bd9Sstevel@tonic-gate 		return (-1);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (*argc > 0) {
1827c478bd9Sstevel@tonic-gate 		int count = *argc;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		input_mode = FILEMODE;
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * copy file names from command line
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		do {
1897c478bd9Sstevel@tonic-gate 			*names++ = *++argv;
1907c478bd9Sstevel@tonic-gate 		} while (--count > 0);
1917c478bd9Sstevel@tonic-gate 	} else
1927c478bd9Sstevel@tonic-gate 		input_mode = PIPEMODE;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (returnstat);
1957c478bd9Sstevel@tonic-gate }
196