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