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.
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.
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]
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.
34 #include "ns_internal.h"
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.
43 * 1 = alias not found;
44 * -1 = other failure. Contents in answer are undefined.
47 #define ALIAS_FILTER "(&(objectclass=mailgroup)(|(cn=%s)(mail=%s)))"
48 #define ALIAS_FILTER_SSD "(&(%%s)(|(cn=%s)(mail=%s)))"
50 #define MAIL_ATTRIBUTE "mail"
51 #define MAIL_MEMBER "mgrpRFC822MailMember"
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.
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".
69 __s_api_merge_SSD_filter(const ns_ldap_search_desc_t
*desc
,
77 if (realfilter
== NULL
)
78 return (NS_LDAP_INVALID_PARAM
);
81 if (desc
== NULL
|| desc
->filter
== NULL
|| userdata
== NULL
)
82 return (NS_LDAP_INVALID_PARAM
);
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
;
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. */
95 return (NS_LDAP_INVALID_PARAM
);
96 } while (checker
!= NULL
);
98 len
= strlen(userdata
) + strlen(desc
->filter
) + 1;
100 *realfilter
= (char *)malloc(len
);
101 if (*realfilter
== NULL
)
102 return (NS_LDAP_MEMORY
);
104 (void) sprintf(*realfilter
, (char *)userdata
, desc
->filter
);
106 return (NS_LDAP_SUCCESS
);
109 __getldapaliasbyname(char *alias
, int *retval
)
111 char *service
= "aliases";
112 char filter
[BUFSIZE
];
113 char userdata
[BUFSIZE
];
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
;
123 if (!alias
|| !*alias
) {
129 answer
= malloc(ans_size
);
130 if (answer
== NULL
) {
137 /* get the aliases */
138 if (snprintf(filter
, sizeof (filter
), ALIAS_FILTER
, alias
, alias
) < 0) {
144 /* get the userdata for __ns_ldap_list filter call back */
145 if (snprintf(userdata
, sizeof (userdata
), ALIAS_FILTER_SSD
,
152 attribute
[0] = MAIL_MEMBER
;
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
);
161 if (rc
== NS_LDAP_NOTFOUND
) {
165 } else if (rc
!= NS_LDAP_SUCCESS
) {
168 (void) __ns_ldap_err2str(rc
, &p
);
171 (void) fprintf(stderr
, "%s (%s)\n", p
,
174 (void) fprintf(stderr
, "%s\n", p
);
176 (void) __ns_ldap_freeError(&errorp
);
181 /* build the return value */
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
) {
193 for (j
= 0; attr_value
[j
]; j
++) {
197 while (*tmp
== ' ' || *tmp
== '\t' && *tmp
!= '\0')
200 while (*tmp
!= '\0') tmp
++;
201 while (*tmp
== ' ' || *tmp
== '\t' || *tmp
== '\0' &&
205 len
= len
+ comma
+ strlen(newhead
);
206 if ((len
+ 1) > ans_size
) {
208 new_answer
= realloc(answer
, ans_size
);
209 if (new_answer
== NULL
) {
210 (void) __ns_ldap_freeResult(&result
);
219 (void) strcat(answer
, ",");
222 (void) strcat(answer
, newhead
);
226 (void) __ns_ldap_freeResult(&result
);