better error reporting

This commit is contained in:
Love Hornquist Astrand
2013-07-10 21:50:04 +02:00
parent 5dfaa0d10b
commit 403f599dbd

View File

@@ -223,7 +223,11 @@ scrub_file (int fd)
return errno; return errno;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
while(pos > 0) { while(pos > 0) {
ssize_t tmp = write(fd, buf, min((off_t)sizeof(buf), pos)); ssize_t tmp;
size_t wr = sizeof(buf);
if (wr > pos)
wr = (size_t)pos;
tmp = write(fd, buf, wr);
if (tmp < 0) if (tmp < 0)
return errno; return errno;
@@ -330,18 +334,20 @@ fcc_gen_new(krb5_context context, krb5_ccache *id)
} }
ret = _krb5_expand_path_tokens(context, file, &exp_file); ret = _krb5_expand_path_tokens(context, file, &exp_file);
free(file); free(file);
if (ret) if (ret) {
free(f);
return ret; return ret;
}
file = exp_file; file = exp_file;
fd = mkstemp(exp_file); fd = mkstemp(exp_file);
if(fd < 0) { if(fd < 0) {
int xret = errno; ret = (krb5_error_code)errno;
krb5_set_error_message(context, xret, N_("mkstemp %s failed", ""), exp_file); krb5_set_error_message(context, ret, N_("mkstemp %s failed", ""), exp_file);
free(f); free(f);
free(exp_file); free(exp_file);
return xret; return ret;
} }
close(fd); close(fd);
f->filename = exp_file; f->filename = exp_file;
@@ -379,6 +385,7 @@ storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
static krb5_error_code KRB5_CALLCONV static krb5_error_code KRB5_CALLCONV
fcc_open(krb5_context context, fcc_open(krb5_context context,
krb5_ccache id, krb5_ccache id,
const char *operation,
int *fd_ret, int *fd_ret,
int flags, int flags,
mode_t mode) mode_t mode)
@@ -399,8 +406,8 @@ fcc_open(krb5_context context,
char buf[128]; char buf[128];
ret = errno; ret = errno;
rk_strerror_r(ret, buf, sizeof(buf)); rk_strerror_r(ret, buf, sizeof(buf));
krb5_set_error_message(context, ret, N_("open(%s): %s", "file, error"), krb5_set_error_message(context, ret, N_("%s open(%s): %s", "file, error"),
filename, buf); operation, filename, buf);
return ret; return ret;
} }
rk_cloexec(fd); rk_cloexec(fd);
@@ -427,7 +434,7 @@ fcc_initialize(krb5_context context,
unlink (f->filename); unlink (f->filename);
ret = fcc_open(context, id, &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600); ret = fcc_open(context, id, "initialize", &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
if(ret) if(ret)
return ret; return ret;
{ {
@@ -502,7 +509,7 @@ fcc_store_cred(krb5_context context,
int ret; int ret;
int fd; int fd;
ret = fcc_open(context, id, &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0); ret = fcc_open(context, id, "store", &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0);
if(ret) if(ret)
return ret; return ret;
{ {
@@ -535,11 +542,12 @@ fcc_store_cred(krb5_context context,
} }
static krb5_error_code static krb5_error_code
init_fcc (krb5_context context, init_fcc(krb5_context context,
krb5_ccache id, krb5_ccache id,
krb5_storage **ret_sp, const char *operation,
int *ret_fd, krb5_storage **ret_sp,
krb5_deltat *kdc_offset) int *ret_fd,
krb5_deltat *kdc_offset)
{ {
int fd; int fd;
int8_t pvno, tag; int8_t pvno, tag;
@@ -549,13 +557,14 @@ init_fcc (krb5_context context,
if (kdc_offset) if (kdc_offset)
*kdc_offset = 0; *kdc_offset = 0;
ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0); ret = fcc_open(context, id, operation, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
if(ret) if(ret)
return ret; return ret;
sp = krb5_storage_from_fd(fd); sp = krb5_storage_from_fd(fd);
if(sp == NULL) { if(sp == NULL) {
ret = krb5_enomem(context); krb5_clear_error_message(context);
ret = ENOMEM;
goto out; goto out;
} }
krb5_storage_set_eof_code(sp, KRB5_CC_END); krb5_storage_set_eof_code(sp, KRB5_CC_END);
@@ -692,7 +701,7 @@ fcc_get_principal(krb5_context context,
int fd; int fd;
krb5_storage *sp; krb5_storage *sp;
ret = init_fcc (context, id, &sp, &fd, NULL); ret = init_fcc (context, id, "get-pricipal", &sp, &fd, NULL);
if (ret) if (ret)
return ret; return ret;
ret = krb5_ret_principal(sp, principal); ret = krb5_ret_principal(sp, principal);
@@ -721,12 +730,14 @@ fcc_get_first (krb5_context context,
return krb5_einval(context, 2); return krb5_einval(context, 2);
*cursor = malloc(sizeof(struct fcc_cursor)); *cursor = malloc(sizeof(struct fcc_cursor));
if (*cursor == NULL) if (*cursor == NULL) {
return krb5_enomem(context); krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
return ENOMEM;
}
memset(*cursor, 0, sizeof(struct fcc_cursor)); memset(*cursor, 0, sizeof(struct fcc_cursor));
ret = init_fcc (context, id, &FCC_CURSOR(*cursor)->sp, ret = init_fcc(context, id, "get-frist", &FCC_CURSOR(*cursor)->sp,
&FCC_CURSOR(*cursor)->fd, NULL); &FCC_CURSOR(*cursor)->fd, NULL);
if (ret) { if (ret) {
free(*cursor); free(*cursor);
*cursor = NULL; *cursor = NULL;
@@ -820,7 +831,7 @@ fcc_remove_cred(krb5_context context,
ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id)); ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
if (ret < 0 || newname == NULL) { if (ret < 0 || newname == NULL) {
krb5_cc_destroy(context, copy); krb5_cc_destroy(context, copy);
return krb5_enomem(context); return ENOMEM;
} }
fd = mkstemp(&newname[5]); fd = mkstemp(&newname[5]);
@@ -887,8 +898,10 @@ fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
struct fcache_iter *iter; struct fcache_iter *iter;
iter = calloc(1, sizeof(*iter)); iter = calloc(1, sizeof(*iter));
if (iter == NULL) if (iter == NULL) {
return krb5_enomem(context); krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
return ENOMEM;
}
iter->first = 1; iter->first = 1;
*cursor = iter; *cursor = iter;
return 0; return 0;
@@ -911,7 +924,14 @@ fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
} }
iter->first = 0; iter->first = 0;
fn = krb5_cc_default_name(context); /*
* Can't call krb5_cc_default_name here since it refers back to
* krb5_cc_cache_match() which will call back into this function.
*
* Just use the default value if its set, otherwise, use the
* default hardcoded value.
*/
fn = context->default_cc_name;
if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) { if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
ret = _krb5_expand_default_cc_name(context, ret = _krb5_expand_default_cc_name(context,
KRB5_DEFAULT_CCNAME_FILE, KRB5_DEFAULT_CCNAME_FILE,
@@ -971,13 +991,13 @@ fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
int fd1, fd2; int fd1, fd2;
char buf[BUFSIZ]; char buf[BUFSIZ];
ret = fcc_open(context, from, &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0); ret = fcc_open(context, from, "move/from", &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
if(ret) if(ret)
return ret; return ret;
unlink(FILENAME(to)); unlink(FILENAME(to));
ret = fcc_open(context, to, &fd2, ret = fcc_open(context, to, "move/to", &fd2,
O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600); O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
if(ret) if(ret)
goto out1; goto out1;
@@ -1019,7 +1039,7 @@ fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
{ {
krb5_storage *sp; krb5_storage *sp;
int fd; int fd;
if ((ret = init_fcc (context, to, &sp, &fd, NULL)) == 0) { if ((ret = init_fcc (context, to, "move", &sp, &fd, NULL)) == 0) {
if (sp) if (sp)
krb5_storage_free(sp); krb5_storage_free(sp);
fcc_unlock(context, fd); fcc_unlock(context, fd);
@@ -1047,7 +1067,7 @@ fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
struct stat sb; struct stat sb;
int fd; int fd;
ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0); ret = fcc_open(context, id, "lastchange", &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
if(ret) if(ret)
return ret; return ret;
ret = fstat(fd, &sb); ret = fstat(fd, &sb);
@@ -1073,7 +1093,7 @@ fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset
krb5_error_code ret; krb5_error_code ret;
krb5_storage *sp = NULL; krb5_storage *sp = NULL;
int fd; int fd;
ret = init_fcc(context, id, &sp, &fd, kdc_offset); ret = init_fcc(context, id, "get-kdc-offset", &sp, &fd, kdc_offset);
if (sp) if (sp)
krb5_storage_free(sp); krb5_storage_free(sp);
fcc_unlock(context, fd); fcc_unlock(context, fd);