17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 558091fd8Ssetje * Common Development and Distribution License (the "License"). 658091fd8Ssetje * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22e333042dSgww * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 326dcd8691Sgww #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 366dcd8691Sgww #include <string.h> 376dcd8691Sgww #include <strings.h> 387c478bd9Sstevel@tonic-gate #include <signal.h> 396dcd8691Sgww #include <unistd.h> 406dcd8691Sgww 416a3b10dbStz204579 #include <bsm/adt.h> 426a3b10dbStz204579 #include <bsm/adt_event.h> 436dcd8691Sgww 44*b08d8a12Sgww #include <sys/types.h> 456dcd8691Sgww #include <sys/uadmin.h> 46*b08d8a12Sgww #include <sys/wait.h> 477c478bd9Sstevel@tonic-gate 4858091fd8Ssetje #define SMF_RST "/etc/svc/volatile/resetting" 49*b08d8a12Sgww #define RETRY_COUNT 15 /* number of 1 sec retries for audit(1M) to complete */ 5058091fd8Ssetje 517c478bd9Sstevel@tonic-gate static const char *Usage = "Usage: %s cmd fcn [mdep]\n"; 527c478bd9Sstevel@tonic-gate 53*b08d8a12Sgww static int closeout_audit(int, int); 54*b08d8a12Sgww static int turnoff_auditd(void); 556a3b10dbStz204579 static void wait_for_auqueue(); 56*b08d8a12Sgww static int change_audit_file(void); 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate int 597c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate int cmd, fcn; 627c478bd9Sstevel@tonic-gate uintptr_t mdep = NULL; 637c478bd9Sstevel@tonic-gate sigset_t set; 646a3b10dbStz204579 adt_session_data_t *ah; /* audit session handle */ 656a3b10dbStz204579 adt_event_data_t *event = NULL; /* event to be generated */ 666a3b10dbStz204579 au_event_t event_id; 676a3b10dbStz204579 enum adt_uadmin_fcn fcn_id; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate if (argc < 3 || argc > 4) { 707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Usage, argv[0]); 717c478bd9Sstevel@tonic-gate return (1); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate (void) sigfillset(&set); 757c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &set, NULL); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate cmd = atoi(argv[1]); 787c478bd9Sstevel@tonic-gate fcn = atoi(argv[2]); 797c478bd9Sstevel@tonic-gate if (argc == 4) { /* mdep argument given */ 802df1fe9cSrandyf if (cmd != A_REBOOT && cmd != A_SHUTDOWN && cmd != A_DUMP && 812df1fe9cSrandyf cmd != A_FREEZE) { 827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: mdep argument not " 837c478bd9Sstevel@tonic-gate "allowed for this cmd value\n", argv[0]); 847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Usage, argv[0]); 857c478bd9Sstevel@tonic-gate return (1); 867c478bd9Sstevel@tonic-gate } else { 877c478bd9Sstevel@tonic-gate mdep = (uintptr_t)argv[3]; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 916a3b10dbStz204579 /* set up audit session and event */ 926a3b10dbStz204579 if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) { 936a3b10dbStz204579 (void) fprintf(stderr, "%s: can't start audit session\n", 946a3b10dbStz204579 argv[0]); 956a3b10dbStz204579 } 966a3b10dbStz204579 switch (cmd) { 976a3b10dbStz204579 case A_SHUTDOWN: 986a3b10dbStz204579 event_id = ADT_uadmin_shutdown; 996a3b10dbStz204579 break; 1006a3b10dbStz204579 case A_REBOOT: 1016a3b10dbStz204579 event_id = ADT_uadmin_reboot; 1026a3b10dbStz204579 break; 1036a3b10dbStz204579 case A_DUMP: 1046a3b10dbStz204579 event_id = ADT_uadmin_dump; 1056a3b10dbStz204579 break; 1066a3b10dbStz204579 case A_REMOUNT: 1076a3b10dbStz204579 event_id = ADT_uadmin_remount; 1086a3b10dbStz204579 break; 1096a3b10dbStz204579 case A_FREEZE: 1106a3b10dbStz204579 event_id = ADT_uadmin_freeze; 1116a3b10dbStz204579 break; 1126a3b10dbStz204579 case A_FTRACE: 1136a3b10dbStz204579 event_id = ADT_uadmin_ftrace; 1146a3b10dbStz204579 break; 1156a3b10dbStz204579 case A_SWAPCTL: 1166a3b10dbStz204579 event_id = ADT_uadmin_swapctl; 1176a3b10dbStz204579 break; 1186a3b10dbStz204579 default: 1196a3b10dbStz204579 event_id = 0; 1206a3b10dbStz204579 } 1216a3b10dbStz204579 if ((event_id != 0) && 1226a3b10dbStz204579 (event = adt_alloc_event(ah, event_id)) == NULL) { 1236a3b10dbStz204579 (void) fprintf(stderr, "%s: can't allocate audit event\n", 1246a3b10dbStz204579 argv[0]); 1256a3b10dbStz204579 } 1266a3b10dbStz204579 switch (fcn) { 1276a3b10dbStz204579 case AD_HALT: 1286a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_HALT; 1296a3b10dbStz204579 break; 1306a3b10dbStz204579 case AD_POWEROFF: 1316a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_POWEROFF; 1326a3b10dbStz204579 break; 1336a3b10dbStz204579 case AD_BOOT: 1346a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_BOOT; 1356a3b10dbStz204579 break; 1366a3b10dbStz204579 case AD_IBOOT: 1376a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_IBOOT; 1386a3b10dbStz204579 break; 1396a3b10dbStz204579 case AD_SBOOT: 1406a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_SBOOT; 1416a3b10dbStz204579 break; 1426a3b10dbStz204579 case AD_SIBOOT: 1436a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_SIBOOT; 1446a3b10dbStz204579 break; 1456a3b10dbStz204579 case AD_NOSYNC: 1466a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_NOSYNC; 1476a3b10dbStz204579 break; 1486a3b10dbStz204579 default: 1496a3b10dbStz204579 fcn_id = 0; 1506a3b10dbStz204579 } 1516a3b10dbStz204579 if (cmd == A_FREEZE) { 1526a3b10dbStz204579 switch (fcn) { 1536a3b10dbStz204579 case AD_SUSPEND_TO_DISK: 1546a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_DISK; 1556a3b10dbStz204579 break; 1566a3b10dbStz204579 case AD_CHECK_SUSPEND_TO_DISK: 1576a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_DISK; 1586a3b10dbStz204579 break; 1596a3b10dbStz204579 case AD_FORCE: 1606a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_FORCE; 1616a3b10dbStz204579 break; 1626a3b10dbStz204579 case AD_SUSPEND_TO_RAM: 1636a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_RAM; 1646a3b10dbStz204579 break; 1656a3b10dbStz204579 case AD_CHECK_SUSPEND_TO_RAM: 1666a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_RAM; 1676a3b10dbStz204579 break; 1686a3b10dbStz204579 case AD_REUSEINIT: 1696a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_REUSEINIT; 1706a3b10dbStz204579 break; 1716a3b10dbStz204579 case AD_REUSABLE: 1726a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_REUSABLE; 1736a3b10dbStz204579 break; 1746a3b10dbStz204579 case AD_REUSEFINI: 1756a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_REUSEFINI; 1766a3b10dbStz204579 break; 1776a3b10dbStz204579 } 1786a3b10dbStz204579 } else if (cmd == A_FTRACE) { 1796a3b10dbStz204579 switch (fcn) { 1806a3b10dbStz204579 case AD_FTRACE_START: 1816a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_FTRACE_START; 1826a3b10dbStz204579 break; 1836a3b10dbStz204579 case AD_FTRACE_STOP: 1846a3b10dbStz204579 fcn_id = ADT_UADMIN_FCN_AD_FTRACE_STOP; 1856a3b10dbStz204579 break; 1866a3b10dbStz204579 } 1876a3b10dbStz204579 } 1886a3b10dbStz204579 1897c478bd9Sstevel@tonic-gate if (geteuid() == 0) { 1906a3b10dbStz204579 if (event != NULL) { 1916a3b10dbStz204579 switch (cmd) { 1926a3b10dbStz204579 case A_SHUTDOWN: 1936a3b10dbStz204579 event->adt_uadmin_shutdown.fcn = fcn_id; 1946a3b10dbStz204579 event->adt_uadmin_shutdown.mdep = (char *)mdep; 1956a3b10dbStz204579 break; 1966a3b10dbStz204579 case A_REBOOT: 1976a3b10dbStz204579 event->adt_uadmin_reboot.fcn = fcn_id; 1986a3b10dbStz204579 event->adt_uadmin_reboot.mdep = (char *)mdep; 1996a3b10dbStz204579 break; 2006a3b10dbStz204579 case A_DUMP: 2016a3b10dbStz204579 event->adt_uadmin_dump.fcn = fcn_id; 2026a3b10dbStz204579 event->adt_uadmin_dump.mdep = (char *)mdep; 2036a3b10dbStz204579 break; 2046a3b10dbStz204579 case A_REMOUNT: 2056a3b10dbStz204579 /* no parameters */ 2066a3b10dbStz204579 break; 2076a3b10dbStz204579 case A_FREEZE: 2086a3b10dbStz204579 event->adt_uadmin_freeze.fcn = fcn_id; 2096a3b10dbStz204579 event->adt_uadmin_freeze.mdep = (char *)mdep; 2106a3b10dbStz204579 break; 2116a3b10dbStz204579 case A_FTRACE: 2126a3b10dbStz204579 event->adt_uadmin_ftrace.fcn = fcn_id; 2136a3b10dbStz204579 break; 2146a3b10dbStz204579 case A_SWAPCTL: 2156a3b10dbStz204579 event->adt_uadmin_swapctl.fcn = fcn_id; 2166a3b10dbStz204579 break; 2176a3b10dbStz204579 } 2186a3b10dbStz204579 2196a3b10dbStz204579 if (adt_put_event(event, ADT_SUCCESS, 0) != 0) { 2206a3b10dbStz204579 (void) fprintf(stderr, 2216a3b10dbStz204579 "%s: can't put audit event\n", argv[0]); 2226a3b10dbStz204579 } 2236a3b10dbStz204579 /* 2246a3b10dbStz204579 * allow audit record to be processed in the kernel 2256a3b10dbStz204579 * audit queue 2266a3b10dbStz204579 */ 2276a3b10dbStz204579 wait_for_auqueue(); 2286a3b10dbStz204579 } 2296a3b10dbStz204579 230*b08d8a12Sgww if (closeout_audit(cmd, fcn) == -1) 2317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: can't turn off auditd\n", 2327c478bd9Sstevel@tonic-gate argv[0]); 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (cmd == A_SHUTDOWN || cmd == A_REBOOT) 23558091fd8Ssetje (void) creat(SMF_RST, 0777); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2386a3b10dbStz204579 (void) adt_free_event(event); 2397c478bd9Sstevel@tonic-gate if (uadmin(cmd, fcn, mdep) < 0) { 2407c478bd9Sstevel@tonic-gate perror("uadmin"); 2417c478bd9Sstevel@tonic-gate 24258091fd8Ssetje (void) unlink(SMF_RST); 24358091fd8Ssetje 2447c478bd9Sstevel@tonic-gate return (1); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 247*b08d8a12Sgww /* If returning from a suspend, audit thaw */ 248*b08d8a12Sgww if ((cmd == A_FREEZE) && 249*b08d8a12Sgww ((fcn == AD_FORCE) || 250*b08d8a12Sgww (fcn == AD_REUSABLE) || 251*b08d8a12Sgww (fcn == AD_SUSPEND_TO_DISK) || 252*b08d8a12Sgww (fcn == AD_SUSPEND_TO_RAM))) { 253*b08d8a12Sgww if ((event = adt_alloc_event(ah, ADT_uadmin_thaw)) == NULL) { 254*b08d8a12Sgww (void) fprintf(stderr, "%s: can't allocate thaw audit " 255*b08d8a12Sgww "event\n", argv[0]); 256*b08d8a12Sgww } 257*b08d8a12Sgww event->adt_uadmin_thaw.fcn = fcn_id; 258*b08d8a12Sgww if (adt_put_event(event, ADT_SUCCESS, 0) != 0) { 259*b08d8a12Sgww (void) fprintf(stderr, "%s: can't put thaw audit " 260*b08d8a12Sgww "event\n", argv[0]); 261*b08d8a12Sgww } 262*b08d8a12Sgww (void) adt_free_event(event); 263*b08d8a12Sgww } 264*b08d8a12Sgww (void) adt_end_session(ah); 265*b08d8a12Sgww 2667c478bd9Sstevel@tonic-gate return (0); 2677c478bd9Sstevel@tonic-gate } 2686a3b10dbStz204579 2696a3b10dbStz204579 static int 270*b08d8a12Sgww closeout_audit(int cmd, int fcn) 2716a3b10dbStz204579 { 272*b08d8a12Sgww if (!adt_audit_state(AUC_AUDITING)) { 273*b08d8a12Sgww /* auditd not running, just return */ 274*b08d8a12Sgww return (0); 275*b08d8a12Sgww } 276e333042dSgww switch (cmd) { 277e333042dSgww case A_SHUTDOWN: 278e333042dSgww case A_REBOOT: 279e333042dSgww case A_DUMP: 280e333042dSgww /* system shutting down, turn off auditd */ 281*b08d8a12Sgww return (turnoff_auditd()); 282e333042dSgww case A_REMOUNT: 283e333042dSgww case A_SWAPCTL: 284e333042dSgww case A_FTRACE: 285e333042dSgww /* No system discontinuity, don't turn off auditd */ 286e333042dSgww return (0); 287e333042dSgww case A_FREEZE: 288e333042dSgww switch (fcn) { 289e333042dSgww case AD_CHECK_SUSPEND_TO_DISK: /* AD_CHECK */ 290e333042dSgww case AD_CHECK_SUSPEND_TO_RAM: 291e333042dSgww case AD_REUSEINIT: 292e333042dSgww case AD_REUSEFINI: 293e333042dSgww /* No system discontinuity, don't turn off auditd */ 294e333042dSgww return (0); 295e333042dSgww case AD_REUSABLE: 296e333042dSgww case AD_SUSPEND_TO_DISK: /* AD_COMPRESS */ 297e333042dSgww case AD_SUSPEND_TO_RAM: 298e333042dSgww case AD_FORCE: 299e333042dSgww /* suspend the system, change audit files */ 300*b08d8a12Sgww return (change_audit_file()); 301e333042dSgww default: 302*b08d8a12Sgww return (0); /* not an audit error */ 303e333042dSgww } 304e333042dSgww default: 305*b08d8a12Sgww return (0); /* not an audit error */ 306*b08d8a12Sgww } 307e333042dSgww } 308e333042dSgww 309*b08d8a12Sgww static int 310*b08d8a12Sgww turnoff_auditd(void) 311*b08d8a12Sgww { 312*b08d8a12Sgww int rc; 313*b08d8a12Sgww int retries = RETRY_COUNT; 314*b08d8a12Sgww 315*b08d8a12Sgww if ((rc = (int)fork()) == 0) { 3166dcd8691Sgww (void) execl("/usr/sbin/audit", "audit", "-t", NULL); 3176a3b10dbStz204579 (void) fprintf(stderr, "error disabling auditd: %s\n", 3186dcd8691Sgww strerror(errno)); 3196dcd8691Sgww _exit(-1); 3206dcd8691Sgww } else if (rc == -1) { 3216dcd8691Sgww (void) fprintf(stderr, "error disabling auditd: %s\n", 3226dcd8691Sgww strerror(errno)); 3236a3b10dbStz204579 return (-1); 3246a3b10dbStz204579 } 3256a3b10dbStz204579 3266dcd8691Sgww /* 3276dcd8691Sgww * wait for auditd to finish its work. auditd will change the 3286dcd8691Sgww * auditstart from AUC_AUDITING (auditd up and running) to 3296dcd8691Sgww * AUC_NOAUDIT. Other states are errors, so we're done as well. 3306dcd8691Sgww */ 3316a3b10dbStz204579 do { 3326dcd8691Sgww int auditstate; 3336a3b10dbStz204579 3346dcd8691Sgww rc = -1; 3356dcd8691Sgww if ((auditon(A_GETCOND, (caddr_t)&auditstate, 3366dcd8691Sgww sizeof (auditstate)) == 0) && 3376dcd8691Sgww (auditstate == AUC_AUDITING)) { 3386a3b10dbStz204579 retries--; 3396a3b10dbStz204579 (void) sleep(1); 3406a3b10dbStz204579 } else { 3416a3b10dbStz204579 rc = 0; 3426a3b10dbStz204579 } 3436dcd8691Sgww } while ((rc != 0) && (retries != 0)); 3446a3b10dbStz204579 3456a3b10dbStz204579 return (rc); 3466a3b10dbStz204579 } 3476a3b10dbStz204579 348*b08d8a12Sgww static int 349*b08d8a12Sgww change_audit_file(void) 350*b08d8a12Sgww { 351*b08d8a12Sgww pid_t pid; 352*b08d8a12Sgww 353*b08d8a12Sgww if ((pid = fork()) == 0) { 354*b08d8a12Sgww (void) execl("/usr/sbin/audit", "audit", "-n", NULL); 355*b08d8a12Sgww (void) fprintf(stderr, "error changing audit files: %s\n", 356*b08d8a12Sgww strerror(errno)); 357*b08d8a12Sgww _exit(-1); 358*b08d8a12Sgww } else if (pid == -1) { 359*b08d8a12Sgww (void) fprintf(stderr, "error changing audit files: %s\n", 360*b08d8a12Sgww strerror(errno)); 361*b08d8a12Sgww return (-1); 362*b08d8a12Sgww } else { 363*b08d8a12Sgww pid_t rc; 364*b08d8a12Sgww int retries = RETRY_COUNT; 365*b08d8a12Sgww 366*b08d8a12Sgww /* 367*b08d8a12Sgww * Wait for audit(1M) -n process to complete 368*b08d8a12Sgww * 369*b08d8a12Sgww */ 370*b08d8a12Sgww do { 371*b08d8a12Sgww if ((rc = waitpid(pid, NULL, WNOHANG)) == pid) { 372*b08d8a12Sgww return (0); 373*b08d8a12Sgww } else if (rc == -1) { 374*b08d8a12Sgww return (-1); 375*b08d8a12Sgww } else { 376*b08d8a12Sgww (void) sleep(1); 377*b08d8a12Sgww retries--; 378*b08d8a12Sgww } 379*b08d8a12Sgww 380*b08d8a12Sgww } while (retries != 0); 381*b08d8a12Sgww } 382*b08d8a12Sgww return (-1); 383*b08d8a12Sgww } 384*b08d8a12Sgww 3856a3b10dbStz204579 static void 3866a3b10dbStz204579 wait_for_auqueue() 3876a3b10dbStz204579 { 3886a3b10dbStz204579 au_stat_t au_stat; 3896a3b10dbStz204579 int retries = 10; 3906a3b10dbStz204579 3916a3b10dbStz204579 while (retries-- && auditon(A_GETSTAT, (caddr_t)&au_stat, NULL) == 0) { 3926a3b10dbStz204579 if (au_stat.as_enqueue == au_stat.as_written) { 3936a3b10dbStz204579 break; 3946a3b10dbStz204579 } 3956a3b10dbStz204579 (void) sleep(1); 3966a3b10dbStz204579 } 3976a3b10dbStz204579 } 398