xref: /freebsd/contrib/openbsm/libbsm/bsm_user.c (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
152267f74SRobert Watson /*-
252267f74SRobert Watson  * Copyright (c) 2004 Apple Inc.
3ca0716f5SRobert Watson  * Copyright (c) 2006 Robert N. M. Watson
4ca0716f5SRobert Watson  * All rights reserved.
5ca0716f5SRobert Watson  *
6ca0716f5SRobert Watson  * Redistribution and use in source and binary forms, with or without
7ca0716f5SRobert Watson  * modification, are permitted provided that the following conditions
8ca0716f5SRobert Watson  * are met:
9ca0716f5SRobert Watson  * 1.  Redistributions of source code must retain the above copyright
10ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer.
11ca0716f5SRobert Watson  * 2.  Redistributions in binary form must reproduce the above copyright
12ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer in the
13ca0716f5SRobert Watson  *     documentation and/or other materials provided with the distribution.
1452267f74SRobert Watson  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15ca0716f5SRobert Watson  *     its contributors may be used to endorse or promote products derived
16ca0716f5SRobert Watson  *     from this software without specific prior written permission.
17ca0716f5SRobert Watson  *
18ca0716f5SRobert Watson  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19ca0716f5SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ca0716f5SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ca0716f5SRobert Watson  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22ca0716f5SRobert Watson  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ca0716f5SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ca0716f5SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ca0716f5SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26ca0716f5SRobert Watson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27ca0716f5SRobert Watson  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28ca0716f5SRobert Watson  * POSSIBILITY OF SUCH DAMAGE.
29ca0716f5SRobert Watson  */
30ca0716f5SRobert Watson 
3152267f74SRobert Watson #include <config/config.h>
3252267f74SRobert Watson 
33ca0716f5SRobert Watson #include <bsm/libbsm.h>
34ca0716f5SRobert Watson 
35ca0716f5SRobert Watson #include <string.h>
367a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
37ca0716f5SRobert Watson #include <pthread.h>
387a0a89d2SRobert Watson #endif
39ca0716f5SRobert Watson #include <stdio.h>
40ca0716f5SRobert Watson #include <stdlib.h>
41ca0716f5SRobert Watson 
4252267f74SRobert Watson #ifndef HAVE_STRLCPY
4352267f74SRobert Watson #include <compat/strlcpy.h>
4452267f74SRobert Watson #endif
4552267f74SRobert Watson 
46ca0716f5SRobert Watson /*
47ca0716f5SRobert Watson  * Parse the contents of the audit_user file into au_user_ent structures.
48ca0716f5SRobert Watson  */
49ca0716f5SRobert Watson 
50ca0716f5SRobert Watson static FILE		*fp = NULL;
51ca0716f5SRobert Watson static char		 linestr[AU_LINE_MAX];
52ca0716f5SRobert Watson static const char	*user_delim = ":";
53ca0716f5SRobert Watson 
547a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
55ca0716f5SRobert Watson static pthread_mutex_t	mutex = PTHREAD_MUTEX_INITIALIZER;
567a0a89d2SRobert Watson #endif
57ca0716f5SRobert Watson 
58ca0716f5SRobert Watson /*
59ca0716f5SRobert Watson  * Parse one line from the audit_user file into the au_user_ent structure.
60ca0716f5SRobert Watson  */
61ca0716f5SRobert Watson static struct au_user_ent *
userfromstr(char * str,struct au_user_ent * u)62ca0716f5SRobert Watson userfromstr(char *str, struct au_user_ent *u)
63ca0716f5SRobert Watson {
64ca0716f5SRobert Watson 	char *username, *always, *never;
65ca0716f5SRobert Watson 	char *last;
66ca0716f5SRobert Watson 
67ca0716f5SRobert Watson 	username = strtok_r(str, user_delim, &last);
68ca0716f5SRobert Watson 	always = strtok_r(NULL, user_delim, &last);
69ca0716f5SRobert Watson 	never = strtok_r(NULL, user_delim, &last);
70ca0716f5SRobert Watson 
71ca0716f5SRobert Watson 	if ((username == NULL) || (always == NULL) || (never == NULL))
72ca0716f5SRobert Watson 		return (NULL);
73ca0716f5SRobert Watson 
74ca0716f5SRobert Watson 	if (strlen(username) >= AU_USER_NAME_MAX)
75ca0716f5SRobert Watson 		return (NULL);
76ca0716f5SRobert Watson 
7752267f74SRobert Watson 	strlcpy(u->au_name, username, AU_USER_NAME_MAX);
78ca0716f5SRobert Watson 	if (getauditflagsbin(always, &(u->au_always)) == -1)
79ca0716f5SRobert Watson 		return (NULL);
80ca0716f5SRobert Watson 
81ca0716f5SRobert Watson 	if (getauditflagsbin(never, &(u->au_never)) == -1)
82ca0716f5SRobert Watson 		return (NULL);
83ca0716f5SRobert Watson 
84ca0716f5SRobert Watson 	return (u);
85ca0716f5SRobert Watson }
86ca0716f5SRobert Watson 
87ca0716f5SRobert Watson /*
88ca0716f5SRobert Watson  * Rewind to beginning of the file
89ca0716f5SRobert Watson  */
90ca0716f5SRobert Watson static void
setauuser_locked(void)91ca0716f5SRobert Watson setauuser_locked(void)
92ca0716f5SRobert Watson {
93ca0716f5SRobert Watson 
94ca0716f5SRobert Watson 	if (fp != NULL)
95ca0716f5SRobert Watson 		fseek(fp, 0, SEEK_SET);
96ca0716f5SRobert Watson }
97ca0716f5SRobert Watson 
98ca0716f5SRobert Watson void
setauuser(void)99ca0716f5SRobert Watson setauuser(void)
100ca0716f5SRobert Watson {
101ca0716f5SRobert Watson 
1027a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
103ca0716f5SRobert Watson 	pthread_mutex_lock(&mutex);
1047a0a89d2SRobert Watson #endif
105ca0716f5SRobert Watson 	setauuser_locked();
1067a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
107ca0716f5SRobert Watson 	pthread_mutex_unlock(&mutex);
1087a0a89d2SRobert Watson #endif
109ca0716f5SRobert Watson }
110ca0716f5SRobert Watson 
111ca0716f5SRobert Watson /*
112ca0716f5SRobert Watson  * Close the file descriptor
113ca0716f5SRobert Watson  */
114ca0716f5SRobert Watson void
endauuser(void)115ca0716f5SRobert Watson endauuser(void)
116ca0716f5SRobert Watson {
117ca0716f5SRobert Watson 
1187a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
119ca0716f5SRobert Watson 	pthread_mutex_lock(&mutex);
1207a0a89d2SRobert Watson #endif
121ca0716f5SRobert Watson 	if (fp != NULL) {
122ca0716f5SRobert Watson 		fclose(fp);
123ca0716f5SRobert Watson 		fp = NULL;
124ca0716f5SRobert Watson 	}
1257a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
126ca0716f5SRobert Watson 	pthread_mutex_unlock(&mutex);
1277a0a89d2SRobert Watson #endif
128ca0716f5SRobert Watson }
129ca0716f5SRobert Watson 
130ca0716f5SRobert Watson /*
131ca0716f5SRobert Watson  * Enumerate the au_user_ent structures from the file
132ca0716f5SRobert Watson  */
133ca0716f5SRobert Watson static struct au_user_ent *
getauuserent_r_locked(struct au_user_ent * u)134ca0716f5SRobert Watson getauuserent_r_locked(struct au_user_ent *u)
135ca0716f5SRobert Watson {
136ca0716f5SRobert Watson 	char *nl;
137ca0716f5SRobert Watson 
138ca0716f5SRobert Watson 	if ((fp == NULL) && ((fp = fopen(AUDIT_USER_FILE, "r")) == NULL))
139ca0716f5SRobert Watson 		return (NULL);
140ca0716f5SRobert Watson 
141ca0716f5SRobert Watson 	while (1) {
142ca0716f5SRobert Watson 		if (fgets(linestr, AU_LINE_MAX, fp) == NULL)
143ca0716f5SRobert Watson 			return (NULL);
144ca0716f5SRobert Watson 
145ca0716f5SRobert Watson 		/* Remove new lines. */
146ca0716f5SRobert Watson 		if ((nl = strrchr(linestr, '\n')) != NULL)
147ca0716f5SRobert Watson 			*nl = '\0';
148ca0716f5SRobert Watson 
149ca0716f5SRobert Watson 		/* Skip comments. */
150ca0716f5SRobert Watson 		if (linestr[0] == '#')
151ca0716f5SRobert Watson 			continue;
152ca0716f5SRobert Watson 
153ca0716f5SRobert Watson 		/* Get the next structure. */
154ca0716f5SRobert Watson 		if (userfromstr(linestr, u) == NULL)
155ca0716f5SRobert Watson 			return (NULL);
156ca0716f5SRobert Watson 		break;
157ca0716f5SRobert Watson 	}
158ca0716f5SRobert Watson 
159ca0716f5SRobert Watson 	return (u);
160ca0716f5SRobert Watson }
161ca0716f5SRobert Watson 
162ca0716f5SRobert Watson struct au_user_ent *
getauuserent_r(struct au_user_ent * u)163ca0716f5SRobert Watson getauuserent_r(struct au_user_ent *u)
164ca0716f5SRobert Watson {
165ca0716f5SRobert Watson 	struct au_user_ent *up;
166ca0716f5SRobert Watson 
1677a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
168ca0716f5SRobert Watson 	pthread_mutex_lock(&mutex);
1697a0a89d2SRobert Watson #endif
170ca0716f5SRobert Watson 	up = getauuserent_r_locked(u);
1717a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
172ca0716f5SRobert Watson 	pthread_mutex_unlock(&mutex);
1737a0a89d2SRobert Watson #endif
174ca0716f5SRobert Watson 	return (up);
175ca0716f5SRobert Watson }
176ca0716f5SRobert Watson 
177ca0716f5SRobert Watson struct au_user_ent *
getauuserent(void)178ca0716f5SRobert Watson getauuserent(void)
179ca0716f5SRobert Watson {
180ca0716f5SRobert Watson 	static char user_ent_name[AU_USER_NAME_MAX];
181ca0716f5SRobert Watson 	static struct au_user_ent u;
182ca0716f5SRobert Watson 
183ca0716f5SRobert Watson 	bzero(&u, sizeof(u));
184ca0716f5SRobert Watson 	bzero(user_ent_name, sizeof(user_ent_name));
185ca0716f5SRobert Watson 	u.au_name = user_ent_name;
186ca0716f5SRobert Watson 
187ca0716f5SRobert Watson 	return (getauuserent_r(&u));
188ca0716f5SRobert Watson }
189ca0716f5SRobert Watson 
190ca0716f5SRobert Watson /*
191ca0716f5SRobert Watson  * Find a au_user_ent structure matching the given user name.
192ca0716f5SRobert Watson  */
193ca0716f5SRobert Watson struct au_user_ent *
getauusernam_r(struct au_user_ent * u,const char * name)194ca0716f5SRobert Watson getauusernam_r(struct au_user_ent *u, const char *name)
195ca0716f5SRobert Watson {
196ca0716f5SRobert Watson 	struct au_user_ent *up;
197ca0716f5SRobert Watson 
198ca0716f5SRobert Watson 	if (name == NULL)
199ca0716f5SRobert Watson 		return (NULL);
200ca0716f5SRobert Watson 
2017a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
202ca0716f5SRobert Watson 	pthread_mutex_lock(&mutex);
2037a0a89d2SRobert Watson #endif
204ca0716f5SRobert Watson 
205ca0716f5SRobert Watson 	setauuser_locked();
206ca0716f5SRobert Watson 	while ((up = getauuserent_r_locked(u)) != NULL) {
207ca0716f5SRobert Watson 		if (strcmp(name, u->au_name) == 0) {
2087a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
209ca0716f5SRobert Watson 			pthread_mutex_unlock(&mutex);
2107a0a89d2SRobert Watson #endif
211*aa772005SRobert Watson 			return (up);
212ca0716f5SRobert Watson 		}
213ca0716f5SRobert Watson 	}
214ca0716f5SRobert Watson 
2157a0a89d2SRobert Watson #ifdef HAVE_PTHREAD_MUTEX_LOCK
216ca0716f5SRobert Watson 	pthread_mutex_unlock(&mutex);
2177a0a89d2SRobert Watson #endif
218ca0716f5SRobert Watson 	return (NULL);
219ca0716f5SRobert Watson 
220ca0716f5SRobert Watson }
221ca0716f5SRobert Watson 
222ca0716f5SRobert Watson struct au_user_ent *
getauusernam(const char * name)223ca0716f5SRobert Watson getauusernam(const char *name)
224ca0716f5SRobert Watson {
225ca0716f5SRobert Watson 	static char user_ent_name[AU_USER_NAME_MAX];
226ca0716f5SRobert Watson 	static struct au_user_ent u;
227ca0716f5SRobert Watson 
228ca0716f5SRobert Watson 	bzero(&u, sizeof(u));
229ca0716f5SRobert Watson 	bzero(user_ent_name, sizeof(user_ent_name));
230ca0716f5SRobert Watson 	u.au_name = user_ent_name;
231ca0716f5SRobert Watson 
232ca0716f5SRobert Watson 	return (getauusernam_r(&u, name));
233ca0716f5SRobert Watson }
234ca0716f5SRobert Watson 
235ca0716f5SRobert Watson /*
236ca0716f5SRobert Watson  * Read the default system wide audit classes from audit_control, combine with
237ca0716f5SRobert Watson  * the per-user audit class and update the binary preselection mask.
238ca0716f5SRobert Watson  */
239ca0716f5SRobert Watson int
au_user_mask(char * username,au_mask_t * mask_p)240ca0716f5SRobert Watson au_user_mask(char *username, au_mask_t *mask_p)
241ca0716f5SRobert Watson {
242ca0716f5SRobert Watson 	char auditstring[MAX_AUDITSTRING_LEN + 1];
243ca0716f5SRobert Watson 	char user_ent_name[AU_USER_NAME_MAX];
244ca0716f5SRobert Watson 	struct au_user_ent u, *up;
245ca0716f5SRobert Watson 
246ca0716f5SRobert Watson 	bzero(&u, sizeof(u));
247ca0716f5SRobert Watson 	bzero(user_ent_name, sizeof(user_ent_name));
248ca0716f5SRobert Watson 	u.au_name = user_ent_name;
249ca0716f5SRobert Watson 
250ca0716f5SRobert Watson 	/* Get user mask. */
251ca0716f5SRobert Watson 	if ((up = getauusernam_r(&u, username)) != NULL) {
252ca0716f5SRobert Watson 		if (-1 == getfauditflags(&up->au_always, &up->au_never,
253ca0716f5SRobert Watson 		    mask_p))
254ca0716f5SRobert Watson 			return (-1);
255ca0716f5SRobert Watson 		return (0);
256ca0716f5SRobert Watson 	}
257ca0716f5SRobert Watson 
258ca0716f5SRobert Watson 	/* Read the default system mask. */
259ca0716f5SRobert Watson 	if (getacflg(auditstring, MAX_AUDITSTRING_LEN) == 0) {
260ca0716f5SRobert Watson 		if (-1 == getauditflagsbin(auditstring, mask_p))
261ca0716f5SRobert Watson 			return (-1);
262ca0716f5SRobert Watson 		return (0);
263ca0716f5SRobert Watson 	}
264ca0716f5SRobert Watson 
265ca0716f5SRobert Watson 	/* No masks defined. */
266ca0716f5SRobert Watson 	return (-1);
267ca0716f5SRobert Watson }
268ca0716f5SRobert Watson 
269ca0716f5SRobert Watson /*
270ca0716f5SRobert Watson  * Generate the process audit state by combining the audit masks passed as
271ca0716f5SRobert Watson  * parameters with the system audit masks.
272ca0716f5SRobert Watson  */
273ca0716f5SRobert Watson int
getfauditflags(au_mask_t * usremask,au_mask_t * usrdmask,au_mask_t * lastmask)274ca0716f5SRobert Watson getfauditflags(au_mask_t *usremask, au_mask_t *usrdmask, au_mask_t *lastmask)
275ca0716f5SRobert Watson {
276ca0716f5SRobert Watson 	char auditstring[MAX_AUDITSTRING_LEN + 1];
277ca0716f5SRobert Watson 
278ca0716f5SRobert Watson 	if ((usremask == NULL) || (usrdmask == NULL) || (lastmask == NULL))
279ca0716f5SRobert Watson 		return (-1);
280ca0716f5SRobert Watson 
281ca0716f5SRobert Watson 	lastmask->am_success = 0;
282ca0716f5SRobert Watson 	lastmask->am_failure = 0;
283ca0716f5SRobert Watson 
284ca0716f5SRobert Watson 	/* Get the system mask. */
285ca0716f5SRobert Watson 	if (getacflg(auditstring, MAX_AUDITSTRING_LEN) == 0) {
286ca0716f5SRobert Watson 		if (getauditflagsbin(auditstring, lastmask) != 0)
287ca0716f5SRobert Watson 			return (-1);
288ca0716f5SRobert Watson 	}
289ca0716f5SRobert Watson 
290ca0716f5SRobert Watson 	ADDMASK(lastmask, usremask);
291ca0716f5SRobert Watson 	SUBMASK(lastmask, usrdmask);
292ca0716f5SRobert Watson 
293ca0716f5SRobert Watson 	return (0);
294ca0716f5SRobert Watson }
295