Clean up a bunch of signed/unsigned comparison warnings
[krb5.git] / src / lib / krb5 / ccache / ser_cc.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * lib/krb5/ccache/ser_rc.c
4  *
5  * Copyright 1995 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.  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.
26  *
27  */
28
29 /*
30  * ser_rcdfl.c - Serialize replay cache context.
31  */
32 #include "k5-int.h"
33
34 /*
35  * Routines to deal with externalizing krb5_ccache.
36  *      krb5_ccache_size();
37  *      krb5_ccache_externalize();
38  *      krb5_ccache_internalize();
39  */
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 *);
46
47 /*
48  * Serialization entry for this type.
49  */
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  */
55 };
56 \f
57 /*
58  * krb5_ccache_size()   - Determine the size required to externalize
59  *                                this krb5_ccache variant.
60  */
61 static krb5_error_code
62 krb5_ccache_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
63 {
64     krb5_error_code     kret;
65     krb5_ccache         ccache;
66     size_t              required;
67
68     kret = EINVAL;
69     if ((ccache = (krb5_ccache) arg)) {
70         /*
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
75          */
76         required = sizeof(krb5_int32) * 3;
77         if (ccache->ops->prefix)
78             required += (strlen(ccache->ops->prefix)+1);
79
80         /*
81          * The ccache name is formed as follows:
82          *      <prefix>:<name>
83          */
84         required += strlen(krb5_cc_get_name(kcontext, ccache));
85
86         kret = 0;
87         *sizep += required;
88     }
89     return(kret);
90 }
91 \f
92 /*
93  * krb5_ccache_externalize()    - Externalize the krb5_ccache.
94  */
95 static krb5_error_code
96 krb5_ccache_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
97 {
98     krb5_error_code     kret;
99     krb5_ccache         ccache;
100     size_t              required;
101     krb5_octet          *bp;
102     size_t              remain;
103     char                *ccname;
104     const char          *fnamep;
105
106     required = 0;
107     bp = *buffer;
108     remain = *lenremain;
109     kret = EINVAL;
110     if ((ccache = (krb5_ccache) arg)) {
111         kret = ENOMEM;
112         if (!krb5_ccache_size(kcontext, arg, &required) &&
113             (required <= remain)) {
114             /* Our identifier */
115             (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
116
117             fnamep = krb5_cc_get_name(kcontext, ccache);
118
119             if (ccache->ops->prefix) {
120                 if (asprintf(&ccname, "%s:%s", ccache->ops->prefix, fnamep) < 0)
121                     ccname = NULL;
122             } else
123                 ccname = strdup(fnamep);
124
125             if (ccname) {
126                 /* Put the length of the file name */
127                 (void) krb5_ser_pack_int32((krb5_int32) strlen(ccname),
128                                            &bp, &remain);
129
130                 /* Put the name */
131                 (void) krb5_ser_pack_bytes((krb5_octet *) ccname,
132                                            strlen(ccname),
133                                            &bp, &remain);
134
135                 /* Put the trailer */
136                 (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
137                 kret = 0;
138                 *buffer = bp;
139                 *lenremain = remain;
140                 free(ccname);
141             }
142         }
143     }
144     return(kret);
145 }
146 \f
147 /*
148  * krb5_ccache_internalize()    - Internalize the krb5_ccache.
149  */
150 static krb5_error_code
151 krb5_ccache_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
152 {
153     krb5_error_code     kret;
154     krb5_ccache         ccache;
155     krb5_int32          ibuf;
156     krb5_octet          *bp;
157     size_t              remain;
158     char                *ccname = NULL;
159
160     *argp = NULL;
161
162     bp = *buffer;
163     remain = *lenremain;
164
165     /* Read our magic number. */
166     kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
167     if (kret)
168         return kret;
169     if (ibuf != KV5M_CCACHE)
170         return EINVAL;
171
172     /* Unpack and validate the length of the ccache name. */
173     kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
174     if (kret)
175         return kret;
176     if (ibuf < 0 || (krb5_ui_4) ibuf > remain)
177         return EINVAL;
178
179     /* Allocate and unpack the name. */
180     ccname = malloc(ibuf + 1);
181     if (!ccname)
182         return ENOMEM;
183     kret = krb5_ser_unpack_bytes((krb5_octet *) ccname, (size_t) ibuf,
184                                  &bp, &remain);
185     if (kret)
186         goto cleanup;
187     ccname[ibuf] = '\0';
188
189     /* Read the second magic number. */
190     kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
191     if (kret)
192         goto cleanup;
193     if (ibuf != KV5M_CCACHE) {
194         kret = EINVAL;
195         goto cleanup;
196     }
197
198     /* Resolve the named credential cache. */
199     kret = krb5_cc_resolve(kcontext, ccname, &ccache);
200     if (kret)
201         goto cleanup;
202
203     *buffer = bp;
204     *lenremain = remain;
205     *argp = ccache;
206
207 cleanup:
208     free(ccname);
209     return(kret);
210 }
211 \f
212 /*
213  * Register the ccache serializer.
214  */
215 krb5_error_code KRB5_CALLCONV
216 krb5_ser_ccache_init(krb5_context kcontext)
217 {
218     return(krb5_register_serializer(kcontext, &krb5_ccache_ser_entry));
219 }