Fix bugs I introduced into srv_rcache. The rcache name wasn't being
[krb5.git] / src / lib / krb5 / krb / srv_rcache.c
1 /*
2  * $Source$
3  * $Author$
4  *
5  * Copyright 1991 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  * 
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  M.I.T. makes no representations about the suitability of
21  * this software for any purpose.  It is provided "as is" without express
22  * or implied warranty.
23  * 
24  *
25  * Allocate & prepare a default replay cache for a server.
26  */
27
28 #if !defined(lint) && !defined(SABER)
29 static char rcsid_srv_rcache_c[] =
30 "$Id$";
31 #endif  /* !lint & !SABER */
32
33 #include <krb5/krb5.h>
34 #include <krb5/ext-proto.h>
35
36 krb5_error_code
37 krb5_get_server_rcache(piece, rcptr)
38 const krb5_data *piece;
39 krb5_rcache *rcptr;
40 {
41     krb5_rcache rcache = 0;
42     char *cachename = 0;
43     char tmp[4];
44     extern krb5_deltat krb5_clockskew;
45     krb5_error_code retval;
46     int len, p, i;
47     
48     rcache = (krb5_rcache) malloc(sizeof(*rcache));
49     if (!rcache)
50         return ENOMEM;
51     
52     retval = krb5_rc_resolve_type(&rcache, "dfl");
53     if (retval) goto cleanup;
54
55     len = piece->length + 3 + 1;
56     for (i = 0; i <= piece->length; i++) {
57         if (piece->data[i] == '\\')
58             len++;
59         else if (!isgraph(piece->data[i]))
60             len += 3;
61     }
62     cachename = malloc(len);
63     if (!cachename) {
64         retval = ENOMEM;
65         goto cleanup;
66     }
67     strcpy(cachename, "rc_");
68     p = 3;
69     for (i = 0; i < piece->length; i++) {
70         if (piece->data[i] == '\\') {
71             cachename[p++] = '\\';
72             cachename[p++] = '\\';
73             continue;
74         }
75         if (!isgraph(piece->data[i])) {
76             sprintf(tmp, "%03o", piece->data[i]);
77             cachename[p++] = '\\';
78             cachename[p++] = tmp[0];
79             cachename[p++] = tmp[1];
80             cachename[p++] = tmp[2];
81         }
82         cachename[p++] = piece->data[i];
83     }
84
85     if (retval = krb5_rc_resolve(rcache, cachename))
86         goto cleanup;
87     
88     /*
89      * First try to recover the replay cache; if that doesn't work,
90      * initialize it.
91      */
92     if (krb5_rc_recover(rcache)) {
93         if (retval = krb5_rc_initialize(rcache, krb5_clockskew)) {
94             krb5_rc_close(rcache);
95             rcache = 0;
96             goto cleanup;
97         }
98     }
99
100     *rcptr = rcache;
101     return 0;
102
103 cleanup:
104     if (rcache)
105         krb5_xfree(rcache);
106     if (cachename)
107         krb5_xfree(cachename);
108     return retval;
109 }