* Changes in the source code of Alpine to define internal prototypes
[alpine.git] / web / src / alpined.d / ldap.c
blob1fe5d81126b961f0a2cc57ebab5e0fe25700baea
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 * ========================================================================
13 #include <system.h>
14 #include <general.h>
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"
23 #include "ldap.h"
26 #ifdef ENABLE_LDAP
28 int
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;
35 char *a;
36 int got_n_entries = 0, retval = -5;
37 BerElement *ber;
39 dprint((7, "ldap_addr_select, srchstr: %s", srchstr));
40 for(res_list = ac->res_head; res_list; res_list = res_list->next){
41 tmp_rl = res_list;
42 for(e = ldap_first_entry(res_list->ld, res_list->res);
43 e != NULL;
44 e = ldap_next_entry(res_list->ld, e)){
45 tmp_e = e;
46 got_n_entries++;
49 if(got_n_entries == 1){
50 for(a = ldap_first_attribute(tmp_rl->ld, tmp_e, &ber);
51 a != NULL;
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);
55 break;
58 if(ALPINE_LDAP_can_use(mail)){
59 retval = 0;
60 if(result){
61 (*result) =
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;
69 else{
70 retval = -2;
73 else
74 retval = -3;
76 return(retval);
80 char *
81 peLdapPname(char *mailbox, char *host)
83 char *retstr = NULL;
84 char adrstr[1024];
85 struct berval **cn = NULL;
86 int ecnt;
87 CUSTOM_FILT_S *filter;
88 WP_ERR_S wp_err;
89 LDAP_CHOOSE_S *winning_e = NULL;
90 LDAP_SERV_RES_S *results = NULL;
91 LDAP_SERV_RES_S *trl;
92 LDAPMessage *e;
94 sprintf(adrstr, "(mail=%.500s@%.500s)", mailbox, host);
95 filter = (CUSTOM_FILT_S *)fs_get(sizeof(CUSTOM_FILT_S));
96 filter->filt = cpystr(adrstr);
97 filter->combine = 0;
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,
105 &wp_err, &results);
106 if(filter){
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,
117 NULL, NULL);
118 if(cn){
119 retstr = cpystr(cn[0]->bv_val);
120 ldap_value_free_len(cn);
124 return(retstr);
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)
133 char *a;
134 struct berval **cn, **org, **unit, **title, **mail, **sn;
135 BerElement *ber;
137 cn = org = title = unit = mail = sn = NULL;
139 for(a = ldap_first_attribute(trl->ld, e, &ber);
140 a != NULL;
141 a = ldap_next_attribute(trl->ld, e, ber)){
142 dprint((9, " %s", a));
143 if(strcmp(a, trl->info_used->cnattr) == 0){
144 if(!cn)
145 cn = ldap_get_values_len(trl->ld, e, a);
147 if(cn && !ALPINE_LDAP_can_use(cn)){
148 ldap_value_free_len(cn);
149 cn = NULL;
152 else if(strcmp(a, trl->info_used->mailattr) == 0){
153 if(!mail)
154 mail = ldap_get_values_len(trl->ld, e, a);
156 else if(strcmp(a, "o") == 0){
157 if(!org)
158 org = ldap_get_values_len(trl->ld, e, a);
160 else if(strcmp(a, "ou") == 0){
161 if(!unit)
162 unit = ldap_get_values_len(trl->ld, e, a);
164 else if(strcmp(a, "title") == 0){
165 if(!title)
166 title = ldap_get_values_len(trl->ld, e, a);
169 our_ldap_memfree(a);
172 if(!cn){
173 for(a = ldap_first_attribute(trl->ld, e, &ber);
174 a != NULL;
175 a = ldap_next_attribute(trl->ld, e, ber)){
177 if(strcmp(a, trl->info_used->snattr) == 0){
178 if(!sn)
179 sn = ldap_get_values_len(trl->ld, e, a);
181 if(sn && !ALPINE_LDAP_can_use(sn)){
182 ldap_value_free_len(sn);
183 sn = NULL;
186 our_ldap_memfree(a);
189 if(ret_cn)
190 (*ret_cn) = cn;
191 else if(cn) ldap_value_free_len(cn);
192 if(ret_org)
193 (*ret_org) = org;
194 else if(org) ldap_value_free_len(org);
195 if(ret_unit)
196 (*ret_unit) = unit;
197 else if(unit) ldap_value_free_len(unit);
198 if(ret_title)
199 (*ret_title) = title;
200 else if(title) ldap_value_free_len(title);
201 if(ret_mail)
202 (*ret_mail) = mail;
203 else if(mail) ldap_value_free_len(mail);
204 if(ret_sn)
205 (*ret_sn) = sn;
206 else if(sn) ldap_value_free_len(sn);
208 return 0;
211 WPLDAPRES_S *
212 free_wpldapres(WPLDAPRES_S *wpldapr)
214 WPLDAPRES_S *tmp1, *tmp2;
216 for(tmp1 = wpldapr; tmp1; tmp1 = tmp2){
217 tmp2 = tmp1->next;
218 if(tmp1->str)
219 fs_give((void **)&tmp1->str);
220 if(tmp1->reslist)
221 free_ldap_result_list(&tmp1->reslist);
223 fs_give((void **)&wpldapr);
224 return(NULL);
226 #endif /* ENABLE_LDAP */