xref: /onnv-gate/usr/src/lib/libbsm/common/audit_halt.c (revision 12160:f00ea86e54a6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
511706SJan.Friedel@Sun.COM  * Common Development and Distribution License (the "License").
611706SJan.Friedel@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12160SMarek.Pospisil@Sun.COM  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <sys/fcntl.h>
290Sstevel@tonic-gate #include <bsm/audit.h>
300Sstevel@tonic-gate #include <bsm/audit_record.h>
310Sstevel@tonic-gate #include <bsm/audit_uevents.h>
320Sstevel@tonic-gate #include <bsm/libbsm.h>
330Sstevel@tonic-gate #include <bsm/audit_private.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <syslog.h>
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <libgen.h>
390Sstevel@tonic-gate #include <generic.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef C2_DEBUG
4211706SJan.Friedel@Sun.COM #define	dprintf(x) { (void) printf x; }
430Sstevel@tonic-gate #else
440Sstevel@tonic-gate #define	dprintf(x)
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static int audit_halt_generic(int);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* ARGSUSED */
500Sstevel@tonic-gate int
audit_halt_setup(int argc,char ** argv)510Sstevel@tonic-gate audit_halt_setup(int argc, char **argv)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	char *cmdname;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	dprintf(("audit_halt_setup()\n"));
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (cannot_audit(0)) {
580Sstevel@tonic-gate 		return (0);
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	cmdname = basename(*argv);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	aug_init();
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (strcmp(cmdname, "halt") == 0)
660Sstevel@tonic-gate 		aug_save_event(AUE_halt_solaris);
670Sstevel@tonic-gate 	else if (strcmp(cmdname, "poweroff") == 0)
680Sstevel@tonic-gate 		aug_save_event(AUE_poweroff_solaris);
690Sstevel@tonic-gate 	else
700Sstevel@tonic-gate 		exit(1);
710Sstevel@tonic-gate 	(void) aug_save_me();
720Sstevel@tonic-gate 	return (0);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate int
audit_halt_fail()760Sstevel@tonic-gate audit_halt_fail()
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	return (audit_halt_generic(-1));
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate int
audit_halt_success()820Sstevel@tonic-gate audit_halt_success()
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	int res = 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	(void) audit_halt_generic(0);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/* wait for audit daemon to put halt message onto audit trail */
890Sstevel@tonic-gate 	if (!cannot_audit(0)) {
900Sstevel@tonic-gate 		int cond = AUC_NOAUDIT;
910Sstevel@tonic-gate 		int canaudit;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		(void) sleep(1);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 		/* find out if audit daemon is running */
960Sstevel@tonic-gate 		(void) auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond));
970Sstevel@tonic-gate 		canaudit = ((cond == AUC_AUDITING) || (cond == AUC_NOSPACE));
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		/* turn off audit daemon and try to flush audit queue */
100*12160SMarek.Pospisil@Sun.COM 		if (canaudit && system("/usr/sbin/audit -T"))
1010Sstevel@tonic-gate 			res = -1;
1020Sstevel@tonic-gate 		else
1030Sstevel@tonic-gate 		/* give a chance for syslogd to do the job */
1040Sstevel@tonic-gate 			(void) sleep(5);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	return (res);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate int
audit_halt_generic(sorf)1110Sstevel@tonic-gate audit_halt_generic(sorf)
1120Sstevel@tonic-gate 	int sorf;
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	int r;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	dprintf(("audit_halt_generic(%d)\n", sorf));
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (cannot_audit(0)) {
1190Sstevel@tonic-gate 		return (0);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	aug_save_sorf(sorf);
1230Sstevel@tonic-gate 	r = aug_audit();
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	return (r);
1260Sstevel@tonic-gate }
127