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

  1. /*
  2.  * $Source: /usr/src/kerberosIV/krb/RCS/read_service_key.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_read_service_key_c =
  14. "$Id: read_service_key.c,v 4.10 90/06/25 20:57:14 kfall Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit-copyright.h>
  18. #include <des.h>
  19. #include <krb.h>
  20. #include <stdio.h>
  21. #include <strings.h>
  22.  
  23. /*
  24.  * The private keys for servers on a given host are stored in a
  25.  * "srvtab" file (typically "/etc/srvtab").  This routine extracts
  26.  * a given server's key from the file.
  27.  *
  28.  * read_service_key() takes the server's name ("service"), "instance",
  29.  * and "realm" and a key version number "kvno", and looks in the given
  30.  * "file" for the corresponding entry, and if found, returns the entry's
  31.  * key field in "key".
  32.  * 
  33.  * If "instance" contains the string "*", then it will match
  34.  * any instance, and the chosen instance will be copied to that
  35.  * string.  For this reason it is important that the there is enough
  36.  * space beyond the "*" to receive the entry.
  37.  *
  38.  * If "kvno" is 0, it is treated as a wild card and the first
  39.  * matching entry regardless of the "vno" field is returned.
  40.  *
  41.  * This routine returns KSUCCESS on success, otherwise KFAILURE.
  42.  *
  43.  * The format of each "srvtab" entry is as follows:
  44.  *
  45.  * Size            Variable        Field in file
  46.  * ----            --------        -------------
  47.  * string        serv            server name
  48.  * string        inst            server instance
  49.  * string        realm            server realm
  50.  * 1 byte        vno            server key version #
  51.  * 8 bytes        key            server's key
  52.  * ...            ...            ...
  53.  */
  54.  
  55.  
  56. /*ARGSUSED */
  57. read_service_key(service,instance,realm,kvno,file,key)
  58.     char *service;              /* Service Name */
  59.     char *instance;             /* Instance name or "*" */
  60.     char *realm;                /* Realm */
  61.     int kvno;                   /* Key version number */
  62.     char *file;                 /* Filename */
  63.     char *key;                  /* Pointer to key to be filled in */
  64. {
  65.     char serv[SNAME_SZ];
  66.     char inst[INST_SZ];
  67.     char rlm[REALM_SZ];
  68.     unsigned char vno;          /* Key version number */
  69.     int wcard;
  70.  
  71.     int stab, open();
  72.  
  73.     if ((stab = open(file, 0, 0)) < NULL)
  74.         return(KFAILURE);
  75.  
  76.     wcard = (instance[0] == '*') && (instance[1] == '\0');
  77.  
  78.     while(getst(stab,serv,SNAME_SZ) > 0) { /* Read sname */
  79.         (void) getst(stab,inst,INST_SZ); /* Instance */
  80.         (void) getst(stab,rlm,REALM_SZ); /* Realm */
  81.         /* Vers number */
  82.         if (read(stab,(char *)&vno,1) != 1)
  83.             return(KFAILURE);
  84.         /* Key */
  85.         if (read(stab,key,8) != 8)
  86.             return(KFAILURE);
  87.         /* Is this the right service */
  88.         if (strcmp(serv,service))
  89.             continue;
  90.         /* How about instance */
  91.         if (!wcard && strcmp(inst,instance))
  92.             continue;
  93.         if (wcard)
  94.             (void) strncpy(instance,inst,INST_SZ);
  95.         /* Is this the right realm */
  96. #ifdef ATHENA_COMPAT
  97.     /* XXX For backward compatibility:  if keyfile says "Athena"
  98.        and caller wants "ATHENA.MIT.EDU", call it a match */
  99.         if (strcmp(rlm,realm) &&
  100.         (strcmp(rlm,"Athena") ||
  101.          strcmp(realm,"ATHENA.MIT.EDU")))
  102.         continue;
  103. #else /* ! ATHENA_COMPAT */
  104.         if (strcmp(rlm,realm)) 
  105.         continue;
  106. #endif /* ATHENA_COMPAT */
  107.  
  108.         /* How about the key version number */
  109.         if (kvno && kvno != (int) vno)
  110.             continue;
  111.  
  112.         (void) close(stab);
  113.         return(KSUCCESS);
  114.     }
  115.  
  116.     /* Can't find the requested service */
  117.     (void) close(stab);
  118.     return(KFAILURE);
  119. }
  120.