* clear out some warnings by gcc 9.3.1.
[alpine.git] / web / src / alpined.d / ldap.c
blob4107b375d90712c8a637afa302addf05e177348f
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: ldap.c 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $";
3 #endif
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 * ========================================================================
17 #include <system.h>
18 #include <general.h>
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"
27 #include "ldap.h"
30 #ifdef ENABLE_LDAP
32 int
33 ldap_addr_select(ps, ac, result, style, wp_err, srchstr)
34 struct pine *ps;
35 ADDR_CHOOSE_S *ac;
36 LDAP_CHOOSE_S **result;
37 LDAPLookupStyle style;
38 WP_ERR_S *wp_err;
39 char *srchstr;
41 LDAP_SERV_RES_S *res_list, *tmp_rl;
42 LDAPMessage *e, *tmp_e;
43 struct berval **mail = NULL;
44 char *a;
45 int got_n_entries = 0, retval = -5;
46 BerElement *ber;
48 dprint((7, "ldap_addr_select, srchstr: %s", srchstr));
49 for(res_list = ac->res_head; res_list; res_list = res_list->next){
50 tmp_rl = res_list;
51 for(e = ldap_first_entry(res_list->ld, res_list->res);
52 e != NULL;
53 e = ldap_next_entry(res_list->ld, e)){
54 tmp_e = e;
55 got_n_entries++;
58 if(got_n_entries == 1){
59 for(a = ldap_first_attribute(tmp_rl->ld, tmp_e, &ber);
60 a != NULL;
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);
64 break;
67 if(ALPINE_LDAP_can_use(mail)){
68 retval = 0;
69 if(result){
70 (*result) =
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;
78 else{
79 retval = -2;
82 else
83 retval = -3;
85 return(retval);
89 char *
90 peLdapPname(mailbox, host)
91 char *mailbox;
92 char *host;
94 char *retstr = NULL;
95 char adrstr[1024];
96 struct berval **cn = NULL;
97 int ecnt;
98 CUSTOM_FILT_S *filter;
99 WP_ERR_S wp_err;
100 LDAP_CHOOSE_S *winning_e = NULL;
101 LDAP_SERV_RES_S *results = NULL;
102 LDAP_SERV_RES_S *trl;
103 LDAPMessage *e;
105 sprintf(adrstr, "(mail=%.500s@%.500s)", mailbox, host);
106 filter = (CUSTOM_FILT_S *)fs_get(sizeof(CUSTOM_FILT_S));
107 filter->filt = cpystr(adrstr);
108 filter->combine = 0;
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,
116 &wp_err, &results);
117 if(filter){
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,
128 NULL, NULL);
129 if(cn){
130 retstr = cpystr(cn[0]->bv_val);
131 ldap_value_free_len(cn);
135 return(retstr);
139 peLdapEntryParse(trl, e, ret_cn, ret_org, ret_unit,
140 ret_title, ret_mail, ret_sn)
141 LDAP_SERV_RES_S *trl;
142 LDAPMessage *e;
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;
150 char *a;
151 struct berval **cn, **org, **unit, **title, **mail, **sn;
152 BerElement *ber;
154 cn = org = title = unit = mail = sn = NULL;
156 for(a = ldap_first_attribute(trl->ld, e, &ber);
157 a != NULL;
158 a = ldap_next_attribute(trl->ld, e, ber)){
159 dprint((9, " %s", a));
160 if(strcmp(a, trl->info_used->cnattr) == 0){
161 if(!cn)
162 cn = ldap_get_values_len(trl->ld, e, a);
164 if(cn && !ALPINE_LDAP_can_use(cn)){
165 ldap_value_free_len(cn);
166 cn = NULL;
169 else if(strcmp(a, trl->info_used->mailattr) == 0){
170 if(!mail)
171 mail = ldap_get_values_len(trl->ld, e, a);
173 else if(strcmp(a, "o") == 0){
174 if(!org)
175 org = ldap_get_values_len(trl->ld, e, a);
177 else if(strcmp(a, "ou") == 0){
178 if(!unit)
179 unit = ldap_get_values_len(trl->ld, e, a);
181 else if(strcmp(a, "title") == 0){
182 if(!title)
183 title = ldap_get_values_len(trl->ld, e, a);
186 our_ldap_memfree(a);
189 if(!cn){
190 for(a = ldap_first_attribute(trl->ld, e, &ber);
191 a != NULL;
192 a = ldap_next_attribute(trl->ld, e, ber)){
194 if(strcmp(a, trl->info_used->snattr) == 0){
195 if(!sn)
196 sn = ldap_get_values_len(trl->ld, e, a);
198 if(sn && !ALPINE_LDAP_can_use(sn)){
199 ldap_value_free_len(sn);
200 sn = NULL;
203 our_ldap_memfree(a);
206 if(ret_cn)
207 (*ret_cn) = cn;
208 else if(cn) ldap_value_free_len(cn);
209 if(ret_org)
210 (*ret_org) = org;
211 else if(org) ldap_value_free_len(org);
212 if(ret_unit)
213 (*ret_unit) = unit;
214 else if(unit) ldap_value_free_len(unit);
215 if(ret_title)
216 (*ret_title) = title;
217 else if(title) ldap_value_free_len(title);
218 if(ret_mail)
219 (*ret_mail) = mail;
220 else if(mail) ldap_value_free_len(mail);
221 if(ret_sn)
222 (*ret_sn) = sn;
223 else if(sn) ldap_value_free_len(sn);
225 return 0;
228 WPLDAPRES_S *
229 free_wpldapres(wpldapr)
230 WPLDAPRES_S *wpldapr;
232 WPLDAPRES_S *tmp1, *tmp2;
234 for(tmp1 = wpldapr; tmp1; tmp1 = tmp2){
235 tmp2 = tmp1->next;
236 if(tmp1->str)
237 fs_give((void **)&tmp1->str);
238 if(tmp1->reslist)
239 free_ldap_result_list(&tmp1->reslist);
241 fs_give((void **)&wpldapr);
242 return(NULL);
244 #endif /* ENABLE_LDAP */