From: Barry Jaspan Date: Tue, 30 Jan 1990 16:50:48 +0000 (+0000) Subject: function. Also random bug fixes, and the keyblock confusion has been X-Git-Tag: krb5-1.0-alpha2~1182 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b70673f44985430908ea7ee7660c7aa7531c34b4;p=krb5.git function. Also random bug fixes, and the keyblock confusion has been fixed. bjaspan: changed #ifdef OPENCLOSE to check of flags, added set_flags git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@201 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/ccache/file/fcc.h b/src/lib/krb5/ccache/file/fcc.h index a407f8348..cbfb8e77f 100644 --- a/src/lib/krb5/ccache/file/fcc.h +++ b/src/lib/krb5/ccache/file/fcc.h @@ -22,18 +22,23 @@ #include "fcc-os.h" /* XXX Until I find out the right #define for this.. XXX */ -#define KRB5_OK 0 -#define KRB5_NOMEM 1 -#define KRB5_NOTFOUND 2 +#define KRB5_OK -1000 +#define KRB5_NOMEM -1001 +#define KRB5_NOTFOUND -1002 +#define KRB5_EOF -1003 #define KRB5_FCC_MAXLEN 100 #ifndef TKT_ROOT #define TKT_ROOT "/tmp/tkt" #endif +/* macros to make checking flags easier */ +#define OPENCLOSE(id) (((krb5_fcc_data *)id->data)->flags & KRB5_TC_OPENCLOSE) + typedef struct _krb5_fcc_data { char *filename; int fd; + krb5_flags flags; } krb5_fcc_data; /* An off_t can be arbitrarily complex */ diff --git a/src/lib/krb5/ccache/file/fcc_close.c b/src/lib/krb5/ccache/file/fcc_close.c index 7c808b5c1..4a6a39053 100644 --- a/src/lib/krb5/ccache/file/fcc_close.c +++ b/src/lib/krb5/ccache/file/fcc_close.c @@ -25,26 +25,14 @@ static char fcc_resolve_c[] = "$Id$"; * Effects: * Closes the file cache, invalidates the id, and frees any resources * associated with the cache. - * - * Errors: - * system errors - * permission errors */ krb5_error_code krb5_fcc_close(id) krb5_ccache id; { - int ret; - -#ifdef OPENCLOSE -#else - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); - ret = unlink(((krb5_fcc_data *) id->data)->filename); - if (ret < 0) - return errno; - free(((krb5_fcc_data *) id->data)->filename); free(((krb5_fcc_data *) id->data)); free(id); diff --git a/src/lib/krb5/ccache/file/fcc_destry.c b/src/lib/krb5/ccache/file/fcc_destry.c index 708d32a69..23c9a88cc 100644 --- a/src/lib/krb5/ccache/file/fcc_destry.c +++ b/src/lib/krb5/ccache/file/fcc_destry.c @@ -33,14 +33,15 @@ krb5_error_code krb5_fcc_destroy(id) char zeros[BUFSIZ]; int ret, i; -#ifdef OPENCLOSE - ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data)-> - filename, O_RDWR, 0); - if (((krb5_fcc_data *) id->data)->fd < 0) - return errno; -#else - lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); -#endif + if (OPENCLOSE(id)) { + ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data) + ->filename, O_RDWR, 0); + if (((krb5_fcc_data *) id->data)->fd < 0) + return errno; + } + else + lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); + ret = unlink(((krb5_fcc_data *) id->data)->filename); if (ret < 0) return errno; diff --git a/src/lib/krb5/ccache/file/fcc_eseq.c b/src/lib/krb5/ccache/file/fcc_eseq.c index 54190985d..7f9db3c10 100644 --- a/src/lib/krb5/ccache/file/fcc_eseq.c +++ b/src/lib/krb5/ccache/file/fcc_eseq.c @@ -35,12 +35,10 @@ krb5_fcc_end_seq_get(id, cursor) krb5_ccache id; krb5_cc_cursor *cursor; { -#ifdef OPENCLOSE -#else - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); - free((krb5_fcc_cursor *) cursor); + free((krb5_fcc_cursor *) *cursor); return KRB5_OK; } diff --git a/src/lib/krb5/ccache/file/fcc_gennew.c b/src/lib/krb5/ccache/file/fcc_gennew.c index be8675ea8..2776a7421 100644 --- a/src/lib/krb5/ccache/file/fcc_gennew.c +++ b/src/lib/krb5/ccache/file/fcc_gennew.c @@ -35,46 +35,46 @@ extern krb5_cc_ops krb5_fcc_ops; */ krb5_error_code krb5_fcc_generate_new (id) - krb5_ccache id; - + krb5_ccache *id; { + krb5_ccache lid; int ret; char scratch[100]; /* XXX Is this large enough */ /* Allocate memory */ - id = (krb5_ccache) malloc(sizeof(struct _krb5_ccache)); - if (id == NULL) + lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache)); + if (lid == NULL) return KRB5_NOMEM; + lid->ops = &krb5_fcc_ops; + sprintf(scratch, "%sXXXXXX", TKT_ROOT); mktemp(scratch); - id->data = (krb5_fcc_data *) malloc(sizeof(krb5_fcc_data)); - if (((krb5_fcc_data *) id->data) == NULL) { - free(id); + lid->data = (krb5_fcc_data *) malloc(sizeof(krb5_fcc_data)); + if (((krb5_fcc_data *) lid->data) == NULL) { + free(lid); return KRB5_NOMEM; } - ((krb5_fcc_data *) id->data)->filename = (char *) + ((krb5_fcc_data *) lid->data)->filename = (char *) malloc(strlen(scratch) + 1); - if (((krb5_fcc_data *) id->data)->filename == NULL) { - free(((krb5_fcc_data *) id->data)); - free(id); + if (((krb5_fcc_data *) lid->data)->filename == NULL) { + free(((krb5_fcc_data *) lid->data)); + free(lid); return KRB5_NOMEM; } /* Set up the filename */ - strcpy(((krb5_fcc_data *) id->data)->filename, scratch); - - /* Copy the virtual operation pointers into id */ - bcopy((char *) &krb5_fcc_ops, id->ops, sizeof(krb5_cc_ops)); + strcpy(((krb5_fcc_data *) lid->data)->filename, scratch); /* Make sure the file name is reserved */ - ret = open(((krb5_fcc_data *) id->data)->filename, O_CREAT| O_EXCL,0600); + ret = open(((krb5_fcc_data *) lid->data)->filename, O_CREAT| O_EXCL,0600); if (ret == -1) return ret; else { close(ret); + *id = lid; return KRB5_OK; } } diff --git a/src/lib/krb5/ccache/file/fcc_gprin.c b/src/lib/krb5/ccache/file/fcc_gprin.c index b644f0e57..ef0044ca1 100644 --- a/src/lib/krb5/ccache/file/fcc_gprin.c +++ b/src/lib/krb5/ccache/file/fcc_gprin.c @@ -36,20 +36,20 @@ krb5_fcc_get_principal(id, princ) krb5_principal *princ; { krb5_error_code kret; -#ifdef OPENCLOSE - ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data)-> - filename, O_RDONLY, 0); - if (((krb5_fcc_data *) id->data)->fd < 0) - return errno; -#else - lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); -#endif + + if (OPENCLOSE(id)) { + ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data) + ->filename, O_RDONLY, 0); + if (((krb5_fcc_data *) id->data)->fd < 0) + return errno; + } + else + lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); kret = krb5_fcc_read_principal(id, princ); -#ifdef OPENCLOSE - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); return kret; } diff --git a/src/lib/krb5/ccache/file/fcc_init.c b/src/lib/krb5/ccache/file/fcc_init.c index bf337e03c..a135378d5 100644 --- a/src/lib/krb5/ccache/file/fcc_init.c +++ b/src/lib/krb5/ccache/file/fcc_init.c @@ -37,7 +37,8 @@ krb5_fcc_initialize(id, princ) { int ret; - ret = open(((krb5_fcc_data *) id->data)->filename, O_CREAT | O_TRUNC | O_RDWR, 0); + ret = open(((krb5_fcc_data *) id->data)->filename, O_CREAT | O_TRUNC | + O_RDWR, 0); if (ret < 0) return errno; ((krb5_fcc_data *) id->data)->fd = ret; @@ -48,9 +49,8 @@ krb5_fcc_initialize(id, princ) krb5_fcc_store_principal(id, princ); -#ifdef OPENCLOSE - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); return KRB5_OK; } diff --git a/src/lib/krb5/ccache/file/fcc_nseq.c b/src/lib/krb5/ccache/file/fcc_nseq.c index 3f3a0159f..ef1a7817c 100644 --- a/src/lib/krb5/ccache/file/fcc_nseq.c +++ b/src/lib/krb5/ccache/file/fcc_nseq.c @@ -51,14 +51,14 @@ krb5_fcc_next_cred(id, cursor, creds) krb5_error_code kret; krb5_fcc_cursor *fcursor; -#ifdef OPENCLOSE - ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); - if (ret < 0) - return errno; - ((krb5_fcc_data *) id->data)->fd = ret; -#endif + if (OPENCLOSE(id)) { + ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); + if (ret < 0) + return errno; + ((krb5_fcc_data *) id->data)->fd = ret; + } - fcursor = (krb5_fcc_cursor *) cursor; + fcursor = (krb5_fcc_cursor *) *cursor; ret = lseek(((krb5_fcc_data *) id->data)->fd, fcursor->pos, L_SET); if (ret < 0) diff --git a/src/lib/krb5/ccache/file/fcc_read.c b/src/lib/krb5/ccache/file/fcc_read.c index 9a6cb2709..f03518958 100644 --- a/src/lib/krb5/ccache/file/fcc_read.c +++ b/src/lib/krb5/ccache/file/fcc_read.c @@ -18,12 +18,32 @@ static char fcc_read_c[] = "$Id$"; #include #include "fcc.h" -/* XXX Deal with kret return values */ #define CHECK(ret) if (ret != KRB5_OK) return ret; -/* XXX Doesn't deal if < sizeof(o) bytes are written XXX */ -#define krb5_fcc_read(i,b,l) (read(((krb5_fcc_data *)i->data)->fd,b,l) == -1 \ - ? errno : KRB5_OK) +/* + * Effects: + * Reads len bytes from the cache id, storing them in buf. + * + * Errors: + * KRB5_EOF - there were not len bytes available + * system errors (read) + */ +krb5_error_code +krb5_fcc_read(id, buf, len) + krb5_ccache id; + krb5_pointer buf; + int len; +{ + int ret; + + ret = read(((krb5_fcc_data *) id->data)->fd, (char *) buf, len); + if (ret == -1) + return errno; + else if (ret != len) + return KRB5_EOF; + else + return KRB5_OK; +} /* * FOR ALL OF THE FOLLOWING FUNCTIONS: @@ -86,34 +106,19 @@ krb5_fcc_read_keyblock(id, keyblock) krb5_keyblock *keyblock; { krb5_error_code kret; - char *temp; /* HACK! */ int ret; - /* - * XXX The third field should be a char *, not a char[1]. - * Alternatively, I am *VERY* confused. - */ - kret = krb5_fcc_read_keytype(id, &keyblock->keytype); CHECK(kret); kret = krb5_fcc_read_int(id, &keyblock->length); CHECK(kret); - - temp = (char *) malloc(keyblock->length*sizeof(krb5_octet)); - if (temp == NULL) - return KRB5_NOMEM; - - ret = read(((krb5_fcc_data *) id->data)->fd, temp, - (keyblock->length)*sizeof(krb5_octet)); - - /* - keyblock->contents = (char *) malloc(keyblock->length*sizeof(krb5_octet)); + keyblock->contents = (unsigned char *) malloc(keyblock->length* + sizeof(krb5_octet)); if (keyblock->contents == NULL) return KRB5_NOMEM; ret = read(((krb5_fcc_data *) id->data)->fd, keyblock->contents, (keyblock->length)*sizeof(krb5_octet)); - */ if (ret < 0) return errno; @@ -129,7 +134,7 @@ krb5_fcc_read_data(id, data) krb5_error_code kret; int ret; - kret = krb5_fcc_read_int(id, &data->length); + kret = krb5_fcc_read_int32(id, &data->length); CHECK(kret); data->data = (char *) malloc(data->length); @@ -148,7 +153,7 @@ krb5_fcc_read_int32(id, i) krb5_ccache id; krb5_int32 *i; { - return krb5_fcc_read(id, i, sizeof(krb5_int32)); + return krb5_fcc_read(id, (krb5_pointer) i, sizeof(krb5_int32)); } krb5_error_code @@ -156,7 +161,7 @@ krb5_fcc_read_keytype(id, k) krb5_ccache id; krb5_keytype *k; { - return krb5_fcc_read(id, k, sizeof(krb5_keytype)); + return krb5_fcc_read(id, (krb5_pointer) k, sizeof(krb5_keytype)); } krb5_error_code @@ -164,7 +169,7 @@ krb5_fcc_read_int(id, i) krb5_ccache id; int *i; { - return krb5_fcc_read(id, i, sizeof(int)); + return krb5_fcc_read(id, (krb5_pointer) i, sizeof(int)); } krb5_error_code @@ -172,7 +177,7 @@ krb5_fcc_read_bool(id, b) krb5_ccache id; krb5_boolean *b; { - return krb5_fcc_read(id, b, sizeof(krb5_boolean)); + return krb5_fcc_read(id, (krb5_pointer) b, sizeof(krb5_boolean)); } krb5_error_code @@ -180,7 +185,7 @@ krb5_fcc_read_times(id, t) krb5_ccache id; krb5_ticket_times *t; { - return krb5_fcc_read(id, t, sizeof(krb5_ticket_times)); + return krb5_fcc_read(id, (krb5_pointer) t, sizeof(krb5_ticket_times)); } krb5_error_code @@ -188,5 +193,5 @@ krb5_fcc_read_flags(id, f) krb5_ccache id; krb5_flags *f; { - return krb5_fcc_read(id, f, sizeof(krb5_flags)); + return krb5_fcc_read(id, (krb5_pointer) f, sizeof(krb5_flags)); } diff --git a/src/lib/krb5/ccache/file/fcc_retrv.c b/src/lib/krb5/ccache/file/fcc_retrv.c index daf18844f..d6d805336 100644 --- a/src/lib/krb5/ccache/file/fcc_retrv.c +++ b/src/lib/krb5/ccache/file/fcc_retrv.c @@ -78,7 +78,7 @@ krb5_fcc_retrieve(id, whichfields, mcreds, creds) (! set(KRB5_TC_MATCH_TIMES) || times_match(mcreds->times, creds->times))) { - krb5_fcc_end_seq_get(id, cursor); + krb5_fcc_end_seq_get(id, &cursor); return KRB5_OK; } diff --git a/src/lib/krb5/ccache/file/fcc_sseq.c b/src/lib/krb5/ccache/file/fcc_sseq.c index 5daaff57e..1fd1d0498 100644 --- a/src/lib/krb5/ccache/file/fcc_sseq.c +++ b/src/lib/krb5/ccache/file/fcc_sseq.c @@ -44,22 +44,21 @@ krb5_fcc_start_seq_get(id, cursor) return KRB5_NOMEM; /* Make sure we start reading right after the primary principal */ -#ifdef OPENCLOSE - ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); - if (ret < 0) - return errno; - ((krb5_fcc_data *) id->data)->fd = ret; -#else - lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); -#endif + if (OPENCLOSE(id)) { + ret = open(((krb5_fcc_data *) id->data)->filename, O_RDONLY, 0); + if (ret < 0) + return errno; + ((krb5_fcc_data *) id->data)->fd = ret; + } + else + lseek(((krb5_fcc_data *) id->data)->fd, 0, L_SET); krb5_fcc_skip_principal(id); fcursor->pos = tell(((krb5_fcc_data *) id->data)->fd); - *cursor = (krb5_cc_cursor *) fcursor; + *cursor = (krb5_cc_cursor) fcursor; -#ifdef OPENCLOSE - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); return KRB5_OK; } diff --git a/src/lib/krb5/ccache/file/fcc_store.c b/src/lib/krb5/ccache/file/fcc_store.c index 3f20b3260..a98a3f9ed 100644 --- a/src/lib/krb5/ccache/file/fcc_store.c +++ b/src/lib/krb5/ccache/file/fcc_store.c @@ -40,12 +40,13 @@ krb5_fcc_store(id, creds) krb5_error_code ret; /* Make sure we are writing to the end of the file */ -#ifdef OPENCLOSE - ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data)-> - filename, O_RDWR|O_APPEND, 0); - if (((krb5_fcc_data *) id->data)->fd < 0) - return errno; -#endif + if (OPENCLOSE(id)) { + ((krb5_fcc_data *) id->data)->fd = open(((krb5_fcc_data *) id->data) + ->filename, + O_RDWR | O_APPEND, 0); + if (((krb5_fcc_data *) id->data)->fd < 0) + return errno; + } ret = lseek(((krb5_fcc_data *) id->data)->fd, 0, L_XTND); if (ret < 0) @@ -61,6 +62,8 @@ krb5_fcc_store(id, creds) TCHECK(ret); ret = krb5_fcc_store_bool(id, &creds->is_skey); TCHECK(ret); + ret = krb5_fcc_store_flags(id, &creds->ticket_flags); + TCHECK(ret); ret = krb5_fcc_store_data(id, &creds->ticket); TCHECK(ret); ret = krb5_fcc_store_data(id, &creds->second_ticket); @@ -68,9 +71,8 @@ krb5_fcc_store(id, creds) lose: -#ifdef OPENCLOSE - close(((krb5_fcc_data *) id->data)->fd); -#endif + if (OPENCLOSE(id)) + close(((krb5_fcc_data *) id->data)->fd); return ret; #undef TCHECK diff --git a/src/lib/krb5/ccache/file/fcc_test.c b/src/lib/krb5/ccache/file/fcc_test.c index 7a755ab50..a8f7f5120 100644 --- a/src/lib/krb5/ccache/file/fcc_test.c +++ b/src/lib/krb5/ccache/file/fcc_test.c @@ -36,7 +36,7 @@ krb5_creds test_creds = { { 1, 1, - "1" + (unsigned char *) "1" }, { 1111, @@ -72,3 +72,42 @@ void init_test_cred() test_creds.server[1] = &server2; test_creds.server[2] = NULL; } + +#define CHECK(kret,msg) \ + if (kret != KRB5_OK) {\ + printf("%s returned %d\n", msg, kret);\ + }; + +void fcc_test() +{ + krb5_ccache id; + krb5_creds creds; + krb5_error_code kret; + krb5_cc_cursor cursor; + + init_test_cred(); + + kret = krb5_fcc_resolve(&id, "/tmp/tkt_test"); + CHECK(kret, "resolve"); + kret = krb5_fcc_initialize(id, test_creds.client); + CHECK(kret, "initialize"); + kret = krb5_fcc_store(id, &test_creds); + CHECK(kret, "store"); + + kret = krb5_fcc_start_seq_get(id, &cursor); + CHECK(kret, "start_seq_get"); + kret = 0; + while (kret != KRB5_EOF) { + printf("Calling next_cred\n"); + kret = krb5_fcc_next_cred(id, &cursor, &creds); + CHECK(kret, "next_cred"); + } + kret = krb5_fcc_end_seq_get(id, &cursor); + CHECK(kret, "end_seq_get"); + + kret = krb5_fcc_destroy(id); + CHECK(kret, "destroy"); + kret = krb5_fcc_close(id); + CHECK(kret, "close"); +} + diff --git a/src/lib/krb5/ccache/file/fcc_write.c b/src/lib/krb5/ccache/file/fcc_write.c index 741eb2275..f77cd40b9 100644 --- a/src/lib/krb5/ccache/file/fcc_write.c +++ b/src/lib/krb5/ccache/file/fcc_write.c @@ -123,7 +123,7 @@ krb5_fcc_store_int32(id, i) krb5_ccache id; krb5_int32 *i; { - return krb5_fcc_write(id, i, sizeof(krb5_int32)); + return krb5_fcc_write(id, (char *) i, sizeof(krb5_int32)); } krb5_error_code @@ -131,7 +131,7 @@ krb5_fcc_store_keytype(id, k) krb5_ccache id; krb5_keytype *k; { - return krb5_fcc_write(id, k, sizeof(krb5_keytype)); + return krb5_fcc_write(id, (char *) k, sizeof(krb5_keytype)); } krb5_error_code @@ -139,7 +139,7 @@ krb5_fcc_store_int(id, i) krb5_ccache id; int *i; { - return krb5_fcc_write(id, i, sizeof(int)); + return krb5_fcc_write(id, (char *) i, sizeof(int)); } krb5_error_code @@ -147,7 +147,7 @@ krb5_fcc_store_bool(id, b) krb5_ccache id; krb5_boolean *b; { - return krb5_fcc_write(id, b, sizeof(krb5_boolean)); + return krb5_fcc_write(id, (char *) b, sizeof(krb5_boolean)); } krb5_error_code @@ -155,8 +155,13 @@ krb5_fcc_store_times(id, t) krb5_ccache id; krb5_ticket_times *t; { - return krb5_fcc_write(id, t, sizeof(krb5_ticket_times)); + return krb5_fcc_write(id, (char *) t, sizeof(krb5_ticket_times)); } - - +krb5_error_code +krb5_fcc_store_flags(id, f) + krb5_ccache id; + krb5_flags *f; +{ + return krb5_fcc_write(id, (char *) f, sizeof(krb5_flags)); +}