1 /* ========================================================================
2 * Copyright 2006-2008 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * ========================================================================
16 #include "../../../c-client/c-client.h"
18 #include "../../../pith/state.h"
19 #include "../../../pith/debug.h"
20 #include "../../../pith/adrbklib.h"
21 #include "../../../pith/ldap.h"
29 ldap_addr_select(struct pine
*ps
, ADDR_CHOOSE_S
*ac
, LDAP_CHOOSE_S
**result
,
30 LDAPLookupStyle style
, WP_ERR_S
*wp_err
, char *srchstr
)
32 LDAP_SERV_RES_S
*res_list
, *tmp_rl
;
33 LDAPMessage
*e
, *tmp_e
;
34 struct berval
**mail
= NULL
;
36 int got_n_entries
= 0, retval
= -5;
39 dprint((7, "ldap_addr_select, srchstr: %s", srchstr
));
40 for(res_list
= ac
->res_head
; res_list
; res_list
= res_list
->next
){
42 for(e
= ldap_first_entry(res_list
->ld
, res_list
->res
);
44 e
= ldap_next_entry(res_list
->ld
, e
)){
49 if(got_n_entries
== 1){
50 for(a
= ldap_first_attribute(tmp_rl
->ld
, tmp_e
, &ber
);
52 a
= ldap_next_attribute(tmp_rl
->ld
, tmp_e
, ber
)){
53 if(strcmp(a
, tmp_rl
->info_used
->mailattr
) == 0){
54 mail
= ldap_get_values_len(tmp_rl
->ld
, tmp_e
, a
);
58 if(ALPINE_LDAP_can_use(mail
)){
62 (LDAP_CHOOSE_S
*)fs_get(sizeof(LDAP_CHOOSE_S
));
63 (*result
)->ld
= tmp_rl
->ld
;
64 (*result
)->selected_entry
= tmp_e
;
65 (*result
)->info_used
= tmp_rl
->info_used
;
66 (*result
)->serv
= tmp_rl
->serv
;
81 peLdapPname(char *mailbox
, char *host
)
85 struct berval
**cn
= NULL
;
87 CUSTOM_FILT_S
*filter
;
89 LDAP_CHOOSE_S
*winning_e
= NULL
;
90 LDAP_SERV_RES_S
*results
= NULL
;
94 sprintf(adrstr
, "(mail=%.500s@%.500s)", mailbox
, host
);
95 filter
= (CUSTOM_FILT_S
*)fs_get(sizeof(CUSTOM_FILT_S
));
96 filter
->filt
= cpystr(adrstr
);
98 memset(&wp_err
, 0, sizeof(wp_err
));
99 wpldap_global
->query_no
++;
100 if(wpldap_global
->ldap_search_list
){
101 wpldap_global
->ldap_search_list
=
102 free_wpldapres(wpldap_global
->ldap_search_list
);
104 ldap_lookup_all("", 0, 0, AlwaysDisplay
, filter
, &winning_e
,
107 fs_give((void **)&filter
->filt
);
108 fs_give((void **)&filter
);
110 if(wpldap_global
->ldap_search_list
){
111 trl
= wpldap_global
->ldap_search_list
->reslist
;
112 for(ecnt
= 0, e
= ldap_first_entry(trl
->ld
, trl
->res
);
113 e
!= NULL
; e
= ldap_next_entry(trl
->ld
, e
), ecnt
++);
114 if(ecnt
== 1) { /* found the one true name */
115 e
= ldap_first_entry(trl
->ld
, trl
->res
);
116 peLdapEntryParse(trl
, e
, &cn
, NULL
, NULL
, NULL
,
119 retstr
= cpystr(cn
[0]->bv_val
);
120 ldap_value_free_len(cn
);
128 peLdapEntryParse(LDAP_SERV_RES_S
*trl
, LDAPMessage
*e
, struct berval
***ret_cn
,
129 struct berval
***ret_org
, struct berval
***ret_unit
,
130 struct berval
***ret_title
, struct berval
***ret_mail
,
131 struct berval
***ret_sn
)
134 struct berval
**cn
, **org
, **unit
, **title
, **mail
, **sn
;
137 cn
= org
= title
= unit
= mail
= sn
= NULL
;
139 for(a
= ldap_first_attribute(trl
->ld
, e
, &ber
);
141 a
= ldap_next_attribute(trl
->ld
, e
, ber
)){
142 dprint((9, " %s", a
));
143 if(strcmp(a
, trl
->info_used
->cnattr
) == 0){
145 cn
= ldap_get_values_len(trl
->ld
, e
, a
);
147 if(cn
&& !ALPINE_LDAP_can_use(cn
)){
148 ldap_value_free_len(cn
);
152 else if(strcmp(a
, trl
->info_used
->mailattr
) == 0){
154 mail
= ldap_get_values_len(trl
->ld
, e
, a
);
156 else if(strcmp(a
, "o") == 0){
158 org
= ldap_get_values_len(trl
->ld
, e
, a
);
160 else if(strcmp(a
, "ou") == 0){
162 unit
= ldap_get_values_len(trl
->ld
, e
, a
);
164 else if(strcmp(a
, "title") == 0){
166 title
= ldap_get_values_len(trl
->ld
, e
, a
);
173 for(a
= ldap_first_attribute(trl
->ld
, e
, &ber
);
175 a
= ldap_next_attribute(trl
->ld
, e
, ber
)){
177 if(strcmp(a
, trl
->info_used
->snattr
) == 0){
179 sn
= ldap_get_values_len(trl
->ld
, e
, a
);
181 if(sn
&& !ALPINE_LDAP_can_use(sn
)){
182 ldap_value_free_len(sn
);
191 else if(cn
) ldap_value_free_len(cn
);
194 else if(org
) ldap_value_free_len(org
);
197 else if(unit
) ldap_value_free_len(unit
);
199 (*ret_title
) = title
;
200 else if(title
) ldap_value_free_len(title
);
203 else if(mail
) ldap_value_free_len(mail
);
206 else if(sn
) ldap_value_free_len(sn
);
212 free_wpldapres(WPLDAPRES_S
*wpldapr
)
214 WPLDAPRES_S
*tmp1
, *tmp2
;
216 for(tmp1
= wpldapr
; tmp1
; tmp1
= tmp2
){
219 fs_give((void **)&tmp1
->str
);
221 free_ldap_result_list(&tmp1
->reslist
);
223 fs_give((void **)&wpldapr
);
226 #endif /* ENABLE_LDAP */