This commit was manufactured by cvs2svn to create tag
[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.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  * 
26  *
27  * Allocate & prepare a default replay cache for a server.
28  */
29
30 #include "k5-int.h"
31 #include <ctype.h>
32 #include <stdio.h>
33
34 /* Macro for valid RC name characters*/
35 #define isvalidrcname(x) ((!ispunct(x))&&isgraph(x))
36 krb5_error_code KRB5_CALLCONV
37 krb5_get_server_rcache(krb5_context context, const krb5_data *piece, krb5_rcache *rcptr)
38 {
39     krb5_rcache rcache = 0;
40     char *cachename = 0;
41     char tmp[4];
42     krb5_error_code retval;
43     int p, i;
44     unsigned int len;
45
46 #ifdef HAVE_GETEUID
47     unsigned long tens;
48     unsigned long uid = geteuid();
49 #endif
50     
51     if (piece == NULL)
52         return ENOMEM;
53     
54     rcache = (krb5_rcache) malloc(sizeof(*rcache));
55     if (!rcache)
56         return ENOMEM;
57     
58     retval = krb5_rc_resolve_type(context, &rcache, 
59                                   krb5_rc_default_type(context));
60     if (retval) goto cleanup;
61
62     len = piece->length + 3 + 1;
63     for (i = 0; i < piece->length; i++) {
64         if (piece->data[i] == '-')
65             len++;
66         else if (!isvalidrcname((int) piece->data[i]))
67             len += 3;
68     }
69
70 #ifdef HAVE_GETEUID
71     len += 2;   /* _<uid> */
72     for (tens = 1; (uid / tens) > 9 ; tens *= 10)
73         len++;
74 #endif
75     
76     cachename = malloc(len);
77     if (!cachename) {
78         retval = ENOMEM;
79         goto cleanup;
80     }
81     strcpy(cachename, "rc_");
82     p = 3;
83     for (i = 0; i < piece->length; i++) {
84         if (piece->data[i] == '-') {
85             cachename[p++] = '-';
86             cachename[p++] = '-';
87             continue;
88         }
89         if (!isvalidrcname((int) piece->data[i])) {
90             sprintf(tmp, "%03o", piece->data[i]);
91             cachename[p++] = '-';
92             cachename[p++] = tmp[0];
93             cachename[p++] = tmp[1];
94             cachename[p++] = tmp[2];
95             continue;
96         }
97         cachename[p++] = piece->data[i];
98     }
99
100 #ifdef HAVE_GETEUID
101     cachename[p++] = '_';
102     while (tens) {
103         cachename[p++] = '0' + ((uid / tens) % 10);
104         tens /= 10;
105     }
106 #endif
107
108     cachename[p++] = '\0';
109
110     if ((retval = krb5_rc_resolve(context, rcache, cachename)))
111         goto cleanup;
112     
113     /*
114      * First try to recover the replay cache; if that doesn't work,
115      * initialize it.
116      */
117     if (krb5_rc_recover(context, rcache)) {
118         if ((retval = krb5_rc_initialize(context, rcache,
119                                          context->clockskew))) {
120             krb5_rc_close(context, rcache);
121             rcache = 0;
122             goto cleanup;
123         }
124     }
125
126     *rcptr = rcache;
127     rcache = 0;
128     retval = 0;
129
130 cleanup:
131     if (rcache)
132         krb5_xfree(rcache);
133     if (cachename)
134         krb5_xfree(cachename);
135     return retval;
136 }