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