home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / krb / get_krbhst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-25  |  2.5 KB  |  88 lines

  1. /*
  2.  * $Source: /usr/src/kerberosIV/krb/RCS/get_krbhst.c,v $
  3.  * $Author: kfall $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char *rcsid_get_krbhst_c =
  14. "$Header: /usr/src/kerberosIV/krb/RCS/get_krbhst.c,v 4.9 90/06/25 20:55:54 kfall Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit-copyright.h>
  18. #include <stdio.h>
  19. #include <des.h>
  20. #include <krb.h>
  21. #include <strings.h>
  22.  
  23. /*
  24.  * Given a Kerberos realm, find a host on which the Kerberos authenti-
  25.  * cation server can be found.
  26.  *
  27.  * krb_get_krbhst takes a pointer to be filled in, a pointer to the name
  28.  * of the realm for which a server is desired, and an integer, n, and
  29.  * returns (in h) the nth entry from the configuration file (KRB_CONF,
  30.  * defined in "krb.h") associated with the specified realm.
  31.  *
  32.  * On end-of-file, krb_get_krbhst returns KFAILURE.  If n=1 and the
  33.  * configuration file does not exist, krb_get_krbhst will return KRB_HOST
  34.  * (also defined in "krb.h").  If all goes well, the routine returnes
  35.  * KSUCCESS.
  36.  *
  37.  * The KRB_CONF file contains the name of the local realm in the first
  38.  * line (not used by this routine), followed by lines indicating realm/host
  39.  * entries.  The words "admin server" following the hostname indicate that 
  40.  * the host provides an administrative database server.
  41.  *
  42.  * For example:
  43.  *
  44.  *    ATHENA.MIT.EDU
  45.  *    ATHENA.MIT.EDU kerberos-1.mit.edu admin server
  46.  *    ATHENA.MIT.EDU kerberos-2.mit.edu
  47.  *    LCS.MIT.EDU kerberos.lcs.mit.edu admin server
  48.  *
  49.  * This is a temporary hack to allow us to find the nearest system running
  50.  * kerberos.  In the long run, this functionality will be provided by a
  51.  * nameserver.
  52.  */
  53.  
  54. krb_get_krbhst(h,r,n)
  55.     char *h;
  56.     char *r;
  57.     int n;
  58. {
  59.     FILE *cnffile;
  60.     char tr[REALM_SZ];
  61.     char linebuf[BUFSIZ];
  62.     register int i;
  63.  
  64.     if ((cnffile = fopen(KRB_CONF,"r")) == NULL) {
  65.         if (n==1) {
  66.             (void) strcpy(h,KRB_HOST);
  67.             return(KSUCCESS);
  68.         }
  69.         else
  70.             return(KFAILURE);
  71.     }
  72.     if (fscanf(cnffile,"%s",tr) == EOF)
  73.         return(KFAILURE);
  74.     /* run through the file, looking for the nth server for this realm */
  75.     for (i = 1; i <= n;) {
  76.     if (fgets(linebuf, BUFSIZ, cnffile) == NULL) {
  77.             (void) fclose(cnffile);
  78.             return(KFAILURE);
  79.         }
  80.     if (sscanf(linebuf, "%s %s", tr, h) != 2)
  81.         continue;
  82.         if (!strcmp(tr,r))
  83.             i++;
  84.     }
  85.     (void) fclose(cnffile);
  86.     return(KSUCCESS);
  87. }
  88.