xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/tsol_getrhent.c (revision 1676:37f4a3e2bd99)
1*1676Sjpk /*
2*1676Sjpk  * CDDL HEADER START
3*1676Sjpk  *
4*1676Sjpk  * The contents of this file are subject to the terms of the
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * You may not use this file except in compliance with the License.
7*1676Sjpk  *
8*1676Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1676Sjpk  * or http://www.opensolaris.org/os/licensing.
10*1676Sjpk  * See the License for the specific language governing permissions
11*1676Sjpk  * and limitations under the License.
12*1676Sjpk  *
13*1676Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
14*1676Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1676Sjpk  * If applicable, add the following below this CDDL HEADER, with the
16*1676Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
17*1676Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1676Sjpk  *
19*1676Sjpk  * CDDL HEADER END
20*1676Sjpk  */
21*1676Sjpk /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1676Sjpk  * Use is subject to license terms.
24*1676Sjpk  */
25*1676Sjpk 
26*1676Sjpk #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*1676Sjpk 
28*1676Sjpk #include <netdb.h>
29*1676Sjpk #include "ldap_common.h"
30*1676Sjpk #include <sys/types.h>
31*1676Sjpk #include <sys/socket.h>
32*1676Sjpk #include <netinet/in.h>
33*1676Sjpk #include <arpa/inet.h>
34*1676Sjpk #include <sys/tsol/tndb.h>
35*1676Sjpk 
36*1676Sjpk /* tnrhdb attributes filters */
37*1676Sjpk #define	_TNRHDB_ADDR		"ipTnetNumber"
38*1676Sjpk #define	_TNRHDB_TNAME		"ipTnetTemplateName"
39*1676Sjpk #define	_F_GETTNDBBYADDR	"(&(objectClass=ipTnetHost)(ipTnetNumber=%s))"
40*1676Sjpk #define	_F_GETTNDBBYADDR_SSD	"(&(%%s)(ipTnetNumber=%s))"
41*1676Sjpk 
42*1676Sjpk static const char *tnrhdb_attrs[] = {
43*1676Sjpk 	_TNRHDB_ADDR,
44*1676Sjpk 	_TNRHDB_TNAME,
45*1676Sjpk 	NULL
46*1676Sjpk };
47*1676Sjpk 
48*1676Sjpk static int
49*1676Sjpk _nss_ldap_tnrhdb2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
50*1676Sjpk {
51*1676Sjpk 	int			i, nss_result;
52*1676Sjpk 	int			len = 0;
53*1676Sjpk 	int			buflen = 0;
54*1676Sjpk 	char			*buffer = NULL;
55*1676Sjpk 	char			*ceiling = NULL;
56*1676Sjpk 	ns_ldap_attr_t		*attrptr;
57*1676Sjpk 	ns_ldap_result_t	*result = be->result;
58*1676Sjpk 	tsol_rhstr_t		*rhstrp;
59*1676Sjpk 
60*1676Sjpk 	buffer = argp->buf.buffer;
61*1676Sjpk 	buflen = argp->buf.buflen;
62*1676Sjpk 	if (argp->buf.result == NULL) {
63*1676Sjpk 		nss_result = NSS_STR_PARSE_ERANGE;
64*1676Sjpk 		goto result_tnrhdb2ent;
65*1676Sjpk 	}
66*1676Sjpk 	rhstrp = (tsol_rhstr_t *)(argp->buf.result);
67*1676Sjpk 	rhstrp->family = 0;
68*1676Sjpk 	rhstrp->address = rhstrp->template = NULL;
69*1676Sjpk 	ceiling = buffer + buflen;
70*1676Sjpk 	(void) memset(argp->buf.buffer, 0, buflen);
71*1676Sjpk 	attrptr = getattr(result, 0);
72*1676Sjpk 	if (attrptr == NULL) {
73*1676Sjpk 		nss_result = NSS_STR_PARSE_PARSE;
74*1676Sjpk 		goto result_tnrhdb2ent;
75*1676Sjpk 	}
76*1676Sjpk 	for (i = 0; i < result->entry->attr_count; i++) {
77*1676Sjpk 		attrptr = getattr(result, i);
78*1676Sjpk 		if (attrptr == NULL) {
79*1676Sjpk 			nss_result = NSS_STR_PARSE_PARSE;
80*1676Sjpk 			goto result_tnrhdb2ent;
81*1676Sjpk 		}
82*1676Sjpk 		if (strcasecmp(attrptr->attrname, _TNRHDB_ADDR) == 0) {
83*1676Sjpk 			len = strlen(attrptr->attrvalue[0]);
84*1676Sjpk 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
85*1676Sjpk 				nss_result = NSS_STR_PARSE_PARSE;
86*1676Sjpk 				goto result_tnrhdb2ent;
87*1676Sjpk 			}
88*1676Sjpk 			rhstrp->address = buffer;
89*1676Sjpk 			buffer += len + 1;
90*1676Sjpk 			if (buffer >= ceiling) {
91*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_ERANGE;
92*1676Sjpk 				goto result_tnrhdb2ent;
93*1676Sjpk 			}
94*1676Sjpk 			(void) strcpy(rhstrp->address, attrptr->attrvalue[0]);
95*1676Sjpk 			continue;
96*1676Sjpk 		}
97*1676Sjpk 		if (strcasecmp(attrptr->attrname, _TNRHDB_TNAME) == 0) {
98*1676Sjpk 			len = strlen(attrptr->attrvalue[0]);
99*1676Sjpk 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
100*1676Sjpk 				nss_result = NSS_STR_PARSE_PARSE;
101*1676Sjpk 				goto result_tnrhdb2ent;
102*1676Sjpk 			}
103*1676Sjpk 			rhstrp->template = buffer;
104*1676Sjpk 			buffer += len + 1;
105*1676Sjpk 			if (buffer >= ceiling) {
106*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_ERANGE;
107*1676Sjpk 				goto result_tnrhdb2ent;
108*1676Sjpk 			}
109*1676Sjpk 			(void) strcpy(rhstrp->template, attrptr->attrvalue[0]);
110*1676Sjpk 			continue;
111*1676Sjpk 		}
112*1676Sjpk 	}
113*1676Sjpk 	nss_result = NSS_STR_PARSE_SUCCESS;
114*1676Sjpk 
115*1676Sjpk #ifdef	DEBUG
116*1676Sjpk 	(void) printf("\n[tsol_getrhent.c: _nss_ldap_tnrhdb2ent]\n");
117*1676Sjpk 	(void) printf("      address: [%s]\n",
118*1676Sjpk 	    rhstrp->address ? rhstrp->address : "NULL");
119*1676Sjpk 	(void) printf("template: [%s]\n",
120*1676Sjpk 	    rhstrp->template ? rhstrp->template : "NULL");
121*1676Sjpk #endif	/* DEBUG */
122*1676Sjpk 
123*1676Sjpk result_tnrhdb2ent:
124*1676Sjpk 	(void) __ns_ldap_freeResult(&be->result);
125*1676Sjpk 	return (nss_result);
126*1676Sjpk }
127*1676Sjpk 
128*1676Sjpk 
129*1676Sjpk static nss_status_t
130*1676Sjpk getbyaddr(ldap_backend_ptr be, void *a)
131*1676Sjpk {
132*1676Sjpk 	char		searchfilter[SEARCHFILTERLEN];
133*1676Sjpk 	char		userdata[SEARCHFILTERLEN];
134*1676Sjpk 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
135*1676Sjpk 	struct in_addr  addr;
136*1676Sjpk 	char 		buf[18];
137*1676Sjpk 	extern char	*inet_ntoa_r();
138*1676Sjpk 
139*1676Sjpk #ifdef	DEBUG
140*1676Sjpk 	(void) fprintf(stdout, "\n[tsol_getrhent.c: getbyaddr]\n");
141*1676Sjpk #endif	/* DEBUG */
142*1676Sjpk 
143*1676Sjpk 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
144*1676Sjpk 	(void) inet_ntoa_r(addr, buf);
145*1676Sjpk 
146*1676Sjpk 	if (snprintf(searchfilter, sizeof (searchfilter), _F_GETTNDBBYADDR,
147*1676Sjpk 	    buf) < 0)
148*1676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
149*1676Sjpk 
150*1676Sjpk 	if (snprintf(userdata, sizeof (userdata), _F_GETTNDBBYADDR_SSD,
151*1676Sjpk 	    buf) < 0)
152*1676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
153*1676Sjpk 
154*1676Sjpk 	return (_nss_ldap_lookup(be, argp, _TNRHDB, searchfilter, NULL,
155*1676Sjpk 	    _merge_SSD_filter, userdata));
156*1676Sjpk }
157*1676Sjpk 
158*1676Sjpk 
159*1676Sjpk static ldap_backend_op_t tnrhdb_ops[] = {
160*1676Sjpk 	_nss_ldap_destr,
161*1676Sjpk 	_nss_ldap_endent,
162*1676Sjpk 	_nss_ldap_setent,
163*1676Sjpk 	_nss_ldap_getent,
164*1676Sjpk 	getbyaddr
165*1676Sjpk };
166*1676Sjpk 
167*1676Sjpk 
168*1676Sjpk /* ARGSUSED */
169*1676Sjpk nss_backend_t *
170*1676Sjpk _nss_ldap_tnrhdb_constr(const char *dummy1,
171*1676Sjpk     const char *dummy2,
172*1676Sjpk     const char *dummy3,
173*1676Sjpk     const char *dummy4,
174*1676Sjpk     const char *dummy5)
175*1676Sjpk {
176*1676Sjpk #ifdef	DEBUG
177*1676Sjpk 	(void) fprintf(stdout,
178*1676Sjpk 	    "\n[tsol_getrhent.c: _nss_ldap_tnrhdb_constr]\n");
179*1676Sjpk #endif
180*1676Sjpk 	return ((nss_backend_t *)_nss_ldap_constr(tnrhdb_ops,
181*1676Sjpk 		sizeof (tnrhdb_ops)/sizeof (tnrhdb_ops[0]), _TNRHDB,
182*1676Sjpk 		tnrhdb_attrs, _nss_ldap_tnrhdb2ent));
183*1676Sjpk }
184