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)
418 struct sigaction action;
423 /* Set up the signal hander. */
424 action.sa_handler = auditd_relay_signal;
426 * sa_flags must not include SA_RESTART, so that read(2) will be
427 * interruptible in auditd_wait_for_events
430 sigemptyset(&action.sa_mask);
431 if (sigaction(SIGTERM, &action, NULL) != 0) {
433 "Could not set signal handler for SIGTERM");
436 if (sigaction(SIGCHLD, &action, NULL) != 0) {
438 "Could not set signal handler for SIGCHLD");
441 if (sigaction(SIGHUP, &action, NULL) != 0) {
443 "Could not set signal handler for SIGHUP");
446 if (sigaction(SIGALRM, &action, NULL) != 0) {
448 "Could not set signal handler for SIGALRM");
452 if ((pidfile = fopen(AUDITD_PIDFILE, "a")) == NULL) {
453 auditd_log_err("Could not open PID file");
454 audit_warn_tmpfile();
458 /* Attempt to lock the pid file; if a lock is present, exit. */
459 fd = fileno(pidfile);
460 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
462 "PID file is locked (is another auditd running?).");
469 if (fprintf(pidfile, "%u\n", pid) < 0) {
470 /* Should not start the daemon. */
479 * Handle the audit trigger event.
481 * We suppress (ignore) duplicated triggers in close succession in order to
482 * try to avoid thrashing-like behavior. However, not all triggers can be
483 * ignored, as triggers generally represent edge triggers, not level
484 * triggers, and won't be retransmitted if the condition persists. Of
485 * specific concern is the rotate trigger -- if one is dropped, then it will
486 * not be retransmitted, and the log file will grow in an unbounded fashion.
488 #define DUPLICATE_INTERVAL 30
490 auditd_handle_trigger(int trigger)
492 static int last_trigger, last_warning;
493 static time_t last_time;
501 * Suppress duplicate messages from the kernel within the specified
504 if (gettimeofday(&ts, &tzp) == 0) {
505 tt = (time_t)ts.tv_sec;
507 case AUDIT_TRIGGER_LOW_SPACE:
508 case AUDIT_TRIGGER_NO_SPACE:
510 * Triggers we can suppress. Of course, we also need
511 * to rate limit the warnings, so apply the same
512 * interval limit on syslog messages.
514 if ((trigger == last_trigger) &&
515 (tt < (last_time + DUPLICATE_INTERVAL))) {
516 if (tt >= (last_warning + DUPLICATE_INTERVAL))
518 "Suppressing duplicate trigger %d",
525 case AUDIT_TRIGGER_ROTATE_KERNEL:
526 case AUDIT_TRIGGER_ROTATE_USER:
527 case AUDIT_TRIGGER_READ_FILE:
528 case AUDIT_TRIGGER_CLOSE_AND_DIE:
529 case AUDIT_TRIGGER_INITIALIZE:
531 * Triggers that we cannot suppress.
537 * Only update last_trigger after aborting due to a duplicate
538 * trigger, not before, or we will never allow that trigger
541 last_trigger = trigger;
545 au_state = auditd_get_state();
548 * Message processing is done here.
551 case AUDIT_TRIGGER_LOW_SPACE:
552 auditd_log_notice("Got low space trigger");
553 if (do_trail_file() == -1)
554 auditd_log_err("Error swapping audit file");
557 case AUDIT_TRIGGER_NO_SPACE:
558 auditd_log_notice("Got no space trigger");
559 if (do_trail_file() == -1)
560 auditd_log_err("Error swapping audit file");
563 case AUDIT_TRIGGER_ROTATE_KERNEL:
564 case AUDIT_TRIGGER_ROTATE_USER:
565 auditd_log_info("Got open new trigger from %s", trigger ==
566 AUDIT_TRIGGER_ROTATE_KERNEL ? "kernel" : "user");
567 if (au_state == AUD_STATE_ENABLED && do_trail_file() == -1)
568 auditd_log_err("Error swapping audit file");
571 case AUDIT_TRIGGER_READ_FILE:
572 auditd_log_info("Got read file trigger");
573 if (au_state == AUD_STATE_ENABLED) {
574 if (auditd_config_controls() == -1)
575 auditd_log_err("Error setting audit controls");
576 else if (do_trail_file() == -1)
577 auditd_log_err("Error swapping audit file");
581 case AUDIT_TRIGGER_CLOSE_AND_DIE:
582 auditd_log_info("Got close and die trigger");
583 if (au_state == AUD_STATE_ENABLED)
586 * Running under launchd don't exit. Wait for launchd to
590 auditd_log_info("auditd exiting.");
595 case AUDIT_TRIGGER_INITIALIZE:
596 auditd_log_info("Got audit initialize trigger");
597 if (au_state == AUD_STATE_DISABLED)
601 case AUDIT_TRIGGER_EXPIRE_TRAILS:
602 auditd_log_info("Got audit expire trails trigger");
603 err = auditd_expire_trails(audit_warn_expired);
605 auditd_log_err("auditd_expire_trails(): %s",
606 auditd_strerror(err));
610 auditd_log_err("Got unknown trigger %d", trigger);
619 auditd_reap_children(void)
624 while ((child = waitpid(-1, &wstatus, WNOHANG)) > 0) {
627 auditd_log_info("warn process [pid=%d] %s %d.", child,
628 ((WIFEXITED(wstatus)) ? "exited with non-zero status" :
629 "exited as a result of signal"),
630 ((WIFEXITED(wstatus)) ? WEXITSTATUS(wstatus) :
636 * Reap any children and terminate. If under launchd don't shutdown auditing
637 * but just the other stuff.
640 auditd_terminate(void)
644 auditd_reap_children();
655 * Configure the audit controls in the kernel: the event to class mapping,
656 * kernel preselection mask, etc.
659 auditd_config_controls(void)
665 * Configure event to class mappings in kernel.
667 cnt = auditd_set_evcmap();
669 auditd_log_err("auditd_set_evcmap() failed: %m");
671 } else if (cnt == 0) {
672 auditd_log_err("No events to class mappings registered.");
675 auditd_log_debug("Registered %d event to class mappings.", cnt);
678 * Configure non-attributable event mask in kernel.
680 err = auditd_set_namask();
682 auditd_log_err("auditd_set_namask() %s: %m",
683 auditd_strerror(err));
686 auditd_log_debug("Registered non-attributable event mask.");
689 * Configure audit policy in kernel.
691 err = auditd_set_policy();
693 auditd_log_err("auditd_set_policy() %s: %m",
694 auditd_strerror(err));
697 auditd_log_debug("Set audit policy in kernel.");
700 * Configure audit trail log size in kernel.
702 err = auditd_set_fsize();
704 auditd_log_err("audit_set_fsize() %s: %m",
705 auditd_strerror(err));
708 auditd_log_debug("Set audit trail size in kernel.");
711 * Configure audit trail volume minimum free percentage of blocks in
714 err = auditd_set_minfree();
716 auditd_log_err("auditd_set_minfree() %s: %m",
717 auditd_strerror(err));
721 "Set audit trail min free percent in kernel.");
724 * Configure host address in the audit kernel information.
726 err = auditd_set_host();
728 if (err == ADE_PARSE) {
730 "audit_control(5) may be missing 'host:' field");
732 auditd_log_err("auditd_set_host() %s: %m",
733 auditd_strerror(err));
738 "Set audit host address information in kernel.");
744 * Setup and initialize auditd.
751 if (auditd_open_trigger(launchd_flag) < 0) {
752 auditd_log_err("Error opening trigger messaging mechanism");
757 * To prevent event feedback cycles and avoid auditd becoming
758 * stalled if auditing is suspended, auditd and its children run
759 * without their events being audited. We allow the uid, tid, and
760 * mask fields to be implicitly set to zero, but do set the pid. We
761 * run this after opening the trigger device to avoid configuring
762 * audit state without audit present in the system.
764 err = auditd_prevent_audit();
766 auditd_log_err("auditd_prevent_audit() %s: %m",
767 auditd_strerror(err));
772 * Make sure auditd auditing state is correct.
774 auditd_set_state(AUD_STATE_INIT);
777 * If under launchd, don't start auditing. Wait for a trigger to
785 main(int argc, char **argv)
789 #ifdef AUDIT_REVIEW_GROUP
793 while ((ch = getopt(argc, argv, "dl")) != -1) {
801 /* Be launchd friendly. */
807 (void)fprintf(stderr,
808 "usage: auditd [-d] [-l]\n");
813 audit_review_gid = getgid();
815 #ifdef AUDIT_REVIEW_GROUP
817 * XXXRW: Currently, this code falls back to the daemon gid, which is
818 * likely the wheel group. Is there a better way to deal with this?
820 grp = getgrnam(AUDIT_REVIEW_GROUP);
822 audit_review_gid = grp->gr_gid;
825 auditd_openlog(debug, audit_review_gid);
828 auditd_log_info("started by launchd...");
830 auditd_log_info("starting...");
832 #ifdef AUDIT_REVIEW_GROUP
835 "Audit review group '%s' not available, using daemon gid (%d)",
836 AUDIT_REVIEW_GROUP, audit_review_gid);
838 if (debug == 0 && launchd_flag == 0 && daemon(0, 0) == -1) {
839 auditd_log_err("Failed to daemonize");
843 if (register_daemon() == -1) {
844 auditd_log_err("Could not register as daemon");
851 * auditd_wait_for_events() shouldn't return unless something is wrong.
853 auditd_wait_for_events();
855 auditd_log_err("abnormal exit.");