blob: 8d45c336f5f816768962c39e8af6b10e0bb0e21e [file] [log] [blame]
Randal L. Schwartz979e32f2005-10-25 16:29:09 -07001#include "cache.h"
Junio C Hamano85023572006-12-19 14:34:12 -08002#include "pkt-line.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05003#include "exec_cmd.h"
Johannes Sixt5d87dd42009-06-14 22:38:51 +02004#include "run-command.h"
5#include "strbuf.h"
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02006#include "string-list.h"
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07007
Johannes Schindelin695dffe2006-09-28 12:00:35 +02008#ifndef HOST_NAME_MAX
9#define HOST_NAME_MAX 256
10#endif
11
Markus Duft28449232010-10-27 10:39:52 +020012#ifdef NO_INITGROUPS
13#define initgroups(x, y) (0) /* nothing */
14#endif
15
Petr Baudis9048fe12005-09-24 16:13:01 +020016static int log_syslog;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020017static int verbose;
Mark Wooding1955fab2006-02-03 20:27:04 +000018static int reuseaddr;
Jeff Kingd5570f42011-10-14 17:19:21 -040019static int informative_errors;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020020
H. Peter Anvin960decc2005-10-19 14:27:01 -070021static const char daemon_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020022"git daemon [--verbose] [--syslog] [--export-all]\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020023" [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
24" [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
25" [--user-path | --user-path=<path>]\n"
26" [--interpolated-path=<path>]\n"
Erik Faye-Lund9cddf562010-11-04 02:35:23 +010027" [--reuseaddr] [--pid-file=<file>]\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020028" [--(enable|disable|allow-override|forbid-override)=<service>]\n"
Junio C Hamano93741e42012-08-14 11:37:51 -070029" [--access-hook=<path>]\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020030" [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
Erik Faye-Lund9cddf562010-11-04 02:35:23 +010031" [--detach] [--user=<user> [--group=<group>]]\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020032" [<directory>...]";
H. Peter Anvin4ae95682005-09-26 19:10:55 -070033
34/* List of acceptable pathname prefixes */
David Rientjes96f1e582006-08-15 10:23:48 -070035static char **ok_paths;
36static int strict_paths;
H. Peter Anvin4ae95682005-09-26 19:10:55 -070037
38/* If this is set, git-daemon-export-ok is not required */
David Rientjes96f1e582006-08-15 10:23:48 -070039static int export_all_trees;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020040
Petr Baudisb21c31c2005-12-23 02:27:40 +010041/* Take all paths relative to this one if non-NULL */
Jeff King1055a892014-06-18 15:41:58 -040042static const char *base_path;
43static const char *interpolated_path;
Jens Axboe73a7a652007-07-27 14:00:29 -070044static int base_path_relaxed;
Jon Loeliger49ba83f2006-09-19 20:31:51 -050045
Junio C Hamano603968d2006-02-04 22:27:29 -080046/* If defined, ~user notation is allowed and the string is inserted
47 * after ~user/. E.g. a request to git://host/~alice/frotz would
48 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
49 */
David Rientjes96f1e582006-08-15 10:23:48 -070050static const char *user_path;
Junio C Hamano603968d2006-02-04 22:27:29 -080051
H. Peter Anvin960decc2005-10-19 14:27:01 -070052/* Timeout, and initial timeout */
David Rientjes96f1e582006-08-15 10:23:48 -070053static unsigned int timeout;
54static unsigned int init_timeout;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020055
René Scharfe01cec542015-03-07 11:50:37 +010056struct hostinfo {
57 struct strbuf hostname;
58 struct strbuf canon_hostname;
59 struct strbuf ip_address;
60 struct strbuf tcp_port;
61 unsigned int hostname_lookup_done:1;
62 unsigned int saw_extended_args:1;
63};
Jon Loeliger49ba83f2006-09-19 20:31:51 -050064
René Scharfe01cec542015-03-07 11:50:37 +010065static void lookup_hostname(struct hostinfo *hi);
René Scharfeedef9532015-02-15 19:31:41 +010066
René Scharfe01cec542015-03-07 11:50:37 +010067static const char *get_canon_hostname(struct hostinfo *hi)
René Scharfeedef9532015-02-15 19:31:41 +010068{
René Scharfe01cec542015-03-07 11:50:37 +010069 lookup_hostname(hi);
70 return hi->canon_hostname.buf;
René Scharfeedef9532015-02-15 19:31:41 +010071}
72
René Scharfe01cec542015-03-07 11:50:37 +010073static const char *get_ip_address(struct hostinfo *hi)
René Scharfeedef9532015-02-15 19:31:41 +010074{
René Scharfe01cec542015-03-07 11:50:37 +010075 lookup_hostname(hi);
76 return hi->ip_address.buf;
René Scharfeedef9532015-02-15 19:31:41 +010077}
78
Petr Baudis9048fe12005-09-24 16:13:01 +020079static void logreport(int priority, const char *err, va_list params)
Petr Baudisf8ff0c02005-09-22 11:25:28 +020080{
Petr Baudis9048fe12005-09-24 16:13:01 +020081 if (log_syslog) {
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +020082 char buf[1024];
83 vsnprintf(buf, sizeof(buf), err, params);
Petr Baudis9048fe12005-09-24 16:13:01 +020084 syslog(priority, "%s", buf);
Junio C Hamano460c2012008-08-24 13:27:10 -070085 } else {
86 /*
Erik Faye-Lund48cfaea2010-11-04 02:35:17 +010087 * Since stderr is set to buffered mode, the
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +020088 * logging of different processes will not overlap
Erik Faye-Lund48cfaea2010-11-04 02:35:17 +010089 * unless they overflow the (rather big) buffers.
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +020090 */
David Soria Parra85e72832008-08-31 14:09:39 +020091 fprintf(stderr, "[%"PRIuMAX"] ", (uintmax_t)getpid());
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +020092 vfprintf(stderr, err, params);
93 fputc('\n', stderr);
Erik Faye-Lund48cfaea2010-11-04 02:35:17 +010094 fflush(stderr);
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +020095 }
Petr Baudisf8ff0c02005-09-22 11:25:28 +020096}
97
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080098__attribute__((format (printf, 1, 2)))
Pavel Roskincdda4742005-09-29 16:53:14 -040099static void logerror(const char *err, ...)
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200100{
101 va_list params;
102 va_start(params, err);
Petr Baudis9048fe12005-09-24 16:13:01 +0200103 logreport(LOG_ERR, err, params);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200104 va_end(params);
105}
106
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800107__attribute__((format (printf, 1, 2)))
Pavel Roskincdda4742005-09-29 16:53:14 -0400108static void loginfo(const char *err, ...)
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200109{
110 va_list params;
111 if (!verbose)
112 return;
113 va_start(params, err);
Petr Baudis9048fe12005-09-24 16:13:01 +0200114 logreport(LOG_INFO, err, params);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200115 va_end(params);
116}
117
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +0200118static void NORETURN daemon_die(const char *err, va_list params)
119{
120 logreport(LOG_ERR, err, params);
121 exit(1);
122}
123
René Scharfedc8edc82015-02-15 19:33:52 +0100124struct expand_path_context {
125 const char *directory;
René Scharfe01cec542015-03-07 11:50:37 +0100126 struct hostinfo *hostinfo;
René Scharfedc8edc82015-02-15 19:33:52 +0100127};
128
129static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
130{
131 struct expand_path_context *context = ctx;
René Scharfe01cec542015-03-07 11:50:37 +0100132 struct hostinfo *hi = context->hostinfo;
René Scharfedc8edc82015-02-15 19:33:52 +0100133
134 switch (placeholder[0]) {
135 case 'H':
René Scharfe01cec542015-03-07 11:50:37 +0100136 strbuf_addbuf(sb, &hi->hostname);
René Scharfedc8edc82015-02-15 19:33:52 +0100137 return 1;
138 case 'C':
139 if (placeholder[1] == 'H') {
René Scharfe01cec542015-03-07 11:50:37 +0100140 strbuf_addstr(sb, get_canon_hostname(hi));
René Scharfedc8edc82015-02-15 19:33:52 +0100141 return 2;
142 }
143 break;
144 case 'I':
145 if (placeholder[1] == 'P') {
René Scharfe01cec542015-03-07 11:50:37 +0100146 strbuf_addstr(sb, get_ip_address(hi));
René Scharfedc8edc82015-02-15 19:33:52 +0100147 return 2;
148 }
149 break;
150 case 'P':
René Scharfe01cec542015-03-07 11:50:37 +0100151 strbuf_addbuf(sb, &hi->tcp_port);
René Scharfedc8edc82015-02-15 19:33:52 +0100152 return 1;
153 case 'D':
154 strbuf_addstr(sb, context->directory);
155 return 1;
156 }
157 return 0;
158}
159
René Scharfe01cec542015-03-07 11:50:37 +0100160static const char *path_ok(const char *directory, struct hostinfo *hi)
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700161{
Junio C Hamano603968d2006-02-04 22:27:29 -0800162 static char rpath[PATH_MAX];
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500163 static char interp_path[PATH_MAX];
Erik Faye-Lund1c64b482011-10-04 16:02:00 -0400164 const char *path;
Jeff King1055a892014-06-18 15:41:58 -0400165 const char *dir;
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500166
René Scharfe9d7ca662008-11-23 00:15:01 +0100167 dir = directory;
Junio C Hamanod79374c2005-12-03 01:45:57 -0800168
Shawn O. Pearce34b6cb82009-11-09 11:26:43 -0800169 if (daemon_avoid_alias(dir)) {
Junio C Hamanod79374c2005-12-03 01:45:57 -0800170 logerror("'%s': aliased", dir);
171 return NULL;
172 }
173
Junio C Hamano603968d2006-02-04 22:27:29 -0800174 if (*dir == '~') {
175 if (!user_path) {
176 logerror("'%s': User-path not allowed", dir);
177 return NULL;
178 }
179 if (*user_path) {
180 /* Got either "~alice" or "~alice/foo";
181 * rewrite them to "~alice/%s" or
182 * "~alice/%s/foo".
183 */
184 int namlen, restlen = strlen(dir);
Jeff King1055a892014-06-18 15:41:58 -0400185 const char *slash = strchr(dir, '/');
Junio C Hamano603968d2006-02-04 22:27:29 -0800186 if (!slash)
187 slash = dir + restlen;
188 namlen = slash - dir;
189 restlen -= namlen;
190 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
191 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
192 namlen, dir, user_path, restlen, slash);
193 dir = rpath;
194 }
195 }
René Scharfe01cec542015-03-07 11:50:37 +0100196 else if (interpolated_path && hi->saw_extended_args) {
René Scharfe9d7ca662008-11-23 00:15:01 +0100197 struct strbuf expanded_path = STRBUF_INIT;
René Scharfedc8edc82015-02-15 19:33:52 +0100198 struct expand_path_context context;
René Scharfe9d7ca662008-11-23 00:15:01 +0100199
René Scharfedc8edc82015-02-15 19:33:52 +0100200 context.directory = directory;
René Scharfe01cec542015-03-07 11:50:37 +0100201 context.hostinfo = hi;
René Scharfedc8edc82015-02-15 19:33:52 +0100202
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500203 if (*dir != '/') {
204 /* Allow only absolute */
205 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
206 return NULL;
207 }
208
René Scharfe9d7ca662008-11-23 00:15:01 +0100209 strbuf_expand(&expanded_path, interpolated_path,
René Scharfedc8edc82015-02-15 19:33:52 +0100210 expand_path, &context);
René Scharfe9d7ca662008-11-23 00:15:01 +0100211 strlcpy(interp_path, expanded_path.buf, PATH_MAX);
212 strbuf_release(&expanded_path);
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500213 loginfo("Interpolated dir '%s'", interp_path);
214
215 dir = interp_path;
216 }
Junio C Hamano603968d2006-02-04 22:27:29 -0800217 else if (base_path) {
218 if (*dir != '/') {
219 /* Allow only absolute */
Mark Wooding1fda3d52006-02-03 20:27:02 +0000220 logerror("'%s': Non-absolute path denied (base-path active)", dir);
Petr Baudisb21c31c2005-12-23 02:27:40 +0100221 return NULL;
222 }
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500223 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
224 dir = rpath;
Petr Baudisb21c31c2005-12-23 02:27:40 +0100225 }
226
René Scharfea5839712008-12-26 11:01:57 +0100227 path = enter_repo(dir, strict_paths);
228 if (!path && base_path && base_path_relaxed) {
Jens Axboe73a7a652007-07-27 14:00:29 -0700229 /*
230 * if we fail and base_path_relaxed is enabled, try without
231 * prefixing the base path
232 */
René Scharfea5839712008-12-26 11:01:57 +0100233 dir = directory;
234 path = enter_repo(dir, strict_paths);
235 }
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700236
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100237 if (!path) {
Jeff King05ac6b32009-03-04 03:32:29 -0500238 logerror("'%s' does not appear to be a git repository", dir);
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100239 return NULL;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700240 }
241
242 if ( ok_paths && *ok_paths ) {
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800243 char **pp;
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100244 int pathlen = strlen(path);
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700245
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800246 /* The validation is done on the paths after enter_repo
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700247 * appends optional {.git,.git/.git} and friends, but
Junio C Hamanod79374c2005-12-03 01:45:57 -0800248 * it does not use getcwd(). So if your /pub is
249 * a symlink to /mnt/pub, you can whitelist /pub and
250 * do not have to say /mnt/pub.
251 * Do not say /pub/.
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800252 */
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700253 for ( pp = ok_paths ; *pp ; pp++ ) {
254 int len = strlen(*pp);
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800255 if (len <= pathlen &&
256 !memcmp(*pp, path, len) &&
257 (path[len] == '\0' ||
258 (!strict_paths && path[len] == '/')))
259 return path;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700260 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100261 }
262 else {
263 /* be backwards compatible */
264 if (!strict_paths)
265 return path;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700266 }
267
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100268 logerror("'%s': not in whitelist", path);
269 return NULL; /* Fallthrough. Deny by default */
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700270}
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700271
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700272typedef int (*daemon_service_fn)(void);
273struct daemon_service {
274 const char *name;
275 const char *config_name;
276 daemon_service_fn fn;
277 int enabled;
278 int overridable;
279};
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700280
Jeff Kingd5570f42011-10-14 17:19:21 -0400281static int daemon_error(const char *dir, const char *msg)
282{
283 if (!informative_errors)
284 msg = "access denied or repository not exported";
285 packet_write(1, "ERR %s: %s", msg, dir);
286 return -1;
287}
288
Jeff King1055a892014-06-18 15:41:58 -0400289static const char *access_hook;
Junio C Hamano93741e42012-08-14 11:37:51 -0700290
René Scharfe01cec542015-03-07 11:50:37 +0100291static int run_access_hook(struct daemon_service *service, const char *dir,
292 const char *path, struct hostinfo *hi)
Junio C Hamano93741e42012-08-14 11:37:51 -0700293{
René Scharfed3180272014-08-19 21:09:35 +0200294 struct child_process child = CHILD_PROCESS_INIT;
Junio C Hamano93741e42012-08-14 11:37:51 -0700295 struct strbuf buf = STRBUF_INIT;
296 const char *argv[8];
297 const char **arg = argv;
298 char *eol;
299 int seen_errors = 0;
300
Junio C Hamano93741e42012-08-14 11:37:51 -0700301 *arg++ = access_hook;
302 *arg++ = service->name;
303 *arg++ = path;
René Scharfe01cec542015-03-07 11:50:37 +0100304 *arg++ = hi->hostname.buf;
305 *arg++ = get_canon_hostname(hi);
306 *arg++ = get_ip_address(hi);
307 *arg++ = hi->tcp_port.buf;
Junio C Hamano93741e42012-08-14 11:37:51 -0700308 *arg = NULL;
Junio C Hamano93741e42012-08-14 11:37:51 -0700309
Junio C Hamano93741e42012-08-14 11:37:51 -0700310 child.use_shell = 1;
311 child.argv = argv;
312 child.no_stdin = 1;
313 child.no_stderr = 1;
314 child.out = -1;
315 if (start_command(&child)) {
316 logerror("daemon access hook '%s' failed to start",
317 access_hook);
318 goto error_return;
319 }
320 if (strbuf_read(&buf, child.out, 0) < 0) {
321 logerror("failed to read from pipe to daemon access hook '%s'",
322 access_hook);
323 strbuf_reset(&buf);
324 seen_errors = 1;
325 }
326 if (close(child.out) < 0) {
327 logerror("failed to close pipe to daemon access hook '%s'",
328 access_hook);
329 seen_errors = 1;
330 }
331 if (finish_command(&child))
332 seen_errors = 1;
333
334 if (!seen_errors) {
335 strbuf_release(&buf);
336 return 0;
337 }
338
339error_return:
340 strbuf_ltrim(&buf);
341 if (!buf.len)
342 strbuf_addstr(&buf, "service rejected");
343 eol = strchr(buf.buf, '\n');
344 if (eol)
345 *eol = '\0';
346 errno = EACCES;
347 daemon_error(dir, buf.buf);
348 strbuf_release(&buf);
349 return -1;
350}
351
René Scharfe01cec542015-03-07 11:50:37 +0100352static int run_service(const char *dir, struct daemon_service *service,
353 struct hostinfo *hi)
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700354{
355 const char *path;
356 int enabled = service->enabled;
Tanay Abhra8939d322014-08-07 09:21:16 -0700357 struct strbuf var = STRBUF_INIT;
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700358
René Scharfea47551c2008-11-23 00:21:52 +0100359 loginfo("Request %s for '%s'", service->name, dir);
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700360
361 if (!enabled && !service->overridable) {
362 logerror("'%s': service not enabled.", service->name);
363 errno = EACCES;
Jeff Kingd5570f42011-10-14 17:19:21 -0400364 return daemon_error(dir, "service not enabled");
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700365 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100366
René Scharfe01cec542015-03-07 11:50:37 +0100367 if (!(path = path_ok(dir, hi)))
Jeff Kingd5570f42011-10-14 17:19:21 -0400368 return daemon_error(dir, "no such repository");
H. Peter Anvin47888f02005-09-27 08:49:40 -0700369
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700370 /*
371 * Security on the cheap.
372 *
Junio C Hamanoa935c392005-10-20 23:19:36 -0700373 * We want a readable HEAD, usable "objects" directory, and
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700374 * a "git-daemon-export-ok" flag that says that the other side
375 * is ok with us doing this.
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100376 *
377 * path_ok() uses enter_repo() and does whitelist checking.
378 * We only need to make sure the repository is exported.
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700379 */
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100380
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700381 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100382 logerror("'%s': repository not exported.", path);
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700383 errno = EACCES;
Jeff Kingd5570f42011-10-14 17:19:21 -0400384 return daemon_error(dir, "repository not exported");
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700385 }
386
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700387 if (service->overridable) {
Tanay Abhra8939d322014-08-07 09:21:16 -0700388 strbuf_addf(&var, "daemon.%s", service->config_name);
389 git_config_get_bool(var.buf, &enabled);
390 strbuf_release(&var);
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700391 }
392 if (!enabled) {
393 logerror("'%s': service not enabled for '%s'",
394 service->name, path);
395 errno = EACCES;
Jeff Kingd5570f42011-10-14 17:19:21 -0400396 return daemon_error(dir, "service not enabled");
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700397 }
398
Linus Torvalds02d57da2005-07-15 22:53:31 -0700399 /*
Junio C Hamano93741e42012-08-14 11:37:51 -0700400 * Optionally, a hook can choose to deny access to the
401 * repository depending on the phase of the moon.
402 */
René Scharfe01cec542015-03-07 11:50:37 +0100403 if (access_hook && run_access_hook(service, dir, path, hi))
Junio C Hamano93741e42012-08-14 11:37:51 -0700404 return -1;
405
406 /*
Linus Torvalds02d57da2005-07-15 22:53:31 -0700407 * We'll ignore SIGTERM from now on, we have a
408 * good client.
409 */
410 signal(SIGTERM, SIG_IGN);
411
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700412 return service->fn();
413}
414
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200415static void copy_to_log(int fd)
416{
417 struct strbuf line = STRBUF_INIT;
418 FILE *fp;
419
420 fp = fdopen(fd, "r");
421 if (fp == NULL) {
422 logerror("fdopen of error channel failed");
423 close(fd);
424 return;
425 }
426
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800427 while (strbuf_getline_lf(&line, fp) != EOF) {
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200428 logerror("%s", line.buf);
429 strbuf_setlen(&line, 0);
430 }
431
432 strbuf_release(&line);
433 fclose(fp);
434}
435
436static int run_service_command(const char **argv)
437{
René Scharfed3180272014-08-19 21:09:35 +0200438 struct child_process cld = CHILD_PROCESS_INIT;
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200439
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200440 cld.argv = argv;
441 cld.git_cmd = 1;
442 cld.err = -1;
443 if (start_command(&cld))
444 return -1;
445
446 close(0);
447 close(1);
448
449 copy_to_log(cld.err);
450
451 return finish_command(&cld);
452}
453
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700454static int upload_pack(void)
455{
456 /* Timeout as string */
457 char timeout_buf[64];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000458 const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
459
460 argv[2] = timeout_buf;
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700461
H. Peter Anvin960decc2005-10-19 14:27:01 -0700462 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200463 return run_service_command(argv);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700464}
465
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200466static int upload_archive(void)
467{
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200468 static const char *argv[] = { "upload-archive", ".", NULL };
469 return run_service_command(argv);
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200470}
471
Linus Torvalds4b3b1e12007-01-21 11:04:13 -0800472static int receive_pack(void)
473{
Johannes Sixt5d87dd42009-06-14 22:38:51 +0200474 static const char *argv[] = { "receive-pack", ".", NULL };
475 return run_service_command(argv);
Linus Torvalds4b3b1e12007-01-21 11:04:13 -0800476}
477
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700478static struct daemon_service daemon_service[] = {
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200479 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700480 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
Linus Torvalds4b3b1e12007-01-21 11:04:13 -0800481 { "receive-pack", "receivepack", receive_pack, 0, 1 },
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700482};
483
Junio C Hamanof3fa1832007-11-08 15:35:32 -0800484static void enable_service(const char *name, int ena)
485{
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700486 int i;
487 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
488 if (!strcmp(daemon_service[i].name, name)) {
489 daemon_service[i].enabled = ena;
490 return;
491 }
492 }
493 die("No such service %s", name);
494}
495
Junio C Hamanof3fa1832007-11-08 15:35:32 -0800496static void make_service_overridable(const char *name, int ena)
497{
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700498 int i;
499 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
500 if (!strcmp(daemon_service[i].name, name)) {
501 daemon_service[i].overridable = ena;
502 return;
503 }
504 }
505 die("No such service %s", name);
506}
507
Ilari Liusvaarae8dbd762010-01-26 20:24:41 +0200508static void parse_host_and_port(char *hostport, char **host,
509 char **port)
510{
511 if (*hostport == '[') {
512 char *end;
513
514 end = strchr(hostport, ']');
515 if (!end)
Junio C Hamano9517e6b2010-02-03 21:23:18 -0800516 die("Invalid request ('[' without ']')");
Ilari Liusvaarae8dbd762010-01-26 20:24:41 +0200517 *end = '\0';
518 *host = hostport + 1;
519 if (!end[1])
520 *port = NULL;
521 else if (end[1] == ':')
522 *port = end + 2;
523 else
524 die("Garbage after end of host part");
525 } else {
526 *host = hostport;
527 *port = strrchr(hostport, ':');
528 if (*port) {
Imre Deake9bd3232010-03-20 04:23:58 +0200529 **port = '\0';
Ilari Liusvaarae8dbd762010-01-26 20:24:41 +0200530 ++*port;
531 }
532 }
533}
534
Jon Loeligereb30aed2006-09-27 11:16:10 -0500535/*
Jeff Kingb4853732015-02-17 14:09:24 -0500536 * Sanitize a string from the client so that it's OK to be inserted into a
537 * filesystem path. Specifically, we disallow slashes, runs of "..", and
538 * trailing and leading dots, which means that the client cannot escape
539 * our base path via ".." traversal.
540 */
René Scharfe7a646ce2015-03-07 11:50:29 +0100541static void sanitize_client(struct strbuf *out, const char *in)
Jeff Kingb4853732015-02-17 14:09:24 -0500542{
543 for (; *in; in++) {
544 if (*in == '/')
545 continue;
546 if (*in == '.' && (!out->len || out->buf[out->len - 1] == '.'))
547 continue;
548 strbuf_addch(out, *in);
549 }
550
551 while (out->len && out->buf[out->len - 1] == '.')
552 strbuf_setlen(out, out->len - 1);
553}
554
Jeff Kingb4853732015-02-17 14:09:24 -0500555/*
556 * Like sanitize_client, but we also perform any canonicalization
557 * to make life easier on the admin.
558 */
René Scharfe7a646ce2015-03-07 11:50:29 +0100559static void canonicalize_client(struct strbuf *out, const char *in)
Jeff Kingb4853732015-02-17 14:09:24 -0500560{
René Scharfe7a646ce2015-03-07 11:50:29 +0100561 sanitize_client(out, in);
562 strbuf_tolower(out);
Jeff Kingb4853732015-02-17 14:09:24 -0500563}
564
565/*
Shawn O. Pearce73bb33a2009-06-04 18:33:32 -0700566 * Read the host as supplied by the client connection.
Jon Loeligereb30aed2006-09-27 11:16:10 -0500567 */
René Scharfe01cec542015-03-07 11:50:37 +0100568static void parse_host_arg(struct hostinfo *hi, char *extra_args, int buflen)
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500569{
570 char *val;
571 int vallen;
572 char *end = extra_args + buflen;
573
Shawn O. Pearce73bb33a2009-06-04 18:33:32 -0700574 if (extra_args < end && *extra_args) {
René Scharfe01cec542015-03-07 11:50:37 +0100575 hi->saw_extended_args = 1;
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500576 if (strncasecmp("host=", extra_args, 5) == 0) {
577 val = extra_args + 5;
578 vallen = strlen(val) + 1;
579 if (*val) {
Jon Loeligereb30aed2006-09-27 11:16:10 -0500580 /* Split <host>:<port> at colon. */
Ilari Liusvaarae8dbd762010-01-26 20:24:41 +0200581 char *host;
582 char *port;
583 parse_host_and_port(val, &host, &port);
René Scharfe01cec542015-03-07 11:50:37 +0100584 if (port)
585 sanitize_client(&hi->tcp_port, port);
586 canonicalize_client(&hi->hostname, host);
587 hi->hostname_lookup_done = 0;
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500588 }
Jon Loeligereb30aed2006-09-27 11:16:10 -0500589
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500590 /* On to the next one */
591 extra_args = val + vallen;
592 }
Shawn O. Pearce73bb33a2009-06-04 18:33:32 -0700593 if (extra_args < end && *extra_args)
594 die("Invalid request");
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500595 }
René Scharfeedef9532015-02-15 19:31:41 +0100596}
Jon Loeligerdd467622006-09-26 09:47:43 -0500597
René Scharfeedef9532015-02-15 19:31:41 +0100598/*
599 * Locate canonical hostname and its IP address.
600 */
René Scharfe01cec542015-03-07 11:50:37 +0100601static void lookup_hostname(struct hostinfo *hi)
René Scharfeedef9532015-02-15 19:31:41 +0100602{
René Scharfe01cec542015-03-07 11:50:37 +0100603 if (!hi->hostname_lookup_done && hi->hostname.len) {
Jon Loeligerdd467622006-09-26 09:47:43 -0500604#ifndef NO_IPV6
Jon Loeligerdd467622006-09-26 09:47:43 -0500605 struct addrinfo hints;
Benjamin Kramer3e8a00a2009-04-27 15:59:49 +0200606 struct addrinfo *ai;
Jon Loeligerdd467622006-09-26 09:47:43 -0500607 int gai;
608 static char addrbuf[HOST_NAME_MAX + 1];
609
610 memset(&hints, 0, sizeof(hints));
611 hints.ai_flags = AI_CANONNAME;
612
René Scharfe01cec542015-03-07 11:50:37 +0100613 gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
Jon Loeligerdd467622006-09-26 09:47:43 -0500614 if (!gai) {
Benjamin Kramer3e8a00a2009-04-27 15:59:49 +0200615 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
Jon Loeligerdd467622006-09-26 09:47:43 -0500616
Benjamin Kramer3e8a00a2009-04-27 15:59:49 +0200617 inet_ntop(AF_INET, &sin_addr->sin_addr,
618 addrbuf, sizeof(addrbuf));
René Scharfe01cec542015-03-07 11:50:37 +0100619 strbuf_addstr(&hi->ip_address, addrbuf);
Benjamin Kramer3e8a00a2009-04-27 15:59:49 +0200620
René Scharfe7a646ce2015-03-07 11:50:29 +0100621 if (ai->ai_canonname)
René Scharfe01cec542015-03-07 11:50:37 +0100622 sanitize_client(&hi->canon_hostname,
René Scharfe7a646ce2015-03-07 11:50:29 +0100623 ai->ai_canonname);
624 else
René Scharfe01cec542015-03-07 11:50:37 +0100625 strbuf_addbuf(&hi->canon_hostname,
626 &hi->ip_address);
Benjamin Kramer3e8a00a2009-04-27 15:59:49 +0200627
628 freeaddrinfo(ai);
Jon Loeligerdd467622006-09-26 09:47:43 -0500629 }
Jon Loeligerdd467622006-09-26 09:47:43 -0500630#else
Jon Loeligerdd467622006-09-26 09:47:43 -0500631 struct hostent *hent;
632 struct sockaddr_in sa;
633 char **ap;
634 static char addrbuf[HOST_NAME_MAX + 1];
635
Junio C Hamanod358f772015-05-05 11:03:24 -0700636 hent = gethostbyname(hi->hostname.buf);
René Scharfeeb6c4032014-10-01 12:16:17 +0200637 if (hent) {
638 ap = hent->h_addr_list;
639 memset(&sa, 0, sizeof sa);
640 sa.sin_family = hent->h_addrtype;
641 sa.sin_port = htons(0);
642 memcpy(&sa.sin_addr, *ap, hent->h_length);
Jon Loeligerdd467622006-09-26 09:47:43 -0500643
René Scharfeeb6c4032014-10-01 12:16:17 +0200644 inet_ntop(hent->h_addrtype, &sa.sin_addr,
645 addrbuf, sizeof(addrbuf));
Jon Loeligerdd467622006-09-26 09:47:43 -0500646
René Scharfe01cec542015-03-07 11:50:37 +0100647 sanitize_client(&hi->canon_hostname, hent->h_name);
648 strbuf_addstr(&hi->ip_address, addrbuf);
René Scharfeeb6c4032014-10-01 12:16:17 +0200649 }
Jon Loeligerdd467622006-09-26 09:47:43 -0500650#endif
René Scharfe01cec542015-03-07 11:50:37 +0100651 hi->hostname_lookup_done = 1;
René Scharfe6720e952008-12-26 11:12:15 +0100652 }
Jon Loeligerdd467622006-09-26 09:47:43 -0500653}
654
René Scharfe01cec542015-03-07 11:50:37 +0100655static void hostinfo_init(struct hostinfo *hi)
656{
657 memset(hi, 0, sizeof(*hi));
658 strbuf_init(&hi->hostname, 0);
659 strbuf_init(&hi->canon_hostname, 0);
660 strbuf_init(&hi->ip_address, 0);
661 strbuf_init(&hi->tcp_port, 0);
662}
663
664static void hostinfo_clear(struct hostinfo *hi)
665{
666 strbuf_release(&hi->hostname);
667 strbuf_release(&hi->canon_hostname);
668 strbuf_release(&hi->ip_address);
669 strbuf_release(&hi->tcp_port);
670}
Jon Loeligerdd467622006-09-26 09:47:43 -0500671
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100672static int execute(void)
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700673{
Jeff King74543a02013-02-20 15:02:57 -0500674 char *line = packet_buffer;
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700675 int pktlen, len, i;
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100676 char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
René Scharfe01cec542015-03-07 11:50:37 +0100677 struct hostinfo hi;
678
679 hostinfo_init(&hi);
Linus Torvalds7d806942005-07-15 09:27:05 -0700680
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100681 if (addr)
682 loginfo("Connection from %s:%s", addr, port);
David Woodhouse5b276ee2006-06-20 15:38:13 +0100683
H. Peter Anvin960decc2005-10-19 14:27:01 -0700684 alarm(init_timeout ? init_timeout : timeout);
Jeff King4981fe72013-02-23 17:31:34 -0500685 pktlen = packet_read(0, NULL, NULL, packet_buffer, sizeof(packet_buffer), 0);
H. Peter Anvin960decc2005-10-19 14:27:01 -0700686 alarm(0);
Linus Torvalds7d806942005-07-15 09:27:05 -0700687
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500688 len = strlen(line);
689 if (pktlen != len)
690 loginfo("Extended attributes (%d bytes) exist <%.*s>",
691 (int) pktlen - len,
692 (int) pktlen - len, line + len + 1);
Junio C Hamano83543a22006-10-23 18:26:05 -0700693 if (len && line[len-1] == '\n') {
Linus Torvalds7d806942005-07-15 09:27:05 -0700694 line[--len] = 0;
Junio C Hamano83543a22006-10-23 18:26:05 -0700695 pktlen--;
696 }
Linus Torvalds7d806942005-07-15 09:27:05 -0700697
René Scharfed433ed02008-11-23 00:19:09 +0100698 if (len != pktlen)
René Scharfe01cec542015-03-07 11:50:37 +0100699 parse_host_arg(&hi, line + len + 1, pktlen - len - 1);
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500700
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700701 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
702 struct daemon_service *s = &(daemon_service[i]);
Jeff Kingd12c24d2014-06-18 15:49:44 -0400703 const char *arg;
704
705 if (skip_prefix(line, "git-", &arg) &&
706 skip_prefix(arg, s->name, &arg) &&
707 *arg++ == ' ') {
Jon Loeligereb30aed2006-09-27 11:16:10 -0500708 /*
709 * Note: The directory here is probably context sensitive,
710 * and might depend on the actual service being performed.
711 */
René Scharfe01cec542015-03-07 11:50:37 +0100712 int rc = run_service(arg, s, &hi);
713 hostinfo_clear(&hi);
714 return rc;
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500715 }
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700716 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700717
René Scharfe01cec542015-03-07 11:50:37 +0100718 hostinfo_clear(&hi);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200719 logerror("Protocol error: '%s'", line);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700720 return -1;
721}
722
Erik Faye-Lund15515b72010-01-09 15:13:28 +0100723static int addrcmp(const struct sockaddr_storage *s1,
724 const struct sockaddr_storage *s2)
725{
Brandon Casey3aff8742010-03-15 17:10:06 -0500726 const struct sockaddr *sa1 = (const struct sockaddr*) s1;
727 const struct sockaddr *sa2 = (const struct sockaddr*) s2;
728
729 if (sa1->sa_family != sa2->sa_family)
730 return sa1->sa_family - sa2->sa_family;
731 if (sa1->sa_family == AF_INET)
Erik Faye-Lund15515b72010-01-09 15:13:28 +0100732 return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
733 &((struct sockaddr_in *)s2)->sin_addr,
734 sizeof(struct in_addr));
735#ifndef NO_IPV6
Brandon Casey3aff8742010-03-15 17:10:06 -0500736 if (sa1->sa_family == AF_INET6)
Erik Faye-Lund15515b72010-01-09 15:13:28 +0100737 return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
738 &((struct sockaddr_in6 *)s2)->sin6_addr,
739 sizeof(struct in6_addr));
740#endif
741 return 0;
742}
743
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200744static int max_connections = 32;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700745
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200746static unsigned int live_children;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700747
Linus Torvalds4d8fa912005-08-01 12:11:53 -0700748static struct child {
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200749 struct child *next;
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100750 struct child_process cld;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400751 struct sockaddr_storage address;
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200752} *firstborn;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700753
Erik Faye-Lundc295cf02010-11-04 02:35:22 +0100754static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
Linus Torvalds66e631d2005-07-15 21:51:57 -0700755{
Junio C Hamano460c2012008-08-24 13:27:10 -0700756 struct child *newborn, **cradle;
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200757
Junio C Hamano460c2012008-08-24 13:27:10 -0700758 newborn = xcalloc(1, sizeof(*newborn));
759 live_children++;
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100760 memcpy(&newborn->cld, cld, sizeof(*cld));
Junio C Hamano460c2012008-08-24 13:27:10 -0700761 memcpy(&newborn->address, addr, addrlen);
762 for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
Erik Faye-Lund15515b72010-01-09 15:13:28 +0100763 if (!addrcmp(&(*cradle)->address, &newborn->address))
Junio C Hamano460c2012008-08-24 13:27:10 -0700764 break;
765 newborn->next = *cradle;
766 *cradle = newborn;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700767}
768
Linus Torvalds66e631d2005-07-15 21:51:57 -0700769/*
770 * This gets called if the number of connections grows
771 * past "max_connections".
772 *
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200773 * We kill the newest connection from a duplicate IP.
Linus Torvalds66e631d2005-07-15 21:51:57 -0700774 */
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200775static void kill_some_child(void)
Linus Torvalds66e631d2005-07-15 21:51:57 -0700776{
Junio C Hamano460c2012008-08-24 13:27:10 -0700777 const struct child *blanket, *next;
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200778
Junio C Hamano460c2012008-08-24 13:27:10 -0700779 if (!(blanket = firstborn))
780 return;
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200781
Junio C Hamano460c2012008-08-24 13:27:10 -0700782 for (; (next = blanket->next); blanket = next)
Erik Faye-Lund15515b72010-01-09 15:13:28 +0100783 if (!addrcmp(&blanket->address, &next->address)) {
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100784 kill(blanket->cld.pid, SIGTERM);
Junio C Hamano460c2012008-08-24 13:27:10 -0700785 break;
786 }
Linus Torvalds66e631d2005-07-15 21:51:57 -0700787}
788
Johannes Schindelina5a91262008-07-03 16:27:24 +0100789static void check_dead_children(void)
790{
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200791 int status;
792 pid_t pid;
Johannes Schindelina5a91262008-07-03 16:27:24 +0100793
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100794 struct child **cradle, *blanket;
795 for (cradle = &firstborn; (blanket = *cradle);)
796 if ((pid = waitpid(blanket->cld.pid, &status, WNOHANG)) > 1) {
797 const char *dead = "";
798 if (status)
799 dead = " (with error)";
800 loginfo("[%"PRIuMAX"] Disconnected%s", (uintmax_t)pid, dead);
801
802 /* remove the child */
803 *cradle = blanket->next;
804 live_children--;
René Scharfeb1b49ff2015-10-24 14:23:20 +0200805 child_process_clear(&blanket->cld);
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100806 free(blanket);
807 } else
808 cradle = &blanket->next;
Linus Torvalds02d57da2005-07-15 22:53:31 -0700809}
810
Jeff King850d2fe2016-02-22 17:44:21 -0500811static struct argv_array cld_argv = ARGV_ARRAY_INIT;
Erik Faye-Lundc295cf02010-11-04 02:35:22 +0100812static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
Linus Torvalds02d57da2005-07-15 22:53:31 -0700813{
René Scharfed3180272014-08-19 21:09:35 +0200814 struct child_process cld = CHILD_PROCESS_INIT;
Linus Torvalds02d57da2005-07-15 22:53:31 -0700815
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200816 if (max_connections && live_children >= max_connections) {
817 kill_some_child();
Junio C Hamano460c2012008-08-24 13:27:10 -0700818 sleep(1); /* give it some time to die */
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200819 check_dead_children();
820 if (live_children >= max_connections) {
821 close(incoming);
822 logerror("Too many children, dropping connection");
Linus Torvalds02d57da2005-07-15 22:53:31 -0700823 return;
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +0200824 }
825 }
Linus Torvalds02d57da2005-07-15 22:53:31 -0700826
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100827 if (addr->sa_family == AF_INET) {
Jeff Kingf063d382015-09-24 17:08:00 -0400828 char buf[128] = "";
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100829 struct sockaddr_in *sin_addr = (void *) addr;
Jeff Kingf063d382015-09-24 17:08:00 -0400830 inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf));
831 argv_array_pushf(&cld.env_array, "REMOTE_ADDR=%s", buf);
832 argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
833 ntohs(sin_addr->sin_port));
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100834#ifndef NO_IPV6
Stefan Beller5d9cfa22013-07-14 23:35:46 +0200835 } else if (addr->sa_family == AF_INET6) {
Jeff Kingf063d382015-09-24 17:08:00 -0400836 char buf[128] = "";
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100837 struct sockaddr_in6 *sin6_addr = (void *) addr;
Jeff Kingf063d382015-09-24 17:08:00 -0400838 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf));
839 argv_array_pushf(&cld.env_array, "REMOTE_ADDR=[%s]", buf);
840 argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
841 ntohs(sin6_addr->sin6_port));
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +0100842#endif
843 }
844
Jeff King850d2fe2016-02-22 17:44:21 -0500845 cld.argv = cld_argv.argv;
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100846 cld.in = incoming;
847 cld.out = dup(incoming);
Linus Torvalds02d57da2005-07-15 22:53:31 -0700848
Erik Faye-Lund30e15602010-11-04 02:35:16 +0100849 if (start_command(&cld))
850 logerror("unable to fork");
851 else
852 add_child(&cld, addr, addrlen);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700853}
854
Linus Torvaldseaa94912005-07-15 20:42:28 -0700855static void child_handler(int signo)
856{
Junio C Hamano460c2012008-08-24 13:27:10 -0700857 /*
858 * Otherwise empty handler because systemcalls will get interrupted
Stephen R. van den Berg695605b2008-08-14 20:02:20 +0200859 * upon signal receipt
860 * SysV needs the handler to be rearmed
861 */
Stephen R. van den Bergbd7b3712008-08-12 21:36:13 +0200862 signal(SIGCHLD, child_handler);
Linus Torvaldseaa94912005-07-15 20:42:28 -0700863}
864
Mark Wooding1955fab2006-02-03 20:27:04 +0000865static int set_reuse_addr(int sockfd)
866{
867 int on = 1;
868
869 if (!reuseaddr)
870 return 0;
871 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
872 &on, sizeof(on));
873}
874
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200875struct socketlist {
876 int *list;
877 size_t nr;
878 size_t alloc;
879};
880
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +1100881static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
882{
883#ifdef NO_IPV6
884 static char ip[INET_ADDRSTRLEN];
885#else
886 static char ip[INET6_ADDRSTRLEN];
887#endif
888
889 switch (family) {
890#ifndef NO_IPV6
891 case AF_INET6:
892 inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
893 break;
894#endif
895 case AF_INET:
896 inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
897 break;
898 default:
Jeff King5096d492015-09-24 17:06:08 -0400899 xsnprintf(ip, sizeof(ip), "<unknown>");
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +1100900 }
901 return ip;
902}
903
Peter Anvin6573faf2005-09-28 17:26:44 -0700904#ifndef NO_IPV6
905
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200906static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700907{
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200908 int socknum = 0;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400909 char pbuf[NI_MAXSERV];
Peter Anvin6573faf2005-09-28 17:26:44 -0700910 struct addrinfo hints, *ai0, *ai;
911 int gai;
Alexandre Julliard20276882007-02-14 18:10:26 +0100912 long flags;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400913
Jeff King5096d492015-09-24 17:06:08 -0400914 xsnprintf(pbuf, sizeof(pbuf), "%d", listen_port);
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400915 memset(&hints, 0, sizeof(hints));
916 hints.ai_family = AF_UNSPEC;
917 hints.ai_socktype = SOCK_STREAM;
918 hints.ai_protocol = IPPROTO_TCP;
919 hints.ai_flags = AI_PASSIVE;
920
Jon Loeligerdd467622006-09-26 09:47:43 -0500921 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200922 if (gai) {
923 logerror("getaddrinfo() for %s failed: %s", listen_addr, gai_strerror(gai));
924 return 0;
925 }
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400926
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400927 for (ai = ai0; ai; ai = ai->ai_next) {
928 int sockfd;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400929
930 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
931 if (sockfd < 0)
932 continue;
933 if (sockfd >= FD_SETSIZE) {
Stephen R. van den Bergdf0daf82008-08-14 20:02:20 +0200934 logerror("Socket descriptor too large");
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400935 close(sockfd);
936 continue;
937 }
938
939#ifdef IPV6_V6ONLY
940 if (ai->ai_family == AF_INET6) {
941 int on = 1;
942 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
943 &on, sizeof(on));
944 /* Note: error is not fatal */
945 }
946#endif
947
Mark Wooding1955fab2006-02-03 20:27:04 +0000948 if (set_reuse_addr(sockfd)) {
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +1100949 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
Mark Wooding1955fab2006-02-03 20:27:04 +0000950 close(sockfd);
Serge E. Hallyn0032d542006-04-18 08:11:06 -0500951 continue;
Mark Wooding1955fab2006-02-03 20:27:04 +0000952 }
953
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400954 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +1100955 logerror("Could not bind to %s: %s",
956 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
957 strerror(errno));
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400958 close(sockfd);
959 continue; /* not fatal */
960 }
961 if (listen(sockfd, 5) < 0) {
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +1100962 logerror("Could not listen to %s: %s",
963 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
964 strerror(errno));
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400965 close(sockfd);
966 continue; /* not fatal */
967 }
968
Alexandre Julliard20276882007-02-14 18:10:26 +0100969 flags = fcntl(sockfd, F_GETFD, 0);
970 if (flags >= 0)
971 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
972
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200973 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
974 socklist->list[socklist->nr++] = sockfd;
975 socknum++;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400976 }
977
978 freeaddrinfo(ai0);
979
Peter Anvin6573faf2005-09-28 17:26:44 -0700980 return socknum;
981}
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700982
Peter Anvin6573faf2005-09-28 17:26:44 -0700983#else /* NO_IPV6 */
984
Alexander Sulfrian2caa3212010-08-30 13:30:50 +0200985static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
Peter Anvin6573faf2005-09-28 17:26:44 -0700986{
987 struct sockaddr_in sin;
988 int sockfd;
Alexandre Julliard20276882007-02-14 18:10:26 +0100989 long flags;
Peter Anvin6573faf2005-09-28 17:26:44 -0700990
Jon Loeligerdd467622006-09-26 09:47:43 -0500991 memset(&sin, 0, sizeof sin);
992 sin.sin_family = AF_INET;
993 sin.sin_port = htons(listen_port);
994
995 if (listen_addr) {
996 /* Well, host better be an IP address here. */
997 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
998 return 0;
999 } else {
1000 sin.sin_addr.s_addr = htonl(INADDR_ANY);
1001 }
1002
Peter Anvin6573faf2005-09-28 17:26:44 -07001003 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1004 if (sockfd < 0)
1005 return 0;
1006
Mark Wooding1955fab2006-02-03 20:27:04 +00001007 if (set_reuse_addr(sockfd)) {
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +11001008 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
Mark Wooding1955fab2006-02-03 20:27:04 +00001009 close(sockfd);
1010 return 0;
1011 }
1012
Peter Anvin6573faf2005-09-28 17:26:44 -07001013 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
René Scharfe9d1b9aa2014-10-01 12:18:15 +02001014 logerror("Could not bind to %s: %s",
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +11001015 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1016 strerror(errno));
Peter Anvin6573faf2005-09-28 17:26:44 -07001017 close(sockfd);
1018 return 0;
1019 }
1020
Paul Sericef35230f2005-11-21 11:07:23 -06001021 if (listen(sockfd, 5) < 0) {
Nguyễn Thái Ngọc Duy089d82e2011-10-04 06:13:28 +11001022 logerror("Could not listen to %s: %s",
1023 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1024 strerror(errno));
Paul Sericef35230f2005-11-21 11:07:23 -06001025 close(sockfd);
1026 return 0;
1027 }
1028
Alexandre Julliard20276882007-02-14 18:10:26 +01001029 flags = fcntl(sockfd, F_GETFD, 0);
1030 if (flags >= 0)
1031 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
1032
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001033 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
1034 socklist->list[socklist->nr++] = sockfd;
Paul Sericef35230f2005-11-21 11:07:23 -06001035 return 1;
Peter Anvin6573faf2005-09-28 17:26:44 -07001036}
1037
1038#endif
1039
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02001040static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001041{
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02001042 if (!listen_addr->nr)
1043 setup_named_sock(NULL, listen_port, socklist);
1044 else {
1045 int i, socknum;
1046 for (i = 0; i < listen_addr->nr; i++) {
1047 socknum = setup_named_sock(listen_addr->items[i].string,
1048 listen_port, socklist);
1049
1050 if (socknum == 0)
1051 logerror("unable to allocate any listen sockets for host %s on port %u",
1052 listen_addr->items[i].string, listen_port);
1053 }
1054 }
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001055}
1056
1057static int service_loop(struct socketlist *socklist)
Peter Anvin6573faf2005-09-28 17:26:44 -07001058{
1059 struct pollfd *pfd;
1060 int i;
1061
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001062 pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
Peter Anvin6573faf2005-09-28 17:26:44 -07001063
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001064 for (i = 0; i < socklist->nr; i++) {
1065 pfd[i].fd = socklist->list[i];
Peter Anvin6573faf2005-09-28 17:26:44 -07001066 pfd[i].events = POLLIN;
1067 }
H. Peter Anvin92202822005-09-30 11:01:57 -07001068
1069 signal(SIGCHLD, child_handler);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001070
1071 for (;;) {
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -04001072 int i;
Peter Anvin6573faf2005-09-28 17:26:44 -07001073
Stephen R. van den Berg695605b2008-08-14 20:02:20 +02001074 check_dead_children();
1075
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001076 if (poll(pfd, socklist->nr, -1) < 0) {
Junio C Hamano1eef0b32005-07-26 13:26:52 -07001077 if (errno != EINTR) {
Stephen R. van den Bergdf0daf82008-08-14 20:02:20 +02001078 logerror("Poll failed, resuming: %s",
Junio C Hamano1eef0b32005-07-26 13:26:52 -07001079 strerror(errno));
1080 sleep(1);
1081 }
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -04001082 continue;
1083 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001084
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001085 for (i = 0; i < socklist->nr; i++) {
Peter Anvin6573faf2005-09-28 17:26:44 -07001086 if (pfd[i].revents & POLLIN) {
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +01001087 union {
1088 struct sockaddr sa;
1089 struct sockaddr_in sai;
1090#ifndef NO_IPV6
1091 struct sockaddr_in6 sai6;
1092#endif
1093 } ss;
Erik Faye-Lundc295cf02010-11-04 02:35:22 +01001094 socklen_t sslen = sizeof(ss);
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +01001095 int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -04001096 if (incoming < 0) {
1097 switch (errno) {
1098 case EAGAIN:
1099 case EINTR:
1100 case ECONNABORTED:
1101 continue;
1102 default:
Thomas Rastd824cbb2009-06-27 17:58:46 +02001103 die_errno("accept returned");
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -04001104 }
1105 }
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +01001106 handle(incoming, &ss.sa, sslen);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001107 }
1108 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001109 }
1110}
1111
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001112#ifdef NO_POSIX_GOODIES
1113
1114struct credentials;
1115
1116static void drop_privileges(struct credentials *cred)
1117{
1118 /* nothing */
1119}
1120
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001121static struct credentials *prepare_credentials(const char *user_name,
1122 const char *group_name)
1123{
1124 die("--user not supported on this platform");
1125}
1126
1127#else
1128
1129struct credentials {
1130 struct passwd *pass;
1131 gid_t gid;
1132};
1133
1134static void drop_privileges(struct credentials *cred)
1135{
1136 if (cred && (initgroups(cred->pass->pw_name, cred->gid) ||
1137 setgid (cred->gid) || setuid(cred->pass->pw_uid)))
1138 die("cannot drop privileges");
1139}
1140
1141static struct credentials *prepare_credentials(const char *user_name,
1142 const char *group_name)
1143{
1144 static struct credentials c;
1145
1146 c.pass = getpwnam(user_name);
1147 if (!c.pass)
1148 die("user not found - %s", user_name);
1149
1150 if (!group_name)
1151 c.gid = c.pass->pw_gid;
1152 else {
1153 struct group *group = getgrnam(group_name);
1154 if (!group)
1155 die("group not found - %s", group_name);
1156
1157 c.gid = group->gr_gid;
1158 }
1159
1160 return &c;
1161}
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001162#endif
Matthias Lederhofera5262762006-07-13 18:47:13 +02001163
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001164static int serve(struct string_list *listen_addr, int listen_port,
1165 struct credentials *cred)
Peter Anvin6573faf2005-09-28 17:26:44 -07001166{
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001167 struct socketlist socklist = { NULL, 0, 0 };
Junio C Hamano4ae22d92005-10-20 23:21:50 -07001168
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001169 socksetup(listen_addr, listen_port, &socklist);
1170 if (socklist.nr == 0)
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02001171 die("unable to allocate any listen sockets on port %u",
1172 listen_port);
Junio C Hamano4ae22d92005-10-20 23:21:50 -07001173
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001174 drop_privileges(cred);
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001175
Clemens Buchacherf6a34cf2012-01-07 12:42:46 +01001176 loginfo("Ready to rumble");
1177
Alexander Sulfrian2caa3212010-08-30 13:30:50 +02001178 return service_loop(&socklist);
Junio C Hamano4ae22d92005-10-20 23:21:50 -07001179}
Peter Anvin6573faf2005-09-28 17:26:44 -07001180
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001181int main(int argc, char **argv)
1182{
Jon Loeligerdd467622006-09-26 09:47:43 -05001183 int listen_port = 0;
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02001184 struct string_list listen_addr = STRING_LIST_INIT_NODUP;
Erik Faye-Lund30e15602010-11-04 02:35:16 +01001185 int serve_mode = 0, inetd_mode = 0;
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001186 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
Matthias Lederhofera5262762006-07-13 18:47:13 +02001187 int detach = 0;
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001188 struct credentials *cred = NULL;
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001189 int i;
1190
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +01001191 git_setup_gettext();
1192
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +01001193 git_extract_argv0_path(argv[0]);
1194
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001195 for (i = 1; i < argc; i++) {
1196 char *arg = argv[i];
Jeff Kingae021d82014-06-18 15:47:50 -04001197 const char *v;
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001198
Jeff Kingae021d82014-06-18 15:47:50 -04001199 if (skip_prefix(arg, "--listen=", &v)) {
1200 string_list_append(&listen_addr, xstrdup_tolower(v));
René Scharfe6720e952008-12-26 11:12:15 +01001201 continue;
Jon Loeligerdd467622006-09-26 09:47:43 -05001202 }
Jeff Kingae021d82014-06-18 15:47:50 -04001203 if (skip_prefix(arg, "--port=", &v)) {
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001204 char *end;
1205 unsigned long n;
Jeff Kingae021d82014-06-18 15:47:50 -04001206 n = strtoul(v, &end, 0);
1207 if (*v && !*end) {
Jon Loeligerdd467622006-09-26 09:47:43 -05001208 listen_port = n;
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001209 continue;
1210 }
1211 }
Erik Faye-Lund30e15602010-11-04 02:35:16 +01001212 if (!strcmp(arg, "--serve")) {
1213 serve_mode = 1;
1214 continue;
1215 }
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001216 if (!strcmp(arg, "--inetd")) {
1217 inetd_mode = 1;
Andreas Ericssona8883282005-11-17 00:38:29 +01001218 log_syslog = 1;
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001219 continue;
1220 }
Petr Baudisf8ff0c02005-09-22 11:25:28 +02001221 if (!strcmp(arg, "--verbose")) {
1222 verbose = 1;
1223 continue;
1224 }
Petr Baudis9048fe12005-09-24 16:13:01 +02001225 if (!strcmp(arg, "--syslog")) {
1226 log_syslog = 1;
Petr Baudis9048fe12005-09-24 16:13:01 +02001227 continue;
1228 }
H. Peter Anvin4ae95682005-09-26 19:10:55 -07001229 if (!strcmp(arg, "--export-all")) {
1230 export_all_trees = 1;
1231 continue;
1232 }
Jeff Kingae021d82014-06-18 15:47:50 -04001233 if (skip_prefix(arg, "--access-hook=", &v)) {
1234 access_hook = v;
Junio C Hamano93741e42012-08-14 11:37:51 -07001235 continue;
1236 }
Jeff Kingae021d82014-06-18 15:47:50 -04001237 if (skip_prefix(arg, "--timeout=", &v)) {
1238 timeout = atoi(v);
Andreas Ericssona8883282005-11-17 00:38:29 +01001239 continue;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001240 }
Jeff Kingae021d82014-06-18 15:47:50 -04001241 if (skip_prefix(arg, "--init-timeout=", &v)) {
1242 init_timeout = atoi(v);
Andreas Ericssona8883282005-11-17 00:38:29 +01001243 continue;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001244 }
Jeff Kingae021d82014-06-18 15:47:50 -04001245 if (skip_prefix(arg, "--max-connections=", &v)) {
1246 max_connections = atoi(v);
Stephen R. van den Berg3bd62c22008-08-14 20:02:20 +02001247 if (max_connections < 0)
1248 max_connections = 0; /* unlimited */
1249 continue;
1250 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +01001251 if (!strcmp(arg, "--strict-paths")) {
1252 strict_paths = 1;
1253 continue;
1254 }
Jeff Kingae021d82014-06-18 15:47:50 -04001255 if (skip_prefix(arg, "--base-path=", &v)) {
1256 base_path = v;
Petr Baudisb21c31c2005-12-23 02:27:40 +01001257 continue;
1258 }
Jens Axboe73a7a652007-07-27 14:00:29 -07001259 if (!strcmp(arg, "--base-path-relaxed")) {
1260 base_path_relaxed = 1;
1261 continue;
1262 }
Jeff Kingae021d82014-06-18 15:47:50 -04001263 if (skip_prefix(arg, "--interpolated-path=", &v)) {
1264 interpolated_path = v;
Jon Loeliger49ba83f2006-09-19 20:31:51 -05001265 continue;
1266 }
Mark Wooding1955fab2006-02-03 20:27:04 +00001267 if (!strcmp(arg, "--reuseaddr")) {
1268 reuseaddr = 1;
1269 continue;
1270 }
Junio C Hamano603968d2006-02-04 22:27:29 -08001271 if (!strcmp(arg, "--user-path")) {
1272 user_path = "";
1273 continue;
1274 }
Jeff Kingae021d82014-06-18 15:47:50 -04001275 if (skip_prefix(arg, "--user-path=", &v)) {
1276 user_path = v;
Junio C Hamano603968d2006-02-04 22:27:29 -08001277 continue;
1278 }
Jeff Kingae021d82014-06-18 15:47:50 -04001279 if (skip_prefix(arg, "--pid-file=", &v)) {
1280 pid_file = v;
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +02001281 continue;
1282 }
Matthias Lederhofera5262762006-07-13 18:47:13 +02001283 if (!strcmp(arg, "--detach")) {
1284 detach = 1;
1285 log_syslog = 1;
1286 continue;
1287 }
Jeff Kingae021d82014-06-18 15:47:50 -04001288 if (skip_prefix(arg, "--user=", &v)) {
1289 user_name = v;
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001290 continue;
1291 }
Jeff Kingae021d82014-06-18 15:47:50 -04001292 if (skip_prefix(arg, "--group=", &v)) {
1293 group_name = v;
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001294 continue;
1295 }
Jeff Kingae021d82014-06-18 15:47:50 -04001296 if (skip_prefix(arg, "--enable=", &v)) {
1297 enable_service(v, 1);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001298 continue;
1299 }
Jeff Kingae021d82014-06-18 15:47:50 -04001300 if (skip_prefix(arg, "--disable=", &v)) {
1301 enable_service(v, 0);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001302 continue;
1303 }
Jeff Kingae021d82014-06-18 15:47:50 -04001304 if (skip_prefix(arg, "--allow-override=", &v)) {
1305 make_service_overridable(v, 1);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001306 continue;
1307 }
Jeff Kingae021d82014-06-18 15:47:50 -04001308 if (skip_prefix(arg, "--forbid-override=", &v)) {
1309 make_service_overridable(v, 0);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001310 continue;
1311 }
Nguyễn Thái Ngọc Duy82246b72013-12-20 17:53:52 +07001312 if (!strcmp(arg, "--informative-errors")) {
Jeff Kingd5570f42011-10-14 17:19:21 -04001313 informative_errors = 1;
1314 continue;
1315 }
Nguyễn Thái Ngọc Duy82246b72013-12-20 17:53:52 +07001316 if (!strcmp(arg, "--no-informative-errors")) {
Jeff Kingd5570f42011-10-14 17:19:21 -04001317 informative_errors = 0;
1318 continue;
1319 }
H. Peter Anvin4ae95682005-09-26 19:10:55 -07001320 if (!strcmp(arg, "--")) {
1321 ok_paths = &argv[i+1];
1322 break;
1323 } else if (arg[0] != '-') {
1324 ok_paths = &argv[i];
1325 break;
1326 }
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001327
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001328 usage(daemon_usage);
1329 }
1330
Johannes Sixt22665bb2008-02-25 14:25:20 +01001331 if (log_syslog) {
Stephen R. van den Berg6a992e92008-08-14 20:02:20 +02001332 openlog("git-daemon", LOG_PID, LOG_DAEMON);
Johannes Sixt22665bb2008-02-25 14:25:20 +01001333 set_die_routine(daemon_die);
Junio C Hamano460c2012008-08-24 13:27:10 -07001334 } else
Junio C Hamano48196af2008-09-03 20:33:29 -07001335 /* avoid splitting a message in the middle */
Erik Faye-Lund48cfaea2010-11-04 02:35:17 +01001336 setvbuf(stderr, NULL, _IOFBF, 4096);
Johannes Sixt22665bb2008-02-25 14:25:20 +01001337
Erik Faye-Lund9cddf562010-11-04 02:35:23 +01001338 if (inetd_mode && (detach || group_name || user_name))
1339 die("--detach, --user and --group are incompatible with --inetd");
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001340
Alexander Sulfrian3a3a29c2010-08-30 13:30:51 +02001341 if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
Jon Loeligerdd467622006-09-26 09:47:43 -05001342 die("--listen= and --port= are incompatible with --inetd");
1343 else if (listen_port == 0)
1344 listen_port = DEFAULT_GIT_PORT;
1345
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001346 if (group_name && !user_name)
1347 die("--group supplied without --user");
1348
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001349 if (user_name)
1350 cred = prepare_credentials(user_name, group_name);
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001351
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +02001352 if (strict_paths && (!ok_paths || !*ok_paths))
1353 die("option --strict-paths requires a whitelist");
1354
Junio C Hamano90b4a712008-09-09 01:27:07 -07001355 if (base_path && !is_directory(base_path))
1356 die("base-path '%s' does not exist or is not a directory",
1357 base_path);
Johannes Sixt20632072008-02-26 13:00:55 +01001358
lars.doelle@on-line.de7c3693f2005-09-08 03:50:01 +02001359 if (inetd_mode) {
Erik Faye-Lund30e15602010-11-04 02:35:16 +01001360 if (!freopen("/dev/null", "w", stderr))
1361 die_errno("failed to redirect stderr to /dev/null");
1362 }
1363
Erik Faye-Lundf9c87be2010-11-04 02:35:19 +01001364 if (inetd_mode || serve_mode)
1365 return execute();
Andreas Ericssonbce82302005-11-14 17:41:01 +01001366
Nguyễn Thái Ngọc Duyde0957c2014-02-08 14:08:51 +07001367 if (detach) {
1368 if (daemonize())
1369 die("--detach not supported on this platform");
1370 } else
Matthias Lederhofera5262762006-07-13 18:47:13 +02001371 sanitize_stdfds();
Matthias Lederhofer258e93a2006-07-13 18:32:11 +02001372
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +02001373 if (pid_file)
Junio C Hamano1f76a102015-08-24 13:20:39 -07001374 write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +02001375
Erik Faye-Lund30e15602010-11-04 02:35:16 +01001376 /* prepare argv for serving-processes */
Jeff King850d2fe2016-02-22 17:44:21 -05001377 argv_array_push(&cld_argv, argv[0]); /* git-daemon */
1378 argv_array_push(&cld_argv, "--serve");
Jonathan Nieder081f84e2011-01-03 22:04:46 -06001379 for (i = 1; i < argc; ++i)
Jeff King850d2fe2016-02-22 17:44:21 -05001380 argv_array_push(&cld_argv, argv[i]);
Erik Faye-Lund30e15602010-11-04 02:35:16 +01001381
Erik Faye-Lunda666b472010-11-04 02:35:24 +01001382 return serve(&listen_addr, listen_port, cred);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001383}