Revert due to potential file modes race condition
authorTom Yu <tlyu@mit.edu>
Fri, 18 Jul 2008 03:08:38 +0000 (03:08 +0000)
committerTom Yu <tlyu@mit.edu>
Fri, 18 Jul 2008 03:08:38 +0000 (03:08 +0000)
ticket: 6002
status: open

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20538 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/rcache/rc_io.c

index 84c83382fe332939d7404337de9184ff572becf6..59d15c2faba2711cd198206118e3a9a61b2dd5b9 100644 (file)
@@ -68,6 +68,7 @@ getdir(void)
 krb5_error_code
 krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn)
 {
+    char *c;
     krb5_int16 rc_vno = htons(KRB5_RC_VNO);
     krb5_error_code retval = 0;
     int do_not_unlink = 0;
@@ -85,29 +86,24 @@ krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn)
        d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL |
                               O_BINARY, 0600);
     } else {
-       if (asprintf(&d->fn, "%s%skrb5_RCXXXXXX",
-                    dir, PATH_SEPARATOR) < 0) {
+       if (asprintf(&d->fn, "%s%skrb5_RC%daaa",
+                    dir, PATH_SEPARATOR, (int) UNIQUE) < 0) {
            d->fn = NULL;
            return KRB5_RC_IO_MALLOC;
        }
-       d->fd = mkstemp(d->fn);
-       if (d->fd != -1) {
-#if defined(HAVE_FCHMOD) || defined(HAVE_CHMOD)
-#ifdef HAVE_FCHMOD
-           retval = fchmod(d->fd, 0600);
-#else
-           retval = chmod(d->fn, 0600);
-#endif
-           if (retval == -1) {
-               retval = KRB5_RC_IO_UNKNOWN;
-               krb5_set_error_message(context, retval,
-                                      "Cannot chmod replay cache file %s: %s",
-                                      d->fn, strerror(errno));
-               goto cleanup;
+       c = d->fn + strlen(d->fn) - 3;
+       while ((d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC |
+                                      O_EXCL | O_BINARY, 0600)) == -1) {
+           if ((c[2]++) == 'z') {
+               c[2] = 'a';
+               if ((c[1]++) == 'z') {
+                   c[1] = 'a';
+                   if ((c[0]++) == 'z')
+                       break; /* sigh */
+               }
            }
-#endif
        }
-       if (retval != 0 && d->fd != -1 && fn) {
+       if (fn) {
            *fn = strdup(d->fn + dirlen);
            if (*fn == NULL) {
                free(d->fn);