shithub: riscv

Download patch

ref: 6dc133ad99486167343885af1860d4e85a0f946c
parent: b6a42aa49c1dcbad21afbbdc3aeb1be6cd032548
author: cinap_lenrek <[email protected]>
date: Fri Jan 11 19:16:07 EST 2013

webfs: preauth support

--- a/sys/man/4/webfs
+++ b/sys/man/4/webfs
@@ -59,6 +59,32 @@
 Writing strings of the form
 .RB `` attr " " value ''
 sets a particular attribute.
+.PP
+The following global parameters can be set:
+.TP
+.B useragent
+Sets the HTTP user agent string.
+.TP
+.B timeout
+Sets the request timeout in seconds.
+.TP
+.BI flushauth " url"
+Flushes any associated authentication information for
+resources under
+.I url
+or all resources if no url was given.
+.TP
+.BI preauth " url realm"
+Preauthenticates all resources under
+.I url
+with the given
+.I realm
+using HTTP Basic authentication. This will cause
+.I webfs
+to preemtively send the resulting authorization information
+not waiting for the server to respond with an
+HTTP 401 Unauthorized status.
+.PP
 The top-level directory also contains
 numbered directories corresponding to connections, which
 may be used to fetch a single URL.
--- a/sys/src/cmd/webfs/fns.h
+++ b/sys/src/cmd/webfs/fns.h
@@ -33,5 +33,6 @@
 void	buflushreq(Buq *q, Req *r);
 
 /* http */
+int authenticate(Url *u, Url *ru, char *method, char *s);
 void flushauth(Url *u, char *t);
 void http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost);
--- a/sys/src/cmd/webfs/fs.c
+++ b/sys/src/cmd/webfs/fs.c
@@ -543,7 +543,7 @@
 }
 
 static char*
-rootctl(char *ctl, char *arg)
+rootctl(Srv *fs, char *ctl, char *arg)
 {
 	Url *u;
 
@@ -578,6 +578,28 @@
 		return nil;
 	}
 
+	/* ppreemptive authentication only basic
+	 * auth supported, ctl message of the form:
+	 *    preauth url realm
+	 */
+	if(!strcmp(ctl, "preauth")){
+		char *a[3], buf[256];
+		int rc;
+
+		if(tokenize(arg, a, nelem(a)) != 2)
+			return "preauth - bad field count";
+		if((u = saneurl(url(a[0], 0))) == nil)
+			return "preauth - malformed url";
+		snprint(buf, sizeof(buf), "BASIC realm=\"%s\"", a[1]);
+		srvrelease(fs);
+		rc = authenticate(u, u, "GET", buf);
+		srvacquire(fs);
+		freeurl(u);
+		if(rc == -1)
+			return "preauth failed";
+		return nil;
+	}
+
 	return "bad ctl message";
 }
 
@@ -670,7 +692,7 @@
 		if(f->level == Qctl)
 			t = clientctl(f->client, s, t);
 		else
-			t = rootctl(s, t);
+			t = rootctl(r->srv, s, t);
 		free(s);
 		respond(r, t);
 		return;
--- a/sys/src/cmd/webfs/http.c
+++ b/sys/src/cmd/webfs/http.c
@@ -311,7 +311,7 @@
 	}
 }
 
-static int
+int
 authenticate(Url *u, Url *ru, char *method, char *s)
 {
 	char *user, *pass, *realm, *nonce, *opaque, *x;