From 89bd3ffd7194a36097e38600cc3baa4cf6e259f2 Mon Sep 17 00:00:00 2001 From: John Kohl Date: Tue, 3 Jul 1990 15:43:18 +0000 Subject: [PATCH] better error codes & error checking git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1037 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/ccache/file/fcc_close.c | 11 ++++-- src/lib/krb5/ccache/file/fcc_destry.c | 51 ++++++++++++++++++++------- src/lib/krb5/ccache/file/fcc_gennew.c | 10 +++--- src/lib/krb5/ccache/file/fcc_gprin.c | 2 +- src/lib/krb5/ccache/file/fcc_nseq.c | 14 +++++--- src/lib/krb5/ccache/file/fcc_read.c | 34 +++++++++--------- src/lib/krb5/ccache/file/fcc_reslv.c | 8 ++--- src/lib/krb5/ccache/file/fcc_retrv.c | 4 +-- src/lib/krb5/ccache/file/fcc_sflags.c | 2 +- src/lib/krb5/ccache/file/fcc_sseq.c | 6 ++-- src/lib/krb5/ccache/file/fcc_store.c | 4 +-- src/lib/krb5/ccache/file/fcc_test.c | 2 +- src/lib/krb5/ccache/file/fcc_write.c | 12 +++---- 13 files changed, 100 insertions(+), 60 deletions(-) diff --git a/src/lib/krb5/ccache/file/fcc_close.c b/src/lib/krb5/ccache/file/fcc_close.c index 3f71b8777..ce2fdb791 100644 --- a/src/lib/krb5/ccache/file/fcc_close.c +++ b/src/lib/krb5/ccache/file/fcc_close.c @@ -30,13 +30,20 @@ krb5_error_code krb5_fcc_close(id) krb5_ccache id; { + register int closeval = KRB5_OK; + if (OPENCLOSE(id)) { - close(((krb5_fcc_data *) id->data)->fd); + closeval = close(((krb5_fcc_data *) id->data)->fd); ((krb5_fcc_data *) id->data)->fd = -1; + if (closeval == -1) { + closeval = krb5_fcc_interpret(errno); + } else + closeval = KRB5_OK; + } xfree(((krb5_fcc_data *) id->data)->filename); xfree(((krb5_fcc_data *) id->data)); xfree(id); - return KRB5_OK; + return closeval; } diff --git a/src/lib/krb5/ccache/file/fcc_destry.c b/src/lib/krb5/ccache/file/fcc_destry.c index 3c2d3b9a3..2df010919 100644 --- a/src/lib/krb5/ccache/file/fcc_destry.c +++ b/src/lib/krb5/ccache/file/fcc_destry.c @@ -31,38 +31,65 @@ krb5_error_code krb5_fcc_destroy(id) struct stat buf; unsigned long size; char zeros[BUFSIZ]; - int ret, i; + register int ret, i; if (OPENCLOSE(id)) { ret = open(((krb5_fcc_data *) id->data)->filename, O_RDWR, 0); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ((krb5_fcc_data *) id->data)->fd = ret; } else lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); ret = unlink(((krb5_fcc_data *) id->data)->filename); - if (ret < 0) - return errno; + if (ret < 0) { + ret = krb5_fcc_interpret(errno); + if (OPENCLOSE(id)) { + (void) close(((krb5_fcc_data *)id->data)->fd); + ((krb5_fcc_data *) id->data)->fd = -1; + } + return ret; + } ret = fstat(((krb5_fcc_data *) id->data)->fd, &buf); - if (ret < 0) - return errno; + if (ret < 0) { + ret = krb5_fcc_interpret(errno); + if (OPENCLOSE(id)) { + (void) close(((krb5_fcc_data *)id->data)->fd); + ((krb5_fcc_data *) id->data)->fd = -1; + } + return ret; + } /* XXX This may not be legal XXX */ size = (unsigned long) buf.st_size; bzero(zeros, BUFSIZ); for (i=0; i < size / BUFSIZ; i++) - if (write(((krb5_fcc_data *) id->data)->fd, zeros, BUFSIZ) < 0) - return errno; + if (write(((krb5_fcc_data *) id->data)->fd, zeros, BUFSIZ) < 0) { + ret = krb5_fcc_interpret(errno); + if (OPENCLOSE(id)) { + (void) close(((krb5_fcc_data *)id->data)->fd); + ((krb5_fcc_data *) id->data)->fd = -1; + } + return ret; + } - if (write(((krb5_fcc_data *) id->data)->fd, zeros, size % BUFSIZ) < 0) - return errno; + if (write(((krb5_fcc_data *) id->data)->fd, zeros, size % BUFSIZ) < 0) { + ret = krb5_fcc_interpret(errno); + if (OPENCLOSE(id)) { + (void) close(((krb5_fcc_data *)id->data)->fd); + ((krb5_fcc_data *) id->data)->fd = -1; + } + return ret; + } - close(((krb5_fcc_data *) id->data)->fd); + ret = close(((krb5_fcc_data *) id->data)->fd); ((krb5_fcc_data *) id->data)->fd = -1; - return KRB5_OK; + if (ret) + ret = krb5_fcc_interpret(errno); + + return ret; } diff --git a/src/lib/krb5/ccache/file/fcc_gennew.c b/src/lib/krb5/ccache/file/fcc_gennew.c index 8db6419f8..0da4ef63e 100644 --- a/src/lib/krb5/ccache/file/fcc_gennew.c +++ b/src/lib/krb5/ccache/file/fcc_gennew.c @@ -30,7 +30,7 @@ extern krb5_cc_ops krb5_fcc_ops; * The filled in krb5_ccache id. * * Errors: - * KRB5_NOMEM - there was insufficient memory to allocate the + * KRB5_CC_NOMEM - there was insufficient memory to allocate the * krb5_ccache. id is undefined. * system errors (from open) */ @@ -45,7 +45,7 @@ krb5_fcc_generate_new (id) /* Allocate memory */ lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache)); if (lid == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; lid->ops = &krb5_fcc_ops; @@ -55,7 +55,7 @@ krb5_fcc_generate_new (id) lid->data = (krb5_pointer) malloc(sizeof(krb5_fcc_data)); if (lid->data == NULL) { xfree(lid); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } ((krb5_fcc_data *) lid->data)->filename = (char *) @@ -63,7 +63,7 @@ krb5_fcc_generate_new (id) if (((krb5_fcc_data *) lid->data)->filename == NULL) { xfree(((krb5_fcc_data *) lid->data)); xfree(lid); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } ((krb5_fcc_data *) lid->data)->flags = 0; @@ -74,7 +74,7 @@ krb5_fcc_generate_new (id) /* Make sure the file name is reserved */ ret = open(((krb5_fcc_data *) lid->data)->filename, O_CREAT | O_EXCL, 0); if (ret == -1) - return errno; + return krb5_fcc_interpret(errno); else { /* Ignore user's umask, set mode = 0600 */ fchmod(ret, S_IREAD | S_IWRITE); diff --git a/src/lib/krb5/ccache/file/fcc_gprin.c b/src/lib/krb5/ccache/file/fcc_gprin.c index a95409b52..869bf69f7 100644 --- a/src/lib/krb5/ccache/file/fcc_gprin.c +++ b/src/lib/krb5/ccache/file/fcc_gprin.c @@ -28,7 +28,7 @@ static char fcc_gprinc_c[] = "$Id$"; * * Errors: * system errors - * KRB5_NOMEM + * KRB5_CC_NOMEM */ krb5_error_code krb5_fcc_get_principal(id, princ) diff --git a/src/lib/krb5/ccache/file/fcc_nseq.c b/src/lib/krb5/ccache/file/fcc_nseq.c index 4c5fba350..0303a0460 100644 --- a/src/lib/krb5/ccache/file/fcc_nseq.c +++ b/src/lib/krb5/ccache/file/fcc_nseq.c @@ -56,15 +56,21 @@ krb5_fcc_next_cred(id, cursor, creds) if (OPENCLOSE(id)) { ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ((krb5_fcc_data *) id->data)->fd = ret; } fcursor = (krb5_fcc_cursor *) *cursor; ret = lseek(((krb5_fcc_data *) id->data)->fd, fcursor->pos, L_SET); - if (ret < 0) - return errno; + if (ret < 0) { + ret = krb5_fcc_interpret(errno); + if (OPENCLOSE(id)) { + (void) close(((krb5_fcc_data *)id->data)->fd); + ((krb5_fcc_data *)id->data)->fd = -1; + } + return ret; + } kret = krb5_fcc_read_principal(id, &creds->client); TCHECK(kret); @@ -88,11 +94,11 @@ krb5_fcc_next_cred(id, cursor, creds) fcursor->pos = tell(((krb5_fcc_data *) id->data)->fd); cursor = (krb5_cc_cursor *) fcursor; +lose: if (OPENCLOSE(id)) { close(((krb5_fcc_data *) id->data)->fd); ((krb5_fcc_data *) id->data)->fd = -1; } -lose: if (kret != KRB5_OK) { if (creds->client) krb5_free_principal(creds->client); diff --git a/src/lib/krb5/ccache/file/fcc_read.c b/src/lib/krb5/ccache/file/fcc_read.c index 6947ae680..f69a36838 100644 --- a/src/lib/krb5/ccache/file/fcc_read.c +++ b/src/lib/krb5/ccache/file/fcc_read.c @@ -25,7 +25,7 @@ static char fcc_read_c[] = "$Id$"; * Reads len bytes from the cache id, storing them in buf. * * Errors: - * KRB5_EOF - there were not len bytes available + * KRB5_CC_END - there were not len bytes available * system errors (read) */ krb5_error_code @@ -38,9 +38,9 @@ krb5_fcc_read(id, buf, len) ret = read(((krb5_fcc_data *) id->data)->fd, (char *) buf, len); if (ret == -1) - return errno; + return krb5_fcc_interpret(errno); else if (ret != len) - return KRB5_EOF; + return KRB5_CC_END; else return KRB5_OK; } @@ -59,7 +59,7 @@ krb5_fcc_read(id, buf, len) * * Errors: * system errors (read errors) - * KRB5_NOMEM + * KRB5_CC_NOMEM */ krb5_error_code @@ -89,13 +89,13 @@ krb5_fcc_read_principal(id, princ) */ *princ = (krb5_principal) calloc(length+1, sizeof(krb5_data *)); if (*princ == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; for (i=0; i < length; i++) { (*princ)[i] = (krb5_data *) malloc(sizeof(krb5_data)); if ((*princ)[i] == NULL) { krb5_free_principal(*princ); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } kret = krb5_fcc_read_data(id, (*princ)[i]); CHECK(kret); @@ -128,13 +128,13 @@ krb5_fcc_read_addrs(id, addrs) */ *addrs = (krb5_address **) calloc(length+1, sizeof(krb5_address *)); if (*addrs == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; for (i=0; i < length; i++) { (*addrs)[i] = (krb5_address *) malloc(sizeof(krb5_address)); if ((*addrs)[i] == NULL) { krb5_free_address(*addrs); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } kret = krb5_fcc_read_addr(id, (*addrs)[i]); CHECK(kret); @@ -164,18 +164,18 @@ krb5_fcc_read_keyblock(id, keyblock) keyblock->contents = (unsigned char *) malloc(keyblock->length* sizeof(krb5_octet)); if (keyblock->contents == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; ret = read(((krb5_fcc_data *) id->data)->fd, (char *)keyblock->contents, (keyblock->length)*sizeof(krb5_octet)); if (ret < 0) { xfree(keyblock->contents); - return errno; + return krb5_fcc_interpret(errno); } if (ret != (keyblock->length)*sizeof(krb5_octet)) { xfree(keyblock->contents); - return KRB5_EOF; + return KRB5_CC_END; } return KRB5_OK; @@ -200,17 +200,17 @@ krb5_fcc_read_data(id, data) data->data = (char *) malloc(data->length); if (data->data == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; ret = read(((krb5_fcc_data *) id->data)->fd, (char *)data->data, data->length); if (ret == -1) { xfree(data->data); - return errno; + return krb5_fcc_interpret(errno); } if (ret != data->length) { xfree(data->data); - return KRB5_EOF; + return KRB5_CC_END; } return KRB5_OK; errout: @@ -237,17 +237,17 @@ krb5_fcc_read_addr(id, addr) addr->contents = (krb5_octet *) malloc(addr->length); if (addr->contents == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; ret = read(((krb5_fcc_data *) id->data)->fd, (char *)addr->contents, (addr->length)*sizeof(krb5_octet)); if (ret == -1) { xfree(addr->contents); - return errno; + return krb5_fcc_interpret(errno); } if (ret != (addr->length)*sizeof(krb5_octet)) { xfree(addr->contents); - return KRB5_EOF; + return KRB5_CC_END; } return KRB5_OK; errout: diff --git a/src/lib/krb5/ccache/file/fcc_reslv.c b/src/lib/krb5/ccache/file/fcc_reslv.c index 620ce609e..4dd5a520b 100644 --- a/src/lib/krb5/ccache/file/fcc_reslv.c +++ b/src/lib/krb5/ccache/file/fcc_reslv.c @@ -35,7 +35,7 @@ extern krb5_cc_ops krb5_fcc_ops; * A filled in krb5_ccache structure "id". * * Errors: - * KRB5_NOMEM - there was insufficient memory to allocate the + * KRB5_CC_NOMEM - there was insufficient memory to allocate the * krb5_ccache. id is undefined. * permission errors */ @@ -48,14 +48,14 @@ krb5_fcc_resolve (id, residual) lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache)); if (lid == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; lid->ops = &krb5_fcc_ops; lid->data = (krb5_pointer) malloc(sizeof(krb5_fcc_data)); if (lid->data == NULL) { xfree(lid); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } ((krb5_fcc_data *) lid->data)->filename = (char *) @@ -64,7 +64,7 @@ krb5_fcc_resolve (id, residual) if (((krb5_fcc_data *) lid->data)->filename == NULL) { xfree(((krb5_fcc_data *) lid->data)); xfree(lid); - return KRB5_NOMEM; + return KRB5_CC_NOMEM; } /* default to open/close on every trn */ diff --git a/src/lib/krb5/ccache/file/fcc_retrv.c b/src/lib/krb5/ccache/file/fcc_retrv.c index 3a9c701d7..a2773f3e2 100644 --- a/src/lib/krb5/ccache/file/fcc_retrv.c +++ b/src/lib/krb5/ccache/file/fcc_retrv.c @@ -45,7 +45,7 @@ static krb5_boolean standard_fields_match * Errors: * system errors * permission errors - * KRB5_NOMEM + * KRB5_CC_NOMEM */ krb5_error_code krb5_fcc_retrieve(id, whichfields, mcreds, creds) @@ -94,7 +94,7 @@ krb5_fcc_retrieve(id, whichfields, mcreds, creds) /* If we get here, a match wasn't found */ krb5_fcc_end_seq_get(id, &cursor); - return KRB5_NOTFOUND; + return KRB5_CC_NOTFOUND; } static krb5_boolean diff --git a/src/lib/krb5/ccache/file/fcc_sflags.c b/src/lib/krb5/ccache/file/fcc_sflags.c index c0b23e340..24e38175f 100644 --- a/src/lib/krb5/ccache/file/fcc_sflags.c +++ b/src/lib/krb5/ccache/file/fcc_sflags.c @@ -49,7 +49,7 @@ krb5_fcc_set_flags(id, flags) if (OPENCLOSE(id)) { ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ((krb5_fcc_data *) id->data)->fd = ret; } } diff --git a/src/lib/krb5/ccache/file/fcc_sseq.c b/src/lib/krb5/ccache/file/fcc_sseq.c index ce66421c8..0ff5a66ec 100644 --- a/src/lib/krb5/ccache/file/fcc_sseq.c +++ b/src/lib/krb5/ccache/file/fcc_sseq.c @@ -28,7 +28,7 @@ static char fcc_sseq_c[] = "$Id$"; * of the final krb5_fcc_end_seq_get, the results are undefined. * * Errors: - * KRB5_NOMEM + * KRB5_CC_NOMEM * system errors */ krb5_error_code @@ -41,13 +41,13 @@ krb5_fcc_start_seq_get(id, cursor) fcursor = (krb5_fcc_cursor *) malloc(sizeof(krb5_fcc_cursor)); if (fcursor == NULL) - return KRB5_NOMEM; + return KRB5_CC_NOMEM; /* Make sure we start reading right after the primary principal */ if (OPENCLOSE(id)) { ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ((krb5_fcc_data *) id->data)->fd = ret; } else diff --git a/src/lib/krb5/ccache/file/fcc_store.c b/src/lib/krb5/ccache/file/fcc_store.c index 7141f5221..e6cd28305 100644 --- a/src/lib/krb5/ccache/file/fcc_store.c +++ b/src/lib/krb5/ccache/file/fcc_store.c @@ -44,13 +44,13 @@ krb5_fcc_store(id, creds) ret = open(((krb5_fcc_data *) id->data)->filename, O_RDWR | O_APPEND, 0); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ((krb5_fcc_data *) id->data)->fd = ret; } ret = lseek(((krb5_fcc_data *) id->data)->fd, 0, L_XTND); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); ret = krb5_fcc_store_principal(id, creds->client); TCHECK(ret); diff --git a/src/lib/krb5/ccache/file/fcc_test.c b/src/lib/krb5/ccache/file/fcc_test.c index a8f7f5120..0b866f5de 100644 --- a/src/lib/krb5/ccache/file/fcc_test.c +++ b/src/lib/krb5/ccache/file/fcc_test.c @@ -97,7 +97,7 @@ void fcc_test() kret = krb5_fcc_start_seq_get(id, &cursor); CHECK(kret, "start_seq_get"); kret = 0; - while (kret != KRB5_EOF) { + while (kret != KRB5_CC_END) { printf("Calling next_cred\n"); kret = krb5_fcc_next_cred(id, &cursor, &creds); CHECK(kret, "next_cred"); diff --git a/src/lib/krb5/ccache/file/fcc_write.c b/src/lib/krb5/ccache/file/fcc_write.c index 7181ae7b6..0c7121c42 100644 --- a/src/lib/krb5/ccache/file/fcc_write.c +++ b/src/lib/krb5/ccache/file/fcc_write.c @@ -40,7 +40,7 @@ krb5_fcc_write(id, buf, len) ret = write(((krb5_fcc_data *)id->data)->fd, (char *) buf, len); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); return KRB5_OK; } @@ -120,9 +120,9 @@ krb5_fcc_store_keyblock(id, keyblock) ret = write(((krb5_fcc_data *) id->data)->fd, (char *)keyblock->contents, (keyblock->length)*sizeof(krb5_octet)); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); if (ret != (keyblock->length)*sizeof(krb5_octet)) - return KRB5_EOF; + return KRB5_CC_END; return KRB5_OK; } @@ -141,9 +141,9 @@ krb5_fcc_store_addr(id, addr) ret = write(((krb5_fcc_data *) id->data)->fd, (char *)addr->contents, (addr->length)*sizeof(krb5_octet)); if (ret < 0) - return errno; + return krb5_fcc_interpret(errno); if (ret != (addr->length)*sizeof(krb5_octet)) - return KRB5_EOF; + return KRB5_CC_END; return KRB5_OK; } @@ -159,7 +159,7 @@ krb5_fcc_store_data(id, data) CHECK(ret); ret = write(((krb5_fcc_data *) id->data)->fd, data->data, data->length); if (ret == -1) - return errno; + return krb5_fcc_interpret(errno); return KRB5_OK; } -- 2.26.2