2 * lib/krb5/krb/srv_rcache.c
4 * Copyright 1991 by the Massachusetts Institute of Technology.
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.
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.
27 * Allocate & prepare a default replay cache for a server.
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)
39 krb5_rcache rcache = 0;
42 krb5_error_code retval;
48 unsigned long uid = geteuid();
54 rcache = (krb5_rcache) malloc(sizeof(*rcache));
58 retval = krb5_rc_resolve_type(context, &rcache,
59 krb5_rc_default_type(context));
60 if (retval) goto cleanup;
62 len = piece->length + 3 + 1;
63 for (i = 0; i < piece->length; i++) {
64 if (piece->data[i] == '-')
66 else if (!isvalidrcname((int) piece->data[i]))
71 len += 2; /* _<uid> */
72 for (tens = 1; (uid / tens) > 9 ; tens *= 10)
76 cachename = malloc(len);
81 strcpy(cachename, "rc_");
83 for (i = 0; i < piece->length; i++) {
84 if (piece->data[i] == '-') {
89 if (!isvalidrcname((int) piece->data[i])) {
90 sprintf(tmp, "%03o", piece->data[i]);
92 cachename[p++] = tmp[0];
93 cachename[p++] = tmp[1];
94 cachename[p++] = tmp[2];
97 cachename[p++] = piece->data[i];
101 cachename[p++] = '_';
103 cachename[p++] = '0' + ((uid / tens) % 10);
108 cachename[p++] = '\0';
110 if ((retval = krb5_rc_resolve(context, rcache, cachename)))
114 * First try to recover the replay cache; if that doesn't work,
117 if (krb5_rc_recover(context, rcache)) {
118 if ((retval = krb5_rc_initialize(context, rcache,
119 context->clockskew))) {
120 krb5_rc_close(context, rcache);
134 krb5_xfree(cachename);