adapted from scc_maybe
authorJohn Kohl <jtkohl@mit.edu>
Tue, 12 Feb 1991 14:11:55 +0000 (14:11 +0000)
committerJohn Kohl <jtkohl@mit.edu>
Tue, 12 Feb 1991 14:11:55 +0000 (14:11 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1676 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/ccache/file/fcc_maybe.c [new file with mode: 0644]

diff --git a/src/lib/krb5/ccache/file/fcc_maybe.c b/src/lib/krb5/ccache/file/fcc_maybe.c
new file mode 100644 (file)
index 0000000..2a2a887
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * $Source$
+ * $Author$
+ *
+ * Copyright 1990, 1991 by the Massachusetts Institute of Technology.
+ *
+ * For copying and distribution information, please see the file
+ * <krb5/copyright.h>.
+ *
+ * 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;
+}