]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/kerberos_ldap_group/support_resolv.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / helpers / external_acl / kerberos_ldap_group / support_resolv.cc
1 /*
2 * -----------------------------------------------------------------------------
3 *
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
5 *
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * -----------------------------------------------------------------------------
23 */
24
25 #include "squid.h"
26 #include "util.h"
27
28 #ifdef HAVE_LDAP
29
30 #include "support.h"
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_NETDB_H
35 #include <netdb.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef HAVE_RESOLV_H
41 #include <resolv.h>
42 #endif
43 #ifdef HAVE_ARPA_NAMESER_H
44 #include <arpa/nameser.h>
45 #endif
46
47 void nsError(int nserror, char *server);
48 static int compare_hosts(struct hstruct *h1, struct hstruct *h2);
49 static void swap(struct hstruct *a, struct hstruct *b);
50 static void sort(struct hstruct *array, int nitems, int (*cmp) (struct hstruct *, struct hstruct *), int begin, int end);
51 static void msort(struct hstruct *array, size_t nitems, int (*cmp) (struct hstruct *, struct hstruct *));
52
53 /*
54 * See http://www.ietf.org/rfc/rfc1035.txt
55 */
56 /*
57 * See http://www.ietf.org/rfc/rfc2782.txt
58 *
59 */
60 void
61 nsError(int nserror, char *service)
62 {
63 switch (nserror) {
64 case HOST_NOT_FOUND:
65 error((char *) "%s| %s: ERROR: res_search: Unknown service record: %s\n", LogTime(), PROGRAM, service);
66 break;
67 case NO_DATA:
68 error((char *) "%s| %s: ERROR: res_search: No SRV record for %s\n", LogTime(), PROGRAM, service);
69 break;
70 case TRY_AGAIN:
71 error((char *) "%s| %s: ERROR: res_search: No response for SRV query\n", LogTime(), PROGRAM);
72 break;
73 default:
74 error((char *) "%s| %s: ERROR: res_search: Unexpected error: %s\n", LogTime(), PROGRAM, strerror(nserror));
75 }
76 }
77
78 static void
79 swap(struct hstruct *a, struct hstruct *b)
80 {
81 struct hstruct c;
82
83 c.host = a->host;
84 c.priority = a->priority;
85 c.weight = a->weight;
86 a->host = b->host;
87 a->priority = b->priority;
88 a->weight = b->weight;
89 b->host = c.host;
90 b->priority = c.priority;
91 b->weight = c.weight;
92 }
93
94 static void
95 sort(struct hstruct *array, int nitems, int (*cmp) (struct hstruct *, struct hstruct *), int begin, int end)
96 {
97 if (end > begin) {
98 int pivot = begin;
99 int l = begin + 1;
100 int r = end;
101 while (l < r) {
102 if (cmp(&array[l], &array[pivot]) <= 0) {
103 l += 1;
104 } else {
105 r -= 1;
106 swap(&array[l], &array[r]);
107 }
108 }
109 l -= 1;
110 swap(&array[begin], &array[l]);
111 sort(array, nitems, cmp, begin, l);
112 sort(array, nitems, cmp, r, end);
113 }
114 }
115
116 static void
117 msort(struct hstruct *array, size_t nitems, int (*cmp) (struct hstruct *, struct hstruct *))
118 {
119 sort(array, nitems, cmp, 0, nitems - 1);
120 }
121
122 static int
123 compare_hosts(struct hstruct *host1, struct hstruct *host2)
124 {
125 /*
126 *
127 * The comparison function must return an integer less than, equal to,
128 * or greater than zero if the first argument is considered to be
129 * respectively less than, equal to, or greater than the second.
130 */
131 if ((host1->priority < host2->priority) && (host1->priority != -1))
132 return -1;
133 if ((host1->priority < host2->priority) && (host1->priority == -1))
134 return 1;
135 if ((host1->priority > host2->priority) && (host2->priority != -1))
136 return 1;
137 if ((host1->priority > host2->priority) && (host2->priority == -1))
138 return -1;
139 if (host1->priority == host2->priority) {
140 if (host1->weight > host2->weight)
141 return -1;
142 if (host1->weight < host2->weight)
143 return 1;
144 }
145 return 0;
146 }
147
148 int
149 free_hostname_list(struct hstruct **hlist, int nhosts)
150 {
151 struct hstruct *hp = NULL;
152 int i;
153
154 hp = *hlist;
155 for (i = 0; i < nhosts; i++) {
156 if (hp[i].host)
157 xfree(hp[i].host);
158 hp[i].host = NULL;
159 }
160
161
162 if (hp)
163 xfree(hp);
164 hp = NULL;
165 *hlist = hp;
166 return 0;
167 }
168
169 int
170 get_hostname_list(struct main_args *margs, struct hstruct **hlist, int nhosts, char *name)
171 {
172 /*
173 * char host[sysconf(_SC_HOST_NAME_MAX)];
174 */
175 char host[1024];
176 struct addrinfo *hres = NULL, *hres_list;
177 int rc, count;
178 struct hstruct *hp = NULL;
179
180 if (!name)
181 return (nhosts);
182
183 hp = *hlist;
184 rc = getaddrinfo((const char *) name, NULL, NULL, &hres);
185 if (rc != 0) {
186 error((char *) "%s| %s: ERROR: Error while resolving hostname with getaddrinfo: %s\n", LogTime(), PROGRAM, gai_strerror(rc));
187 return (nhosts);
188 }
189 hres_list = hres;
190 count = 0;
191 while (hres_list) {
192 count++;
193 hres_list = hres_list->ai_next;
194 }
195 hres_list = hres;
196 count = 0;
197 while (hres_list) {
198 rc = getnameinfo(hres_list->ai_addr, hres_list->ai_addrlen, host, sizeof(host), NULL, 0, 0);
199 if (rc != 0) {
200 error((char *) "%s| %s: ERROR: Error while resolving ip address with getnameinfo: %s\n", LogTime(), PROGRAM, gai_strerror(rc));
201 freeaddrinfo(hres);
202 *hlist = hp;
203 return (nhosts);
204 }
205 count++;
206 debug((char *) "%s| %s: DEBUG: Resolved address %d of %s to %s\n", LogTime(), PROGRAM, count, name, host);
207
208 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nhosts + 1));
209 hp[nhosts].host = xstrdup(host);
210 hp[nhosts].port = -1;
211 hp[nhosts].priority = -1;
212 hp[nhosts].weight = -1;
213 nhosts++;
214
215 hres_list = hres_list->ai_next;
216 }
217
218 freeaddrinfo(hres);
219 *hlist = hp;
220 return (nhosts);
221 }
222
223 int
224 get_ldap_hostname_list(struct main_args *margs, struct hstruct **hlist, int nh, char *domain)
225 {
226
227 /*
228 * char name[sysconf(_SC_HOST_NAME_MAX)];
229 */
230 char name[1024];
231 char host[NS_MAXDNAME];
232 char *service = NULL;
233 struct hstruct *hp = NULL;
234 struct lsstruct *ls = NULL;
235 int nhosts = 0;
236 int size;
237 int type, rdlength;
238 int priority, weight, port;
239 int len, olen;
240 int i, j, k;
241 u_char *buffer = NULL;
242 u_char *p;
243
244 ls = margs->lservs;
245 while (ls) {
246 debug((char *) "%s| %s: DEBUG: Ldap server loop: lserver@domain %s@%s\n", LogTime(), PROGRAM, ls->lserver, ls->domain?ls->domain:"NULL");
247 if (ls->domain && !strcasecmp(ls->domain, domain)) {
248 debug((char *) "%s| %s: DEBUG: Found lserver@domain %s@%s\n", LogTime(), PROGRAM, ls->lserver, ls->domain);
249 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nhosts + 1));
250 hp[nhosts].host = strdup(ls->lserver);
251 hp[nhosts].port = -1;
252 hp[nhosts].priority = -2;
253 hp[nhosts].weight = -2;
254 nhosts++;
255 } else if ( !ls->domain || !strcasecmp(ls->domain, "") ) {
256 debug((char *) "%s| %s: DEBUG: Found lserver@domain %s@%s\n", LogTime(), PROGRAM, ls->lserver, ls->domain?ls->domain:"NULL");
257 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nhosts + 1));
258 hp[nhosts].host = strdup(ls->lserver);
259 hp[nhosts].port = -1;
260 hp[nhosts].priority = -2;
261 hp[nhosts].weight = -2;
262 nhosts++;
263
264 }
265 ls = ls->next;
266 }
267 /* found ldap servers in predefined list -> exit */
268 if (nhosts > 0)
269 goto cleanup;
270
271 if (margs->ssl) {
272 service = (char *) xmalloc(strlen("_ldaps._tcp.") + strlen(domain) + 1);
273 strcpy(service, "_ldaps._tcp.");
274 } else {
275 service = (char *) xmalloc(strlen("_ldap._tcp.") + strlen(domain) + 1);
276 strcpy(service, "_ldap._tcp.");
277 }
278 strcat(service, domain);
279
280 #ifndef PACKETSZ_MULT
281 /*
282 * It seems Solaris doesn't give back the real length back when res_search uses a to small buffer
283 * Set a bigger one here
284 */
285 #define PACKETSZ_MULT 10
286 #endif
287
288 hp = *hlist;
289 buffer = (u_char *) xmalloc(PACKETSZ_MULT * NS_PACKETSZ);
290 if ((len = res_search(service, ns_c_in, ns_t_srv, (u_char *) buffer, PACKETSZ_MULT * NS_PACKETSZ)) < 0) {
291 error((char *) "%s| %s: ERROR: Error while resolving service record %s with res_search\n", LogTime(), PROGRAM, service);
292 nsError(h_errno, service);
293 if (margs->ssl) {
294 xfree(service);
295 service = (char *) xmalloc(strlen("_ldap._tcp.") + strlen(domain) + 1);
296 strcpy(service, "_ldap._tcp.");
297 strcat(service, domain);
298 if ((len = res_search(service, ns_c_in, ns_t_srv, (u_char *) buffer, PACKETSZ_MULT * NS_PACKETSZ)) < 0) {
299 error((char *) "%s| %s: ERROR: Error while resolving service record %s with res_search\n", LogTime(), PROGRAM, service);
300 nsError(h_errno, service);
301 goto cleanup;
302 }
303 } else {
304 goto cleanup;
305 }
306 }
307 if (len > PACKETSZ_MULT * NS_PACKETSZ) {
308 olen = len;
309 buffer = (u_char *) xrealloc(buffer, len);
310 if ((len = res_search(service, ns_c_in, ns_t_srv, (u_char *) buffer, len)) < 0) {
311 error((char *) "%s| %s: ERROR: Error while resolving service record %s with res_search\n", LogTime(), PROGRAM, service);
312 nsError(h_errno, service);
313 goto cleanup;
314 }
315 if (len > olen) {
316 error((char *) "%s| %s: ERROR: Reply to big: buffer: %d reply length: %d\n", LogTime(), PROGRAM, olen, len);
317 goto cleanup;
318 }
319 }
320 p = buffer;
321 p += 6 * NS_INT16SZ; /* Header(6*16bit) = id + flags + 4*section count */
322 if (p > buffer + len) {
323 error((char *) "%s| %s: ERROR: Message to small: %d < header size\n", LogTime(), PROGRAM, len);
324 goto cleanup;
325 }
326 if ((size = dn_expand(buffer, buffer + len, p, name, sysconf(_SC_HOST_NAME_MAX))) < 0) {
327 error((char *) "%s| %s: ERROR: Error while expanding query name with dn_expand: %s\n", LogTime(), PROGRAM, strerror(errno));
328 goto cleanup;
329 }
330 p += size; /* Query name */
331 p += 2 * NS_INT16SZ; /* Query type + class (2*16bit) */
332 if (p > buffer + len) {
333 error((char *) "%s| %s: ERROR: Message to small: %d < header + query name,type,class \n", LogTime(), PROGRAM, len);
334 goto cleanup;
335 }
336 while (p < buffer + len) {
337 if ((size = dn_expand(buffer, buffer + len, p, name, sysconf(_SC_HOST_NAME_MAX))) < 0) {
338 error((char *) "%s| %s: ERROR: Error while expanding answer name with dn_expand: %s\n", LogTime(), PROGRAM, strerror(errno));
339 goto cleanup;
340 }
341 p += size; /* Resource Record name */
342 if (p > buffer + len) {
343 error((char *) "%s| %s: ERROR: Message to small: %d < header + query name,type,class + answer name\n", LogTime(), PROGRAM, len);
344 goto cleanup;
345 }
346 NS_GET16(type, p); /* RR type (16bit) */
347 p += NS_INT16SZ + NS_INT32SZ; /* RR class + ttl (16bit+32bit) */
348 if (p > buffer + len) {
349 error((char *) "%s| %s: ERROR: Message to small: %d < header + query name,type,class + answer name + RR type,class,ttl\n", LogTime(), PROGRAM, len);
350 goto cleanup;
351 }
352 NS_GET16(rdlength, p); /* RR data length (16bit) */
353
354 if (type == ns_t_srv) { /* SRV record */
355 if (p > buffer + len) {
356 error((char *) "%s| %s: ERROR: Message to small: %d < header + query name,type,class + answer name + RR type,class,ttl + RR data length\n", LogTime(), PROGRAM, len);
357 goto cleanup;
358 }
359 NS_GET16(priority, p); /* Priority (16bit) */
360 if (p > buffer + len) {
361 error((char *) "%s| %s: ERROR: Message to small: %d < SRV RR + priority\n", LogTime(), PROGRAM, len);
362 goto cleanup;
363 }
364 NS_GET16(weight, p); /* Weight (16bit) */
365 if (p > buffer + len) {
366 error((char *) "%s| %s: ERROR: Message to small: %d < SRV RR + priority + weight\n", LogTime(), PROGRAM, len);
367 goto cleanup;
368 }
369 NS_GET16(port, p); /* Port (16bit) */
370 if (p > buffer + len) {
371 error((char *) "%s| %s: ERROR: Message to small: %d < SRV RR + priority + weight + port\n", LogTime(), PROGRAM, len);
372 goto cleanup;
373 }
374 if ((size = dn_expand(buffer, buffer + len, p, host, NS_MAXDNAME)) < 0) {
375 error((char *) "%s| %s: ERROR: Error while expanding SRV RR name with dn_expand: %s\n", LogTime(), PROGRAM, strerror(errno));
376 goto cleanup;
377 }
378 debug((char *) "%s| %s: DEBUG: Resolved SRV %s record to %s\n", LogTime(), PROGRAM, service, host);
379 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nh + 1));
380 hp[nh].host = xstrdup(host);
381 hp[nh].port = port;
382 hp[nh].priority = priority;
383 hp[nh].weight = weight;
384 nh++;
385 p += size;
386 } else {
387 p += rdlength;
388 }
389 if (p > buffer + len) {
390 error((char *) "%s| %s: ERROR: Message to small: %d < SRV RR + priority + weight + port + name\n", LogTime(), PROGRAM, len);
391 goto cleanup;
392 }
393 }
394 if (p != buffer + len) {
395 #if (SIZEOF_LONG == 8)
396 error("%s| %s: ERROR: Inconsistence message length: %ld!=0\n", LogTime(), PROGRAM, buffer + len - p);
397 #else
398 error((char *) "%s| %s: ERROR: Inconsistence message length: %d!=0\n", LogTime(), PROGRAM, buffer + len - p);
399 #endif
400 goto cleanup;
401 }
402
403 cleanup:
404 nhosts = get_hostname_list(margs, &hp, nh, domain);
405
406 debug("%s| %s: DEBUG: Adding %s to list\n", LogTime(), PROGRAM, domain);
407
408 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nhosts + 1));
409 hp[nhosts].host = strdup(domain);
410 hp[nhosts].port = -1;
411 hp[nhosts].priority = -2;
412 hp[nhosts].weight = -2;
413 nhosts++;
414
415 /* Remove duplicates */
416 for (i = 0; i < nhosts; i++) {
417 for (j = i + 1; j < nhosts; j++) {
418 if (!strcasecmp(hp[i].host, hp[j].host)) {
419 if (hp[i].port == hp[j].port ||
420 (hp[i].port == -1 && hp[j].port == 389) ||
421 (hp[i].port == 389 && hp[j].port == -1)) {
422 xfree(hp[j].host);
423 for (k = j + 1; k < nhosts; k++) {
424 hp[k - 1].host = hp[k].host;
425 hp[k - 1].port = hp[k].port;
426 hp[k - 1].priority = hp[k].priority;
427 hp[k - 1].weight = hp[k].weight;
428 }
429 j--;
430 nhosts--;
431 hp = (struct hstruct *) xrealloc(hp, sizeof(struct hstruct) * (nhosts + 1));
432 }
433 }
434 }
435 }
436
437 /* Sort by Priority / Weight */
438 msort(hp, nhosts, compare_hosts);
439
440 if (debug_enabled) {
441 debug((char *) "%s| %s: DEBUG: Sorted ldap server names for domain %s:\n", LogTime(), PROGRAM, domain);
442 for (i = 0; i < nhosts; i++) {
443 debug((char *) "%s| %s: DEBUG: Host: %s Port: %d Priority: %d Weight: %d\n", LogTime(), PROGRAM, hp[i].host, hp[i].port, hp[i].priority, hp[i].weight);
444 }
445 }
446 if (buffer)
447 xfree(buffer);
448 if (service)
449 xfree(service);
450 *hlist = hp;
451 return (nhosts);
452 }
453 #endif