daemon: --access-hook option

The --access-hook option to "git daemon" specifies an external command to be run every time a client connects, with - service name (e.g. "upload-pack", etc.), - path to the repository, - hostname (%H), - canonical hostname (%CH), - ip address (%IP), - tcp port (%P) as its command line arguments. The external command can decide to decline the service by exiting with a non-zero status (or to allow it by exiting with a zero status). It can also look at the $REMOTE_ADDR and $REMOTE_PORT environment variables to learn about the requestor when making this decision. The external command can optionally write a single line to its standard output to be sent to the requestor as an error message when it declines the service. Acked-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 14, 2012 at 11:37 UTC 93741e4a915fc33a00e6facff88cd27565d10f82
2 files changed +93
Documentation/git-daemon.txt
+16
@@ -16,6 +16,7 @@ SYNOPSIS
16 [--reuseaddr] [--detach] [--pid-file=<file>]
17 [--enable=<service>] [--disable=<service>]
18 [--allow-override=<service>] [--forbid-override=<service>]
19 + [--access-hook=<path>]
20 [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>] [--user=<user> [--group=<group>]]
21 [<directory>...]
22
@@ -171,6 +172,21 @@ the facility of inet daemon to achieve the same before spawning
172 errors are not enabled, all errors report "access denied" to the
173 client. The default is --no-informative-errors.
174
175 +--access-hook=<path>::
176 + Every time a client connects, first run an external command
177 + specified by the <path> with service name (e.g. "upload-pack"),
178 + path to the repository, hostname (%H), canonical hostname
179 + (%CH), ip address (%IP), and tcp port (%P) as its command line
180 + arguments. The external command can decide to decline the
181 + service by exiting with a non-zero status (or to allow it by
182 + exiting with a zero status). It can also look at the $REMOTE_ADDR
183 + and $REMOTE_PORT environment variables to learn about the
184 + requestor when making this decision.
185 ++
186 +The external command can optionally write a single line to its
187 +standard output to be sent to the requestor as an error message when
188 +it declines the service.
189 +
190 <directory>::
191 A directory to add to the whitelist of allowed directories. Unless
192 --strict-paths is specified this will also include subdirectories
daemon.c
+77
@@ -30,6 +30,7 @@ static const char daemon_usage[] =
30 " [--interpolated-path=<path>]\n"
31 " [--reuseaddr] [--pid-file=<file>]\n"
32 " [--(enable|disable|allow-override|forbid-override)=<service>]\n"
33 +" [--access-hook=<path>]\n"
34 " [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
35 " [--detach] [--user=<user> [--group=<group>]]\n"
36 " [<directory>...]";
@@ -256,6 +257,71 @@ static int daemon_error(const char *dir, const char *msg)
257 return -1;
258 }
259
260 +static char *access_hook;
261 +
262 +static int run_access_hook(struct daemon_service *service, const char *dir, const char *path)
263 +{
264 + struct child_process child;
265 + struct strbuf buf = STRBUF_INIT;
266 + const char *argv[8];
267 + const char **arg = argv;
268 + char *eol;
269 + int seen_errors = 0;
270 +
271 +#define STRARG(x) ((x) ? (x) : "")
272 + *arg++ = access_hook;
273 + *arg++ = service->name;
274 + *arg++ = path;
275 + *arg++ = STRARG(hostname);
276 + *arg++ = STRARG(canon_hostname);
277 + *arg++ = STRARG(ip_address);
278 + *arg++ = STRARG(tcp_port);
279 + *arg = NULL;
280 +#undef STRARG
281 +
282 + memset(&child, 0, sizeof(child));
283 + child.use_shell = 1;
284 + child.argv = argv;
285 + child.no_stdin = 1;
286 + child.no_stderr = 1;
287 + child.out = -1;
288 + if (start_command(&child)) {
289 + logerror("daemon access hook '%s' failed to start",
290 + access_hook);
291 + goto error_return;
292 + }
293 + if (strbuf_read(&buf, child.out, 0) < 0) {
294 + logerror("failed to read from pipe to daemon access hook '%s'",
295 + access_hook);
296 + strbuf_reset(&buf);
297 + seen_errors = 1;
298 + }
299 + if (close(child.out) < 0) {
300 + logerror("failed to close pipe to daemon access hook '%s'",
301 + access_hook);
302 + seen_errors = 1;
303 + }
304 + if (finish_command(&child))
305 + seen_errors = 1;
306 +
307 + if (!seen_errors) {
308 + strbuf_release(&buf);
309 + return 0;
310 + }
311 +
312 +error_return:
313 + strbuf_ltrim(&buf);
314 + if (!buf.len)
315 + strbuf_addstr(&buf, "service rejected");
316 + eol = strchr(buf.buf, '\n');
317 + if (eol)
318 + *eol = '\0';
319 + errno = EACCES;
320 + daemon_error(dir, buf.buf);
321 + strbuf_release(&buf);
322 + return -1;
323 +}
324 +
325 static int run_service(char *dir, struct daemon_service *service)
326 {
327 const char *path;
@@ -303,6 +369,13 @@ static int run_service(char *dir, struct daemon_service *service)
369 return daemon_error(dir, "service not enabled");
370 }
371
372 + /*
373 + * Optionally, a hook can choose to deny access to the
374 + * repository depending on the phase of the moon.
375 + */
376 + if (access_hook && run_access_hook(service, dir, path))
377 + return -1;
378 +
379 /*
380 * We'll ignore SIGTERM from now on, we have a
381 * good client.
@@ -1142,6 +1215,10 @@ int main(int argc, char **argv)
1215 export_all_trees = 1;
1216 continue;
1217 }
1218 + if (!prefixcmp(arg, "--access-hook=")) {
1219 + access_hook = arg + 14;
1220 + continue;
1221 + }
1222 if (!prefixcmp(arg, "--timeout=")) {
1223 timeout = atoi(arg+10);
1224 continue;