summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/ccache/stdio/scc_write.c
blob: cbfa4a449fc9fefc26b7c2cf6df20dfb5004dfec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * lib/krb5/ccache/stdio/scc_write.c
 *
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * This file contains the source code for krb5_scc_write_<type>.
 */



#include "scc.h"

#define CHECK(ret) if (ret != KRB5_OK) return ret;

/*
 * Requires:
 * id is open
 *
 * Effects:
 * Writes len bytes from buf into the file cred cache id.
 *
 * Errors:
 * system errors
 */
krb5_error_code
krb5_scc_write(context, id, buf, len)
   krb5_context context;
   krb5_ccache id;
   krb5_pointer buf;
   int len;
{
     int ret;

     errno = 0;
     ret = fwrite((char *) buf, 1, len, ((krb5_scc_data *)id->data)->file);
     if ((ret == 0) && errno) {
	  return krb5_scc_interpret (context, errno);
     } else if (ret != len)
	 return KRB5_CC_END;
     return KRB5_OK;
}

/*
 * FOR ALL OF THE FOLLOWING FUNCTIONS:
 * 
 * Requires:
 * ((krb5_scc_data *) id->data)->file is open and at the right position.
 * 
 * Effects:
 * Stores an encoded version of the second argument in the
 * cache file.
 *
 * Errors:
 * system errors
 */

krb5_error_code
krb5_scc_store_principal(context, id, princ)
   krb5_context context;
   krb5_ccache id;
   krb5_principal princ;
{
    krb5_scc_data *data = (krb5_scc_data *)id->data;
    krb5_error_code ret;
    krb5_int32 i, length, tmp, type;

    type = krb5_princ_type(context, princ);
    tmp = length = krb5_princ_size(context, princ);

    if (data->version == KRB5_SCC_FVNO_1) {
	/*
	 * DCE-compatible format means that the length count
	 * includes the realm.  (It also doesn't include the
	 * principal type information.)
	 */
	tmp++;
    } else {
	ret = krb5_scc_store_int32(context, id, type);
	CHECK(ret);
    }
    
    ret = krb5_scc_store_int32(context, id, tmp);
    CHECK(ret);

    ret = krb5_scc_store_data(context, id, krb5_princ_realm(context, princ));
    CHECK(ret);

    for (i=0; i < length; i++) {
	ret = krb5_scc_store_data(context, id, 
      krb5_princ_component(context, princ, i));
	CHECK(ret);
    }

    return KRB5_OK;
}

krb5_error_code
krb5_scc_store_addrs(context, id, addrs)
   krb5_context context;
   krb5_ccache id;
   krb5_address ** addrs;
{
     krb5_error_code ret;
     krb5_address **temp;
     krb5_int32 i, length = 0;

     /* Count the number of components */
     if (addrs) {
	temp = addrs;
	while (*temp++)
	     length += 1;
     }

     ret = krb5_scc_store_int32(context, id, length);
     CHECK(ret);
     for (i=0; i < length; i++) {
	  ret = krb5_scc_store_addr(context, id, addrs[i]);
	  CHECK(ret);
     }

     return KRB5_OK;
}

krb5_error_code
krb5_scc_store_keyblock(context, id, keyblock)
   krb5_context context;
   krb5_ccache id;
   krb5_keyblock *keyblock;
{
     krb5_scc_data *data = (krb5_scc_data *)id->data;
     krb5_error_code ret;

     ret = krb5_scc_store_ui_2(context, id, keyblock->enctype);
     CHECK(ret);
     if (data->version == KRB5_SCC_FVNO_3) {
	 ret = krb5_scc_store_ui_2(context, id, keyblock->enctype);
	 CHECK(ret);
     }
     ret = krb5_scc_store_int32(context, id, keyblock->length);
     CHECK(ret);
     return krb5_scc_write(context, id, (char *) keyblock->contents, keyblock->length);
}

krb5_error_code
krb5_scc_store_addr(context, id, addr)
   krb5_context context;
   krb5_ccache id;
   krb5_address *addr;
{
     krb5_error_code ret;

     ret = krb5_scc_store_ui_2(context, id, addr->addrtype);
     CHECK(ret);
     ret = krb5_scc_store_int32(context, id, addr->length);
     CHECK(ret);
     return krb5_scc_write(context, id, (char *) addr->contents, addr->length);
}


krb5_error_code
krb5_scc_store_data(context, id, data)
   krb5_context context;
   krb5_ccache id;
   krb5_data *data;
{
     krb5_error_code ret;

     ret = krb5_scc_store_int32(context, id, data->length);
     CHECK(ret);
     return krb5_scc_write(context, id, data->data, data->length);
}

krb5_error_code
krb5_scc_store_int32(context, id, i)
   krb5_context context;
   krb5_ccache id;
   krb5_int32 i;
{
    krb5_scc_data *data = (krb5_scc_data *)id->data;
    unsigned char buf[4];

    if ((data->version == KRB5_SCC_FVNO_1) ||
	(data->version == KRB5_SCC_FVNO_2)) 
	return krb5_scc_write(context, id, (char *) &i, sizeof(krb5_int32));
    else {
	buf[3] = i & 0xFF;
	i >>= 8;
	buf[2] = i & 0xFF;
	i >>= 8;
	buf[1] = i & 0xFF;
	i >>= 8;
	buf[0] = i & 0xFF;
	
	return krb5_scc_write(context, id, buf, 4);
    }
}

krb5_error_code
krb5_scc_store_ui_2(context, id, i)
   krb5_context context;
    krb5_ccache id;
    krb5_int32 i;
{
    krb5_scc_data *data = (krb5_scc_data *)id->data;
    krb5_ui_2 ibuf;
    unsigned char buf[2];
    
    if ((data->version == KRB5_SCC_FVNO_1) ||
	(data->version == KRB5_SCC_FVNO_2)) {
	ibuf = i;
	return krb5_scc_write(context, id, (char *) &ibuf, sizeof(krb5_ui_2));
    } else {
	buf[1] = i & 0xFF;
	i >>= 8;
	buf[0] = i & 0xFF;
	
	return krb5_scc_write(context, id, buf, 2);
    }
}
   
krb5_error_code
krb5_scc_store_octet(context, id, i)
   krb5_context context;
    krb5_ccache id;
    krb5_int32 i;
{
    krb5_octet ibuf;

    ibuf = i;
    return krb5_scc_write(context, id, (char *) &ibuf, 1);
}
   
krb5_error_code
krb5_scc_store_times(context, id, t)
   krb5_context context;
   krb5_ccache id;
   krb5_ticket_times *t;
{
    krb5_scc_data *data = (krb5_scc_data *)id->data;
    krb5_error_code retval;

    if ((data->version == KRB5_SCC_FVNO_1) ||
	(data->version == KRB5_SCC_FVNO_2))
	return krb5_scc_write(context, id, (char *) t, sizeof(krb5_ticket_times));
    else {
	retval = krb5_scc_store_int32(context, id, t->authtime);
	CHECK(retval);
	retval = krb5_scc_store_int32(context, id, t->starttime);
	CHECK(retval);
	retval = krb5_scc_store_int32(context, id, t->endtime);
	CHECK(retval);
	retval = krb5_scc_store_int32(context, id, t->renew_till);
	CHECK(retval);
	return 0;
    }
}
   
krb5_error_code
krb5_scc_store_authdata(context, id, a)
   krb5_context context;
    krb5_ccache id;
    krb5_authdata **a;
{
    krb5_error_code ret;
    krb5_authdata **temp;
    krb5_int32 i, length=0;

    if (a != NULL) {
	for (temp=a; *temp; temp++)
	    length++;
    }

    ret = krb5_scc_store_int32(context, id, length);
    CHECK(ret);
    for (i=0; i<length; i++) {
	ret = krb5_scc_store_authdatum (context, id, a[i]);
	CHECK(ret);
    }
    return KRB5_OK;
}

krb5_error_code
krb5_scc_store_authdatum (context, id, a)
   krb5_context context;
    krb5_ccache id;
    krb5_authdata *a;
{
    krb5_error_code ret;
    ret = krb5_scc_store_ui_2(context, id, a->ad_type);
    CHECK(ret);
    ret = krb5_scc_store_int32(context, id, a->length);
    CHECK(ret);
    return krb5_scc_write(context, id, (krb5_pointer) a->contents, a->length);
}