From ec338af87bc3cc5310b86b6aa6a0381b25414599 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 17 Apr 2011 04:06:33 +0000 Subject: [PATCH] cwfs: code cleanup --- sys/src/cmd/cwfs/9p2.c | 37 +++----- sys/src/cmd/cwfs/auth.c | 171 ++++++++++--------------------------- sys/src/cmd/cwfs/main.c | 2 - sys/src/cmd/cwfs/net.c | 30 +++---- sys/src/cmd/cwfs/portdat.h | 3 +- sys/src/cmd/cwfs/portfns.h | 8 +- 6 files changed, 71 insertions(+), 180 deletions(-) diff --git a/sys/src/cmd/cwfs/9p2.c b/sys/src/cmd/cwfs/9p2.c index 947db5fa6..ec89a5263 100644 --- a/sys/src/cmd/cwfs/9p2.c +++ b/sys/src/cmd/cwfs/9p2.c @@ -152,14 +152,14 @@ version(Chan* chan, Fcall* f, Fcall* r) return 0; } -struct { - Lock; - ulong hi; -} authpath; static int auth(Chan* chan, Fcall* f, Fcall* r) { + static struct { + Lock; + ulong hi; + } authpath; char *aname; File *file; Filsys *fs; @@ -195,13 +195,12 @@ auth(Chan* chan, Fcall* f, Fcall* r) file->open = FREAD+FWRITE; freewp(file->wpath); file->wpath = 0; - file->auth = authnew(f->uname, f->aname); - if(file->auth == nil){ + file->uid = -1; + if((file->auth = authnew()) == nil){ error = Eauthfile; goto out; } r->aqid = file->qid; - out: if((cons.flags & attachflag) && error) print("9p2: auth %s %T SUCK EGGS --- %s\n", @@ -218,7 +217,7 @@ static int authorize(Chan* chan, Fcall* f) { File* af; - int db, uid = -1; + int db, uid; db = cons.flags & authdebugflag; @@ -247,26 +246,9 @@ authorize(Chan* chan, Fcall* f) /* fake read to get auth info */ authread(af, nil, 0); - - if(af->auth == nil){ - if(db) - print("authorize: af->auth == nil\n"); - goto out; - } - if(strcmp(f->uname, authuname(af->auth)) != 0){ - if(db) - print("authorize: strcmp(f->uname, authuname(af->auth)) != 0\n"); - goto out; - } - if(strcmp(f->aname, authaname(af->auth)) != 0){ - if(db) - print("authorize: strcmp(f->aname, authaname(af->auth)) != 0\n"); - goto out; - } - uid = authuid(af->auth); + uid = af->uid; if(db) print("authorize: uid is %d\n", uid); -out: qunlock(af); return uid; } @@ -1321,6 +1303,7 @@ _clunk(File* file, int remove, int wok) file->open = 0; freewp(file->wpath); authfree(file->auth); + file->auth = 0; freefp(file); qunlock(file); @@ -1368,7 +1351,7 @@ fs_stat(Chan* chan, Fcall* f, Fcall* r, uchar* data) d = &dentry; mkqid9p1(&d->qid, &file->qid); strcpy(d->name, "#¿"); - d->uid = authuid(file->auth); + d->uid = file->uid; d->gid = d->uid; d->muid = d->uid; d->atime = time(nil); diff --git a/sys/src/cmd/cwfs/auth.c b/sys/src/cmd/cwfs/auth.c index a7726a4a7..f55717c8e 100644 --- a/sys/src/cmd/cwfs/auth.c +++ b/sys/src/cmd/cwfs/auth.c @@ -16,7 +16,6 @@ nvrgetconfig(void) /* * we shouldn't be writing nvram any more. * the secstore/config field is now just secstore key. - * we still use authid, authdom and machkey for authentication. */ int @@ -84,161 +83,77 @@ conslock(void) return 1; } -/* authentication structure */ -struct Auth -{ - int inuse; - char uname[NAMELEN]; /* requestor's remote user name */ - char aname[NAMELEN]; /* requested aname */ - Userid uid; /* uid decided on */ - AuthRpc *rpc; -}; +static char *keyspec = "proto=p9any role=server"; -Auth* auths; -Lock authlock; - -void -authinit(void) -{ - auths = malloc(conf.nauth * sizeof(*auths)); -} - -static int -failure(Auth *s, char *why) +void* +authnew(void) { AuthRpc *rpc; + int fd; - if(why && *why)print("authentication failed: %s: %r\n", why); - s->uid = -1; - if(rpc = s->rpc){ - s->rpc = 0; + if(access("/mnt/factotum", 0) < 0) + if((fd = open("/srv/factotum", ORDWR)) >= 0) + mount(fd, -1, "/mnt", MBEFORE, ""); + if((fd = open("/mnt/factotum/rpc", ORDWR)) < 0) + return nil; + if((rpc = auth_allocrpc(fd)) == nil){ + close(fd); + return nil; + } + if(auth_rpc(rpc, "start", keyspec, strlen(keyspec)) != ARok){ auth_freerpc(rpc); + return nil; } - return -1; -} - -Auth* -authnew(char *uname, char *aname) -{ - static int si = 0; - int afd, i, nwrap; - Auth *s; - - i = si; - nwrap = 0; - for(;;){ - if(i < 0 || i >= conf.nauth){ - if(++nwrap > 1) - return nil; - i = 0; - } - s = &auths[i++]; - if(s->inuse) - continue; - lock(&authlock); - if(s->inuse == 0){ - s->inuse = 1; - strncpy(s->uname, uname, NAMELEN-1); - strncpy(s->aname, aname, NAMELEN-1); - failure(s, ""); - si = i; - unlock(&authlock); - break; - } - unlock(&authlock); - } - if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0){ - failure(s, "open /mnt/factotum/rpc"); - return s; - } - if((s->rpc = auth_allocrpc(afd)) == 0){ - failure(s, "auth_allocrpc"); - close(afd); - return s; - } - if(auth_rpc(s->rpc, "start", "proto=p9any role=server", 23) != ARok) - failure(s, "auth_rpc: start"); - return s; + return rpc; } void -authfree(Auth *s) +authfree(void *auth) { - if(s){ - failure(s, ""); - s->inuse = 0; - } + AuthRpc *rpc; + + if(rpc = auth) + auth_freerpc(rpc); } int -authread(File* file, uchar* data, int n) +authread(File *file, uchar *data, int count) { AuthInfo *ai; - Auth *s; + AuthRpc *rpc; - s = file->auth; - if(s == nil) + if((rpc = file->auth) == nil) return -1; - if(s->rpc == nil) - return -1; - switch(auth_rpc(s->rpc, "read", nil, 0)){ - default: - failure(s, "auth_rpc: read"); - break; + switch(auth_rpc(rpc, "read", nil, 0)){ case ARdone: - if((ai = auth_getinfo(s->rpc)) == nil){ - failure(s, "auth_getinfo failed"); - break; - } - if(ai->cuid == nil || *ai->cuid == '\0'){ - failure(s, "auth with no cuid"); - auth_freeAI(ai); - break; - } - failure(s, ""); - s->uid = strtouid(ai->cuid); + if((ai = auth_getinfo(rpc)) == nil) + return -1; + file->uid = strtouid(ai->cuid); auth_freeAI(ai); + if(file->uid < 0) + return -1; return 0; case ARok: - if(n < s->rpc->narg) - break; - memmove(data, s->rpc->arg, s->rpc->narg); - return s->rpc->narg; + if(count < rpc->narg) + return -1; + memmove(data, rpc->arg, rpc->narg); + return rpc->narg; + case ARphase: + return -1; + default: + return -1; } - return -1; } int -authwrite(File* file, uchar *data, int n) +authwrite(File *file, uchar *data, int count) { - Auth *s; + AuthRpc *rpc; - s = file->auth; - if(s == nil) + if((rpc = file->auth) == nil) return -1; - if(s->rpc == nil) + if(auth_rpc(rpc, "write", data, count) != ARok) return -1; - if(auth_rpc(s->rpc, "write", data, n) != ARok){ - failure(s, "auth_rpc: write"); - return -1; - } - return n; + return count; } -int -authuid(Auth* s) -{ - return s->uid; -} - -char* -authaname(Auth* s) -{ - return s->aname; -} - -char* -authuname(Auth* s) -{ - return s->uname; -} diff --git a/sys/src/cmd/cwfs/main.c b/sys/src/cmd/cwfs/main.c index 9d12066d0..bd8265c0c 100644 --- a/sys/src/cmd/cwfs/main.c +++ b/sys/src/cmd/cwfs/main.c @@ -140,7 +140,6 @@ confinit(void) localconfinit(); conf.nwpath = conf.nfile*8; - conf.nauth = conf.nfile/10; conf.gidspace = conf.nuid*3; cons.flags = 0; @@ -364,7 +363,6 @@ main(int argc, char **argv) wpaths = malloc(conf.nwpath * sizeof(*wpaths)); uid = malloc(conf.nuid * sizeof(*uid)); gidspace = malloc(conf.gidspace * sizeof(*gidspace)); - authinit(); print("iobufinit\n"); iobufinit(); diff --git a/sys/src/cmd/cwfs/net.c b/sys/src/cmd/cwfs/net.c index 3cd8f08ca..927433db1 100644 --- a/sys/src/cmd/cwfs/net.c +++ b/sys/src/cmd/cwfs/net.c @@ -52,30 +52,27 @@ static void neti(void *v) { int lisfd, accfd; - Network *net; NetConnInfo *nci; + Network *net; net = v; print("net%di\n", net->ctlrno); +Listen: + if((lisfd = listen(net->anndir, net->lisdir)) < 0){ + print("listen %s failed: %r\n", net->anndir); + return; + } for(;;) { - lisfd = listen(net->anndir, net->lisdir); - if (lisfd < 0) { - print("listen %s failed: %r\n", net->anndir); - continue; - } - /* got new call on lisfd */ - accfd = accept(lisfd, net->lisdir); - if (accfd < 0) { + if((accfd = accept(lisfd, net->lisdir)) < 0){ print("accept %d (from %s) failed: %r\n", lisfd, net->lisdir); - continue; + close(lisfd); + goto Listen; } - nci = getnetconninfo(net->lisdir, accfd); srvchan(accfd, nci->raddr); freenetconninfo(nci); - close(lisfd); } } @@ -85,7 +82,7 @@ netstart(void) Network *net; for(net = &netif[0]; net < &netif[Maxnets]; net++){ - if(net->dialstr == nil) + if(net->dialstr == nil || *net->anndir == 0) continue; sprint(net->name, "net%di", net->ctlrno); newproc(neti, net, net->name); @@ -99,10 +96,13 @@ netinit(void) for (net = netif; net < netif + Maxnets; net++) { net->dialstr = annstrs[net - netif]; - if (net->dialstr == nil) + if(net->dialstr == nil) continue; - if((net->annfd = announce(net->dialstr, net->anndir)) < 0) + if((net->annfd = announce(net->dialstr, net->anndir)) < 0){ print("can't announce %s: %r", net->dialstr); + net->dialstr = nil; + continue; + } print("netinit: announced on %s\n", net->dialstr); } } diff --git a/sys/src/cmd/cwfs/portdat.h b/sys/src/cmd/cwfs/portdat.h index a2e96b157..4a5264486 100644 --- a/sys/src/cmd/cwfs/portdat.h +++ b/sys/src/cmd/cwfs/portdat.h @@ -358,7 +358,7 @@ struct File Off lastra; /* read ahead address */ ulong fid; Userid uid; - Auth *auth; + void *auth; char open; #define FREAD 1 #define FWRITE 2 @@ -434,7 +434,6 @@ struct Conf char *confdev; char *devmap; /* name of config->file device mapping file */ - ulong nauth; /* number of Auth structs */ uchar nodump; /* no periodic dumps */ uchar dumpreread; /* read and compare in dump copy */ }; diff --git a/sys/src/cmd/cwfs/portfns.h b/sys/src/cmd/cwfs/portfns.h index 3a2d70ac5..f35c37022 100644 --- a/sys/src/cmd/cwfs/portfns.h +++ b/sys/src/cmd/cwfs/portfns.h @@ -2,13 +2,9 @@ void accessdir(Iobuf*, Dentry*, int, int); void addfree(Device*, Off, Superb*); void arpstart(void); void arginit(void); -char* authaname(Auth*); -void authinit(void); -void authfree(Auth*); -Auth* authnew(char*, char*); +void* authnew(void); +void authfree(void*); int authread(File*, uchar*, int); -int authuid(Auth*); -char* authuname(Auth*); int authwrite(File*, uchar*, int); void cdiag(char*, int); int cnumb(void);