kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / lib / libbsm / common / audit_allocate.c
blob0a7d8821c7af55d143f04a738c42904702a95e43
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <bsm/audit.h>
28 #include <bsm/libbsm.h>
29 #include <bsm/audit_private.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <bsm/audit_uevents.h>
33 #include <generic.h>
34 #include <stdlib.h>
35 #include <alloca.h>
37 static int s_audit; /* successful audit event */
38 static int f_audit; /* failure audit event */
40 static int ad; /* audit descriptor */
42 void
43 audit_allocate_argv(flg, argc, argv)
44 int flg;
45 int argc;
46 char *argv[];
48 int i;
50 if (cannot_audit(0)) {
51 return;
54 switch (flg) {
55 case 0:
56 s_audit = AUE_allocate_succ;
57 f_audit = AUE_allocate_fail;
58 break;
59 case 1:
60 s_audit = AUE_deallocate_succ;
61 f_audit = AUE_deallocate_fail;
62 break;
63 case 2:
64 s_audit = AUE_listdevice_succ;
65 f_audit = AUE_listdevice_fail;
66 break;
69 ad = au_open();
71 for (i = 0; i < argc; i++)
72 (void) au_write(ad, au_to_text(argv[i]));
75 void
76 audit_allocate_device(path)
77 char *path;
79 if (cannot_audit(0)) {
80 return;
82 (void) au_write(ad, au_to_path(path));
85 int
86 audit_allocate_record(status)
87 char status; /* success failure of operation */
89 auditinfo_addr_t mask; /* audit ID */
90 au_event_t event; /* audit event number */
91 uint32_t policy; /* audit policy */
92 int ng; /* number of groups in process */
94 #ifdef DEBUG
95 (void) printf("audit_allocate_record(%d)\n", status);
96 #endif
98 if (cannot_audit(0)) {
99 return (0);
102 if (getaudit_addr(&mask, sizeof (mask)) < 0) {
103 if (!status)
104 return (1);
105 return (0);
108 if (auditon(A_GETPOLICY, (caddr_t)&policy, 0) < 0) {
109 if (!status)
110 return (1);
111 return (0);
115 /* determine if we're preselected */
116 if (status)
117 event = f_audit;
118 else
119 event = s_audit;
121 if (au_preselect(event, &mask.ai_mask, AU_PRS_BOTH, AU_PRS_REREAD)
122 == NULL)
123 return (0);
125 (void) au_write(ad, au_to_me()); /* add subject token */
126 if (policy & AUDIT_GROUP) { /* add optional group token */
127 gid_t *grplst;
128 int maxgrp = getgroups(0, NULL);
130 grplst = alloca(maxgrp * sizeof (gid_t));
132 if ((ng = getgroups(maxgrp, grplst)) < 0) {
133 (void) au_close(ad, 0, 0);
134 if (!status)
135 return (1);
136 return (0);
138 (void) au_write(ad, au_to_newgroups(ng, grplst));
141 if (status)
142 (void) au_write(ad, au_to_exit(status, -1));
143 else
144 (void) au_write(ad, au_to_exit(0, 0));
146 /* write audit record */
147 if (au_close(ad, 1, event) < 0) {
148 (void) au_close(ad, 0, 0);
149 if (!status)
150 return (1);
153 return (0);
156 void
157 audit_allocate_list(list)
158 char *list;
160 char *buf;
161 char *file;
162 char *last;
164 if (cannot_audit(0)) {
165 return;
168 if ((buf = strdup(list)) == NULL)
169 return;
171 for (file = strtok_r(buf, " ", &last); file;
172 file = strtok_r(NULL, " ", &last))
173 (void) au_write(ad, au_to_path(file));
175 free(buf);