5 * Copyright 1990 by the Massachusetts Institute of Technology.
7 * For copying and distribution information, please see the file
10 * krb5_get_host_realm()
13 #if !defined(lint) && !defined(SABER)
14 static char rcsid_hst_realm_c[] =
16 #endif /* !lint & !SABER */
18 #include <krb5/copyright.h>
21 Figures out the Kerberos realm names for host, filling in a
22 pointer to an argv[] style list of names, terminated with a null pointer.
24 If host is NULL, the local host's realms are determined.
26 If there are no known realms for the host, the filled-in pointer is set
29 The pointer array and strings pointed to are all in allocated storage,
30 and should be freed by the caller when finished.
36 * Implementation notes:
38 * this implementation only provides one realm per host, using the same
39 * mapping file used in kerberos v4.
41 * Given a fully-qualified domain-style primary host name,
42 * return the name of the Kerberos realm for the host.
43 * If the hostname contains no discernable domain, or an error occurs,
44 * return the local realm name, as supplied by krb5_get_default_realm().
45 * If the hostname contains a domain, but no translation is found,
46 * the hostname's domain is converted to upper-case and returned.
48 * The format of each line of the translation file is:
49 * domain_name kerberos_realm
51 * host_name kerberos_realm
53 * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
54 * host names should be in the usual form (e.g. FOO.BAR.BAZ)
58 #include <krb5/krb5.h>
59 #include <krb5/krb5_err.h>
60 #include <krb5/ext-proto.h>
62 #include <krb5/libos-proto.h>
65 #include <sys/param.h>
67 /* for old Unixes and friends ... */
68 #ifndef MAXHOSTNAMELEN
69 #define MAXHOSTNAMELEN 64
72 #define DEF_REALMNAME_SIZE 256
74 extern char *krb5_trans_file;
77 krb5_get_host_realm(host, realmsp)
84 char trans_host[MAXHOSTNAMELEN+1];
85 char trans_realm[DEF_REALMNAME_SIZE];
86 krb5_error_code retval;
88 char scanstring[7+2*16]; /* 7 chars + 16 for each decimal
91 if (!(retrealms = (char **)calloc(2, sizeof(*retrealms))))
93 domain = index(host, '.');
99 if (!(retrealms[0] = malloc(strlen(&domain[1])+1))) {
103 strcpy(retrealms[0], &domain[1]);
104 /* Upper-case realm */
105 for (cp = retrealms[0]; *cp; cp++)
109 if (!(retrealms[0] = malloc(DEF_REALMNAME_SIZE))) {
113 if (retval = krb5_get_default_realm(DEF_REALMNAME_SIZE-1,
121 if ((trans_file = fopen(krb5_trans_file, "r")) == (FILE *) 0) {
124 return KRB5_TRANS_CANTOPEN;
126 (void) sprintf(scanstring, "%%%ds %%%ds",
127 sizeof(trans_host)-1,sizeof(trans_realm)-1);
129 if ((scanval = fscanf(trans_file, scanstring,
130 trans_host, trans_realm)) != 2) {
131 if (scanval == EOF) {
135 continue; /* ignore broken lines */
137 trans_host[sizeof(trans_host)-1] = '\0';
138 trans_realm[sizeof(trans_realm)-1] = '\0';
139 if (!strcasecmp(trans_host, host)) {
140 /* exact match of hostname, so return the realm */
141 if (!(retrealms[0] = realloc(retrealms[0],
142 strlen(trans_realm)+1))) {
146 (void) strcpy(retrealms[0], trans_realm);
150 if ((trans_host[0] == '.') && domain) {
151 /* this is a possible domain match */
152 if (!strcasecmp(trans_host, domain)) {
153 /* domain match, save for later */
154 if (!(retrealms[0] = realloc(retrealms[0],
155 strlen(trans_realm)+1))) {
159 (void) strcpy(retrealms[0], trans_realm);
165 *realmsp = retrealms;