summaryrefslogtreecommitdiffstats
path: root/src/lib/krb4/realmofhost.c
diff options
context:
space:
mode:
authorMark Eichin <eichin@mit.edu>1995-04-27 15:51:19 +0000
committerMark Eichin <eichin@mit.edu>1995-04-27 15:51:19 +0000
commitf487a0a0f51ce55e769b2c6766662307dab9df0f (patch)
tree937704a125c9d9928f82ece186f73a77b4d32516 /src/lib/krb4/realmofhost.c
parent8c6a3a0d276bbb877c0a01b8ac8f18da25876b0b (diff)
downloadkrb5-f487a0a0f51ce55e769b2c6766662307dab9df0f.tar.gz
krb5-f487a0a0f51ce55e769b2c6766662307dab9df0f.tar.xz
krb5-f487a0a0f51ce55e769b2c6766662307dab9df0f.zip
add CNS V4 library to tree for compatibility use. Installs as libkrb4.a
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@5537 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/lib/krb4/realmofhost.c')
-rw-r--r--src/lib/krb4/realmofhost.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/lib/krb4/realmofhost.c b/src/lib/krb4/realmofhost.c
new file mode 100644
index 0000000000..2114b516a1
--- /dev/null
+++ b/src/lib/krb4/realmofhost.c
@@ -0,0 +1,141 @@
+/*
+ * realmofhost.c
+ *
+ * Copyright 1988 by the Massachusetts Institute of Technology.
+ *
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
+ *
+ * routine to convert hostname into realm name.
+ */
+
+#include "mit-copyright.h"
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <netdb.h> /* For struct hostent, gethostbyname, etc */
+#include <sys/param.h> /* For MAXHOSTNAMELEN */
+#ifdef POSIX
+#include <stdlib.h>
+#else
+extern char *malloc();
+#endif
+#define DEFINE_SOCKADDR /* Ask for MAXHOSTNAMELEN */
+#include "krb.h"
+
+/*
+ * 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)
+ */
+
+static char ret_realm[REALM_SZ+1];
+
+char * INTERFACE
+krb_realmofhost(host)
+char *host;
+{
+ char *domain;
+ FILE *trans_file;
+ FILE *krb__get_realmsfile();
+ char trans_host[MAXHOSTNAMELEN+1];
+ char trans_realm[REALM_SZ+1];
+ int retval;
+ struct hostent *h;
+ char *lhost;
+
+ /* First, canonicalize it. This is in case the caller
+ didn't have a fully qualified domain name. */
+ if ((h=gethostbyname(host)) == NULL)
+ lhost = host;
+ else {
+ lhost = h->h_name;
+#ifdef DO_REVERSE_RESOLVE
+ if (h->h_addr_list != NULL && h->h_addr_list[0] != NULL) {
+ char *rev_addr; int rev_type, rev_len;
+
+ rev_type = h->h_addrtype;
+ rev_len = h->h_length;
+ rev_addr = malloc(rev_len);
+ if (rev_addr != NULL) {
+ memcpy(rev_addr, h->h_addr_list[0], rev_len);
+ h = gethostbyaddr(rev_addr, rev_len, rev_type);
+ free(rev_addr);
+ if (h == NULL)
+ lhost = host;
+ else
+ lhost = h->h_name;
+ }
+ }
+#endif
+ }
+
+ domain = strchr(lhost, '.');
+
+ /* prepare default */
+ if (domain) {
+ char *cp;
+
+ /* If the domain is just below the top, e.g., CYGNUS.COM,
+ then we special-case it; if someone really wants a
+ realm called COM they will just have to specify it
+ properly. */
+ if (((cp = strchr(domain+1, '.')) == (char *) 0)
+ /* Handle root domain properly (COM.): */
+ || (*(cp + 1) == '\0'))
+ domain = lhost - 1; /* -1 fakes "period" before domain */
+
+ strncpy(ret_realm, domain+1, REALM_SZ);
+ ret_realm[REALM_SZ] = '\0';
+ /* Upper-case realm */
+ for (cp = ret_realm; *cp; cp++)
+ if (islower(*cp))
+ *cp = toupper(*cp);
+ } else {
+ krb_get_lrealm(ret_realm, 1);
+ }
+
+ if ((trans_file = krb__get_realmsfile()) == (FILE *) 0)
+ /* krb_errno = KRB_NO_TRANS */
+ return(ret_realm);
+
+ /* loop while not exact match, and more entries to read */
+ while (1) {
+ if ((retval = fscanf(trans_file, "%s %s",
+ trans_host, trans_realm)) != 2) {
+ if (retval == EOF)
+ break;
+ continue; /* ignore broken lines */
+ }
+ trans_host[MAXHOSTNAMELEN] = '\0';
+ trans_realm[REALM_SZ] = '\0';
+ if (trans_host[0] == '.') {
+ /* want domain match only */
+ if (domain && !strcasecmp (trans_host, domain)) {
+ /* got domain match, save for later */
+ (void) strcpy (ret_realm, trans_realm);
+ continue;
+ }
+ } else {
+ /* want exact match of hostname */
+ if (!strcasecmp (trans_host, lhost)) {
+ (void) strcpy (ret_realm, trans_realm);
+ break;
+ }
+ }
+ }
+ fclose (trans_file);
+ return ret_realm;
+}