Change __signed to signed.
[dragonfly.git] / crypto / kerberosIV / lib / krb / read_service_key.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
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.
8
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
18 or implied warranty.
19
20   */
21
22 #include "krb_locl.h"
23
24 RCSID("$Id: read_service_key.c,v 1.12 1999/09/16 20:41:54 assar Exp $");
25
26 /*
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.
30  *
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
34  * key field in "key".
35  * 
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.
40  *
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.
43  *
44  * This routine returns KSUCCESS on success, otherwise KFAILURE.
45  *
46  * The format of each "srvtab" entry is as follows:
47  *
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
55  * ...                  ...                     ...
56  */
57
58
59 int
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 */
66 {
67     char serv[SNAME_SZ];
68     char inst[INST_SZ];
69     char rlm[REALM_SZ];
70     unsigned char vno;          /* Key version number */
71     int wcard;
72
73     int stab;
74
75     if ((stab = open(file, O_RDONLY, 0)) < 0)
76         return(KFAILURE);
77
78     wcard = (instance[0] == '*') && (instance[1] == '\0');
79
80     while (getst(stab,serv,SNAME_SZ) > 0) { /* Read sname */
81         getst(stab,inst,INST_SZ); /* Instance */
82         getst(stab,rlm,REALM_SZ); /* Realm */
83         /* Vers number */
84         if (read(stab, &vno, 1) != 1) {
85             close(stab);
86             return(KFAILURE);
87         }
88         /* Key */
89         if (read(stab,key,8) != 8) {
90             close(stab);
91             return(KFAILURE);
92         }
93         /* Is this the right service */
94         if (strcmp(serv,service))
95             continue;
96         /* How about instance */
97         if (!wcard && strcmp(inst,instance))
98             continue;
99         if (wcard) {
100             strlcpy (instance, inst, INST_SZ);
101         }
102         /* Is this the right realm */
103         if (strcmp(rlm,realm)) 
104             continue;
105
106         /* How about the key version number */
107         if (kvno && kvno != (int) vno)
108             continue;
109
110         close(stab);
111         return(KSUCCESS);
112     }
113
114     /* Can't find the requested service */
115     close(stab);
116     return(KFAILURE);
117 }