daemon: use strbuf_expand() instead of interpolate()
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Nov 23, 2008 at 00:15 UTC
9d7ca667466b9e1a8160d20cce7c06d09213ab6b
1 file changed
+54
-55
daemon.c
+54
-55
@@ -1,7 +1,6 @@
1
#include "cache.h"
2
#include "pkt-line.h"
3
#include "exec_cmd.h"
4
-#include "interpolate.h"
4
5
#include <syslog.h>
6
@@ -54,26 +53,11 @@ static const char *user_path;
53
static unsigned int timeout;
54
static unsigned int init_timeout;
55
57
-/*
58
- * Static table for now. Ugh.
59
- * Feel free to make dynamic as needed.
60
- */
61
-#define INTERP_SLOT_HOST (0)
62
-#define INTERP_SLOT_CANON_HOST (1)
63
-#define INTERP_SLOT_IP (2)
64
-#define INTERP_SLOT_PORT (3)
65
-#define INTERP_SLOT_DIR (4)
66
-#define INTERP_SLOT_PERCENT (5)
67
-
68
-static struct interp interp_table[] = {
69
- { "%H", 0},
70
- { "%CH", 0},
71
- { "%IP", 0},
72
- { "%P", 0},
73
- { "%D", 0},
74
- { "%%", 0},
75
-};
76
-
56
+static char *hostname;
57
+static char *canon_hostname;
58
+static char *ip_address;
59
+static char *tcp_port;
60
+static char *directory;
61
62
static void logreport(int priority, const char *err, va_list params)
63
{
@@ -163,7 +147,7 @@ static int avoid_alias(char *p)
147
}
148
}
149
166
-static char *path_ok(struct interp *itable)
150
+static char *path_ok(void)
151
{
152
static char rpath[PATH_MAX];
153
static char interp_path[PATH_MAX];
@@ -171,7 +155,7 @@ static char *path_ok(struct interp *itable)
155
char *path;
156
char *dir;
157
174
- dir = itable[INTERP_SLOT_DIR].value;
158
+ dir = directory;
159
160
if (avoid_alias(dir)) {
161
logerror("'%s': aliased", dir);
@@ -201,14 +185,27 @@ static char *path_ok(struct interp *itable)
185
}
186
}
187
else if (interpolated_path && saw_extended_args) {
188
+ struct strbuf expanded_path = STRBUF_INIT;
189
+ struct strbuf_expand_dict_entry dict[] = {
190
+ { "H", hostname },
191
+ { "CH", canon_hostname },
192
+ { "IP", ip_address },
193
+ { "P", tcp_port },
194
+ { "D", directory },
195
+ { "%", "%" },
196
+ { NULL }
197
+ };
198
+
199
if (*dir != '/') {
200
/* Allow only absolute */
201
logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
202
return NULL;
203
}
204
210
- interpolate(interp_path, PATH_MAX, interpolated_path,
211
- interp_table, ARRAY_SIZE(interp_table));
205
+ strbuf_expand(&expanded_path, interpolated_path,
206
+ strbuf_expand_dict_cb, &dict);
207
+ strlcpy(interp_path, expanded_path.buf, PATH_MAX);
208
+ strbuf_release(&expanded_path);
209
loginfo("Interpolated dir '%s'", interp_path);
210
211
dir = interp_path;
@@ -233,7 +230,7 @@ static char *path_ok(struct interp *itable)
230
* prefixing the base path
231
*/
232
if (base_path && base_path_relaxed && !retried_path) {
236
- dir = itable[INTERP_SLOT_DIR].value;
233
+ dir = directory;
234
retried_path = 1;
235
continue;
236
}
@@ -299,14 +296,12 @@ static int git_daemon_config(const char *var, const char *value, void *cb)
296
return 0;
297
}
298
302
-static int run_service(struct interp *itable, struct daemon_service *service)
299
+static int run_service(struct daemon_service *service)
300
{
301
const char *path;
302
int enabled = service->enabled;
303
307
- loginfo("Request %s for '%s'",
308
- service->name,
309
- itable[INTERP_SLOT_DIR].value);
304
+ loginfo("Request %s for '%s'", service->name, directory);
305
306
if (!enabled && !service->overridable) {
307
logerror("'%s': service not enabled.", service->name);
@@ -314,7 +309,7 @@ static int run_service(struct interp *itable, struct daemon_service *service)
309
return -1;
310
}
311
317
- if (!(path = path_ok(itable)))
312
+ if (!(path = path_ok()))
313
return -1;
314
315
/*
@@ -413,9 +408,8 @@ static void make_service_overridable(const char *name, int ena)
408
409
/*
410
* Separate the "extra args" information as supplied by the client connection.
416
- * Any resulting data is squirreled away in the given interpolation table.
411
*/
418
-static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
412
+static void parse_extra_args(char *extra_args, int buflen)
413
{
414
char *val;
415
int vallen;
@@ -433,9 +427,11 @@ static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
427
if (port) {
428
*port = 0;
429
port++;
436
- interp_set_entry(table, INTERP_SLOT_PORT, port);
430
+ free(tcp_port);
431
+ tcp_port = xstrdup(port);
432
}
438
- interp_set_entry(table, INTERP_SLOT_HOST, host);
433
+ free(hostname);
434
+ hostname = xstrdup(host);
435
}
436
437
/* On to the next one */
@@ -444,14 +440,14 @@ static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
440
}
441
}
442
447
-static void fill_in_extra_table_entries(struct interp *itable)
443
+static void fill_in_extra_table_entries(void)
444
{
445
char *hp;
446
447
/*
448
* Replace literal host with lowercase-ized hostname.
449
*/
454
- hp = interp_table[INTERP_SLOT_HOST].value;
450
+ hp = hostname;
451
if (!hp)
452
return;
453
for ( ; *hp; hp++)
@@ -470,17 +466,17 @@ static void fill_in_extra_table_entries(struct interp *itable)
466
memset(&hints, 0, sizeof(hints));
467
hints.ai_flags = AI_CANONNAME;
468
473
- gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0);
469
+ gai = getaddrinfo(hostname, 0, &hints, &ai0);
470
if (!gai) {
471
for (ai = ai0; ai; ai = ai->ai_next) {
472
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
473
474
inet_ntop(AF_INET, &sin_addr->sin_addr,
475
addrbuf, sizeof(addrbuf));
480
- interp_set_entry(interp_table,
481
- INTERP_SLOT_CANON_HOST, ai->ai_canonname);
482
- interp_set_entry(interp_table,
483
- INTERP_SLOT_IP, addrbuf);
476
+ free(canon_hostname);
477
+ canon_hostname = xstrdup(ai->ai_canonname);
478
+ free(ip_address);
479
+ ip_address = xstrdup(addrbuf);
480
break;
481
}
482
freeaddrinfo(ai0);
@@ -493,7 +489,7 @@ static void fill_in_extra_table_entries(struct interp *itable)
489
char **ap;
490
static char addrbuf[HOST_NAME_MAX + 1];
491
496
- hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value);
492
+ hent = gethostbyname(hostname);
493
494
ap = hent->h_addr_list;
495
memset(&sa, 0, sizeof sa);
@@ -504,8 +500,10 @@ static void fill_in_extra_table_entries(struct interp *itable)
500
inet_ntop(hent->h_addrtype, &sa.sin_addr,
501
addrbuf, sizeof(addrbuf));
502
507
- interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name);
508
- interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf);
503
+ free(canon_hostname);
504
+ canon_hostname = xstrdup(hent->h_name);
505
+ free(ip_address);
506
+ ip_address = xstrdup(addrbuf);
507
}
508
#endif
509
}
@@ -557,15 +555,16 @@ static int execute(struct sockaddr *addr)
555
pktlen--;
556
}
557
560
- /*
561
- * Initialize the path interpolation table for this connection.
562
- */
563
- interp_clear_table(interp_table, ARRAY_SIZE(interp_table));
564
- interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%");
558
+ free(hostname);
559
+ free(canon_hostname);
560
+ free(ip_address);
561
+ free(tcp_port);
562
+ free(directory);
563
+ hostname = canon_hostname = ip_address = tcp_port = directory = NULL;
564
565
if (len != pktlen) {
567
- parse_extra_args(interp_table, line + len + 1, pktlen - len - 1);
568
- fill_in_extra_table_entries(interp_table);
566
+ parse_extra_args(line + len + 1, pktlen - len - 1);
567
+ fill_in_extra_table_entries();
568
}
569
570
for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
@@ -578,9 +577,9 @@ static int execute(struct sockaddr *addr)
577
* Note: The directory here is probably context sensitive,
578
* and might depend on the actual service being performed.
579
*/
581
- interp_set_entry(interp_table,
582
- INTERP_SLOT_DIR, line + namelen + 5);
583
- return run_service(interp_table, s);
580
+ free(directory);
581
+ directory = xstrdup(line + namelen + 5);
582
+ return run_service(s);
583
}
584
}
585