xref: /titanic_41/usr/src/cmd/praudit/main.c (revision a2bb96e7d59f447f59bc306e53b7d00e38594667)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1993-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <dirent.h>
30 #include <locale.h>
31 #include <libintl.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 
37 #include <sys/types.h>
38 #include <sys/file.h>
39 
40 #include <bsm/audit.h>
41 #include <bsm/audit_record.h>
42 #include <bsm/libbsm.h>
43 
44 #include "praudit.h"
45 #include "toktable.h"
46 
47 static int	process_options(int *argc, char *argv[], char *names[]);
48 
49 static int	input_mode;	/* audit file source */
50 static int	format = PRF_DEFAULTM;	/* output mode */
51 
52 static char	SEPARATOR[SEP_SIZE] = ",";	/* field separator */
53 
54 
55 /*
56  * ----------------------------------------------------------------------
57  * praudit  -  display contents of audit trail file
58  *
59  * main() - main control
60  * input:    - command line input:   praudit -r|s -l -x -ddelim. -c filename(s)
61  * ----------------------------------------------------------------------
62  */
63 
64 int
65 main(int argc, char **argv)
66 {
67 	int	i = 0, retstat;
68 	char	*names[MAXFILENAMES];
69 
70 	/* Internationalization */
71 	(void) setlocale(LC_ALL, "");
72 	(void) textdomain(TEXT_DOMAIN);
73 	/*
74 	 * get audit file names
75 	 */
76 	if ((retstat = process_options(&argc, argv, names)) == 0) {
77 		if (format & PRF_XMLM)
78 			print_audit_xml_prolog();
79 		do {
80 			retstat = 0;
81 			/*
82 			 * process each audit file
83 			 */
84 			if (input_mode == FILEMODE) {
85 				if (freopen(names[i], "r", stdin) == NULL) {
86 					(void) fprintf(stderr,
87 					    gettext("praudit: Can't assign %s "
88 					    "to stdin.\n"), names[i]);
89 					break;
90 				}
91 			}
92 
93 			/*
94 			 * Call the library routine to format the
95 			 * audit data from stdin and print to stdout
96 			 */
97 			retstat = print_audit(format, SEPARATOR);
98 
99 		} while ((++i < argc) && retstat >= 0);
100 	}
101 	if ((retstat == 0) && (format & PRF_XMLM))
102 		print_audit_xml_ending();
103 
104 	if (retstat == -2) {
105 		(void) printf(gettext("\nusage: praudit [-r/-s] [-l] [-x] "
106 		    "[-ddel] [-c] filename...\n"));
107 		retstat = -1;
108 	}
109 	if (retstat == 1)
110 		retstat = 0;
111 	return (retstat);
112 }
113 
114 
115 /*
116  * -------------------------------------------------------------------
117  * process_options() - get command line flags and file names
118  * input:    - praudit [-r]/[-s] [-l] [-x] [-ddel] [-c] {audit file names}
119  * output:   - {audit file names}
120  * globals set:	format:		RAWM / SHORTM / XML / ONELINE or DEFAULTM
121  *			SEPARATOR:  default, ",", set here if
122  *				user specified
123  * NOTE: no changes required here for new audit record format
124  * -------------------------------------------------------------------
125  */
126 int
127 process_options(int *argc, char **argv, char **names)
128 {
129 	int	c, returnstat = 0;
130 
131 	/*
132 	 * check for flags
133 	 */
134 
135 	while ((c = getopt(*argc, argv, "crslxd:")) != -1) {
136 		switch (c) {
137 		case 'c':
138 			format |= PRF_NOCACHE;	/* turn off cache */
139 			break;
140 		case 'r':
141 			if (format & PRF_SHORTM)
142 				returnstat = -2;
143 			else
144 				format |= PRF_RAWM;
145 			break;
146 		case 's':
147 			if (format & PRF_RAWM)
148 				returnstat = -2;
149 			else
150 				format |= PRF_SHORTM;
151 			break;
152 		case 'l':
153 			format |= PRF_ONELINE;
154 			break;
155 		case 'x':
156 			format |= PRF_XMLM;
157 			break;
158 		case 'd':
159 			if (strlen(optarg) < sizeof (SEPARATOR))
160 				(void) strlcpy(SEPARATOR, optarg,
161 				    sizeof (SEPARATOR));
162 			else {
163 				(void) fprintf(stderr,
164 				    gettext("praudit: Delimiter too "
165 				    "long.  Using default.\n"));
166 			}
167 			break;
168 		default:
169 			returnstat = -2;
170 			break;
171 		}
172 	}
173 
174 	argv = &argv[optind - 1];
175 	*argc -= optind;
176 
177 	if (*argc > MAXFILENAMES) {
178 		(void) fprintf(stderr, gettext("praudit: Too many file "
179 		    "names.\n"));
180 		return (-1);
181 	}
182 	if (*argc > 0) {
183 		int count = *argc;
184 
185 		input_mode = FILEMODE;
186 		/*
187 		 * copy file names from command line
188 		 */
189 		do {
190 			*names++ = *++argv;
191 		} while (--count > 0);
192 	} else
193 		input_mode = PIPEMODE;
194 
195 	return (returnstat);
196 }
197