xref: /freebsd/contrib/openbsm/bin/audit/audit.c (revision 3b97a967e1e992eaa2010e8a42f23f51760bc8cb)
1ca0716f5SRobert Watson /*
2ca0716f5SRobert Watson  * Copyright (c) 2005 Apple Computer, Inc.
3ca0716f5SRobert Watson  * All rights reserved.
4ca0716f5SRobert Watson  *
5ca0716f5SRobert Watson  * @APPLE_BSD_LICENSE_HEADER_START@
6ca0716f5SRobert Watson  *
7ca0716f5SRobert Watson  * Redistribution and use in source and binary forms, with or without
8ca0716f5SRobert Watson  * modification, are permitted provided that the following conditions
9ca0716f5SRobert Watson  * are met:
10ca0716f5SRobert Watson  *
11ca0716f5SRobert Watson  * 1.  Redistributions of source code must retain the above copyright
12ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer.
13ca0716f5SRobert Watson  * 2.  Redistributions in binary form must reproduce the above copyright
14ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer in the
15ca0716f5SRobert Watson  *     documentation and/or other materials provided with the distribution.
16ca0716f5SRobert Watson  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17ca0716f5SRobert Watson  *     its contributors may be used to endorse or promote products derived
18ca0716f5SRobert Watson  *     from this software without specific prior written permission.
19ca0716f5SRobert Watson  *
20ca0716f5SRobert Watson  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21ca0716f5SRobert Watson  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22ca0716f5SRobert Watson  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23ca0716f5SRobert Watson  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24ca0716f5SRobert Watson  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25ca0716f5SRobert Watson  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26ca0716f5SRobert Watson  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27ca0716f5SRobert Watson  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ca0716f5SRobert Watson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29ca0716f5SRobert Watson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ca0716f5SRobert Watson  *
31ca0716f5SRobert Watson  * @APPLE_BSD_LICENSE_HEADER_END@
32ca0716f5SRobert Watson  *
333b97a967SRobert Watson  * $P4: //depot/projects/trustedbsd/openbsm/bin/audit/audit.c#6 $
34ca0716f5SRobert Watson  */
35ca0716f5SRobert Watson /*
36ca0716f5SRobert Watson  * Program to trigger the audit daemon with a message that is either:
37ca0716f5SRobert Watson  *    - Open a new audit log file
38ca0716f5SRobert Watson  *    - Read the audit control file and take action on it
39ca0716f5SRobert Watson  *    - Close the audit log file and exit
40ca0716f5SRobert Watson  *
41ca0716f5SRobert Watson  */
42ca0716f5SRobert Watson 
43ca0716f5SRobert Watson #include <sys/types.h>
44f4e380b0SRobert Watson #include <sys/queue.h>
45ca0716f5SRobert Watson #include <sys/uio.h>
46ca0716f5SRobert Watson 
473b97a967SRobert Watson #include <bsm/libbsm.h>
48ca0716f5SRobert Watson 
49ca0716f5SRobert Watson #include <fcntl.h>
50ca0716f5SRobert Watson #include <stdio.h>
51ca0716f5SRobert Watson #include <stdlib.h>
52ca0716f5SRobert Watson #include <unistd.h>
53ca0716f5SRobert Watson 
54ca0716f5SRobert Watson static void
55ca0716f5SRobert Watson usage(void)
56ca0716f5SRobert Watson {
57ca0716f5SRobert Watson 
58ca0716f5SRobert Watson 	(void)fprintf(stderr, "Usage: audit -n | -s | -t \n");
59ca0716f5SRobert Watson 	exit(-1);
60ca0716f5SRobert Watson }
61ca0716f5SRobert Watson 
62ca0716f5SRobert Watson /*
63ca0716f5SRobert Watson  * Main routine to process command line options.
64ca0716f5SRobert Watson  */
65ca0716f5SRobert Watson int
66ca0716f5SRobert Watson main(int argc, char **argv)
67ca0716f5SRobert Watson {
6823bf6e20SRobert Watson 	int ch;
69ca0716f5SRobert Watson 	unsigned int trigger = 0;
70ca0716f5SRobert Watson 
71ca0716f5SRobert Watson 	if (argc != 2)
72ca0716f5SRobert Watson 		usage();
73ca0716f5SRobert Watson 
74ca0716f5SRobert Watson 	while ((ch = getopt(argc, argv, "nst")) != -1) {
75ca0716f5SRobert Watson 		switch(ch) {
76ca0716f5SRobert Watson 
77ca0716f5SRobert Watson 		case 'n':
78ca0716f5SRobert Watson 			trigger = AUDIT_TRIGGER_OPEN_NEW;
79ca0716f5SRobert Watson 			break;
80ca0716f5SRobert Watson 
81ca0716f5SRobert Watson 		case 's':
82ca0716f5SRobert Watson 			trigger = AUDIT_TRIGGER_READ_FILE;
83ca0716f5SRobert Watson 			break;
84ca0716f5SRobert Watson 
85ca0716f5SRobert Watson 		case 't':
86ca0716f5SRobert Watson 			trigger = AUDIT_TRIGGER_CLOSE_AND_DIE;
87ca0716f5SRobert Watson 			break;
88ca0716f5SRobert Watson 
89ca0716f5SRobert Watson 		case '?':
90ca0716f5SRobert Watson 		default:
91ca0716f5SRobert Watson 			usage();
92ca0716f5SRobert Watson 			break;
93ca0716f5SRobert Watson 		}
94ca0716f5SRobert Watson 	}
95ca0716f5SRobert Watson 	if (auditon(A_SENDTRIGGER, &trigger, sizeof(trigger)) < 0) {
96ca0716f5SRobert Watson 		perror("Error sending trigger");
97ca0716f5SRobert Watson 		exit(-1);
98ca0716f5SRobert Watson 	} else {
99ca0716f5SRobert Watson 		printf("Trigger sent.\n");
100ca0716f5SRobert Watson 		exit (0);
101ca0716f5SRobert Watson 	}
102ca0716f5SRobert Watson }
103