From c325452f2bfe0e697535f4b1a78e8a0f5440b932 Mon Sep 17 00:00:00 2001 From: John Kohl Date: Tue, 12 Feb 1991 14:11:55 +0000 Subject: [PATCH] adapted from scc_maybe git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1676 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/ccache/file/fcc_maybe.c | 86 ++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/lib/krb5/ccache/file/fcc_maybe.c diff --git a/src/lib/krb5/ccache/file/fcc_maybe.c b/src/lib/krb5/ccache/file/fcc_maybe.c new file mode 100644 index 000000000..2a2a8878a --- /dev/null +++ b/src/lib/krb5/ccache/file/fcc_maybe.c @@ -0,0 +1,86 @@ +/* + * $Source$ + * $Author$ + * + * Copyright 1990, 1991 by the Massachusetts Institute of Technology. + * + * For copying and distribution information, please see the file + * . + * + * This file contains the source code for conditional open/close calls. + */ + +#include "fcc.h" + +krb5_error_code +krb5_fcc_close_file (id) + krb5_ccache id; +{ + int ret; + krb5_fcc_data *data = (krb5_fcc_data *)id->data; + + if (data->fd == -1) { + abort (); /* XXX? */ + } + ret = close (data->fd); + data->fd = -1; + return (ret == -1) ? krb5_fcc_interpret (errno) : 0; +} + +krb5_error_code +krb5_fcc_open_file (id, mode) + krb5_ccache id; + int mode; +{ + krb5_fcc_data *data = (krb5_fcc_data *)id->data; + krb5_int16 fcc_fvno = htons(KRB5_FCC_FVNO); + int fd; + int open_flag; + + if (data->fd != -1) { + /* Don't know what state it's in; shut down and start anew. */ + (void) close (data->fd); + data->fd = -1; + } + switch(mode) { + case FCC_OPEN_AND_ERASE: + open_flag = O_CREAT|O_TRUNC|O_RDWR; + break; + case FCC_OPEN_RDWR: + open_flag = O_RDWR; + break; + case FCC_OPEN_RDONLY: + default: + open_flag = O_RDONLY; + break; + } + + fd = open (data->filename, open_flag, 0600); + if (fd == -1) + return krb5_fcc_interpret (errno); + + if (mode == FCC_OPEN_AND_ERASE) { + /* write the version number */ + int errsave, cnt; + + if ((cnt = write(fd, (char *)&fcc_fvno, sizeof(fcc_fvno))) != + sizeof(fcc_fvno)) { + errsave = errno; + (void) close(fd); + return (cnt == -1) ? krb5_fcc_interpret(errsave) : KRB5_CC_IO; + } + } else { + /* verify a valid version number is there */ + if (read(fd, (char *)&fcc_fvno, sizeof(fcc_fvno)) != + sizeof(fcc_fvno)) { + (void) close(fd); + return KRB5_CCACHE_BADVNO; + } + if (fcc_fvno != htons(KRB5_FCC_FVNO)) { + (void) close(fd); + return KRB5_CCACHE_BADVNO; + } + } + data->fd = fd; + return 0; +} -- 2.26.2