summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbsm/common/audit_user.c
blob: 8342687717fb197442b0c1d9dc9fd42bb11b03c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* Interfaces to audit_user(4) (/etc/security/audit_user) */

#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <string.h>
#include <bsm/audit.h>
#include <bsm/libbsm.h>
#include <synch.h>
#include <nss_dbdefs.h>
#include <stdlib.h>
#include <utmpx.h>

#define	MAX_USERNAME	sizeof (((struct utmpx *)0)->ut_user)

static mutex_t mutex_userfile = DEFAULTMUTEX;
static au_user_ent_t *auuserstr2ent(au_user_ent_t *, au_user_str_t *);

/* Externs from libnsl */
extern void _setauuser(void);
extern void _endauuser(void);
extern au_user_str_t *_getauuserent(au_user_str_t *, char *, int, int *);
extern au_user_str_t *_getauusernam(char *, au_user_str_t *, char *, int,
    int *);

void
setauuser()
{
	(void) mutex_lock(&mutex_userfile);
	_setauuser();
	(void) mutex_unlock(&mutex_userfile);
}

void
endauuser()
{
	(void) mutex_lock(&mutex_userfile);
	_endauuser();
	(void) mutex_unlock(&mutex_userfile);
}

au_user_ent_t *
getauuserent()
{
	static au_user_ent_t au_user_entry;
	static char	logname[MAX_USERNAME+1];

	/* initialize au_user_entry structure */
	au_user_entry.au_name = logname;

	return (getauuserent_r(&au_user_entry));

}

au_user_ent_t *
getauuserent_r(au_user_ent_t *au_user_entry)
{
	au_user_str_t	us;
	au_user_str_t	*tmp;
	char 		buf[NSS_BUFLEN_AUDITUSER];
	int 		errp = 0;

	(void) mutex_lock(&mutex_userfile);
	(void) memset(buf, NULL, NSS_BUFLEN_AUDITUSER);
	tmp = _getauuserent(&us, buf, NSS_BUFLEN_AUDITUSER, &errp);
	(void) mutex_unlock(&mutex_userfile);

	return (auuserstr2ent(au_user_entry, tmp));
}

au_user_ent_t *
getauusernam(char *name)
{
	static au_user_ent_t u;
	static char	logname[MAX_USERNAME+1];

	/* initialize au_user_entry structure */
	u.au_name = logname;

	return (getauusernam_r(&u, name));
}

au_user_ent_t *
getauusernam_r(au_user_ent_t *u, char *name)
{
	au_user_str_t	us;
	au_user_str_t	*tmp;
	char		buf[NSS_BUFLEN_AUDITUSER];
	int		errp = 0;

	if (name == NULL) {
		return ((au_user_ent_t *)NULL);
	}
	tmp = _getauusernam(name, &us, buf, NSS_BUFLEN_AUDITUSER, &errp);

	return (auuserstr2ent(u, tmp));
}

static au_user_ent_t *
auuserstr2ent(au_user_ent_t *ue, au_user_str_t *us)
{
	if (us == NULL)
		return (NULL);

	if (getauditflagsbin(us->au_always, &ue->au_always) < 0) {
		return (NULL);
	}
	if (getauditflagsbin(us->au_never, &ue->au_never) < 0) {
		ue->au_never.am_success = AU_MASK_NONE;
		ue->au_never.am_failure = AU_MASK_NONE;
	}
	(void) strncpy(ue->au_name, us->au_name, MAX_USERNAME);

	return (ue);
}