2 Copyright (C) 1989 by the Massachusetts Institute of Technology
4 Export of this software from the United States of America is assumed
5 to require a specific license from the United States Government.
6 It is the responsibility of any person or organization contemplating
7 export to obtain such a license before exporting.
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission. M.I.T. makes no representations about the suitability of
17 this software for any purpose. It is provided "as is" without express
24 RCSID("$Id: getrealm.c,v 1.36 1999/09/16 20:41:51 assar Exp $");
26 #ifndef MATCH_SUBDOMAINS
27 #define MATCH_SUBDOMAINS 0
32 * Given a fully-qualified domain-style primary host name,
33 * return the name of the Kerberos realm for the host.
34 * If the hostname contains no discernable domain, or an error occurs,
35 * return the local realm name, as supplied by get_krbrlm().
36 * If the hostname contains a domain, but no translation is found,
37 * the hostname's domain is converted to upper-case and returned.
39 * The format of each line of the translation file is:
40 * domain_name kerberos_realm
42 * host_name kerberos_realm
44 * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
45 * host names should be in the usual form (e.g. FOO.BAR.BAZ)
48 /* To automagically find the correct realm of a host (without
49 * krb.realms) add a text record for your domain with the name of your
52 * krb4-realm IN TXT FOO.SE
54 * The search is recursive, so you can also add entries for specific
55 * hosts. To find the realm of host a.b.c, it first tries
56 * krb4-realm.a.b.c, then krb4-realm.b.c and so on.
60 dns_find_realm(char *hostname, char *realm)
62 char domain[MaxHostNameLen + sizeof("krb4-realm..")];
70 snprintf(domain, sizeof(domain), "krb4-realm.%s.", p);
75 r = dns_lookup(domain, "TXT");
77 struct resource_record *rr = r->head;
79 if(rr->type == T_TXT){
80 strlcpy(realm, rr->u.txt, REALM_SZ);
98 char file[MaxPathLen];
101 for(i = 0; krb_get_krbrealms(i, file, sizeof(file)) == 0; i++)
102 if ((res = fopen(file, "r")) != NULL)
108 file_find_realm(const char *phost, const char *domain,
109 char *ret_realm, size_t ret_realm_sz)
115 if ((trans_file = open_krb_realms()) == NULL)
118 while (fgets(buf, sizeof(buf), trans_file) != NULL) {
124 tok = strtok_r(buf, " \t\r\n", &save);
128 tok = strtok_r(NULL, " \t\r\n", &save);
132 if (strcasecmp(tmp_host, phost) == 0) {
133 /* exact match of hostname, so return the realm */
134 strlcpy(ret_realm, tmp_realm, ret_realm_sz);
138 if ((tmp_host[0] == '.') && domain) {
139 const char *cp = domain;
141 if(strcasecmp(tmp_host, cp) == 0){
142 /* domain match, save for later */
143 strlcpy(ret_realm, tmp_realm, ret_realm_sz);
147 cp = strchr(cp + 1, '.');
148 } while(MATCH_SUBDOMAINS && cp);
158 krb_realmofhost(const char *host)
160 static char ret_realm[REALM_SZ];
162 char phost[MaxHostNameLen];
164 krb_name_to_name(host, phost, sizeof(phost));
166 domain = strchr(phost, '.');
168 if(file_find_realm(phost, domain, ret_realm, sizeof ret_realm) == 0)
171 if(dns_find_realm(phost, ret_realm) >= 0)
177 strlcpy(ret_realm, &domain[1], REALM_SZ);
178 /* Upper-case realm */
179 for (cp = ret_realm; *cp; cp++)
182 strncpy(ret_realm, krb_get_default_realm(), REALM_SZ); /* Wild guess */