1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: ldap.c 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $";
5 /* ========================================================================
6 * Copyright 2006-2008 University of Washington
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * ========================================================================
20 #include "../../../c-client/c-client.h"
22 #include "../../../pith/state.h"
23 #include "../../../pith/debug.h"
24 #include "../../../pith/adrbklib.h"
25 #include "../../../pith/ldap.h"
33 ldap_addr_select(ps
, ac
, result
, style
, wp_err
, srchstr
)
36 LDAP_CHOOSE_S
**result
;
37 LDAPLookupStyle style
;
41 LDAP_SERV_RES_S
*res_list
, *tmp_rl
;
42 LDAPMessage
*e
, *tmp_e
;
43 struct berval
**mail
= NULL
;
45 int got_n_entries
= 0, retval
= -5;
48 dprint((7, "ldap_addr_select, srchstr: %s", srchstr
));
49 for(res_list
= ac
->res_head
; res_list
; res_list
= res_list
->next
){
51 for(e
= ldap_first_entry(res_list
->ld
, res_list
->res
);
53 e
= ldap_next_entry(res_list
->ld
, e
)){
58 if(got_n_entries
== 1){
59 for(a
= ldap_first_attribute(tmp_rl
->ld
, tmp_e
, &ber
);
61 a
= ldap_next_attribute(tmp_rl
->ld
, tmp_e
, ber
)){
62 if(strcmp(a
, tmp_rl
->info_used
->mailattr
) == 0){
63 mail
= ldap_get_values_len(tmp_rl
->ld
, tmp_e
, a
);
67 if(ALPINE_LDAP_can_use(mail
)){
71 (LDAP_CHOOSE_S
*)fs_get(sizeof(LDAP_CHOOSE_S
));
72 (*result
)->ld
= tmp_rl
->ld
;
73 (*result
)->selected_entry
= tmp_e
;
74 (*result
)->info_used
= tmp_rl
->info_used
;
75 (*result
)->serv
= tmp_rl
->serv
;
90 peLdapPname(mailbox
, host
)
96 struct berval
**cn
= NULL
;
98 CUSTOM_FILT_S
*filter
;
100 LDAP_CHOOSE_S
*winning_e
= NULL
;
101 LDAP_SERV_RES_S
*results
= NULL
;
102 LDAP_SERV_RES_S
*trl
;
105 sprintf(adrstr
, "(mail=%.500s@%.500s)", mailbox
, host
);
106 filter
= (CUSTOM_FILT_S
*)fs_get(sizeof(CUSTOM_FILT_S
));
107 filter
->filt
= cpystr(adrstr
);
109 memset(&wp_err
, 0, sizeof(wp_err
));
110 wpldap_global
->query_no
++;
111 if(wpldap_global
->ldap_search_list
){
112 wpldap_global
->ldap_search_list
=
113 free_wpldapres(wpldap_global
->ldap_search_list
);
115 ldap_lookup_all("", 0, 0, AlwaysDisplay
, filter
, &winning_e
,
118 fs_give((void **)&filter
->filt
);
119 fs_give((void **)&filter
);
121 if(wpldap_global
->ldap_search_list
){
122 trl
= wpldap_global
->ldap_search_list
->reslist
;
123 for(ecnt
= 0, e
= ldap_first_entry(trl
->ld
, trl
->res
);
124 e
!= NULL
; e
= ldap_next_entry(trl
->ld
, e
), ecnt
++);
125 if(ecnt
== 1) { /* found the one true name */
126 e
= ldap_first_entry(trl
->ld
, trl
->res
);
127 peLdapEntryParse(trl
, e
, &cn
, NULL
, NULL
, NULL
,
130 retstr
= cpystr(cn
[0]->bv_val
);
131 ldap_value_free_len(cn
);
139 peLdapEntryParse(trl
, e
, ret_cn
, ret_org
, ret_unit
,
140 ret_title
, ret_mail
, ret_sn
)
141 LDAP_SERV_RES_S
*trl
;
143 struct berval
***ret_cn
;
144 struct berval
***ret_org
;
145 struct berval
***ret_unit
;
146 struct berval
***ret_title
;
147 struct berval
***ret_mail
;
148 struct berval
***ret_sn
;
151 struct berval
**cn
, **org
, **unit
, **title
, **mail
, **sn
;
154 cn
= org
= title
= unit
= mail
= sn
= NULL
;
156 for(a
= ldap_first_attribute(trl
->ld
, e
, &ber
);
158 a
= ldap_next_attribute(trl
->ld
, e
, ber
)){
159 dprint((9, " %s", a
));
160 if(strcmp(a
, trl
->info_used
->cnattr
) == 0){
162 cn
= ldap_get_values_len(trl
->ld
, e
, a
);
164 if(cn
&& !ALPINE_LDAP_can_use(cn
)){
165 ldap_value_free_len(cn
);
169 else if(strcmp(a
, trl
->info_used
->mailattr
) == 0){
171 mail
= ldap_get_values_len(trl
->ld
, e
, a
);
173 else if(strcmp(a
, "o") == 0){
175 org
= ldap_get_values_len(trl
->ld
, e
, a
);
177 else if(strcmp(a
, "ou") == 0){
179 unit
= ldap_get_values_len(trl
->ld
, e
, a
);
181 else if(strcmp(a
, "title") == 0){
183 title
= ldap_get_values_len(trl
->ld
, e
, a
);
190 for(a
= ldap_first_attribute(trl
->ld
, e
, &ber
);
192 a
= ldap_next_attribute(trl
->ld
, e
, ber
)){
194 if(strcmp(a
, trl
->info_used
->snattr
) == 0){
196 sn
= ldap_get_values_len(trl
->ld
, e
, a
);
198 if(sn
&& !ALPINE_LDAP_can_use(sn
)){
199 ldap_value_free_len(sn
);
208 else if(cn
) ldap_value_free_len(cn
);
211 else if(org
) ldap_value_free_len(org
);
214 else if(unit
) ldap_value_free_len(unit
);
216 (*ret_title
) = title
;
217 else if(title
) ldap_value_free_len(title
);
220 else if(mail
) ldap_value_free_len(mail
);
223 else if(sn
) ldap_value_free_len(sn
);
229 free_wpldapres(wpldapr
)
230 WPLDAPRES_S
*wpldapr
;
232 WPLDAPRES_S
*tmp1
, *tmp2
;
234 for(tmp1
= wpldapr
; tmp1
; tmp1
= tmp2
){
237 fs_give((void **)&tmp1
->str
);
239 free_ldap_result_list(&tmp1
->reslist
);
241 fs_give((void **)&wpldapr
);
244 #endif /* ENABLE_LDAP */