2 * Copyright (c) 2001 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 #include "locate_plugin.h"
39 string_to_proto(const char *string
)
41 if(strcasecmp(string
, "udp") == 0)
42 return KRB5_KRBHST_UDP
;
43 else if(strcasecmp(string
, "tcp") == 0)
44 return KRB5_KRBHST_TCP
;
45 else if(strcasecmp(string
, "http") == 0)
46 return KRB5_KRBHST_HTTP
;
51 * set `res' and `count' to the result of looking up SRV RR in DNS for
52 * `proto', `proto', `realm' using `dns_type'.
53 * if `port' != 0, force that port number
56 static krb5_error_code
57 srv_find_realm(krb5_context context
, krb5_krbhst_info
***res
, int *count
,
58 const char *realm
, const char *dns_type
,
59 const char *proto
, const char *service
, int port
)
62 struct rk_dns_reply
*r
;
63 struct rk_resource_record
*rr
;
71 proto_num
= string_to_proto(proto
);
73 krb5_set_error_message(context
, EINVAL
,
74 N_("unknown protocol `%s' to lookup", ""),
79 if(proto_num
== KRB5_KRBHST_HTTP
)
80 def_port
= ntohs(krb5_getportbyname (context
, "http", "tcp", 80));
82 def_port
= ntohs(krb5_getportbyname (context
, service
, proto
, 88));
86 snprintf(domain
, sizeof(domain
), "_%s._%s.%s.", service
, proto
, realm
);
88 r
= rk_dns_lookup(domain
, dns_type
);
90 _krb5_debug(context
, 0,
91 "DNS lookup failed domain: %s", domain
);
92 return KRB5_KDC_UNREACH
;
95 for(num_srv
= 0, rr
= r
->head
; rr
; rr
= rr
->next
)
96 if(rr
->type
== rk_ns_t_srv
)
99 *res
= malloc(num_srv
* sizeof(**res
));
102 krb5_set_error_message(context
, ENOMEM
,
103 N_("malloc: out of memory", ""));
109 for(num_srv
= 0, rr
= r
->head
; rr
; rr
= rr
->next
)
110 if(rr
->type
== rk_ns_t_srv
) {
111 krb5_krbhst_info
*hi
;
112 size_t len
= strlen(rr
->u
.srv
->target
);
114 hi
= calloc(1, sizeof(*hi
) + len
);
117 while(--num_srv
>= 0)
118 free((*res
)[num_srv
]);
123 (*res
)[num_srv
++] = hi
;
125 hi
->proto
= proto_num
;
127 hi
->def_port
= def_port
;
131 hi
->port
= rr
->u
.srv
->port
;
133 strlcpy(hi
->hostname
, rr
->u
.srv
->target
, len
+ 1);
143 struct krb5_krbhst_data
{
147 int port
; /* hardwired port number if != 0 */
151 #define KD_SRV_HTTP 8
152 #define KD_FALLBACK 16
153 #define KD_CONFIG_EXISTS 32
154 #define KD_LARGE_MSG 64
155 #define KD_PLUGIN 128
156 krb5_error_code (*get_next
)(krb5_context
, struct krb5_krbhst_data
*,
159 unsigned int fallback_count
;
161 struct krb5_krbhst_info
*hosts
, **index
, **end
;
165 krbhst_empty(const struct krb5_krbhst_data
*kd
)
167 return kd
->index
== &kd
->hosts
;
171 * Return the default protocol for the `kd' (either TCP or UDP)
175 krbhst_get_default_proto(struct krb5_krbhst_data
*kd
)
177 if (kd
->flags
& KD_LARGE_MSG
)
178 return KRB5_KRBHST_TCP
;
179 return KRB5_KRBHST_UDP
;
184 * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
185 * and forcing it to `port' if port != 0
188 static struct krb5_krbhst_info
*
189 parse_hostspec(krb5_context context
, struct krb5_krbhst_data
*kd
,
190 const char *spec
, int def_port
, int port
)
192 const char *p
= spec
, *q
;
193 struct krb5_krbhst_info
*hi
;
195 hi
= calloc(1, sizeof(*hi
) + strlen(spec
));
199 hi
->proto
= krbhst_get_default_proto(kd
);
201 if(strncmp(p
, "http://", 7) == 0){
202 hi
->proto
= KRB5_KRBHST_HTTP
;
204 } else if(strncmp(p
, "http/", 5) == 0) {
205 hi
->proto
= KRB5_KRBHST_HTTP
;
207 def_port
= ntohs(krb5_getportbyname (context
, "http", "tcp", 80));
208 }else if(strncmp(p
, "tcp/", 4) == 0){
209 hi
->proto
= KRB5_KRBHST_TCP
;
211 } else if(strncmp(p
, "udp/", 4) == 0) {
215 if (p
[0] == '[' && (q
= strchr(p
, ']')) != NULL
) {
216 /* if address looks like [foo:bar] or [foo:bar]: its a ipv6
217 adress, strip of [] */
218 memcpy(hi
->hostname
, &p
[1], q
- p
- 1);
219 hi
->hostname
[q
- p
- 1] = '\0';
224 } else if(strsep_copy(&p
, ":", hi
->hostname
, strlen(spec
) + 1) < 0) {
225 /* copy everything before : */
229 /* get rid of trailing /, and convert to lower case */
230 hi
->hostname
[strcspn(hi
->hostname
, "/")] = '\0';
231 strlwr(hi
->hostname
);
233 hi
->port
= hi
->def_port
= def_port
;
234 if(p
!= NULL
&& p
[0]) {
236 hi
->port
= strtol(p
, &end
, 0);
248 _krb5_free_krbhst_info(krb5_krbhst_info
*hi
)
251 freeaddrinfo(hi
->ai
);
256 _krb5_krbhost_info_move(krb5_context context
,
257 krb5_krbhst_info
*from
,
258 krb5_krbhst_info
**to
)
260 size_t hostnamelen
= strlen(from
->hostname
);
261 /* trailing NUL is included in structure */
262 *to
= calloc(1, sizeof(**to
) + hostnamelen
);
264 krb5_set_error_message(context
, ENOMEM
,
265 N_("malloc: out of memory", ""));
269 (*to
)->proto
= from
->proto
;
270 (*to
)->port
= from
->port
;
271 (*to
)->def_port
= from
->def_port
;
272 (*to
)->ai
= from
->ai
;
275 memcpy((*to
)->hostname
, from
->hostname
, hostnamelen
+ 1);
281 append_host_hostinfo(struct krb5_krbhst_data
*kd
, struct krb5_krbhst_info
*host
)
283 struct krb5_krbhst_info
*h
;
285 for(h
= kd
->hosts
; h
; h
= h
->next
)
286 if(h
->proto
== host
->proto
&&
287 h
->port
== host
->port
&&
288 strcmp(h
->hostname
, host
->hostname
) == 0) {
289 _krb5_free_krbhst_info(host
);
293 kd
->end
= &host
->next
;
296 static krb5_error_code
297 append_host_string(krb5_context context
, struct krb5_krbhst_data
*kd
,
298 const char *host
, int def_port
, int port
)
300 struct krb5_krbhst_info
*hi
;
302 hi
= parse_hostspec(context
, kd
, host
, def_port
, port
);
306 append_host_hostinfo(kd
, hi
);
311 * return a readable representation of `host' in `hostname, hostlen'
314 krb5_error_code KRB5_LIB_FUNCTION
315 krb5_krbhst_format_string(krb5_context context
, const krb5_krbhst_info
*host
,
316 char *hostname
, size_t hostlen
)
318 const char *proto
= "";
319 char portstr
[7] = "";
320 if(host
->proto
== KRB5_KRBHST_TCP
)
322 else if(host
->proto
== KRB5_KRBHST_HTTP
)
324 if(host
->port
!= host
->def_port
)
325 snprintf(portstr
, sizeof(portstr
), ":%d", host
->port
);
326 snprintf(hostname
, hostlen
, "%s%s%s", proto
, host
->hostname
, portstr
);
331 * create a getaddrinfo `hints' based on `proto'
335 make_hints(struct addrinfo
*hints
, int proto
)
337 memset(hints
, 0, sizeof(*hints
));
338 hints
->ai_family
= AF_UNSPEC
;
340 case KRB5_KRBHST_UDP
:
341 hints
->ai_socktype
= SOCK_DGRAM
;
343 case KRB5_KRBHST_HTTP
:
344 case KRB5_KRBHST_TCP
:
345 hints
->ai_socktype
= SOCK_STREAM
;
351 * return an `struct addrinfo *' in `ai' corresponding to the information
352 * in `host'. free:ing is handled by krb5_krbhst_free.
355 krb5_error_code KRB5_LIB_FUNCTION
356 krb5_krbhst_get_addrinfo(krb5_context context
, krb5_krbhst_info
*host
,
357 struct addrinfo
**ai
)
359 struct addrinfo hints
;
360 char portstr
[NI_MAXSERV
];
363 if (host
->ai
== NULL
) {
364 make_hints(&hints
, host
->proto
);
365 snprintf (portstr
, sizeof(portstr
), "%d", host
->port
);
366 ret
= getaddrinfo(host
->hostname
, portstr
, &hints
, &host
->ai
);
368 return krb5_eai_to_heim_errno(ret
, errno
);
375 get_next(struct krb5_krbhst_data
*kd
, krb5_krbhst_info
**host
)
377 struct krb5_krbhst_info
*hi
= *kd
->index
;
380 kd
->index
= &(*kd
->index
)->next
;
387 srv_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
388 const char *proto
, const char *service
)
391 krb5_krbhst_info
**res
;
394 ret
= srv_find_realm(context
, &res
, &count
, kd
->realm
, "SRV", proto
, service
,
396 _krb5_debug(context
, 2, "searching DNS for realm %s %s.%s -> %d",
397 kd
->realm
, proto
, service
, ret
);
400 for(i
= 0; i
< count
; i
++)
401 append_host_hostinfo(kd
, res
[i
]);
406 * read the configuration for `conf_string', defaulting to kd->def_port and
407 * forcing it to `kd->port' if kd->port != 0
411 config_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
412 const char *conf_string
)
416 hostlist
= krb5_config_get_strings(context
, NULL
,
417 "realms", kd
->realm
, conf_string
, NULL
);
419 _krb5_debug(context
, 2, "configuration file for realm %s%s found",
420 kd
->realm
, hostlist
? "" : " not");
424 kd
->flags
|= KD_CONFIG_EXISTS
;
425 for(i
= 0; hostlist
&& hostlist
[i
] != NULL
; i
++)
426 append_host_string(context
, kd
, hostlist
[i
], kd
->def_port
, kd
->port
);
428 krb5_config_free_strings(hostlist
);
432 * as a fallback, look for `serv_string.kd->realm' (typically
433 * kerberos.REALM, kerberos-1.REALM, ...
434 * `port' is the default port for the service, and `proto' the
438 static krb5_error_code
439 fallback_get_hosts(krb5_context context
, struct krb5_krbhst_data
*kd
,
440 const char *serv_string
, int port
, int proto
)
445 struct addrinfo hints
;
446 char portstr
[NI_MAXSERV
];
448 _krb5_debug(context
, 2, "fallback lookup %d for realm %s (service %s)",
449 kd
->fallback_count
, kd
->realm
, serv_string
);
452 * Don't try forever in case the DNS server keep returning us
453 * entries (like wildcard entries or the .nu TLD)
455 if(kd
->fallback_count
>= 5) {
456 kd
->flags
|= KD_FALLBACK
;
460 if(kd
->fallback_count
== 0)
461 asprintf(&host
, "%s.%s.", serv_string
, kd
->realm
);
463 asprintf(&host
, "%s-%d.%s.",
464 serv_string
, kd
->fallback_count
, kd
->realm
);
469 make_hints(&hints
, proto
);
470 snprintf(portstr
, sizeof(portstr
), "%d", port
);
471 ret
= getaddrinfo(host
, portstr
, &hints
, &ai
);
473 /* no more hosts, so we're done here */
475 kd
->flags
|= KD_FALLBACK
;
477 struct krb5_krbhst_info
*hi
;
478 size_t hostlen
= strlen(host
);
480 hi
= calloc(1, sizeof(*hi
) + hostlen
);
487 hi
->port
= hi
->def_port
= port
;
489 memmove(hi
->hostname
, host
, hostlen
);
490 hi
->hostname
[hostlen
] = '\0';
492 append_host_hostinfo(kd
, hi
);
493 kd
->fallback_count
++;
499 * Fetch hosts from plugin
502 static krb5_error_code
503 add_locate(void *ctx
, int type
, struct sockaddr
*addr
)
505 struct krb5_krbhst_info
*hi
;
506 struct krb5_krbhst_data
*kd
= ctx
;
507 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
508 struct addrinfo hints
, *ai
;
513 socklen
= socket_sockaddr_size(addr
);
515 ret
= getnameinfo(addr
, socklen
, host
, sizeof(host
), port
, sizeof(port
),
516 NI_NUMERICHOST
|NI_NUMERICSERV
);
520 make_hints(&hints
, krbhst_get_default_proto(kd
));
521 ret
= getaddrinfo(host
, port
, &hints
, &ai
);
525 hostlen
= strlen(host
);
527 hi
= calloc(1, sizeof(*hi
) + hostlen
);
531 hi
->proto
= krbhst_get_default_proto(kd
);
532 hi
->port
= hi
->def_port
= socket_get_port(addr
);
534 memmove(hi
->hostname
, host
, hostlen
);
535 hi
->hostname
[hostlen
] = '\0';
536 append_host_hostinfo(kd
, hi
);
542 plugin_get_hosts(krb5_context context
,
543 struct krb5_krbhst_data
*kd
,
544 enum locate_service_type type
)
546 struct krb5_plugin
*list
= NULL
, *e
;
549 ret
= _krb5_plugin_find(context
, PLUGIN_TYPE_DATA
,
550 KRB5_PLUGIN_LOCATE
, &list
);
551 if(ret
!= 0 || list
== NULL
)
554 for (e
= list
; e
!= NULL
; e
= _krb5_plugin_get_next(e
)) {
555 krb5plugin_service_locate_ftable
*service
;
558 service
= _krb5_plugin_get_symbol(e
);
559 if (service
->minor_version
!= 0)
562 (*service
->init
)(context
, &ctx
);
563 ret
= (*service
->lookup
)(ctx
, type
, kd
->realm
, 0, 0, add_locate
, kd
);
564 (*service
->fini
)(ctx
);
565 if (ret
&& ret
!= KRB5_PLUGIN_NO_HANDLE
) {
566 krb5_set_error_message(context
, ret
,
567 N_("Locate plugin failed to lookup realm %s: %d", ""),
570 } else if (ret
== 0) {
571 _krb5_debug(context
, 2, "plugin found result for realm %s", kd
->realm
);
572 kd
->flags
|= KD_CONFIG_EXISTS
;
576 _krb5_plugin_free(list
);
583 static krb5_error_code
584 kdc_get_next(krb5_context context
,
585 struct krb5_krbhst_data
*kd
,
586 krb5_krbhst_info
**host
)
590 if ((kd
->flags
& KD_PLUGIN
) == 0) {
591 plugin_get_hosts(context
, kd
, locate_service_kdc
);
592 kd
->flags
|= KD_PLUGIN
;
593 if(get_next(kd
, host
))
597 if((kd
->flags
& KD_CONFIG
) == 0) {
598 config_get_hosts(context
, kd
, "kdc");
599 kd
->flags
|= KD_CONFIG
;
600 if(get_next(kd
, host
))
604 if (kd
->flags
& KD_CONFIG_EXISTS
) {
605 _krb5_debug(context
, 1,
606 "Configuration exists for realm %s, wont go to DNS",
608 return KRB5_KDC_UNREACH
;
611 if(context
->srv_lookup
) {
612 if((kd
->flags
& KD_SRV_UDP
) == 0 && (kd
->flags
& KD_LARGE_MSG
) == 0) {
613 srv_get_hosts(context
, kd
, "udp", "kerberos");
614 kd
->flags
|= KD_SRV_UDP
;
615 if(get_next(kd
, host
))
619 if((kd
->flags
& KD_SRV_TCP
) == 0) {
620 srv_get_hosts(context
, kd
, "tcp", "kerberos");
621 kd
->flags
|= KD_SRV_TCP
;
622 if(get_next(kd
, host
))
625 if((kd
->flags
& KD_SRV_HTTP
) == 0) {
626 srv_get_hosts(context
, kd
, "http", "kerberos");
627 kd
->flags
|= KD_SRV_HTTP
;
628 if(get_next(kd
, host
))
633 while((kd
->flags
& KD_FALLBACK
) == 0) {
634 ret
= fallback_get_hosts(context
, kd
, "kerberos",
636 krbhst_get_default_proto(kd
));
639 if(get_next(kd
, host
))
643 _krb5_debug(context
, 0, "No KDC entries found for %s", kd
->realm
);
645 return KRB5_KDC_UNREACH
; /* XXX */
648 static krb5_error_code
649 admin_get_next(krb5_context context
,
650 struct krb5_krbhst_data
*kd
,
651 krb5_krbhst_info
**host
)
655 if ((kd
->flags
& KD_PLUGIN
) == 0) {
656 plugin_get_hosts(context
, kd
, locate_service_kadmin
);
657 kd
->flags
|= KD_PLUGIN
;
658 if(get_next(kd
, host
))
662 if((kd
->flags
& KD_CONFIG
) == 0) {
663 config_get_hosts(context
, kd
, "admin_server");
664 kd
->flags
|= KD_CONFIG
;
665 if(get_next(kd
, host
))
669 if (kd
->flags
& KD_CONFIG_EXISTS
) {
670 _krb5_debug(context
, 1,
671 "Configuration exists for realm %s, wont go to DNS",
673 return KRB5_KDC_UNREACH
;
676 if(context
->srv_lookup
) {
677 if((kd
->flags
& KD_SRV_TCP
) == 0) {
678 srv_get_hosts(context
, kd
, "tcp", "kerberos-adm");
679 kd
->flags
|= KD_SRV_TCP
;
680 if(get_next(kd
, host
))
686 && (kd
->flags
& KD_FALLBACK
) == 0) {
687 ret
= fallback_get_hosts(context
, kd
, "kerberos",
689 krbhst_get_default_proto(kd
));
692 kd
->flags
|= KD_FALLBACK
;
693 if(get_next(kd
, host
))
697 _krb5_debug(context
, 0, "No admin entries found for realm %s", kd
->realm
);
699 return KRB5_KDC_UNREACH
; /* XXX */
702 static krb5_error_code
703 kpasswd_get_next(krb5_context context
,
704 struct krb5_krbhst_data
*kd
,
705 krb5_krbhst_info
**host
)
709 if ((kd
->flags
& KD_PLUGIN
) == 0) {
710 plugin_get_hosts(context
, kd
, locate_service_kpasswd
);
711 kd
->flags
|= KD_PLUGIN
;
712 if(get_next(kd
, host
))
716 if((kd
->flags
& KD_CONFIG
) == 0) {
717 config_get_hosts(context
, kd
, "kpasswd_server");
718 kd
->flags
|= KD_CONFIG
;
719 if(get_next(kd
, host
))
723 if (kd
->flags
& KD_CONFIG_EXISTS
) {
724 _krb5_debug(context
, 1,
725 "Configuration exists for realm %s, wont go to DNS",
727 return KRB5_KDC_UNREACH
;
730 if(context
->srv_lookup
) {
731 if((kd
->flags
& KD_SRV_UDP
) == 0) {
732 srv_get_hosts(context
, kd
, "udp", "kpasswd");
733 kd
->flags
|= KD_SRV_UDP
;
734 if(get_next(kd
, host
))
737 if((kd
->flags
& KD_SRV_TCP
) == 0) {
738 srv_get_hosts(context
, kd
, "tcp", "kpasswd");
739 kd
->flags
|= KD_SRV_TCP
;
740 if(get_next(kd
, host
))
745 /* no matches -> try admin */
747 if (krbhst_empty(kd
)) {
749 kd
->port
= kd
->def_port
;
750 kd
->get_next
= admin_get_next
;
751 ret
= (*kd
->get_next
)(context
, kd
, host
);
753 (*host
)->proto
= krbhst_get_default_proto(kd
);
757 _krb5_debug(context
, 0, "No kpasswd entries found for realm %s", kd
->realm
);
759 return KRB5_KDC_UNREACH
;
762 static krb5_error_code
763 krb524_get_next(krb5_context context
,
764 struct krb5_krbhst_data
*kd
,
765 krb5_krbhst_info
**host
)
767 if ((kd
->flags
& KD_PLUGIN
) == 0) {
768 plugin_get_hosts(context
, kd
, locate_service_krb524
);
769 kd
->flags
|= KD_PLUGIN
;
770 if(get_next(kd
, host
))
774 if((kd
->flags
& KD_CONFIG
) == 0) {
775 config_get_hosts(context
, kd
, "krb524_server");
776 if(get_next(kd
, host
))
778 kd
->flags
|= KD_CONFIG
;
781 if (kd
->flags
& KD_CONFIG_EXISTS
) {
782 _krb5_debug(context
, 1,
783 "Configuration exists for realm %s, wont go to DNS",
785 return KRB5_KDC_UNREACH
;
788 if(context
->srv_lookup
) {
789 if((kd
->flags
& KD_SRV_UDP
) == 0) {
790 srv_get_hosts(context
, kd
, "udp", "krb524");
791 kd
->flags
|= KD_SRV_UDP
;
792 if(get_next(kd
, host
))
796 if((kd
->flags
& KD_SRV_TCP
) == 0) {
797 srv_get_hosts(context
, kd
, "tcp", "krb524");
798 kd
->flags
|= KD_SRV_TCP
;
799 if(get_next(kd
, host
))
804 /* no matches -> try kdc */
806 if (krbhst_empty(kd
)) {
808 kd
->port
= kd
->def_port
;
809 kd
->get_next
= kdc_get_next
;
810 return (*kd
->get_next
)(context
, kd
, host
);
813 _krb5_debug(context
, 0, "No kpasswd entries found for realm %s", kd
->realm
);
815 return KRB5_KDC_UNREACH
;
818 static struct krb5_krbhst_data
*
819 common_init(krb5_context context
,
824 struct krb5_krbhst_data
*kd
;
826 if((kd
= calloc(1, sizeof(*kd
))) == NULL
)
829 if((kd
->realm
= strdup(realm
)) == NULL
) {
834 _krb5_debug(context
, 2, "Trying to find service %s for realm %s flags %x",
835 service
, realm
, flags
);
837 /* For 'realms' without a . do not even think of going to DNS */
838 if (!strchr(realm
, '.'))
839 kd
->flags
|= KD_CONFIG_EXISTS
;
841 if (flags
& KRB5_KRBHST_FLAGS_LARGE_MSG
)
842 kd
->flags
|= KD_LARGE_MSG
;
843 kd
->end
= kd
->index
= &kd
->hosts
;
848 * initialize `handle' to look for hosts of type `type' in realm `realm'
851 krb5_error_code KRB5_LIB_FUNCTION
852 krb5_krbhst_init(krb5_context context
,
855 krb5_krbhst_handle
*handle
)
857 return krb5_krbhst_init_flags(context
, realm
, type
, 0, handle
);
860 krb5_error_code KRB5_LIB_FUNCTION
861 krb5_krbhst_init_flags(krb5_context context
,
865 krb5_krbhst_handle
*handle
)
867 struct krb5_krbhst_data
*kd
;
868 krb5_error_code (*next
)(krb5_context
, struct krb5_krbhst_data
*,
869 krb5_krbhst_info
**);
874 case KRB5_KRBHST_KDC
:
876 def_port
= ntohs(krb5_getportbyname (context
, "kerberos", "udp", 88));
879 case KRB5_KRBHST_ADMIN
:
880 next
= admin_get_next
;
881 def_port
= ntohs(krb5_getportbyname (context
, "kerberos-adm",
885 case KRB5_KRBHST_CHANGEPW
:
886 next
= kpasswd_get_next
;
887 def_port
= ntohs(krb5_getportbyname (context
, "kpasswd", "udp",
889 service
= "change_password";
891 case KRB5_KRBHST_KRB524
:
892 next
= krb524_get_next
;
893 def_port
= ntohs(krb5_getportbyname (context
, "krb524", "udp", 4444));
897 krb5_set_error_message(context
, ENOTTY
,
898 N_("unknown krbhst type (%u)", ""), type
);
901 if((kd
= common_init(context
, service
, realm
, flags
)) == NULL
)
904 kd
->def_port
= def_port
;
910 * return the next host information from `handle' in `host'
913 krb5_error_code KRB5_LIB_FUNCTION
914 krb5_krbhst_next(krb5_context context
,
915 krb5_krbhst_handle handle
,
916 krb5_krbhst_info
**host
)
918 if(get_next(handle
, host
))
921 return (*handle
->get_next
)(context
, handle
, host
);
925 * return the next host information from `handle' as a host name
926 * in `hostname' (or length `hostlen)
929 krb5_error_code KRB5_LIB_FUNCTION
930 krb5_krbhst_next_as_string(krb5_context context
,
931 krb5_krbhst_handle handle
,
936 krb5_krbhst_info
*host
;
937 ret
= krb5_krbhst_next(context
, handle
, &host
);
940 return krb5_krbhst_format_string(context
, host
, hostname
, hostlen
);
944 void KRB5_LIB_FUNCTION
945 krb5_krbhst_reset(krb5_context context
, krb5_krbhst_handle handle
)
947 handle
->index
= &handle
->hosts
;
950 void KRB5_LIB_FUNCTION
951 krb5_krbhst_free(krb5_context context
, krb5_krbhst_handle handle
)
953 krb5_krbhst_info
*h
, *next
;
958 for (h
= handle
->hosts
; h
!= NULL
; h
= next
) {
960 _krb5_free_krbhst_info(h
);
967 /* backwards compatibility ahead */
969 static krb5_error_code
970 gethostlist(krb5_context context
, const char *realm
,
971 unsigned int type
, char ***hostlist
)
975 krb5_krbhst_handle handle
;
976 char host
[MAXHOSTNAMELEN
];
977 krb5_krbhst_info
*hostinfo
;
979 ret
= krb5_krbhst_init(context
, realm
, type
, &handle
);
983 while(krb5_krbhst_next(context
, handle
, &hostinfo
) == 0)
986 krb5_set_error_message(context
, KRB5_KDC_UNREACH
,
987 N_("No KDC found for realm %s", ""), realm
);
988 return KRB5_KDC_UNREACH
;
990 *hostlist
= calloc(nhost
+ 1, sizeof(**hostlist
));
991 if(*hostlist
== NULL
) {
992 krb5_krbhst_free(context
, handle
);
996 krb5_krbhst_reset(context
, handle
);
998 while(krb5_krbhst_next_as_string(context
, handle
,
999 host
, sizeof(host
)) == 0) {
1000 if(((*hostlist
)[nhost
++] = strdup(host
)) == NULL
) {
1001 krb5_free_krbhst(context
, *hostlist
);
1002 krb5_krbhst_free(context
, handle
);
1006 (*hostlist
)[nhost
] = NULL
;
1007 krb5_krbhst_free(context
, handle
);
1012 * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1015 krb5_error_code KRB5_LIB_FUNCTION
1016 krb5_get_krb_admin_hst (krb5_context context
,
1017 const krb5_realm
*realm
,
1020 return gethostlist(context
, *realm
, KRB5_KRBHST_ADMIN
, hostlist
);
1024 * return an malloced list of changepw-hosts for `realm' in `hostlist'
1027 krb5_error_code KRB5_LIB_FUNCTION
1028 krb5_get_krb_changepw_hst (krb5_context context
,
1029 const krb5_realm
*realm
,
1032 return gethostlist(context
, *realm
, KRB5_KRBHST_CHANGEPW
, hostlist
);
1036 * return an malloced list of 524-hosts for `realm' in `hostlist'
1039 krb5_error_code KRB5_LIB_FUNCTION
1040 krb5_get_krb524hst (krb5_context context
,
1041 const krb5_realm
*realm
,
1044 return gethostlist(context
, *realm
, KRB5_KRBHST_KRB524
, hostlist
);
1049 * return an malloced list of KDC's for `realm' in `hostlist'
1052 krb5_error_code KRB5_LIB_FUNCTION
1053 krb5_get_krbhst (krb5_context context
,
1054 const krb5_realm
*realm
,
1057 return gethostlist(context
, *realm
, KRB5_KRBHST_KDC
, hostlist
);
1061 * free all the memory allocated in `hostlist'
1064 krb5_error_code KRB5_LIB_FUNCTION
1065 krb5_free_krbhst (krb5_context context
,
1070 for (p
= hostlist
; *p
; ++p
)