git: 9front

Download patch

ref: 70bd006be0d883fb17bc9a25d5c7b04484f957d2
parent: e8d2400c5eec117be414d005dc0b034d0deaacbf
author: cinap_lenrek <cinap_lenrek@localhost>
date: Sun Apr 17 00:06:33 EDT 2011

cwfs: code cleanup

--- a/sys/src/cmd/cwfs/9p2.c
+++ b/sys/src/cmd/cwfs/9p2.c
@@ -152,14 +152,14 @@
 	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 @@
 	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 @@
 authorize(Chan* chan, Fcall* f)
 {
 	File* af;
-	int db, uid = -1;
+	int db, uid;
 
 	db = cons.flags & authdebugflag;
 
@@ -247,26 +246,9 @@
 
 	/* 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 @@
 	file->open = 0;
 	freewp(file->wpath);
 	authfree(file->auth);
+	file->auth = 0;
 	freefp(file);
 	qunlock(file);
 
@@ -1368,7 +1351,7 @@
 		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);
--- a/sys/src/cmd/cwfs/auth.c
+++ b/sys/src/cmd/cwfs/auth.c
@@ -16,7 +16,6 @@
 /*
  * 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 @@
 	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)
+void*
+authnew(void)
 {
-	auths = malloc(conf.nauth * sizeof(*auths));
-}
-
-static int
-failure(Auth *s, char *why)
-{
 	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;
+	return rpc;
 }
 
-Auth*
-authnew(char *uname, char *aname)
+void
+authfree(void *auth)
 {
-	static int si = 0;
-	int afd, i, nwrap;
-	Auth *s;
+	AuthRpc *rpc;
 
-	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;
+	if(rpc = auth)
+		auth_freerpc(rpc);
 }
 
-void
-authfree(Auth *s)
-{
-	if(s){
-		failure(s, "");
-		s->inuse = 0;
-	}
-}
-
 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;
-}
--- a/sys/src/cmd/cwfs/main.c
+++ b/sys/src/cmd/cwfs/main.c
@@ -140,7 +140,6 @@
 	localconfinit();
 
 	conf.nwpath = conf.nfile*8;
-	conf.nauth =  conf.nfile/10;
 	conf.gidspace = conf.nuid*3;
 
 	cons.flags = 0;
@@ -364,7 +363,6 @@
 	wpaths = malloc(conf.nwpath * sizeof(*wpaths));
 	uid = malloc(conf.nuid * sizeof(*uid));
 	gidspace = malloc(conf.gidspace * sizeof(*gidspace));
-	authinit();
 
 	print("iobufinit\n");
 	iobufinit();
--- a/sys/src/cmd/cwfs/net.c
+++ b/sys/src/cmd/cwfs/net.c
@@ -52,30 +52,27 @@
 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 @@
 	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 @@
 
 	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);
 	}
 }
--- a/sys/src/cmd/cwfs/portdat.h
+++ b/sys/src/cmd/cwfs/portdat.h
@@ -358,7 +358,7 @@
 	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 @@
 	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 */
 };
--- a/sys/src/cmd/cwfs/portfns.h
+++ b/sys/src/cmd/cwfs/portfns.h
@@ -2,13 +2,9 @@
 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);
--