xref: /titanic_51/usr/src/lib/nsswitch/ldap/common/tsol_getrhent.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
145916cd2Sjpk /*
245916cd2Sjpk  * CDDL HEADER START
345916cd2Sjpk  *
445916cd2Sjpk  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
745916cd2Sjpk  *
845916cd2Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk  * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk  * See the License for the specific language governing permissions
1145916cd2Sjpk  * and limitations under the License.
1245916cd2Sjpk  *
1345916cd2Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk  * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk  *
1945916cd2Sjpk  * CDDL HEADER END
2045916cd2Sjpk  */
2145916cd2Sjpk /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2345916cd2Sjpk  * Use is subject to license terms.
2445916cd2Sjpk  */
2545916cd2Sjpk 
2645916cd2Sjpk #pragma ident	"%Z%%M%	%I%	%E% SMI"
2745916cd2Sjpk 
2845916cd2Sjpk #include <netdb.h>
2945916cd2Sjpk #include "ldap_common.h"
3045916cd2Sjpk #include <sys/types.h>
3145916cd2Sjpk #include <sys/socket.h>
3245916cd2Sjpk #include <netinet/in.h>
3345916cd2Sjpk #include <arpa/inet.h>
3445916cd2Sjpk #include <sys/tsol/tndb.h>
3545916cd2Sjpk 
3645916cd2Sjpk /* tnrhdb attributes filters */
3745916cd2Sjpk #define	_TNRHDB_ADDR		"ipTnetNumber"
3845916cd2Sjpk #define	_TNRHDB_TNAME		"ipTnetTemplateName"
3945916cd2Sjpk #define	_F_GETTNDBBYADDR	"(&(objectClass=ipTnetHost)(ipTnetNumber=%s))"
4045916cd2Sjpk #define	_F_GETTNDBBYADDR_SSD	"(&(%%s)(ipTnetNumber=%s))"
4145916cd2Sjpk 
4245916cd2Sjpk static const char *tnrhdb_attrs[] = {
4345916cd2Sjpk 	_TNRHDB_ADDR,
4445916cd2Sjpk 	_TNRHDB_TNAME,
4545916cd2Sjpk 	NULL
4645916cd2Sjpk };
4745916cd2Sjpk 
48*cb5caa98Sdjl static void
49*cb5caa98Sdjl escape_colon(char *in, char *out) {
50*cb5caa98Sdjl 	int i, j;
51*cb5caa98Sdjl 	for (i = 0, j = 0; in[i] != '\0'; i++) {
52*cb5caa98Sdjl 		if (in[i] == ':') {
53*cb5caa98Sdjl 			out[j++] = '\\';
54*cb5caa98Sdjl 			out[j++] = in[i];
55*cb5caa98Sdjl 		} else
56*cb5caa98Sdjl 			out[j++] = in[i];
57*cb5caa98Sdjl 	}
58*cb5caa98Sdjl 	out[j] = '\0';
59*cb5caa98Sdjl }
60*cb5caa98Sdjl 
61*cb5caa98Sdjl /*
62*cb5caa98Sdjl  * _nss_ldap_tnrhdb2str is the data marshaling method for the tnrhdb
63*cb5caa98Sdjl  * (tsol_getrhbyaddr()/tsol_getrhent()) backend processes.
64*cb5caa98Sdjl  * This method is called after a successful ldap search has been performed.
65*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
66*cb5caa98Sdjl  *
67*cb5caa98Sdjl  * e.g.
68*cb5caa98Sdjl  *
69*cb5caa98Sdjl  * 192.168.120.6:public
70*cb5caa98Sdjl  * fec0\:\:a00\:20ff\:fea0\:21f7:cipso
71*cb5caa98Sdjl  *
72*cb5caa98Sdjl  */
7345916cd2Sjpk static int
74*cb5caa98Sdjl _nss_ldap_tnrhdb2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
7545916cd2Sjpk {
76*cb5caa98Sdjl 	int			nss_result = NSS_STR_PARSE_SUCCESS;
7745916cd2Sjpk 	int			len = 0;
7845916cd2Sjpk 	char			*buffer = NULL;
79*cb5caa98Sdjl 	char			**addr, **template, *addr_out;
8045916cd2Sjpk 	ns_ldap_result_t	*result = be->result;
81*cb5caa98Sdjl 	char addr6[INET6_ADDRSTRLEN + 5]; /* 5 '\' for ':' at most */
8245916cd2Sjpk 
83*cb5caa98Sdjl 	if (result == NULL)
84*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
85*cb5caa98Sdjl 
86*cb5caa98Sdjl 	addr = __ns_ldap_getAttr(result->entry, _TNRHDB_ADDR);
87*cb5caa98Sdjl 	if (addr == NULL || addr[0] == NULL || (strlen(addr[0]) < 1)) {
88*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
89*cb5caa98Sdjl 		goto result_tnrhdb2str;
90*cb5caa98Sdjl 	}
91*cb5caa98Sdjl 
92*cb5caa98Sdjl 	/*
93*cb5caa98Sdjl 	 * Escape ':' in IPV6.
94*cb5caa98Sdjl 	 * The value is stored in LDAP directory without escape charaters.
95*cb5caa98Sdjl 	 */
96*cb5caa98Sdjl 	if (strchr(addr[0], ':') != NULL) {
97*cb5caa98Sdjl 		escape_colon(addr[0], addr6);
98*cb5caa98Sdjl 		addr_out = addr6;
99*cb5caa98Sdjl 	} else
100*cb5caa98Sdjl 		addr_out = addr[0];
101*cb5caa98Sdjl 
102*cb5caa98Sdjl 	template = __ns_ldap_getAttr(result->entry, _TNRHDB_TNAME);
103*cb5caa98Sdjl 	if (template == NULL || template[0] == NULL ||
104*cb5caa98Sdjl 			(strlen(template[0]) < 1)) {
105*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
106*cb5caa98Sdjl 		goto result_tnrhdb2str;
107*cb5caa98Sdjl 	}
108*cb5caa98Sdjl 	/* "addr:template" */
109*cb5caa98Sdjl 	len = strlen(addr_out) + strlen(template[0]) + 2;
110*cb5caa98Sdjl 
111*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
112*cb5caa98Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
113*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
114*cb5caa98Sdjl 			goto result_tnrhdb2str;
115*cb5caa98Sdjl 		}
116*cb5caa98Sdjl 		be->buflen = len - 1;
117*cb5caa98Sdjl 		buffer = be->buffer;
118*cb5caa98Sdjl 	} else
11945916cd2Sjpk 		buffer = argp->buf.buffer;
12045916cd2Sjpk 
121*cb5caa98Sdjl 	(void) snprintf(buffer, len, "%s:%s", addr_out, template[0]);
12245916cd2Sjpk 
123*cb5caa98Sdjl result_tnrhdb2str:
12445916cd2Sjpk 	(void) __ns_ldap_freeResult(&be->result);
12545916cd2Sjpk 	return (nss_result);
12645916cd2Sjpk }
12745916cd2Sjpk 
12845916cd2Sjpk 
12945916cd2Sjpk static nss_status_t
13045916cd2Sjpk getbyaddr(ldap_backend_ptr be, void *a)
13145916cd2Sjpk {
13245916cd2Sjpk 	char		searchfilter[SEARCHFILTERLEN];
13345916cd2Sjpk 	char		userdata[SEARCHFILTERLEN];
13445916cd2Sjpk 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
13545916cd2Sjpk 
136*cb5caa98Sdjl 	if (argp->key.hostaddr.addr == NULL ||
137*cb5caa98Sdjl 		(argp->key.hostaddr.type != AF_INET &&
138*cb5caa98Sdjl 		argp->key.hostaddr.type != AF_INET6))
139*cb5caa98Sdjl 			return (NSS_NOTFOUND);
140*cb5caa98Sdjl 	if (strchr(argp->key.hostaddr.addr, ':') != NULL) {
141*cb5caa98Sdjl 		/* IPV6 */
142*cb5caa98Sdjl 		if (argp->key.hostaddr.type == AF_INET)
143*cb5caa98Sdjl 			return (NSS_NOTFOUND);
144*cb5caa98Sdjl 	} else {
145*cb5caa98Sdjl 		/* IPV4 */
146*cb5caa98Sdjl 		if (argp->key.hostaddr.type == AF_INET6)
147*cb5caa98Sdjl 			return (NSS_NOTFOUND);
148*cb5caa98Sdjl 	}
14945916cd2Sjpk 
150*cb5caa98Sdjl 	/*
151*cb5caa98Sdjl 	 * The IPV6 addresses are saved in the directory without '\'s.
152*cb5caa98Sdjl 	 * So don't need to escape colons in IPV6 addresses.
153*cb5caa98Sdjl 	 */
15445916cd2Sjpk 	if (snprintf(searchfilter, sizeof (searchfilter), _F_GETTNDBBYADDR,
155*cb5caa98Sdjl 	    argp->key.hostaddr.addr) < 0)
15645916cd2Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
15745916cd2Sjpk 
15845916cd2Sjpk 	if (snprintf(userdata, sizeof (userdata), _F_GETTNDBBYADDR_SSD,
159*cb5caa98Sdjl 	    argp->key.hostaddr.addr) < 0)
16045916cd2Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
16145916cd2Sjpk 
16245916cd2Sjpk 	return (_nss_ldap_lookup(be, argp, _TNRHDB, searchfilter, NULL,
16345916cd2Sjpk 	    _merge_SSD_filter, userdata));
16445916cd2Sjpk }
16545916cd2Sjpk 
16645916cd2Sjpk 
16745916cd2Sjpk static ldap_backend_op_t tnrhdb_ops[] = {
16845916cd2Sjpk 	_nss_ldap_destr,
16945916cd2Sjpk 	_nss_ldap_endent,
17045916cd2Sjpk 	_nss_ldap_setent,
17145916cd2Sjpk 	_nss_ldap_getent,
17245916cd2Sjpk 	getbyaddr
17345916cd2Sjpk };
17445916cd2Sjpk 
17545916cd2Sjpk 
17645916cd2Sjpk /* ARGSUSED */
17745916cd2Sjpk nss_backend_t *
17845916cd2Sjpk _nss_ldap_tnrhdb_constr(const char *dummy1,
17945916cd2Sjpk     const char *dummy2,
18045916cd2Sjpk     const char *dummy3,
18145916cd2Sjpk     const char *dummy4,
18245916cd2Sjpk     const char *dummy5)
18345916cd2Sjpk {
18445916cd2Sjpk 	return ((nss_backend_t *)_nss_ldap_constr(tnrhdb_ops,
18545916cd2Sjpk 		sizeof (tnrhdb_ops)/sizeof (tnrhdb_ops[0]), _TNRHDB,
186*cb5caa98Sdjl 		tnrhdb_attrs, _nss_ldap_tnrhdb2str));
18745916cd2Sjpk }
188