summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/locate_kdc.c
blob: a6c208c442fc93b22fcc0fa6b7dd6377ce86b49b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * get socket addresses for KDC.
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_locate_kdc_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/krb5.h>
#include <krb5/osconf.h>

#include <krb5/ext-proto.h>
#include <stdio.h>
#include <krb5/los-proto.h>

#include <sys/types.h>
#include <sys/socket.h>
#ifdef KRB5_USE_INET
#include <netinet/in.h>
#endif
#include <netdb.h>
#include "os-proto.h"

#ifdef KRB5_USE_INET
extern char *krb5_kdc_udp_portname;
extern char *krb5_kdc_sec_udp_portname;
#endif

/*
 * returns count of number of addresses found
 */

krb5_error_code
krb5_locate_kdc(realm, addr_pp, naddrs)
    const krb5_data *realm;
    struct sockaddr **addr_pp;
    int *naddrs;
{
    char **hostlist;
    int code;
    int i, j, out, count;
    struct sockaddr *addr_p;
    struct sockaddr_in *sin_p;
    struct hostent *hp;
    struct servent *sp;
#ifdef KRB5_USE_INET
    u_short udpport = 0;		/* 0 is an invalid UDP port #. */
    u_short sec_udpport = 0;		/* 0 is an invalid UDP port #. */
#endif

    hostlist = 0;
    
    if (code = krb5_get_krbhst (realm, &hostlist))
	return(code);

#ifdef KRB5_USE_INET
    if (sp = getservbyname(krb5_kdc_udp_portname, "udp"))
	udpport = sp->s_port;
    if (krb5_kdc_sec_udp_portname)
    	if (sp = getservbyname(krb5_kdc_sec_udp_portname, "udp")) {
#ifdef KRB5_TRY_SECONDARY_PORT_FIRST
	    sec_udpport = udpport;
	    udpport = sp->s_port;
#else
	    sec_udpport = sp->s_port;
#endif
	}
#endif

    count = 0;
    while (hostlist[count])
	    count++;
    
    if (count == 0) {
	*naddrs = 0;
	return 0;
    }
    
#ifdef KRB5_USE_INET
    if (sec_udpport)
	    count = count * 2;
#endif

    addr_p = (struct sockaddr *)malloc (sizeof (struct sockaddr) * count);

    for (i=0, out=0; hostlist[i]; i++) {
	hp = gethostbyname(hostlist[i]);
	if (hp != 0) {
	    switch (hp->h_addrtype) {
#ifdef KRB5_USE_INET
	    case AF_INET:
		if (udpport)		/* must have gotten a port # */
		for (j=0; hp->h_addr_list[j]; j++) {
		    sin_p = (struct sockaddr_in *) &addr_p[out++];
		    memset ((char *)sin_p, 0, sizeof(struct sockaddr));
		    sin_p->sin_family = hp->h_addrtype;
		    sin_p->sin_port = udpport;
		    memcpy((char *)&sin_p->sin_addr,
			   (char *)hp->h_addr_list[j],
			   sizeof(struct in_addr));
		    if (out >= count) {
			count *= 2;
			addr_p = (struct sockaddr *)
			    realloc ((char *)addr_p,
				     sizeof(struct sockaddr) * count);
		    }
		    if (sec_udpport) {
			addr_p[out] = addr_p[out-1];
			sin_p = (struct sockaddr_in *) &addr_p[out++];
			sin_p->sin_port = sec_udpport;
			if (out >= count) {
			    count *= 2;
			    addr_p = (struct sockaddr *)
				    realloc ((char *)addr_p,
					     sizeof(struct sockaddr) * count);
			}
		    }
		}
		break;
#endif
	    default:
		break;
	    }
	}
	free(hostlist[i]);
	hostlist[i] = 0;
    }
    free ((char *)hostlist);
				/*
				 * XXX need to distinguish between
				 * "can't resolve KDC name" and
				 * "can't find any KDC names"
				 */
    *addr_pp = addr_p;
    *naddrs = out;
    return 0;
}