Commit Diff


commit - e019e61b9b185dc80cca8bd0340ae3a075a1e28df7ab5c9ee801937d3ab260a8
commit + fb19edde339286346d944b66d9fbcd7cfc4686bcaa2db632c8ad1f5546b1e5aa
blob - c7bf114be08d24f4fa08bf391df07ffe425699bac0b116a62f0f0d6e18419f86
blob + dff93340975a0e8de1f9747339134df42f320da2523fd0a424792b825870fbb2
--- gotwebd/fcgi.c	
+++ gotwebd/fcgi.c	
@@ -186,6 +186,7 @@ static const struct querystring_keys querystring_keys[
 	{ "headref",		HEADREF },
 	{ "index_page",		INDEX_PAGE },
 	{ "path",		PATH },
+	{ "login",		LOGIN },
 };
 
 static const struct action_keys action_keys[] = {
blob - 964956044084e15e68c131c4e14c9d229c4bb379fc21ffe26d7aa2c03f868c5c
blob + f17dcb7d1cb6eb55a8bb76ca55872db336f82fc4d3b647054bfe7d88031929cb
--- gotwebd/gotwebd.h	
+++ gotwebd/gotwebd.h	
@@ -59,6 +59,8 @@
 #define MAX_QUERYSTRING		 2048
 #define MAX_DOCUMENT_URI	 255
 #define MAX_SERVER_NAME		 255
+#define MAX_AUTH_COOKIE		 255
+#define MAX_IDENTIFIER_SIZE	 32
 
 #define GOTWEB_GIT_DIR		 ".git"
 
@@ -280,6 +282,7 @@ struct querystring {
 	char		 headref[MAX_DOCUMENT_URI];
 	int		 index_page;
 	char		 path[PATH_MAX];
+	char		 login[MAX_AUTH_COOKIE];
 };
 
 struct gotwebd_fcgi_params {
@@ -287,6 +290,7 @@ struct gotwebd_fcgi_params {
 	struct querystring		 qs;
 	char				 document_uri[MAX_DOCUMENT_URI];
 	char				 server_name[MAX_SERVER_NAME];
+	char				 auth_cookie[MAX_AUTH_COOKIE];
 	int				 https;
 };
 
@@ -317,6 +321,9 @@ struct request {
 	int				 nparams_parsed;
 
 	int				 client_status;
+
+	uid_t				 client_uid;
+	char				 access_identifier[MAX_IDENTIFIER_SIZE];
 };
 TAILQ_HEAD(requestlist, request);
 
@@ -344,6 +351,35 @@ struct address {
 };
 TAILQ_HEAD(addresslist, address);
 
+enum gotwebd_auth_config {
+	GOTWEBD_AUTH_DISABLED	= 0xf00000ff,
+	GOTWEBD_AUTH_SECURE	= 0x00808000,
+	GOTWEBD_AUTH_INSECURE	= 0x0f7f7f00
+};
+
+enum gotwebd_access {
+	GOTWEBD_ACCESS_DENIED = -1,
+	GOTWEBD_ACCESS_PERMITTED = 1
+};
+
+struct gotwebd_access_rule {
+	STAILQ_ENTRY(gotwebd_access_rule) entry;
+
+	enum gotwebd_access access;
+	char identifier[MAX_IDENTIFIER_SIZE];
+};
+STAILQ_HEAD(gotwebd_access_rule_list, gotwebd_access_rule);
+
+struct gotwebd_repo {
+	TAILQ_ENTRY(gotwebd_repo)	 entry;
+
+	char name[NAME_MAX];
+
+	enum gotwebd_auth_config	auth_config;
+	struct gotwebd_access_rule_list access_rules;
+};
+TAILQ_HEAD(gotwebd_repolist, gotwebd_repo);
+
 struct server {
 	TAILQ_ENTRY(server)	 entry;
 
@@ -368,6 +404,11 @@ struct server {
 	int		 show_repo_description;
 	int		 show_repo_cloneurl;
 	int		 respect_exportok;
+
+	enum gotwebd_auth_config auth_config;
+	struct gotwebd_access_rule_list access_rules;
+
+	struct gotwebd_repolist	 repos;
 };
 TAILQ_HEAD(serverlist, server);
 
@@ -409,6 +450,9 @@ struct gotwebd {
 	struct socket	*login_sock;
 	struct event	 login_pause_ev;
 
+	enum gotwebd_auth_config auth_config;
+	struct gotwebd_access_rule_list access_rules;
+
 	int		 pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
 	int		 priv_fd[PRIV_FDS__MAX];
 
@@ -424,9 +468,11 @@ struct gotwebd {
 	struct imsgev	*iev_fcgi;
 	struct imsgev	*iev_login;
 	struct imsgev	*iev_gotsh;
+	struct imsgev	*iev_auth;
 	struct imsgev	*iev_gotweb;
 
 	uint16_t	 prefork;
+	int		 auth_pending;
 	int		 gotweb_pending;
 	int		 *worker_load;
 
@@ -465,6 +511,7 @@ enum querystring_elements {
 	HEADREF,
 	INDEX_PAGE,
 	PATH,
+	LOGIN,
 };
 
 extern struct gotwebd	*gotwebd_env;
@@ -533,6 +580,7 @@ int	gotweb_render_patch(struct template *);
 int	gotweb_render_rss(struct template *);
 
 /* parse.y */
+struct gotwebd_repo * gotwebd_new_repo(const char *);
 int parse_config(const char *, struct gotwebd *);
 int cmdline_symset(char *);
 
blob - 2f2e8be4d1f40c81e09f467b13d81edb14dcc12bd05e816ef65eb266995625bb
blob + c111f1c53d5fc9ef53f12ab163d8cddf0979f90d3dbfd5df516653cff8eaa0f8
--- gotwebd/parse.y	
+++ gotwebd/parse.y	
@@ -103,6 +103,13 @@ struct address *get_unix_addr(const char *);
 int		 addr_dup_check(struct addresslist *, struct address *);
 void		 add_addr(struct address *);
 
+static struct gotwebd_repo	*new_repo;
+static struct gotwebd_repo	*conf_new_repo(struct server *, const char *);
+static void			 conf_new_access_rule(
+				    struct gotwebd_access_rule_list *,
+				    enum gotwebd_access, char *);
+
+
 typedef struct {
 	union {
 		long long	 number;
@@ -118,12 +125,14 @@ typedef struct {
 %token	MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
 %token	SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
 %token	SERVER CHROOT CUSTOM_CSS SOCKET
-%token	SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY USER
+%token	SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY USER AUTHENTICATION
+%token	ENABLE DISABLE INSECURE REPOSITORY PERMIT DENY
 
 %token	<v.string>	STRING
 %token	<v.number>	NUMBER
 %type	<v.number>	boolean
 %type	<v.string>	listen_addr
+%type	<v.string>	numberstring
 
 %%
 
@@ -153,6 +162,16 @@ varset		: STRING '=' STRING	{
 		}
 		;
 
+numberstring	: STRING
+		| NUMBER {
+			if (asprintf(&$$, "%lld", (long long)$1) == -1) {
+				yyerror("asprintf: %s", strerror(errno));
+				YYERROR;
+			}
+		}
+		;
+
+
 boolean		: STRING {
 			if (strcasecmp($1, "1") == 0 ||
 			    strcasecmp($1, "on") == 0)
@@ -260,6 +279,38 @@ main		: PREFORK NUMBER {
 			free(gotwebd->www_user);
 			gotwebd->www_user = $3;
 		}
+		| DISABLE AUTHENTICATION {
+			if (gotwebd->auth_config != 0) {
+				yyerror("ambiguous global authentication "
+				    "setting");
+				YYERROR;
+			}
+			gotwebd->auth_config = GOTWEBD_AUTH_DISABLED;
+		}
+		| ENABLE AUTHENTICATION {
+			if (gotwebd->auth_config != 0) {
+				yyerror("ambiguous global authentication "
+				    "setting");
+				YYERROR;
+			}
+			gotwebd->auth_config = GOTWEBD_AUTH_SECURE;
+		}
+		| ENABLE AUTHENTICATION INSECURE {
+			if (gotwebd->auth_config != 0) {
+				yyerror("ambiguous global authentication "
+				    "setting");
+				YYERROR;
+			}
+			gotwebd->auth_config = GOTWEBD_AUTH_INSECURE;
+		}
+		| PERMIT numberstring {
+			conf_new_access_rule(&gotwebd->access_rules,
+			    GOTWEBD_ACCESS_PERMITTED, $2);
+		}
+		| DENY numberstring {
+			conf_new_access_rule(&gotwebd->access_rules,
+			    GOTWEBD_ACCESS_DENIED, $2);
+		}
 		| LOGIN SOCKET STRING {
 			struct address *h;
 			h = get_unix_addr($3);
@@ -427,12 +478,108 @@ serveropts1	: REPOS_PATH STRING {
 			}
 			new_srv->summary_tags_display = $2;
 		}
+		| DISABLE AUTHENTICATION {
+			if (new_srv->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for server %s",
+				    new_srv->name);
+				YYERROR;
+			}
+			new_srv->auth_config = GOTWEBD_AUTH_DISABLED;
+		}
+		| ENABLE AUTHENTICATION {
+			if (new_srv->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for server %s",
+				    new_srv->name);
+				YYERROR;
+			}
+			new_srv->auth_config = GOTWEBD_AUTH_SECURE;
+		}
+		| ENABLE AUTHENTICATION INSECURE {
+			if (new_srv->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for server %s",
+				    new_srv->name);
+				YYERROR;
+			}
+			new_srv->auth_config = GOTWEBD_AUTH_INSECURE;
+		}
+		| PERMIT numberstring {
+			conf_new_access_rule(&new_srv->access_rules,
+			    GOTWEBD_ACCESS_PERMITTED, $2);
+		}
+		| DENY numberstring {
+			conf_new_access_rule(&new_srv->access_rules,
+			    GOTWEBD_ACCESS_DENIED, $2);
+		}
+		| repository
 		;
 
 serveropts2	: serveropts2 serveropts1 nl
 		| serveropts1 optnl
 		;
 
+repository	: REPOSITORY STRING {
+			struct gotwebd_repo *repo;
+
+			TAILQ_FOREACH(repo, &new_srv->repos, entry) {
+				if (strcmp(repo->name, $2) == 0) {
+					yyerror("duplicate repository "
+					    "'%s' in server '%s'", $2,
+					    new_srv->name);
+					free($2);
+					YYERROR;
+				}
+			}
+
+			new_repo = conf_new_repo(new_srv, $2);
+			free($2);
+		} '{' optnl repoopts2 '}' {
+		}
+		;
+
+repoopts2	: repoopts2 repoopts1 nl
+		| repoopts1 optnl
+		;
+
+repoopts1	: DISABLE AUTHENTICATION {
+			if (new_repo->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for repository %s",
+				    new_repo->name);
+				YYERROR;
+			}
+			new_repo->auth_config = GOTWEBD_AUTH_DISABLED;
+		}
+		| ENABLE AUTHENTICATION {
+			if (new_repo->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for repository %s",
+				    new_repo->name);
+				YYERROR;
+			}
+			new_repo->auth_config = GOTWEBD_AUTH_SECURE;
+		}
+		| ENABLE AUTHENTICATION INSECURE {
+			if (new_repo->auth_config != 0) {
+				yyerror("ambiguous authentication "
+				    "setting for repository %s",
+				    new_repo->name);
+				YYERROR;
+			}
+			new_repo->auth_config = GOTWEBD_AUTH_INSECURE;
+		}
+		| PERMIT numberstring {
+			conf_new_access_rule(&new_repo->access_rules,
+			    GOTWEBD_ACCESS_PERMITTED, $2);
+		}
+		| DENY numberstring {
+			conf_new_access_rule(&new_repo->access_rules,
+			    GOTWEBD_ACCESS_DENIED, $2);
+		}
+		;
+
 nl		: '\n' optnl
 		;
 
@@ -474,8 +621,13 @@ lookup(char *s)
 {
 	/* This has to be sorted always. */
 	static const struct keywords keywords[] = {
+		{ "authentication",		AUTHENTICATION },
 		{ "chroot",			CHROOT },
 		{ "custom_css",			CUSTOM_CSS },
+		{ "deny",			DENY },
+		{ "disable",			DISABLE },
+		{ "enable",			ENABLE },
+		{ "insecure",			INSECURE },
 		{ "listen",			LISTEN },
 		{ "login",			LOGIN },
 		{ "logo",			LOGO },
@@ -483,9 +635,11 @@ lookup(char *s)
 		{ "max_commits_display",	MAX_COMMITS_DISPLAY },
 		{ "max_repos_display",		MAX_REPOS_DISPLAY },
 		{ "on",				ON },
+		{ "permit",			PERMIT },
 		{ "port",			PORT },
 		{ "prefork",			PREFORK },
 		{ "repos_path",			REPOS_PATH },
+		{ "repository",			REPOSITORY },
 		{ "respect_exportok",		RESPECT_EXPORTOK },
 		{ "server",			SERVER },
 		{ "show_repo_age",		SHOW_REPO_AGE },
@@ -833,6 +987,8 @@ int
 parse_config(const char *filename, struct gotwebd *env)
 {
 	struct sym *sym, *next;
+	struct server *srv;
+	struct gotwebd_repo *repo;
 
 	if (config_init(env) == -1)
 		fatalx("failed to initialize configuration");
@@ -904,6 +1060,27 @@ parse_config(const char *filename, struct gotwebd *env
 		gotwebd->login_sock = sockets_conf_new_socket(-1, h);
 	}
 
+	/* Enable authentication if not explicitly configured. */
+	switch (env->auth_config) {
+	case GOTWEBD_AUTH_SECURE:
+	case GOTWEBD_AUTH_INSECURE:
+	case GOTWEBD_AUTH_DISABLED:
+		break;
+	default:
+		env->auth_config = GOTWEBD_AUTH_SECURE;
+		break;
+	}
+
+	/* Inherit implicit authentication config from parent scope. */
+	TAILQ_FOREACH(srv, &env->servers, entry) {
+		if (srv->auth_config == 0)
+			srv->auth_config = env->auth_config;
+		TAILQ_FOREACH(repo, &srv->repos, entry) {
+			if (repo->auth_config == 0)
+				repo->auth_config = srv->auth_config;
+		}
+	}
+
 	return (0);
 }
 
@@ -962,6 +1139,9 @@ conf_new_server(const char *name)
 	srv->summary_commits_display = D_MAXSLCOMMDISP;
 	srv->summary_tags_display = D_MAXSLTAGDISP;
 
+	STAILQ_INIT(&srv->access_rules);
+	TAILQ_INIT(&srv->repos);
+
 	TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
 
 	return srv;
@@ -1157,3 +1337,69 @@ add_addr(struct address *h)
 
 	free(h);
 }
+
+
+struct gotwebd_repo *
+gotwebd_new_repo(const char *name)
+{
+	struct gotwebd_repo *repo;
+
+	repo = calloc(1, sizeof(*repo));
+	if (repo == NULL)
+		return NULL;
+
+	STAILQ_INIT(&repo->access_rules);
+
+	if (strlcpy(repo->name, name, sizeof(repo->name)) >=
+	    sizeof(repo->name)) {
+		free(repo);
+		errno = ENOSPC;
+		return NULL;
+	}
+
+	return repo;
+}
+
+static struct gotwebd_repo *
+conf_new_repo(struct server *server, const char *name)
+{
+	struct gotwebd_repo *repo;
+
+	if (name[0] == '\0') {
+		fatalx("syntax error: empty repository name found in %s",
+		    file->name);
+	}
+
+	if (strchr(name, '/') != NULL)
+		fatalx("repository names must not contain slashes: %s", name);
+
+	if (strchr(name, '\n') != NULL)
+		fatalx("repository names must not contain linefeeds: %s", name);
+
+	repo = gotwebd_new_repo(name);
+	if (repo == NULL)
+		fatal("gotwebd_new_repo");
+
+	TAILQ_INSERT_TAIL(&server->repos, repo, entry);
+
+	return repo;
+};
+
+static void
+conf_new_access_rule(struct gotwebd_access_rule_list *rules,
+    enum gotwebd_access access, char *identifier)
+{
+	struct gotwebd_access_rule *rule;
+
+	rule = calloc(1, sizeof(*rule));
+	if (rule == NULL)
+		fatal("calloc");
+
+	rule->access = access;
+	if (strlcpy(rule->identifier, identifier,
+	    sizeof(rule->identifier)) >= sizeof(rule->identifier))
+		fatalx("identifier too long (max %zu bytes): %s",
+		    sizeof(rule->identifier) - 1, identifier);
+
+	STAILQ_INSERT_TAIL(rules, rule, entry);
+}