xref: /freebsd/contrib/openbsm/bin/audit/audit.c (revision 7a0a89d2cb29ee2c383600fa59e42d714a6dcbcb)
152267f74SRobert Watson /*-
252267f74SRobert Watson  * Copyright (c) 2005-2008 Apple Inc.
3ca0716f5SRobert Watson  * All rights reserved.
4ca0716f5SRobert Watson  *
5ca0716f5SRobert Watson  * Redistribution and use in source and binary forms, with or without
6ca0716f5SRobert Watson  * modification, are permitted provided that the following conditions
7ca0716f5SRobert Watson  * are met:
8ca0716f5SRobert Watson  *
9ca0716f5SRobert Watson  * 1.  Redistributions of source code must retain the above copyright
10ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer.
11ca0716f5SRobert Watson  * 2.  Redistributions in binary form must reproduce the above copyright
12ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer in the
13ca0716f5SRobert Watson  *     documentation and/or other materials provided with the distribution.
1452267f74SRobert Watson  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15ca0716f5SRobert Watson  *     its contributors may be used to endorse or promote products derived
16ca0716f5SRobert Watson  *     from this software without specific prior written permission.
17ca0716f5SRobert Watson  *
18ca0716f5SRobert Watson  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19ca0716f5SRobert Watson  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20ca0716f5SRobert Watson  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21ca0716f5SRobert Watson  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22ca0716f5SRobert Watson  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23ca0716f5SRobert Watson  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24ca0716f5SRobert Watson  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25ca0716f5SRobert Watson  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26ca0716f5SRobert Watson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27ca0716f5SRobert Watson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28ca0716f5SRobert Watson  *
297a0a89d2SRobert Watson  * $P4: //depot/projects/trustedbsd/openbsm/bin/audit/audit.c#13 $
30ca0716f5SRobert Watson  */
31ca0716f5SRobert Watson /*
32ca0716f5SRobert Watson  * Program to trigger the audit daemon with a message that is either:
33ca0716f5SRobert Watson  *    - Open a new audit log file
34ca0716f5SRobert Watson  *    - Read the audit control file and take action on it
35ca0716f5SRobert Watson  *    - Close the audit log file and exit
36ca0716f5SRobert Watson  *
37ca0716f5SRobert Watson  */
38ca0716f5SRobert Watson 
39ca0716f5SRobert Watson #include <sys/types.h>
4052267f74SRobert Watson #include <config/config.h>
4152267f74SRobert Watson #ifdef HAVE_FULL_QUEUE_H
42f4e380b0SRobert Watson #include <sys/queue.h>
4352267f74SRobert Watson #else /* !HAVE_FULL_QUEUE_H */
4452267f74SRobert Watson #include <compat/queue.h>
4552267f74SRobert Watson #endif /* !HAVE_FULL_QUEUE_H */
46ca0716f5SRobert Watson #include <sys/uio.h>
47ca0716f5SRobert Watson 
483b97a967SRobert Watson #include <bsm/libbsm.h>
49ca0716f5SRobert Watson 
507a0a89d2SRobert Watson #include <errno.h>
51ca0716f5SRobert Watson #include <fcntl.h>
52ca0716f5SRobert Watson #include <stdio.h>
53ca0716f5SRobert Watson #include <stdlib.h>
54ca0716f5SRobert Watson #include <unistd.h>
55ca0716f5SRobert Watson 
5652267f74SRobert Watson 
5752267f74SRobert Watson static int send_trigger(unsigned int);
5852267f74SRobert Watson 
5952267f74SRobert Watson #ifdef USE_MACH_IPC
6052267f74SRobert Watson #include <mach/mach.h>
6152267f74SRobert Watson #include <servers/netname.h>
6252267f74SRobert Watson #include <mach/message.h>
6352267f74SRobert Watson #include <mach/port.h>
6452267f74SRobert Watson #include <mach/mach_error.h>
6552267f74SRobert Watson #include <mach/host_special_ports.h>
6652267f74SRobert Watson #include <servers/bootstrap.h>
6752267f74SRobert Watson 
687a0a89d2SRobert Watson #include "auditd_control.h"
697a0a89d2SRobert Watson 
707a0a89d2SRobert Watson /*
717a0a89d2SRobert Watson  * XXX the following is temporary until this can be added to the kernel
727a0a89d2SRobert Watson  * audit.h header.
737a0a89d2SRobert Watson  */
747a0a89d2SRobert Watson #ifndef AUDIT_TRIGGER_INITIALIZE
757a0a89d2SRobert Watson #define	AUDIT_TRIGGER_INITIALIZE	7
767a0a89d2SRobert Watson #endif
7752267f74SRobert Watson 
7852267f74SRobert Watson static int
7952267f74SRobert Watson send_trigger(unsigned int trigger)
8052267f74SRobert Watson {
8152267f74SRobert Watson 	mach_port_t     serverPort;
8252267f74SRobert Watson 	kern_return_t	error;
8352267f74SRobert Watson 
8452267f74SRobert Watson 	error = host_get_audit_control_port(mach_host_self(), &serverPort);
8552267f74SRobert Watson 	if (error != KERN_SUCCESS) {
867a0a89d2SRobert Watson 		if (geteuid() != 0) {
877a0a89d2SRobert Watson 			errno = EPERM;
887a0a89d2SRobert Watson 			perror("audit requires root privileges");
897a0a89d2SRobert Watson 		} else
907a0a89d2SRobert Watson 			mach_error("Cannot get auditd_control Mach port:",
917a0a89d2SRobert Watson 			    error);
9252267f74SRobert Watson 		return (-1);
9352267f74SRobert Watson 	}
9452267f74SRobert Watson 
9552267f74SRobert Watson 	error = auditd_control(serverPort, trigger);
9652267f74SRobert Watson 	if (error != KERN_SUCCESS) {
9752267f74SRobert Watson 		mach_error("Error sending trigger: ", error);
9852267f74SRobert Watson 		return (-1);
9952267f74SRobert Watson 	}
10052267f74SRobert Watson 
10152267f74SRobert Watson 	return (0);
10252267f74SRobert Watson }
10352267f74SRobert Watson 
10452267f74SRobert Watson #else /* ! USE_MACH_IPC */
10552267f74SRobert Watson 
10652267f74SRobert Watson static int
10752267f74SRobert Watson send_trigger(unsigned int trigger)
10852267f74SRobert Watson {
10952267f74SRobert Watson 	int error;
11052267f74SRobert Watson 
11152267f74SRobert Watson 	error = auditon(A_SENDTRIGGER, &trigger, sizeof(trigger));
11252267f74SRobert Watson 	if (error != 0) {
1137a0a89d2SRobert Watson 		if (error == EPERM)
1147a0a89d2SRobert Watson 			perror("audit requires root privileges");
1157a0a89d2SRobert Watson 		else
11652267f74SRobert Watson 			perror("Error sending trigger");
11752267f74SRobert Watson 		return (-1);
11852267f74SRobert Watson 	}
11952267f74SRobert Watson 
12052267f74SRobert Watson 	return (0);
12152267f74SRobert Watson }
12252267f74SRobert Watson #endif /* ! USE_MACH_IPC */
12352267f74SRobert Watson 
124ca0716f5SRobert Watson static void
125ca0716f5SRobert Watson usage(void)
126ca0716f5SRobert Watson {
127ca0716f5SRobert Watson 
1287a0a89d2SRobert Watson 	(void)fprintf(stderr, "Usage: audit -i | -n | -s | -t \n");
129ca0716f5SRobert Watson 	exit(-1);
130ca0716f5SRobert Watson }
131ca0716f5SRobert Watson 
132ca0716f5SRobert Watson /*
133ca0716f5SRobert Watson  * Main routine to process command line options.
134ca0716f5SRobert Watson  */
135ca0716f5SRobert Watson int
136ca0716f5SRobert Watson main(int argc, char **argv)
137ca0716f5SRobert Watson {
13823bf6e20SRobert Watson 	int ch;
139ca0716f5SRobert Watson 	unsigned int trigger = 0;
140ca0716f5SRobert Watson 
141ca0716f5SRobert Watson 	if (argc != 2)
142ca0716f5SRobert Watson 		usage();
143ca0716f5SRobert Watson 
1447a0a89d2SRobert Watson 	while ((ch = getopt(argc, argv, "inst")) != -1) {
145ca0716f5SRobert Watson 		switch(ch) {
146ca0716f5SRobert Watson 
1477a0a89d2SRobert Watson 		case 'i':
1487a0a89d2SRobert Watson 			trigger = AUDIT_TRIGGER_INITIALIZE;
1497a0a89d2SRobert Watson 			break;
1507a0a89d2SRobert Watson 
151ca0716f5SRobert Watson 		case 'n':
152bb97b418SRobert Watson 			trigger = AUDIT_TRIGGER_ROTATE_USER;
153ca0716f5SRobert Watson 			break;
154ca0716f5SRobert Watson 
155ca0716f5SRobert Watson 		case 's':
156ca0716f5SRobert Watson 			trigger = AUDIT_TRIGGER_READ_FILE;
157ca0716f5SRobert Watson 			break;
158ca0716f5SRobert Watson 
159ca0716f5SRobert Watson 		case 't':
160ca0716f5SRobert Watson 			trigger = AUDIT_TRIGGER_CLOSE_AND_DIE;
161ca0716f5SRobert Watson 			break;
162ca0716f5SRobert Watson 
163ca0716f5SRobert Watson 		case '?':
164ca0716f5SRobert Watson 		default:
165ca0716f5SRobert Watson 			usage();
166ca0716f5SRobert Watson 			break;
167ca0716f5SRobert Watson 		}
168ca0716f5SRobert Watson 	}
16952267f74SRobert Watson 	if (send_trigger(trigger) < 0)
170ca0716f5SRobert Watson 		exit(-1);
17152267f74SRobert Watson 
172ca0716f5SRobert Watson 	printf("Trigger sent.\n");
173ca0716f5SRobert Watson 	exit (0);
174ca0716f5SRobert Watson }
175