1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * lib/krb5/ccache/ser_rc.c
5 * Copyright 1995 by the Massachusetts Institute of Technology.
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.
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. Furthermore if you modify this software you must label
21 * your software as modified software and not distribute it in such a
22 * fashion that it might be confused with the original M.I.T. software.
23 * M.I.T. makes no representations about the suitability of
24 * this software for any purpose. It is provided "as is" without express
25 * or implied warranty.
30 * ser_rcdfl.c - Serialize replay cache context.
35 * Routines to deal with externalizing krb5_ccache.
37 * krb5_ccache_externalize();
38 * krb5_ccache_internalize();
40 static krb5_error_code krb5_ccache_size
41 (krb5_context, krb5_pointer, size_t *);
42 static krb5_error_code krb5_ccache_externalize
43 (krb5_context, krb5_pointer, krb5_octet **, size_t *);
44 static krb5_error_code krb5_ccache_internalize
45 (krb5_context,krb5_pointer *, krb5_octet **, size_t *);
48 * Serialization entry for this type.
50 static const krb5_ser_entry krb5_ccache_ser_entry = {
51 KV5M_CCACHE, /* Type */
52 krb5_ccache_size, /* Sizer routine */
53 krb5_ccache_externalize, /* Externalize routine */
54 krb5_ccache_internalize /* Internalize routine */
58 * krb5_ccache_size() - Determine the size required to externalize
59 * this krb5_ccache variant.
61 static krb5_error_code
62 krb5_ccache_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
69 if ((ccache = (krb5_ccache) arg)) {
71 * Saving FILE: variants of krb5_ccache requires at minimum:
72 * krb5_int32 for KV5M_CCACHE
73 * krb5_int32 for length of ccache name.
74 * krb5_int32 for KV5M_CCACHE
76 required = sizeof(krb5_int32) * 3;
77 if (ccache->ops->prefix)
78 required += (strlen(ccache->ops->prefix)+1);
81 * The ccache name is formed as follows:
84 required += strlen(krb5_cc_get_name(kcontext, ccache));
93 * krb5_ccache_externalize() - Externalize the krb5_ccache.
95 static krb5_error_code
96 krb5_ccache_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
110 if ((ccache = (krb5_ccache) arg)) {
112 if (!krb5_ccache_size(kcontext, arg, &required) &&
113 (required <= remain)) {
115 (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
117 fnamep = krb5_cc_get_name(kcontext, ccache);
119 if (ccache->ops->prefix) {
120 if (asprintf(&ccname, "%s:%s", ccache->ops->prefix, fnamep) < 0)
123 ccname = strdup(fnamep);
126 /* Put the length of the file name */
127 (void) krb5_ser_pack_int32((krb5_int32) strlen(ccname),
131 (void) krb5_ser_pack_bytes((krb5_octet *) ccname,
135 /* Put the trailer */
136 (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
148 * krb5_ccache_internalize() - Internalize the krb5_ccache.
150 static krb5_error_code
151 krb5_ccache_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
153 krb5_error_code kret;
165 /* Read our magic number. */
166 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
169 if (ibuf != KV5M_CCACHE)
172 /* Unpack and validate the length of the ccache name. */
173 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
176 if (ibuf < 0 || (krb5_ui_4) ibuf > remain)
179 /* Allocate and unpack the name. */
180 ccname = malloc(ibuf + 1);
183 kret = krb5_ser_unpack_bytes((krb5_octet *) ccname, (size_t) ibuf,
189 /* Read the second magic number. */
190 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
193 if (ibuf != KV5M_CCACHE) {
198 /* Resolve the named credential cache. */
199 kret = krb5_cc_resolve(kcontext, ccname, &ccache);
213 * Register the ccache serializer.
215 krb5_error_code KRB5_CALLCONV
216 krb5_ser_ccache_init(krb5_context kcontext)
218 return(krb5_register_serializer(kcontext, &krb5_ccache_ser_entry));