xref: /titanic_41/usr/src/lib/libsldap/common/ns_getalias.c (revision 721df37ded2cce623b0249cc35a872c3ce291b9b)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26  */
27 
28 #include <stdlib.h>
29 #include <libintl.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <strings.h>
33 #include "ns_sldap.h"
34 #include "ns_internal.h"
35 
36 /*
37  * getldaplaliasbyname() retrieves the aliases information from the LDAP server.
38  * This is requires that the LDAP naming information (ie. LDAP_CLIENT_CACHE
39  * file) is configured properly on the client machine.
40  *
41  * Return value:
42  *      0 = success;
43  *      1 = alias not found;
44  *      -1 = other failure.  Contents in answer are undefined.
45  */
46 
47 #define	ALIAS_FILTER	 "(&(objectclass=mailgroup)(|(cn=%s)(mail=%s)))"
48 #define	ALIAS_FILTER_SSD "(&(%%s)(|(cn=%s)(mail=%s)))"
49 #define	MAIL_CN		"cn"
50 #define	MAIL_ATTRIBUTE	"mail"
51 #define	MAIL_MEMBER	"mgrpRFC822MailMember"
52 
53 /*
54  * This is a generic filter call back function for
55  * merging the filter from service search descriptor with
56  * an existing search filter. This routine expects userdata
57  * contain a format string with a single %s in it, and will
58  * use the format string with sprintf() to insert the SSD filter.
59  *
60  * This routine is passed to the __ns_ldap_list() API as the
61  * filter call back together with filter and userdata. For example,
62  * "(&(objectclass=mailgroup)(|(cn=abc)(mail=abc)))" as filter
63  * and "(&(%s)(|(cn=abc)(mail=abc)))" as userdata.
64  * This routine will then be called by __ns_ldap_list() to output
65  * "(&(dept=sds)(|(cn=abc)(mail=abc)))" as the real search
66  * filter, if the input SSD contains a filter "dpet=sds".
67  */
68 int
69 __s_api_merge_SSD_filter(const ns_ldap_search_desc_t *desc,
70 			char **realfilter,
71 			const void *userdata)
72 {
73 	int	len;
74 	char *checker;
75 
76 	/* sanity check */
77 	if (realfilter == NULL)
78 		return (NS_LDAP_INVALID_PARAM);
79 	*realfilter = NULL;
80 
81 	if (desc == NULL || desc->filter == NULL || userdata == NULL)
82 		return (NS_LDAP_INVALID_PARAM);
83 
84 	/* Parameter check.  We only want one %s here, otherwise bail. */
85 	len = 0;	/* Reuse 'len' as "Number of %s hits"... */
86 	checker = (char *)userdata;
87 	do {
88 		checker = strchr(checker, '%');
89 		if (checker != NULL) {
90 			if (len > 0 || *(checker + 1) != 's')
91 				return (NS_LDAP_INVALID_PARAM);
92 			len++;	/* Got our %s. */
93 			checker += 2;
94 		} else if (len != 1)
95 			return (NS_LDAP_INVALID_PARAM);
96 	} while (checker != NULL);
97 
98 	len = strlen(userdata) + strlen(desc->filter) + 1;
99 
100 	*realfilter = (char *)malloc(len);
101 	if (*realfilter == NULL)
102 		return (NS_LDAP_MEMORY);
103 
104 	(void) sprintf(*realfilter, (char *)userdata, desc->filter);
105 
106 	return (NS_LDAP_SUCCESS);
107 }
108 char *
109 __getldapaliasbyname(char *alias, int *retval)
110 {
111 	char		*service = "aliases";
112 	char		filter[BUFSIZE];
113 	char		userdata[BUFSIZE];
114 	char		*attribute[2];
115 	ns_ldap_result_t	*result = NULL;
116 	ns_ldap_error_t	*errorp = NULL;
117 	int		rc, i, j, len, comma;
118 	ns_ldap_entry_t	*entry = NULL;
119 	char		**attr_value = NULL;
120 	char		*answer, *new_answer;
121 	size_t		ans_size = BUFSIZE;
122 
123 	if (!alias || !*alias) {
124 		errno = EINVAL;
125 		*retval = -1;
126 		return (NULL);
127 	}
128 
129 	answer = malloc(ans_size);
130 	if (answer == NULL) {
131 		errno = ENOMEM;
132 		*retval = -1;
133 		return (NULL);
134 	}
135 	answer[0] = '\0';
136 
137 	/* get the aliases */
138 	if (snprintf(filter, sizeof (filter), ALIAS_FILTER, alias, alias) < 0) {
139 		errno = EINVAL;
140 		*retval = -1;
141 		return (NULL);
142 	}
143 
144 	/* get the userdata for __ns_ldap_list filter call back */
145 	if (snprintf(userdata, sizeof (userdata), ALIAS_FILTER_SSD,
146 	    alias, alias) < 0) {
147 		errno = EINVAL;
148 		*retval = -1;
149 		return (NULL);
150 	}
151 
152 	attribute[0] = MAIL_MEMBER;
153 	attribute[1] = NULL;
154 
155 	/* should we do hardlookup */
156 	rc = __ns_ldap_list(service, (const char *)filter,
157 	    __s_api_merge_SSD_filter,
158 	    (const char **)attribute, NULL, 0, &result,
159 	    &errorp, NULL, userdata);
160 
161 	if (rc == NS_LDAP_NOTFOUND) {
162 		errno = ENOENT;
163 		*retval = 1;
164 		return (NULL);
165 	} else if (rc != NS_LDAP_SUCCESS) {
166 #ifdef DEBUG
167 		char *p;
168 		(void) __ns_ldap_err2str(rc, &p);
169 		if (errorp) {
170 			if (errorp->message)
171 				(void) fprintf(stderr, "%s (%s)\n", p,
172 				    errorp->message);
173 		} else
174 			(void) fprintf(stderr, "%s\n", p);
175 #endif /* DEBUG */
176 		(void) __ns_ldap_freeError(&errorp);
177 		*retval = -1;
178 		return (NULL);
179 	}
180 
181 	/* build the return value */
182 	answer[0] = '\0';
183 	len = 0;
184 	comma = 0;
185 	entry = result->entry;
186 	for (i = 0; i < result->entries_count; i++) {
187 		attr_value = __ns_ldap_getAttr(entry, MAIL_MEMBER);
188 		if (attr_value == NULL) {
189 			errno = ENOENT;
190 			*retval = -1;
191 			return (NULL);
192 		}
193 		for (j = 0; attr_value[j]; j++) {
194 			char	*tmp, *newhead;
195 
196 			tmp = attr_value[j];
197 			while (*tmp == ' ' || *tmp == '\t' && *tmp != '\0')
198 				tmp++;
199 			newhead = tmp;
200 			while (*tmp != '\0') tmp++;
201 			while (*tmp == ' ' || *tmp == '\t' || *tmp == '\0' &&
202 			    tmp != newhead) {
203 				*tmp-- = '\0';
204 			}
205 			len = len + comma + strlen(newhead);
206 			if ((len + 1) > ans_size) {
207 				ans_size += BUFSIZE;
208 				new_answer = realloc(answer, ans_size);
209 				if (new_answer == NULL) {
210 					(void) __ns_ldap_freeResult(&result);
211 					errno = ENOMEM;
212 					*retval = -1;
213 					free(answer);
214 					return (NULL);
215 				}
216 				answer = new_answer;
217 			}
218 			if (comma)
219 				(void) strcat(answer, ",");
220 			else
221 				comma = 1;
222 			(void) strcat(answer, newhead);
223 		}
224 	}
225 
226 	(void) __ns_ldap_freeResult(&result);
227 	errno = 0;
228 	*retval = 0;
229 	return (answer);
230 }
231