diff options
Diffstat (limited to 'src/lib/krb5/rcache/rc_io.c')
-rw-r--r-- | src/lib/krb5/rcache/rc_io.c | 358 |
1 files changed, 179 insertions, 179 deletions
diff --git a/src/lib/krb5/rcache/rc_io.c b/src/lib/krb5/rcache/rc_io.c index 1043c30..5abf109 100644 --- a/src/lib/krb5/rcache/rc_io.c +++ b/src/lib/krb5/rcache/rc_io.c @@ -1,3 +1,4 @@ +/* -*- mode: c; indent-tabs-mode: nil -*- */ /* * lib/krb5/rcache/rc_io.c * @@ -6,7 +7,6 @@ * */ - /* * I/O functions for the replay cache default implementation. */ @@ -17,7 +17,7 @@ # define PATH_SEPARATOR "/" #endif -#define KRB5_RC_VNO 0x0501 /* krb5, rcache v 1 */ +#define KRB5_RC_VNO 0x0501 /* krb5, rcache v 1 */ #if HAVE_SYS_STAT_H #include <sys/stat.h> @@ -52,17 +52,17 @@ getdir(void) if (!(dir = getenv("KRB5RCACHEDIR"))) { #if defined(_WIN32) - if (!(dir = getenv("TEMP"))) - if (!(dir = getenv("TMP"))) - dir = "C:"; + if (!(dir = getenv("TEMP"))) + if (!(dir = getenv("TMP"))) + dir = "C:"; #else - if (!(dir = getenv("TMPDIR"))) { + if (!(dir = getenv("TMPDIR"))) { #ifdef RCTMPDIR - dir = RCTMPDIR; + dir = RCTMPDIR; #else - dir = "/tmp"; + dir = "/tmp"; #endif - } + } #endif } return dir; @@ -85,17 +85,17 @@ krb5_rc_io_mkstemp(krb5_context context, krb5_rc_iostuff *d, char *dir) memset(&stbuf, 0, sizeof(stbuf)); if (asprintf(&d->fn, "%s%skrb5_RCXXXXXX", - dir, PATH_SEPARATOR) < 0) { - d->fn = NULL; - return KRB5_RC_IO_MALLOC; + dir, PATH_SEPARATOR) < 0) { + d->fn = NULL; + return KRB5_RC_IO_MALLOC; } d->fd = mkstemp(d->fn); if (d->fd == -1) { - /* - * This return value is deliberate because d->fd == -1 causes - * caller to go into errno interpretation code. - */ - return 0; + /* + * This return value is deliberate because d->fd == -1 causes + * caller to go into errno interpretation code. + */ + return 0; } #if HAVE_SYS_STAT_H /* @@ -104,18 +104,18 @@ krb5_rc_io_mkstemp(krb5_context context, krb5_rc_iostuff *d, char *dir) */ retval = fstat(d->fd, &stbuf); if (retval) { - krb5_set_error_message(context, retval, - "Cannot fstat replay cache file %s: %s", - d->fn, strerror(errno)); - return KRB5_RC_IO_UNKNOWN; + krb5_set_error_message(context, retval, + "Cannot fstat replay cache file %s: %s", + d->fn, strerror(errno)); + return KRB5_RC_IO_UNKNOWN; } if (stbuf.st_mode & 077) { - krb5_set_error_message(context, retval, - "Insecure mkstemp() file mode " - "for replay cache file %s; " - "try running this program " - "with umask 077 ", d->fn); - return KRB5_RC_IO_UNKNOWN; + krb5_set_error_message(context, retval, + "Insecure mkstemp() file mode " + "for replay cache file %s; " + "try running this program " + "with umask 077 ", d->fn); + return KRB5_RC_IO_UNKNOWN; } #endif return 0; @@ -127,7 +127,7 @@ static krb5_error_code rc_map_errno (int) __attribute__((cold)); static krb5_error_code rc_map_errno (krb5_context context, int e, const char *fn, - const char *operation) + const char *operation) { switch (e) { case EFBIG: @@ -135,25 +135,25 @@ rc_map_errno (krb5_context context, int e, const char *fn, case EDQUOT: #endif case ENOSPC: - return KRB5_RC_IO_SPACE; + return KRB5_RC_IO_SPACE; case EIO: - return KRB5_RC_IO_IO; + return KRB5_RC_IO_IO; case EPERM: case EACCES: case EROFS: case EEXIST: - krb5_set_error_message(context, KRB5_RC_IO_PERM, - "Cannot %s replay cache file %s: %s", - operation, fn, strerror(e)); - return KRB5_RC_IO_PERM; + krb5_set_error_message(context, KRB5_RC_IO_PERM, + "Cannot %s replay cache file %s: %s", + operation, fn, strerror(e)); + return KRB5_RC_IO_PERM; default: - krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, - "Cannot %s replay cache: %s", - operation, strerror(e)); - return KRB5_RC_IO_UNKNOWN; + krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, + "Cannot %s replay cache: %s", + operation, strerror(e)); + return KRB5_RC_IO_UNKNOWN; } } @@ -169,55 +169,55 @@ krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn) GETDIR; if (fn && *fn) { - if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, *fn) < 0) - return KRB5_RC_IO_MALLOC; - unlink(d->fn); - d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | - O_BINARY, 0600); + if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, *fn) < 0) + return KRB5_RC_IO_MALLOC; + unlink(d->fn); + d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | + O_BINARY, 0600); } else { - retval = krb5_rc_io_mkstemp(context, d, dir); - if (retval) - goto cleanup; - if (d->fd != -1 && fn) { - *fn = strdup(d->fn + dirlen); - if (*fn == NULL) { - free(d->fn); - return KRB5_RC_IO_MALLOC; - } - } + retval = krb5_rc_io_mkstemp(context, d, dir); + if (retval) + goto cleanup; + if (d->fd != -1 && fn) { + *fn = strdup(d->fn + dirlen); + if (*fn == NULL) { + free(d->fn); + return KRB5_RC_IO_MALLOC; + } + } } if (d->fd == -1) { - retval = rc_map_errno(context, errno, d->fn, "create"); - if (retval == KRB5_RC_IO_PERM) - do_not_unlink = 1; - goto cleanup; + retval = rc_map_errno(context, errno, d->fn, "create"); + if (retval == KRB5_RC_IO_PERM) + do_not_unlink = 1; + goto cleanup; } set_cloexec_fd(d->fd); retval = krb5_rc_io_write(context, d, (krb5_pointer)&rc_vno, - sizeof(rc_vno)); + sizeof(rc_vno)); if (retval) - goto cleanup; + goto cleanup; retval = krb5_rc_io_sync(context, d); - cleanup: +cleanup: if (retval) { - if (d->fn) { - if (!do_not_unlink) - (void) unlink(d->fn); - FREE(d->fn); - d->fn = NULL; - } - if (d->fd != -1) { - (void) close(d->fd); - } + if (d->fn) { + if (!do_not_unlink) + (void) unlink(d->fn); + FREE(d->fn); + d->fn = NULL; + } + if (d->fd != -1) { + (void) close(d->fd); + } } return retval; } static krb5_error_code krb5_rc_io_open_internal(krb5_context context, krb5_rc_iostuff *d, char *fn, - char* full_pathname) + char* full_pathname) { krb5_int16 rc_vno; krb5_error_code retval = 0; @@ -230,54 +230,54 @@ krb5_rc_io_open_internal(krb5_context context, krb5_rc_iostuff *d, char *fn, GETDIR; if (full_pathname) { - if (!(d->fn = strdup(full_pathname))) - return KRB5_RC_IO_MALLOC; + if (!(d->fn = strdup(full_pathname))) + return KRB5_RC_IO_MALLOC; } else { - if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, fn) < 0) - return KRB5_RC_IO_MALLOC; + if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, fn) < 0) + return KRB5_RC_IO_MALLOC; } #ifdef NO_USERID d->fd = THREEPARAMOPEN(d->fn, O_RDWR | O_BINARY, 0600); #else if ((d->fd = stat(d->fn, &statb)) != -1) { - uid_t me; - - me = geteuid(); - /* must be owned by this user, to prevent some security problems with - * other users modifying replay cache stufff */ - if ((statb.st_uid != me) || ((statb.st_mode & S_IFMT) != S_IFREG)) { - FREE(d->fn); - return KRB5_RC_IO_PERM; - } - d->fd = THREEPARAMOPEN(d->fn, O_RDWR | O_BINARY, 0600); + uid_t me; + + me = geteuid(); + /* must be owned by this user, to prevent some security problems with + * other users modifying replay cache stufff */ + if ((statb.st_uid != me) || ((statb.st_mode & S_IFMT) != S_IFREG)) { + FREE(d->fn); + return KRB5_RC_IO_PERM; + } + d->fd = THREEPARAMOPEN(d->fn, O_RDWR | O_BINARY, 0600); } #endif if (d->fd == -1) { - retval = rc_map_errno(context, errno, d->fn, "open"); - goto cleanup; + retval = rc_map_errno(context, errno, d->fn, "open"); + goto cleanup; } set_cloexec_fd(d->fd); do_not_unlink = 0; retval = krb5_rc_io_read(context, d, (krb5_pointer) &rc_vno, - sizeof(rc_vno)); + sizeof(rc_vno)); if (retval) - goto cleanup; + goto cleanup; if (ntohs(rc_vno) != KRB5_RC_VNO) - retval = KRB5_RCACHE_BADVNO; + retval = KRB5_RCACHE_BADVNO; - cleanup: +cleanup: if (retval) { - if (d->fn) { - if (!do_not_unlink) - (void) unlink(d->fn); - FREE(d->fn); - d->fn = NULL; - } - if (d->fd >= 0) - (void) close(d->fd); + if (d->fn) { + if (!do_not_unlink) + (void) unlink(d->fn); + FREE(d->fn); + d->fn = NULL; + } + if (d->fd >= 0) + (void) close(d->fd); } return retval; } @@ -290,7 +290,7 @@ krb5_rc_io_open(krb5_context context, krb5_rc_iostuff *d, char *fn) krb5_error_code krb5_rc_io_move(krb5_context context, krb5_rc_iostuff *new1, - krb5_rc_iostuff *old) + krb5_rc_iostuff *old) { #if defined(_WIN32) || defined(__CYGWIN__) char *new_fn = NULL; @@ -334,29 +334,29 @@ krb5_rc_io_move(krb5_context context, krb5_rc_iostuff *new1, old->fd = -1; if (rename(old_fn, new_fn) == -1) { /* MUST be atomic! */ - retval = KRB5_RC_IO_UNKNOWN; - goto cleanup; + retval = KRB5_RC_IO_UNKNOWN; + goto cleanup; } retval = krb5_rc_io_open_internal(context, new1, 0, new_fn); if (retval) - goto cleanup; + goto cleanup; if (lseek(new1->fd, offset, SEEK_SET) == -1) { - retval = KRB5_RC_IO_UNKNOWN; - goto cleanup; + retval = KRB5_RC_IO_UNKNOWN; + goto cleanup; } - cleanup: +cleanup: free(new_fn); free(old_fn); return retval; #else char *fn = NULL; if (rename(old->fn, new1->fn) == -1) /* MUST be atomic! */ - return KRB5_RC_IO_UNKNOWN; + return KRB5_RC_IO_UNKNOWN; fn = new1->fn; - new1->fn = NULL; /* avoid clobbering */ + new1->fn = NULL; /* avoid clobbering */ (void) krb5_rc_io_close(context, new1); new1->fn = fn; new1->fd = dup(old->fd); @@ -367,32 +367,32 @@ krb5_rc_io_move(krb5_context context, krb5_rc_iostuff *new1, krb5_error_code krb5_rc_io_write(krb5_context context, krb5_rc_iostuff *d, krb5_pointer buf, - unsigned int num) + unsigned int num) { if (write(d->fd, (char *) buf, num) == -1) - switch(errno) - { + switch(errno) + { #ifdef EDQUOT - case EDQUOT: + case EDQUOT: #endif - case EFBIG: - case ENOSPC: - krb5_set_error_message (context, KRB5_RC_IO_SPACE, - "Can't write to replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_SPACE; - case EIO: - krb5_set_error_message (context, KRB5_RC_IO_IO, - "Can't write to replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_IO; - case EBADF: - default: - krb5_set_error_message (context, KRB5_RC_IO_UNKNOWN, - "Can't write to replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_UNKNOWN; - } + case EFBIG: + case ENOSPC: + krb5_set_error_message (context, KRB5_RC_IO_SPACE, + "Can't write to replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_SPACE; + case EIO: + krb5_set_error_message (context, KRB5_RC_IO_IO, + "Can't write to replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_IO; + case EBADF: + default: + krb5_set_error_message (context, KRB5_RC_IO_UNKNOWN, + "Can't write to replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_UNKNOWN; + } return 0; } @@ -405,38 +405,38 @@ krb5_rc_io_sync(krb5_context context, krb5_rc_iostuff *d) #endif #endif if (fsync(d->fd) == -1) { - switch(errno) - { - case EBADF: return KRB5_RC_IO_UNKNOWN; - case EIO: return KRB5_RC_IO_IO; - default: - krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, - "Cannot sync replay cache file: %s", - strerror(errno)); - return KRB5_RC_IO_UNKNOWN; - } + switch(errno) + { + case EBADF: return KRB5_RC_IO_UNKNOWN; + case EIO: return KRB5_RC_IO_IO; + default: + krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, + "Cannot sync replay cache file: %s", + strerror(errno)); + return KRB5_RC_IO_UNKNOWN; + } } return 0; } krb5_error_code krb5_rc_io_read(krb5_context context, krb5_rc_iostuff *d, krb5_pointer buf, - unsigned int num) + unsigned int num) { int count; if ((count = read(d->fd, (char *) buf, num)) == -1) - switch(errno) - { - case EIO: return KRB5_RC_IO_IO; - case EBADF: - default: - krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, - "Can't read from replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_UNKNOWN; - } + switch(errno) + { + case EIO: return KRB5_RC_IO_IO; + case EBADF: + default: + krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, + "Can't read from replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_UNKNOWN; + } if (count < 0 || (unsigned int)count != num) - return KRB5_RC_IO_EOF; + return KRB5_RC_IO_EOF; return 0; } @@ -444,13 +444,13 @@ krb5_error_code krb5_rc_io_close(krb5_context context, krb5_rc_iostuff *d) { if (d->fn != NULL) { - FREE(d->fn); - d->fn = NULL; + FREE(d->fn); + d->fn = NULL; } if (d->fd != -1) { - if (close(d->fd) == -1) /* can't happen */ - return KRB5_RC_IO_UNKNOWN; - d->fd = -1; + if (close(d->fd) == -1) /* can't happen */ + return KRB5_RC_IO_UNKNOWN; + d->fd = -1; } return 0; } @@ -459,27 +459,27 @@ krb5_error_code krb5_rc_io_destroy(krb5_context context, krb5_rc_iostuff *d) { if (unlink(d->fn) == -1) - switch(errno) - { - case EIO: - krb5_set_error_message(context, KRB5_RC_IO_IO, - "Can't destroy replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_IO; - case EPERM: - case EBUSY: - case EROFS: - krb5_set_error_message(context, KRB5_RC_IO_PERM, - "Can't destroy replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_PERM; - case EBADF: - default: - krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, - "Can't destroy replay cache: %s", - strerror(errno)); - return KRB5_RC_IO_UNKNOWN; - } + switch(errno) + { + case EIO: + krb5_set_error_message(context, KRB5_RC_IO_IO, + "Can't destroy replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_IO; + case EPERM: + case EBUSY: + case EROFS: + krb5_set_error_message(context, KRB5_RC_IO_PERM, + "Can't destroy replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_PERM; + case EBADF: + default: + krb5_set_error_message(context, KRB5_RC_IO_UNKNOWN, + "Can't destroy replay cache: %s", + strerror(errno)); + return KRB5_RC_IO_UNKNOWN; + } return 0; } @@ -503,7 +503,7 @@ krb5_rc_io_size(krb5_context context, krb5_rc_iostuff *d) struct stat statb; if (fstat(d->fd, &statb) == 0) - return statb.st_size; + return statb.st_size; else - return 0; + return 0; } |