xref: /titanic_52/usr/src/lib/libsldap/common/ns_getalias.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <strings.h>
34 #include "ns_sldap.h"
35 #include "ns_internal.h"
36 
37 /*
38  * getldaplaliasbyname() retrieves the aliases information from the LDAP server.
39  * This is requires that the LDAP naming information (ie. LDAP_CLIENT_CACHE
40  * file) is configured properly on the client machine.
41  *
42  * Return value:
43  *      0 = success;
44  *      1 = alias not found;
45  *      -1 = other failure.  Contents in answer are undefined.
46  */
47 
48 #define	ALIAS_FILTER	 "(&(objectclass=mailgroup)(|(cn=%s)(mail=%s)))"
49 #define	ALIAS_FILTER_SSD "(&(%%s)(|(cn=%s)(mail=%s)))"
50 #define	MAIL_CN		"cn"
51 #define	MAIL_ATTRIBUTE	"mail"
52 #define	MAIL_MEMBER	"mgrpRFC822MailMember"
53 
54 /*
55  * This is a generic filter call back function for
56  * merging the filter from service search descriptor with
57  * an existing search filter. This routine expects userdata
58  * contain a format string with a single %s in it, and will
59  * use the format string with sprintf() to insert the SSD filter.
60  *
61  * This routine is passed to the __ns_ldap_list() API as the
62  * filter call back together with filter and userdata. For example,
63  * "(&(objectclass=mailgroup)(|(cn=abc)(mail=abc)))" as filter
64  * and "(&(%s)(|(cn=abc)(mail=abc)))" as userdata.
65  * This routine will then be called by __ns_ldap_list() to output
66  * "(&(dept=sds)(|(cn=abc)(mail=abc)))" as the real search
67  * filter, if the input SSD contains a filter "dpet=sds".
68  */
69 int
70 __s_api_merge_SSD_filter(const ns_ldap_search_desc_t *desc,
71 			char **realfilter,
72 			const void *userdata)
73 {
74 	int	len;
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 ||
82 			userdata == NULL)
83 		return (NS_LDAP_INVALID_PARAM);
84 
85 	len = strlen(userdata) + strlen(desc->filter) + 1;
86 
87 	*realfilter = (char *)malloc(len);
88 	if (*realfilter == NULL)
89 		return (NS_LDAP_MEMORY);
90 
91 	(void) sprintf(*realfilter, (char *)userdata,
92 			desc->filter);
93 
94 	return (NS_LDAP_SUCCESS);
95 }
96 int
97 __getldapaliasbyname(char *alias, char *answer, size_t ans_len)
98 {
99 	char		*service = "aliases";
100 	char		filter[BUFSIZE];
101 	char		userdata[BUFSIZE];
102 	char		*attribute[2];
103 	ns_ldap_result_t	*result = NULL;
104 	ns_ldap_error_t	*errorp = NULL;
105 	int		rc, i, j, len, comma;
106 	ns_ldap_entry_t	*entry = NULL;
107 	char		**attr_value = NULL;
108 
109 	if (!alias || !*alias || !answer || ans_len == 0) {
110 		errno = EINVAL;
111 		return (-1);
112 	}
113 
114 	answer[0] = '\0';
115 
116 	/* get the aliases */
117 	if (snprintf(filter, sizeof (filter), ALIAS_FILTER, alias, alias) < 0) {
118 		errno = EINVAL;
119 		return (-1);
120 	}
121 
122 	/* get the userdata for __ns_ldap_list filter call back */
123 	if (snprintf(userdata, sizeof (userdata), ALIAS_FILTER_SSD,
124 	    alias, alias) < 0) {
125 		errno = EINVAL;
126 		return (-1);
127 	}
128 
129 	attribute[0] = MAIL_MEMBER;
130 	attribute[1] = NULL;
131 
132 	/* should we do hardlookup */
133 	rc = __ns_ldap_list(service, (const char *)filter,
134 		__s_api_merge_SSD_filter,
135 		(const char **)attribute, NULL, 0, &result,
136 		&errorp, NULL, userdata);
137 
138 	if (rc == NS_LDAP_NOTFOUND) {
139 		errno = ENOENT;
140 		return (1);
141 	} else if (rc != NS_LDAP_SUCCESS) {
142 #ifdef DEBUG
143 		char *p;
144 		(void) __ns_ldap_err2str(rc, &p);
145 		if (errorp) {
146 			if (errorp->message)
147 				(void) fprintf(stderr, "%s (%s)\n", p,
148 					errorp->message);
149 		} else
150 			(void) fprintf(stderr, "%s\n", p);
151 #endif /* DEBUG */
152 		(void) __ns_ldap_freeError(&errorp);
153 		return (-1);
154 	}
155 
156 	/* build the return value */
157 	answer[0] = '\0';
158 	len = 0;
159 	comma = 0;
160 	entry = result->entry;
161 	for (i = 0; i < result->entries_count; i++) {
162 		attr_value = __ns_ldap_getAttr(entry, MAIL_MEMBER);
163 		if (attr_value == NULL) {
164 			errno = ENOENT;
165 			return (-1);
166 		}
167 		for (j = 0; attr_value[j]; j++) {
168 			char	*tmp, *newhead;
169 
170 			tmp = attr_value[j];
171 			while (*tmp == ' ' || *tmp == '\t' && *tmp != '\0')
172 				tmp++;
173 			newhead = tmp;
174 			while (*tmp != '\0') tmp++;
175 			while (*tmp == ' ' || *tmp == '\t' || *tmp == '\0' &&
176 			    tmp != newhead) {
177 				*tmp-- = '\0';
178 			}
179 			len = len + comma + strlen(newhead);
180 			if ((len + 1) > ans_len) {
181 				(void) __ns_ldap_freeResult(&result);
182 				errno = EOVERFLOW;
183 				return (-1);
184 			}
185 			if (comma)
186 				(void) strcat(answer, ",");
187 			else
188 				comma = 1;
189 			(void) strcat(answer, newhead);
190 		}
191 	}
192 
193 	(void) __ns_ldap_freeResult(&result);
194 	errno = 0;
195 	return (0);
196 }
197