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