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