Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / getrealm.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
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.
8
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
18 or implied warranty.
19
20   */
21
22 #include "krb_locl.h"
23
24 RCSID("$Id: getrealm.c,v 1.36 1999/09/16 20:41:51 assar Exp $");
25
26 #ifndef MATCH_SUBDOMAINS
27 #define MATCH_SUBDOMAINS 0
28 #endif
29
30 /*
31  * krb_realmofhost.
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.
38  *
39  * The format of each line of the translation file is:
40  * domain_name kerberos_realm
41  * -or-
42  * host_name kerberos_realm
43  *
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)
46  */
47
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
50  * realm, like this:
51  *
52  * krb4-realm   IN      TXT     FOO.SE
53  *
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.
57  */
58
59 static int
60 dns_find_realm(char *hostname, char *realm)
61 {
62     char domain[MaxHostNameLen + sizeof("krb4-realm..")];
63     char *p;
64     int level = 0;
65     struct dns_reply *r;
66     
67     p = hostname;
68
69     while(1){
70         snprintf(domain, sizeof(domain), "krb4-realm.%s.", p);
71         p = strchr(p, '.');
72         if(p == NULL)
73             break;
74         p++;
75         r = dns_lookup(domain, "TXT");
76         if(r){
77             struct resource_record *rr = r->head;
78             while(rr){
79                 if(rr->type == T_TXT){
80                     strlcpy(realm, rr->u.txt, REALM_SZ);
81                     dns_free_data(r);
82                     return level;
83                 }
84                 rr = rr->next;
85             }
86             dns_free_data(r);
87         }
88         level++;
89     }
90     return -1;
91 }
92
93
94 static FILE *
95 open_krb_realms(void)
96 {
97     int i;
98     char file[MaxPathLen];
99     FILE *res;
100
101     for(i = 0; krb_get_krbrealms(i, file, sizeof(file)) == 0; i++)
102         if ((res = fopen(file, "r")) != NULL)
103             return res;
104   return NULL;
105 }
106
107 static int
108 file_find_realm(const char *phost, const char *domain,
109                 char *ret_realm, size_t ret_realm_sz)
110 {
111     FILE *trans_file;
112     char buf[1024];
113     int ret = -1;
114     
115     if ((trans_file = open_krb_realms()) == NULL)
116         return -1;
117
118     while (fgets(buf, sizeof(buf), trans_file) != NULL) {
119         char *save = NULL;
120         char *tok;
121         char *tmp_host;
122         char *tmp_realm;
123
124         tok = strtok_r(buf, " \t\r\n", &save);
125         if(tok == NULL)
126             continue;
127         tmp_host = tok;
128         tok = strtok_r(NULL, " \t\r\n", &save);
129         if(tok == NULL)
130             continue;
131         tmp_realm = tok;
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);
135             ret = 0;
136             break;
137         }
138         if ((tmp_host[0] == '.') && domain) { 
139             const char *cp = domain;
140             do {
141                 if(strcasecmp(tmp_host, cp) == 0){
142                     /* domain match, save for later */ 
143                     strlcpy(ret_realm, tmp_realm, ret_realm_sz);
144                     ret = 0;
145                     break;
146                 }
147                 cp = strchr(cp + 1, '.');
148             } while(MATCH_SUBDOMAINS && cp);
149         }
150         if (ret == 0)
151             break;
152     }
153     fclose(trans_file);
154     return ret;
155 }
156
157 char *
158 krb_realmofhost(const char *host)
159 {
160     static char ret_realm[REALM_SZ];
161     char *domain;
162     char phost[MaxHostNameLen];
163         
164     krb_name_to_name(host, phost, sizeof(phost));
165         
166     domain = strchr(phost, '.');
167
168     if(file_find_realm(phost, domain, ret_realm, sizeof ret_realm) == 0)
169         return ret_realm;
170
171     if(dns_find_realm(phost, ret_realm) >= 0)
172         return ret_realm;
173   
174     if (domain) {
175         char *cp;
176           
177         strlcpy(ret_realm, &domain[1], REALM_SZ);
178         /* Upper-case realm */
179         for (cp = ret_realm; *cp; cp++)
180             *cp = toupper(*cp);
181     } else {
182         strncpy(ret_realm, krb_get_default_realm(), REALM_SZ); /* Wild guess */
183     }
184     return ret_realm;
185 }