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.
30 #include <sys/types.h>
32 #include <config/config.h>
34 #include <sys/dirent.h>
35 #ifdef HAVE_FULL_QUEUE_H
36 #include <sys/queue.h>
37 #else /* !HAVE_FULL_QUEUE_H */
38 #include <compat/queue.h>
39 #endif /* !HAVE_FULL_QUEUE_H */
41 #include <sys/param.h>
45 #include <bsm/audit.h>
46 #include <bsm/audit_uevents.h>
47 #include <bsm/auditd_lib.h>
48 #include <bsm/libbsm.h>
64 #include <compat/strlcpy.h>
68 * XXX The following are temporary until these can be added to the kernel
71 #ifndef AUDIT_TRIGGER_INITIALIZE
72 #define AUDIT_TRIGGER_INITIALIZE 7
74 #ifndef AUDIT_TRIGGER_EXPIRE_TRAILS
75 #define AUDIT_TRIGGER_EXPIRE_TRAILS 8
80 * LaunchD flag (Mac OS X and, maybe, FreeBSD only.) See launchd(8) and
81 * http://wiki.freebsd.org/launchd for more information.
83 * In order for auditd to work "on demand" with launchd(8) it can't:
85 * call fork and having the parent process exit
86 * change uids or gids.
87 * set up the current working directory or chroot.
89 * change stdio to /dev/null.
93 * auditd (in 'launchd mode') is launched on demand so it must catch
94 * SIGTERM to exit cleanly.
96 static int launchd_flag = 0;
99 * The GID of the audit review group (if used). The audit trail files and
100 * system logs (Mac OS X only) can only be reviewed by members of this group
101 * or the audit administrator (aka. "root").
103 static gid_t audit_review_gid = -1;
106 * The path and file name of the last audit trail file.
108 static char *lastfile = NULL;
111 * Error starting auditd. Run warn script and exit.
117 audit_warn_nostart();
122 * Follow the 'current' symlink to get the active trail file name.
130 cf = malloc(MAXPATHLEN);
132 auditd_log_err("malloc failed: %m");
136 len = readlink(AUDIT_CURRENT_LINK, cf, MAXPATHLEN - 1);
142 /* readlink() doesn't terminate string. */
149 * Close the previous audit trail file.
152 close_lastfile(char *TS)
157 /* If lastfile is NULL try to get it from the 'current' link. */
158 if (lastfile == NULL)
159 lastfile = get_curfile();
161 if (lastfile != NULL) {
162 oldname = strdup(lastfile);
166 /* Rename the last file -- append timestamp. */
167 if ((ptr = strstr(lastfile, NOT_TERMINATED)) != NULL) {
168 memcpy(ptr, TS, POSTFIX_LEN);
169 if (auditd_rename(oldname, lastfile) != 0)
171 "Could not rename %s to %s: %m", oldname,
175 * Remove the 'current' symlink since the link
178 (void) unlink(AUDIT_CURRENT_LINK);
179 auditd_log_notice("renamed %s to %s",
181 audit_warn_closefile(lastfile);
184 auditd_log_err("Could not rename %s to %s", oldname,
194 * Create the new file name, swap with existing audit file.
197 swap_audit_file(void)
200 char *newfile, *name;
201 char TS[TIMESTAMP_LEN + 1];
204 if (getTSstr(tt, TS, sizeof(TS)) != 0)
207 * If prefix and suffix are the same, it means that records are
208 * being produced too fast. We don't want to rename now, because
209 * next trail file can get the same name and once that one is
210 * terminated also within one second it will overwrite the current
211 * one. Just keep writing to the same trail and wait for the next
212 * trigger from the kernel.
213 * FREEBSD KERNEL WAS UPDATED TO KEEP SENDING TRIGGERS, WHICH MIGHT
214 * NOT BE THE CASE FOR OTHER OSES.
215 * If the kernel will not keep sending triggers, trail file will not
218 if (lastfile == NULL) {
221 name = strrchr(lastfile, '/');
225 if (name != NULL && strncmp(name, TS, TIMESTAMP_LEN) == 0) {
226 auditd_log_debug("Not ready to terminate trail file yet.");
229 err = auditd_swap_trail(TS, &newfile, audit_review_gid,
230 audit_warn_getacdir);
231 if (err != ADE_NOERR) {
232 auditd_log_err("%s: %m", auditd_strerror(err));
238 * Only close the last file if were in an auditing state before
239 * calling swap_audit_file(). We may need to recover from a crash.
241 if (auditd_get_state() == AUD_STATE_ENABLED)
246 * auditd_swap_trail() potentially enables auditing (if not already
247 * enabled) so updated the cached state as well.
249 auditd_set_state(AUD_STATE_ENABLED);
252 * Create 'current' symlink. Recover from crash, if needed.
254 if (auditd_new_curlink(newfile) != 0)
255 auditd_log_err("auditd_new_curlink(\"%s\") failed: %s: %m",
256 newfile, auditd_strerror(err));
259 auditd_log_notice("New audit file is %s", newfile);
265 * Create a new audit log trail file and swap with the current one, if any.
273 * First, refresh the list of audit log directories.
275 err = auditd_read_dirs(audit_warn_soft, audit_warn_hard);
277 auditd_log_err("auditd_read_dirs(): %s",
278 auditd_strerror(err));
279 if (err == ADE_HARDLIM)
280 audit_warn_allhard();
281 if (err != ADE_SOFTLIM)
284 audit_warn_allsoft();
285 /* continue on with soft limit error */
289 * Create a new file and swap with the one being used in kernel.
291 if (swap_audit_file() == -1) {
293 * XXX Faulty directory listing? - user should be given
294 * XXX an opportunity to change the audit_control file
295 * XXX switch to a reduced mode of auditing?
301 * Finally, see if there are any trail files to expire.
303 err = auditd_expire_trails(audit_warn_expired);
305 auditd_log_err("auditd_expire_trails(): %s",
306 auditd_strerror(err));
319 /* Configure trail files distribution. */
320 err = auditd_set_dist();
322 auditd_log_err("auditd_set_dist() %s: %m",
323 auditd_strerror(err));
325 auditd_log_debug("Configured trail files distribution.");
327 if (do_trail_file() == -1) {
328 auditd_log_err("Error creating audit trail file");
332 /* Generate an audit record. */
333 err = auditd_gen_record(AUE_audit_startup, NULL);
335 auditd_log_err("auditd_gen_record(AUE_audit_startup) %s: %m",
336 auditd_strerror(err));
338 if (auditd_config_controls() == 0)
339 auditd_log_info("Audit controls init successful");
341 auditd_log_err("Audit controls init failed");
346 * Close auditd pid file and trigger mechanism.
353 if (unlink(AUDITD_PIDFILE) == -1 && errno != ENOENT) {
354 auditd_log_err("Couldn't remove %s: %m", AUDITD_PIDFILE);
359 if (auditd_close_trigger() != 0) {
360 auditd_log_err("Error closing trigger messaging mechanism");
367 * Close all log files, control files, and tell the audit system.
373 char TS[TIMESTAMP_LEN + 1];
378 err = auditd_gen_record(AUE_audit_shutdown, NULL);
380 auditd_log_err("auditd_gen_record(AUE_audit_shutdown) %s: %m",
381 auditd_strerror(err));
383 /* Flush contents. */
385 err_ret = audit_set_cond(&cond);
387 auditd_log_err("Disabling audit failed! : %s", strerror(errno));
392 * Updated the cached state that auditing has been disabled.
394 auditd_set_state(AUD_STATE_DISABLED);
396 if (getTSstr(tt, TS, sizeof(TS)) == 0)
398 if (lastfile != NULL)
401 err_ret += close_misc();
404 auditd_log_err("Could not unregister");
405 audit_warn_postsigterm();
408 auditd_log_info("Finished");
413 * Register the daemon with the signal handler and the auditd pid file.
416 register_daemon(void)
422 /* Set up the signal hander. */
423 if (signal(SIGTERM, auditd_relay_signal) == SIG_ERR) {
425 "Could not set signal handler for SIGTERM");
428 if (signal(SIGCHLD, auditd_relay_signal) == SIG_ERR) {
430 "Could not set signal handler for SIGCHLD");
433 if (signal(SIGHUP, auditd_relay_signal) == SIG_ERR) {
435 "Could not set signal handler for SIGHUP");
438 if (signal(SIGALRM, auditd_relay_signal) == SIG_ERR) {
440 "Could not set signal handler for SIGALRM");
444 if ((pidfile = fopen(AUDITD_PIDFILE, "a")) == NULL) {
445 auditd_log_err("Could not open PID file");
446 audit_warn_tmpfile();
450 /* Attempt to lock the pid file; if a lock is present, exit. */
451 fd = fileno(pidfile);
452 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
454 "PID file is locked (is another auditd running?).");
461 if (fprintf(pidfile, "%u\n", pid) < 0) {
462 /* Should not start the daemon. */
471 * Handle the audit trigger event.
473 * We suppress (ignore) duplicated triggers in close succession in order to
474 * try to avoid thrashing-like behavior. However, not all triggers can be
475 * ignored, as triggers generally represent edge triggers, not level
476 * triggers, and won't be retransmitted if the condition persists. Of
477 * specific concern is the rotate trigger -- if one is dropped, then it will
478 * not be retransmitted, and the log file will grow in an unbounded fashion.
480 #define DUPLICATE_INTERVAL 30
482 auditd_handle_trigger(int trigger)
484 static int last_trigger, last_warning;
485 static time_t last_time;
493 * Suppress duplicate messages from the kernel within the specified
496 if (gettimeofday(&ts, &tzp) == 0) {
497 tt = (time_t)ts.tv_sec;
499 case AUDIT_TRIGGER_LOW_SPACE:
500 case AUDIT_TRIGGER_NO_SPACE:
502 * Triggers we can suppress. Of course, we also need
503 * to rate limit the warnings, so apply the same
504 * interval limit on syslog messages.
506 if ((trigger == last_trigger) &&
507 (tt < (last_time + DUPLICATE_INTERVAL))) {
508 if (tt >= (last_warning + DUPLICATE_INTERVAL))
510 "Suppressing duplicate trigger %d",
517 case AUDIT_TRIGGER_ROTATE_KERNEL:
518 case AUDIT_TRIGGER_ROTATE_USER:
519 case AUDIT_TRIGGER_READ_FILE:
520 case AUDIT_TRIGGER_CLOSE_AND_DIE:
521 case AUDIT_TRIGGER_INITIALIZE:
523 * Triggers that we cannot suppress.
529 * Only update last_trigger after aborting due to a duplicate
530 * trigger, not before, or we will never allow that trigger
533 last_trigger = trigger;
537 au_state = auditd_get_state();
540 * Message processing is done here.
543 case AUDIT_TRIGGER_LOW_SPACE:
544 auditd_log_notice("Got low space trigger");
545 if (do_trail_file() == -1)
546 auditd_log_err("Error swapping audit file");
549 case AUDIT_TRIGGER_NO_SPACE:
550 auditd_log_notice("Got no space trigger");
551 if (do_trail_file() == -1)
552 auditd_log_err("Error swapping audit file");
555 case AUDIT_TRIGGER_ROTATE_KERNEL:
556 case AUDIT_TRIGGER_ROTATE_USER:
557 auditd_log_info("Got open new trigger from %s", trigger ==
558 AUDIT_TRIGGER_ROTATE_KERNEL ? "kernel" : "user");
559 if (au_state == AUD_STATE_ENABLED && do_trail_file() == -1)
560 auditd_log_err("Error swapping audit file");
563 case AUDIT_TRIGGER_READ_FILE:
564 auditd_log_info("Got read file trigger");
565 if (au_state == AUD_STATE_ENABLED) {
566 if (auditd_config_controls() == -1)
567 auditd_log_err("Error setting audit controls");
568 else if (do_trail_file() == -1)
569 auditd_log_err("Error swapping audit file");
573 case AUDIT_TRIGGER_CLOSE_AND_DIE:
574 auditd_log_info("Got close and die trigger");
575 if (au_state == AUD_STATE_ENABLED)
578 * Running under launchd don't exit. Wait for launchd to
582 auditd_log_info("auditd exiting.");
587 case AUDIT_TRIGGER_INITIALIZE:
588 auditd_log_info("Got audit initialize trigger");
589 if (au_state == AUD_STATE_DISABLED)
593 case AUDIT_TRIGGER_EXPIRE_TRAILS:
594 auditd_log_info("Got audit expire trails trigger");
595 err = auditd_expire_trails(audit_warn_expired);
597 auditd_log_err("auditd_expire_trails(): %s",
598 auditd_strerror(err));
602 auditd_log_err("Got unknown trigger %d", trigger);
611 auditd_reap_children(void)
616 while ((child = waitpid(-1, &wstatus, WNOHANG)) > 0) {
619 auditd_log_info("warn process [pid=%d] %s %d.", child,
620 ((WIFEXITED(wstatus)) ? "exited with non-zero status" :
621 "exited as a result of signal"),
622 ((WIFEXITED(wstatus)) ? WEXITSTATUS(wstatus) :
628 * Reap any children and terminate. If under launchd don't shutdown auditing
629 * but just the other stuff.
632 auditd_terminate(void)
636 auditd_reap_children();
647 * Configure the audit controls in the kernel: the event to class mapping,
648 * kernel preselection mask, etc.
651 auditd_config_controls(void)
657 * Configure event to class mappings in kernel.
659 cnt = auditd_set_evcmap();
661 auditd_log_err("auditd_set_evcmap() failed: %m");
663 } else if (cnt == 0) {
664 auditd_log_err("No events to class mappings registered.");
667 auditd_log_debug("Registered %d event to class mappings.", cnt);
670 * Configure non-attributable event mask in kernel.
672 err = auditd_set_namask();
674 auditd_log_err("auditd_set_namask() %s: %m",
675 auditd_strerror(err));
678 auditd_log_debug("Registered non-attributable event mask.");
681 * Configure audit policy in kernel.
683 err = auditd_set_policy();
685 auditd_log_err("auditd_set_policy() %s: %m",
686 auditd_strerror(err));
689 auditd_log_debug("Set audit policy in kernel.");
692 * Configure audit trail log size in kernel.
694 err = auditd_set_fsize();
696 auditd_log_err("audit_set_fsize() %s: %m",
697 auditd_strerror(err));
700 auditd_log_debug("Set audit trail size in kernel.");
703 * Configure audit trail volume minimum free percentage of blocks in
706 err = auditd_set_minfree();
708 auditd_log_err("auditd_set_minfree() %s: %m",
709 auditd_strerror(err));
713 "Set audit trail min free percent in kernel.");
716 * Configure host address in the audit kernel information.
718 err = auditd_set_host();
720 if (err == ADE_PARSE) {
722 "audit_control(5) may be missing 'host:' field");
724 auditd_log_err("auditd_set_host() %s: %m",
725 auditd_strerror(err));
730 "Set audit host address information in kernel.");
736 * Setup and initialize auditd.
743 if (auditd_open_trigger(launchd_flag) < 0) {
744 auditd_log_err("Error opening trigger messaging mechanism");
749 * To prevent event feedback cycles and avoid auditd becoming
750 * stalled if auditing is suspended, auditd and its children run
751 * without their events being audited. We allow the uid, tid, and
752 * mask fields to be implicitly set to zero, but do set the pid. We
753 * run this after opening the trigger device to avoid configuring
754 * audit state without audit present in the system.
756 err = auditd_prevent_audit();
758 auditd_log_err("auditd_prevent_audit() %s: %m",
759 auditd_strerror(err));
764 * Make sure auditd auditing state is correct.
766 auditd_set_state(AUD_STATE_INIT);
769 * If under launchd, don't start auditing. Wait for a trigger to
777 main(int argc, char **argv)
781 #ifdef AUDIT_REVIEW_GROUP
785 while ((ch = getopt(argc, argv, "dl")) != -1) {
793 /* Be launchd friendly. */
799 (void)fprintf(stderr,
800 "usage: auditd [-d] [-l]\n");
805 audit_review_gid = getgid();
807 #ifdef AUDIT_REVIEW_GROUP
809 * XXXRW: Currently, this code falls back to the daemon gid, which is
810 * likely the wheel group. Is there a better way to deal with this?
812 grp = getgrnam(AUDIT_REVIEW_GROUP);
814 audit_review_gid = grp->gr_gid;
817 auditd_openlog(debug, audit_review_gid);
820 auditd_log_info("started by launchd...");
822 auditd_log_info("starting...");
824 #ifdef AUDIT_REVIEW_GROUP
827 "Audit review group '%s' not available, using daemon gid (%d)",
828 AUDIT_REVIEW_GROUP, audit_review_gid);
830 if (debug == 0 && launchd_flag == 0 && daemon(0, 0) == -1) {
831 auditd_log_err("Failed to daemonize");
835 if (register_daemon() == -1) {
836 auditd_log_err("Could not register as daemon");
843 * auditd_wait_for_events() shouldn't return unless something is wrong.
845 auditd_wait_for_events();
847 auditd_log_err("abnormal exit.");