2 * lib/krb5/os/hst_realm.c
4 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * krb5_get_host_realm()
29 Figures out the Kerberos realm names for host, filling in a
30 pointer to an argv[] style list of names, terminated with a null pointer.
32 If host is NULL, the local host's realms are determined.
34 If there are no known realms for the host, the filled-in pointer is set
37 The pointer array and strings pointed to are all in allocated storage,
38 and should be freed by the caller when finished.
44 * Implementation notes:
46 * this implementation only provides one realm per host, using the same
47 * mapping file used in kerberos v4.
49 * Given a fully-qualified domain-style primary host name,
50 * return the name of the Kerberos realm for the host.
51 * If the hostname contains no discernable domain, or an error occurs,
52 * return the local realm name, as supplied by krb5_get_default_realm().
53 * If the hostname contains a domain, but no translation is found,
54 * the hostname's domain is converted to upper-case and returned.
56 * The format of each line of the translation file is:
57 * domain_name kerberos_realm
59 * host_name kerberos_realm
61 * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
62 * host names should be in the usual form (e.g. FOO.BAR.BAZ)
75 /* for old Unixes and friends ... */
76 #ifndef MAXHOSTNAMELEN
77 #define MAXHOSTNAMELEN 64
80 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
81 krb5_get_host_realm(context, host, realmsp)
84 char FAR * FAR * FAR *realmsp;
87 char *default_realm, *realm, *cp;
88 krb5_error_code retval;
90 char local_host[MAXHOSTNAMELEN+1];
93 strncpy(local_host, host, MAXHOSTNAMELEN);
95 if (gethostname(local_host, sizeof(local_host)-1) == -1)
98 local_host[sizeof(local_host)-1] = '\0';
99 for (cp = local_host; *cp; cp++) {
103 l = strlen(local_host);
104 /* strip off trailing dot */
105 if (l && local_host[l-1] == '.')
109 Search for the best match for the host or domain.
110 Example: Given a host a.b.c.d, try to match on:
121 realm = default_realm = (char *)NULL;
123 retval = profile_get_string(context->profile, "domain_realm", cp,
124 0, (char *)NULL, &realm);
127 if (realm != (char *)NULL)
128 break; /* Match found */
130 /* Setup for another test */
133 if (default_realm == (char *)NULL) {
134 /* If nothing else works, use the host's domain */
138 cp = strchr(cp, '.');
142 if (realm == (char *)NULL) {
143 if (default_realm != (char *)NULL) {
144 /* We are defaulting to the realm of the host */
145 if (!(cp = (char *)malloc(strlen(default_realm)+1)))
147 strcpy(cp, default_realm);
150 /* Assume the realm name is upper case */
151 for (cp = realm; *cp; cp++)
155 /* We are defaulting to the local realm */
156 retval = krb5_get_default_realm(context, &realm);
162 if (!(retrealms = (char **)calloc(2, sizeof(*retrealms)))) {
163 if (realm != (char *)NULL)
168 retrealms[0] = realm;
171 *realmsp = retrealms;