dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libbsm / common / audit_allocate.c
blobdc14b9587ac82d39633fe25f2d94230a066fd60e
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.
25 * Copyright 2016 PALO, Richard.
28 #include <sys/types.h>
29 #include <bsm/audit.h>
30 #include <bsm/libbsm.h>
31 #include <bsm/audit_private.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <bsm/audit_uevents.h>
35 #include <generic.h>
36 #include <stdlib.h>
37 #include <alloca.h>
39 static int s_audit; /* successful audit event */
40 static int f_audit; /* failure audit event */
42 static int ad; /* audit descriptor */
44 void
45 audit_allocate_argv(int flg, int argc, char *argv[])
47 int i;
49 if (cannot_audit(0)) {
50 return;
53 switch (flg) {
54 case 0:
55 s_audit = AUE_allocate_succ;
56 f_audit = AUE_allocate_fail;
57 break;
58 case 1:
59 s_audit = AUE_deallocate_succ;
60 f_audit = AUE_deallocate_fail;
61 break;
62 case 2:
63 s_audit = AUE_listdevice_succ;
64 f_audit = AUE_listdevice_fail;
65 break;
68 ad = au_open();
70 for (i = 0; i < argc; i++)
71 (void) au_write(ad, au_to_text(argv[i]));
74 void
75 audit_allocate_device(char *path)
77 if (cannot_audit(0)) {
78 return;
80 (void) au_write(ad, au_to_path(path));
83 int
84 audit_allocate_record(int status) /* success failure of operation */
86 auditinfo_addr_t mask; /* audit ID */
87 au_event_t event; /* audit event number */
88 uint32_t policy; /* audit policy */
89 int ng; /* number of groups in process */
91 #ifdef DEBUG
92 (void) printf("audit_allocate_record(%d)\n", status);
93 #endif
95 if (cannot_audit(0)) {
96 return (0);
99 if (getaudit_addr(&mask, sizeof (mask)) < 0) {
100 if (!status)
101 return (1);
102 return (0);
105 if (auditon(A_GETPOLICY, (caddr_t)&policy, 0) < 0) {
106 if (!status)
107 return (1);
108 return (0);
112 /* determine if we're preselected */
113 if (status)
114 event = f_audit;
115 else
116 event = s_audit;
118 if (au_preselect(event, &mask.ai_mask, AU_PRS_BOTH, AU_PRS_REREAD) == 0)
119 return (0);
121 (void) au_write(ad, au_to_me()); /* add subject token */
122 if (policy & AUDIT_GROUP) { /* add optional group token */
123 gid_t *grplst;
124 int maxgrp = getgroups(0, NULL);
126 grplst = alloca(maxgrp * sizeof (gid_t));
128 if ((ng = getgroups(maxgrp, grplst)) < 0) {
129 (void) au_close(ad, 0, 0);
130 if (!status)
131 return (1);
132 return (0);
134 (void) au_write(ad, au_to_newgroups(ng, grplst));
137 if (status)
138 (void) au_write(ad, au_to_exit(status, -1));
139 else
140 (void) au_write(ad, au_to_exit(0, 0));
142 /* write audit record */
143 if (au_close(ad, 1, event) < 0) {
144 (void) au_close(ad, 0, 0);
145 if (!status)
146 return (1);
149 return (0);
152 void
153 audit_allocate_list(char *list)
155 char *buf;
156 char *file;
157 char *last;
159 if (cannot_audit(0)) {
160 return;
163 if ((buf = strdup(list)) == NULL)
164 return;
166 for (file = strtok_r(buf, " ", &last); file;
167 file = strtok_r(NULL, " ", &last))
168 (void) au_write(ad, au_to_path(file));
170 free(buf);