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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/*
* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "krb5_locl.h"
RCSID("$Id: fcache.c,v 1.31 2000/12/05 09:15:10 joda Exp $");
typedef struct krb5_fcache{
char *filename;
int version;
}krb5_fcache;
struct fcc_cursor {
int fd;
krb5_storage *sp;
};
#define KRB5_FCC_FVNO_1 1
#define KRB5_FCC_FVNO_2 2
#define KRB5_FCC_FVNO_3 3
#define KRB5_FCC_FVNO_4 4
#define FCC_TAG_DELTATIME 1
#define FCACHE(X) ((krb5_fcache*)(X)->data.data)
#define FILENAME(X) (FCACHE(X)->filename)
#define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
static char*
fcc_get_name(krb5_context context,
krb5_ccache id)
{
return FILENAME(id);
}
static krb5_error_code
fcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
{
krb5_fcache *f;
f = malloc(sizeof(*f));
if(f == NULL)
return KRB5_CC_NOMEM;
f->filename = strdup(res);
if(f->filename == NULL){
free(f);
return KRB5_CC_NOMEM;
}
f->version = 0;
(*id)->data.data = f;
(*id)->data.length = sizeof(*f);
return 0;
}
/*
* Try to scrub the contents of `filename' safely.
*/
static int
scrub_file (int fd)
{
off_t pos;
char buf[128];
pos = lseek(fd, 0, SEEK_END);
if (pos < 0)
return errno;
if (lseek(fd, 0, SEEK_SET) < 0)
return errno;
memset(buf, 0, sizeof(buf));
while(pos > 0) {
ssize_t tmp = write(fd, buf, min(sizeof(buf), pos));
if (tmp < 0)
return errno;
pos -= tmp;
}
fsync (fd);
return 0;
}
/*
* Erase `filename' if it exists, trying to remove the contents if
* it's `safe'. We always try to remove the file, it it exists. It's
* only overwritten if it's a regular file (not a symlink and not a
* hardlink)
*/
static krb5_error_code
erase_file(const char *filename)
{
int fd;
struct stat sb1, sb2;
int ret;
ret = lstat (filename, &sb1);
if (ret < 0)
return errno;
fd = open(filename, O_RDWR | O_BINARY);
if(fd < 0) {
if(errno == ENOENT)
return 0;
else
return errno;
}
if (unlink(filename) < 0) {
close (fd);
return errno;
}
ret = fstat (fd, &sb2);
if (ret < 0) {
close (fd);
return errno;
}
/* check if someone was playing with symlinks */
if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
close (fd);
return EPERM;
}
/* there are still hard links to this file */
if (sb2.st_nlink != 0) {
close (fd);
return 0;
}
ret = scrub_file (fd);
close (fd);
return ret;
}
static krb5_error_code
fcc_gen_new(krb5_context context, krb5_ccache *id)
{
krb5_fcache *f;
int fd;
char *file;
f = malloc(sizeof(*f));
if(f == NULL)
return KRB5_CC_NOMEM;
asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
if(file == NULL) {
free(f);
return KRB5_CC_NOMEM;
}
fd = mkstemp(file);
if(fd < 0) {
free(f);
free(file);
return errno;
}
close(fd);
f->filename = file;
f->version = 0;
(*id)->data.data = f;
(*id)->data.length = sizeof(*f);
return 0;
}
static void
storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
{
int flags = 0;
switch(vno) {
case KRB5_FCC_FVNO_1:
flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
flags |= KRB5_STORAGE_HOST_BYTEORDER;
break;
case KRB5_FCC_FVNO_2:
flags |= KRB5_STORAGE_HOST_BYTEORDER;
break;
case KRB5_FCC_FVNO_3:
flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
break;
case KRB5_FCC_FVNO_4:
break;
default:
krb5_abortx(context,
"storage_set_flags called with bad vno (%x)", vno);
}
krb5_storage_set_flags(sp, flags);
}
static krb5_error_code
fcc_initialize(krb5_context context,
krb5_ccache id,
krb5_principal primary_principal)
{
krb5_fcache *f = FCACHE(id);
int ret = 0;
int fd;
char *filename = f->filename;
unlink (filename);
fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
if(fd == -1)
return errno;
{
krb5_storage *sp;
sp = krb5_storage_from_fd(fd);
if(context->fcache_vno != 0)
f->version = context->fcache_vno;
else
f->version = KRB5_FCC_FVNO_4;
ret |= krb5_store_int8(sp, 5);
ret |= krb5_store_int8(sp, f->version);
storage_set_flags(context, sp, f->version);
if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
/* V4 stuff */
if (context->kdc_sec_offset) {
ret |= krb5_store_int16 (sp, 12); /* length */
ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
ret |= krb5_store_int16 (sp, 8); /* length of data */
ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
} else {
ret |= krb5_store_int16 (sp, 0);
}
}
ret |= krb5_store_principal(sp, primary_principal);
krb5_storage_free(sp);
}
if(close(fd) < 0)
if (ret == 0)
ret = errno;
return ret;
}
static krb5_error_code
fcc_close(krb5_context context,
krb5_ccache id)
{
free (FILENAME(id));
krb5_data_free(&id->data);
return 0;
}
static krb5_error_code
fcc_destroy(krb5_context context,
krb5_ccache id)
{
char *f;
f = FILENAME(id);
erase_file(f);
return 0;
}
static krb5_error_code
fcc_store_cred(krb5_context context,
krb5_ccache id,
krb5_creds *creds)
{
int ret;
int fd;
char *f;
f = FILENAME(id);
fd = open(f, O_WRONLY | O_APPEND | O_BINARY);
if(fd < 0)
return errno;
{
krb5_storage *sp;
sp = krb5_storage_from_fd(fd);
storage_set_flags(context, sp, FCACHE(id)->version);
ret = krb5_store_creds(sp, creds);
krb5_storage_free(sp);
}
if (close(fd) < 0)
if (ret == 0)
ret = errno;
return ret;
}
static krb5_error_code
fcc_read_cred (krb5_context context,
krb5_fcache *fc,
krb5_storage *sp,
krb5_creds *creds)
{
krb5_error_code ret;
storage_set_flags(context, sp, fc->version);
ret = krb5_ret_creds(sp, creds);
return ret;
}
static krb5_error_code
init_fcc (krb5_context context,
krb5_fcache *fcache,
krb5_storage **ret_sp,
int *ret_fd)
{
int fd;
int8_t pvno, tag;
krb5_storage *sp;
krb5_error_code ret;
fd = open(fcache->filename, O_RDONLY | O_BINARY);
if(fd < 0)
return errno;
sp = krb5_storage_from_fd(fd);
ret = krb5_ret_int8(sp, &pvno);
if(ret == KRB5_CC_END)
return ENOENT;
if(ret)
return ret;
if(pvno != 5) {
krb5_storage_free(sp);
close(fd);
return KRB5_CCACHE_BADVNO;
}
krb5_ret_int8(sp, &tag); /* should not be host byte order */
fcache->version = tag;
storage_set_flags(context, sp, fcache->version);
switch (tag) {
case KRB5_FCC_FVNO_4: {
int16_t length;
krb5_ret_int16 (sp, &length);
while(length > 0) {
int16_t tag, data_len;
int i;
int8_t dummy;
krb5_ret_int16 (sp, &tag);
krb5_ret_int16 (sp, &data_len);
switch (tag) {
case FCC_TAG_DELTATIME :
krb5_ret_int32 (sp, &context->kdc_sec_offset);
krb5_ret_int32 (sp, &context->kdc_usec_offset);
break;
default :
for (i = 0; i < data_len; ++i)
krb5_ret_int8 (sp, &dummy);
break;
}
length -= 4 + data_len;
}
break;
}
case KRB5_FCC_FVNO_3:
case KRB5_FCC_FVNO_2:
case KRB5_FCC_FVNO_1:
break;
default :
krb5_storage_free (sp);
close (fd);
return KRB5_CCACHE_BADVNO;
}
*ret_sp = sp;
*ret_fd = fd;
return 0;
}
static krb5_error_code
fcc_get_principal(krb5_context context,
krb5_ccache id,
krb5_principal *principal)
{
krb5_error_code ret;
krb5_fcache *f = FCACHE(id);
int fd;
krb5_storage *sp;
ret = init_fcc (context, f, &sp, &fd);
if (ret)
return ret;
ret = krb5_ret_principal(sp, principal);
krb5_storage_free(sp);
close(fd);
return ret;
}
static krb5_error_code
fcc_get_first (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor)
{
krb5_error_code ret;
krb5_principal principal;
krb5_fcache *f = FCACHE(id);
*cursor = malloc(sizeof(struct fcc_cursor));
ret = init_fcc (context, f, &FCC_CURSOR(*cursor)->sp,
&FCC_CURSOR(*cursor)->fd);
if (ret)
return ret;
krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
krb5_free_principal (context, principal);
return 0;
}
static krb5_error_code
fcc_get_next (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor,
krb5_creds *creds)
{
return fcc_read_cred (context, FCACHE(id), FCC_CURSOR(*cursor)->sp, creds);
}
static krb5_error_code
fcc_end_get (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor)
{
krb5_storage_free(FCC_CURSOR(*cursor)->sp);
close (FCC_CURSOR(*cursor)->fd);
free(*cursor);
return 0;
}
static krb5_error_code
fcc_remove_cred(krb5_context context,
krb5_ccache id,
krb5_flags which,
krb5_creds *cred)
{
return 0; /* XXX */
}
static krb5_error_code
fcc_set_flags(krb5_context context,
krb5_ccache id,
krb5_flags flags)
{
return 0; /* XXX */
}
static krb5_error_code
fcc_get_version(krb5_context context,
krb5_ccache id)
{
return FCACHE(id)->version;
}
const krb5_cc_ops krb5_fcc_ops = {
"FILE",
fcc_get_name,
fcc_resolve,
fcc_gen_new,
fcc_initialize,
fcc_destroy,
fcc_close,
fcc_store_cred,
NULL, /* fcc_retrieve */
fcc_get_principal,
fcc_get_first,
fcc_get_next,
fcc_end_get,
fcc_remove_cred,
fcc_set_flags,
fcc_get_version
};
|