summaryrefslogtreecommitdiff
path: root/kerberosIV/krb/getrealm.c
blob: 582c3d8f5b8439d2e4d325f05374c74bb91d29d9 (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
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
/*	$OpenBSD: getrealm.c,v 1.10 1998/02/25 15:51:15 art Exp $	*/
/* $KTH: getrealm.c,v 1.26 1997/10/08 22:51:13 joda Exp $ */

/*
 * This source code is no longer held under any constraint of USA
 * `cryptographic laws' since it was exported legally.  The cryptographic
 * functions were removed from the code and a "Bones" distribution was
 * made.  A Commodity Jurisdiction Request #012-94 was filed with the
 * USA State Department, who handed it to the Commerce department.  The
 * code was determined to fall under General License GTDA under ECCN 5D96G,
 * and hence exportable.  The cryptographic interfaces were re-added by Eric
 * Young, and then KTH proceeded to maintain the code in the free world.
 *
 */

/* 
 *  Copyright (C) 1989 by the Massachusetts Institute of Technology
 *
 *  Export of this software from the United States of America is assumed
 *  to 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.
 *
 */

#include "krb_locl.h"

#define MATCH_SUBDOMAINS        0

/*
 * krb_realmofhost.
 * Given a fully-qualified domain-style primary host name,
 * return the name of the Kerberos realm for the host.
 * If the hostname contains no discernable domain, or an error occurs,
 * return the local realm name, as supplied by get_krbrlm().
 * If the hostname contains a domain, but no translation is found,
 * the hostname's domain is converted to upper-case and returned.
 *
 * The format of each line of the translation file is:
 * domain_name kerberos_realm
 * -or-
 * host_name kerberos_realm
 *
 * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
 * host names should be in the usual form (e.g. FOO.BAR.BAZ)
 */

/* To automagically find the correct realm of a host (without
 * krb.realms) add a text record for your domain with the name of your
 * realm, like this:
 *
 * krb4-realm	IN	TXT	FOO.SE
 *
 * The search is recursive, so you can also add entries for specific
 * hosts. To find the realm of host a.b.c, it first tries
 * krb4-realm.a.b.c, then krb4-realm.b.c and so on.
 */

static int
dns_find_realm(char *hostname, char *realm)
{
    char domain[MAXHOSTNAMELEN + sizeof("krb4-realm..")];
    char *p;
    int level = 0;
    struct dns_reply *r;
    
    p = hostname;

    while(1){
	snprintf(domain, sizeof(domain), "krb4-realm.%s.", p);
	p = strchr(p, '.');
	if(p == NULL)
	    break;
	p++;
	r = dns_lookup(domain, "TXT");
	if(r){
	    struct resource_record *rr = r->head;
	    while(rr){
		if(rr->type == T_TXT){
		    strncpy(realm, rr->u.txt, REALM_SZ);
		    realm[REALM_SZ - 1] = 0;
		    dns_free_data(r);
		    return level;
		}
		rr = rr->next;
	    }
	    dns_free_data(r);
	}
	level++;
    }
    return -1;
}


static FILE *
open_krb_realms(void)
{
    int i;
    char file[128];
    FILE *res;
    for(i = 0; krb_get_krbrealms(i, file, sizeof(file)) == 0; i++)
	if ((res = fopen(file, "r")) != NULL)
	    return res;

    return NULL;
}

static int
file_find_realm(const char *phost, const char *domain, 
		char *ret_realm, size_t ret_realm_sz)
{
    FILE *trans_file;
    char buf[1024];
    int ret = -1;
    
    if ((trans_file = open_krb_realms()) == NULL)
	return -1;

    while (fgets(buf, sizeof(buf), trans_file) != NULL) {
	char *save = NULL;
	char *tok;
	char *tmp_host;
	char *tmp_realm;

        tok = strtok_r(buf, " \t\r\n", &save);
        if(tok == NULL)
            continue;
	tmp_host = tok;
	tok = strtok_r(NULL, " \t\r\n", &save);
	if(tok == NULL)
	    continue;
	tmp_realm = tok;
	if (strcasecmp(tmp_host, phost) == 0) {
	    /* exact match of hostname, so return the realm */
	    strncpy(ret_realm, tmp_realm, ret_realm_sz);
	    ret_realm[ret_realm_sz - 1] = '\0';
	    ret = 0;
	    break;
	}
	if ((tmp_host[0] == '.') && domain) { 
	    const char *cp = domain;
	    do {
		if(strcasecmp(tmp_host, domain) == 0){
		    /* domain match, save for later */ 
		    strncpy(ret_realm, tmp_realm, ret_realm_sz);
		    ret_realm[ret_realm_sz - 1] = '\0';
		    ret = 0;
		    break;
		}
		cp = strchr(cp + 1, '.');
	    } while(MATCH_SUBDOMAINS && cp);
	}
    }
    fclose(trans_file);
    return ret;
}

char *
krb_realmofhost(const char *host)
{
    static char ret_realm[REALM_SZ];
    char *domain;
    char phost[MAXHOSTNAMELEN];
	
    krb_name_to_name(host, phost, sizeof(phost));
	
    domain = strchr(phost, '.');

    if(file_find_realm(phost, domain, ret_realm, sizeof(ret_realm)) == 0)
	return ret_realm;

    if(dns_find_realm(phost, ret_realm) >= 0)
	return ret_realm;
  
    if (domain) {
	char *cp;
	  
	strncpy(ret_realm, &domain[1], REALM_SZ);
	ret_realm[REALM_SZ - 1] = 0;
	/* Upper-case realm */
	for (cp = ret_realm; *cp; cp++)
	    *cp = toupper(*cp);
    } else {
	krb_get_lrealm(ret_realm, 1);
    }
    return ret_realm;
}