home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / krb / getrealm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-05  |  2.6 KB  |  108 lines

  1. /*
  2.  * $Source: /usr/src/kerberosIV/krb/RCS/getrealm.c,v $
  3.  * $Author: kfall $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  *
  10.  * routine to convert hostname into realm name.
  11.  */
  12.  
  13. #ifndef    lint
  14. static char rcsid_getrealm_c[] =
  15. "$Id: getrealm.c,v 4.7 90/06/25 20:56:21 kfall Exp Locker: kfall $";
  16. #endif    lint
  17.  
  18. #include <mit-copyright.h>
  19. #include <strings.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <des.h>
  23. #include <krb.h>
  24. #include <sys/param.h>
  25.  
  26. /* for Ultrix and friends ... */
  27. #ifndef MAXHOSTNAMELEN
  28. #define MAXHOSTNAMELEN 64
  29. #endif
  30.  
  31. /*
  32.  * krb_realmofhost.
  33.  * Given a fully-qualified domain-style primary host name,
  34.  * return the name of the Kerberos realm for the host.
  35.  * If the hostname contains no discernable domain, or an error occurs,
  36.  * return the local realm name, as supplied by get_krbrlm().
  37.  * If the hostname contains a domain, but no translation is found,
  38.  * the hostname's domain is converted to upper-case and returned.
  39.  *
  40.  * The format of each line of the translation file is:
  41.  * domain_name kerberos_realm
  42.  * -or-
  43.  * host_name kerberos_realm
  44.  *
  45.  * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
  46.  * host names should be in the usual form (e.g. FOO.BAR.BAZ)
  47.  */
  48.  
  49. static char ret_realm[REALM_SZ+1];
  50.  
  51. char *
  52. krb_realmofhost(host)
  53. char *host;
  54. {
  55.     char *domain;
  56.     FILE *trans_file;
  57.     char trans_host[MAXHOSTNAMELEN+1];
  58.     char trans_realm[REALM_SZ+1];
  59.     int retval;
  60.  
  61.     domain = index(host, '.');
  62.  
  63.     /* prepare default */
  64.     if (domain) {
  65.         char *cp;
  66.  
  67.         strncpy(ret_realm, &domain[1], REALM_SZ);
  68.         ret_realm[REALM_SZ] = '\0';
  69.         /* Upper-case realm */
  70.         for (cp = ret_realm; *cp; cp++)
  71.             if (islower(*cp))
  72.                 *cp = toupper(*cp);
  73.     } else {
  74.         krb_get_lrealm(ret_realm, 0);
  75.     }
  76.  
  77.     if ((trans_file = fopen(KRB_RLM_TRANS, "r")) == (FILE *) 0) {
  78.         /* krb_errno = KRB_NO_TRANS */
  79.         return(ret_realm);
  80.     }
  81.     while (1) {
  82.         if ((retval = fscanf(trans_file, "%s %s",
  83.                      trans_host, trans_realm)) != 2) {
  84.             if (retval == EOF) {
  85.                 fclose(trans_file);
  86.                 return(ret_realm);
  87.             }
  88.             continue;    /* ignore broken lines */
  89.         }
  90.         trans_host[MAXHOSTNAMELEN] = '\0';
  91.         trans_realm[REALM_SZ] = '\0';
  92.         if (!strcasecmp(trans_host, host)) {
  93.             /* exact match of hostname, so return the realm */
  94.             (void) strcpy(ret_realm, trans_realm);
  95.             fclose(trans_file);
  96.             return(ret_realm);
  97.         }
  98.         if ((trans_host[0] == '.') && domain) { 
  99.             /* this is a domain match */
  100.             if (!strcasecmp(trans_host, domain)) {
  101.                 /* domain match, save for later */
  102.                 (void) strcpy(ret_realm, trans_realm);
  103.                 continue;
  104.             }
  105.         }
  106.     }
  107. }
  108.