@matheusbsbs / matheus-bagliano / commits / d819e4e682
Fork 0

daemon: prepare for multiple services.

This adds an infrastructure to selectively enable and disable more than one services in git-daemon. Currently upload-pack service, which serves the git-fetch-pack and git-peek-remote clients, is the only service that is defined. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 20, 2006 at 19:03 UTC d819e4e682d68ca41378e4352b9e2bba084971dc
1 file changed +106 -7
daemon.c
+106 -7
@@ -232,13 +232,42 @@ static char *path_ok(char *dir)
232 return NULL; /* Fallthrough. Deny by default */
233 }
234
235 -static int upload(char *dir)
235 +typedef int (*daemon_service_fn)(void);
236 +struct daemon_service {
237 + const char *name;
238 + const char *config_name;
239 + daemon_service_fn fn;
240 + int enabled;
241 + int overridable;
242 +};
243 +
244 +static struct daemon_service *service_looking_at;
245 +static int service_enabled;
246 +
247 +static int git_daemon_config(const char *var, const char *value)
248 +{
249 + if (!strncmp(var, "daemon.", 7) &&
250 + !strcmp(var + 7, service_looking_at->config_name)) {
251 + service_enabled = git_config_bool(var, value);
252 + return 0;
253 + }
254 +
255 + /* we are not interested in parsing any other configuration here */
256 + return 0;
257 +}
258 +
259 +static int run_service(char *dir, struct daemon_service *service)
260 {
237 - /* Timeout as string */
238 - char timeout_buf[64];
261 const char *path;
262 + int enabled = service->enabled;
263 +
264 + loginfo("Request %s for '%s'", service->name, dir);
265
241 - loginfo("Request for '%s'", dir);
266 + if (!enabled && !service->overridable) {
267 + logerror("'%s': service not enabled.", service->name);
268 + errno = EACCES;
269 + return -1;
270 + }
271
272 if (!(path = path_ok(dir)))
273 return -1;
@@ -260,12 +289,34 @@ static int upload(char *dir)
289 return -1;
290 }
291
292 + if (service->overridable) {
293 + service_looking_at = service;
294 + service_enabled = -1;
295 + git_config(git_daemon_config);
296 + if (0 <= service_enabled)
297 + enabled = service_enabled;
298 + }
299 + if (!enabled) {
300 + logerror("'%s': service not enabled for '%s'",
301 + service->name, path);
302 + errno = EACCES;
303 + return -1;
304 + }
305 +
306 /*
307 * We'll ignore SIGTERM from now on, we have a
308 * good client.
309 */
310 signal(SIGTERM, SIG_IGN);
311
312 + return service->fn();
313 +}
314 +
315 +static int upload_pack(void)
316 +{
317 + /* Timeout as string */
318 + char timeout_buf[64];
319 +
320 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
321
322 /* git-upload-pack only ever reads stuff, so this is safe */
@@ -273,10 +324,36 @@ static int upload(char *dir)
324 return -1;
325 }
326
327 +static struct daemon_service daemon_service[] = {
328 + { "upload-pack", "uploadpack", upload_pack, 1, 1 },
329 +};
330 +
331 +static void enable_service(const char *name, int ena) {
332 + int i;
333 + for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
334 + if (!strcmp(daemon_service[i].name, name)) {
335 + daemon_service[i].enabled = ena;
336 + return;
337 + }
338 + }
339 + die("No such service %s", name);
340 +}
341 +
342 +static void make_service_overridable(const char *name, int ena) {
343 + int i;
344 + for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
345 + if (!strcmp(daemon_service[i].name, name)) {
346 + daemon_service[i].overridable = ena;
347 + return;
348 + }
349 + }
350 + die("No such service %s", name);
351 +}
352 +
353 static int execute(struct sockaddr *addr)
354 {
355 static char line[1000];
279 - int pktlen, len;
356 + int pktlen, len, i;
357
358 if (addr) {
359 char addrbuf[256] = "";
@@ -313,8 +390,14 @@ static int execute(struct sockaddr *addr)
390 if (len && line[len-1] == '\n')
391 line[--len] = 0;
392
316 - if (!strncmp("git-upload-pack ", line, 16))
317 - return upload(line+16);
393 + for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
394 + struct daemon_service *s = &(daemon_service[i]);
395 + int namelen = strlen(s->name);
396 + if (!strncmp("git-", line, 4) &&
397 + !strncmp(s->name, line + 4, namelen) &&
398 + line[namelen + 4] == ' ')
399 + return run_service(line + namelen + 5, s);
400 + }
401
402 logerror("Protocol error: '%s'", line);
403 return -1;
@@ -805,6 +888,22 @@ int main(int argc, char **argv)
888 group_name = arg + 8;
889 continue;
890 }
891 + if (!strncmp(arg, "--enable=", 9)) {
892 + enable_service(arg + 9, 1);
893 + continue;
894 + }
895 + if (!strncmp(arg, "--disable=", 10)) {
896 + enable_service(arg + 10, 0);
897 + continue;
898 + }
899 + if (!strncmp(arg, "--enable-override=", 18)) {
900 + make_service_overridable(arg + 18, 1);
901 + continue;
902 + }
903 + if (!strncmp(arg, "--disable-override=", 19)) {
904 + make_service_overridable(arg + 19, 0);
905 + continue;
906 + }
907 if (!strcmp(arg, "--")) {
908 ok_paths = &argv[i+1];
909 break;