From f297ed4778f7bc4829c3e49faeef8e78439020cc Mon Sep 17 00:00:00 2001 From: Danilo Almeida Date: Wed, 20 Jun 2001 23:00:36 +0000 Subject: [PATCH] * rc_dfl.c, rc_io.c: More compliance with coding standards: use ANSI function declarations, use curly braces to fix up #ifdef reindenting under Emacs, reindent code, strip trailing whitespace, wrap lines, fix commans. Not finished: fix indenting of curly braces in control structures. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@13423 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/rcache/ChangeLog | 8 + src/lib/krb5/rcache/rc_dfl.c | 411 ++++++++++++++++----------------- src/lib/krb5/rcache/rc_io.c | 412 +++++++++++++++++----------------- 3 files changed, 406 insertions(+), 425 deletions(-) diff --git a/src/lib/krb5/rcache/ChangeLog b/src/lib/krb5/rcache/ChangeLog index 622d4fe1c..409315b5d 100644 --- a/src/lib/krb5/rcache/ChangeLog +++ b/src/lib/krb5/rcache/ChangeLog @@ -1,3 +1,11 @@ +2001-06-20 Danilo Almeida + + * rc_dfl.c, rc_io.c: More compliance with coding standards: use + ANSI function declarations, use curly braces to fix up #ifdef + reindenting under Emacs, reindent code, strip trailing whitespace, + wrap lines, fix commans. Not finished: fix indenting of curly + braces in control structures. + 2001-01-23 Tom Yu * rc_io.c (getdir): Subtract one from dirlen, since sizeof diff --git a/src/lib/krb5/rcache/rc_dfl.c b/src/lib/krb5/rcache/rc_dfl.c index cc046e782..596be4086 100644 --- a/src/lib/krb5/rcache/rc_dfl.c +++ b/src/lib/krb5/rcache/rc_dfl.c @@ -17,19 +17,19 @@ #include /* -If NOIOSTUFF is defined at compile time, dfl rcaches will be per-process. -*/ + * If NOIOSTUFF is defined at compile time, dfl rcaches will be per-process. + */ /* Local stuff: -static int hash(krb5_donot_replay *rep,int hsize) +static int hash(krb5_donot_replay *rep, int hsize) returns hash value of *rep, between 0 and hsize - 1 HASHSIZE size of hash table (constant), can be preset -static int cmp(krb5_donot_replay *old,krb5_donot_replay *new,krb5_deltat t) +static int cmp(krb5_donot_replay *old, krb5_donot_replay *new, krb5_deltat t) compare old and new; return CMP_REPLAY or CMP_HOHUM -static int alive (krb5_context, krb5_donot_replay *new,krb5_deltat t) +static int alive(krb5_context, krb5_donot_replay *new, krb5_deltat t) see if new is still alive; return CMP_EXPIRED or CMP_HOHUM CMP_MALLOC, CMP_EXPIRED, CMP_REPLAY, CMP_HOHUM return codes from cmp(), alive(), and store() @@ -37,7 +37,7 @@ struct dfl_data data stored in this cache type, namely "dfl" struct authlist multilinked list of reps -static int rc_store(context, krb5_rcache id,krb5_donot_replay *rep) +static int rc_store(context, krb5_rcache id, krb5_donot_replay *rep) store rep in cache id; return CMP_REPLAY if replay, else CMP_MALLOC/CMP_HOHUM */ @@ -49,24 +49,26 @@ static int rc_store(context, krb5_rcache id,krb5_donot_replay *rep) #ifndef EXCESSREPS #define EXCESSREPS 30 #endif -/* The rcache will be automatically expunged when the number of expired -krb5_donot_replays encountered incidentally in searching exceeds the number -of live krb5_donot_replays by EXCESSREPS. With the defaults here, a typical -cache might build up some 10K of expired krb5_donot_replays before an automatic -expunge, with the waste basically independent of the number of stores per -minute. - -The rcache will also automatically be expunged when it encounters more -than EXCESSREPS expired entries when recovering a cache in -dfl_recover. */ - -static int hash(rep, hsize) -krb5_donot_replay *rep; -int hsize; + +/* + * The rcache will be automatically expunged when the number of + * expired krb5_donot_replays encountered incidentally in searching + * exceeds the number of live krb5_donot_replays by EXCESSREPS. With + * the defaults here, a typical cache might build up some 10K of + * expired krb5_donot_replays before an automatic expunge, with the + * waste basically independent of the number of stores per minute. + * + * The rcache will also automatically be expunged when it encounters + * more than EXCESSREPS expired entries when recovering a cache in + * dfl_recover. + */ + +static int +hash(krb5_donot_replay *rep, int hsize) { - return (int) ((((rep->cusec + rep->ctime + *rep->server + *rep->client) - % hsize) + hsize) % hsize); - /* We take this opportunity to once again complain about C's idiotic %. */ + return (int) ((((rep->cusec + rep->ctime + *rep->server + *rep->client) + % hsize) + hsize) % hsize); + /* We take this opportunity to once again complain about C's idiotic %. */ } #define CMP_MALLOC -3 @@ -75,124 +77,114 @@ int hsize; #define CMP_HOHUM 0 /*ARGSUSED*/ -static int cmp(old, new, t) -krb5_donot_replay *old; -krb5_donot_replay *new; -krb5_deltat t; +static int +cmp(krb5_donot_replay *old, krb5_donot_replay *new, krb5_deltat t) { - if ((old->cusec == new->cusec) && /* most likely to distinguish */ - (old->ctime == new->ctime) && - (strcmp(old->client,new->client) == 0) && - (strcmp(old->server,new->server) == 0)) /* always true */ - return CMP_REPLAY; - return CMP_HOHUM; + if ((old->cusec == new->cusec) && /* most likely to distinguish */ + (old->ctime == new->ctime) && + (strcmp(old->client, new->client) == 0) && + (strcmp(old->server, new->server) == 0)) /* always true */ + return CMP_REPLAY; + return CMP_HOHUM; } -static int alive(context, new, t) - krb5_context context; - krb5_donot_replay *new; - krb5_deltat t; +static int +alive(krb5_context context, krb5_donot_replay *new, krb5_deltat t) { - krb5_int32 mytime; - - if (krb5_timeofday(context, &mytime)) - return CMP_HOHUM; /* who cares? */ - if (new->ctime + t < mytime) /* I hope we don't have to worry about overflow */ - return CMP_EXPIRED; - return CMP_HOHUM; + krb5_int32 mytime; + + if (krb5_timeofday(context, &mytime)) + return CMP_HOHUM; /* who cares? */ + /* I hope we don't have to worry about overflow */ + if (new->ctime + t < mytime) + return CMP_EXPIRED; + return CMP_HOHUM; } struct dfl_data - { - char *name; - krb5_deltat lifespan; - int hsize; - int numhits; - int nummisses; - struct authlist **h; - struct authlist *a; +{ + char *name; + krb5_deltat lifespan; + int hsize; + int numhits; + int nummisses; + struct authlist **h; + struct authlist *a; #ifndef NOIOSTUFF - krb5_rc_iostuff d; + krb5_rc_iostuff d; #endif - char recovering; - } -; + char recovering; +}; struct authlist - { - krb5_donot_replay rep; - struct authlist *na; - struct authlist *nh; - } -; +{ + krb5_donot_replay rep; + struct authlist *na; + struct authlist *nh; +}; /* of course, list is backwards from file */ /* hash could be forwards since we have to search on match, but naaaah */ -static int rc_store(context, id, rep) - krb5_context context; - krb5_rcache id; - krb5_donot_replay *rep; +static int +rc_store(krb5_context context, krb5_rcache id, krb5_donot_replay *rep) { - struct dfl_data *t = (struct dfl_data *)id->data; - int rephash; - struct authlist *ta; - - rephash = hash(rep,t->hsize); + struct dfl_data *t = (struct dfl_data *)id->data; + int rephash; + struct authlist *ta; + + rephash = hash(rep, t->hsize); + + for (ta = t->h[rephash]; ta; ta = ta->nh) { + switch(cmp(&ta->rep, rep, t->lifespan)) + { + case CMP_REPLAY: + return CMP_REPLAY; + case CMP_HOHUM: + if (alive(context, &ta->rep, t->lifespan) == CMP_EXPIRED) + t->nummisses++; + else + t->numhits++; + break; + default: + ; /* wtf? */ + } + } - for (ta = t->h[rephash];ta;ta = ta->nh) - switch(cmp(&ta->rep,rep,t->lifespan)) - { - case CMP_REPLAY: return CMP_REPLAY; - case CMP_HOHUM: if (alive(context, &ta->rep,t->lifespan) == CMP_EXPIRED) - t->nummisses++; - else - t->numhits++; - break; - default: ; /* wtf? */ + if (!(ta = (struct authlist *) malloc(sizeof(struct authlist)))) + return CMP_MALLOC; + ta->na = t->a; t->a = ta; + ta->nh = t->h[rephash]; t->h[rephash] = ta; + ta->rep = *rep; + if (!(ta->rep.client = strdup(rep->client))) { + FREE(ta); + return CMP_MALLOC; + } + if (!(ta->rep.server = strdup(rep->server))) { + FREE(ta->rep.client); + FREE(ta); + return CMP_MALLOC; } - if (!(ta = (struct authlist *) malloc(sizeof(struct authlist)))) - return CMP_MALLOC; - ta->na = t->a; t->a = ta; - ta->nh = t->h[rephash]; t->h[rephash] = ta; - ta->rep = *rep; - if (!(ta->rep.client = strdup(rep->client))) { - FREE(ta); - return CMP_MALLOC; - } - if (!(ta->rep.server = strdup(rep->server))) { - FREE(ta->rep.client); - FREE(ta); - return CMP_MALLOC; - } - - return CMP_HOHUM; + return CMP_HOHUM; } char * KRB5_CALLCONV -krb5_rc_dfl_get_name(context, id) - krb5_context context; - krb5_rcache id; +krb5_rc_dfl_get_name(krb5_context context, krb5_rcache id) { - return ((struct dfl_data *) (id->data))->name; + return ((struct dfl_data *) (id->data))->name; } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_get_span(context, id, lifespan) - krb5_context context; - krb5_rcache id; - krb5_deltat *lifespan; +krb5_rc_dfl_get_span(krb5_context context, krb5_rcache id, + krb5_deltat *lifespan) { - *lifespan = ((struct dfl_data *) (id->data))->lifespan; - return 0; + *lifespan = ((struct dfl_data *) (id->data))->lifespan; + return 0; } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_init(context, id, lifespan) - krb5_context context; -krb5_rcache id; -krb5_deltat lifespan; +krb5_rc_dfl_init(krb5_context context, krb5_rcache id, krb5_deltat lifespan) { struct dfl_data *t = (struct dfl_data *)id->data; krb5_error_code retval; @@ -200,7 +192,7 @@ krb5_deltat lifespan; t->lifespan = lifespan ? lifespan : context->clockskew; /* default to clockskew from the context */ #ifndef NOIOSTUFF - if ((retval = krb5_rc_io_creat(context, &t->d,&t->name))) + if ((retval = krb5_rc_io_creat(context, &t->d, &t->name))) return retval; if ((krb5_rc_io_write(context, &t->d, (krb5_pointer) &t->lifespan, sizeof(t->lifespan)) @@ -210,35 +202,32 @@ krb5_deltat lifespan; return 0; } -krb5_error_code krb5_rc_dfl_close_no_free(context, id) - krb5_context context; - krb5_rcache id; +krb5_error_code +krb5_rc_dfl_close_no_free(krb5_context context, krb5_rcache id) { - struct dfl_data *t = (struct dfl_data *)id->data; - struct authlist *q; - - FREE(t->h); - if (t->name) - FREE(t->name); - while ((q = t->a)) - { - t->a = q->na; - FREE(q->rep.client); - FREE(q->rep.server); - FREE(q); - } + struct dfl_data *t = (struct dfl_data *)id->data; + struct authlist *q; + + FREE(t->h); + if (t->name) + FREE(t->name); + while ((q = t->a)) + { + t->a = q->na; + FREE(q->rep.client); + FREE(q->rep.server); + FREE(q); + } #ifndef NOIOSTUFF - if (t->d.fd >= 0) - (void) krb5_rc_io_close(context, &t->d); + if (t->d.fd >= 0) + (void) krb5_rc_io_close(context, &t->d); #endif - FREE(t); - return 0; + FREE(t); + return 0; } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_close(context, id) - krb5_context context; - krb5_rcache id; +krb5_rc_dfl_close(krb5_context context, krb5_rcache id) { krb5_rc_dfl_close_no_free(context, id); free(id); @@ -246,22 +235,17 @@ krb5_rc_dfl_close(context, id) } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_destroy(context, id) - krb5_context context; -krb5_rcache id; +krb5_rc_dfl_destroy(krb5_context context, krb5_rcache id) { #ifndef NOIOSTUFF - if (krb5_rc_io_destroy(context, &((struct dfl_data *) (id->data))->d)) - return KRB5_RC_IO; + if (krb5_rc_io_destroy(context, &((struct dfl_data *) (id->data))->d)) + return KRB5_RC_IO; #endif - return krb5_rc_dfl_close(context, id); + return krb5_rc_dfl_close(context, id); } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_resolve(context, id, name) - krb5_context context; - krb5_rcache id; - char *name; +krb5_rc_dfl_resolve(krb5_context context, krb5_rcache id, char *name) { struct dfl_data *t = 0; krb5_error_code retval; @@ -294,7 +278,7 @@ krb5_rc_dfl_resolve(context, id, name) #endif t->recovering = 0; return 0; - + cleanup: if (t) { if (t->name) @@ -306,14 +290,13 @@ cleanup: return retval; } -void krb5_rc_free_entry (context, rep) - krb5_context context; - krb5_donot_replay **rep; +void +krb5_rc_free_entry(krb5_context context, krb5_donot_replay **rep) { krb5_donot_replay *rp = *rep; - + *rep = NULL; - if (rp) + if (rp) { if (rp->client) free(rp->client); @@ -326,36 +309,34 @@ void krb5_rc_free_entry (context, rep) } } -static krb5_error_code krb5_rc_io_fetch(context, t, rep, maxlen) - krb5_context context; - struct dfl_data *t; - krb5_donot_replay *rep; - int maxlen; +static krb5_error_code +krb5_rc_io_fetch(krb5_context context, struct dfl_data *t, + krb5_donot_replay *rep, int maxlen) { int len; krb5_error_code retval; rep->client = rep->server = 0; - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) &len, sizeof(len)); - if (retval) + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) &len, sizeof(len)); + if (retval) return retval; - + if ((len <= 0) || (len >= maxlen)) return KRB5_RC_IO_EOF; rep->client = malloc (len); if (!rep->client) return KRB5_RC_MALLOC; - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) rep->client, len); + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) rep->client, len); if (retval) goto errout; - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) &len, sizeof(len)); + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) &len, sizeof(len)); if (retval) goto errout; - + if ((len <= 0) || (len >= maxlen)) { retval = KRB5_RC_IO_EOF; goto errout; @@ -366,21 +347,23 @@ static krb5_error_code krb5_rc_io_fetch(context, t, rep, maxlen) retval = KRB5_RC_MALLOC; goto errout; } - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) rep->server, len); + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) rep->server, len); if (retval) goto errout; - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) &rep->cusec, sizeof(rep->cusec)); + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) &rep->cusec, + sizeof(rep->cusec)); if (retval) goto errout; - - retval = krb5_rc_io_read (context, &t->d, (krb5_pointer) &rep->ctime, sizeof(rep->ctime)); + + retval = krb5_rc_io_read(context, &t->d, (krb5_pointer) &rep->ctime, + sizeof(rep->ctime)); if (retval) goto errout; return 0; - + errout: if (rep->client) krb5_xfree(rep->client); @@ -389,13 +372,11 @@ errout: rep->client = rep->server = 0; return retval; } - + krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_recover(context, id) - krb5_context context; -krb5_rcache id; +krb5_rc_dfl_recover(krb5_context context, krb5_rcache id) { #ifdef NOIOSTUFF return KRB5_RC_NOIO; @@ -411,11 +392,12 @@ krb5_rcache id; return retval; t->recovering = 1; - + max_size = krb5_rc_io_size(context, &t->d); - + rep = NULL; - if (krb5_rc_io_read(context, &t->d,(krb5_pointer) &t->lifespan,sizeof(t->lifespan))) { + if (krb5_rc_io_read(context, &t->d, (krb5_pointer) &t->lifespan, + sizeof(t->lifespan))) { retval = KRB5_RC_IO; goto io_fail; } @@ -433,19 +415,19 @@ krb5_rcache id; retval = KRB5_RC_IO; goto io_fail; } - - retval = krb5_rc_io_fetch (context, t, rep, (int) max_size); + + retval = krb5_rc_io_fetch(context, t, rep, (int) max_size); if (retval == KRB5_RC_IO_EOF) break; else if (retval != 0) goto io_fail; - - if (alive(context, rep,t->lifespan) != CMP_EXPIRED) { + + if (alive(context, rep, t->lifespan) != CMP_EXPIRED) { if (rc_store(context, id, rep) == CMP_MALLOC) { retval = KRB5_RC_MALLOC; goto io_fail; - } + } } else { expired_entries++; } @@ -471,25 +453,23 @@ io_fail: retval = krb5_rc_dfl_expunge(context, id); t->recovering = 0; return retval; - + #endif } static krb5_error_code -krb5_rc_io_store (context, t, rep) - krb5_context context; - struct dfl_data *t; - krb5_donot_replay *rep; +krb5_rc_io_store(krb5_context context, struct dfl_data *t, + krb5_donot_replay *rep) { unsigned int clientlen, serverlen, len; char *buf, *ptr; krb5_error_code ret; - clientlen = strlen (rep->client) + 1; - serverlen = strlen (rep->server) + 1; + clientlen = strlen(rep->client) + 1; + serverlen = strlen(rep->server) + 1; len = sizeof(clientlen) + clientlen + sizeof(serverlen) + serverlen + sizeof(rep->cusec) + sizeof(rep->ctime); - buf = malloc (len); + buf = malloc(len); if (buf == 0) return KRB5_RC_MALLOC; ptr = buf; @@ -506,31 +486,28 @@ krb5_rc_io_store (context, t, rep) } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_store(context, id, rep) - krb5_context context; -krb5_rcache id; -krb5_donot_replay *rep; +krb5_rc_dfl_store(krb5_context context, krb5_rcache id, krb5_donot_replay *rep) { krb5_error_code ret; struct dfl_data *t = (struct dfl_data *)id->data; - switch(rc_store(context, id,rep)) { + switch(rc_store(context, id, rep)) { case CMP_MALLOC: - return KRB5_RC_MALLOC; + return KRB5_RC_MALLOC; case CMP_REPLAY: - return KRB5KRB_AP_ERR_REPEAT; + return KRB5KRB_AP_ERR_REPEAT; case 0: break; default: /* wtf? */ ; } #ifndef NOIOSTUFF - ret = krb5_rc_io_store (context, t, rep); + ret = krb5_rc_io_store(context, t, rep); if (ret) return ret; #endif - /* Shall we automatically expunge? */ - if (t->nummisses > t->numhits + EXCESSREPS) + /* Shall we automatically expunge? */ + if (t->nummisses > t->numhits + EXCESSREPS) { - return krb5_rc_dfl_expunge(context, id); + return krb5_rc_dfl_expunge(context, id); } #ifndef NOIOSTUFF else @@ -539,13 +516,11 @@ krb5_donot_replay *rep; return KRB5_RC_IO; } #endif - return 0; + return 0; } krb5_error_code KRB5_CALLCONV -krb5_rc_dfl_expunge(context, id) - krb5_context context; -krb5_rcache id; +krb5_rc_dfl_expunge(krb5_context context, krb5_rcache id) { struct dfl_data *t = (struct dfl_data *)id->data; #ifdef NOIOSTUFF @@ -555,24 +530,24 @@ krb5_rcache id; struct authlist *r; struct authlist *rt; - for (q = &t->a;*q;q = qt) { + for (q = &t->a; *q; q = qt) { qt = &(*q)->na; - if (alive(context, &(*q)->rep,t->lifespan) == CMP_EXPIRED) { + if (alive(context, &(*q)->rep, t->lifespan) == CMP_EXPIRED) { FREE((*q)->rep.client); FREE((*q)->rep.server); FREE(*q); *q = *qt; /* why doesn't this feel right? */ } } - for (i = 0;i < t->hsize;i++) + for (i = 0; i < t->hsize; i++) t->h[i] = (struct authlist *) 0; - for (r = t->a;r;r = r->na) { - i = hash(&r->rep,t->hsize); + for (r = t->a; r; r = r->na) { + i = hash(&r->rep, t->hsize); rt = t->h[i]; t->h[i] = r; r->nh = rt; } - + #else struct authlist *q; char *name; @@ -583,8 +558,8 @@ krb5_rcache id; if (! t->recovering) { name = t->name; t->name = 0; /* Clear name so it isn't freed */ - (void) krb5_rc_dfl_close_no_free(context, id); - retval = krb5_rc_dfl_resolve(context, id, name); + (void) krb5_rc_dfl_close_no_free(context, id); + retval = krb5_rc_dfl_resolve(context, id, name); free(name); if (retval) return retval; @@ -606,15 +581,15 @@ krb5_rcache id; retval = krb5_rc_initialize(context, tmp, lifespan); if (retval) return retval; - for (q = t->a;q;q = q->na) { - if (krb5_rc_io_store (context, (struct dfl_data *)tmp->data, &q->rep)) + for (q = t->a; q; q = q->na) { + if (krb5_rc_io_store(context, (struct dfl_data *)tmp->data, &q->rep)) return KRB5_RC_IO; } if (krb5_rc_io_sync(context, &t->d)) return KRB5_RC_IO; if (krb5_rc_io_move(context, &t->d, &((struct dfl_data *)tmp->data)->d)) return KRB5_RC_IO; - (void) krb5_rc_dfl_close(context, tmp); + (void) krb5_rc_dfl_close(context, tmp); #endif return 0; } diff --git a/src/lib/krb5/rcache/rc_io.c b/src/lib/krb5/rcache/rc_io.c index 8c7853eeb..e9d566b34 100644 --- a/src/lib/krb5/rcache/rc_io.c +++ b/src/lib/krb5/rcache/rc_io.c @@ -19,7 +19,7 @@ #define KRB5_RC_VNO 0x0501 /* krb5, rcache v 1 */ #define NEED_SOCKETS -#define NEED_LOWLEVEL_IO +#define NEED_LOWLEVEL_IO #include #include /* for P_tmpdir */ @@ -36,7 +36,7 @@ #include #endif #else - #error find some way to use net-byte-order file version numbers. +#error find some way to use net-byte-order file version numbers. #endif #ifndef HAVE_ERRNO @@ -55,195 +55,202 @@ static char *dir; #define GETDIR do { if (!dirlen) getdir(); } while(0) -static void getdir() +static void +getdir() { - if (!(dir = getenv("KRB5RCACHEDIR"))) + if (!(dir = getenv("KRB5RCACHEDIR"))) { #if defined(_MSDOS) || 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 - dirlen = strlen(dir) + sizeof(PATH_SEPARATOR) - 1; + } + dirlen = strlen(dir) + sizeof(PATH_SEPARATOR) - 1; } -krb5_error_code krb5_rc_io_creat (context, d, fn) - krb5_context context; - krb5_rc_iostuff *d; - char **fn; +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; - - GETDIR; - if (fn && *fn) - { - if (!(d->fn = malloc(strlen(*fn) + dirlen + 1))) - return KRB5_RC_IO_MALLOC; - (void) strcpy(d->fn,dir); - (void) strcat(d->fn,PATH_SEPARATOR); - (void) strcat(d->fn,*fn); - d->fd = THREEPARAMOPEN(d->fn,O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | O_BINARY,0600); - } - else - { - /* %d is max 11 digits (-, 10 digits of 32-bit number) - * 11 + /krb5_RC + aaa = 24, +6 for slop */ - if (!(d->fn = malloc(30 + dirlen))) - return KRB5_RC_IO_MALLOC; - if (fn) - if (!(*fn = malloc(35))) - { FREE(d->fn); return KRB5_RC_IO_MALLOC; } - (void) sprintf(d->fn,"%s%skrb5_RC%d",dir,PATH_SEPARATOR, (int) UNIQUE); - c = d->fn + strlen(d->fn); - (void) strcpy(c,"aaa"); - while ((d->fd = THREEPARAMOPEN(d->fn,O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY,0600)) == -1) + char *c; + krb5_int16 rc_vno = htons(KRB5_RC_VNO); + krb5_error_code retval; + + GETDIR; + if (fn && *fn) { - if ((c[2]++) == 'z') - { - c[2] = 'a'; - if ((c[1]++) == 'z') - { - c[1] = 'a'; - if ((c[0]++) == 'z') - break; /* sigh */ - } - } + if (!(d->fn = malloc(strlen(*fn) + dirlen + 1))) + return KRB5_RC_IO_MALLOC; + (void) strcpy(d->fn, dir); + (void) strcat(d->fn, PATH_SEPARATOR); + (void) strcat(d->fn, *fn); + d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | + O_BINARY, 0600); } - if (fn) - (void) strcpy(*fn,d->fn + dirlen); - } - if (d->fd == -1) + else { - switch(errno) + /* %d is max 11 digits (-, 10 digits of 32-bit number) + * 11 + /krb5_RC + aaa = 24, +6 for slop */ + if (!(d->fn = malloc(30 + dirlen))) + return KRB5_RC_IO_MALLOC; + if (fn) + if (!(*fn = malloc(35))) { + FREE(d->fn); + return KRB5_RC_IO_MALLOC; + } + (void) sprintf(d->fn, "%s%skrb5_RC%d", dir, PATH_SEPARATOR, + (int) UNIQUE); + c = d->fn + strlen(d->fn); + (void) strcpy(c, "aaa"); + 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 */ + } + } + } + if (fn) + (void) strcpy(*fn, d->fn + dirlen); + } + if (d->fd == -1) { - case EFBIG: + switch(errno) + { + case EFBIG: #ifdef EDQUOT case EDQUOT: #endif - case ENOSPC: - retval = KRB5_RC_IO_SPACE; + case ENOSPC: + retval = KRB5_RC_IO_SPACE; goto fail; - case EIO: - retval = KRB5_RC_IO_IO; goto fail; - case EPERM: - case EACCES: - case EROFS: - case EEXIST: - retval = KRB5_RC_IO_PERM; goto no_unlink; + case EIO: + retval = KRB5_RC_IO_IO; + goto fail; - default: - retval = KRB5_RC_IO_UNKNOWN; goto fail; - } + case EPERM: + case EACCES: + case EROFS: + case EEXIST: + retval = KRB5_RC_IO_PERM; + goto no_unlink; + + default: + retval = KRB5_RC_IO_UNKNOWN; + goto fail; + } } - if ((retval = krb5_rc_io_write(context, d, (krb5_pointer)&rc_vno, sizeof(rc_vno))) || + if ((retval = krb5_rc_io_write(context, d, (krb5_pointer)&rc_vno, + sizeof(rc_vno))) || (retval = krb5_rc_io_sync(context, d))) { fail: - (void) unlink(d->fn); + (void) unlink(d->fn); no_unlink: - FREE(d->fn); + FREE(d->fn); d->fn = NULL; - (void) close(d->fd); - return retval; - } - return 0; + (void) close(d->fd); + return retval; + } + return 0; } -krb5_error_code krb5_rc_io_open (context, d, fn) - krb5_context context; - krb5_rc_iostuff *d; - char *fn; +krb5_error_code +krb5_rc_io_open(krb5_context context, krb5_rc_iostuff *d, char *fn) { - krb5_int16 rc_vno; - krb5_error_code retval; + krb5_int16 rc_vno; + krb5_error_code retval; #ifndef NO_USERID - struct stat statb; + struct stat statb; #endif - GETDIR; - if (!(d->fn = malloc(strlen(fn) + dirlen + 1))) - return KRB5_RC_IO_MALLOC; - (void) strcpy(d->fn,dir); - (void) strcat(d->fn,PATH_SEPARATOR); - (void) strcat(d->fn,fn); + GETDIR; + if (!(d->fn = malloc(strlen(fn) + dirlen + 1))) + return KRB5_RC_IO_MALLOC; + (void) strcpy(d->fn, dir); + (void) strcat(d->fn, PATH_SEPARATOR); + (void) strcat(d->fn, fn); #ifdef NO_USERID - d->fd = THREEPARAMOPEN(d->fn,O_RDWR | O_BINARY,0600); + d->fd = THREEPARAMOPEN(d->fn, O_RDWR | O_BINARY, 0600); #else - if ((d->fd = stat(d->fn, &statb)) != -1) { - uid_t me; + 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 + 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); - } + 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) { - switch(errno) - { - case EFBIG: + if (d->fd == -1) { + switch(errno) + { + case EFBIG: #ifdef EDQUOT case EDQUOT: #endif case ENOSPC: - retval = KRB5_RC_IO_SPACE; + retval = KRB5_RC_IO_SPACE; goto fail; - case EIO: - retval = KRB5_RC_IO_IO; + case EIO: + retval = KRB5_RC_IO_IO; goto fail; - case EPERM: + case EPERM: case EACCES: - case EROFS: - retval = KRB5_RC_IO_PERM; + case EROFS: + retval = KRB5_RC_IO_PERM; goto fail; - default: - retval = KRB5_RC_IO_UNKNOWN; + default: + retval = KRB5_RC_IO_UNKNOWN; goto fail; + } } - } if ((retval = krb5_rc_io_read(context, d, (krb5_pointer) &rc_vno, sizeof(rc_vno)))) goto unlk; - if (ntohs(rc_vno) != KRB5_RC_VNO) + if (ntohs(rc_vno) != KRB5_RC_VNO) { retval = KRB5_RCACHE_BADVNO; unlk: unlink(d->fn); fail: - (void) close(d->fd); - FREE(d->fn); + (void) close(d->fd); + FREE(d->fn); d->fn = NULL; - return retval; - } - return 0; + return retval; + } + return 0; } -krb5_error_code krb5_rc_io_move (context, new, old) - krb5_context context; - krb5_rc_iostuff *new; - krb5_rc_iostuff *old; +krb5_error_code +krb5_rc_io_move(krb5_context context, krb5_rc_iostuff *new, + krb5_rc_iostuff *old) { char *fn = NULL; - #if defined(_MSDOS) || defined(_WIN32) /* * Work around provided by Tom Sanfilippo to work around poor @@ -254,7 +261,7 @@ krb5_error_code krb5_rc_io_move (context, new, old) close(new->fd); unlink(new->fn); close(old->fd); - if (rename(old->fn,new->fn) == -1) /* MUST be atomic! */ + if (rename(old->fn, new->fn) == -1) /* MUST be atomic! */ return KRB5_RC_IO_UNKNOWN; fn = new->fn; new->fn = NULL; /* avoid clobbering */ @@ -262,7 +269,7 @@ krb5_error_code krb5_rc_io_move (context, new, old) krb5_rc_io_open(context, new, fn); free(fn); #else - if (rename(old->fn,new->fn) == -1) /* MUST be atomic! */ + if (rename(old->fn, new->fn) == -1) /* MUST be atomic! */ return KRB5_RC_IO_UNKNOWN; fn = new->fn; new->fn = NULL; /* avoid clobbering */ @@ -277,118 +284,109 @@ krb5_error_code krb5_rc_io_move (context, new, old) return 0; } -krb5_error_code krb5_rc_io_write (context, d, buf, num) - krb5_context context; - krb5_rc_iostuff *d; - krb5_pointer buf; - unsigned int num; +krb5_error_code +krb5_rc_io_write(krb5_context context, krb5_rc_iostuff *d, krb5_pointer buf, + unsigned int num) { - if (write(d->fd,(char *) buf,num) == -1) - switch(errno) - { - case EBADF: return KRB5_RC_IO_UNKNOWN; - case EFBIG: return KRB5_RC_IO_SPACE; + if (write(d->fd, (char *) buf, num) == -1) + switch(errno) + { + case EBADF: return KRB5_RC_IO_UNKNOWN; + case EFBIG: return KRB5_RC_IO_SPACE; #ifdef EDQUOT - case EDQUOT: return KRB5_RC_IO_SPACE; + case EDQUOT: return KRB5_RC_IO_SPACE; #endif - case ENOSPC: return KRB5_RC_IO_SPACE; - case EIO: return KRB5_RC_IO_IO; - default: return KRB5_RC_IO_UNKNOWN; - } - return 0; + case ENOSPC: return KRB5_RC_IO_SPACE; + case EIO: return KRB5_RC_IO_IO; + default: return KRB5_RC_IO_UNKNOWN; + } + return 0; } -krb5_error_code krb5_rc_io_sync (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_error_code +krb5_rc_io_sync( + krb5_context context, + krb5_rc_iostuff *d + ) { #if !defined(MSDOS_FILESYSTEM) && !defined(macintosh) if (fsync(d->fd) == -1) { - switch(errno) - { - case EBADF: return KRB5_RC_IO_UNKNOWN; - case EIO: return KRB5_RC_IO_IO; - default: return KRB5_RC_IO_UNKNOWN; - } + switch(errno) + { + case EBADF: return KRB5_RC_IO_UNKNOWN; + case EIO: return KRB5_RC_IO_IO; + default: return KRB5_RC_IO_UNKNOWN; + } } #endif return 0; } -krb5_error_code krb5_rc_io_read (context, d, buf, num) - krb5_context context; - krb5_rc_iostuff *d; - krb5_pointer buf; - unsigned int num; +krb5_error_code +krb5_rc_io_read(krb5_context context, krb5_rc_iostuff *d, krb5_pointer buf, + unsigned int num) { - int count; - if ((count = read(d->fd,(char *) buf,num)) == -1) - switch(errno) - { - case EBADF: return KRB5_RC_IO_UNKNOWN; - case EIO: return KRB5_RC_IO_IO; - default: return KRB5_RC_IO_UNKNOWN; - } - if (count == 0) - return KRB5_RC_IO_EOF; - return 0; + int count; + if ((count = read(d->fd, (char *) buf, num)) == -1) + switch(errno) + { + case EBADF: return KRB5_RC_IO_UNKNOWN; + case EIO: return KRB5_RC_IO_IO; + default: return KRB5_RC_IO_UNKNOWN; + } + if (count == 0) + return KRB5_RC_IO_EOF; + return 0; } -krb5_error_code krb5_rc_io_close (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_error_code +krb5_rc_io_close(krb5_context context, krb5_rc_iostuff *d) { - if (d->fn != NULL) - FREE(d->fn); - d->fn = NULL; - if (close(d->fd) == -1) /* can't happen */ - return KRB5_RC_IO_UNKNOWN; - return 0; + if (d->fn != NULL) + FREE(d->fn); + d->fn = NULL; + if (close(d->fd) == -1) /* can't happen */ + return KRB5_RC_IO_UNKNOWN; + return 0; } -krb5_error_code krb5_rc_io_destroy (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_error_code +krb5_rc_io_destroy(krb5_context context, krb5_rc_iostuff *d) { - if (unlink(d->fn) == -1) - switch(errno) - { - case EBADF: return KRB5_RC_IO_UNKNOWN; - case EIO: return KRB5_RC_IO_IO; - case EPERM: return KRB5_RC_IO_PERM; - case EBUSY: return KRB5_RC_IO_PERM; - case EROFS: return KRB5_RC_IO_PERM; - default: return KRB5_RC_IO_UNKNOWN; - } - return 0; + if (unlink(d->fn) == -1) + switch(errno) + { + case EBADF: return KRB5_RC_IO_UNKNOWN; + case EIO: return KRB5_RC_IO_IO; + case EPERM: return KRB5_RC_IO_PERM; + case EBUSY: return KRB5_RC_IO_PERM; + case EROFS: return KRB5_RC_IO_PERM; + default: return KRB5_RC_IO_UNKNOWN; + } + return 0; } -krb5_error_code krb5_rc_io_mark (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_error_code +krb5_rc_io_mark(krb5_context context, krb5_rc_iostuff *d) { - d->mark = lseek(d->fd,(off_t) 0,SEEK_CUR); /* can't fail */ - return 0; + d->mark = lseek(d->fd, (off_t) 0, SEEK_CUR); /* can't fail */ + return 0; } -krb5_error_code krb5_rc_io_unmark (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_error_code +krb5_rc_io_unmark(krb5_context context, krb5_rc_iostuff *d) { - (void) lseek(d->fd,d->mark,SEEK_SET); /* if it fails, tough luck */ - return 0; + (void) lseek(d->fd, d->mark, SEEK_SET); /* if it fails, tough luck */ + return 0; } long -krb5_rc_io_size (context, d) - krb5_context context; - krb5_rc_iostuff *d; +krb5_rc_io_size(krb5_context context, krb5_rc_iostuff *d) { struct stat statb; - - if (fstat (d->fd, &statb) == 0) + + if (fstat(d->fd, &statb) == 0) return statb.st_size; else return 0; } - -- 2.26.2