blob: e74ecac952fa0d399a1ed0c426a9e27d96b3ddcb [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"
Jon Loeliger49ba83f2006-09-19 20:31:51 -05004#include "interpolate.h"
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07005
Junio C Hamano85023572006-12-19 14:34:12 -08006#include <syslog.h>
7
Johannes Schindelin695dffe2006-09-28 12:00:35 +02008#ifndef HOST_NAME_MAX
9#define HOST_NAME_MAX 256
10#endif
11
Petr Baudis9048fe12005-09-24 16:13:01 +020012static int log_syslog;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020013static int verbose;
Mark Wooding1955fab2006-02-03 20:27:04 +000014static int reuseaddr;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020015
H. Peter Anvin960decc2005-10-19 14:27:01 -070016static const char daemon_usage[] =
Jon Loeligerdd467622006-09-26 09:47:43 -050017"git-daemon [--verbose] [--syslog] [--export-all]\n"
Petr Baudisb21c31c2005-12-23 02:27:40 +010018" [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
Junio C Hamano603968d2006-02-04 22:27:29 -080019" [--base-path=path] [--user-path | --user-path=path]\n"
Jon Loeliger49ba83f2006-09-19 20:31:51 -050020" [--interpolated-path=path]\n"
Tilman Sauerbeck678dac62006-08-22 19:37:41 +020021" [--reuseaddr] [--detach] [--pid-file=file]\n"
Junio C Hamanod9edcbd2006-09-07 01:40:04 -070022" [--[enable|disable|allow-override|forbid-override]=service]\n"
Jon Loeligerdd467622006-09-26 09:47:43 -050023" [--inetd | [--listen=host_or_ipaddr] [--port=n]\n"
24" [--user=user [--group=group]]\n"
25" [directory...]";
H. Peter Anvin4ae95682005-09-26 19:10:55 -070026
27/* List of acceptable pathname prefixes */
David Rientjes96f1e582006-08-15 10:23:48 -070028static char **ok_paths;
29static int strict_paths;
H. Peter Anvin4ae95682005-09-26 19:10:55 -070030
31/* If this is set, git-daemon-export-ok is not required */
David Rientjes96f1e582006-08-15 10:23:48 -070032static int export_all_trees;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020033
Petr Baudisb21c31c2005-12-23 02:27:40 +010034/* Take all paths relative to this one if non-NULL */
David Rientjes96f1e582006-08-15 10:23:48 -070035static char *base_path;
Jon Loeliger49ba83f2006-09-19 20:31:51 -050036static char *interpolated_path;
37
38/* Flag indicating client sent extra args. */
39static int saw_extended_args;
Petr Baudisb21c31c2005-12-23 02:27:40 +010040
Junio C Hamano603968d2006-02-04 22:27:29 -080041/* If defined, ~user notation is allowed and the string is inserted
42 * after ~user/. E.g. a request to git://host/~alice/frotz would
43 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
44 */
David Rientjes96f1e582006-08-15 10:23:48 -070045static const char *user_path;
Junio C Hamano603968d2006-02-04 22:27:29 -080046
H. Peter Anvin960decc2005-10-19 14:27:01 -070047/* Timeout, and initial timeout */
David Rientjes96f1e582006-08-15 10:23:48 -070048static unsigned int timeout;
49static unsigned int init_timeout;
Petr Baudisf8ff0c02005-09-22 11:25:28 +020050
Jon Loeliger49ba83f2006-09-19 20:31:51 -050051/*
52 * Static table for now. Ugh.
53 * Feel free to make dynamic as needed.
54 */
55#define INTERP_SLOT_HOST (0)
Jon Loeligerdd467622006-09-26 09:47:43 -050056#define INTERP_SLOT_CANON_HOST (1)
57#define INTERP_SLOT_IP (2)
58#define INTERP_SLOT_PORT (3)
59#define INTERP_SLOT_DIR (4)
60#define INTERP_SLOT_PERCENT (5)
Jon Loeliger49ba83f2006-09-19 20:31:51 -050061
62static struct interp interp_table[] = {
63 { "%H", 0},
Jon Loeligerdd467622006-09-26 09:47:43 -050064 { "%CH", 0},
65 { "%IP", 0},
66 { "%P", 0},
Jon Loeliger49ba83f2006-09-19 20:31:51 -050067 { "%D", 0},
Jon Loeligereb30aed2006-09-27 11:16:10 -050068 { "%%", 0},
Jon Loeliger49ba83f2006-09-19 20:31:51 -050069};
70
71
Petr Baudis9048fe12005-09-24 16:13:01 +020072static void logreport(int priority, const char *err, va_list params)
Petr Baudisf8ff0c02005-09-22 11:25:28 +020073{
74 /* We should do a single write so that it is atomic and output
75 * of several processes do not get intermingled. */
76 char buf[1024];
77 int buflen;
78 int maxlen, msglen;
79
80 /* sizeof(buf) should be big enough for "[pid] \n" */
Junio C Hamano1bedd4c2005-09-23 23:26:55 -070081 buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
Petr Baudisf8ff0c02005-09-22 11:25:28 +020082
83 maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
84 msglen = vsnprintf(buf + buflen, maxlen, err, params);
85
Petr Baudis9048fe12005-09-24 16:13:01 +020086 if (log_syslog) {
87 syslog(priority, "%s", buf);
88 return;
89 }
90
Petr Baudisf8ff0c02005-09-22 11:25:28 +020091 /* maxlen counted our own LF but also counts space given to
92 * vsnprintf for the terminating NUL. We want to make sure that
93 * we have space for our own LF and NUL after the "meat" of the
94 * message, so truncate it at maxlen - 1.
95 */
96 if (msglen > maxlen - 1)
97 msglen = maxlen - 1;
98 else if (msglen < 0)
99 msglen = 0; /* Protect against weird return values. */
100 buflen += msglen;
101
102 buf[buflen++] = '\n';
103 buf[buflen] = '\0';
104
Andy Whitcroft93822c22007-01-08 15:58:23 +0000105 write_in_full(2, buf, buflen);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200106}
107
Pavel Roskincdda4742005-09-29 16:53:14 -0400108static void logerror(const char *err, ...)
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200109{
110 va_list params;
111 va_start(params, err);
Petr Baudis9048fe12005-09-24 16:13:01 +0200112 logreport(LOG_ERR, err, params);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200113 va_end(params);
114}
115
Pavel Roskincdda4742005-09-29 16:53:14 -0400116static void loginfo(const char *err, ...)
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200117{
118 va_list params;
119 if (!verbose)
120 return;
121 va_start(params, err);
Petr Baudis9048fe12005-09-24 16:13:01 +0200122 logreport(LOG_INFO, err, params);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200123 va_end(params);
124}
125
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +0200126static void NORETURN daemon_die(const char *err, va_list params)
127{
128 logreport(LOG_ERR, err, params);
129 exit(1);
130}
131
Junio C Hamanod79374c2005-12-03 01:45:57 -0800132static int avoid_alias(char *p)
133{
134 int sl, ndot;
135
136 /*
137 * This resurrects the belts and suspenders paranoia check by HPA
138 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
139 * does not do getcwd() based path canonicalizations.
140 *
141 * sl becomes true immediately after seeing '/' and continues to
142 * be true as long as dots continue after that without intervening
143 * non-dot character.
144 */
145 if (!p || (*p != '/' && *p != '~'))
146 return -1;
147 sl = 1; ndot = 0;
148 p++;
149
150 while (1) {
151 char ch = *p++;
152 if (sl) {
153 if (ch == '.')
154 ndot++;
155 else if (ch == '/') {
156 if (ndot < 3)
157 /* reject //, /./ and /../ */
158 return -1;
159 ndot = 0;
160 }
161 else if (ch == 0) {
162 if (0 < ndot && ndot < 3)
163 /* reject /.$ and /..$ */
164 return -1;
165 return 0;
166 }
167 else
168 sl = ndot = 0;
169 }
170 else if (ch == 0)
171 return 0;
172 else if (ch == '/') {
173 sl = 1;
174 ndot = 0;
175 }
176 }
177}
178
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500179static char *path_ok(struct interp *itable)
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700180{
Junio C Hamano603968d2006-02-04 22:27:29 -0800181 static char rpath[PATH_MAX];
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500182 static char interp_path[PATH_MAX];
Junio C Hamanod79374c2005-12-03 01:45:57 -0800183 char *path;
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500184 char *dir;
185
186 dir = itable[INTERP_SLOT_DIR].value;
Junio C Hamanod79374c2005-12-03 01:45:57 -0800187
188 if (avoid_alias(dir)) {
189 logerror("'%s': aliased", dir);
190 return NULL;
191 }
192
Junio C Hamano603968d2006-02-04 22:27:29 -0800193 if (*dir == '~') {
194 if (!user_path) {
195 logerror("'%s': User-path not allowed", dir);
196 return NULL;
197 }
198 if (*user_path) {
199 /* Got either "~alice" or "~alice/foo";
200 * rewrite them to "~alice/%s" or
201 * "~alice/%s/foo".
202 */
203 int namlen, restlen = strlen(dir);
204 char *slash = strchr(dir, '/');
205 if (!slash)
206 slash = dir + restlen;
207 namlen = slash - dir;
208 restlen -= namlen;
209 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
210 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
211 namlen, dir, user_path, restlen, slash);
212 dir = rpath;
213 }
214 }
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500215 else if (interpolated_path && saw_extended_args) {
216 if (*dir != '/') {
217 /* Allow only absolute */
218 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
219 return NULL;
220 }
221
222 interpolate(interp_path, PATH_MAX, interpolated_path,
223 interp_table, ARRAY_SIZE(interp_table));
224 loginfo("Interpolated dir '%s'", interp_path);
225
226 dir = interp_path;
227 }
Junio C Hamano603968d2006-02-04 22:27:29 -0800228 else if (base_path) {
229 if (*dir != '/') {
230 /* Allow only absolute */
Mark Wooding1fda3d52006-02-03 20:27:02 +0000231 logerror("'%s': Non-absolute path denied (base-path active)", dir);
Petr Baudisb21c31c2005-12-23 02:27:40 +0100232 return NULL;
233 }
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500234 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
235 dir = rpath;
Petr Baudisb21c31c2005-12-23 02:27:40 +0100236 }
237
Junio C Hamanod79374c2005-12-03 01:45:57 -0800238 path = enter_repo(dir, strict_paths);
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700239
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100240 if (!path) {
241 logerror("'%s': unable to chdir or not a git archive", dir);
242 return NULL;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700243 }
244
245 if ( ok_paths && *ok_paths ) {
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800246 char **pp;
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100247 int pathlen = strlen(path);
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700248
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800249 /* The validation is done on the paths after enter_repo
Junio C Hamanod79374c2005-12-03 01:45:57 -0800250 * appends optional {.git,.git/.git} and friends, but
251 * it does not use getcwd(). So if your /pub is
252 * a symlink to /mnt/pub, you can whitelist /pub and
253 * do not have to say /mnt/pub.
254 * Do not say /pub/.
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800255 */
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700256 for ( pp = ok_paths ; *pp ; pp++ ) {
257 int len = strlen(*pp);
Junio C Hamanoce335fe2005-11-21 01:21:18 -0800258 if (len <= pathlen &&
259 !memcmp(*pp, path, len) &&
260 (path[len] == '\0' ||
261 (!strict_paths && path[len] == '/')))
262 return path;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700263 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100264 }
265 else {
266 /* be backwards compatible */
267 if (!strict_paths)
268 return path;
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700269 }
270
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100271 logerror("'%s': not in whitelist", path);
272 return NULL; /* Fallthrough. Deny by default */
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700273}
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700274
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700275typedef int (*daemon_service_fn)(void);
276struct daemon_service {
277 const char *name;
278 const char *config_name;
279 daemon_service_fn fn;
280 int enabled;
281 int overridable;
282};
H. Peter Anvin4ae95682005-09-26 19:10:55 -0700283
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700284static struct daemon_service *service_looking_at;
285static int service_enabled;
286
287static int git_daemon_config(const char *var, const char *value)
288{
Junio C Hamanocc44c762007-02-20 01:53:29 -0800289 if (!prefixcmp(var, "daemon.") &&
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700290 !strcmp(var + 7, service_looking_at->config_name)) {
291 service_enabled = git_config_bool(var, value);
292 return 0;
293 }
294
295 /* we are not interested in parsing any other configuration here */
296 return 0;
297}
298
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500299static int run_service(struct interp *itable, struct daemon_service *service)
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700300{
301 const char *path;
302 int enabled = service->enabled;
303
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500304 loginfo("Request %s for '%s'",
305 service->name,
306 itable[INTERP_SLOT_DIR].value);
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700307
308 if (!enabled && !service->overridable) {
309 logerror("'%s': service not enabled.", service->name);
310 errno = EACCES;
311 return -1;
312 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100313
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500314 if (!(path = path_ok(itable)))
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700315 return -1;
H. Peter Anvin47888f02005-09-27 08:49:40 -0700316
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700317 /*
318 * Security on the cheap.
319 *
Junio C Hamanoa935c392005-10-20 23:19:36 -0700320 * We want a readable HEAD, usable "objects" directory, and
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700321 * a "git-daemon-export-ok" flag that says that the other side
322 * is ok with us doing this.
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100323 *
324 * path_ok() uses enter_repo() and does whitelist checking.
325 * We only need to make sure the repository is exported.
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700326 */
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100327
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700328 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
Andreas Ericsson4dbd1352005-11-17 20:37:14 +0100329 logerror("'%s': repository not exported.", path);
H. Peter Anvin3e04c622005-10-18 18:26:52 -0700330 errno = EACCES;
331 return -1;
332 }
333
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700334 if (service->overridable) {
335 service_looking_at = service;
336 service_enabled = -1;
337 git_config(git_daemon_config);
338 if (0 <= service_enabled)
339 enabled = service_enabled;
340 }
341 if (!enabled) {
342 logerror("'%s': service not enabled for '%s'",
343 service->name, path);
344 errno = EACCES;
345 return -1;
346 }
347
Linus Torvalds02d57da2005-07-15 22:53:31 -0700348 /*
349 * We'll ignore SIGTERM from now on, we have a
350 * good client.
351 */
352 signal(SIGTERM, SIG_IGN);
353
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700354 return service->fn();
355}
356
357static int upload_pack(void)
358{
359 /* Timeout as string */
360 char timeout_buf[64];
361
H. Peter Anvin960decc2005-10-19 14:27:01 -0700362 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
363
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700364 /* git-upload-pack only ever reads stuff, so this is safe */
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500365 execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700366 return -1;
367}
368
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200369static int upload_archive(void)
370{
371 execl_git_cmd("upload-archive", ".", NULL);
372 return -1;
373}
374
Linus Torvalds4b3b1e12007-01-21 11:04:13 -0800375static int receive_pack(void)
376{
377 execl_git_cmd("receive-pack", ".", NULL);
378 return -1;
379}
380
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700381static struct daemon_service daemon_service[] = {
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200382 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700383 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
Linus Torvalds4b3b1e12007-01-21 11:04:13 -0800384 { "receive-pack", "receivepack", receive_pack, 0, 1 },
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700385};
386
387static void enable_service(const char *name, int ena) {
388 int i;
389 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
390 if (!strcmp(daemon_service[i].name, name)) {
391 daemon_service[i].enabled = ena;
392 return;
393 }
394 }
395 die("No such service %s", name);
396}
397
398static void make_service_overridable(const char *name, int ena) {
399 int i;
400 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
401 if (!strcmp(daemon_service[i].name, name)) {
402 daemon_service[i].overridable = ena;
403 return;
404 }
405 }
406 die("No such service %s", name);
407}
408
Jon Loeligereb30aed2006-09-27 11:16:10 -0500409/*
410 * Separate the "extra args" information as supplied by the client connection.
Pavel Roskin3dff5372007-02-03 23:49:16 -0500411 * Any resulting data is squirreled away in the given interpolation table.
Jon Loeligereb30aed2006-09-27 11:16:10 -0500412 */
413static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500414{
415 char *val;
416 int vallen;
417 char *end = extra_args + buflen;
418
419 while (extra_args < end && *extra_args) {
420 saw_extended_args = 1;
421 if (strncasecmp("host=", extra_args, 5) == 0) {
422 val = extra_args + 5;
423 vallen = strlen(val) + 1;
424 if (*val) {
Jon Loeligereb30aed2006-09-27 11:16:10 -0500425 /* Split <host>:<port> at colon. */
426 char *host = val;
427 char *port = strrchr(host, ':');
Jon Loeligerdd467622006-09-26 09:47:43 -0500428 if (port) {
429 *port = 0;
430 port++;
Jon Loeligereb30aed2006-09-27 11:16:10 -0500431 interp_set_entry(table, INTERP_SLOT_PORT, port);
Jon Loeligerdd467622006-09-26 09:47:43 -0500432 }
Jon Loeligereb30aed2006-09-27 11:16:10 -0500433 interp_set_entry(table, INTERP_SLOT_HOST, host);
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500434 }
Jon Loeligereb30aed2006-09-27 11:16:10 -0500435
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500436 /* On to the next one */
437 extra_args = val + vallen;
438 }
439 }
440}
441
Jon Loeligerdd467622006-09-26 09:47:43 -0500442void fill_in_extra_table_entries(struct interp *itable)
443{
444 char *hp;
Jon Loeligerdd467622006-09-26 09:47:43 -0500445
446 /*
447 * Replace literal host with lowercase-ized hostname.
448 */
449 hp = interp_table[INTERP_SLOT_HOST].value;
Junio C Hamano83543a22006-10-23 18:26:05 -0700450 if (!hp)
451 return;
Jon Loeligerdd467622006-09-26 09:47:43 -0500452 for ( ; *hp; hp++)
453 *hp = tolower(*hp);
454
455 /*
456 * Locate canonical hostname and its IP address.
457 */
458#ifndef NO_IPV6
459 {
460 struct addrinfo hints;
461 struct addrinfo *ai, *ai0;
462 int gai;
463 static char addrbuf[HOST_NAME_MAX + 1];
464
465 memset(&hints, 0, sizeof(hints));
466 hints.ai_flags = AI_CANONNAME;
467
468 gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0);
469 if (!gai) {
470 for (ai = ai0; ai; ai = ai->ai_next) {
471 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
472
Jon Loeligerdd467622006-09-26 09:47:43 -0500473 inet_ntop(AF_INET, &sin_addr->sin_addr,
474 addrbuf, sizeof(addrbuf));
Jon Loeligereb30aed2006-09-27 11:16:10 -0500475 interp_set_entry(interp_table,
476 INTERP_SLOT_CANON_HOST, ai->ai_canonname);
477 interp_set_entry(interp_table,
478 INTERP_SLOT_IP, addrbuf);
Jon Loeligerdd467622006-09-26 09:47:43 -0500479 break;
480 }
481 freeaddrinfo(ai0);
482 }
483 }
484#else
485 {
486 struct hostent *hent;
487 struct sockaddr_in sa;
488 char **ap;
489 static char addrbuf[HOST_NAME_MAX + 1];
490
491 hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value);
Jon Loeligerdd467622006-09-26 09:47:43 -0500492
493 ap = hent->h_addr_list;
494 memset(&sa, 0, sizeof sa);
495 sa.sin_family = hent->h_addrtype;
496 sa.sin_port = htons(0);
497 memcpy(&sa.sin_addr, *ap, hent->h_length);
498
499 inet_ntop(hent->h_addrtype, &sa.sin_addr,
500 addrbuf, sizeof(addrbuf));
Jon Loeligereb30aed2006-09-27 11:16:10 -0500501
502 interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name);
503 interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf);
Jon Loeligerdd467622006-09-26 09:47:43 -0500504 }
505#endif
Jon Loeligerdd467622006-09-26 09:47:43 -0500506}
507
508
David Woodhouse5b276ee2006-06-20 15:38:13 +0100509static int execute(struct sockaddr *addr)
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700510{
Linus Torvalds7d806942005-07-15 09:27:05 -0700511 static char line[1000];
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700512 int pktlen, len, i;
Linus Torvalds7d806942005-07-15 09:27:05 -0700513
David Woodhouse5b276ee2006-06-20 15:38:13 +0100514 if (addr) {
515 char addrbuf[256] = "";
516 int port = -1;
517
518 if (addr->sa_family == AF_INET) {
519 struct sockaddr_in *sin_addr = (void *) addr;
520 inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
521 port = sin_addr->sin_port;
522#ifndef NO_IPV6
523 } else if (addr && addr->sa_family == AF_INET6) {
524 struct sockaddr_in6 *sin6_addr = (void *) addr;
525
526 char *buf = addrbuf;
527 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
528 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
529 strcat(buf, "]");
530
531 port = sin6_addr->sin6_port;
532#endif
533 }
534 loginfo("Connection from %s:%d", addrbuf, port);
535 }
536
H. Peter Anvin960decc2005-10-19 14:27:01 -0700537 alarm(init_timeout ? init_timeout : timeout);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500538 pktlen = packet_read_line(0, line, sizeof(line));
H. Peter Anvin960decc2005-10-19 14:27:01 -0700539 alarm(0);
Linus Torvalds7d806942005-07-15 09:27:05 -0700540
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500541 len = strlen(line);
542 if (pktlen != len)
543 loginfo("Extended attributes (%d bytes) exist <%.*s>",
544 (int) pktlen - len,
545 (int) pktlen - len, line + len + 1);
Junio C Hamano83543a22006-10-23 18:26:05 -0700546 if (len && line[len-1] == '\n') {
Linus Torvalds7d806942005-07-15 09:27:05 -0700547 line[--len] = 0;
Junio C Hamano83543a22006-10-23 18:26:05 -0700548 pktlen--;
549 }
Linus Torvalds7d806942005-07-15 09:27:05 -0700550
Jon Loeligereb30aed2006-09-27 11:16:10 -0500551 /*
552 * Initialize the path interpolation table for this connection.
553 */
554 interp_clear_table(interp_table, ARRAY_SIZE(interp_table));
555 interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%");
556
Jon Loeligerdd467622006-09-26 09:47:43 -0500557 if (len != pktlen) {
Jon Loeligereb30aed2006-09-27 11:16:10 -0500558 parse_extra_args(interp_table, line + len + 1, pktlen - len - 1);
Jon Loeligerdd467622006-09-26 09:47:43 -0500559 fill_in_extra_table_entries(interp_table);
560 }
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500561
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700562 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
563 struct daemon_service *s = &(daemon_service[i]);
564 int namelen = strlen(s->name);
Junio C Hamano599065a2007-02-20 01:54:00 -0800565 if (!prefixcmp(line, "git-") &&
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700566 !strncmp(s->name, line + 4, namelen) &&
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500567 line[namelen + 4] == ' ') {
Jon Loeligereb30aed2006-09-27 11:16:10 -0500568 /*
569 * Note: The directory here is probably context sensitive,
570 * and might depend on the actual service being performed.
571 */
572 interp_set_entry(interp_table,
573 INTERP_SLOT_DIR, line + namelen + 5);
Jon Loeliger49ba83f2006-09-19 20:31:51 -0500574 return run_service(interp_table, s);
575 }
Junio C Hamanod819e4e2006-08-20 19:03:13 -0700576 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700577
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200578 logerror("Protocol error: '%s'", line);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700579 return -1;
580}
581
Linus Torvalds66e631d2005-07-15 21:51:57 -0700582
583/*
584 * We count spawned/reaped separately, just to avoid any
585 * races when updating them from signals. The SIGCHLD handler
586 * will only update children_reaped, and the fork logic will
587 * only update children_spawned.
588 *
589 * MAX_CHILDREN should be a power-of-two to make the modulus
590 * operation cheap. It should also be at least twice
591 * the maximum number of connections we will ever allow.
592 */
593#define MAX_CHILDREN 128
594
595static int max_connections = 25;
596
597/* These are updated by the signal handler */
David Rientjes96f1e582006-08-15 10:23:48 -0700598static volatile unsigned int children_reaped;
Linus Torvalds4d8fa912005-08-01 12:11:53 -0700599static pid_t dead_child[MAX_CHILDREN];
Linus Torvalds66e631d2005-07-15 21:51:57 -0700600
601/* These are updated by the main loop */
David Rientjes96f1e582006-08-15 10:23:48 -0700602static unsigned int children_spawned;
603static unsigned int children_deleted;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700604
Linus Torvalds4d8fa912005-08-01 12:11:53 -0700605static struct child {
Linus Torvalds66e631d2005-07-15 21:51:57 -0700606 pid_t pid;
Junio C Hamano7fa09082005-09-11 13:58:41 -0700607 int addrlen;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400608 struct sockaddr_storage address;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700609} live_child[MAX_CHILDREN];
610
Junio C Hamano7fa09082005-09-11 13:58:41 -0700611static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen)
Linus Torvalds66e631d2005-07-15 21:51:57 -0700612{
613 live_child[idx].pid = pid;
614 live_child[idx].addrlen = addrlen;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400615 memcpy(&live_child[idx].address, addr, addrlen);
Linus Torvalds66e631d2005-07-15 21:51:57 -0700616}
617
618/*
619 * Walk from "deleted" to "spawned", and remove child "pid".
620 *
621 * We move everything up by one, since the new "deleted" will
622 * be one higher.
623 */
624static void remove_child(pid_t pid, unsigned deleted, unsigned spawned)
625{
626 struct child n;
627
628 deleted %= MAX_CHILDREN;
629 spawned %= MAX_CHILDREN;
630 if (live_child[deleted].pid == pid) {
631 live_child[deleted].pid = -1;
632 return;
633 }
634 n = live_child[deleted];
635 for (;;) {
636 struct child m;
637 deleted = (deleted + 1) % MAX_CHILDREN;
638 if (deleted == spawned)
639 die("could not find dead child %d\n", pid);
640 m = live_child[deleted];
641 live_child[deleted] = n;
642 if (m.pid == pid)
643 return;
644 n = m;
645 }
646}
647
648/*
649 * This gets called if the number of connections grows
650 * past "max_connections".
651 *
652 * We _should_ start off by searching for connections
653 * from the same IP, and if there is some address wth
654 * multiple connections, we should kill that first.
655 *
656 * As it is, we just "randomly" kill 25% of the connections,
657 * and our pseudo-random generator sucks too. I have no
658 * shame.
659 *
660 * Really, this is just a place-holder for a _real_ algorithm.
661 */
Linus Torvalds02d57da2005-07-15 22:53:31 -0700662static void kill_some_children(int signo, unsigned start, unsigned stop)
Linus Torvalds66e631d2005-07-15 21:51:57 -0700663{
664 start %= MAX_CHILDREN;
665 stop %= MAX_CHILDREN;
666 while (start != stop) {
667 if (!(start & 3))
Linus Torvalds02d57da2005-07-15 22:53:31 -0700668 kill(live_child[start].pid, signo);
Linus Torvalds66e631d2005-07-15 21:51:57 -0700669 start = (start + 1) % MAX_CHILDREN;
670 }
671}
672
Linus Torvalds02d57da2005-07-15 22:53:31 -0700673static void check_max_connections(void)
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700674{
Linus Torvalds02d57da2005-07-15 22:53:31 -0700675 for (;;) {
Linus Torvaldseaa94912005-07-15 20:42:28 -0700676 int active;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700677 unsigned spawned, reaped, deleted;
Linus Torvaldseaa94912005-07-15 20:42:28 -0700678
Linus Torvalds66e631d2005-07-15 21:51:57 -0700679 spawned = children_spawned;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700680 reaped = children_reaped;
681 deleted = children_deleted;
682
683 while (deleted < reaped) {
684 pid_t pid = dead_child[deleted % MAX_CHILDREN];
685 remove_child(pid, deleted, spawned);
686 deleted++;
Linus Torvaldseaa94912005-07-15 20:42:28 -0700687 }
Linus Torvalds66e631d2005-07-15 21:51:57 -0700688 children_deleted = deleted;
689
690 active = spawned - deleted;
Linus Torvalds02d57da2005-07-15 22:53:31 -0700691 if (active <= max_connections)
692 break;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700693
Linus Torvalds02d57da2005-07-15 22:53:31 -0700694 /* Kill some unstarted connections with SIGTERM */
695 kill_some_children(SIGTERM, deleted, spawned);
696 if (active <= max_connections << 1)
697 break;
Linus Torvalds66e631d2005-07-15 21:51:57 -0700698
Linus Torvalds02d57da2005-07-15 22:53:31 -0700699 /* If the SIGTERM thing isn't helping use SIGKILL */
700 kill_some_children(SIGKILL, deleted, spawned);
701 sleep(1);
702 }
703}
704
Junio C Hamano7fa09082005-09-11 13:58:41 -0700705static void handle(int incoming, struct sockaddr *addr, int addrlen)
Linus Torvalds02d57da2005-07-15 22:53:31 -0700706{
707 pid_t pid = fork();
708
709 if (pid) {
710 unsigned idx;
711
712 close(incoming);
713 if (pid < 0)
714 return;
715
716 idx = children_spawned % MAX_CHILDREN;
717 children_spawned++;
718 add_child(idx, pid, addr, addrlen);
719
720 check_max_connections();
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700721 return;
722 }
723
724 dup2(incoming, 0);
725 dup2(incoming, 1);
726 close(incoming);
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200727
David Woodhouse5b276ee2006-06-20 15:38:13 +0100728 exit(execute(addr));
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700729}
730
Linus Torvaldseaa94912005-07-15 20:42:28 -0700731static void child_handler(int signo)
732{
733 for (;;) {
Petr Baudis9048fe12005-09-24 16:13:01 +0200734 int status;
735 pid_t pid = waitpid(-1, &status, WNOHANG);
Linus Torvalds66e631d2005-07-15 21:51:57 -0700736
737 if (pid > 0) {
738 unsigned reaped = children_reaped;
739 dead_child[reaped % MAX_CHILDREN] = pid;
740 children_reaped = reaped + 1;
Petr Baudisf8ff0c02005-09-22 11:25:28 +0200741 /* XXX: Custom logging, since we don't wanna getpid() */
Petr Baudis9048fe12005-09-24 16:13:01 +0200742 if (verbose) {
Timo Hirvonen554fe202006-06-28 12:04:39 +0300743 const char *dead = "";
Petr Baudis9048fe12005-09-24 16:13:01 +0200744 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
745 dead = " (with error)";
746 if (log_syslog)
747 syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead);
748 else
749 fprintf(stderr, "[%d] Disconnected%s\n", pid, dead);
750 }
Linus Torvaldseaa94912005-07-15 20:42:28 -0700751 continue;
752 }
753 break;
754 }
755}
756
Mark Wooding1955fab2006-02-03 20:27:04 +0000757static int set_reuse_addr(int sockfd)
758{
759 int on = 1;
760
761 if (!reuseaddr)
762 return 0;
763 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
764 &on, sizeof(on));
765}
766
Peter Anvin6573faf2005-09-28 17:26:44 -0700767#ifndef NO_IPV6
768
Jon Loeligerdd467622006-09-26 09:47:43 -0500769static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700770{
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400771 int socknum = 0, *socklist = NULL;
772 int maxfd = -1;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400773 char pbuf[NI_MAXSERV];
Peter Anvin6573faf2005-09-28 17:26:44 -0700774 struct addrinfo hints, *ai0, *ai;
775 int gai;
Alexandre Julliard20276882007-02-14 18:10:26 +0100776 long flags;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400777
Jon Loeligerdd467622006-09-26 09:47:43 -0500778 sprintf(pbuf, "%d", listen_port);
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400779 memset(&hints, 0, sizeof(hints));
780 hints.ai_family = AF_UNSPEC;
781 hints.ai_socktype = SOCK_STREAM;
782 hints.ai_protocol = IPPROTO_TCP;
783 hints.ai_flags = AI_PASSIVE;
784
Jon Loeligerdd467622006-09-26 09:47:43 -0500785 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400786 if (gai)
787 die("getaddrinfo() failed: %s\n", gai_strerror(gai));
788
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400789 for (ai = ai0; ai; ai = ai->ai_next) {
790 int sockfd;
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400791
792 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
793 if (sockfd < 0)
794 continue;
795 if (sockfd >= FD_SETSIZE) {
796 error("too large socket descriptor.");
797 close(sockfd);
798 continue;
799 }
800
801#ifdef IPV6_V6ONLY
802 if (ai->ai_family == AF_INET6) {
803 int on = 1;
804 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
805 &on, sizeof(on));
806 /* Note: error is not fatal */
807 }
808#endif
809
Mark Wooding1955fab2006-02-03 20:27:04 +0000810 if (set_reuse_addr(sockfd)) {
811 close(sockfd);
Serge E. Hallyn0032d542006-04-18 08:11:06 -0500812 continue;
Mark Wooding1955fab2006-02-03 20:27:04 +0000813 }
814
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400815 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
816 close(sockfd);
817 continue; /* not fatal */
818 }
819 if (listen(sockfd, 5) < 0) {
820 close(sockfd);
821 continue; /* not fatal */
822 }
823
Alexandre Julliard20276882007-02-14 18:10:26 +0100824 flags = fcntl(sockfd, F_GETFD, 0);
825 if (flags >= 0)
826 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
827
Jonas Fonseca83572c12006-08-26 16:16:18 +0200828 socklist = xrealloc(socklist, sizeof(int) * (socknum + 1));
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400829 socklist[socknum++] = sockfd;
830
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400831 if (maxfd < sockfd)
832 maxfd = sockfd;
833 }
834
835 freeaddrinfo(ai0);
836
Peter Anvin6573faf2005-09-28 17:26:44 -0700837 *socklist_p = socklist;
838 return socknum;
839}
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700840
Peter Anvin6573faf2005-09-28 17:26:44 -0700841#else /* NO_IPV6 */
842
Alex Riesen100690b2006-09-28 20:48:14 +0200843static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
Peter Anvin6573faf2005-09-28 17:26:44 -0700844{
845 struct sockaddr_in sin;
846 int sockfd;
Alexandre Julliard20276882007-02-14 18:10:26 +0100847 long flags;
Peter Anvin6573faf2005-09-28 17:26:44 -0700848
Jon Loeligerdd467622006-09-26 09:47:43 -0500849 memset(&sin, 0, sizeof sin);
850 sin.sin_family = AF_INET;
851 sin.sin_port = htons(listen_port);
852
853 if (listen_addr) {
854 /* Well, host better be an IP address here. */
855 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
856 return 0;
857 } else {
858 sin.sin_addr.s_addr = htonl(INADDR_ANY);
859 }
860
Peter Anvin6573faf2005-09-28 17:26:44 -0700861 sockfd = socket(AF_INET, SOCK_STREAM, 0);
862 if (sockfd < 0)
863 return 0;
864
Mark Wooding1955fab2006-02-03 20:27:04 +0000865 if (set_reuse_addr(sockfd)) {
866 close(sockfd);
867 return 0;
868 }
869
Peter Anvin6573faf2005-09-28 17:26:44 -0700870 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
871 close(sockfd);
872 return 0;
873 }
874
Paul Sericef35230f2005-11-21 11:07:23 -0600875 if (listen(sockfd, 5) < 0) {
876 close(sockfd);
877 return 0;
878 }
879
Alexandre Julliard20276882007-02-14 18:10:26 +0100880 flags = fcntl(sockfd, F_GETFD, 0);
881 if (flags >= 0)
882 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
883
H. Peter Anvin1b4713f2005-09-30 10:47:50 -0700884 *socklist_p = xmalloc(sizeof(int));
Peter Anvin6573faf2005-09-28 17:26:44 -0700885 **socklist_p = sockfd;
Paul Sericef35230f2005-11-21 11:07:23 -0600886 return 1;
Peter Anvin6573faf2005-09-28 17:26:44 -0700887}
888
889#endif
890
891static int service_loop(int socknum, int *socklist)
892{
893 struct pollfd *pfd;
894 int i;
895
H. Peter Anvin1b4713f2005-09-30 10:47:50 -0700896 pfd = xcalloc(socknum, sizeof(struct pollfd));
Peter Anvin6573faf2005-09-28 17:26:44 -0700897
898 for (i = 0; i < socknum; i++) {
899 pfd[i].fd = socklist[i];
900 pfd[i].events = POLLIN;
901 }
H. Peter Anvin92202822005-09-30 11:01:57 -0700902
903 signal(SIGCHLD, child_handler);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700904
905 for (;;) {
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400906 int i;
Peter Anvin6573faf2005-09-28 17:26:44 -0700907
Jens Axboe7872e052005-10-20 00:52:32 -0700908 if (poll(pfd, socknum, -1) < 0) {
Junio C Hamano1eef0b32005-07-26 13:26:52 -0700909 if (errno != EINTR) {
Peter Anvin6573faf2005-09-28 17:26:44 -0700910 error("poll failed, resuming: %s",
Junio C Hamano1eef0b32005-07-26 13:26:52 -0700911 strerror(errno));
912 sleep(1);
913 }
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400914 continue;
915 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700916
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400917 for (i = 0; i < socknum; i++) {
Peter Anvin6573faf2005-09-28 17:26:44 -0700918 if (pfd[i].revents & POLLIN) {
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400919 struct sockaddr_storage ss;
H. Peter Anvin7626e492005-09-30 10:48:21 -0700920 unsigned int sslen = sizeof(ss);
Peter Anvin6573faf2005-09-28 17:26:44 -0700921 int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
YOSHIFUJI Hideakidf076bd2005-07-23 04:24:59 -0400922 if (incoming < 0) {
923 switch (errno) {
924 case EAGAIN:
925 case EINTR:
926 case ECONNABORTED:
927 continue;
928 default:
929 die("accept returned %s", strerror(errno));
930 }
931 }
932 handle(incoming, (struct sockaddr *)&ss, sslen);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700933 }
934 }
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700935 }
936}
937
Matthias Lederhofer258e93a2006-07-13 18:32:11 +0200938/* if any standard file descriptor is missing open it to /dev/null */
939static void sanitize_stdfds(void)
940{
941 int fd = open("/dev/null", O_RDWR, 0);
942 while (fd != -1 && fd < 2)
943 fd = dup(fd);
944 if (fd == -1)
945 die("open /dev/null or dup failed: %s", strerror(errno));
946 if (fd > 2)
947 close(fd);
948}
949
Matthias Lederhofera5262762006-07-13 18:47:13 +0200950static void daemonize(void)
951{
952 switch (fork()) {
953 case 0:
954 break;
955 case -1:
956 die("fork failed: %s", strerror(errno));
957 default:
958 exit(0);
959 }
960 if (setsid() == -1)
961 die("setsid failed: %s", strerror(errno));
962 close(0);
963 close(1);
964 close(2);
965 sanitize_stdfds();
966}
967
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +0200968static void store_pid(const char *path)
969{
970 FILE *f = fopen(path, "w");
971 if (!f)
972 die("cannot open pid file %s: %s", path, strerror(errno));
973 fprintf(f, "%d\n", getpid());
974 fclose(f);
975}
976
Jon Loeligerdd467622006-09-26 09:47:43 -0500977static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
Peter Anvin6573faf2005-09-28 17:26:44 -0700978{
979 int socknum, *socklist;
Junio C Hamano4ae22d92005-10-20 23:21:50 -0700980
Jon Loeligerdd467622006-09-26 09:47:43 -0500981 socknum = socksetup(listen_addr, listen_port, &socklist);
Peter Anvin6573faf2005-09-28 17:26:44 -0700982 if (socknum == 0)
Jon Loeligerdd467622006-09-26 09:47:43 -0500983 die("unable to allocate any listen sockets on host %s port %u",
984 listen_addr, listen_port);
Junio C Hamano4ae22d92005-10-20 23:21:50 -0700985
Tilman Sauerbeck678dac62006-08-22 19:37:41 +0200986 if (pass && gid &&
987 (initgroups(pass->pw_name, gid) || setgid (gid) ||
988 setuid(pass->pw_uid)))
989 die("cannot drop privileges");
990
Peter Anvin6573faf2005-09-28 17:26:44 -0700991 return service_loop(socknum, socklist);
Junio C Hamano4ae22d92005-10-20 23:21:50 -0700992}
Peter Anvin6573faf2005-09-28 17:26:44 -0700993
Linus Torvaldsa87e8be2005-07-13 19:45:26 -0700994int main(int argc, char **argv)
995{
Jon Loeligerdd467622006-09-26 09:47:43 -0500996 int listen_port = 0;
997 char *listen_addr = NULL;
Linus Torvaldse64e1b72005-07-15 09:32:16 -0700998 int inetd_mode = 0;
Tilman Sauerbeck678dac62006-08-22 19:37:41 +0200999 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
Matthias Lederhofera5262762006-07-13 18:47:13 +02001000 int detach = 0;
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001001 struct passwd *pass = NULL;
1002 struct group *group;
1003 gid_t gid = 0;
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001004 int i;
1005
Junio C Hamanof0b73672006-06-19 18:25:21 -07001006 /* Without this we cannot rely on waitpid() to tell
1007 * what happened to our children.
1008 */
1009 signal(SIGCHLD, SIG_DFL);
1010
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001011 for (i = 1; i < argc; i++) {
1012 char *arg = argv[i];
1013
Junio C Hamanocc44c762007-02-20 01:53:29 -08001014 if (!prefixcmp(arg, "--listen=")) {
Jon Loeligerdd467622006-09-26 09:47:43 -05001015 char *p = arg + 9;
1016 char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1);
1017 while (*p)
1018 *ph++ = tolower(*p++);
1019 *ph = 0;
1020 continue;
1021 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001022 if (!prefixcmp(arg, "--port=")) {
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001023 char *end;
1024 unsigned long n;
1025 n = strtoul(arg+7, &end, 0);
1026 if (arg[7] && !*end) {
Jon Loeligerdd467622006-09-26 09:47:43 -05001027 listen_port = n;
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001028 continue;
1029 }
1030 }
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001031 if (!strcmp(arg, "--inetd")) {
1032 inetd_mode = 1;
Andreas Ericssona8883282005-11-17 00:38:29 +01001033 log_syslog = 1;
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001034 continue;
1035 }
Petr Baudisf8ff0c02005-09-22 11:25:28 +02001036 if (!strcmp(arg, "--verbose")) {
1037 verbose = 1;
1038 continue;
1039 }
Petr Baudis9048fe12005-09-24 16:13:01 +02001040 if (!strcmp(arg, "--syslog")) {
1041 log_syslog = 1;
Petr Baudis9048fe12005-09-24 16:13:01 +02001042 continue;
1043 }
H. Peter Anvin4ae95682005-09-26 19:10:55 -07001044 if (!strcmp(arg, "--export-all")) {
1045 export_all_trees = 1;
1046 continue;
1047 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001048 if (!prefixcmp(arg, "--timeout=")) {
H. Peter Anvin960decc2005-10-19 14:27:01 -07001049 timeout = atoi(arg+10);
Andreas Ericssona8883282005-11-17 00:38:29 +01001050 continue;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001051 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001052 if (!prefixcmp(arg, "--init-timeout=")) {
H. Peter Anvin960decc2005-10-19 14:27:01 -07001053 init_timeout = atoi(arg+15);
Andreas Ericssona8883282005-11-17 00:38:29 +01001054 continue;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001055 }
Andreas Ericsson4dbd1352005-11-17 20:37:14 +01001056 if (!strcmp(arg, "--strict-paths")) {
1057 strict_paths = 1;
1058 continue;
1059 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001060 if (!prefixcmp(arg, "--base-path=")) {
Petr Baudisb21c31c2005-12-23 02:27:40 +01001061 base_path = arg+12;
1062 continue;
1063 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001064 if (!prefixcmp(arg, "--interpolated-path=")) {
Jon Loeliger49ba83f2006-09-19 20:31:51 -05001065 interpolated_path = arg+20;
1066 continue;
1067 }
Mark Wooding1955fab2006-02-03 20:27:04 +00001068 if (!strcmp(arg, "--reuseaddr")) {
1069 reuseaddr = 1;
1070 continue;
1071 }
Junio C Hamano603968d2006-02-04 22:27:29 -08001072 if (!strcmp(arg, "--user-path")) {
1073 user_path = "";
1074 continue;
1075 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001076 if (!prefixcmp(arg, "--user-path=")) {
Junio C Hamano603968d2006-02-04 22:27:29 -08001077 user_path = arg + 12;
1078 continue;
1079 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001080 if (!prefixcmp(arg, "--pid-file=")) {
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +02001081 pid_file = arg + 11;
1082 continue;
1083 }
Matthias Lederhofera5262762006-07-13 18:47:13 +02001084 if (!strcmp(arg, "--detach")) {
1085 detach = 1;
1086 log_syslog = 1;
1087 continue;
1088 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001089 if (!prefixcmp(arg, "--user=")) {
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001090 user_name = arg + 7;
1091 continue;
1092 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001093 if (!prefixcmp(arg, "--group=")) {
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001094 group_name = arg + 8;
1095 continue;
1096 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001097 if (!prefixcmp(arg, "--enable=")) {
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001098 enable_service(arg + 9, 1);
1099 continue;
1100 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001101 if (!prefixcmp(arg, "--disable=")) {
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001102 enable_service(arg + 10, 0);
1103 continue;
1104 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001105 if (!prefixcmp(arg, "--allow-override=")) {
Junio C Hamano74c0cc22006-08-20 19:03:50 -07001106 make_service_overridable(arg + 17, 1);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001107 continue;
1108 }
Junio C Hamanocc44c762007-02-20 01:53:29 -08001109 if (!prefixcmp(arg, "--forbid-override=")) {
Junio C Hamano74c0cc22006-08-20 19:03:50 -07001110 make_service_overridable(arg + 18, 0);
Junio C Hamanod819e4e2006-08-20 19:03:13 -07001111 continue;
1112 }
H. Peter Anvin4ae95682005-09-26 19:10:55 -07001113 if (!strcmp(arg, "--")) {
1114 ok_paths = &argv[i+1];
1115 break;
1116 } else if (arg[0] != '-') {
1117 ok_paths = &argv[i];
1118 break;
1119 }
Linus Torvaldse64e1b72005-07-15 09:32:16 -07001120
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001121 usage(daemon_usage);
1122 }
1123
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001124 if (inetd_mode && (group_name || user_name))
1125 die("--user and --group are incompatible with --inetd");
1126
Jon Loeligerdd467622006-09-26 09:47:43 -05001127 if (inetd_mode && (listen_port || listen_addr))
1128 die("--listen= and --port= are incompatible with --inetd");
1129 else if (listen_port == 0)
1130 listen_port = DEFAULT_GIT_PORT;
1131
Tilman Sauerbeck678dac62006-08-22 19:37:41 +02001132 if (group_name && !user_name)
1133 die("--group supplied without --user");
1134
1135 if (user_name) {
1136 pass = getpwnam(user_name);
1137 if (!pass)
1138 die("user not found - %s", user_name);
1139
1140 if (!group_name)
1141 gid = pass->pw_gid;
1142 else {
1143 group = getgrnam(group_name);
1144 if (!group)
1145 die("group not found - %s", group_name);
1146
1147 gid = group->gr_gid;
1148 }
1149 }
1150
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +02001151 if (log_syslog) {
Andreas Ericssona8883282005-11-17 00:38:29 +01001152 openlog("git-daemon", 0, LOG_DAEMON);
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +02001153 set_die_routine(daemon_die);
Andreas Ericsson4dbd1352005-11-17 20:37:14 +01001154 }
1155
Matthias Lederhoferad8b4f52006-07-13 12:02:29 +02001156 if (strict_paths && (!ok_paths || !*ok_paths))
1157 die("option --strict-paths requires a whitelist");
1158
lars.doelle@on-line.de7c3693f2005-09-08 03:50:01 +02001159 if (inetd_mode) {
David Woodhouse5b276ee2006-06-20 15:38:13 +01001160 struct sockaddr_storage ss;
1161 struct sockaddr *peer = (struct sockaddr *)&ss;
1162 socklen_t slen = sizeof(ss);
1163
Junio C Hamanoba0012c2006-06-21 16:37:48 -07001164 freopen("/dev/null", "w", stderr);
David Woodhouse5b276ee2006-06-20 15:38:13 +01001165
1166 if (getpeername(0, peer, &slen))
1167 peer = NULL;
1168
1169 return execute(peer);
lars.doelle@on-line.de7c3693f2005-09-08 03:50:01 +02001170 }
Andreas Ericssonbce82302005-11-14 17:41:01 +01001171
Matthias Lederhofera5262762006-07-13 18:47:13 +02001172 if (detach)
1173 daemonize();
1174 else
1175 sanitize_stdfds();
Matthias Lederhofer258e93a2006-07-13 18:32:11 +02001176
Matthias Lederhofer45ed5d72006-07-13 12:18:08 +02001177 if (pid_file)
1178 store_pid(pid_file);
1179
Jon Loeligerdd467622006-09-26 09:47:43 -05001180 return serve(listen_addr, listen_port, pass, gid);
Linus Torvaldsa87e8be2005-07-13 19:45:26 -07001181}