152267f74SRobert Watson /*-
206edd2f1SRobert Watson * Copyright (c) 2005-2009 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 */
29ca0716f5SRobert Watson /*
30ca0716f5SRobert Watson * Program to trigger the audit daemon with a message that is either:
31ca0716f5SRobert Watson * - Open a new audit log file
32ca0716f5SRobert Watson * - Read the audit control file and take action on it
33ca0716f5SRobert Watson * - Close the audit log file and exit
34ca0716f5SRobert Watson *
35ca0716f5SRobert Watson */
36ca0716f5SRobert Watson
37ca0716f5SRobert Watson #include <sys/types.h>
3852267f74SRobert Watson #include <config/config.h>
3952267f74SRobert Watson #ifdef HAVE_FULL_QUEUE_H
40f4e380b0SRobert Watson #include <sys/queue.h>
4152267f74SRobert Watson #else /* !HAVE_FULL_QUEUE_H */
4252267f74SRobert Watson #include <compat/queue.h>
4352267f74SRobert Watson #endif /* !HAVE_FULL_QUEUE_H */
44ca0716f5SRobert Watson #include <sys/uio.h>
45ca0716f5SRobert Watson
463b97a967SRobert Watson #include <bsm/libbsm.h>
47ca0716f5SRobert Watson
487a0a89d2SRobert Watson #include <errno.h>
49ca0716f5SRobert Watson #include <fcntl.h>
50ca0716f5SRobert Watson #include <stdio.h>
51ca0716f5SRobert Watson #include <stdlib.h>
52ca0716f5SRobert Watson #include <unistd.h>
53ca0716f5SRobert Watson
5452267f74SRobert Watson
55c0020399SRobert Watson static int send_trigger(int);
5652267f74SRobert Watson
5752267f74SRobert Watson #ifdef USE_MACH_IPC
5852267f74SRobert Watson #include <mach/mach.h>
5952267f74SRobert Watson #include <servers/netname.h>
6052267f74SRobert Watson #include <mach/message.h>
6152267f74SRobert Watson #include <mach/port.h>
6252267f74SRobert Watson #include <mach/mach_error.h>
6352267f74SRobert Watson #include <mach/host_special_ports.h>
6452267f74SRobert Watson #include <servers/bootstrap.h>
6552267f74SRobert Watson
667a0a89d2SRobert Watson #include "auditd_control.h"
677a0a89d2SRobert Watson
687a0a89d2SRobert Watson /*
6906edd2f1SRobert Watson * XXX The following are temporary until these can be added to the kernel
707a0a89d2SRobert Watson * audit.h header.
717a0a89d2SRobert Watson */
727a0a89d2SRobert Watson #ifndef AUDIT_TRIGGER_INITIALIZE
737a0a89d2SRobert Watson #define AUDIT_TRIGGER_INITIALIZE 7
747a0a89d2SRobert Watson #endif
7506edd2f1SRobert Watson #ifndef AUDIT_TRIGGER_EXPIRE_TRAILS
7606edd2f1SRobert Watson #define AUDIT_TRIGGER_EXPIRE_TRAILS 8
7706edd2f1SRobert Watson #endif
7852267f74SRobert Watson
7952267f74SRobert Watson static int
send_trigger(int trigger)80c0020399SRobert Watson send_trigger(int trigger)
8152267f74SRobert Watson {
8252267f74SRobert Watson mach_port_t serverPort;
8352267f74SRobert Watson kern_return_t error;
8452267f74SRobert Watson
8552267f74SRobert Watson error = host_get_audit_control_port(mach_host_self(), &serverPort);
8652267f74SRobert Watson if (error != KERN_SUCCESS) {
877a0a89d2SRobert Watson if (geteuid() != 0) {
887a0a89d2SRobert Watson errno = EPERM;
897a0a89d2SRobert Watson perror("audit requires root privileges");
907a0a89d2SRobert Watson } else
917a0a89d2SRobert Watson mach_error("Cannot get auditd_control Mach port:",
927a0a89d2SRobert Watson error);
9352267f74SRobert Watson return (-1);
9452267f74SRobert Watson }
9552267f74SRobert Watson
9652267f74SRobert Watson error = auditd_control(serverPort, trigger);
9752267f74SRobert Watson if (error != KERN_SUCCESS) {
9852267f74SRobert Watson mach_error("Error sending trigger: ", error);
9952267f74SRobert Watson return (-1);
10052267f74SRobert Watson }
10152267f74SRobert Watson
10252267f74SRobert Watson return (0);
10352267f74SRobert Watson }
10452267f74SRobert Watson
10552267f74SRobert Watson #else /* ! USE_MACH_IPC */
10652267f74SRobert Watson
10752267f74SRobert Watson static int
send_trigger(int trigger)108c0020399SRobert Watson send_trigger(int trigger)
10952267f74SRobert Watson {
11052267f74SRobert Watson int error;
11152267f74SRobert Watson
112c0020399SRobert Watson error = audit_send_trigger(&trigger);
11352267f74SRobert Watson if (error != 0) {
1147a0a89d2SRobert Watson if (error == EPERM)
1157a0a89d2SRobert Watson perror("audit requires root privileges");
1167a0a89d2SRobert Watson else
11752267f74SRobert Watson perror("Error sending trigger");
11852267f74SRobert Watson return (-1);
11952267f74SRobert Watson }
12052267f74SRobert Watson
12152267f74SRobert Watson return (0);
12252267f74SRobert Watson }
12352267f74SRobert Watson #endif /* ! USE_MACH_IPC */
12452267f74SRobert Watson
125ca0716f5SRobert Watson static void
usage(void)126ca0716f5SRobert Watson usage(void)
127ca0716f5SRobert Watson {
128ca0716f5SRobert Watson
12906edd2f1SRobert Watson (void)fprintf(stderr, "Usage: audit -e | -i | -n | -s | -t \n");
130ca0716f5SRobert Watson exit(-1);
131ca0716f5SRobert Watson }
132ca0716f5SRobert Watson
133ca0716f5SRobert Watson /*
134ca0716f5SRobert Watson * Main routine to process command line options.
135ca0716f5SRobert Watson */
136ca0716f5SRobert Watson int
main(int argc,char ** argv)137ca0716f5SRobert Watson main(int argc, char **argv)
138ca0716f5SRobert Watson {
13923bf6e20SRobert Watson int ch;
140ca0716f5SRobert Watson unsigned int trigger = 0;
141ca0716f5SRobert Watson
142ca0716f5SRobert Watson if (argc != 2)
143ca0716f5SRobert Watson usage();
144ca0716f5SRobert Watson
14506edd2f1SRobert Watson while ((ch = getopt(argc, argv, "einst")) != -1) {
146ca0716f5SRobert Watson switch(ch) {
147ca0716f5SRobert Watson
14806edd2f1SRobert Watson case 'e':
14906edd2f1SRobert Watson trigger = AUDIT_TRIGGER_EXPIRE_TRAILS;
15006edd2f1SRobert Watson break;
15106edd2f1SRobert Watson
1527a0a89d2SRobert Watson case 'i':
1537a0a89d2SRobert Watson trigger = AUDIT_TRIGGER_INITIALIZE;
1547a0a89d2SRobert Watson break;
1557a0a89d2SRobert Watson
156ca0716f5SRobert Watson case 'n':
157bb97b418SRobert Watson trigger = AUDIT_TRIGGER_ROTATE_USER;
158ca0716f5SRobert Watson break;
159ca0716f5SRobert Watson
160ca0716f5SRobert Watson case 's':
161ca0716f5SRobert Watson trigger = AUDIT_TRIGGER_READ_FILE;
162ca0716f5SRobert Watson break;
163ca0716f5SRobert Watson
164ca0716f5SRobert Watson case 't':
165ca0716f5SRobert Watson trigger = AUDIT_TRIGGER_CLOSE_AND_DIE;
166ca0716f5SRobert Watson break;
167ca0716f5SRobert Watson
168ca0716f5SRobert Watson case '?':
169ca0716f5SRobert Watson default:
170ca0716f5SRobert Watson usage();
171ca0716f5SRobert Watson break;
172ca0716f5SRobert Watson }
173ca0716f5SRobert Watson }
17452267f74SRobert Watson if (send_trigger(trigger) < 0)
175ca0716f5SRobert Watson exit(-1);
17652267f74SRobert Watson
177ca0716f5SRobert Watson printf("Trigger sent.\n");
178ca0716f5SRobert Watson exit (0);
179ca0716f5SRobert Watson }
180