xref: /illumos-gate/usr/src/cmd/praudit/main.c (revision 8bb3e7e3)
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
50cfb4e8bSjf  * Common Development and Distribution License (the "License").
60cfb4e8bSjf  * 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  */
21*8bb3e7e3SPeter Tribble /*
22*8bb3e7e3SPeter Tribble  * Copyright (c) 2019 Peter Tribble.
23*8bb3e7e3SPeter Tribble  */
247c478bd9Sstevel@tonic-gate /*
25cecaea53SMarek Pospisil  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <dirent.h>
29*8bb3e7e3SPeter Tribble #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <locale.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
417c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
427c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "praudit.h"
457c478bd9Sstevel@tonic-gate #include "toktable.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int	process_options(int *argc, char *argv[], char *names[]);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int	input_mode;	/* audit file source */
507c478bd9Sstevel@tonic-gate static int	format = PRF_DEFAULTM;	/* output mode */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static char	SEPARATOR[SEP_SIZE] = ",";	/* field separator */
537c478bd9Sstevel@tonic-gate 
54*8bb3e7e3SPeter Tribble static FILE	*gf = NULL;
55*8bb3e7e3SPeter Tribble static FILE	*pf = NULL;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
597c478bd9Sstevel@tonic-gate  * praudit  -  display contents of audit trail file
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * main() - main control
62*8bb3e7e3SPeter Tribble  * input: - command line input:
63*8bb3e7e3SPeter Tribble  *    praudit -r|s -l -x -ddelim. -p pwfile -g grpfile -c filename(s)
647c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)687c478bd9Sstevel@tonic-gate main(int argc, char **argv)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int	i = 0, retstat;
717c478bd9Sstevel@tonic-gate 	char	*names[MAXFILENAMES];
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/* Internationalization */
747c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
757c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * get audit file names
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 	if ((retstat = process_options(&argc, argv, names)) == 0) {
80*8bb3e7e3SPeter Tribble 		if (pf != NULL) {
81*8bb3e7e3SPeter Tribble 			errno = 0;
82*8bb3e7e3SPeter Tribble 			loadnames(pf);
83*8bb3e7e3SPeter Tribble 			(void) fclose(pf);
84*8bb3e7e3SPeter Tribble 			if (errno != 0) {
85*8bb3e7e3SPeter Tribble 				(void) fprintf(stderr,
86*8bb3e7e3SPeter Tribble 				    gettext("praudit: Problem reading passwd "
87*8bb3e7e3SPeter Tribble 				    "file.\n"));
88*8bb3e7e3SPeter Tribble 				exit(1);
89*8bb3e7e3SPeter Tribble 			}
90*8bb3e7e3SPeter Tribble 		}
91*8bb3e7e3SPeter Tribble 		if (gf != NULL) {
92*8bb3e7e3SPeter Tribble 			errno = 0;
93*8bb3e7e3SPeter Tribble 			loadgroups(gf);
94*8bb3e7e3SPeter Tribble 			(void) fclose(gf);
95*8bb3e7e3SPeter Tribble 			if (errno != 0) {
96*8bb3e7e3SPeter Tribble 				(void) fprintf(stderr,
97*8bb3e7e3SPeter Tribble 				    gettext("praudit: Problem reading group "
98*8bb3e7e3SPeter Tribble 				    "file.\n"));
99*8bb3e7e3SPeter Tribble 				exit(1);
100*8bb3e7e3SPeter Tribble 			}
101*8bb3e7e3SPeter Tribble 		}
1027c478bd9Sstevel@tonic-gate 		if (format & PRF_XMLM)
1037c478bd9Sstevel@tonic-gate 			print_audit_xml_prolog();
1047c478bd9Sstevel@tonic-gate 		do {
1057c478bd9Sstevel@tonic-gate 			retstat = 0;
1067c478bd9Sstevel@tonic-gate 			/*
1077c478bd9Sstevel@tonic-gate 			 * process each audit file
1087c478bd9Sstevel@tonic-gate 			 */
1097c478bd9Sstevel@tonic-gate 			if (input_mode == FILEMODE) {
1107c478bd9Sstevel@tonic-gate 				if (freopen(names[i], "r", stdin) == NULL) {
1117c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
112cecaea53SMarek Pospisil 					    gettext("praudit: Cannot associate "
113cecaea53SMarek Pospisil 					    "stdin with %s: %s\n"),
114cecaea53SMarek Pospisil 					    names[i], strerror(errno));
1150cfb4e8bSjf 					exit(1);
1167c478bd9Sstevel@tonic-gate 				}
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 			/*
1207c478bd9Sstevel@tonic-gate 			 * Call the library routine to format the
1217c478bd9Sstevel@tonic-gate 			 * audit data from stdin and print to stdout
1227c478bd9Sstevel@tonic-gate 			 */
1237c478bd9Sstevel@tonic-gate 			retstat = print_audit(format, SEPARATOR);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		} while ((++i < argc) && retstat >= 0);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	if ((retstat == 0) && (format & PRF_XMLM))
1287c478bd9Sstevel@tonic-gate 		print_audit_xml_ending();
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (retstat == -2) {
1317c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\nusage: praudit [-r/-s] [-l] [-x] "
132*8bb3e7e3SPeter Tribble 		    "[-ddel] [-p file] [-g file] [-c] filename...\n"));
1330cfb4e8bSjf 		exit(1);
1340cfb4e8bSjf 	} else if (retstat < 0) {
1350cfb4e8bSjf 		exit(1);
1367c478bd9Sstevel@tonic-gate 	}
1370cfb4e8bSjf 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
1437c478bd9Sstevel@tonic-gate  * process_options() - get command line flags and file names
144*8bb3e7e3SPeter Tribble  * input:    - praudit [-r]/[-s] [-l] [-x] [-ddel] [-c]
145*8bb3e7e3SPeter Tribble  *                     -p pwfile -g grpfile -c {audit file names}
1467c478bd9Sstevel@tonic-gate  * output:   - {audit file names}
1477c478bd9Sstevel@tonic-gate  * globals set:	format:		RAWM / SHORTM / XML / ONELINE or DEFAULTM
1487c478bd9Sstevel@tonic-gate  *			SEPARATOR:  default, ",", set here if
1497c478bd9Sstevel@tonic-gate  *				user specified
1507c478bd9Sstevel@tonic-gate  * NOTE: no changes required here for new audit record format
1517c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate int
process_options(int * argc,char ** argv,char ** names)1547c478bd9Sstevel@tonic-gate process_options(int *argc, char **argv, char **names)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	int	c, returnstat = 0;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * check for flags
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 
162*8bb3e7e3SPeter Tribble 	while ((c = getopt(*argc, argv, "crslxd:g:p:")) != -1) {
1637c478bd9Sstevel@tonic-gate 		switch (c) {
1647c478bd9Sstevel@tonic-gate 		case 'c':
1657c478bd9Sstevel@tonic-gate 			format |= PRF_NOCACHE;	/* turn off cache */
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		case 'r':
1687c478bd9Sstevel@tonic-gate 			if (format & PRF_SHORTM)
1697c478bd9Sstevel@tonic-gate 				returnstat = -2;
1707c478bd9Sstevel@tonic-gate 			else
1717c478bd9Sstevel@tonic-gate 				format |= PRF_RAWM;
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 		case 's':
1747c478bd9Sstevel@tonic-gate 			if (format & PRF_RAWM)
1757c478bd9Sstevel@tonic-gate 				returnstat = -2;
1767c478bd9Sstevel@tonic-gate 			else
1777c478bd9Sstevel@tonic-gate 				format |= PRF_SHORTM;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		case 'l':
1807c478bd9Sstevel@tonic-gate 			format |= PRF_ONELINE;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 		case 'x':
1837c478bd9Sstevel@tonic-gate 			format |= PRF_XMLM;
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		case 'd':
1867c478bd9Sstevel@tonic-gate 			if (strlen(optarg) < sizeof (SEPARATOR))
1877c478bd9Sstevel@tonic-gate 				(void) strlcpy(SEPARATOR, optarg,
1887c478bd9Sstevel@tonic-gate 				    sizeof (SEPARATOR));
1897c478bd9Sstevel@tonic-gate 			else {
1907c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1917c478bd9Sstevel@tonic-gate 				    gettext("praudit: Delimiter too "
1927c478bd9Sstevel@tonic-gate 				    "long.  Using default.\n"));
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 			break;
195*8bb3e7e3SPeter Tribble 		case 'g':
196*8bb3e7e3SPeter Tribble 			if ((gf = fopen(optarg, "r")) == NULL) {
197*8bb3e7e3SPeter Tribble 				(void) fprintf(stderr, gettext("praudit: Cannot"
198*8bb3e7e3SPeter Tribble 				    " open specified group file.\n"));
199*8bb3e7e3SPeter Tribble 				return (-1);
200*8bb3e7e3SPeter Tribble 			}
201*8bb3e7e3SPeter Tribble 			break;
202*8bb3e7e3SPeter Tribble 		case 'p':
203*8bb3e7e3SPeter Tribble 			if ((pf = fopen(optarg, "r")) == NULL) {
204*8bb3e7e3SPeter Tribble 				(void) fprintf(stderr, gettext("praudit: Cannot"
205*8bb3e7e3SPeter Tribble 				    " open specified passwd file.\n"));
206*8bb3e7e3SPeter Tribble 				return (-1);
207*8bb3e7e3SPeter Tribble 			}
208*8bb3e7e3SPeter Tribble 			break;
2097c478bd9Sstevel@tonic-gate 		default:
2107c478bd9Sstevel@tonic-gate 			returnstat = -2;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	argv = &argv[optind - 1];
2167c478bd9Sstevel@tonic-gate 	*argc -= optind;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (*argc > MAXFILENAMES) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("praudit: Too many file "
2207c478bd9Sstevel@tonic-gate 		    "names.\n"));
2217c478bd9Sstevel@tonic-gate 		return (-1);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	if (*argc > 0) {
2247c478bd9Sstevel@tonic-gate 		int count = *argc;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		input_mode = FILEMODE;
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * copy file names from command line
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		do {
2317c478bd9Sstevel@tonic-gate 			*names++ = *++argv;
2327c478bd9Sstevel@tonic-gate 		} while (--count > 0);
2337c478bd9Sstevel@tonic-gate 	} else
2347c478bd9Sstevel@tonic-gate 		input_mode = PIPEMODE;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (returnstat);
2377c478bd9Sstevel@tonic-gate }
238