#include "cache.h"
#include "pkt-line.h"
#include "exec_cmd.h"
#include "interpolate.h"
#include <syslog.h>
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
#ifndef NI_MAXSERV
#define NI_MAXSERV 32
#endif
static int log_syslog;
static int verbose;
static int reuseaddr;
static const char daemon_usage[] =
"git-daemon [--verbose] [--syslog] [--export-all]\n"
" [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
" [--base-path=path] [--base-path-relaxed]\n"
" [--user-path | --user-path=path]\n"
" [--interpolated-path=path]\n"
" [--reuseaddr] [--detach] [--pid-file=file]\n"
" [--[enable|disable|allow-override|forbid-override]=service]\n"
" [--inetd | [--listen=host_or_ipaddr] [--port=n]\n"
" [--user=user [--group=group]]\n"
" [directory...]";
/* List of acceptable pathname prefixes */
static char **ok_paths;
static int strict_paths;
/* If this is set, git-daemon-export-ok is not required */
static int export_all_trees;
/* Take all paths relative to this one if non-NULL */
static char *base_path;
static char *interpolated_path;
static int base_path_relaxed;
/* Flag indicating client sent extra args. */
static int saw_extended_args;
/* If defined, ~user notation is allowed and the string is inserted
* after ~user/. E.g. a request to git://host/~alice/frotz would
* go to /home/alice/pub_git/frotz with --user-path=pub_git.
*/
static const char *user_path;
/* Timeout, and initial timeout */
static unsigned int timeout;
static unsigned int init_timeout;
/*
* Static table for now. Ugh.
* Feel free to make dynamic as needed.
*/
#define INTERP_SLOT_HOST (0)
#define INTERP_SLOT_CANON_HOST (1)
#define INTERP_SLOT_IP (2)
#define INTERP_SLOT_PORT (3)
#define INTERP_SLOT_DIR (4)
#define INTERP_SLOT_PERCENT (5)
static struct interp interp_table[] = {
{ "%H", 0},
{ "%CH", 0},
{ "%IP", 0},
{ "%P", 0},
{ "%D", 0},
{ "%%", 0},
};
static void logreport(int priority, const char *err, va_list params)
{
/* We should do a single write so that it is atomic and output
* of several processes do not get intermingled. */
char buf[1024];
int buflen;
int maxlen, msglen;
/* sizeof(buf) should be big enough for "[pid] \n" */
buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
msglen = vsnprintf(buf + buflen, maxlen, err, params);
if (log_syslog) {
syslog(priority, "%s", buf);
return;
}
/* maxlen counted our own LF but also counts space given to
* vsnprintf for the terminating NUL. We want to make sure that
* we have space for our own LF and NUL after the "meat" of the
* message, so truncate it at maxlen - 1.
*/
if (msglen > maxlen - 1)
msglen = maxlen - 1;
else if (msglen < 0)
msglen = 0; /* Protect against weird return values. */
buflen += msglen;
buf[buflen++] = '\n';
buf[buflen] = '\0';
write_in_full(2, buf, buflen);
}
static void logerror(const char *err, ...)
{
va_list params;
va_start(params, err);
logreport(LOG_ERR, err, params);
va_end(params);
}
static void loginfo(const char *err, ...)
{
va_list params;
if (!verbose)
return;
va_start(params, err);
logreport(LOG_INFO, err, params);
va_end(params);
}
static void NORETURN daemon_die(const char *err, va_list params)
{
logreport(LOG_ERR, err, params);
exit(1);
}
static int avoid_alias(char *p)
{
int sl, ndot;
/*
* This resurrects the belts and suspenders paranoia check by HPA
* done in <435560F7.4080006@zytor.com> thread, now enter_repo()
* does not do getcwd() based path canonicalizations.
*
* sl becomes true immediately after seeing '/' and continues to
* be true as long as dots continue after that without intervening
* non-dot character.
*/
if (!p || (*p != '
|