2 * Copyright (c) 2004-2009 Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditd/auditd.c#46 $
32 #include <sys/types.h>
34 #include <config/config.h>
36 #include <sys/dirent.h>
37 #ifdef HAVE_FULL_QUEUE_H
38 #include <sys/queue.h>
39 #else /* !HAVE_FULL_QUEUE_H */
40 #include <compat/queue.h>
41 #endif /* !HAVE_FULL_QUEUE_H */
43 #include <sys/param.h>
47 #include <bsm/audit.h>
48 #include <bsm/audit_uevents.h>
49 #include <bsm/auditd_lib.h>
50 #include <bsm/libbsm.h>
66 #include <compat/strlcpy.h>
70 * XXX The following are temporary until these can be added to the kernel
73 #ifndef AUDIT_TRIGGER_INITIALIZE
74 #define AUDIT_TRIGGER_INITIALIZE 7
76 #ifndef AUDIT_TRIGGER_EXPIRE_TRAILS
77 #define AUDIT_TRIGGER_EXPIRE_TRAILS 8
82 * LaunchD flag (Mac OS X and, maybe, FreeBSD only.) See launchd(8) and
83 * http://wiki.freebsd.org/launchd for more information.
85 * In order for auditd to work "on demand" with launchd(8) it can't:
87 * call fork and having the parent process exit
88 * change uids or gids.
89 * set up the current working directory or chroot.
91 * change stdio to /dev/null.
95 * auditd (in 'launchd mode') is launched on demand so it must catch
96 * SIGTERM to exit cleanly.
98 static int launchd_flag = 0;
101 * The GID of the audit review group (if used). The audit trail files and
102 * system logs (Mac OS X only) can only be reviewed by members of this group
103 * or the audit administrator (aka. "root").
105 static gid_t audit_review_gid = -1;
108 * The path and file name of the last audit trail file.
110 static char *lastfile = NULL;
113 * Error starting auditd. Run warn script and exit.
119 audit_warn_nostart();
124 * Follow the 'current' symlink to get the active trail file name.
132 cf = malloc(MAXPATHLEN);
134 auditd_log_err("malloc failed: %m");
138 len = readlink(AUDIT_CURRENT_LINK, cf, MAXPATHLEN - 1);
144 /* readlink() doesn't terminate string. */
151 * Close the previous audit trail file.
154 close_lastfile(char *TS)
160 /* If lastfile is NULL try to get it from the 'current' link. */
161 if (lastfile == NULL)
162 lastfile = get_curfile();
164 if (lastfile != NULL) {
165 len = strlen(lastfile) + 1;
166 oldname = (char *)malloc(len);
169 strlcpy(oldname, lastfile, len);
171 /* Rename the last file -- append timestamp. */
172 if ((ptr = strstr(lastfile, NOT_TERMINATED)) != NULL) {
173 memcpy(ptr, TS, POSTFIX_LEN);
174 if (rename(oldname, lastfile) != 0)
176 "Could not rename %s to %s: %m", oldname,
180 * Remove the 'current' symlink since the link
183 (void) unlink(AUDIT_CURRENT_LINK);
184 auditd_log_notice( "renamed %s to %s",
186 audit_warn_closefile(lastfile);
189 auditd_log_err( "Could not rename %s to %s", oldname,
199 * Create the new file name, swap with existing audit file.
202 swap_audit_file(void)
206 char TS[TIMESTAMP_LEN];
209 if (getTSstr(tt, TS, TIMESTAMP_LEN) != 0)
211 err = auditd_swap_trail(TS, &newfile, audit_review_gid,
212 audit_warn_getacdir);
213 if (err != ADE_NOERR) {
214 auditd_log_err( "%s: %m", auditd_strerror(err));
220 * Only close the last file if were in an auditing state before
221 * calling swap_audit_file(). We may need to recover from a crash.
223 if (auditd_get_state() == AUD_STATE_ENABLED)
228 * auditd_swap_trail() potentially enables auditing (if not already
229 * enabled) so updated the cached state as well.
231 auditd_set_state(AUD_STATE_ENABLED);
234 * Create 'current' symlink. Recover from crash, if needed.
236 if (auditd_new_curlink(newfile) != 0)
237 auditd_log_err("auditd_new_curlink(\"%s\") failed: %s: %m",
238 newfile, auditd_strerror(err));
241 auditd_log_notice("New audit file is %s", newfile);
247 * Create a new audit log trail file and swap with the current one, if any.
255 * First, refresh the list of audit log directories.
257 err = auditd_read_dirs(audit_warn_soft, audit_warn_hard);
259 auditd_log_err("auditd_read_dirs(): %s",
260 auditd_strerror(err));
261 if (err == ADE_HARDLIM)
262 audit_warn_allhard();
263 if (err != ADE_SOFTLIM)
266 audit_warn_allsoft();
267 /* continue on with soft limit error */
271 * Create a new file and swap with the one being used in kernel.
273 if (swap_audit_file() == -1) {
275 * XXX Faulty directory listing? - user should be given
276 * XXX an opportunity to change the audit_control file
277 * XXX switch to a reduced mode of auditing?
283 * Finally, see if there are any trail files to expire.
285 err = auditd_expire_trails(audit_warn_expired);
287 auditd_log_err("auditd_expire_trails(): %s",
288 auditd_strerror(err));
301 if (do_trail_file() == -1) {
302 auditd_log_err("Error creating audit trail file");
306 /* Generate an audit record. */
307 err = auditd_gen_record(AUE_audit_startup, NULL);
309 auditd_log_err("auditd_gen_record(AUE_audit_startup) %s: %m",
310 auditd_strerror(err));
312 if (auditd_config_controls() == 0)
313 auditd_log_info("Audit controls init successful");
315 auditd_log_err("Audit controls init failed");
321 * Close auditd pid file and trigger mechanism.
328 if (unlink(AUDITD_PIDFILE) == -1 && errno != ENOENT) {
329 auditd_log_err("Couldn't remove %s: %m", AUDITD_PIDFILE);
334 if (auditd_close_trigger() != 0) {
335 auditd_log_err("Error closing trigger messaging mechanism");
342 * Close all log files, control files, and tell the audit system.
348 char TS[TIMESTAMP_LEN];
353 err = auditd_gen_record(AUE_audit_shutdown, NULL);
355 auditd_log_err("auditd_gen_record(AUE_audit_shutdown) %s: %m",
356 auditd_strerror(err));
358 /* Flush contents. */
360 err_ret = audit_set_cond(&cond);
362 auditd_log_err("Disabling audit failed! : %s", strerror(errno));
367 * Updated the cached state that auditing has been disabled.
369 auditd_set_state(AUD_STATE_DISABLED);
371 if (getTSstr(tt, TS, TIMESTAMP_LEN) == 0)
373 if (lastfile != NULL)
376 err_ret += close_misc();
379 auditd_log_err("Could not unregister");
380 audit_warn_postsigterm();
383 auditd_log_info("Finished");
388 * Register the daemon with the signal handler and the auditd pid file.
391 register_daemon(void)
397 /* Set up the signal hander. */
398 if (signal(SIGTERM, auditd_relay_signal) == SIG_ERR) {
400 "Could not set signal handler for SIGTERM");
403 if (signal(SIGCHLD, auditd_relay_signal) == SIG_ERR) {
405 "Could not set signal handler for SIGCHLD");
408 if (signal(SIGHUP, auditd_relay_signal) == SIG_ERR) {
410 "Could not set signal handler for SIGHUP");
413 if (signal(SIGALRM, auditd_relay_signal) == SIG_ERR) {
415 "Could not set signal handler for SIGALRM");
419 if ((pidfile = fopen(AUDITD_PIDFILE, "a")) == NULL) {
420 auditd_log_err("Could not open PID file");
421 audit_warn_tmpfile();
425 /* Attempt to lock the pid file; if a lock is present, exit. */
426 fd = fileno(pidfile);
427 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
429 "PID file is locked (is another auditd running?).");
436 if (fprintf(pidfile, "%u\n", pid) < 0) {
437 /* Should not start the daemon. */
446 * Handle the audit trigger event.
448 * We suppress (ignore) duplicated triggers in close succession in order to
449 * try to avoid thrashing-like behavior. However, not all triggers can be
450 * ignored, as triggers generally represent edge triggers, not level
451 * triggers, and won't be retransmitted if the condition persists. Of
452 * specific concern is the rotate trigger -- if one is dropped, then it will
453 * not be retransmitted, and the log file will grow in an unbounded fashion.
455 #define DUPLICATE_INTERVAL 30
457 auditd_handle_trigger(int trigger)
459 static int last_trigger, last_warning;
460 static time_t last_time;
468 * Suppress duplicate messages from the kernel within the specified
471 if (gettimeofday(&ts, &tzp) == 0) {
472 tt = (time_t)ts.tv_sec;
474 case AUDIT_TRIGGER_LOW_SPACE:
475 case AUDIT_TRIGGER_NO_SPACE:
477 * Triggers we can suppress. Of course, we also need
478 * to rate limit the warnings, so apply the same
479 * interval limit on syslog messages.
481 if ((trigger == last_trigger) &&
482 (tt < (last_time + DUPLICATE_INTERVAL))) {
483 if (tt >= (last_warning + DUPLICATE_INTERVAL))
485 "Suppressing duplicate trigger %d",
492 case AUDIT_TRIGGER_ROTATE_KERNEL:
493 case AUDIT_TRIGGER_ROTATE_USER:
494 case AUDIT_TRIGGER_READ_FILE:
495 case AUDIT_TRIGGER_CLOSE_AND_DIE:
496 case AUDIT_TRIGGER_INITIALIZE:
498 * Triggers that we cannot suppress.
504 * Only update last_trigger after aborting due to a duplicate
505 * trigger, not before, or we will never allow that trigger
508 last_trigger = trigger;
512 au_state = auditd_get_state();
515 * Message processing is done here.
518 case AUDIT_TRIGGER_LOW_SPACE:
519 auditd_log_notice("Got low space trigger");
520 if (do_trail_file() == -1)
521 auditd_log_err("Error swapping audit file");
524 case AUDIT_TRIGGER_NO_SPACE:
525 auditd_log_notice("Got no space trigger");
526 if (do_trail_file() == -1)
527 auditd_log_err("Error swapping audit file");
530 case AUDIT_TRIGGER_ROTATE_KERNEL:
531 case AUDIT_TRIGGER_ROTATE_USER:
532 auditd_log_info("Got open new trigger from %s", trigger ==
533 AUDIT_TRIGGER_ROTATE_KERNEL ? "kernel" : "user");
534 if (au_state == AUD_STATE_ENABLED && do_trail_file() == -1)
535 auditd_log_err("Error swapping audit file");
538 case AUDIT_TRIGGER_READ_FILE:
539 auditd_log_info("Got read file trigger");
540 if (au_state == AUD_STATE_ENABLED) {
541 if (auditd_config_controls() == -1)
542 auditd_log_err("Error setting audit controls");
543 else if (do_trail_file() == -1)
544 auditd_log_err("Error swapping audit file");
548 case AUDIT_TRIGGER_CLOSE_AND_DIE:
549 auditd_log_info("Got close and die trigger");
550 if (au_state == AUD_STATE_ENABLED)
553 * Running under launchd don't exit. Wait for launchd to
557 auditd_log_info("auditd exiting.");
562 case AUDIT_TRIGGER_INITIALIZE:
563 auditd_log_info("Got audit initialize trigger");
564 if (au_state == AUD_STATE_DISABLED)
568 case AUDIT_TRIGGER_EXPIRE_TRAILS:
569 auditd_log_info("Got audit expire trails trigger");
570 err = auditd_expire_trails(audit_warn_expired);
572 auditd_log_err("auditd_expire_trails(): %s",
573 auditd_strerror(err));
577 auditd_log_err("Got unknown trigger %d", trigger);
586 auditd_reap_children(void)
591 while ((child = waitpid(-1, &wstatus, WNOHANG)) > 0) {
594 auditd_log_info("warn process [pid=%d] %s %d.", child,
595 ((WIFEXITED(wstatus)) ? "exited with non-zero status" :
596 "exited as a result of signal"),
597 ((WIFEXITED(wstatus)) ? WEXITSTATUS(wstatus) :
603 * Reap any children and terminate. If under launchd don't shutdown auditing
604 * but just the other stuff.
607 auditd_terminate(void)
611 auditd_reap_children();
622 * Configure the audit controls in the kernel: the event to class mapping,
623 * kernel preselection mask, etc.
626 auditd_config_controls(void)
632 * Configure event to class mappings in kernel.
634 cnt = auditd_set_evcmap();
636 auditd_log_err("auditd_set_evcmap() failed: %m");
638 } else if (cnt == 0) {
639 auditd_log_err("No events to class mappings registered.");
642 auditd_log_debug("Registered %d event to class mappings.", cnt);
645 * Configure non-attributable event mask in kernel.
647 err = auditd_set_namask();
649 auditd_log_err("auditd_set_namask() %s: %m",
650 auditd_strerror(err));
653 auditd_log_debug("Registered non-attributable event mask.");
656 * Configure audit policy in kernel.
658 err = auditd_set_policy();
660 auditd_log_err("auditd_set_policy() %s: %m",
661 auditd_strerror(err));
664 auditd_log_debug("Set audit policy in kernel.");
667 * Configure audit trail log size in kernel.
669 err = auditd_set_fsize();
671 auditd_log_err("audit_set_fsize() %s: %m",
672 auditd_strerror(err));
675 auditd_log_debug("Set audit trail size in kernel.");
678 * Configure audit trail volume minimum free percentage of blocks in
681 err = auditd_set_minfree();
683 auditd_log_err("auditd_set_minfree() %s: %m",
684 auditd_strerror(err));
688 "Set audit trail min free percent in kernel.");
691 * Configure host address in the audit kernel information.
693 err = auditd_set_host();
695 if (err == ADE_PARSE) {
697 "audit_control(5) may be missing 'host:' field");
699 auditd_log_err("auditd_set_host() %s: %m",
700 auditd_strerror(err));
705 "Set audit host address information in kernel.");
711 * Setup and initialize auditd.
718 if (auditd_open_trigger(launchd_flag) < 0) {
719 auditd_log_err("Error opening trigger messaging mechanism");
724 * To prevent event feedback cycles and avoid auditd becoming
725 * stalled if auditing is suspended, auditd and its children run
726 * without their events being audited. We allow the uid, tid, and
727 * mask fields to be implicitly set to zero, but do set the pid. We
728 * run this after opening the trigger device to avoid configuring
729 * audit state without audit present in the system.
731 err = auditd_prevent_audit();
733 auditd_log_err("auditd_prevent_audit() %s: %m",
734 auditd_strerror(err));
739 * Make sure auditd auditing state is correct.
741 auditd_set_state(AUD_STATE_INIT);
744 * If under launchd, don't start auditing. Wait for a trigger to
752 main(int argc, char **argv)
756 #ifdef AUDIT_REVIEW_GROUP
760 while ((ch = getopt(argc, argv, "dl")) != -1) {
768 /* Be launchd friendly. */
774 (void)fprintf(stderr,
775 "usage: auditd [-d] [-l]\n");
780 audit_review_gid = getgid();
782 #ifdef AUDIT_REVIEW_GROUP
784 * XXXRW: Currently, this code falls back to the daemon gid, which is
785 * likely the wheel group. Is there a better way to deal with this?
787 grp = getgrnam(AUDIT_REVIEW_GROUP);
789 audit_review_gid = grp->gr_gid;
792 auditd_openlog(debug, audit_review_gid);
795 auditd_log_info("started by launchd...");
797 auditd_log_info("starting...");
799 #ifdef AUDIT_REVIEW_GROUP
802 "Audit review group '%s' not available, using daemon gid (%d)",
803 AUDIT_REVIEW_GROUP, audit_review_gid);
805 if (debug == 0 && launchd_flag == 0 && daemon(0, 0) == -1) {
806 auditd_log_err("Failed to daemonize");
810 if (register_daemon() == -1) {
811 auditd_log_err("Could not register as daemon");
818 * auditd_wait_for_events() shouldn't return unless something is wrong.
820 auditd_wait_for_events();
822 auditd_log_err("abnormal exit.");