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

  1. /*
  2.  * $Source: /usr/src/kerberosIV/krb/RCS/get_admhst.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_admhst_c =
  14. "$Header: /usr/src/kerberosIV/krb/RCS/get_admhst.c,v 4.1 90/06/25 20:55:48 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 <string.h>
  22.  
  23. /*
  24.  * Given a Kerberos realm, find a host on which the Kerberos database
  25.  * administration server can be found.
  26.  *
  27.  * krb_get_admhst 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 administrative host entry from the configuration
  30.  * file (KRB_CONF, defined in "krb.h") associated with the specified realm.
  31.  *
  32.  * On error, get_admhst returns KFAILURE. If all goes well, the routine
  33.  * returns KSUCCESS.
  34.  *
  35.  * For the format of the KRB_CONF file, see comments describing the routine
  36.  * krb_get_krbhst().
  37.  *
  38.  * This is a temporary hack to allow us to find the nearest system running
  39.  * a Kerberos admin server.  In the long run, this functionality will be
  40.  * provided by a nameserver.
  41.  */
  42.  
  43. krb_get_admhst(h, r, n)
  44.     char *h;
  45.     char *r;
  46.     int n;
  47. {
  48.     FILE *cnffile;
  49.     char tr[REALM_SZ];
  50.     char linebuf[BUFSIZ];
  51.     char scratch[64];
  52.     register int i;
  53.  
  54.     if ((cnffile = fopen(KRB_CONF,"r")) == NULL) {
  55.             return(KFAILURE);
  56.     }
  57.     if (fgets(linebuf, BUFSIZ, cnffile) == NULL) {
  58.     /* error reading */
  59.     (void) fclose(cnffile);
  60.     return(KFAILURE);
  61.     }
  62.     if (!index(linebuf, '\n')) {
  63.     /* didn't all fit into buffer, punt */
  64.     (void) fclose(cnffile);
  65.     return(KFAILURE);
  66.     }
  67.     for (i = 0; i < n; ) {
  68.     /* run through the file, looking for admin host */
  69.     if (fgets(linebuf, BUFSIZ, cnffile) == NULL) {
  70.             (void) fclose(cnffile);
  71.             return(KFAILURE);
  72.         }
  73.     /* need to scan for a token after 'admin' to make sure that
  74.        admin matched correctly */
  75.     if (sscanf(linebuf, "%s %s admin %s", tr, h, scratch) != 3)
  76.         continue;
  77.         if (!strcmp(tr,r))
  78.             i++;
  79.     }
  80.     (void) fclose(cnffile);
  81.     return(KSUCCESS);
  82. }
  83.