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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/*
* lib/krb5/os/locate_kdc.c
*
* 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.
*/
#define NEED_SOCKETS
#include "k5-int.h"
#include <stdio.h>
#ifdef KRB5_DNS_LOOKUP
#ifdef WSHELPER
#include <wshelper.h>
#else /* WSHELPER */
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <netdb.h>
#endif /* WSHELPER */
#ifndef T_SRV
#define T_SRV 33
#endif /* T_SRV */
/* for old Unixes and friends ... */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
#endif /* KRB5_DNS_LOOKUP */
/*
* returns count of number of addresses found
* if master is non-NULL, it is filled in with the index of
* the master kdc
*/
static krb5_error_code
krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmasters)
krb5_context context;
const krb5_data *realm;
const char * name;
struct sockaddr **addr_pp;
int *naddrs;
int *master_index;
int *nmasters;
{
const char *realm_srv_names[4];
char **masterlist, **hostlist, *host, *port, *cp;
krb5_error_code code;
int i, j, out, count, ismaster;
struct sockaddr *addr_p;
struct sockaddr_in *sin_p;
struct hostent *hp;
struct servent *sp;
#ifdef HAVE_NETINET_IN_H
u_short udpport = htons(KRB5_DEFAULT_PORT);
u_short sec_udpport = htons(KRB5_DEFAULT_SEC_PORT);
#endif
if ((host = malloc(realm->length + 1)) == NULL)
return ENOMEM;
strncpy(host, realm->data, realm->length);
host[realm->length] = '\0';
hostlist = 0;
masterlist = NULL;
realm_srv_names[0] = "realms";
realm_srv_names[1] = host;
realm_srv_names[2] = name;
realm_srv_names[3] = 0;
code = profile_get_values(context->profile, realm_srv_names, &hostlist);
if (code) {
if (code == PROF_NO_SECTION || code == PROF_NO_RELATION)
code = KRB5_REALM_UNKNOWN;
krb5_xfree(host);
return code;
}
#ifdef HAVE_NETINET_IN_H
if ((sp = getservbyname(KDC_PORTNAME, "udp")))
udpport = sp->s_port;
if ((sp = getservbyname(KDC_SECONDARY_PORTNAME, "udp")))
sec_udpport = sp->s_port;
#endif
if (sec_udpport == udpport)
sec_udpport = 0;
count = 0;
while (hostlist && hostlist[count])
count++;
if (count == 0) {
krb5_xfree(host);
*naddrs = 0;
return 0;
}
if (master_index) {
*master_index = 0;
*nmasters = 0;
realm_srv_names[0] = "realms";
realm_srv_names[1] = host;
realm_srv_names[2] = "admin_server";
realm_srv_names[3] = 0;
code = profile_get_values(context->profile, realm_srv_names,
&masterlist);
krb5_xfree(host);
if (code == 0) {
for (i=0; masterlist[i]; i++) {
host = masterlist[i];
/*
* Strip off excess whitespace
*/
cp = strchr(host, ' ');
if (cp)
*cp = 0;
cp = strchr(host, '\t');
if (cp)
*cp = 0;
cp = strchr(host, ':');
if (cp)
*cp = 0;
}
}
} else {
krb5_xfree(host);
}
/* at this point, if master is non-NULL, then either the master kdc
is required, and there is one, or the master kdc is not required,
and there may or may not be one. */
#ifdef HAVE_NETINET_IN_H
if (sec_udpport)
count = count * 2;
#endif
addr_p = (struct sockaddr *)malloc (sizeof (struct sockaddr) * count);
if (addr_p == NULL)
return ENOMEM;
for (i=0, out=0; hostlist[i]; i++) {
host = hostlist[i];
/*
* Strip off excess whitespace
*/
cp = strchr(host, ' ');
if (cp)
*cp = 0;
cp = strchr(host, '\t');
if (cp)
*cp = 0;
port = strchr(host, ':');
if (port) {
*port = 0;
port++;
}
if ((hp = gethostbyname(hostlist[i])) == 0) {
free(hostlist[i]);
hostlist[i] = 0;
continue;
}
ismaster = 0;
if (masterlist) {
for (j=0; masterlist[j]; j++) {
if (strcasecmp(hostlist[i], masterlist[j]) == 0) {
*master_index = out;
ismaster = 1;
}
}
}
switch (hp->h_addrtype) {
#ifdef HAVE_NETINET_IN_H
case AF_INET:
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 = port ? htons(atoi(port)) : udpport;
memcpy((char *)&sin_p->sin_addr,
(char *)hp->h_addr_list[j],
sizeof(struct in_addr));
if (out+1 >= count) {
count += 5;
addr_p = (struct sockaddr *)
realloc ((char *)addr_p,
sizeof(struct sockaddr) * count);
if (addr_p == NULL)
return ENOMEM;
}
if (sec_udpport && !port) {
addr_p[out] = addr_p[out-1];
sin_p = (struct sockaddr_in *) &addr_p[out++];
sin_p->sin_port = sec_udpport;
}
}
break;
#endif
default:
break;
}
if (ismaster)
*nmasters = out - *master_index;
/* Free the hostlist entry we are looping over. */
free(hostlist[i]);
hostlist[i] = 0;
}
if (masterlist) {
for (i=0; masterlist[i]; i++)
free(masterlist[i]);
free(masterlist);
}
free ((char *)hostlist);
if (out == 0) { /* Couldn't resolve any KDC names */
free (addr_p);
return KRB5_REALM_CANT_RESOLVE;
}
*addr_pp = addr_p;
*naddrs = out;
return 0;
}
#ifdef KRB5_DNS_LOOKUP
/*
* Lookup a KDC via DNS SRV records
*/
static krb5_error_code
krb5_locate_srv_dns(realm, service, protocol, addr_pp, naddrs)
const krb5_data *realm;
const char *service;
const char *protocol;
struct sockaddr **addr_pp;
int *naddrs;
{
krb5_error_code code;
int out, j, count;
union {
unsigned char bytes[2048];
HEADER hdr;
} answer;
unsigned char *p=NULL;
char host[MAX_DNS_NAMELEN];
struct sockaddr *addr = NULL;
struct sockaddr_in *sin = NULL;
struct hostent *hp = NULL;
int type, class;
int status, priority, weight, size, len, numanswers, numqueries, rdlen;
unsigned short port;
const int hdrsize = sizeof(HEADER);
struct srv_dns_entry {
struct srv_dns_entry *next;
int priority;
int weight;
unsigned short port;
char *host;
};
struct srv_dns_entry *head = NULL;
struct srv_dns_entry *srv = NULL, *entry = NULL;
out = 0;
addr = (struct sockaddr *) malloc(sizeof(struct sockaddr));
if (addr == NULL)
return ENOMEM;
count = 1;
/*
* First off, build a query of the form:
*
* service.protocol.realm
*
* which will most likely be something like:
*
* _kerberos._udp.REALM
*
*/
if ( strlen(service) + strlen(protocol) + realm->length + 5
> MAX_DNS_NAMELEN )
goto out;
sprintf(host, "%s.%s.%.*s", service, protocol, realm->length,
realm->data);
size = res_search(host, C_IN, T_SRV, answer.bytes, sizeof(answer.bytes));
if (size < hdrsize)
goto out;
/*
* We got an answer! First off, parse the header and figure out how
* many answers we got back.
*/
p = answer.bytes;
numqueries = ntohs(answer.hdr.qdcount);
numanswers = ntohs(answer.hdr.ancount);
p += sizeof(HEADER);
/*
* We need to skip over all of the questions, so we have to iterate
* over every query record. dn_expand() is able to tell us the size
* of compress DNS names, so we use it.
*/
#define INCR_CHECK(x,y) x += y; if (x > size + answer.bytes) goto out
#define CHECK(x,y) if (x + y > size + answer.bytes) goto out
#define NTOHSP(x,y) x[0] << 8 | x[1]; x += y
while (numqueries--) {
len = dn_expand(answer.bytes, answer.bytes + size, p, host, sizeof(host));
if (len < 0)
goto out;
INCR_CHECK(p, len + 4);
}
/*
* We're now pointing at the answer records. Only process them if
* they're actually T_SRV records (they might be CNAME records,
* for instance).
*
* But in a DNS reply, if you get a CNAME you always get the associated
* "real" RR for that CNAME. RFC 1034, 3.6.2:
*
* CNAME RRs cause special action in DNS software. When a name server
* fails to find a desired RR in the resource set associated with the
* domain name, it checks to see if the resource set consists of a CNAME
* record with a matching class. If so, the name server includes the CNAME
* record in the response and restarts the query at the domain name
* specified in the data field of the CNAME record. The one exception to
* this rule is that queries which match the CNAME type are not restarted.
*
* In other words, CNAMEs do not need to be expanded by the client.
*/
while (numanswers--) {
/* First is the name; use dn_expand to get the compressed size */
len = dn_expand(answer.bytes, answer.bytes + size, p, host, sizeof(host));
if (len < 0)
goto out;
INCR_CHECK(p, len);
/* Next is the query type */
CHECK(p, 2);
type = NTOHSP(p,2);
/* Next is the query class; also skip over 4 byte TTL */
CHECK(p, 6);
class = NTOHSP(p,6);
/* Record data length */
CHECK(p,2);
rdlen = NTOHSP(p,2);
/*
* If this is an SRV record, process it. Record format is:
*
* Priority
* Weight
* Port
* Server name
*/
if (class == C_IN && type == T_SRV) {
CHECK(p,2);
priority = NTOHSP(p,2);
CHECK(p, 2);
weight = NTOHSP(p,2);
CHECK(p, 2);
port = NTOHSP(p,2);
len = dn_expand(answer.bytes, answer.bytes + size, p, host, sizeof(host));
if (len < 0)
goto out;
INCR_CHECK(p, len);
/*
* We got everything! Insert it into our list, but make sure
* it's in the right order. Right now we don't do anything
* with the weight field
*/
srv = (struct srv_dns_entry *) malloc(sizeof(struct srv_dns_entry));
if (srv == NULL)
goto out;
srv->priority = priority;
srv->weight = weight;
srv->port = port;
srv->host = strdup(host);
if (head == NULL || head->priority > srv->priority) {
srv->next = head;
head = srv;
} else
/*
* This is confusing. Only insert an entry into this
* spot if:
* The next person has a higher priority (lower priorities
* are preferred).
* Or
* There is no next entry (we're at the end)
*/
for (entry = head; entry != NULL; entry = entry->next)
if ((entry->next &&
entry->next->priority > srv->priority) ||
entry->next == NULL) {
srv->next = entry->next;
entry->next = srv;
break;
}
} else
INCR_CHECK(p, rdlen);
}
/*
* Okay! Now we've got a linked list of entries sorted by
* priority. Start looking up A records and returning
* addresses.
*/
if (head == NULL)
goto out;
for (entry = head; entry != NULL; entry = entry->next) {
hp = gethostbyname(entry->host);
if (hp != 0) {
switch (hp->h_addrtype) {
#ifdef HAVE_NETINET_IN_H
case AF_INET:
for (j=0; hp->h_addr_list[j]; j++) {
sin = (struct sockaddr_in *) &addr[out++];
memset ((char *) sin, 0, sizeof (struct sockaddr));
sin->sin_family = hp->h_addrtype;
sin->sin_port = htons(entry->port);
memcpy((char *) &sin->sin_addr,
(char *) hp->h_addr_list[j],
sizeof(struct in_addr));
if (out + 1 >= count) {
count += 5;
addr = (struct sockaddr *)
realloc((char *) addr,
sizeof(struct sockaddr) * count);
if (!addr)
goto out;
}
}
break;
#endif /* HAVE_NETINET_IN_H */
default:
break;
}
}
}
for (entry = head; entry != NULL; ) {
free(entry->host);
entry->host = NULL;
srv = entry;
entry = entry->next;
free(srv);
srv = NULL;
}
out:
if (srv)
free(srv);
if (out == 0) { /* No good servers */
if (addr)
free(addr);
return KRB5_REALM_CANT_RESOLVE;
}
*addr_pp = addr;
*naddrs = out;
return 0;
}
#endif /* KRB5_DNS_LOOKUP */
/*
* Wrapper function for the two backends
*/
krb5_error_code
krb5_locate_kdc(context, realm, addr_pp, naddrs, master_index, nmasters)
krb5_context context;
const krb5_data *realm;
struct sockaddr **addr_pp;
int *naddrs;
int *master_index;
int *nmasters;
{
krb5_error_code code;
#ifdef KRB5_DNS_LOOKUP
struct sockaddr *admin_addr_p, *kdc_addr_p;
int nadmin_addrs, nkdc_addrs;
int i,j;
#endif /* KRB5_DNS_LOOKUP */
/*
* We always try the local file first
*/
code = krb5_locate_srv_conf(context, realm, "kdc", addr_pp, naddrs,
master_index, nmasters);
#ifdef KRB5_DNS_LOOKUP
if (code) {
int use_dns=0;
char * string=NULL;
krb5_error_code code2; /* preserve error code from krb5_locate_srv_conf */
code2 = profile_get_string(context->profile, "libdefaults",
"dns_fallback", 0,
context->profile_in_memory?"1":"0",
&string);
if ( code2 )
return(code2);
if ( string ) {
use_dns = krb5_conf_boolean(string);
free(string);
}
if ( use_dns ) {
code = krb5_locate_srv_dns(realm, "_kerberos", "_udp",
addr_pp, naddrs);
if ( master_index && nmasters ) {
code = krb5_locate_srv_dns(realm, "_kerberos-adm", "_tcp",
&admin_addr_p, &nadmin_addrs);
if ( code ) {
free(*addr_pp);
*addr_pp = NULL;
*naddrs = 0;
return(code);
}
kdc_addr_p = *addr_pp;
nkdc_addrs = *naddrs;
*naddrs = 0;
*addr_pp = (struct sockaddr *) malloc(sizeof(*kdc_addr_p));
if ( *addr_pp == NULL ) {
free(kdc_addr_p);
free(admin_addr_p);
return ENOMEM;
}
for ( i=0; i<nkdc_addrs; i++ ) {
for ( j=0 ; j<nadmin_addrs; j++) {
if ( !memcmp(&kdc_addr_p[i].sa_data[2],&admin_addr_p[j].sa_data[2],4) ) {
memcpy(&(*addr_pp)[(*naddrs)],&kdc_addr_p[i],
sizeof(struct sockaddr));
(*naddrs)++;
break;
}
}
}
free(kdc_addr_p);
free(admin_addr_p);
if ( *naddrs == 0 ) {
free(*addr_pp);
*addr_pp = NULL;
return KRB5_REALM_CANT_RESOLVE;
}
*master_index = 1;
*nmasters = *naddrs;
}
}
}
#endif /* KRB5_DNS_LOOKUP */
return (code);
}
#if 0 /* Why is this useful? It's not used now, and it's certainly
not useful if you don't have the DNS code enabled. -KR */
/*
* It turns out that it is really useful to be able to use these functions
* for other things (like admin servers), so create an abstract function
* for this
*/
krb5_error_code
krb5_locate_server(realm, name, proto, addr_pp, naddrs)
const krb5_data *realm;
const char *name, *proto;
struct sockaddr **addr_pp;
int *naddrs;
{
krb5_error_code code = KRB5_REALM_UNKNOWN;
#ifdef KRB5_DNS_LOOKUP
code = krb5_locate_srv_dns(realm, name, proto,
(struct sockaddr **) addr_pp, naddrs);
#endif /* KRB5_DNS_LOOKUP */
return (code);
}
#endif
|