@matheusbsbs / matheus-bagliano / commits / 678dac6b45
Fork 0

Added support for dropping privileges to git-daemon.

Signed-off-by: Tilman Sauerbeck <tilman@code-monkey.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Tilman Sauerbeck committed Aug 22, 2006 at 19:37 UTC 678dac6b45437d65fa958c03ca47f4a0337efad4
2 files changed +58 -5
Documentation/git-daemon.txt
+13 -1
@@ -11,7 +11,8 @@ SYNOPSIS
11 'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all]
12 [--timeout=n] [--init-timeout=n] [--strict-paths]
13 [--base-path=path] [--user-path | --user-path=path]
14 - [--reuseaddr] [--detach] [--pid-file=file] [directory...]
14 + [--reuseaddr] [--detach] [--pid-file=file]
15 + [--user=user [--group=group]] [directory...]
16
17 DESCRIPTION
18 -----------
@@ -93,6 +94,17 @@ OPTIONS
94 --pid-file=file::
95 Save the process id in 'file'.
96
97 +--user=user, --group=group::
98 + Change daemon's uid and gid before entering the service loop.
99 + When only `--user` is given without `--group`, the
100 + primary group ID for the user is used. The values of
101 + the option are given to `getpwnam(3)` and `getgrnam(3)`
102 + and numeric IDs are not supported.
103 ++
104 +Giving these options is an error when used with `--inetd`; use
105 +the facility of inet daemon to achieve the same before spawning
106 +`git-daemon` if needed.
107 +
108 <directory>::
109 A directory to add to the whitelist of allowed directories. Unless
110 --strict-paths is specified this will also include subdirectories
daemon.c
+45 -4
@@ -7,6 +7,8 @@
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <syslog.h>
10 +#include <pwd.h>
11 +#include <grp.h>
12 #include "pkt-line.h"
13 #include "cache.h"
14 #include "exec_cmd.h"
@@ -19,7 +21,8 @@ static const char daemon_usage[] =
21 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
22 " [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
23 " [--base-path=path] [--user-path | --user-path=path]\n"
22 -" [--reuseaddr] [--detach] [--pid-file=file] [directory...]";
24 +" [--reuseaddr] [--detach] [--pid-file=file]\n"
25 +" [--user=user [[--group=group]] [directory...]";
26
27 /* List of acceptable pathname prefixes */
28 static char **ok_paths;
@@ -701,7 +704,7 @@ static void store_pid(const char *path)
704 fclose(f);
705 }
706
704 -static int serve(int port)
707 +static int serve(int port, struct passwd *pass, gid_t gid)
708 {
709 int socknum, *socklist;
710
@@ -709,6 +712,11 @@ static int serve(int port)
712 if (socknum == 0)
713 die("unable to allocate any listen sockets on port %u", port);
714
715 + if (pass && gid &&
716 + (initgroups(pass->pw_name, gid) || setgid (gid) ||
717 + setuid(pass->pw_uid)))
718 + die("cannot drop privileges");
719 +
720 return service_loop(socknum, socklist);
721 }
722
@@ -716,8 +724,11 @@ int main(int argc, char **argv)
724 {
725 int port = DEFAULT_GIT_PORT;
726 int inetd_mode = 0;
719 - const char *pid_file = NULL;
727 + const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
728 int detach = 0;
729 + struct passwd *pass = NULL;
730 + struct group *group;
731 + gid_t gid = 0;
732 int i;
733
734 /* Without this we cannot rely on waitpid() to tell
@@ -791,6 +802,14 @@ int main(int argc, char **argv)
802 log_syslog = 1;
803 continue;
804 }
805 + if (!strncmp(arg, "--user=", 7)) {
806 + user_name = arg + 7;
807 + continue;
808 + }
809 + if (!strncmp(arg, "--group=", 8)) {
810 + group_name = arg + 8;
811 + continue;
812 + }
813 if (!strcmp(arg, "--")) {
814 ok_paths = &argv[i+1];
815 break;
@@ -802,6 +821,28 @@ int main(int argc, char **argv)
821 usage(daemon_usage);
822 }
823
824 + if (inetd_mode && (group_name || user_name))
825 + die("--user and --group are incompatible with --inetd");
826 +
827 + if (group_name && !user_name)
828 + die("--group supplied without --user");
829 +
830 + if (user_name) {
831 + pass = getpwnam(user_name);
832 + if (!pass)
833 + die("user not found - %s", user_name);
834 +
835 + if (!group_name)
836 + gid = pass->pw_gid;
837 + else {
838 + group = getgrnam(group_name);
839 + if (!group)
840 + die("group not found - %s", group_name);
841 +
842 + gid = group->gr_gid;
843 + }
844 + }
845 +
846 if (log_syslog) {
847 openlog("git-daemon", 0, LOG_DAEMON);
848 set_die_routine(daemon_die);
@@ -831,5 +872,5 @@ int main(int argc, char **argv)
872 if (pid_file)
873 store_pid(pid_file);
874
834 - return serve(port);
875 + return serve(port, pass, gid);
876 }