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
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * 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 /*
22dfc7be02SJan Friedel * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
2745916cd2Sjpk #include <tsol/label.h>
287c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
297c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
307c478bd9Sstevel@tonic-gate #include <bsm/audit_private.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
347c478bd9Sstevel@tonic-gate #include <generic.h>
35583b61f6Saj #include <stdlib.h>
3667dbe2beSCasper H.S. Dik #include <alloca.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static int s_audit; /* successful audit event */
397c478bd9Sstevel@tonic-gate static int f_audit; /* failure audit event */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static int ad; /* audit descriptor */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate void
audit_allocate_argv(flg,argc,argv)447c478bd9Sstevel@tonic-gate audit_allocate_argv(flg, argc, argv)
457c478bd9Sstevel@tonic-gate int flg;
467c478bd9Sstevel@tonic-gate int argc;
477c478bd9Sstevel@tonic-gate char *argv[];
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate int i;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (cannot_audit(0)) {
527c478bd9Sstevel@tonic-gate return;
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate switch (flg) {
567c478bd9Sstevel@tonic-gate case 0:
577c478bd9Sstevel@tonic-gate s_audit = AUE_allocate_succ;
587c478bd9Sstevel@tonic-gate f_audit = AUE_allocate_fail;
597c478bd9Sstevel@tonic-gate break;
607c478bd9Sstevel@tonic-gate case 1:
617c478bd9Sstevel@tonic-gate s_audit = AUE_deallocate_succ;
627c478bd9Sstevel@tonic-gate f_audit = AUE_deallocate_fail;
637c478bd9Sstevel@tonic-gate break;
647c478bd9Sstevel@tonic-gate case 2:
657c478bd9Sstevel@tonic-gate s_audit = AUE_listdevice_succ;
667c478bd9Sstevel@tonic-gate f_audit = AUE_listdevice_fail;
677c478bd9Sstevel@tonic-gate break;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate ad = au_open();
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++)
737c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_text(argv[i]));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate void
audit_allocate_device(path)777c478bd9Sstevel@tonic-gate audit_allocate_device(path)
787c478bd9Sstevel@tonic-gate char *path;
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate if (cannot_audit(0)) {
817c478bd9Sstevel@tonic-gate return;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_path(path));
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate int
audit_allocate_record(status)877c478bd9Sstevel@tonic-gate audit_allocate_record(status)
887c478bd9Sstevel@tonic-gate char status; /* success failure of operation */
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate auditinfo_addr_t mask; /* audit ID */
917c478bd9Sstevel@tonic-gate au_event_t event; /* audit event number */
92*96093503SMarek Pospisil uint32_t policy; /* audit policy */
937c478bd9Sstevel@tonic-gate int ng; /* number of groups in process */
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate #ifdef DEBUG
96dfc7be02SJan Friedel (void) printf("audit_allocate_record(%d)\n", status);
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate if (cannot_audit(0)) {
1007c478bd9Sstevel@tonic-gate return (0);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (getaudit_addr(&mask, sizeof (mask)) < 0) {
1047c478bd9Sstevel@tonic-gate if (!status)
1057c478bd9Sstevel@tonic-gate return (1);
1067c478bd9Sstevel@tonic-gate return (0);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (auditon(A_GETPOLICY, (caddr_t)&policy, 0) < 0) {
1107c478bd9Sstevel@tonic-gate if (!status)
1117c478bd9Sstevel@tonic-gate return (1);
1127c478bd9Sstevel@tonic-gate return (0);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* determine if we're preselected */
1177c478bd9Sstevel@tonic-gate if (status)
1187c478bd9Sstevel@tonic-gate event = f_audit;
1197c478bd9Sstevel@tonic-gate else
1207c478bd9Sstevel@tonic-gate event = s_audit;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if (au_preselect(event, &mask.ai_mask, AU_PRS_BOTH, AU_PRS_REREAD)
1237c478bd9Sstevel@tonic-gate == NULL)
1247c478bd9Sstevel@tonic-gate return (0);
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_me()); /* add subject token */
12781490fd2Sgww if (is_system_labeled())
12881490fd2Sgww (void) au_write(ad, au_to_mylabel());
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (policy & AUDIT_GROUP) { /* add optional group token */
13167dbe2beSCasper H.S. Dik gid_t *grplst;
13267dbe2beSCasper H.S. Dik int maxgrp = getgroups(0, NULL);
13367dbe2beSCasper H.S. Dik
13467dbe2beSCasper H.S. Dik grplst = alloca(maxgrp * sizeof (gid_t));
13567dbe2beSCasper H.S. Dik
13667dbe2beSCasper H.S. Dik if ((ng = getgroups(maxgrp, grplst)) < 0) {
1377c478bd9Sstevel@tonic-gate (void) au_close(ad, 0, 0);
1387c478bd9Sstevel@tonic-gate if (!status)
1397c478bd9Sstevel@tonic-gate return (1);
1407c478bd9Sstevel@tonic-gate return (0);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_newgroups(ng, grplst));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (status)
1467c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_exit(status, -1));
1477c478bd9Sstevel@tonic-gate else
1487c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_exit(0, 0));
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* write audit record */
1517c478bd9Sstevel@tonic-gate if (au_close(ad, 1, event) < 0) {
1527c478bd9Sstevel@tonic-gate (void) au_close(ad, 0, 0);
1537c478bd9Sstevel@tonic-gate if (!status)
1547c478bd9Sstevel@tonic-gate return (1);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate void
audit_allocate_list(list)1617c478bd9Sstevel@tonic-gate audit_allocate_list(list)
1627c478bd9Sstevel@tonic-gate char *list;
1637c478bd9Sstevel@tonic-gate {
164583b61f6Saj char *buf;
1657c478bd9Sstevel@tonic-gate char *file;
1667c478bd9Sstevel@tonic-gate char *last;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (cannot_audit(0)) {
1697c478bd9Sstevel@tonic-gate return;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
172583b61f6Saj if ((buf = strdup(list)) == NULL)
173583b61f6Saj return;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate for (file = strtok_r(buf, " ", &last); file;
1767c478bd9Sstevel@tonic-gate file = strtok_r(NULL, " ", &last))
1777c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_path(file));
178583b61f6Saj
179583b61f6Saj free(buf);
1807c478bd9Sstevel@tonic-gate }
181