xref: /illumos-gate/usr/src/lib/libbsm/common/audit_allocate.c (revision 168c213023b7f347f11abfc72f448b0c621ab718)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <tsol/label.h>
30 #include <bsm/audit.h>
31 #include <bsm/libbsm.h>
32 #include <bsm/audit_private.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <bsm/audit_uevents.h>
36 #include <generic.h>
37 
38 static int s_audit;	/* successful audit event */
39 static int f_audit;	/* failure audit event */
40 
41 static int ad;		/* audit descriptor */
42 
43 void
44 audit_allocate_argv(flg, argc, argv)
45 	int   flg;
46 	int   argc;
47 	char *argv[];
48 {
49 	int i;
50 
51 	if (cannot_audit(0)) {
52 		return;
53 	}
54 
55 	switch (flg) {
56 	case 0:
57 		s_audit = AUE_allocate_succ;
58 		f_audit = AUE_allocate_fail;
59 		break;
60 	case 1:
61 		s_audit = AUE_deallocate_succ;
62 		f_audit = AUE_deallocate_fail;
63 		break;
64 	case 2:
65 		s_audit = AUE_listdevice_succ;
66 		f_audit = AUE_listdevice_fail;
67 		break;
68 	}
69 
70 	ad = au_open();
71 
72 	for (i = 0; i < argc; i++)
73 		(void) au_write(ad, au_to_text(argv[i]));
74 }
75 
76 void
77 audit_allocate_device(path)
78 	char *path;
79 {
80 	if (cannot_audit(0)) {
81 		return;
82 	}
83 	(void) au_write(ad, au_to_path(path));
84 }
85 
86 int
87 audit_allocate_record(status)
88 	char	status;		/* success failure of operation */
89 {
90 	auditinfo_addr_t mask;		/* audit ID */
91 	au_event_t	event;		/* audit event number */
92 	int		policy;		/* audit policy */
93 	int		ng;		/* number of groups in process */
94 	gid_t		grplst[NGROUPS_UMAX];
95 
96 #ifdef DEBUG
97 	printf(("audit_allocate_record(%d)\n", status));
98 #endif
99 
100 	if (cannot_audit(0)) {
101 		return (0);
102 	}
103 
104 	if (getaudit_addr(&mask, sizeof (mask)) < 0) {
105 		if (!status)
106 			return (1);
107 		return (0);
108 	}
109 
110 	if (auditon(A_GETPOLICY, (caddr_t)&policy, 0) < 0) {
111 		if (!status)
112 			return (1);
113 		return (0);
114 	}
115 
116 
117 		/* determine if we're preselected */
118 	if (status)
119 		event = f_audit;
120 	else
121 		event = s_audit;
122 
123 	if (au_preselect(event, &mask.ai_mask, AU_PRS_BOTH, AU_PRS_REREAD)
124 		== NULL)
125 		return (0);
126 
127 	(void) au_write(ad, au_to_me());	/* add subject token */
128 	if (is_system_labeled())
129 		(void) au_write(ad, au_to_mylabel());
130 
131 	if (policy & AUDIT_GROUP) {	/* add optional group token */
132 		(void) memset(grplst, 0, sizeof (grplst));
133 		if ((ng = getgroups(NGROUPS_UMAX, grplst)) < 0) {
134 			(void) au_close(ad, 0, 0);
135 			if (!status)
136 				return (1);
137 			return (0);
138 		}
139 		(void) au_write(ad, au_to_newgroups(ng, grplst));
140 	}
141 
142 	if (status)
143 		(void) au_write(ad, au_to_exit(status, -1));
144 	else
145 		(void) au_write(ad, au_to_exit(0, 0));
146 
147 		/* write audit record */
148 	if (au_close(ad, 1, event) < 0) {
149 		(void) au_close(ad, 0, 0);
150 		if (!status)
151 			return (1);
152 	}
153 
154 	return (0);
155 }
156 
157 void
158 audit_allocate_list(list)
159 	char *list;
160 {
161 	char buf[1024];
162 	char *file;
163 	char *last;
164 
165 	if (cannot_audit(0)) {
166 		return;
167 	}
168 
169 	(void) strcpy(buf, list);
170 
171 	for (file = strtok_r(buf, " ", &last); file;
172 	    file = strtok_r(NULL, " ", &last))
173 		(void) au_write(ad, au_to_path(file));
174 }
175