daemon: use strbuf for hostname info
Convert hostname, canon_hostname, ip_address and tcp_port to strbuf. This allows to get rid of the helpers strbuf_addstr_or_null() and STRARG because a strbuf always represents a valid (initially empty) string. sanitize_client() is not needed anymore and sanitize_client_strbuf() takes its place and name. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
4c3dbbf722
commit
7a646cec5b
98
daemon.c
98
daemon.c
@ -56,10 +56,10 @@ static const char *user_path;
|
|||||||
static unsigned int timeout;
|
static unsigned int timeout;
|
||||||
static unsigned int init_timeout;
|
static unsigned int init_timeout;
|
||||||
|
|
||||||
static char *hostname;
|
static struct strbuf hostname = STRBUF_INIT;
|
||||||
static char *canon_hostname;
|
static struct strbuf canon_hostname = STRBUF_INIT;
|
||||||
static char *ip_address;
|
static struct strbuf ip_address = STRBUF_INIT;
|
||||||
static char *tcp_port;
|
static struct strbuf tcp_port = STRBUF_INIT;
|
||||||
|
|
||||||
static int hostname_lookup_done;
|
static int hostname_lookup_done;
|
||||||
|
|
||||||
@ -68,13 +68,13 @@ static void lookup_hostname(void);
|
|||||||
static const char *get_canon_hostname(void)
|
static const char *get_canon_hostname(void)
|
||||||
{
|
{
|
||||||
lookup_hostname();
|
lookup_hostname();
|
||||||
return canon_hostname;
|
return canon_hostname.buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *get_ip_address(void)
|
static const char *get_ip_address(void)
|
||||||
{
|
{
|
||||||
lookup_hostname();
|
lookup_hostname();
|
||||||
return ip_address;
|
return ip_address.buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void logreport(int priority, const char *err, va_list params)
|
static void logreport(int priority, const char *err, va_list params)
|
||||||
@ -122,12 +122,6 @@ static void NORETURN daemon_die(const char *err, va_list params)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void strbuf_addstr_or_null(struct strbuf *sb, const char *s)
|
|
||||||
{
|
|
||||||
if (s)
|
|
||||||
strbuf_addstr(sb, s);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct expand_path_context {
|
struct expand_path_context {
|
||||||
const char *directory;
|
const char *directory;
|
||||||
};
|
};
|
||||||
@ -138,22 +132,22 @@ static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
|
|||||||
|
|
||||||
switch (placeholder[0]) {
|
switch (placeholder[0]) {
|
||||||
case 'H':
|
case 'H':
|
||||||
strbuf_addstr_or_null(sb, hostname);
|
strbuf_addbuf(sb, &hostname);
|
||||||
return 1;
|
return 1;
|
||||||
case 'C':
|
case 'C':
|
||||||
if (placeholder[1] == 'H') {
|
if (placeholder[1] == 'H') {
|
||||||
strbuf_addstr_or_null(sb, get_canon_hostname());
|
strbuf_addstr(sb, get_canon_hostname());
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'I':
|
case 'I':
|
||||||
if (placeholder[1] == 'P') {
|
if (placeholder[1] == 'P') {
|
||||||
strbuf_addstr_or_null(sb, get_ip_address());
|
strbuf_addstr(sb, get_ip_address());
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'P':
|
case 'P':
|
||||||
strbuf_addstr_or_null(sb, tcp_port);
|
strbuf_addbuf(sb, &tcp_port);
|
||||||
return 1;
|
return 1;
|
||||||
case 'D':
|
case 'D':
|
||||||
strbuf_addstr(sb, context->directory);
|
strbuf_addstr(sb, context->directory);
|
||||||
@ -301,16 +295,14 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
|
|||||||
char *eol;
|
char *eol;
|
||||||
int seen_errors = 0;
|
int seen_errors = 0;
|
||||||
|
|
||||||
#define STRARG(x) ((x) ? (x) : "")
|
|
||||||
*arg++ = access_hook;
|
*arg++ = access_hook;
|
||||||
*arg++ = service->name;
|
*arg++ = service->name;
|
||||||
*arg++ = path;
|
*arg++ = path;
|
||||||
*arg++ = STRARG(hostname);
|
*arg++ = hostname.buf;
|
||||||
*arg++ = STRARG(get_canon_hostname());
|
*arg++ = get_canon_hostname();
|
||||||
*arg++ = STRARG(get_ip_address());
|
*arg++ = get_ip_address();
|
||||||
*arg++ = STRARG(tcp_port);
|
*arg++ = tcp_port.buf;
|
||||||
*arg = NULL;
|
*arg = NULL;
|
||||||
#undef STRARG
|
|
||||||
|
|
||||||
child.use_shell = 1;
|
child.use_shell = 1;
|
||||||
child.argv = argv;
|
child.argv = argv;
|
||||||
@ -542,7 +534,7 @@ static void parse_host_and_port(char *hostport, char **host,
|
|||||||
* trailing and leading dots, which means that the client cannot escape
|
* trailing and leading dots, which means that the client cannot escape
|
||||||
* our base path via ".." traversal.
|
* our base path via ".." traversal.
|
||||||
*/
|
*/
|
||||||
static void sanitize_client_strbuf(struct strbuf *out, const char *in)
|
static void sanitize_client(struct strbuf *out, const char *in)
|
||||||
{
|
{
|
||||||
for (; *in; in++) {
|
for (; *in; in++) {
|
||||||
if (*in == '/')
|
if (*in == '/')
|
||||||
@ -556,23 +548,14 @@ static void sanitize_client_strbuf(struct strbuf *out, const char *in)
|
|||||||
strbuf_setlen(out, out->len - 1);
|
strbuf_setlen(out, out->len - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *sanitize_client(const char *in)
|
|
||||||
{
|
|
||||||
struct strbuf out = STRBUF_INIT;
|
|
||||||
sanitize_client_strbuf(&out, in);
|
|
||||||
return strbuf_detach(&out, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Like sanitize_client, but we also perform any canonicalization
|
* Like sanitize_client, but we also perform any canonicalization
|
||||||
* to make life easier on the admin.
|
* to make life easier on the admin.
|
||||||
*/
|
*/
|
||||||
static char *canonicalize_client(const char *in)
|
static void canonicalize_client(struct strbuf *out, const char *in)
|
||||||
{
|
{
|
||||||
struct strbuf out = STRBUF_INIT;
|
sanitize_client(out, in);
|
||||||
sanitize_client_strbuf(&out, in);
|
strbuf_tolower(out);
|
||||||
strbuf_tolower(&out);
|
|
||||||
return strbuf_detach(&out, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -595,11 +578,11 @@ static void parse_host_arg(char *extra_args, int buflen)
|
|||||||
char *port;
|
char *port;
|
||||||
parse_host_and_port(val, &host, &port);
|
parse_host_and_port(val, &host, &port);
|
||||||
if (port) {
|
if (port) {
|
||||||
free(tcp_port);
|
strbuf_reset(&tcp_port);
|
||||||
tcp_port = sanitize_client(port);
|
sanitize_client(&tcp_port, port);
|
||||||
}
|
}
|
||||||
free(hostname);
|
strbuf_reset(&hostname);
|
||||||
hostname = canonicalize_client(host);
|
canonicalize_client(&hostname, host);
|
||||||
hostname_lookup_done = 0;
|
hostname_lookup_done = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -616,7 +599,7 @@ static void parse_host_arg(char *extra_args, int buflen)
|
|||||||
*/
|
*/
|
||||||
static void lookup_hostname(void)
|
static void lookup_hostname(void)
|
||||||
{
|
{
|
||||||
if (!hostname_lookup_done && hostname) {
|
if (!hostname_lookup_done && hostname.len) {
|
||||||
#ifndef NO_IPV6
|
#ifndef NO_IPV6
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *ai;
|
struct addrinfo *ai;
|
||||||
@ -626,19 +609,21 @@ static void lookup_hostname(void)
|
|||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_flags = AI_CANONNAME;
|
hints.ai_flags = AI_CANONNAME;
|
||||||
|
|
||||||
gai = getaddrinfo(hostname, NULL, &hints, &ai);
|
gai = getaddrinfo(hostname.buf, NULL, &hints, &ai);
|
||||||
if (!gai) {
|
if (!gai) {
|
||||||
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
|
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
|
||||||
|
|
||||||
inet_ntop(AF_INET, &sin_addr->sin_addr,
|
inet_ntop(AF_INET, &sin_addr->sin_addr,
|
||||||
addrbuf, sizeof(addrbuf));
|
addrbuf, sizeof(addrbuf));
|
||||||
free(ip_address);
|
strbuf_reset(&ip_address);
|
||||||
ip_address = xstrdup(addrbuf);
|
strbuf_addstr(&ip_address, addrbuf);
|
||||||
|
|
||||||
free(canon_hostname);
|
strbuf_reset(&canon_hostname);
|
||||||
canon_hostname = ai->ai_canonname ?
|
if (ai->ai_canonname)
|
||||||
sanitize_client(ai->ai_canonname) :
|
sanitize_client(&canon_hostname,
|
||||||
xstrdup(ip_address);
|
ai->ai_canonname);
|
||||||
|
else
|
||||||
|
strbuf_addbuf(&canon_hostname, &ip_address);
|
||||||
|
|
||||||
freeaddrinfo(ai);
|
freeaddrinfo(ai);
|
||||||
}
|
}
|
||||||
@ -648,7 +633,7 @@ static void lookup_hostname(void)
|
|||||||
char **ap;
|
char **ap;
|
||||||
static char addrbuf[HOST_NAME_MAX + 1];
|
static char addrbuf[HOST_NAME_MAX + 1];
|
||||||
|
|
||||||
hent = gethostbyname(hostname);
|
hent = gethostbyname(hostname.buf);
|
||||||
if (hent) {
|
if (hent) {
|
||||||
ap = hent->h_addr_list;
|
ap = hent->h_addr_list;
|
||||||
memset(&sa, 0, sizeof sa);
|
memset(&sa, 0, sizeof sa);
|
||||||
@ -659,10 +644,10 @@ static void lookup_hostname(void)
|
|||||||
inet_ntop(hent->h_addrtype, &sa.sin_addr,
|
inet_ntop(hent->h_addrtype, &sa.sin_addr,
|
||||||
addrbuf, sizeof(addrbuf));
|
addrbuf, sizeof(addrbuf));
|
||||||
|
|
||||||
free(canon_hostname);
|
strbuf_reset(&canon_hostname);
|
||||||
canon_hostname = sanitize_client(hent->h_name);
|
sanitize_client(&canon_hostname, hent->h_name);
|
||||||
free(ip_address);
|
strbuf_reset(&ip_address);
|
||||||
ip_address = xstrdup(addrbuf);
|
strbuf_addstr(&ip_address, addrbuf);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
hostname_lookup_done = 1;
|
hostname_lookup_done = 1;
|
||||||
@ -693,11 +678,10 @@ static int execute(void)
|
|||||||
pktlen--;
|
pktlen--;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(hostname);
|
strbuf_release(&hostname);
|
||||||
free(canon_hostname);
|
strbuf_release(&canon_hostname);
|
||||||
free(ip_address);
|
strbuf_release(&ip_address);
|
||||||
free(tcp_port);
|
strbuf_release(&tcp_port);
|
||||||
hostname = canon_hostname = ip_address = tcp_port = NULL;
|
|
||||||
|
|
||||||
if (len != pktlen)
|
if (len != pktlen)
|
||||||
parse_host_arg(line + len + 1, pktlen - len - 1);
|
parse_host_arg(line + len + 1, pktlen - len - 1);
|
||||||
|
Loading…
Reference in New Issue
Block a user