]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/kerberos_ldap_group/support_resolv.cc
2 * -----------------------------------------------------------------------------
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
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.
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.
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.
22 * -----------------------------------------------------------------------------
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
43 #ifdef HAVE_ARPA_NAMESER_H
44 #include <arpa/nameser.h>
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
*));
54 * See http://www.ietf.org/rfc/rfc1035.txt
57 * See http://www.ietf.org/rfc/rfc2782.txt
61 nsError(int nserror
, char *service
)
65 error((char *) "%s| %s: ERROR: res_search: Unknown service record: %s\n", LogTime(), PROGRAM
, service
);
68 error((char *) "%s| %s: ERROR: res_search: No SRV record for %s\n", LogTime(), PROGRAM
, service
);
71 error((char *) "%s| %s: ERROR: res_search: No response for SRV query\n", LogTime(), PROGRAM
);
74 error((char *) "%s| %s: ERROR: res_search: Unexpected error: %s\n", LogTime(), PROGRAM
, strerror(nserror
));
79 swap(struct hstruct
*a
, struct hstruct
*b
)
84 c
.priority
= a
->priority
;
87 a
->priority
= b
->priority
;
88 a
->weight
= b
->weight
;
90 b
->priority
= c
.priority
;
95 sort(struct hstruct
*array
, int nitems
, int (*cmp
) (struct hstruct
*, struct hstruct
*), int begin
, int end
)
102 if (cmp(&array
[l
], &array
[pivot
]) <= 0) {
106 swap(&array
[l
], &array
[r
]);
110 swap(&array
[begin
], &array
[l
]);
111 sort(array
, nitems
, cmp
, begin
, l
);
112 sort(array
, nitems
, cmp
, r
, end
);
117 msort(struct hstruct
*array
, size_t nitems
, int (*cmp
) (struct hstruct
*, struct hstruct
*))
119 sort(array
, nitems
, cmp
, 0, nitems
- 1);
123 compare_hosts(struct hstruct
*host1
, struct hstruct
*host2
)
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.
131 if ((host1
->priority
< host2
->priority
) && (host1
->priority
!= -1))
133 if ((host1
->priority
< host2
->priority
) && (host1
->priority
== -1))
135 if ((host1
->priority
> host2
->priority
) && (host2
->priority
!= -1))
137 if ((host1
->priority
> host2
->priority
) && (host2
->priority
== -1))
139 if (host1
->priority
== host2
->priority
) {
140 if (host1
->weight
> host2
->weight
)
142 if (host1
->weight
< host2
->weight
)
149 free_hostname_list(struct hstruct
**hlist
, int nhosts
)
151 struct hstruct
*hp
= NULL
;
155 for (i
= 0; i
< nhosts
; i
++) {
170 get_hostname_list(struct main_args
*margs
, struct hstruct
**hlist
, int nhosts
, char *name
)
173 * char host[sysconf(_SC_HOST_NAME_MAX)];
176 struct addrinfo
*hres
= NULL
, *hres_list
;
178 struct hstruct
*hp
= NULL
;
184 rc
= getaddrinfo((const char *) name
, NULL
, NULL
, &hres
);
186 error((char *) "%s| %s: ERROR: Error while resolving hostname with getaddrinfo: %s\n", LogTime(), PROGRAM
, gai_strerror(rc
));
193 hres_list
= hres_list
->ai_next
;
198 rc
= getnameinfo(hres_list
->ai_addr
, hres_list
->ai_addrlen
, host
, sizeof(host
), NULL
, 0, 0);
200 error((char *) "%s| %s: ERROR: Error while resolving ip address with getnameinfo: %s\n", LogTime(), PROGRAM
, gai_strerror(rc
));
206 debug((char *) "%s| %s: DEBUG: Resolved address %d of %s to %s\n", LogTime(), PROGRAM
, count
, name
, host
);
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;
215 hres_list
= hres_list
->ai_next
;
224 get_ldap_hostname_list(struct main_args
*margs
, struct hstruct
**hlist
, int nh
, char *domain
)
228 * char name[sysconf(_SC_HOST_NAME_MAX)];
231 char host
[NS_MAXDNAME
];
232 char *service
= NULL
;
233 struct hstruct
*hp
= NULL
;
234 struct lsstruct
*ls
= NULL
;
238 int priority
, weight
, port
;
241 u_char
*buffer
= NULL
;
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;
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;
267 /* found ldap servers in predefined list -> exit */
272 service
= (char *) xmalloc(strlen("_ldaps._tcp.") + strlen(domain
) + 1);
273 strcpy(service
, "_ldaps._tcp.");
275 service
= (char *) xmalloc(strlen("_ldap._tcp.") + strlen(domain
) + 1);
276 strcpy(service
, "_ldap._tcp.");
278 strcat(service
, domain
);
280 #ifndef PACKETSZ_MULT
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
285 #define PACKETSZ_MULT 10
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
);
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
);
307 if (len
> PACKETSZ_MULT
* NS_PACKETSZ
) {
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
);
316 error((char *) "%s| %s: ERROR: Reply to big: buffer: %d reply length: %d\n", LogTime(), PROGRAM
, olen
, len
);
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
);
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
));
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
);
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
));
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
);
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
);
352 NS_GET16(rdlength
, p
); /* RR data length (16bit) */
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
);
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
);
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
);
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
);
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
));
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
);
382 hp
[nh
].priority
= priority
;
383 hp
[nh
].weight
= weight
;
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
);
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
);
398 error((char *) "%s| %s: ERROR: Inconsistence message length: %d!=0\n", LogTime(), PROGRAM
, buffer
+ len
- p
);
404 nhosts
= get_hostname_list(margs
, &hp
, nh
, domain
);
406 debug("%s| %s: DEBUG: Adding %s to list\n", LogTime(), PROGRAM
, domain
);
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;
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)) {
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
;
431 hp
= (struct hstruct
*) xrealloc(hp
, sizeof(struct hstruct
) * (nhosts
+ 1));
437 /* Sort by Priority / Weight */
438 msort(hp
, nhosts
, compare_hosts
);
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
);