summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/sendto_kdc.c
blob: f38ec31836e5f9645c56c3cf0968168cc6ae6cb1 (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
162
163
164
165
166
167
168
169
170
171
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/mit-copyright.h>.
 *
 * Send packet to KDC for realm; wait for response, retransmitting
 * as necessary.
 */

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

#include <krb5/copyright.h>

#include <krb5/krb5.h>
#include <krb5/krb5_err.h>
#include <errno.h>
#include <krb5/ext-proto.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#define MAX_DGRAM_SIZE	4096		/* XXX! */
#define MAX_TIMEOUT 30			/* XXX */
#define TIMEOUT_SHIFT 2			/* XXX */

#include <stdio.h>
#include <krb5/libos-proto.h>
#include "os-proto.h"

/*
 * send the formatted request 'message' to a KDC for realm 'realm' and
 * return the response (if any) in 'reply'.
 *
 * If the message is sent and a response is received, 0 is returned,
 * otherwise an error code is returned.
 *
 * The storage for 'reply' is allocated and should be freed by the caller
 * when finished.
 *
 */
krb5_error_code
krb5_sendto_kdc (DECLARG(const krb5_data *, message),
		 DECLARG(const krb5_data *, realm),
		 DECLARG(krb5_data *, reply))
OLDDECLARG(const krb5_data *, message)
OLDDECLARG(const krb5_data *, realm)
OLDDECLARG(krb5_data *, reply)
{
    register int timeout, host, i;
    struct sockaddr *addr;
    int naddr;
    int sent, nready;
    krb5_error_code retval;
    int socklist[AF_MAX];		/* one for each, if necessary! */
    fd_set readable;
    struct timeval waitlen;
    struct sockaddr fromaddr;
    int fromlen, cc;

    /*
     * find KDC location(s) for realm
     */

    if (retval = krb5_locate_kdc (realm, &addr, &naddr))
	return retval;
    if (naddr == 0)
	return KRB5_REALM_UNKNOWN;
    
    for (i = 0; i < AF_MAX; i++)
	socklist[i] = -1;

    if (!(reply->data = malloc(MAX_DGRAM_SIZE))) { /* XXX size? */
	free((char *)addr);
	return ENOMEM;
    }
    reply->length = MAX_DGRAM_SIZE;

    /*
     * do exponential backoff.
     */

    for (timeout = 1; timeout < MAX_TIMEOUT; timeout <<=TIMEOUT_SHIFT) {
	sent = 0;
	for (host = 0; host < naddr; host++) {
	    /* send to the host, wait timeout seconds for a response,
	       then move on. */
	    /* cache some sockets for various address families in the
	       list */
	    if (socklist[addr[host].sa_family] == -1) {
		/* XXX 4.2/4.3BSD has PF_xxx = AF_xxx, so the socket
		   creation here will work properly... */
		socklist[addr[host].sa_family] = socket(addr[host].sa_family,
							SOCK_DGRAM,
							0); /* XXX always zero? */
		if (socklist[addr[host].sa_family] == -1)
		    continue;		/* try other hosts */
	    }
	    /* have a socket to send/recv from */
	    if (sendto(socklist[addr[host].sa_family],
		       message->data,
		       message->length,
		       0,
		       &addr[host],
		       sizeof(addr[host])) != message->length)
		continue;
	    else
		sent = 1;
	    waitlen.tv_usec = 0;
	    waitlen.tv_sec = timeout;
	    FD_ZERO(&readable);
	    FD_SET(socklist[addr[host].sa_family], &readable);
	    if (nready = select(1<<socklist[addr[host].sa_family],
				&readable,
				0,
				0,
				&waitlen)) {
		if (nready == -1)
		    continue;		/* XXX */
		if ((cc = recvfrom(socklist[addr[host].sa_family],
				   reply->data,
				   reply->length,
				   0,
				   &fromaddr,
				   &fromlen)) == -1)
		    continue;		/* XXX */
		if (bcmp((char *)&fromaddr, (char *)&addr[host],
			 fromlen)) {
		    /* not from this one, perhaps from an earlier
		       request? */
		    for (i = host-1; i >= 0; i--) {
			if (!bcmp((char *)&fromaddr, (char *)&addr[host],
				  fromlen))
			    break;
		    }
		    if (i < 0)		/* not from someone we asked */
			continue;	/* XXX */
		}
		/* reply came from where we sent a request,
		   so clean up and return. */
		reply->length = cc;
		retval = 0;
		goto out;
	    }
	    /* not ready, go on to next server */
	}
	if (!sent) {
	    /* never were able to send to any servers; give up */
	    retval = KRB5_KDC_UNREACH;
	    break;
	}
    }
    retval = KRB5_KDC_UNREACH;
 out:
    for (i = 0; i < AF_MAX; i++)
	if (socklist[i] != -1)
	    (void) close(socklist[i]);
    free((char *)addr);
    if (retval) {
	free(reply->data);
	reply->data = 0;
	reply->length = 0;
    }
    return retval;
}