2 Copyright (C) 1989 by the Massachusetts Institute of Technology
4 Export of this software from the United States of America is assumed
5 to require a specific license from the United States Government.
6 It is the responsibility of any person or organization contemplating
7 export to obtain such a license before exporting.
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission. M.I.T. makes no representations about the suitability of
17 this software for any purpose. It is provided "as is" without express
24 RCSID("$Id: read_service_key.c,v 1.12 1999/09/16 20:41:54 assar Exp $");
27 * The private keys for servers on a given host are stored in a
28 * "srvtab" file (typically "/etc/srvtab"). This routine extracts
29 * a given server's key from the file.
31 * read_service_key() takes the server's name ("service"), "instance",
32 * and "realm" and a key version number "kvno", and looks in the given
33 * "file" for the corresponding entry, and if found, returns the entry's
36 * If "instance" contains the string "*", then it will match
37 * any instance, and the chosen instance will be copied to that
38 * string. For this reason it is important that the there is enough
39 * space beyond the "*" to receive the entry.
41 * If "kvno" is 0, it is treated as a wild card and the first
42 * matching entry regardless of the "vno" field is returned.
44 * This routine returns KSUCCESS on success, otherwise KFAILURE.
46 * The format of each "srvtab" entry is as follows:
48 * Size Variable Field in file
49 * ---- -------- -------------
50 * string serv server name
51 * string inst server instance
52 * string realm server realm
53 * 1 byte vno server key version #
54 * 8 bytes key server's key
60 read_service_key(const char *service, /* Service Name */
61 char *instance, /* Instance name or "*" */
62 const char *realm, /* Realm */
63 int kvno, /* Key version number */
64 const char *file, /* Filename */
65 void *key) /* Pointer to key to be filled in */
70 unsigned char vno; /* Key version number */
75 if ((stab = open(file, O_RDONLY, 0)) < 0)
78 wcard = (instance[0] == '*') && (instance[1] == '\0');
80 while (getst(stab,serv,SNAME_SZ) > 0) { /* Read sname */
81 getst(stab,inst,INST_SZ); /* Instance */
82 getst(stab,rlm,REALM_SZ); /* Realm */
84 if (read(stab, &vno, 1) != 1) {
89 if (read(stab,key,8) != 8) {
93 /* Is this the right service */
94 if (strcmp(serv,service))
96 /* How about instance */
97 if (!wcard && strcmp(inst,instance))
100 strlcpy (instance, inst, INST_SZ);
102 /* Is this the right realm */
103 if (strcmp(rlm,realm))
106 /* How about the key version number */
107 if (kvno && kvno != (int) vno)
114 /* Can't find the requested service */