@@ -56,10 +56,10 @@ static const char *user_path;
56
56
static unsigned int timeout ;
57
57
static unsigned int init_timeout ;
58
58
59
- static char * hostname ;
60
- static char * canon_hostname ;
61
- static char * ip_address ;
62
- static char * tcp_port ;
59
+ static struct strbuf hostname = STRBUF_INIT ;
60
+ static struct strbuf canon_hostname = STRBUF_INIT ;
61
+ static struct strbuf ip_address = STRBUF_INIT ;
62
+ static struct strbuf tcp_port = STRBUF_INIT ;
63
63
64
64
static int hostname_lookup_done ;
65
65
@@ -68,13 +68,13 @@ static void lookup_hostname(void);
68
68
static const char * get_canon_hostname (void )
69
69
{
70
70
lookup_hostname ();
71
- return canon_hostname ;
71
+ return canon_hostname . buf ;
72
72
}
73
73
74
74
static const char * get_ip_address (void )
75
75
{
76
76
lookup_hostname ();
77
- return ip_address ;
77
+ return ip_address . buf ;
78
78
}
79
79
80
80
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)
122
122
exit (1 );
123
123
}
124
124
125
- static void strbuf_addstr_or_null (struct strbuf * sb , const char * s )
126
- {
127
- if (s )
128
- strbuf_addstr (sb , s );
129
- }
130
-
131
125
struct expand_path_context {
132
126
const char * directory ;
133
127
};
@@ -138,22 +132,22 @@ static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
138
132
139
133
switch (placeholder [0 ]) {
140
134
case 'H' :
141
- strbuf_addstr_or_null (sb , hostname );
135
+ strbuf_addbuf (sb , & hostname );
142
136
return 1 ;
143
137
case 'C' :
144
138
if (placeholder [1 ] == 'H' ) {
145
- strbuf_addstr_or_null (sb , get_canon_hostname ());
139
+ strbuf_addstr (sb , get_canon_hostname ());
146
140
return 2 ;
147
141
}
148
142
break ;
149
143
case 'I' :
150
144
if (placeholder [1 ] == 'P' ) {
151
- strbuf_addstr_or_null (sb , get_ip_address ());
145
+ strbuf_addstr (sb , get_ip_address ());
152
146
return 2 ;
153
147
}
154
148
break ;
155
149
case 'P' :
156
- strbuf_addstr_or_null (sb , tcp_port );
150
+ strbuf_addbuf (sb , & tcp_port );
157
151
return 1 ;
158
152
case 'D' :
159
153
strbuf_addstr (sb , context -> directory );
@@ -301,16 +295,14 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
301
295
char * eol ;
302
296
int seen_errors = 0 ;
303
297
304
- #define STRARG (x ) ((x) ? (x) : "")
305
298
* arg ++ = access_hook ;
306
299
* arg ++ = service -> name ;
307
300
* arg ++ = path ;
308
- * arg ++ = STRARG ( hostname ) ;
309
- * arg ++ = STRARG ( get_canon_hostname () );
310
- * arg ++ = STRARG ( get_ip_address () );
311
- * arg ++ = STRARG ( tcp_port ) ;
301
+ * arg ++ = hostname . buf ;
302
+ * arg ++ = get_canon_hostname ();
303
+ * arg ++ = get_ip_address ();
304
+ * arg ++ = tcp_port . buf ;
312
305
* arg = NULL ;
313
- #undef STRARG
314
306
315
307
child .use_shell = 1 ;
316
308
child .argv = argv ;
@@ -542,7 +534,7 @@ static void parse_host_and_port(char *hostport, char **host,
542
534
* trailing and leading dots, which means that the client cannot escape
543
535
* our base path via ".." traversal.
544
536
*/
545
- static void sanitize_client_strbuf (struct strbuf * out , const char * in )
537
+ static void sanitize_client (struct strbuf * out , const char * in )
546
538
{
547
539
for (; * in ; in ++ ) {
548
540
if (* in == '/' )
@@ -556,23 +548,14 @@ static void sanitize_client_strbuf(struct strbuf *out, const char *in)
556
548
strbuf_setlen (out , out -> len - 1 );
557
549
}
558
550
559
- static char * sanitize_client (const char * in )
560
- {
561
- struct strbuf out = STRBUF_INIT ;
562
- sanitize_client_strbuf (& out , in );
563
- return strbuf_detach (& out , NULL );
564
- }
565
-
566
551
/*
567
552
* Like sanitize_client, but we also perform any canonicalization
568
553
* to make life easier on the admin.
569
554
*/
570
- static char * canonicalize_client (const char * in )
555
+ static void canonicalize_client (struct strbuf * out , const char * in )
571
556
{
572
- struct strbuf out = STRBUF_INIT ;
573
- sanitize_client_strbuf (& out , in );
574
- strbuf_tolower (& out );
575
- return strbuf_detach (& out , NULL );
557
+ sanitize_client (out , in );
558
+ strbuf_tolower (out );
576
559
}
577
560
578
561
/*
@@ -595,11 +578,11 @@ static void parse_host_arg(char *extra_args, int buflen)
595
578
char * port ;
596
579
parse_host_and_port (val , & host , & port );
597
580
if (port ) {
598
- free ( tcp_port );
599
- tcp_port = sanitize_client (port );
581
+ strbuf_reset ( & tcp_port );
582
+ sanitize_client (& tcp_port , port );
600
583
}
601
- free ( hostname );
602
- hostname = canonicalize_client (host );
584
+ strbuf_reset ( & hostname );
585
+ canonicalize_client (& hostname , host );
603
586
hostname_lookup_done = 0 ;
604
587
}
605
588
@@ -616,7 +599,7 @@ static void parse_host_arg(char *extra_args, int buflen)
616
599
*/
617
600
static void lookup_hostname (void )
618
601
{
619
- if (!hostname_lookup_done && hostname ) {
602
+ if (!hostname_lookup_done && hostname . len ) {
620
603
#ifndef NO_IPV6
621
604
struct addrinfo hints ;
622
605
struct addrinfo * ai ;
@@ -626,19 +609,21 @@ static void lookup_hostname(void)
626
609
memset (& hints , 0 , sizeof (hints ));
627
610
hints .ai_flags = AI_CANONNAME ;
628
611
629
- gai = getaddrinfo (hostname , NULL , & hints , & ai );
612
+ gai = getaddrinfo (hostname . buf , NULL , & hints , & ai );
630
613
if (!gai ) {
631
614
struct sockaddr_in * sin_addr = (void * )ai -> ai_addr ;
632
615
633
616
inet_ntop (AF_INET , & sin_addr -> sin_addr ,
634
617
addrbuf , sizeof (addrbuf ));
635
- free ( ip_address );
636
- ip_address = xstrdup ( addrbuf );
618
+ strbuf_reset ( & ip_address );
619
+ strbuf_addstr ( & ip_address , addrbuf );
637
620
638
- free (canon_hostname );
639
- canon_hostname = ai -> ai_canonname ?
640
- sanitize_client (ai -> ai_canonname ) :
641
- xstrdup (ip_address );
621
+ strbuf_reset (& canon_hostname );
622
+ if (ai -> ai_canonname )
623
+ sanitize_client (& canon_hostname ,
624
+ ai -> ai_canonname );
625
+ else
626
+ strbuf_addbuf (& canon_hostname , & ip_address );
642
627
643
628
freeaddrinfo (ai );
644
629
}
@@ -648,7 +633,7 @@ static void lookup_hostname(void)
648
633
char * * ap ;
649
634
static char addrbuf [HOST_NAME_MAX + 1 ];
650
635
651
- hent = gethostbyname (hostname );
636
+ hent = gethostbyname (hostname . buf );
652
637
if (hent ) {
653
638
ap = hent -> h_addr_list ;
654
639
memset (& sa , 0 , sizeof sa );
@@ -659,10 +644,10 @@ static void lookup_hostname(void)
659
644
inet_ntop (hent -> h_addrtype , & sa .sin_addr ,
660
645
addrbuf , sizeof (addrbuf ));
661
646
662
- free ( canon_hostname );
663
- canon_hostname = sanitize_client (hent -> h_name );
664
- free ( ip_address );
665
- ip_address = xstrdup ( addrbuf );
647
+ strbuf_reset ( & canon_hostname );
648
+ sanitize_client (& canon_hostname , hent -> h_name );
649
+ strbuf_reset ( & ip_address );
650
+ strbuf_addstr ( & ip_address , addrbuf );
666
651
}
667
652
#endif
668
653
hostname_lookup_done = 1 ;
@@ -693,11 +678,10 @@ static int execute(void)
693
678
pktlen -- ;
694
679
}
695
680
696
- free (hostname );
697
- free (canon_hostname );
698
- free (ip_address );
699
- free (tcp_port );
700
- hostname = canon_hostname = ip_address = tcp_port = NULL ;
681
+ strbuf_release (& hostname );
682
+ strbuf_release (& canon_hostname );
683
+ strbuf_release (& ip_address );
684
+ strbuf_release (& tcp_port );
701
685
702
686
if (len != pktlen )
703
687
parse_host_arg (line + len + 1 , pktlen - len - 1 );
0 commit comments