xref: /onnv-gate/usr/src/lib/nsswitch/ad/common/getgrent.c (revision 8040:42abce45ef67)
1*8040SBaban.Kenkre@Sun.COM /*
2*8040SBaban.Kenkre@Sun.COM  * CDDL HEADER START
3*8040SBaban.Kenkre@Sun.COM  *
4*8040SBaban.Kenkre@Sun.COM  * The contents of this file are subject to the terms of the
5*8040SBaban.Kenkre@Sun.COM  * Common Development and Distribution License (the "License").
6*8040SBaban.Kenkre@Sun.COM  * You may not use this file except in compliance with the License.
7*8040SBaban.Kenkre@Sun.COM  *
8*8040SBaban.Kenkre@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8040SBaban.Kenkre@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*8040SBaban.Kenkre@Sun.COM  * See the License for the specific language governing permissions
11*8040SBaban.Kenkre@Sun.COM  * and limitations under the License.
12*8040SBaban.Kenkre@Sun.COM  *
13*8040SBaban.Kenkre@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*8040SBaban.Kenkre@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8040SBaban.Kenkre@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*8040SBaban.Kenkre@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*8040SBaban.Kenkre@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8040SBaban.Kenkre@Sun.COM  *
19*8040SBaban.Kenkre@Sun.COM  * CDDL HEADER END
20*8040SBaban.Kenkre@Sun.COM  */
21*8040SBaban.Kenkre@Sun.COM /*
22*8040SBaban.Kenkre@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*8040SBaban.Kenkre@Sun.COM  * Use is subject to license terms.
24*8040SBaban.Kenkre@Sun.COM  */
25*8040SBaban.Kenkre@Sun.COM 
26*8040SBaban.Kenkre@Sun.COM #include <grp.h>
27*8040SBaban.Kenkre@Sun.COM #include <idmap.h>
28*8040SBaban.Kenkre@Sun.COM #include "ad_common.h"
29*8040SBaban.Kenkre@Sun.COM 
30*8040SBaban.Kenkre@Sun.COM static int
31*8040SBaban.Kenkre@Sun.COM update_buffer(ad_backend_ptr be, nss_XbyY_args_t *argp,
32*8040SBaban.Kenkre@Sun.COM 		const char *name, const char *domain, gid_t gid)
33*8040SBaban.Kenkre@Sun.COM {
34*8040SBaban.Kenkre@Sun.COM 	int	buflen;
35*8040SBaban.Kenkre@Sun.COM 	char	*buffer;
36*8040SBaban.Kenkre@Sun.COM 
37*8040SBaban.Kenkre@Sun.COM 	if (domain == NULL)
38*8040SBaban.Kenkre@Sun.COM 		domain = WK_DOMAIN;
39*8040SBaban.Kenkre@Sun.COM 
40*8040SBaban.Kenkre@Sun.COM 	buflen = snprintf(NULL, 0, "%s@%s::%u:", name, domain, gid) + 1;
41*8040SBaban.Kenkre@Sun.COM 
42*8040SBaban.Kenkre@Sun.COM 	if (argp->buf.result != NULL) {
43*8040SBaban.Kenkre@Sun.COM 		buffer = be->buffer = malloc(buflen);
44*8040SBaban.Kenkre@Sun.COM 		if (be->buffer == NULL)
45*8040SBaban.Kenkre@Sun.COM 			return (-1);
46*8040SBaban.Kenkre@Sun.COM 		be->buflen = buflen;
47*8040SBaban.Kenkre@Sun.COM 	} else {
48*8040SBaban.Kenkre@Sun.COM 		if (buflen > argp->buf.buflen)
49*8040SBaban.Kenkre@Sun.COM 			return (-1);
50*8040SBaban.Kenkre@Sun.COM 		buflen = argp->buf.buflen;
51*8040SBaban.Kenkre@Sun.COM 		buffer = argp->buf.buffer;
52*8040SBaban.Kenkre@Sun.COM 	}
53*8040SBaban.Kenkre@Sun.COM 
54*8040SBaban.Kenkre@Sun.COM 	(void) snprintf(buffer, buflen, "%s@%s::%u:", name, domain, gid);
55*8040SBaban.Kenkre@Sun.COM 	return (0);
56*8040SBaban.Kenkre@Sun.COM }
57*8040SBaban.Kenkre@Sun.COM 
58*8040SBaban.Kenkre@Sun.COM /*
59*8040SBaban.Kenkre@Sun.COM  * getbynam gets a group entry by name. This function constructs an ldap
60*8040SBaban.Kenkre@Sun.COM  * search filter using the name invocation parameter and the getgrnam search
61*8040SBaban.Kenkre@Sun.COM  * filter defined. Once the filter is constructed, we search for a matching
62*8040SBaban.Kenkre@Sun.COM  * entry and marshal the data results into struct group for the frontend
63*8040SBaban.Kenkre@Sun.COM  * process. The function _nss_ad_group2ent performs the data marshaling.
64*8040SBaban.Kenkre@Sun.COM  */
65*8040SBaban.Kenkre@Sun.COM static nss_status_t
66*8040SBaban.Kenkre@Sun.COM getbynam(ad_backend_ptr be, void *a)
67*8040SBaban.Kenkre@Sun.COM {
68*8040SBaban.Kenkre@Sun.COM 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
69*8040SBaban.Kenkre@Sun.COM 	char		name[SEARCHFILTERLEN];
70*8040SBaban.Kenkre@Sun.COM 	char		*dname;
71*8040SBaban.Kenkre@Sun.COM 	nss_status_t	stat;
72*8040SBaban.Kenkre@Sun.COM 	idmap_stat	idmaprc;
73*8040SBaban.Kenkre@Sun.COM 	gid_t		gid;
74*8040SBaban.Kenkre@Sun.COM 	int		is_user, is_wuser;
75*8040SBaban.Kenkre@Sun.COM 	idmap_handle_t	*ih;
76*8040SBaban.Kenkre@Sun.COM 
77*8040SBaban.Kenkre@Sun.COM 	be->db_type = NSS_AD_DB_GROUP_BYNAME;
78*8040SBaban.Kenkre@Sun.COM 
79*8040SBaban.Kenkre@Sun.COM 	/* Sanitize name so that it can be used in our LDAP filter */
80*8040SBaban.Kenkre@Sun.COM 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
81*8040SBaban.Kenkre@Sun.COM 		return ((nss_status_t)NSS_NOTFOUND);
82*8040SBaban.Kenkre@Sun.COM 
83*8040SBaban.Kenkre@Sun.COM 	if ((dname = strchr(name, '@')) == NULL)
84*8040SBaban.Kenkre@Sun.COM 		return ((nss_status_t)NSS_NOTFOUND);
85*8040SBaban.Kenkre@Sun.COM 
86*8040SBaban.Kenkre@Sun.COM 	*dname = '\0';
87*8040SBaban.Kenkre@Sun.COM 	dname++;
88*8040SBaban.Kenkre@Sun.COM 
89*8040SBaban.Kenkre@Sun.COM 	/*
90*8040SBaban.Kenkre@Sun.COM 	 * Map the name to gid using idmap service.
91*8040SBaban.Kenkre@Sun.COM 	 */
92*8040SBaban.Kenkre@Sun.COM 	idmaprc = idmap_init(&ih);
93*8040SBaban.Kenkre@Sun.COM 	if (idmaprc != IDMAP_SUCCESS)
94*8040SBaban.Kenkre@Sun.COM 		return ((nss_status_t)NSS_NOTFOUND);
95*8040SBaban.Kenkre@Sun.COM 	is_wuser = -1;
96*8040SBaban.Kenkre@Sun.COM 	is_user = 0; /* Map name to gid */
97*8040SBaban.Kenkre@Sun.COM 	idmaprc = idmap_get_w2u_mapping(ih, NULL, NULL, name, dname,
98*8040SBaban.Kenkre@Sun.COM 	    0, &is_user, &is_wuser, &gid, NULL, NULL, NULL);
99*8040SBaban.Kenkre@Sun.COM 	(void) idmap_fini(ih);
100*8040SBaban.Kenkre@Sun.COM 	if (idmaprc != IDMAP_SUCCESS) {
101*8040SBaban.Kenkre@Sun.COM 		RESET_ERRNO();
102*8040SBaban.Kenkre@Sun.COM 		return ((nss_status_t)NSS_NOTFOUND);
103*8040SBaban.Kenkre@Sun.COM 	}
104*8040SBaban.Kenkre@Sun.COM 
105*8040SBaban.Kenkre@Sun.COM 	/* Create group(4) style string */
106*8040SBaban.Kenkre@Sun.COM 	if (update_buffer(be, argp, name, dname, gid) < 0)
107*8040SBaban.Kenkre@Sun.COM 		return ((nss_status_t)NSS_NOTFOUND);
108*8040SBaban.Kenkre@Sun.COM 
109*8040SBaban.Kenkre@Sun.COM 	/* Marshall the data, sanitize the return status and return */
110*8040SBaban.Kenkre@Sun.COM 	stat = _nss_ad_marshall_data(be, argp);
111*8040SBaban.Kenkre@Sun.COM 	return (_nss_ad_sanitize_status(be, argp, stat));
112*8040SBaban.Kenkre@Sun.COM }
113*8040SBaban.Kenkre@Sun.COM 
114*8040SBaban.Kenkre@Sun.COM /*
115*8040SBaban.Kenkre@Sun.COM  * getbygid gets a group entry by number. This function constructs an ldap
116*8040SBaban.Kenkre@Sun.COM  * search filter using the name invocation parameter and the getgrgid search
117*8040SBaban.Kenkre@Sun.COM  * filter defined. Once the filter is constructed, we searche for a matching
118*8040SBaban.Kenkre@Sun.COM  * entry and marshal the data results into struct group for the frontend
119*8040SBaban.Kenkre@Sun.COM  * process. The function _nss_ad_group2ent performs the data marshaling.
120*8040SBaban.Kenkre@Sun.COM  */
121*8040SBaban.Kenkre@Sun.COM static nss_status_t
122*8040SBaban.Kenkre@Sun.COM getbygid(ad_backend_ptr be, void *a)
123*8040SBaban.Kenkre@Sun.COM {
124*8040SBaban.Kenkre@Sun.COM 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
125*8040SBaban.Kenkre@Sun.COM 	char			*winname = NULL, *windomain = NULL;
126*8040SBaban.Kenkre@Sun.COM 	nss_status_t		stat;
127*8040SBaban.Kenkre@Sun.COM 
128*8040SBaban.Kenkre@Sun.COM 	be->db_type = NSS_AD_DB_GROUP_BYGID;
129*8040SBaban.Kenkre@Sun.COM 
130*8040SBaban.Kenkre@Sun.COM 	stat = (nss_status_t)NSS_NOTFOUND;
131*8040SBaban.Kenkre@Sun.COM 
132*8040SBaban.Kenkre@Sun.COM 	/* nss_ad does not support non ephemeral gids */
133*8040SBaban.Kenkre@Sun.COM 	if (argp->key.gid <= MAXUID)
134*8040SBaban.Kenkre@Sun.COM 		goto out;
135*8040SBaban.Kenkre@Sun.COM 
136*8040SBaban.Kenkre@Sun.COM 	/* Map the given GID to a SID using the idmap service */
137*8040SBaban.Kenkre@Sun.COM 	if (idmap_init(&be->ih) != 0)
138*8040SBaban.Kenkre@Sun.COM 		goto out;
139*8040SBaban.Kenkre@Sun.COM 	if (idmap_get_u2w_mapping(be->ih, &argp->key.gid, NULL, 0,
140*8040SBaban.Kenkre@Sun.COM 	    0, NULL, NULL, NULL, &winname, &windomain,
141*8040SBaban.Kenkre@Sun.COM 	    NULL, NULL) != 0) {
142*8040SBaban.Kenkre@Sun.COM 		RESET_ERRNO();
143*8040SBaban.Kenkre@Sun.COM 		goto out;
144*8040SBaban.Kenkre@Sun.COM 	}
145*8040SBaban.Kenkre@Sun.COM 
146*8040SBaban.Kenkre@Sun.COM 	/*
147*8040SBaban.Kenkre@Sun.COM 	 * NULL winname implies a local SID or unresolvable SID both of
148*8040SBaban.Kenkre@Sun.COM 	 * which cannot be used to generated group(4) entry
149*8040SBaban.Kenkre@Sun.COM 	 */
150*8040SBaban.Kenkre@Sun.COM 	if (winname == NULL)
151*8040SBaban.Kenkre@Sun.COM 		goto out;
152*8040SBaban.Kenkre@Sun.COM 
153*8040SBaban.Kenkre@Sun.COM 	/* Create group(4) style string */
154*8040SBaban.Kenkre@Sun.COM 	if (update_buffer(be, argp, winname, windomain, argp->key.gid) < 0)
155*8040SBaban.Kenkre@Sun.COM 		goto out;
156*8040SBaban.Kenkre@Sun.COM 
157*8040SBaban.Kenkre@Sun.COM 	/* Marshall the data, sanitize the return status and return */
158*8040SBaban.Kenkre@Sun.COM 	stat = _nss_ad_marshall_data(be, argp);
159*8040SBaban.Kenkre@Sun.COM 	stat = _nss_ad_sanitize_status(be, argp, stat);
160*8040SBaban.Kenkre@Sun.COM 
161*8040SBaban.Kenkre@Sun.COM out:
162*8040SBaban.Kenkre@Sun.COM 	idmap_free(winname);
163*8040SBaban.Kenkre@Sun.COM 	idmap_free(windomain);
164*8040SBaban.Kenkre@Sun.COM 	(void) idmap_fini(be->ih);
165*8040SBaban.Kenkre@Sun.COM 	be->ih = NULL;
166*8040SBaban.Kenkre@Sun.COM 	return (stat);
167*8040SBaban.Kenkre@Sun.COM }
168*8040SBaban.Kenkre@Sun.COM 
169*8040SBaban.Kenkre@Sun.COM static ad_backend_op_t gr_ops[] = {
170*8040SBaban.Kenkre@Sun.COM 	_nss_ad_destr,
171*8040SBaban.Kenkre@Sun.COM 	_nss_ad_endent,
172*8040SBaban.Kenkre@Sun.COM 	_nss_ad_setent,
173*8040SBaban.Kenkre@Sun.COM 	_nss_ad_getent,
174*8040SBaban.Kenkre@Sun.COM 	getbynam,
175*8040SBaban.Kenkre@Sun.COM 	getbygid
176*8040SBaban.Kenkre@Sun.COM };
177*8040SBaban.Kenkre@Sun.COM 
178*8040SBaban.Kenkre@Sun.COM /*ARGSUSED0*/
179*8040SBaban.Kenkre@Sun.COM nss_backend_t *
180*8040SBaban.Kenkre@Sun.COM _nss_ad_group_constr(const char *dummy1, const char *dummy2,
181*8040SBaban.Kenkre@Sun.COM 			const char *dummy3)
182*8040SBaban.Kenkre@Sun.COM {
183*8040SBaban.Kenkre@Sun.COM 
184*8040SBaban.Kenkre@Sun.COM 	return ((nss_backend_t *)_nss_ad_constr(gr_ops,
185*8040SBaban.Kenkre@Sun.COM 	    sizeof (gr_ops)/sizeof (gr_ops[0]), _GROUP, NULL, NULL));
186*8040SBaban.Kenkre@Sun.COM }
187