@@ -56,10 +56,10 @@ static const char *user_path;
5656static unsigned int timeout ;
5757static unsigned int init_timeout ;
5858
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 ;
6363
6464static int hostname_lookup_done ;
6565
@@ -68,13 +68,13 @@ static void lookup_hostname(void);
6868static const char * get_canon_hostname (void )
6969{
7070 lookup_hostname ();
71- return canon_hostname ;
71+ return canon_hostname . buf ;
7272}
7373
7474static const char * get_ip_address (void )
7575{
7676 lookup_hostname ();
77- return ip_address ;
77+ return ip_address . buf ;
7878}
7979
8080static 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)
122122 exit (1 );
123123}
124124
125- static void strbuf_addstr_or_null (struct strbuf * sb , const char * s )
126- {
127- if (s )
128- strbuf_addstr (sb , s );
129- }
130-
131125struct expand_path_context {
132126 const char * directory ;
133127};
@@ -138,22 +132,22 @@ static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
138132
139133 switch (placeholder [0 ]) {
140134 case 'H' :
141- strbuf_addstr_or_null (sb , hostname );
135+ strbuf_addbuf (sb , & hostname );
142136 return 1 ;
143137 case 'C' :
144138 if (placeholder [1 ] == 'H' ) {
145- strbuf_addstr_or_null (sb , get_canon_hostname ());
139+ strbuf_addstr (sb , get_canon_hostname ());
146140 return 2 ;
147141 }
148142 break ;
149143 case 'I' :
150144 if (placeholder [1 ] == 'P' ) {
151- strbuf_addstr_or_null (sb , get_ip_address ());
145+ strbuf_addstr (sb , get_ip_address ());
152146 return 2 ;
153147 }
154148 break ;
155149 case 'P' :
156- strbuf_addstr_or_null (sb , tcp_port );
150+ strbuf_addbuf (sb , & tcp_port );
157151 return 1 ;
158152 case 'D' :
159153 strbuf_addstr (sb , context -> directory );
@@ -301,16 +295,14 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
301295 char * eol ;
302296 int seen_errors = 0 ;
303297
304- #define STRARG (x ) ((x) ? (x) : "")
305298 * arg ++ = access_hook ;
306299 * arg ++ = service -> name ;
307300 * 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 ;
312305 * arg = NULL ;
313- #undef STRARG
314306
315307 child .use_shell = 1 ;
316308 child .argv = argv ;
@@ -542,7 +534,7 @@ static void parse_host_and_port(char *hostport, char **host,
542534 * trailing and leading dots, which means that the client cannot escape
543535 * our base path via ".." traversal.
544536 */
545- static void sanitize_client_strbuf (struct strbuf * out , const char * in )
537+ static void sanitize_client (struct strbuf * out , const char * in )
546538{
547539 for (; * in ; in ++ ) {
548540 if (* in == '/' )
@@ -556,23 +548,14 @@ static void sanitize_client_strbuf(struct strbuf *out, const char *in)
556548 strbuf_setlen (out , out -> len - 1 );
557549}
558550
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-
566551/*
567552 * Like sanitize_client, but we also perform any canonicalization
568553 * to make life easier on the admin.
569554 */
570- static char * canonicalize_client (const char * in )
555+ static void canonicalize_client (struct strbuf * out , const char * in )
571556{
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 );
576559}
577560
578561/*
@@ -595,11 +578,11 @@ static void parse_host_arg(char *extra_args, int buflen)
595578 char * port ;
596579 parse_host_and_port (val , & host , & port );
597580 if (port ) {
598- free ( tcp_port );
599- tcp_port = sanitize_client (port );
581+ strbuf_reset ( & tcp_port );
582+ sanitize_client (& tcp_port , port );
600583 }
601- free ( hostname );
602- hostname = canonicalize_client (host );
584+ strbuf_reset ( & hostname );
585+ canonicalize_client (& hostname , host );
603586 hostname_lookup_done = 0 ;
604587 }
605588
@@ -616,7 +599,7 @@ static void parse_host_arg(char *extra_args, int buflen)
616599 */
617600static void lookup_hostname (void )
618601{
619- if (!hostname_lookup_done && hostname ) {
602+ if (!hostname_lookup_done && hostname . len ) {
620603#ifndef NO_IPV6
621604 struct addrinfo hints ;
622605 struct addrinfo * ai ;
@@ -626,19 +609,21 @@ static void lookup_hostname(void)
626609 memset (& hints , 0 , sizeof (hints ));
627610 hints .ai_flags = AI_CANONNAME ;
628611
629- gai = getaddrinfo (hostname , NULL , & hints , & ai );
612+ gai = getaddrinfo (hostname . buf , NULL , & hints , & ai );
630613 if (!gai ) {
631614 struct sockaddr_in * sin_addr = (void * )ai -> ai_addr ;
632615
633616 inet_ntop (AF_INET , & sin_addr -> sin_addr ,
634617 addrbuf , sizeof (addrbuf ));
635- free ( ip_address );
636- ip_address = xstrdup ( addrbuf );
618+ strbuf_reset ( & ip_address );
619+ strbuf_addstr ( & ip_address , addrbuf );
637620
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 );
642627
643628 freeaddrinfo (ai );
644629 }
@@ -648,7 +633,7 @@ static void lookup_hostname(void)
648633 char * * ap ;
649634 static char addrbuf [HOST_NAME_MAX + 1 ];
650635
651- hent = gethostbyname (hostname );
636+ hent = gethostbyname (hostname . buf );
652637 if (hent ) {
653638 ap = hent -> h_addr_list ;
654639 memset (& sa , 0 , sizeof sa );
@@ -659,10 +644,10 @@ static void lookup_hostname(void)
659644 inet_ntop (hent -> h_addrtype , & sa .sin_addr ,
660645 addrbuf , sizeof (addrbuf ));
661646
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 );
666651 }
667652#endif
668653 hostname_lookup_done = 1 ;
@@ -693,11 +678,10 @@ static int execute(void)
693678 pktlen -- ;
694679 }
695680
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 );
701685
702686 if (len != pktlen )
703687 parse_host_arg (line + len + 1 , pktlen - len - 1 );
0 commit comments