xref: /titanic_50/usr/src/cmd/auditreduce/option.c (revision 35fe197b91640f2efc8c0b3849eee882e373c729)
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 2006 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 /*
29  * Command line option processing for auditreduce.
30  * The entry point is process_options(), which is called by main().
31  * Process_options() is the only function visible outside this module.
32  */
33 
34 #include <locale.h>
35 #include <sys/zone.h>	/* for max zonename length */
36 #include "auditr.h"
37 
38 /*
39  * Object entry.
40  * Maps object strings specified on the command line to a flag
41  * used when searching by object type.
42  */
43 
44 struct obj_ent {
45 	char	*obj_str; /* string specified on the command line */
46 	int	obj_flag; /* flag used when searching */
47 };
48 
49 typedef struct obj_ent obj_ent_t;
50 
51 /*
52  * Supports searches by object type.
53  */
54 static obj_ent_t obj_tbl[] = {
55 			{ "file", OBJ_PATH },
56 			{ "filegroup", OBJ_FGROUP },
57 			{ "fileowner", OBJ_FOWNER },
58 			{ "lp", OBJ_LP   },
59 			{ "msgqid", OBJ_MSG  },
60 			{ "msgqgroup", OBJ_MSGGROUP },
61 			{ "msgqowner", OBJ_MSGOWNER },
62 			{ "path", OBJ_PATH },
63 			{ "pid", OBJ_PROC },
64 			{ "procgroup", OBJ_PGROUP },
65 			{ "procowner", OBJ_POWNER },
66 			{ "semid", OBJ_SEM  },
67 			{ "semgroup", OBJ_SEMGROUP  },
68 			{ "semowner", OBJ_SEMOWNER  },
69 			{ "shmid", OBJ_SHM  },
70 			{ "shmgroup", OBJ_SHMGROUP  },
71 			{ "shmowner", OBJ_SHMOWNER  },
72 			{ "sock", OBJ_SOCK } };
73 
74 extern int	derive_date(char *, struct tm *);
75 extern int	parse_time(char *, int);
76 extern char	*re_comp2(char *);
77 extern time_t	tm_to_secs(struct tm *);
78 
79 static int	a_isnum(char *, int);
80 static int	check_file(audit_fcb_t *, int);
81 static int	gather_dir(char *);
82 static audit_pcb_t *get_next_pcb(char *);
83 static obj_ent_t *obj_lkup(char *);
84 static int	proc_class(char *);
85 static int	proc_date(char *, int);
86 static int	proc_file(char *, int);
87 static int	process_fileopt(int, char *argv[], int);
88 static int	proc_group(char *, gid_t *);
89 static int	proc_id(char *, int);
90 static int	proc_object(char *);
91 static void	proc_pcb(audit_pcb_t *, char *, int);
92 static int	proc_slabel(char *);
93 static int	proc_subject(char *);
94 static int	proc_sid(char *);
95 static int	proc_type(char *);
96 static int	proc_user(char *, uid_t *);
97 static int	proc_zonename(char *);
98 
99 /*
100  * .func	process_options - process command line options.
101  * .desc	Process the user's command line options. These are of two types:
102  *	single letter flags that are denoted by '-', and filenames. Some
103  *	of the flags have arguments. Getopt() is used to get the flags.
104  *	When this is done it calls process_fileopt() to handle any filenames
105  *	that were there.
106  * .call	ret = process_options(argc, argv).
107  * .arg	argc	- the original value.
108  * .arg	argv	- the original value.
109  * .ret	0	- no errors detected.
110  * .ret	-1	- command line error detected (message already printed).
111  */
112 int
113 process_options(int argc, char **argv)
114 {
115 	int	opt;
116 	int	error = FALSE;
117 	int	error_combo = FALSE;
118 	extern int	optind;		/* in getopt() */
119 	extern char	*optarg;	/* in getopt() - holds arg to flag */
120 
121 	static char	*options = "ACD:M:NQR:S:VO:"
122 	    "a:b:c:d:e:g:j:l:m:o:r:s:t:u:z:";
123 
124 	error_str = gettext("general error");
125 
126 	zonename = NULL;
127 	/*
128 	 * Big switch to process the flags.
129 	 * Start_over: is for handling the '-' for standard input. Getopt()
130 	 * doesn't recognize it.
131 	 */
132 start_over:
133 	while ((opt = getopt(argc, argv, options)) != EOF) {
134 		switch (opt) {
135 		case 'A':		/* all records from the files */
136 			f_all = TRUE;
137 			break;
138 		case 'C':		/* process only completed files */
139 			f_complete = TRUE;
140 			break;
141 		case 'D':		/* delete the files when done */
142 			/* force 'A' 'C' 'O' to be active */
143 			f_all = f_complete = TRUE;
144 			f_outfile = optarg;
145 			f_delete = TRUE;
146 			break;
147 		case 'M':		/* only files from a certain machine */
148 			f_machine = optarg;
149 			break;
150 		case 'N':		/* new object selection mode */
151 			new_mode = TRUE;
152 			break;
153 		case 'Q':		/* no file error reporting */
154 			f_quiet = TRUE;
155 			break;
156 		case 'R':		/* from specified root */
157 			f_root = optarg;
158 			break;
159 		case 'S':		/* from specified server */
160 			f_server = optarg;
161 			break;
162 		case 'V':		/* list all files as they are opened */
163 			f_verbose = TRUE;
164 			break;
165 		case 'O':		/* write to outfile */
166 			f_outfile = optarg;
167 			break;
168 		case 'a':		/* after 'date' */
169 		case 'b':		/* before 'date' */
170 		case 'd':		/* from 'day' */
171 			if (proc_date(optarg, opt))
172 				error = TRUE;
173 			break;
174 		case 'j':		/* subject */
175 			if (proc_subject(optarg))
176 				error = TRUE;
177 			break;
178 		case 'm':		/* message 'type' */
179 			if (proc_type(optarg))
180 				error = TRUE;
181 			break;
182 		case 'o':		/* object type */
183 			if (proc_object(optarg))
184 				error = TRUE;
185 			break;
186 		case 'c':		/* message class */
187 			if (proc_class(optarg))
188 				error = TRUE;
189 			break;
190 		case 'u':		/* form audit user */
191 		case 'e':		/* form effective user */
192 		case 'r':		/* form real user */
193 		case 'f':		/* form effective group */
194 		case 'g':		/* form real group */
195 			if (proc_id(optarg, opt))
196 				error = TRUE;
197 			break;
198 		case 'l':		/* label range -- reserved for TX */
199 			if (!is_system_labeled()) {
200 				(void) fprintf(stderr,
201 				    gettext("%s option 'l' requires "
202 				    "Trusted Extensions.\n"), ar);
203 				return (-1);
204 			}
205 			if (proc_slabel(optarg))
206 				error = TRUE;
207 			break;
208 		case 's':		/* session ID */
209 			if (proc_sid(optarg))
210 				error = TRUE;
211 			break;
212 		case 'z':		/* zone name */
213 			if (proc_zonename(optarg))
214 				error = TRUE;
215 			break;
216 		case 't':		/* termial ID reserved for later */
217 		default:
218 			return (-1);
219 		}
220 		if (error) {
221 			(void) fprintf(stderr,
222 				gettext("%s command line error - %s.\n"),
223 				ar, error_str);
224 			return (-1);
225 		}
226 	}
227 	/* catch '-' option for stdin processing - getopt() won't see it */
228 	if (optind < argc) {
229 		if (argv[optind][0] == '-' && argv[optind][1] == '\0') {
230 			optind++;
231 			f_stdin = TRUE;
232 			goto start_over;
233 		}
234 	}
235 	/*
236 	 * Give a default value for 'b' option if not specified.
237 	 */
238 	if (m_before == 0)
239 		m_before = MAXLONG;	/* forever */
240 	/*
241 	 * Validate combinations of options.
242 	 * The following are done:
243 	 *	1. Can't have 'M' or 'S' or 'R' with filenames.
244 	 *	2. Can't have an after ('a') time after a before ('b') time.
245 	 *	3. Delete ('D') must have 'C' and 'A' and 'O' with it.
246 	 *	4. Input from stdin ('-') can't have filenames too.
247 	 */
248 	if ((f_machine || f_server || f_root) && (argc != optind)) {
249 		error_str = gettext(
250 		    "no filenames allowed with 'M' or 'S' or 'R' options");
251 		error_combo = TRUE;
252 	}
253 	if (m_after >= m_before) {
254 		error_str =
255 			gettext("'a' parameter must be before 'b' parameter");
256 		error_combo = TRUE;
257 	}
258 	if (f_delete &&
259 	    (!f_complete || !f_all || !f_outfile)) {
260 		error_str = gettext(
261 		    "'C', 'A', and 'O' must be specified with 'D'");
262 		error_combo = TRUE;
263 	}
264 	if (f_stdin && (argc != optind)) {
265 		error_str = gettext("no filenames allowed with '-' option");
266 		error_combo = TRUE;
267 	}
268 	/*
269 	 * If error with option combos then print message and exit.
270 	 * If there was an error with just an option then exit.
271 	 */
272 	if (error_combo) {
273 		(void) fprintf(stderr,
274 		    gettext("%s command line error - %s.\n"), ar, error_str);
275 		return (-1);
276 	}
277 	if (f_root == NULL)
278 		f_root = "/etc/security/audit";
279 	/*
280 	 * Now handle any filenames included in the command line.
281 	 */
282 	return (process_fileopt(argc, argv, optind));
283 }
284 
285 int
286 proc_subject(char *optarg)
287 {
288 	if (flags & M_SUBJECT) {
289 		error_str = gettext("'j' option specified multiple times");
290 		return (-1);
291 	}
292 	flags |= M_SUBJECT;
293 	subj_id = atol(optarg);
294 	return (0);
295 }
296 
297 int
298 proc_sid(char *optarg)
299 {
300 	if (flags & M_SID) {
301 		error_str = gettext("'s' option specified multiple times");
302 		return (-1);
303 	}
304 	flags |= M_SID;
305 	m_sid = atol(optarg);
306 	return (0);
307 }
308 
309 int
310 proc_object(char *optarg)
311 {
312 	char	*obj_str;
313 	char	*obj_val;
314 	char	*obj_arg;
315 	int	err;
316 
317 	obj_ent_t *oep;
318 	struct hostent *he;
319 
320 	if (flags & M_OBJECT) {
321 		error_str = gettext("'o' option specified multiple times");
322 		return (-1);
323 	}
324 	flags |= M_OBJECT;
325 	if ((obj_arg = strdup(optarg)) == (char *)0)
326 		return (-1);
327 	if ((obj_str = strtok(optarg, "=")) == (char *)0 ||
328 	    (oep = obj_lkup(obj_str)) == (obj_ent_t *)0 ||
329 	    (obj_val = strtok((char *)0, "=")) == (char *)0) {
330 		(void) sprintf(errbuf, gettext("invalid object arg (%s)"),
331 		    obj_arg);
332 		error_str = errbuf;
333 		return (-1);
334 	}
335 
336 	obj_flag = oep->obj_flag;
337 
338 	switch (obj_flag) {
339 	case OBJ_PATH:
340 		if ((error_str = re_comp2(obj_val)) != (char *)NULL) {
341 			return (-1);
342 		}
343 		return (0);
344 		/* NOTREACHED */
345 	case OBJ_SOCK:
346 		if (!a_isnum(obj_val, TRUE)) {
347 			obj_id = atol(obj_val);
348 			socket_flag = SOCKFLG_PORT;
349 			return (0);
350 		}
351 		if (*obj_val == '0') {
352 			(void) sscanf(obj_val, "%x", (uint_t *)&obj_id);
353 			socket_flag = SOCKFLG_PORT;
354 			return (0);
355 		}
356 
357 		he = getipnodebyname((const void *)obj_val, AF_INET6, 0, &err);
358 		if (he == 0) {
359 			he = getipnodebyname((const void *)obj_val, AF_INET,
360 			    0, &err);
361 			if (he == 0) {
362 				(void) sprintf(errbuf,
363 				    gettext("invalid machine name (%s)"),
364 				    obj_val);
365 				error_str = errbuf;
366 				return (-1);
367 			}
368 		}
369 
370 		if (he->h_addrtype == AF_INET6) {
371 			/* LINTED */
372 			if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)
373 				he->h_addr_list[0])) {
374 				/* address is IPv4 (32 bits) */
375 				(void) memcpy(&obj_id, he->h_addr_list[0], 4);
376 				ip_type = AU_IPv4;
377 			} else {
378 				(void) memcpy(ip_ipv6, he->h_addr_list[0], 16);
379 				ip_type = AU_IPv6;
380 			}
381 		} else {
382 			/* address is IPv4 (32 bits) */
383 			(void) memcpy(&obj_id, he->h_addr_list[0], 4);
384 			ip_type = AU_IPv4;
385 		}
386 
387 		freehostent(he);
388 		socket_flag = SOCKFLG_MACHINE;
389 		return (0);
390 		break;
391 	case OBJ_MSG:
392 	case OBJ_SEM:
393 	case OBJ_SHM:
394 	case OBJ_PROC:
395 		obj_id = atol(obj_val);
396 		return (0);
397 		/* NOTREACHED */
398 	case OBJ_FGROUP:
399 	case OBJ_MSGGROUP:
400 	case OBJ_SEMGROUP:
401 	case OBJ_SHMGROUP:
402 	case OBJ_PGROUP:
403 		return (proc_group(obj_val, &obj_group));
404 		/* NOTREACHED */
405 	case OBJ_FOWNER:
406 	case OBJ_MSGOWNER:
407 	case OBJ_SEMOWNER:
408 	case OBJ_SHMOWNER:
409 	case OBJ_POWNER:
410 		return (proc_user(obj_val, &obj_owner));
411 		/* NOTREACHED */
412 	case OBJ_LP: /* lp objects have not yet been defined */
413 	default: /* impossible */
414 		(void) sprintf(errbuf, gettext("invalid object type (%s)"),
415 		    obj_str);
416 		error_str = errbuf;
417 		return (-1);
418 		/* NOTREACHED */
419 	} /* switch */
420 	/*NOTREACHED*/
421 }
422 
423 
424 obj_ent_t *
425 obj_lkup(char *obj_str)
426 {
427 	int	i;
428 
429 	for (i = 0; i < sizeof (obj_tbl) / sizeof (obj_ent_t); i++)
430 		if (strcmp(obj_str, obj_tbl[i].obj_str) == 0)
431 			return (&obj_tbl[i]);
432 
433 	/* not in table */
434 	return ((obj_ent_t *)0);
435 }
436 
437 
438 /*
439  * .func	proc_type - process record type.
440  * .desc	Process a record type. It is either as a number or a mnemonic.
441  * .call	ret = proc_type(optstr).
442  * .arg	optstr	- ptr to name or number.
443  * .ret	0	- no errors detected.
444  * .ret	-1	- error detected (error_str contains description).
445  */
446 int
447 proc_type(char *optstr)
448 {
449 	struct au_event_ent *aep;
450 
451 	/*
452 	 * Either a number or a name.
453 	 */
454 
455 	if (flags & M_TYPE) {
456 		error_str = gettext("'m' option specified multiple times");
457 		return (-1);
458 	}
459 	flags |= M_TYPE;
460 	m_type = 0;
461 	if (a_isnum(optstr, TRUE)) {
462 		if ((aep = getauevnam(optstr)) != (struct au_event_ent *)NULL)
463 			m_type = aep->ae_number;
464 	} else {
465 		if ((aep = getauevnum((au_event_t)atoi(optstr))) !=
466 		    (struct au_event_ent *)NULL)
467 			m_type = aep->ae_number;
468 	}
469 	if ((m_type == 0)) {
470 		(void) sprintf(errbuf, gettext("invalid event (%s)"), optstr);
471 		error_str = errbuf;
472 		return (-1);
473 	}
474 	return (0);
475 }
476 
477 
478 /*
479  * .func	a_isnum - is it a number?
480  * .desc	Determine if a string is a number or a name.
481  *	A number may have a leading '+' or '-', but then must be
482  *	all digits.
483  * .call	ret = a_isnum(str).
484  * .arg	str - ptr to the string.
485  * .arg	leading	- TRUE if leading '+-' allowed.
486  * .ret	0	- is a number.
487  * .ret	1	- is not a number.
488  */
489 int
490 a_isnum(char *str, int leading)
491 {
492 	char	*strs;
493 
494 	if ((leading == TRUE) && (*str == '-' || *str == '+'))
495 		strs = str + 1;
496 	else
497 		strs = str;
498 
499 	if (strlen(strs) == strspn(strs, "0123456789"))
500 		return (0);
501 	else
502 		return (1);
503 }
504 
505 
506 /*
507  * .func	proc_id	- process user/group id's/
508  * .desc	Process either a user number/name or group number/name.
509  *	For names check to see if the name is active in the system
510  *	to derive the number. If it is not active then fail. For a number
511  *	also check to see if it is active, but only print a warning if it
512  *	is not. An administrator may be looking at activity of a 'phantom'
513  *	user.
514  * .call	ret = proc_id(optstr, opt).
515  * .arg	optstr	- ptr to name or number.
516  * .arg	opt	- 'u' - audit user, 'e' - effective user, 'r' - real user,
517  *		  'g' - group, 'f' - effective group.
518  * .ret	0	- no errors detected.
519  * .ret	-1	- error detected (error_str contains description).
520  */
521 int
522 proc_id(char *optstr, int opt)
523 {
524 	switch (opt) {
525 	case 'e': 		/* effective user id */
526 		if (flags & M_USERE) {
527 			error_str = gettext(
528 			    "'e' option specified multiple times");
529 			return (-1);
530 		}
531 		flags |= M_USERE;
532 		return (proc_user(optstr, &m_usere));
533 		/* NOTREACHED */
534 	case 'f': 		/* effective group id */
535 		if (flags & M_GROUPE) {
536 			error_str = gettext(
537 			    "'f' option specified multiple times");
538 			return (-1);
539 		}
540 		flags |= M_GROUPE;
541 		return (proc_group(optstr, &m_groupe));
542 		/* NOTREACHED */
543 	case 'r': 		/* real user id */
544 		if (flags & M_USERR) {
545 			error_str = gettext(
546 			    "'r' option specified multiple times");
547 			return (-1);
548 		}
549 		flags |= M_USERR;
550 		return (proc_user(optstr, &m_userr));
551 		/* NOTREACHED */
552 	case 'u': 		/* audit user id */
553 		if (flags & M_USERA) {
554 			error_str = gettext(
555 			    "'u' option specified multiple times");
556 			return (-1);
557 		}
558 		flags |= M_USERA;
559 		return (proc_user(optstr, &m_usera));
560 		/* NOTREACHED */
561 	case 'g': 		/* real group id */
562 		if (flags & M_GROUPR) {
563 			error_str = gettext(
564 			    "'g' option specified multiple times");
565 			return (-1);
566 		}
567 		flags |= M_GROUPR;
568 		return (proc_group(optstr, &m_groupr));
569 		/* NOTREACHED */
570 	default: 		/* impossible */
571 		(void) sprintf(errbuf, gettext("'%c' unknown option"), opt);
572 		error_str = errbuf;
573 		return (-1);
574 		/* NOTREACHED */
575 	}
576 	/*NOTREACHED*/
577 }
578 
579 
580 int
581 proc_group(char *optstr, gid_t *gid)
582 {
583 	struct group *grp;
584 
585 	if ((grp = getgrnam(optstr)) == NULL) {
586 		if (!a_isnum(optstr, TRUE)) {
587 			*gid = (gid_t)atoi(optstr);
588 			return (0);
589 		}
590 		(void) sprintf(errbuf, gettext("group name invalid (%s)"),
591 		    optstr);
592 		error_str = errbuf;
593 		return (-1);
594 	}
595 	*gid = grp->gr_gid;
596 	return (0);
597 }
598 
599 
600 int
601 proc_user(char *optstr, uid_t *uid)
602 {
603 	struct passwd *usr;
604 
605 	if ((usr = getpwnam(optstr)) == NULL) {
606 		if (!a_isnum(optstr, TRUE)) {
607 			*uid = (uid_t)atoi(optstr);
608 			return (0);
609 		}
610 		(void) sprintf(errbuf, gettext("user name invalid (%s)"),
611 		    optstr);
612 		error_str = errbuf;
613 		return (-1);
614 	}
615 	*uid = usr->pw_uid;
616 	return (0);
617 }
618 
619 
620 /*
621  * .func proc_date - process date argument.
622  * .desc Handle a date/time argument. See if the user has erred in combining
623  *	the types of date arguments. Then parse the string and check for
624  *	validity of each part.
625  * .call	ret = proc_date(optstr, opt).
626  * .arg	optstr	- ptr to date/time string.
627  * .arg	opt	- 'd' for day, 'a' for after, or 'b' for before.
628  * .ret	0	- no errors detected.
629  * .ret	-1	- errors detected (error_str knows what it is).
630  */
631 int
632 proc_date(char *optstr, int opt)
633 {
634 	static int	m_day = FALSE;
635 
636 	if (opt == 'd') {
637 		if (m_day == TRUE) {
638 			error_str = gettext(
639 			    "'d' option may not be used with 'a' or 'b'");
640 			return (-1);
641 		}
642 		m_day = TRUE;
643 	}
644 	if ((opt == 'd') && (m_before || m_after)) {
645 		error_str = gettext(
646 		    "'d' option may not be used with 'a' or 'b'");
647 		return (-1);
648 	}
649 	if ((opt == 'a' || opt == 'b') && m_day) {
650 		error_str = gettext(
651 		    "'a' or 'b' option may not be used with 'd'");
652 		return (-1);
653 	}
654 	if ((opt == 'a') && (m_after != 0)) {
655 		error_str = gettext("'a' option specified multiple times");
656 		return (-1);
657 	}
658 	if ((opt == 'b') && (m_before != 0)) {
659 		error_str = gettext("'b' option specified multiple times");
660 		return (-1);
661 	}
662 	if (parse_time(optstr, opt))
663 		return (-1);
664 	return (0);
665 }
666 
667 
668 /*
669  * .func	proc_class - process message class argument.
670  * .desc	Process class type and see if it is for real.
671  * .call	ret = proc_class(optstr).
672  * .arg	optstr	- ptr to class.
673  * .ret	0	- class has class.
674  * .ret	-1	- class in no good.
675  */
676 int
677 proc_class(char *optstr)
678 {
679 	if (flags & M_CLASS) {
680 		error_str = gettext("'c' option specified multiple times");
681 		return (-1);
682 	}
683 	flags |= M_CLASS;
684 
685 	if (getauditflagsbin(optstr, &mask) != 0) {
686 		(void) sprintf(errbuf, gettext("unknown class (%s)"), optstr);
687 		error_str = errbuf;
688 		return (-1);
689 	}
690 
691 	if (mask.am_success != mask.am_failure) {
692 		flags |= M_SORF;
693 	}
694 
695 	return (0);
696 }
697 
698 
699 /*
700  * .func process_fileopt - process command line file options.
701  * .desc Process the command line file options and gather the specified files
702  *	together in file groups based upon file name suffix. The user can
703  *	specify files explicitly on the command line or via a directory.
704  *	This is called after the command line flags are processed (as
705  *	denoted by '-').
706  * .call	ret = process_fileopt(argc, argv, optindex).
707  * .arg	argc	- current value of argc.
708  * .arg	argv	- current value of argv.
709  * .arg	optindex- current index into argv (as setup by getopt()).
710  * .ret	0	- no errors detected.
711  * .ret	-1	- error detected (message already printed).
712  */
713 int
714 process_fileopt(int argc, char **argv, int optindex)
715 {
716 	int	f_mode = FM_ALLDIR;
717 	char	f_dr[MAXNAMLEN+1];
718 	char	*f_dir = f_dr;
719 	char	*fname;
720 	static char	*std = "standard input";
721 	audit_fcb_t *fcb;
722 	DIR * dirp;
723 	struct dirent *dp;
724 	audit_pcb_t *pcb;
725 
726 	/*
727 	 * Take input from stdin, not any files.
728 	 * Use a single fcb to do this.
729 	 */
730 	if (f_stdin) {
731 		fcb = (audit_fcb_t *)a_calloc(1, sizeof (*fcb) + strlen(std));
732 		(void) strcpy(fcb->fcb_file, std);
733 		fcb->fcb_suffix = fcb->fcb_name = fcb->fcb_file;
734 		fcb->fcb_next = NULL;
735 		fcb->fcb_start = 0;
736 		fcb->fcb_end = MAXLONG;		/* forever */
737 		if ((pcb = get_next_pcb((char *)NULL)) == (audit_pcb_t *)NULL)
738 			return (-1);
739 		pcb->pcb_suffix = fcb->fcb_file;
740 		pcb->pcb_dfirst = pcb->pcb_first = fcb;	/* one-item list */
741 		pcb->pcb_dlast = pcb->pcb_last = fcb;
742 		pcb->pcb_cur = fcb;
743 	}
744 	/*
745 	 * No files specified on the command line.
746 	 * Process a directory of files or subdirectories.
747 	 */
748 	else if (argc == optindex) {
749 		/*
750 		 * A specific server directory was requested.
751 		 */
752 		if (f_server) {
753 			if (strchr(f_server, '/')) {	/* given full path */
754 				f_dir = f_server;
755 				f_mode = FM_ALLFILE;	/* all files here */
756 			} else {		/* directory off audit root */
757 				f_dir[0] = '\0';
758 				(void) strcat(f_dir, f_root);
759 				(void) strcat(f_dir, "/");
760 				(void) strcat(f_dir, f_server);
761 				f_mode = FM_ALLFILE;
762 			}
763 		}
764 		/*
765 		 * Gather all of the files in the directory 'f_dir'.
766 		 */
767 		if (f_mode == FM_ALLFILE) {
768 			if (gather_dir(f_dir)) { /* get those files together */
769 				return (-1);
770 			}
771 		} else {
772 			/*
773 			 * Gather all of the files in all of the
774 			 * directories in 'f_root'.
775 			 */
776 			if ((dirp = opendir(f_root)) == NULL) {
777 				(void) sprintf(errbuf, gettext(
778 				    "%s can't open directory %s"), ar, f_root);
779 				perror(errbuf);
780 				return (-1);
781 			}
782 			/* read the directory and process all of the subs */
783 			for (dp = readdir(dirp);
784 			    dp != NULL; dp = readdir(dirp)) {
785 				if (dp->d_name[0] == '.')
786 					continue;
787 				f_dir[0] = '\0';
788 				(void) strcat(f_dir, f_root);
789 				(void) strcat(f_dir, "/");
790 				(void) strcat(f_dir, dp->d_name);
791 				if (gather_dir(f_dir))	/* process a sub */
792 					return (-1);
793 			}
794 			(void) closedir(dirp);
795 		}
796 	} else {
797 		/*
798 		 * User specified filenames on the comm and line.
799 		 */
800 		f_cmdline = TRUE;
801 		for (; optindex < argc; optindex++) {
802 			fname = argv[optindex];		/* get a filename */
803 			if (proc_file(fname, FALSE))
804 				return (-1);
805 		}
806 	}
807 	return (0);
808 }
809 
810 
811 /*
812  * .func	gather_dir - gather a directory's files together.
813  * .desc	Process all of the files in a specific directory. The files may
814  *	be checked for adherence to the file name form at.
815  *	If the directory can't be opened that is ok - just print
816  *	a message and continue.
817  * .call	ret = gather_dir(dir).
818  * .arg	dir	- ptr to full pathname of directory.
819  * .ret	0	- no errors detected.
820  * .ret	-1	- error detected (message already printed).
821  */
822 int
823 gather_dir(char *dir)
824 {
825 	char	dname[MAXNAMLEN+1];
826 	char	fname[MAXNAMLEN+1];
827 	DIR * dirp;
828 	struct dirent *dp;
829 
830 	(void) snprintf(dname, sizeof (dname), "%s/files", dir);
831 
832 	if ((dirp = opendir(dname)) == NULL) {
833 		if (errno != ENOTDIR) {
834 			(void) sprintf(errbuf,
835 			    gettext("%s can't open directory - %s"), ar, dname);
836 			perror(errbuf);
837 		}
838 		return (0);
839 	}
840 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
841 		if (dp->d_name[0] == '.')	/* can't see hidden files */
842 			continue;
843 		fname[0] = '\0';
844 		(void) strcat(fname, dname);	/* create pathname of file */
845 		(void) strcat(fname, "/");
846 		(void) strcat(fname, dp->d_name);
847 		if (proc_file(fname, TRUE))
848 			return (-1);
849 	}
850 	(void) closedir(dirp);
851 	return (0);
852 }
853 
854 
855 /*
856  * .func	proc_file - process a single candidate file.
857  * .desc	Check out a file to see if it should be used in the merge.
858  *	This includes checking the name (mode is TRUE) against the
859  *	file format, checking access rights to the file, and thence
860  *	getting and fcb and installing the fcb into the correct pcb.
861  *	If the file fails then the fcb is not installed into a pcb
862  *	and the file dissapears from view.
863  * .call	proc_file(fname, mode).
864  * .arg	fname	- ptr to full pathna me of file.
865  * .arg	mode	- TRUE if checking adherence to file name format.
866  * .ret	0	- no fatal errors detected.
867  * .ret	-1	- fatal error detected - quit altogether
868  *		  (message already printed).
869  */
870 int
871 proc_file(char *fname, int mode)
872 {
873 	int reject = FALSE;
874 	size_t len;
875 	struct stat stat_buf;
876 	audit_fcb_t *fcb, *fcbp, *fcbprev;
877 	audit_pcb_t *pcb;
878 
879 	/*
880 	 * See if it is a weird file like a directory or
881 	 * character special (around here?).
882 	 */
883 	if (stat(fname, &stat_buf)) {
884 		return (0);
885 	}
886 	if (!S_ISREG(stat_buf.st_mode))
887 		return (0);
888 	/*
889 	 * Allocate a new fcb to hold fcb and full filename.
890 	 */
891 	len = sizeof (audit_fcb_t) + strlen(fname);
892 	fcb = (audit_fcb_t *)a_calloc(1, len);
893 	(void) strcpy(fcb->fcb_file, fname);
894 	if (check_file(fcb, mode)) { /* check file name */
895 		if (!f_quiet) {
896 			(void) fprintf(stderr, "%s %s:\n  %s.\n", ar,
897 			    error_str, fname);
898 		}
899 		reject = TRUE;
900 	} else {
901 		/*
902 		 * Check against file criteria.
903 		 * Check finish-time here, and start-time later on
904 		 * while processing.
905 		 * This is because the start time on a file can be after
906 		 * the first record(s).
907 		 */
908 		if (f_complete && (fcb->fcb_flags & FF_NOTTERM) && !f_cmdline)
909 			reject = TRUE;
910 		if (!f_all && (fcb->fcb_end < m_after))
911 			reject = TRUE;
912 		if (f_machine) {
913 			if (strlen(fcb->fcb_suffix) != strlen(f_machine) ||
914 			    (strcmp(fcb->fcb_suffix, f_machine) != 0)) {
915 				reject = TRUE;
916 			}
917 		}
918 	}
919 	if (reject == FALSE) {
920 		filenum++;	/* count of total files to be processed */
921 		fcb->fcb_next = NULL;
922 		if ((pcb = get_next_pcb(fcb->fcb_suffix)) == NULL) {
923 			return (-1);
924 		}
925 		/* Place FCB into the PCB in order - oldest first.  */
926 		fcbp = pcb->pcb_first;
927 		fcbprev = NULL;
928 		while (fcbp != NULL) {
929 			if (fcb->fcb_start < fcbp->fcb_start) {
930 				if (fcbprev)
931 					fcbprev->fcb_next = fcb;
932 				else
933 					pcb->pcb_dfirst = pcb->pcb_first = fcb;
934 				fcb->fcb_next = fcbp;
935 				break;
936 			}
937 			fcbprev = fcbp;
938 			fcbp = fcbp->fcb_next;
939 		}
940 		/* younger than all || empty list */
941 		if (!fcb->fcb_next) {
942 			if (pcb->pcb_first == NULL)
943 				pcb->pcb_dfirst = pcb->pcb_first = fcb;
944 			pcb->pcb_dlast = pcb->pcb_last = fcb;
945 			if (fcbprev)
946 				fcbprev->fcb_next = fcb;
947 		}
948 	} else {
949 		free((char *)fcb);	/* rejected */
950 	}
951 	return (0);
952 }
953 
954 
955 /*
956  * .func	check_file - check filename and setup fcb.
957  * .desc	Check adherence to the file format (do_check is TRUE) and setup
958  *	the fcb with useful information.
959  *	filename format: yyyymmddhhmmss.yyyymmddhhmmss.suffix
960  *			 yyyymmddhhmmss.not_terminated.suffix
961  *	If do_check is FALSE then still see if the filename does confirm
962  *	to the format. If it does then extract useful information from
963  *	it (start time and end time).  But if it doesn't then don't print
964  *	any error messages.
965  * .call	ret = check_file(fcb, do_check).
966  * .arg	fcb	- ptr to fcb that holds the file.
967  * .arg	do_check - if TRUE do check adherence to file format.
968  * .ret	0	- no errors detected.
969  * .ret	-1	- file failed somehow (error_str tells why).
970  */
971 int
972 check_file(audit_fcb_t *fcb, int do_check)
973 {
974 	int	ret;
975 	char	*namep, *slp;
976 	char	errb[256];		/* build error message */
977 	struct tm tme;
978 
979 	errb[0] = '\0';
980 	/* get just the filename */
981 	for (slp = namep = fcb->fcb_file; *namep; namep++) {
982 		if (*namep == '/')
983 			slp = namep + 1; /* slp -> the filename itself */
984 	}
985 	if (do_check == FALSE) {
986 		fcb->fcb_end = MAXLONG;		/* forever */
987 		fcb->fcb_suffix = NULL;
988 		fcb->fcb_name = slp;
989 		ret = 0;
990 	} else {
991 		ret = -1;
992 	}
993 	if ((int)strlen(slp) < 31) {
994 		(void) sprintf(errbuf, gettext("filename too short (%d)"),
995 		    strlen(slp));
996 		error_str = errbuf;
997 		return (ret);
998 	}
999 	/*
1000 	 * Get working copy of filename.
1001 	 */
1002 	namep = (char *)a_calloc(1, strlen(slp) + 1);
1003 	(void) strcpy(namep, slp);
1004 	if (namep[14] != '.' || namep[29] != '.') {
1005 		(void) sprintf(errbuf,
1006 		    gettext("invalid filename format (%c or %c)"), namep[14],
1007 		    namep[29]);
1008 		error_str = errbuf;
1009 		free(namep);
1010 		return (ret);
1011 	}
1012 	namep[14] = '\0';			/* mark off start time */
1013 	namep[29] = '\0';			/* mark off finish time */
1014 	if (derive_date(namep, &tme)) {
1015 		(void) strcat(errb, gettext("starting time-stamp invalid - "));
1016 		(void) strcat(errb, error_str);
1017 		(void) strcpy(errbuf, errb);
1018 		error_str = errbuf;
1019 		free(namep);
1020 		return (ret);
1021 	}
1022 	/*
1023 	 * Keep start time from filename. Use it to order files in
1024 	 * the file list. Later we will update this when we read
1025 	 * the first record from the file.
1026 	 */
1027 	fcb->fcb_start = tm_to_secs(&tme);
1028 
1029 	if (strcmp(&namep[15], "not_terminated") == 0) {
1030 		fcb->fcb_end = MAXLONG;		/* forever */
1031 		/*
1032 		 * Only treat a 'not_terminated' file as such if
1033 		 * it is not on the command line.
1034 		 */
1035 		if (do_check == TRUE)
1036 			fcb->fcb_flags |= FF_NOTTERM;
1037 	} else if (derive_date(&namep[15], &tme)) {
1038 		(void) strcat(errb, gettext("ending time-stamp invalid - "));
1039 		(void) strcat(errb, error_str);
1040 		(void) strcpy(errbuf, errb);
1041 		error_str = errbuf;
1042 		free(namep);
1043 		return (ret);
1044 	} else {
1045 		fcb->fcb_end = tm_to_secs(&tme);
1046 	}
1047 	fcb->fcb_name = slp;
1048 	fcb->fcb_suffix = &slp[30];
1049 	free(namep);
1050 	return (0);
1051 }
1052 
1053 
1054 /*
1055  * .func get_next_pcb - get a pcb to use.
1056  * .desc	The pcb's in the array audit_pcbs are used to hold single file
1057  *	groups in the form of a linked list. Each pcb holds files that
1058  *	are tied together by a common suffix in the file name. Here we
1059  *	get either 1. the existing pcb holding a specified sufix or
1060  *	2. a new pcb if we can't find an existing one.
1061  * .call	pcb = get_next_pcb(suffix).
1062  * .arg	suffix	- ptr to suffix we are seeking.
1063  * .ret	pcb	- ptr to pcb that hold s the sought suffix.
1064  * .ret	NULL- serious failure in memory allocation. Quit processing.
1065  */
1066 audit_pcb_t *
1067 get_next_pcb(char *suffix)
1068 {
1069 	int	i = 0;
1070 	int	zerosize;
1071 	unsigned int	size;
1072 	audit_pcb_t *pcb;
1073 
1074 	/* Search through (maybe) entire array. */
1075 	while (i < pcbsize) {
1076 		pcb = &audit_pcbs[i++];
1077 		if (pcb->pcb_first == NULL) {
1078 			proc_pcb(pcb, suffix, i);
1079 			return (pcb);	/* came to an unused one */
1080 		}
1081 		if (suffix) {
1082 			if (strcmp(pcb->pcb_suffix, suffix) == 0)
1083 				return (pcb);	/* matched one with suffix */
1084 		}
1085 	}
1086 	/*
1087 	 * Uh-oh, the entire array is used and we haven't gotten one yet.
1088 	 * Allocate a bigger array.
1089 	 */
1090 	pcbsize += PCB_INC;
1091 	size = pcbsize * sizeof (audit_pcb_t);
1092 	zerosize = size - ((pcbsize - PCB_INC) * sizeof (audit_pcb_t));
1093 	if ((audit_pcbs = (audit_pcb_t *)realloc((char *)audit_pcbs, size)) ==
1094 	    NULL) {
1095 		(void) sprintf(errbuf,
1096 		    gettext("%s memory reallocation failed (%d bytes)"), ar,
1097 		    size);
1098 		perror(errbuf);
1099 		audit_stats();		/* give user statistics on usage */
1100 		return (NULL);		/* really bad thing to have happen */
1101 	}
1102 	/*
1103 	 * Don't know if realloc clears the new memory like calloc would.
1104 	 */
1105 	(void) memset((void *) & audit_pcbs[pcbsize-PCB_INC], 0,
1106 	    (size_t)zerosize);
1107 	pcb = &audit_pcbs[pcbsize-PCB_INC];	/* allocate the first new one */
1108 	proc_pcb(pcb, suffix, pcbsize - PCB_INC);
1109 	return (pcb);
1110 }
1111 
1112 
1113 /*
1114  * .func proc_pcb - process pcb.
1115  * .desc	Common pcb processing for above routine.
1116  * .call	proc_pcb(pcb, suffix, i).
1117  * .arg	pcb	- ptr to pcb.
1118  * .arg	suffix	- prt to suffix tha t ties this group together.
1119  * .arg	i	- index into audit_pcbs[ ].
1120  * .ret	void.
1121  */
1122 void
1123 proc_pcb(audit_pcb_t *pcb, char *suffix, int i)
1124 {
1125 	if (suffix)
1126 		pcb->pcb_suffix = suffix;
1127 	pcbnum++;	/* one more pcb in use */
1128 	pcb->pcb_size = AUDITBUFSIZE;
1129 	pcb->pcb_rec = (char *)a_calloc(1, AUDITBUFSIZE);
1130 	pcb->pcb_time = -1;
1131 	pcb->pcb_flags |= PF_FILE;	/* note this one controls files */
1132 	pcb->pcb_procno = i;	/* save index into audit_pcbs [] for id */
1133 }
1134 
1135 
1136 /*
1137  * .func	proc_slabel - process sensitivity label range argument.
1138  * .desc	Parse sensitivity label range sl:sl
1139  * .call	ret = proc_slabel(optstr).
1140  * .arg	opstr	- ptr to label range string
1141  * .ret 0	- no errors detected.
1142  * .ret -1	- errors detected or not Trusted Solaris (error_str set).
1143  */
1144 
1145 int
1146 proc_slabel(char *optstr)
1147 {
1148 	char	*p;
1149 	int	error;
1150 
1151 	if (flags & M_LABEL) {
1152 		error_str = gettext("'l' option specified multiple times");
1153 		return (-1);
1154 	}
1155 	flags |= M_LABEL;
1156 
1157 	if ((m_label = malloc(sizeof (m_range_t))) == NULL) {
1158 		return (-1);
1159 	}
1160 	m_label->lower_bound = NULL;
1161 	m_label->upper_bound = NULL;
1162 
1163 	p = strchr(optstr, ';');
1164 	if (p == NULL) {
1165 		/* exact label match, lower and upper range bounds the same */
1166 		if (str_to_label(optstr, &m_label->lower_bound, MAC_LABEL,
1167 		    L_NO_CORRECTION, &error) == -1) {
1168 			(void) sprintf(errbuf,
1169 			    gettext("invalid sensitivity label (%s) err %d"),
1170 			    optstr, error);
1171 			error_str = errbuf;
1172 			goto errout;
1173 		}
1174 		m_label->upper_bound = m_label->lower_bound;
1175 		return (0);
1176 	}
1177 	if (p == optstr) {
1178 		/* lower bound is not specified .. default is admin_low */
1179 		if (str_to_label(ADMIN_LOW, &m_label->lower_bound, MAC_LABEL,
1180 		    L_NO_CORRECTION, &error) == -1) {
1181 			free(m_label);
1182 			return (-1);
1183 		}
1184 
1185 		p++;
1186 		if (*p == '\0') {
1187 			/* upper bound not specified .. default is admin_high */
1188 			if (str_to_label(ADMIN_HIGH, &m_label->upper_bound,
1189 			    MAC_LABEL, L_NO_CORRECTION, &error) == -1) {
1190 				m_label_free(m_label->lower_bound);
1191 				free(m_label);
1192 				return (-1);
1193 			}
1194 		} else {
1195 			if (str_to_label(p, &m_label->upper_bound, MAC_LABEL,
1196 			    L_NO_CORRECTION, &error) == -1) {
1197 				(void) sprintf(errbuf, gettext(
1198 				    "invalid sensitivity label (%s) err %d"),
1199 				    p, error);
1200 				error_str = errbuf;
1201 				goto errout;
1202 			}
1203 		}
1204 		return (0);
1205 	}
1206 	*p++ = '\0';
1207 	if (str_to_label(optstr, &m_label->lower_bound, MAC_LABEL,
1208 	    L_NO_CORRECTION, &error) == -1) {
1209 		(void) sprintf(errbuf,
1210 		    gettext("invalid sensitivity label (%s) err %d"), optstr,
1211 		    error);
1212 		error_str = errbuf;
1213 		goto errout;
1214 	}
1215 	if (*p == '\0') {
1216 		/* upper bound is not specified .. default is admin_high */
1217 		if (str_to_label(ADMIN_HIGH, &m_label->upper_bound,
1218 		    MAC_LABEL, L_NO_CORRECTION, &error) == -1) {
1219 			m_label_free(m_label->lower_bound);
1220 			free(m_label);
1221 			return (-1);
1222 		}
1223 	} else {
1224 		if (str_to_label(p, &m_label->upper_bound, MAC_LABEL,
1225 		    L_NO_CORRECTION, &error) == -1) {
1226 			(void) sprintf(errbuf,
1227 			    gettext("invalid sensitivity label (%s) err %d"),
1228 			    p, error);
1229 			error_str = errbuf;
1230 			goto errout;
1231 		}
1232 	}
1233 	/* make sure that upper bound dominates the lower bound */
1234 	if (!bldominates(m_label->upper_bound, m_label->lower_bound)) {
1235 		*--p = ';';
1236 		(void) sprintf(errbuf,
1237 		    gettext("invalid sensitivity label range (%s)"), optstr);
1238 		error_str = errbuf;
1239 		goto errout;
1240 	}
1241 	return (0);
1242 
1243 errout:
1244 	m_label_free(m_label->upper_bound);
1245 	m_label_free(m_label->lower_bound);
1246 	free(m_label);
1247 
1248 	return (-1);
1249 }
1250 
1251 /*
1252  * proc_zonename - pick up zone name.
1253  *
1254  * all non-empty and not-too-long strings are valid since any name
1255  * may be valid.
1256  *
1257  * ret 0:	non-empty string
1258  * ret -1:	empty string or string is too long.
1259  */
1260 static int
1261 proc_zonename(char *optstr)
1262 {
1263 	size_t	length = strlen(optstr);
1264 	if ((length < 1) || (length > ZONENAME_MAX)) {
1265 		(void) sprintf(errbuf,
1266 		    gettext("invalid zone name: %s"), optstr);
1267 		error_str = errbuf;
1268 		return (-1);
1269 	}
1270 	zonename = strdup(optstr);
1271 	flags |= M_ZONENAME;
1272 	return (0);
1273 }
1274