17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <ctype.h>
277c478bd9Sstevel@tonic-gate #include <netdb.h>
287c478bd9Sstevel@tonic-gate #include "ns_internal.h"
297c478bd9Sstevel@tonic-gate #include "ldap_common.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* protocols attributes filters */
327c478bd9Sstevel@tonic-gate #define	_P_NAME			"cn"
337c478bd9Sstevel@tonic-gate #define	_P_PROTO		"ipprotocolnumber"
347c478bd9Sstevel@tonic-gate #define	_F_GETPROTOBYNAME	"(&(objectClass=ipProtocol)(cn=%s))"
357c478bd9Sstevel@tonic-gate #define	_F_GETPROTOBYNAME_SSD	"(&(%%s)(cn=%s))"
367c478bd9Sstevel@tonic-gate #define	_F_GETPROTOBYNUMBER	\
377c478bd9Sstevel@tonic-gate 	"(&(objectClass=ipProtocol)(ipProtocolNumber=%d))"
387c478bd9Sstevel@tonic-gate #define	_F_GETPROTOBYNUMBER_SSD	\
397c478bd9Sstevel@tonic-gate 	"(&(%%s)(ipProtocolNumber=%d))"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static const char *protocols_attrs[] = {
427c478bd9Sstevel@tonic-gate 	_P_NAME,
437c478bd9Sstevel@tonic-gate 	_P_PROTO,
447c478bd9Sstevel@tonic-gate 	(char *)NULL
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate 
47*cb5caa98Sdjl typedef struct protocol_alias {
48*cb5caa98Sdjl 	char	*protocol;
49*cb5caa98Sdjl 	char	*alias;
50*cb5caa98Sdjl } protocol_alias_t;
51*cb5caa98Sdjl 
52*cb5caa98Sdjl static const protocol_alias_t ip_aliases[10] = {
53*cb5caa98Sdjl 	{ "ip", "IP" },
54*cb5caa98Sdjl 	{ "ipip", "IP-IP" },
55*cb5caa98Sdjl 	{ "ipcomp", "IPComp" },
56*cb5caa98Sdjl 	{ "ipv6", "IPv6" },
57*cb5caa98Sdjl 	{ "ipv6-route", "IPv6-Route" },
58*cb5caa98Sdjl 	{ "ipv6-frag", "IPv6-Frag" },
59*cb5caa98Sdjl 	{ "ipv6-icmp", "IPv6-ICMP" },
60*cb5caa98Sdjl 	{ "ipv6-nonxt", "IPv6-NoNxt" },
61*cb5caa98Sdjl 	{ "ipv6-opts", "IPv6-Opts" },
62*cb5caa98Sdjl 	{ NULL, NULL }
63*cb5caa98Sdjl };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
66*cb5caa98Sdjl  * When the data is imported by ldapaddent, it does not save the aliase in the
67*cb5caa98Sdjl  * "cn" that is same as the canonical name but only different in case.
68*cb5caa98Sdjl  * e.g.
69*cb5caa98Sdjl  * icmp		1	ICMP
70*cb5caa98Sdjl  *
71*cb5caa98Sdjl  * is saved as
72*cb5caa98Sdjl  *
73*cb5caa98Sdjl  * dn: cn=icmp, ...
74*cb5caa98Sdjl  * ...
75*cb5caa98Sdjl  * cn: icmp
76*cb5caa98Sdjl  * ...
77*cb5caa98Sdjl  *
78*cb5caa98Sdjl  * So it needs to replicate the canonical name as an alias of upper case.
79*cb5caa98Sdjl  * But some protocol does have different aliases.
80*cb5caa98Sdjl  *
81*cb5caa98Sdjl  * e.g.
82*cb5caa98Sdjl  * dn: cn=ospf, ...
83*cb5caa98Sdjl  * ...
84*cb5caa98Sdjl  * cn: ospf
85*cb5caa98Sdjl  * cn: OSPFIGP
86*cb5caa98Sdjl  * ...
87*cb5caa98Sdjl  *
88*cb5caa98Sdjl  * For many ip* protocols, the aliases are mixed cased. Maybe it's case
89*cb5caa98Sdjl  * insensitive. But this fucntion tries to restore the aliases to the original
90*cb5caa98Sdjl  * form as much as possible. If the alias can't be found in the aliases table,
91*cb5caa98Sdjl  * it assumes the alias is all upper case.
92*cb5caa98Sdjl  *
93*cb5caa98Sdjl  */
94*cb5caa98Sdjl static char *
get_alias(char * protocol)95*cb5caa98Sdjl get_alias(char *protocol) {
96*cb5caa98Sdjl 	int	i;
97*cb5caa98Sdjl 	char	*cp;
98*cb5caa98Sdjl 
99*cb5caa98Sdjl 	if (strncmp(protocol, "ip", 2) == 0) {
100*cb5caa98Sdjl 		for (i = 0; ip_aliases[i].protocol != NULL; i++) {
101*cb5caa98Sdjl 			if (strcmp(protocol, ip_aliases[i].protocol) == 0)
102*cb5caa98Sdjl 				return (ip_aliases[i].alias);
103*cb5caa98Sdjl 		}
104*cb5caa98Sdjl 		/*
105*cb5caa98Sdjl 		 * No aliase in the table. Return an all upper case aliase
106*cb5caa98Sdjl 		 */
107*cb5caa98Sdjl 		for (cp = protocol; *cp; cp++)
108*cb5caa98Sdjl 			*cp = toupper(*cp);
109*cb5caa98Sdjl 
110*cb5caa98Sdjl 		return (protocol);
111*cb5caa98Sdjl 	} else {
112*cb5caa98Sdjl 		/* Return an all upper case aliase */
113*cb5caa98Sdjl 		for (cp = protocol; *cp; cp++)
114*cb5caa98Sdjl 			*cp = toupper(*cp);
115*cb5caa98Sdjl 
116*cb5caa98Sdjl 		return (protocol);
117*cb5caa98Sdjl 	}
118*cb5caa98Sdjl 
119*cb5caa98Sdjl }
120*cb5caa98Sdjl /*
121*cb5caa98Sdjl  * _nss_ldap_protocols2str is the data marshaling method for the protocols
1227c478bd9Sstevel@tonic-gate  * getXbyY * (e.g., getbyname(), getbynumber(), getent()) backend processes.
1237c478bd9Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
124*cb5caa98Sdjl  * This method will parse the ldap search values into a file format.
125*cb5caa98Sdjl  * e.g.
126*cb5caa98Sdjl  * idrp 45 IDRP
127*cb5caa98Sdjl  * or
128*cb5caa98Sdjl  * ospf 89 OSPFIGP
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static int
_nss_ldap_protocols2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)132*cb5caa98Sdjl _nss_ldap_protocols2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
1337c478bd9Sstevel@tonic-gate {
134*cb5caa98Sdjl 	uint_t		i;
1357c478bd9Sstevel@tonic-gate 	int		nss_result;
136*cb5caa98Sdjl 	int		buflen = 0, len;
137*cb5caa98Sdjl 	char		*cname = NULL;
138*cb5caa98Sdjl 	char		*buffer = NULL, **number, *alias;
1397c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
140*cb5caa98Sdjl 	ns_ldap_attr_t	*names;
1417c478bd9Sstevel@tonic-gate 
142*cb5caa98Sdjl 	if (result == NULL)
143*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
1447c478bd9Sstevel@tonic-gate 
145*cb5caa98Sdjl 	buflen = argp->buf.buflen;
146*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
147*cb5caa98Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
148*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
149*cb5caa98Sdjl 			goto result_pls2str;
150*cb5caa98Sdjl 		}
151*cb5caa98Sdjl 		buffer = be->buffer;
152*cb5caa98Sdjl 	} else
153*cb5caa98Sdjl 		buffer = argp->buf.buffer;
154*cb5caa98Sdjl 
155*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
1567c478bd9Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
1577c478bd9Sstevel@tonic-gate 
158*cb5caa98Sdjl 	names = __ns_ldap_getAttrStruct(result->entry, _P_NAME);
159*cb5caa98Sdjl 	if (names == NULL || names->attrvalue == NULL) {
160*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
161*cb5caa98Sdjl 		goto result_pls2str;
1627c478bd9Sstevel@tonic-gate 	}
163*cb5caa98Sdjl 	/* Get the canonical name */
164*cb5caa98Sdjl 	cname = __s_api_get_canonical_name(result->entry, names, 1);
165*cb5caa98Sdjl 	if (cname == NULL || (len = strlen(cname)) < 1) {
166*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
167*cb5caa98Sdjl 		goto result_pls2str;
168*cb5caa98Sdjl 	}
169*cb5caa98Sdjl 	number = __ns_ldap_getAttr(result->entry, _P_PROTO);
170*cb5caa98Sdjl 	if (number == NULL || number[0] == NULL ||
171*cb5caa98Sdjl 			(len = strlen(number[0])) < 1) {
172*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
173*cb5caa98Sdjl 		goto result_pls2str;
174*cb5caa98Sdjl 	}
175*cb5caa98Sdjl 	len = snprintf(buffer, buflen,  "%s %s", cname, number[0]);
176*cb5caa98Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_pls2str);
177*cb5caa98Sdjl 	/* Append aliases */
178*cb5caa98Sdjl 	if (names->value_count == 1) {
179*cb5caa98Sdjl 		/* create an aliase from protocol name */
180*cb5caa98Sdjl 		alias = get_alias(cname);
181*cb5caa98Sdjl 		len = snprintf(buffer, buflen,  " %s", alias);
182*cb5caa98Sdjl 		TEST_AND_ADJUST(len, buffer, buflen, result_pls2str);
183*cb5caa98Sdjl 
184*cb5caa98Sdjl 	} else {
185*cb5caa98Sdjl 		for (i = 0; i < names->value_count; i++) {
186*cb5caa98Sdjl 			if (names->attrvalue[i] == NULL) {
187*cb5caa98Sdjl 				nss_result = NSS_STR_PARSE_PARSE;
188*cb5caa98Sdjl 				goto result_pls2str;
1897c478bd9Sstevel@tonic-gate 			}
190*cb5caa98Sdjl 			/* Skip the canonical name */
191*cb5caa98Sdjl 			if (strcasecmp(names->attrvalue[i], cname) != 0) {
192*cb5caa98Sdjl 				len = snprintf(buffer, buflen,  " %s",
193*cb5caa98Sdjl 						names->attrvalue[i]);
194*cb5caa98Sdjl 				TEST_AND_ADJUST(len, buffer, buflen,
195*cb5caa98Sdjl 						result_pls2str);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
200*cb5caa98Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
201*cb5caa98Sdjl 	if (argp->buf.result != NULL)
202*cb5caa98Sdjl 		be->buflen = strlen(be->buffer);
2037c478bd9Sstevel@tonic-gate 
204*cb5caa98Sdjl result_pls2str:
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
2077c478bd9Sstevel@tonic-gate 	return ((int)nss_result);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * getbyname gets struct protoent values by protocol name. This
2137c478bd9Sstevel@tonic-gate  * function constructs an ldap search filter using the protocol
2147c478bd9Sstevel@tonic-gate  * name invocation parameter and the getprotobyname search filter
2157c478bd9Sstevel@tonic-gate  * defined. Once the filter is constructed, we search for a matching
2167c478bd9Sstevel@tonic-gate  * entry and marshal the data results into *proto = (struct *
2177c478bd9Sstevel@tonic-gate  * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent
2187c478bd9Sstevel@tonic-gate  * performs the data marshaling.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)2227c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2257c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2267c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2277c478bd9Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
2287c478bd9Sstevel@tonic-gate 	int		ret;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
2317c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2347c478bd9Sstevel@tonic-gate 	    _F_GETPROTOBYNAME, name);
2357c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2367c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2397c478bd9Sstevel@tonic-gate 	    _F_GETPROTOBYNAME_SSD, name);
2407c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2417c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2447c478bd9Sstevel@tonic-gate 		_PROTOCOLS, searchfilter, NULL,
2457c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * getbynumber gets struct protoent values by protocol number. This
2517c478bd9Sstevel@tonic-gate  * function constructs an ldap search filter using the protocol
2527c478bd9Sstevel@tonic-gate  * name invocation parameter and the getprotobynumber search filter
2537c478bd9Sstevel@tonic-gate  * defined. Once the filter is constructed, we search for a matching
2547c478bd9Sstevel@tonic-gate  * entry and marshal the data results into *proto = (struct *
2557c478bd9Sstevel@tonic-gate  * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent
2567c478bd9Sstevel@tonic-gate  * performs the data marshaling.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static nss_status_t
getbynumber(ldap_backend_ptr be,void * a)2607c478bd9Sstevel@tonic-gate getbynumber(ldap_backend_ptr be, void *a)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2637c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2647c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2657c478bd9Sstevel@tonic-gate 	int		ret;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2687c478bd9Sstevel@tonic-gate 	    _F_GETPROTOBYNUMBER, argp->key.number);
2697c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2707c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2737c478bd9Sstevel@tonic-gate 	    _F_GETPROTOBYNUMBER_SSD, argp->key.number);
2747c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2757c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2787c478bd9Sstevel@tonic-gate 		_PROTOCOLS, searchfilter, NULL,
2797c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static ldap_backend_op_t proto_ops[] = {
2837c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
2847c478bd9Sstevel@tonic-gate 	_nss_ldap_endent,
2857c478bd9Sstevel@tonic-gate 	_nss_ldap_setent,
2867c478bd9Sstevel@tonic-gate 	_nss_ldap_getent,
2877c478bd9Sstevel@tonic-gate 	getbyname,
2887c478bd9Sstevel@tonic-gate 	getbynumber
2897c478bd9Sstevel@tonic-gate };
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * _nss_ldap_protocols_constr is where life begins. This function calls
2947c478bd9Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
2957c478bd9Sstevel@tonic-gate  * data types required to support ldap operations.
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2997c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_ldap_protocols_constr(const char * dummy1,const char * dummy2,const char * dummy3)3007c478bd9Sstevel@tonic-gate _nss_ldap_protocols_constr(const char *dummy1, const char *dummy2,
3017c478bd9Sstevel@tonic-gate 			const char *dummy3)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(proto_ops,
3057c478bd9Sstevel@tonic-gate 		sizeof (proto_ops)/sizeof (proto_ops[0]), _PROTOCOLS,
306*cb5caa98Sdjl 		protocols_attrs, _nss_ldap_protocols2str));
3077c478bd9Sstevel@tonic-gate }
308