1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <secdb.h>
30*0Sstevel@tonic-gate #include "ldap_common.h"
31*0Sstevel@tonic-gate #include <bsm/libbsm.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /* audit_user attributes */
35*0Sstevel@tonic-gate #define	_AU_NAME		"uid"
36*0Sstevel@tonic-gate #define	_AU_ALWAYS		"SolarisAuditAlways"
37*0Sstevel@tonic-gate #define	_AU_NEVER		"SolarisAuditNever"
38*0Sstevel@tonic-gate #define	_AU_GETAUUSERNAME	"(&(objectClass=SolarisAuditUser)(uid=%s))"
39*0Sstevel@tonic-gate #define	_AU_GETAUUSERNAME_SSD	"(&(%%s)(uid=%s))"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static const char *auuser_attrs[] = {
43*0Sstevel@tonic-gate 	_AU_NAME,
44*0Sstevel@tonic-gate 	_AU_ALWAYS,
45*0Sstevel@tonic-gate 	_AU_NEVER,
46*0Sstevel@tonic-gate 	(char *)NULL
47*0Sstevel@tonic-gate };
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static int
51*0Sstevel@tonic-gate _nss_ldap_au2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	int			i, nss_result;
54*0Sstevel@tonic-gate 	int			buflen = (int)0;
55*0Sstevel@tonic-gate 	unsigned long		len = 0L;
56*0Sstevel@tonic-gate 	char			*nullstring = (char *)NULL;
57*0Sstevel@tonic-gate 	char			*buffer = (char *)NULL;
58*0Sstevel@tonic-gate 	char			*ceiling = (char *)NULL;
59*0Sstevel@tonic-gate 	au_user_str_t		*au_user = (au_user_str_t *)NULL;
60*0Sstevel@tonic-gate 	ns_ldap_attr_t		*attrptr;
61*0Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	buffer = argp->buf.buffer;
64*0Sstevel@tonic-gate 	buflen = (size_t)argp->buf.buflen;
65*0Sstevel@tonic-gate 	if (!argp->buf.result) {
66*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_ERANGE;
67*0Sstevel@tonic-gate 		goto result_au2ent;
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 	au_user = (au_user_str_t *)(argp->buf.result);
70*0Sstevel@tonic-gate 	ceiling = buffer + buflen;
71*0Sstevel@tonic-gate 	au_user->au_name = (char *)NULL;
72*0Sstevel@tonic-gate 	au_user->au_always = (char *)NULL;
73*0Sstevel@tonic-gate 	au_user->au_never = (char *)NULL;
74*0Sstevel@tonic-gate 	nss_result = (int)NSS_STR_PARSE_SUCCESS;
75*0Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	attrptr = getattr(result, 0);
78*0Sstevel@tonic-gate 	if (attrptr == NULL) {
79*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_PARSE;
80*0Sstevel@tonic-gate 		goto result_au2ent;
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	for (i = 0; i < result->entry->attr_count; i++) {
83*0Sstevel@tonic-gate 		attrptr = getattr(result, i);
84*0Sstevel@tonic-gate 		if (attrptr == NULL) {
85*0Sstevel@tonic-gate 			nss_result = (int)NSS_STR_PARSE_PARSE;
86*0Sstevel@tonic-gate 			goto result_au2ent;
87*0Sstevel@tonic-gate 		}
88*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AU_NAME) == 0) {
89*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
90*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
91*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_PARSE;
92*0Sstevel@tonic-gate 				goto result_au2ent;
93*0Sstevel@tonic-gate 			}
94*0Sstevel@tonic-gate 			au_user->au_name = buffer;
95*0Sstevel@tonic-gate 			buffer += len + 1;
96*0Sstevel@tonic-gate 			if (buffer >= ceiling) {
97*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_ERANGE;
98*0Sstevel@tonic-gate 				goto result_au2ent;
99*0Sstevel@tonic-gate 			}
100*0Sstevel@tonic-gate 			(void) strcpy(au_user->au_name, attrptr->attrvalue[0]);
101*0Sstevel@tonic-gate 			continue;
102*0Sstevel@tonic-gate 		}
103*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AU_ALWAYS) == 0) {
104*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
105*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
106*0Sstevel@tonic-gate 				au_user->au_always = nullstring;
107*0Sstevel@tonic-gate 			} else {
108*0Sstevel@tonic-gate 				au_user->au_always = buffer;
109*0Sstevel@tonic-gate 				buffer += len + 1;
110*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
111*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
112*0Sstevel@tonic-gate 					goto result_au2ent;
113*0Sstevel@tonic-gate 				}
114*0Sstevel@tonic-gate 				(void) strcpy(au_user->au_always,
115*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
116*0Sstevel@tonic-gate 			}
117*0Sstevel@tonic-gate 			continue;
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AU_NEVER) == 0) {
120*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
121*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
122*0Sstevel@tonic-gate 				au_user->au_never = nullstring;
123*0Sstevel@tonic-gate 			} else {
124*0Sstevel@tonic-gate 				au_user->au_never = buffer;
125*0Sstevel@tonic-gate 				buffer += len + 1;
126*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
127*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
128*0Sstevel@tonic-gate 					goto result_au2ent;
129*0Sstevel@tonic-gate 				}
130*0Sstevel@tonic-gate 				(void) strcpy(au_user->au_never,
131*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
132*0Sstevel@tonic-gate 			}
133*0Sstevel@tonic-gate 			continue;
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate #ifdef	DEBUG
138*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getauuser.c: _nss_ldap_au2ent]\n");
139*0Sstevel@tonic-gate 	(void) fprintf(stdout, "      au_name: [%s]\n", au_user->au_name);
140*0Sstevel@tonic-gate 	if (au_user->au_always != (char *)NULL) {
141*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      au_always: [%s]\n",
142*0Sstevel@tonic-gate 		    au_user->au_always);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 	if (au_user->au_never != (char *)NULL) {
145*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      au_never: [%s]\n",
146*0Sstevel@tonic-gate 		    au_user->au_never);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate #endif	/* DEBUG */
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate result_au2ent:
151*0Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
152*0Sstevel@tonic-gate 	return ((int)nss_result);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static nss_status_t
157*0Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
160*0Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
161*0Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
162*0Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
163*0Sstevel@tonic-gate 	int		ret;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate #ifdef	DEBUG
166*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getauuser.c: getbyname]\n");
167*0Sstevel@tonic-gate #endif	/* DEBUG */
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
170*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
173*0Sstevel@tonic-gate 	    _AU_GETAUUSERNAME, name);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
176*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
179*0Sstevel@tonic-gate 	    _AU_GETAUUSERNAME_SSD, name);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
182*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	return (_nss_ldap_lookup(be, argp, _AUUSER, searchfilter, NULL,
185*0Sstevel@tonic-gate 	    _merge_SSD_filter, userdata));
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static ldap_backend_op_t auuser_ops[] = {
190*0Sstevel@tonic-gate 	_nss_ldap_destr,
191*0Sstevel@tonic-gate 	_nss_ldap_endent,
192*0Sstevel@tonic-gate 	_nss_ldap_setent,
193*0Sstevel@tonic-gate 	_nss_ldap_getent,
194*0Sstevel@tonic-gate 	getbyname
195*0Sstevel@tonic-gate };
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /*ARGSUSED0*/
199*0Sstevel@tonic-gate nss_backend_t *
200*0Sstevel@tonic-gate _nss_ldap_audit_user_constr(const char *dummy1,
201*0Sstevel@tonic-gate     const char *dummy2,
202*0Sstevel@tonic-gate     const char *dummy3,
203*0Sstevel@tonic-gate     const char *dummy4,
204*0Sstevel@tonic-gate     const char *dummy5)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate #ifdef	DEBUG
207*0Sstevel@tonic-gate 	(void) fprintf(stdout,
208*0Sstevel@tonic-gate 	    "\n[getauuser.c: _nss_ldap_audit_user_constr]\n");
209*0Sstevel@tonic-gate #endif
210*0Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(auuser_ops,
211*0Sstevel@tonic-gate 		sizeof (auuser_ops)/sizeof (auuser_ops[0]), _AUUSER,
212*0Sstevel@tonic-gate 		auuser_attrs, _nss_ldap_au2ent));
213*0Sstevel@tonic-gate }
214