Commit Diff


commit - 6861b1cdf80a35e8bcd8fac0e7a0181e16ec4d674e142ba03160d0cb595db97b
commit + 9d049e7f3dcb2a65388f25e0b11268f20c6baca68251886c7a6c7a70b86f69fe
blob - 55c2f211d82888bfb9fd4584245fbd84a5c19cf16e47d20e74e9fbd8562b022c
blob + da2a711d3f34f0d96399f4cd96d9e4ab92b671724d82145dcad1afbdd2abb83a
--- gotwebd/auth.c	
+++ gotwebd/auth.c	
@@ -390,7 +390,7 @@ logged_in:
 	r = tp_writef(c->tp, "Set-Cookie: gwdauth=%s;"
 	    " SameSite=Strict;%s Path=%s; HttpOnly; Max-Age=%llu\r\n", token,
 	    env->auth_config == GOTWEBD_AUTH_SECURE ? " Secure;" : "",
-	    env->gotweb_url_root, validity);
+	    srv->gotweb_url_root, validity);
 	explicit_bzero(token, strlen(token));
 	free(token);
 	if (r == -1) {
@@ -420,6 +420,80 @@ err:
 	}
 }
 
+static void
+do_logout(struct request *c)
+{
+	const struct got_error *error = NULL;
+	struct gotwebd *env = gotwebd_env;
+	uid_t uid;
+	struct server *srv;
+	char *hostname = NULL;
+	const char *identifier = NULL;
+	struct gotweb_url url;
+
+	int r;
+
+	if (login_check_token(&uid, &hostname, c->fcgi_params.auth_cookie,
+	    auth_token_secret, sizeof(auth_token_secret),
+	    "authentication") == -1) {
+		error = got_error(GOT_ERR_LOGOUT_FAILED);
+		goto err;
+	}
+
+	/*
+	 * The www user ID represents the case where no authentication
+	 * occurred. This user must not be allowed to log in.
+	 */
+	if (uid == env->www_uid) {
+		error = got_error(GOT_ERR_LOGOUT_FAILED);
+		goto err;
+	}
+
+	c->client_uid = uid;
+	if (strcmp(hostname, c->fcgi_params.server_name) != 0) {
+		error = got_error_msg(GOT_ERR_LOGOUT_FAILED,
+		    "wrong server name in authentication cookie");
+		goto err;
+	}
+
+	srv = gotweb_get_server(c->fcgi_params.server_name);
+	if (srv == NULL) {
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "invalid server name for logout");
+		goto err;
+	}
+
+	if (gotwebd_env->gotwebd_verbose > 0) {
+		log_info("logging out uid %u as %s for server \"%s\"",
+		    uid, identifier, hostname);
+	}
+
+	/* Ask the browser to delete the authentication cookie.  */
+	r = tp_writef(c->tp, "Set-Cookie: gwdauth=invalid;"
+	    " SameSite=Strict;%s Path=%s; HttpOnly; Max-Age=-1\r\n",
+	    env->auth_config == GOTWEBD_AUTH_SECURE ? " Secure;" : "",
+	    srv->gotweb_url_root);
+	if (r == -1) {
+		error = got_error_from_errno("tp_writef");
+		goto err;
+	}
+
+	memset(&url, 0, sizeof(url));
+	url.action = INDEX;
+	gotweb_reply(c, 307, "text/html", &url);
+	return;
+
+err:
+	free(hostname);
+	hostname = NULL;
+
+	log_warnx("%s: %s", __func__, error->msg);
+	c->t->error = error;
+	if (gotweb_reply(c, 400, "text/html", NULL) == -1)
+		return;
+	gotweb_render_page(c->tp, gotweb_render_error);
+}
+
 static const struct got_error *
 login_error_hint(struct request *c)
 {
@@ -760,6 +834,8 @@ auth_dispatch_sockets(int fd, short event, void *arg)
 
 			if (c->fcgi_params.qs.login[0] != '\0')
 				do_login(c);
+			else if (c->fcgi_params.qs.logout)
+				do_logout(c);
 			else
 				process_request(c);
 
blob - 6f33095b24bb1c13fbc492f1879e463631281c5986b30436d24aa3e52b98701d
blob + db148c7b30005ad5bf5029a384b4dea3c6b5a3299b10f0c0c7043961dda72c82
--- gotwebd/fcgi.c	
+++ gotwebd/fcgi.c	
@@ -192,6 +192,7 @@ static const struct querystring_keys querystring_keys[
 	{ "index_page",	GOTWEBD_QS_INDEX_PAGE },
 	{ "path",		GOTWEBD_QS_PATH },
 	{ "login",		GOTWEBD_QS_LOGIN },
+	{ "logout",		GOTWEBD_QS_LOGOUT },
 };
 
 static const struct action_keys action_keys[] = {
@@ -380,6 +381,11 @@ assign_querystring(struct querystring *qs, char *key, 
 				goto done;
 			}
 			break;
+		case GOTWEBD_QS_LOGOUT:
+			if (strcmp(value, "1") != 0)
+				break;
+			qs->logout = 1;
+			break;
  		}
 
 		/* entry found */
blob - c7cfa23095b2ce95bce29d79969042cdb233b3ffdf5a5f0bf7e2be72c26515d5
blob + 50c35a2566682a1e6e1242ef7ba702ba014e58691f61af9a8c011a7940dc414f
--- gotwebd/gotweb.c	
+++ gotwebd/gotweb.c	
@@ -1469,6 +1469,12 @@ gotweb_render_url(struct request *c, struct gotweb_url
 		sep = "&";
 	}
 
+	if (url->logout == 1) {
+		if (tp_writef(c->tp, "%slogout=1", sep) == -1)
+			return -1;
+		sep = "&";
+	}
+
 	return 0;
 }
 
blob - c1c0a092fbc961e59b48974215ecfed6ba9eb2276b81d944f2fe815d3c26becb
blob + 521d17ad10808a101361c034491da7847ab21c258784152aa84648122ef470da
--- gotwebd/gotwebd.h	
+++ gotwebd/gotwebd.h	
@@ -311,6 +311,7 @@ struct querystring {
 	int		 index_page;
 	char		 path[PATH_MAX];
 	char		 login[MAX_AUTH_COOKIE];
+	int		 logout;
 };
 
 struct gotwebd_fcgi_params {
@@ -556,6 +557,7 @@ struct gotweb_url {
 	const char	*folder;
 	const char	*headref;
 	const char	*path;
+	int		 logout;
 };
 
 struct querystring_keys {
@@ -577,6 +579,7 @@ enum querystring_elements {
 	GOTWEBD_QS_INDEX_PAGE,
 	GOTWEBD_QS_PATH,
 	GOTWEBD_QS_LOGIN,
+	GOTWEBD_QS_LOGOUT,
 };
 
 extern struct gotwebd	*gotwebd_env;
blob - ce953339bc789bf5127c1a3b4b2c11f870b0872fe7ec93a9f20b0f84b1146110
blob + 78189fba8fb23c1b31d117093d25365f8266118d8872a25d50b51ff4c3a4cd2f
--- gotwebd/pages.tmpl	
+++ gotwebd/pages.tmpl	
@@ -169,7 +169,7 @@ nextsep(char *s, char **t)
 	struct request		*c = tp->tp_arg;
 	struct server		*srv = c->srv;
 	const struct querystring *qs = c->t->qs;
-	struct gotweb_url	 u_path;
+	struct gotweb_url	 u_path, u_logout;
 	char			 prefix[MAX_DOCUMENT_URI * 2];
 	const char		*css = srv->custom_css;
 	const char		*url_root, *url_path;
@@ -198,6 +198,11 @@ nextsep(char *s, char **t)
 	memset(&u_path, 0, sizeof(u_path));
 	u_path.index_page = -1;
 	u_path.action = SUMMARY;
+
+	memset(&u_logout, 0, sizeof(u_logout));
+	u_logout.index_page = -1;
+	u_logout.action = INDEX;
+	u_logout.logout = 1;
 !}
 <!doctype html>
 <html>
@@ -223,7 +228,8 @@ nextsep(char *s, char **t)
       </div>
       {{ if (c->access_identifier[0]) }}	
       <div id="login_status">
-        Logged in as: <b>{{ c->access_identifier }}</b>
+        Logged in as: <b>{{ c->access_identifier }}</b> &nbsp;
+	(<a href="{{ render gotweb_render_url(c, &u_logout) }}">Logout</a>)
       </div>
       {{ end }}
     </header>
blob - dbb9a1b16902cc45fc02ef6e8e804088b93755b1aa30506f9eab43e1dd7d9405
blob + ba090fea1fcd6b705ecaf44f184ae949f456ca296f6018547797cf6fd4e89068
--- gotwebd/sockets.c	
+++ gotwebd/sockets.c	
@@ -640,6 +640,9 @@ recv_parsed_params(struct imsg *imsg)
 		goto fail;
 	}
 
+	if (params.qs.logout)
+		p->qs.logout = 1;
+
 	if (params.document_uri[0] != '\0' &&
 	    strlcpy(p->document_uri, params.document_uri,
 	    sizeof(p->document_uri)) >= sizeof(p->document_uri)) {
blob - f0578997902d6f6e642142b56111f9569807f3da23eba6106259e0f0c21df569
blob + e817adacd2a95cfa4a5c9aea593f3c6b46825b86a2c856f4172c63a9520f706d
--- include/got_error.h	
+++ include/got_error.h	
@@ -201,6 +201,7 @@
 #define GOT_ERR_UNKNOWN_COMMAND	193
 #define GOT_ERR_NOT_FOUND	194
 #define GOT_ERR_MEDIA_TYPE	195
+#define GOT_ERR_LOGOUT_FAILED	196
 
 struct got_error {
         int code;
blob - 273b0a7398c713b9803170027a9ea30678e74a4d5da7fc3240d4366e4a18bc01
blob + 3ab249f0ce4e7433056ca49721fa2eab4b9eeaec9083ebdef9d4dd45cc84b8ff
--- lib/error.c	
+++ lib/error.c	
@@ -252,6 +252,7 @@ static const struct got_error got_errors[] = {
 	{ GOT_ERR_UNKNOWN_COMMAND, "command not found" },
 	{ GOT_ERR_NOT_FOUND, "not found" },
 	{ GOT_ERR_MEDIA_TYPE,	"malformed media type" },
+	{ GOT_ERR_LOGOUT_FAILED, "logout failed" },
 };
 
 static struct got_custom_error {