cwfs: code cleanup

This commit is contained in:
cinap_lenrek 2011-04-17 04:06:33 +00:00
parent 049c2c3434
commit ec338af87b
6 changed files with 71 additions and 180 deletions

View file

@ -152,14 +152,14 @@ version(Chan* chan, Fcall* f, Fcall* r)
return 0; return 0;
} }
struct {
Lock;
ulong hi;
} authpath;
static int static int
auth(Chan* chan, Fcall* f, Fcall* r) auth(Chan* chan, Fcall* f, Fcall* r)
{ {
static struct {
Lock;
ulong hi;
} authpath;
char *aname; char *aname;
File *file; File *file;
Filsys *fs; Filsys *fs;
@ -195,13 +195,12 @@ auth(Chan* chan, Fcall* f, Fcall* r)
file->open = FREAD+FWRITE; file->open = FREAD+FWRITE;
freewp(file->wpath); freewp(file->wpath);
file->wpath = 0; file->wpath = 0;
file->auth = authnew(f->uname, f->aname); file->uid = -1;
if(file->auth == nil){ if((file->auth = authnew()) == nil){
error = Eauthfile; error = Eauthfile;
goto out; goto out;
} }
r->aqid = file->qid; r->aqid = file->qid;
out: out:
if((cons.flags & attachflag) && error) if((cons.flags & attachflag) && error)
print("9p2: auth %s %T SUCK EGGS --- %s\n", print("9p2: auth %s %T SUCK EGGS --- %s\n",
@ -218,7 +217,7 @@ static int
authorize(Chan* chan, Fcall* f) authorize(Chan* chan, Fcall* f)
{ {
File* af; File* af;
int db, uid = -1; int db, uid;
db = cons.flags & authdebugflag; db = cons.flags & authdebugflag;
@ -247,26 +246,9 @@ authorize(Chan* chan, Fcall* f)
/* fake read to get auth info */ /* fake read to get auth info */
authread(af, nil, 0); authread(af, nil, 0);
uid = af->uid;
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);
if(db) if(db)
print("authorize: uid is %d\n", uid); print("authorize: uid is %d\n", uid);
out:
qunlock(af); qunlock(af);
return uid; return uid;
} }
@ -1321,6 +1303,7 @@ _clunk(File* file, int remove, int wok)
file->open = 0; file->open = 0;
freewp(file->wpath); freewp(file->wpath);
authfree(file->auth); authfree(file->auth);
file->auth = 0;
freefp(file); freefp(file);
qunlock(file); qunlock(file);
@ -1368,7 +1351,7 @@ fs_stat(Chan* chan, Fcall* f, Fcall* r, uchar* data)
d = &dentry; d = &dentry;
mkqid9p1(&d->qid, &file->qid); mkqid9p1(&d->qid, &file->qid);
strcpy(d->name, "#¿"); strcpy(d->name, "#¿");
d->uid = authuid(file->auth); d->uid = file->uid;
d->gid = d->uid; d->gid = d->uid;
d->muid = d->uid; d->muid = d->uid;
d->atime = time(nil); d->atime = time(nil);

View file

@ -16,7 +16,6 @@ nvrgetconfig(void)
/* /*
* we shouldn't be writing nvram any more. * we shouldn't be writing nvram any more.
* the secstore/config field is now just secstore key. * the secstore/config field is now just secstore key.
* we still use authid, authdom and machkey for authentication.
*/ */
int int
@ -84,161 +83,77 @@ conslock(void)
return 1; return 1;
} }
/* authentication structure */ static char *keyspec = "proto=p9any role=server";
struct Auth
{
int inuse;
char uname[NAMELEN]; /* requestor's remote user name */
char aname[NAMELEN]; /* requested aname */
Userid uid; /* uid decided on */
AuthRpc *rpc;
};
Auth* auths; void*
Lock authlock; authnew(void)
void
authinit(void)
{
auths = malloc(conf.nauth * sizeof(*auths));
}
static int
failure(Auth *s, char *why)
{ {
AuthRpc *rpc; AuthRpc *rpc;
int fd;
if(why && *why)print("authentication failed: %s: %r\n", why); if(access("/mnt/factotum", 0) < 0)
s->uid = -1; if((fd = open("/srv/factotum", ORDWR)) >= 0)
if(rpc = s->rpc){ mount(fd, -1, "/mnt", MBEFORE, "");
s->rpc = 0; 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); auth_freerpc(rpc);
return nil;
} }
return -1; return rpc;
}
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;
} }
void void
authfree(Auth *s) authfree(void *auth)
{ {
if(s){ AuthRpc *rpc;
failure(s, "");
s->inuse = 0; if(rpc = auth)
} auth_freerpc(rpc);
} }
int int
authread(File* file, uchar* data, int n) authread(File *file, uchar *data, int count)
{ {
AuthInfo *ai; AuthInfo *ai;
Auth *s; AuthRpc *rpc;
s = file->auth; if((rpc = file->auth) == nil)
if(s == nil)
return -1; return -1;
if(s->rpc == nil) switch(auth_rpc(rpc, "read", nil, 0)){
return -1;
switch(auth_rpc(s->rpc, "read", nil, 0)){
default:
failure(s, "auth_rpc: read");
break;
case ARdone: case ARdone:
if((ai = auth_getinfo(s->rpc)) == nil){ if((ai = auth_getinfo(rpc)) == nil)
failure(s, "auth_getinfo failed"); return -1;
break; file->uid = strtouid(ai->cuid);
}
if(ai->cuid == nil || *ai->cuid == '\0'){
failure(s, "auth with no cuid");
auth_freeAI(ai);
break;
}
failure(s, "");
s->uid = strtouid(ai->cuid);
auth_freeAI(ai); auth_freeAI(ai);
if(file->uid < 0)
return -1;
return 0; return 0;
case ARok: case ARok:
if(n < s->rpc->narg) if(count < rpc->narg)
break; return -1;
memmove(data, s->rpc->arg, s->rpc->narg); memmove(data, rpc->arg, rpc->narg);
return s->rpc->narg; return rpc->narg;
case ARphase:
return -1;
default:
return -1;
} }
return -1;
} }
int int
authwrite(File* file, uchar *data, int n) authwrite(File *file, uchar *data, int count)
{ {
Auth *s; AuthRpc *rpc;
s = file->auth; if((rpc = file->auth) == nil)
if(s == nil)
return -1; return -1;
if(s->rpc == nil) if(auth_rpc(rpc, "write", data, count) != ARok)
return -1; return -1;
if(auth_rpc(s->rpc, "write", data, n) != ARok){ return count;
failure(s, "auth_rpc: write");
return -1;
}
return n;
} }
int
authuid(Auth* s)
{
return s->uid;
}
char*
authaname(Auth* s)
{
return s->aname;
}
char*
authuname(Auth* s)
{
return s->uname;
}

View file

@ -140,7 +140,6 @@ confinit(void)
localconfinit(); localconfinit();
conf.nwpath = conf.nfile*8; conf.nwpath = conf.nfile*8;
conf.nauth = conf.nfile/10;
conf.gidspace = conf.nuid*3; conf.gidspace = conf.nuid*3;
cons.flags = 0; cons.flags = 0;
@ -364,7 +363,6 @@ main(int argc, char **argv)
wpaths = malloc(conf.nwpath * sizeof(*wpaths)); wpaths = malloc(conf.nwpath * sizeof(*wpaths));
uid = malloc(conf.nuid * sizeof(*uid)); uid = malloc(conf.nuid * sizeof(*uid));
gidspace = malloc(conf.gidspace * sizeof(*gidspace)); gidspace = malloc(conf.gidspace * sizeof(*gidspace));
authinit();
print("iobufinit\n"); print("iobufinit\n");
iobufinit(); iobufinit();

View file

@ -52,30 +52,27 @@ static void
neti(void *v) neti(void *v)
{ {
int lisfd, accfd; int lisfd, accfd;
Network *net;
NetConnInfo *nci; NetConnInfo *nci;
Network *net;
net = v; net = v;
print("net%di\n", net->ctlrno); 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(;;) { for(;;) {
lisfd = listen(net->anndir, net->lisdir);
if (lisfd < 0) {
print("listen %s failed: %r\n", net->anndir);
continue;
}
/* got new call on lisfd */ /* got new call on lisfd */
accfd = accept(lisfd, net->lisdir); if((accfd = accept(lisfd, net->lisdir)) < 0){
if (accfd < 0) {
print("accept %d (from %s) failed: %r\n", print("accept %d (from %s) failed: %r\n",
lisfd, net->lisdir); lisfd, net->lisdir);
continue; close(lisfd);
goto Listen;
} }
nci = getnetconninfo(net->lisdir, accfd); nci = getnetconninfo(net->lisdir, accfd);
srvchan(accfd, nci->raddr); srvchan(accfd, nci->raddr);
freenetconninfo(nci); freenetconninfo(nci);
close(lisfd);
} }
} }
@ -85,7 +82,7 @@ netstart(void)
Network *net; Network *net;
for(net = &netif[0]; net < &netif[Maxnets]; net++){ for(net = &netif[0]; net < &netif[Maxnets]; net++){
if(net->dialstr == nil) if(net->dialstr == nil || *net->anndir == 0)
continue; continue;
sprint(net->name, "net%di", net->ctlrno); sprint(net->name, "net%di", net->ctlrno);
newproc(neti, net, net->name); newproc(neti, net, net->name);
@ -99,10 +96,13 @@ netinit(void)
for (net = netif; net < netif + Maxnets; net++) { for (net = netif; net < netif + Maxnets; net++) {
net->dialstr = annstrs[net - netif]; net->dialstr = annstrs[net - netif];
if (net->dialstr == nil) if(net->dialstr == nil)
continue; 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); print("can't announce %s: %r", net->dialstr);
net->dialstr = nil;
continue;
}
print("netinit: announced on %s\n", net->dialstr); print("netinit: announced on %s\n", net->dialstr);
} }
} }

View file

@ -358,7 +358,7 @@ struct File
Off lastra; /* read ahead address */ Off lastra; /* read ahead address */
ulong fid; ulong fid;
Userid uid; Userid uid;
Auth *auth; void *auth;
char open; char open;
#define FREAD 1 #define FREAD 1
#define FWRITE 2 #define FWRITE 2
@ -434,7 +434,6 @@ struct Conf
char *confdev; char *confdev;
char *devmap; /* name of config->file device mapping file */ char *devmap; /* name of config->file device mapping file */
ulong nauth; /* number of Auth structs */
uchar nodump; /* no periodic dumps */ uchar nodump; /* no periodic dumps */
uchar dumpreread; /* read and compare in dump copy */ uchar dumpreread; /* read and compare in dump copy */
}; };

View file

@ -2,13 +2,9 @@ void accessdir(Iobuf*, Dentry*, int, int);
void addfree(Device*, Off, Superb*); void addfree(Device*, Off, Superb*);
void arpstart(void); void arpstart(void);
void arginit(void); void arginit(void);
char* authaname(Auth*); void* authnew(void);
void authinit(void); void authfree(void*);
void authfree(Auth*);
Auth* authnew(char*, char*);
int authread(File*, uchar*, int); int authread(File*, uchar*, int);
int authuid(Auth*);
char* authuname(Auth*);
int authwrite(File*, uchar*, int); int authwrite(File*, uchar*, int);
void cdiag(char*, int); void cdiag(char*, int);
int cnumb(void); int cnumb(void);