fixes init script for non ipv6 enabled systems #472755
[packages/xinetd.git] / xinetd / service.c
1 /*
2  * (c) Copyright 1992 by Panagiotis Tsirigotis
3  * (c) Sections Copyright 1998-2001 by Rob Braun
4  * All rights reserved.  The file named COPYRIGHT specifies the terms 
5  * and conditions for redistribution.
6  */
7
8 #include "config.h"
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <syslog.h>
14 #include <fcntl.h>
15 #include <netinet/tcp.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <signal.h>
19 #include <time.h>
20 #include <errno.h>
21 #include <netinet/in.h>
22 #include <stdio.h>
23 #ifdef HAVE_MDNS
24 #include "xmdns.h"
25 #endif
26 #ifndef NO_RPC
27  #ifdef HAVE_RPC_PMAP_CLNT_H
28   #ifdef __sun
29    #include <rpc/types.h>
30    #include <rpc/auth.h>
31   #endif
32   #include <rpc/types.h>
33   #include <rpc/xdr.h>
34   #include <rpc/auth.h>
35   #include <rpc/clnt.h>
36   #include <rpc/pmap_clnt.h>
37  #endif
38  #include <rpc/rpc.h>
39 #endif
40
41 #ifdef HAVE_SYS_FILE_H
42 #include <sys/file.h>
43 #endif
44
45 #include "sio.h"
46 #include "service.h"
47 #include "util.h"
48 #include "main.h"
49 #include "sconf.h"
50 #include "msg.h"
51 #include "logctl.h"
52 #include "xconfig.h"
53 #include "special.h"
54
55
56 #define NEW_SVC()              NEW( struct service )
57 #define FREE_SVC( sp )         FREE( sp )
58
59 #define DISABLE( sp )          SVC_STATE((sp)) = SVC_DISABLED
60
61 static void deactivate( const struct service *sp );
62 static int banner_always( const struct service *sp, const connection_s *cp );
63
64 static const struct name_value service_states[] =
65    {
66       { "Not started",        (int) SVC_NOT_STARTED    },
67       { "Active",             (int) SVC_ACTIVE         },
68       { "Disabled",           (int) SVC_DISABLED       },
69       { "Suspended",          (int) SVC_SUSPENDED      },
70       { NULL,                 1                        },
71       { "BAD STATE",          0                        }
72    } ;
73
74
75
76 /*
77  * Allocate a new struct service and initialize it from scp 
78  */
79 struct service *svc_new( struct service_config *scp )
80 {
81    struct service *sp ;
82    const char *func = "svc_new" ;
83
84    sp = NEW_SVC() ;
85    if ( sp == NULL )
86    {
87       out_of_memory( func ) ;
88       return( NULL ) ;
89    }
90    CLEAR( *sp ) ;
91
92    SVC_CONF(sp) = scp ;
93    return( sp ) ;
94 }
95
96
97 struct service *svc_make_special( struct service_config *scp )
98 {
99    struct service      *sp ;
100    const char          *func = "svc_make_special" ;
101
102    if ( ( sp = svc_new( scp ) ) == NULL )
103    {
104       out_of_memory( func ) ;
105       return( NULL ) ;
106    }
107
108    SVC_NOT_GENERIC(sp) = 1 ;
109    SVC_LOG(sp) = ps.rws.program_log ;
110    SVC_REFCOUNT(sp) = 1 ;
111    SVC_STATE(sp) = SVC_ACTIVE ;
112    return( sp ) ;
113 }
114
115
116 void svc_free( struct service *sp )
117 {
118    sc_free( SVC_CONF(sp) ) ;
119    CLEAR( *sp ) ;
120    FREE_SVC( sp ) ;
121 }
122
123
124 static status_e set_fd_modes( struct service *sp )
125 {
126    int sd = SVC_FD( sp ) ;
127    const char *func = "set_fd_modes" ;
128
129    /*
130     * There is a possibility of blocking on a send/write if
131     *
132     * the service does not require forking (==> is internal) AND
133     * it does not accept connections
134     *
135     * To avoid this, we put the descriptor in FNDELAY mode.
136     * (if the service accepts connections, we still need to put the
137     * 'accepted' connection in FNDELAY mode but this is done elsewhere)
138     */
139    if ( ! SVC_FORKS( sp ) && ! SVC_ACCEPTS_CONNECTIONS( sp ) &&
140                               fcntl( sd, F_SETFL, FNDELAY ) == -1 )
141    {
142       msg( LOG_ERR, func,
143          "fcntl failed (%m) for FNDELAY. service = %s", SVC_ID( sp ) ) ;
144       return( FAILED ) ;
145    }
146
147    /*
148     * Always set the close-on-exec flag
149     */
150    if ( fcntl( sd, F_SETFD, FD_CLOEXEC ) == -1 )
151    {
152       msg( LOG_ERR, func,
153          "fcntl failed (%m) for close-on-exec. service = %s", SVC_ID( sp ) ) ;
154       return( FAILED ) ;
155    }
156    return( OK ) ;
157 }
158
159
160 #ifndef NO_RPC
161
162 static status_e activate_rpc( struct service *sp )
163 {
164    union xsockaddr        tsin;
165    socklen_t              sin_len = sizeof(tsin);
166    unsigned long          vers ;
167    struct service_config *scp = SVC_CONF( sp ) ;
168    struct rpc_data       *rdp = SC_RPCDATA( scp ) ;
169    char                  *sid = SC_ID( scp ) ;
170    unsigned               registered_versions = 0 ;
171    int                    sd = SVC_FD( sp ) ;
172    const char            *func = "activate_rpc" ;
173
174    if( SC_BIND_ADDR(scp) != 0 )
175       memcpy( &tsin, SC_BIND_ADDR(scp), sizeof(tsin) );
176    else
177       memset( &tsin, 0, sizeof(tsin));
178
179    if( SC_IPV4( scp ) ) {
180       tsin.sa_in.sin_family = AF_INET ;
181       sin_len = sizeof(struct sockaddr_in);
182    } else if( SC_IPV6( scp ) ) {
183       tsin.sa_in6.sin6_family = AF_INET6 ;
184       sin_len = sizeof(struct sockaddr_in6);
185    }
186
187    if ( bind( sd, &tsin.sa, sin_len ) == -1 )
188    {
189       msg( LOG_ERR, func, "bind failed (%m). service = %s", sid ) ;
190       return( FAILED ) ;
191    }
192
193    /*
194     * Find the port number that was assigned to the socket
195     */
196    if ( getsockname( sd, &tsin.sa, &sin_len ) == -1 )
197    {
198       msg( LOG_ERR, func,
199             "getsockname failed (%m). service = %s", sid ) ;
200       return( FAILED ) ;
201    }
202    
203    if( tsin.sa.sa_family == AF_INET ) 
204       SC_SET_PORT( scp, ntohs( tsin.sa_in.sin_port ) ) ;
205    else if( tsin.sa.sa_family == AF_INET6 )
206       SC_SET_PORT( scp, ntohs( tsin.sa_in6.sin6_port ) ) ;
207
208    /*
209     * Try to register as many versions as possible
210     */
211    for ( vers = RD_MINVERS( rdp ) ; vers <= RD_MAXVERS( rdp ) ; vers++ ) {
212 /*      Is this right?  For instance, if we have both tcp and udp services,
213  *      this will unregister the previously registered protocol.
214  *      pmap_unset(RD_PROGNUM(rdp), vers);
215  */
216       if ( pmap_set( RD_PROGNUM( rdp ), vers, SC_PROTOVAL( scp ), 
217                               SC_PORT( scp ) ) )
218          registered_versions++ ;
219       else
220          msg( LOG_ERR, func,
221             "pmap_set failed. service=%s program=%ld version=%ld",
222                sid, RD_PROGNUM( rdp ), vers ) ;
223       sleep(1);
224    }
225
226    if ( debug.on )
227       msg( LOG_DEBUG, func,
228             "Registered %d versions of %s", registered_versions, sid ) ;
229
230    return( ( registered_versions == 0 ) ? FAILED : OK ) ;
231 }
232
233 #endif   /* ! NO_RPC */
234
235 static status_e activate_normal( struct service *sp )
236 {
237    union xsockaddr         tsin;
238    int                     sd             = SVC_FD( sp ) ;
239    struct service_config  *scp            = SVC_CONF( sp ) ;
240    uint16_t                service_port   = SC_PORT( scp ) ;
241    char                   *sid            = SC_ID( scp ) ;
242    const char             *func           = "activate_normal" ;
243    unsigned int            sin_len        = sizeof(tsin);
244    int                     on             = 1;
245 #ifdef IPV6_V6ONLY
246    int                     v6on           = 0;
247 #endif
248
249    if( SC_BIND_ADDR(scp) != NULL )
250       memcpy(&tsin, SC_BIND_ADDR(scp), sin_len);
251    else
252       memset(&tsin, 0, sin_len);
253    
254    if( SC_IPV4( scp ) ) {
255       tsin.sa_in.sin_family = AF_INET ;
256       tsin.sa_in.sin_port = htons( service_port ) ;
257       sin_len = sizeof(struct sockaddr_in);
258    } else if( SC_IPV6( scp ) ) {
259       tsin.sa_in6.sin6_family = AF_INET6;
260       tsin.sa_in6.sin6_port = htons( service_port );
261       sin_len = sizeof(struct sockaddr_in6);
262    }
263
264 #ifdef IPV6_V6ONLY
265    if( SC_IPV6(scp) ) {
266       if( SC_SPECIFIED(scp, A_V6ONLY) ) {
267          v6on = 1;
268       } else {
269          v6on = 0;
270       }
271       if( setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&v6on, sizeof(v6on)) < 0 ) {
272          msg( LOG_ERR, func, "Setting IPV6_V6ONLY option failed (%m)" );
273       }
274    }
275 #endif
276
277    if ( setsockopt( sd, SOL_SOCKET, SO_REUSEADDR, 
278                     (char *) &on, sizeof( on ) ) == -1 )
279       msg( LOG_WARNING, func, 
280            "setsockopt SO_REUSEADDR failed (%m). service = %s", sid ) ;
281
282    if( SC_NODELAY( scp ) && (SC_PROTOVAL(scp) == IPPROTO_TCP) )
283    {
284       if ( setsockopt( sd, IPPROTO_TCP, TCP_NODELAY, 
285                        (char *) &on, sizeof( on ) ) == -1 )
286          msg( LOG_WARNING, func, 
287               "setsockopt TCP_NODELAY failed (%m). service = %s", sid ) ;
288    }
289
290    if( SC_KEEPALIVE( scp ) && (SC_PROTOVAL(scp) == IPPROTO_TCP) ) 
291    {
292       if( setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, 
293                      (char *)&on, sizeof( on ) ) < 0 )
294          msg( LOG_WARNING, func, 
295               "setsockopt SO_KEEPALIVE failed (%m). service = %s", sid ) ;
296    }
297
298    if ( bind( sd, &tsin.sa, sin_len ) == -1 )
299    {
300       msg( LOG_ERR, func, "bind failed (%m). service = %s", sid ) ;
301       return( FAILED ) ;
302    }
303
304    return( OK ) ;
305 }
306
307
308 /*
309  * Activate a service. 
310  */
311 status_e svc_activate( struct service *sp )
312 {
313    struct service_config    *scp = SVC_CONF( sp ) ;
314    status_e                  status ;
315    const char                     *func = "svc_activate" ;
316
317    /*  No activation for MUXCLIENTS.
318     */
319
320    if (SC_IS_MUXCLIENT( scp ))
321    {
322       return( OK );
323    }
324
325    if( SC_IPV4( scp ) ) {
326       SVC_FD(sp) = socket( AF_INET, 
327                            SC_SOCKET_TYPE( scp ), SC_PROTOVAL( scp ) ) ;
328    } else if( SC_IPV6( scp ) ) {
329       SVC_FD(sp) = socket( AF_INET6, 
330                            SC_SOCKET_TYPE( scp ), SC_PROTOVAL( scp ) ) ;
331    }
332
333    if ( SVC_FD(sp) == -1 )
334    {
335       msg( LOG_ERR, func,
336                   "socket creation failed (%m). service = %s", SC_ID( scp ) ) ;
337       return( FAILED ) ;
338    }
339
340    if ( set_fd_modes( sp ) == FAILED )
341    {
342       (void) Sclose( SVC_FD(sp) ) ;
343       return( FAILED ) ;
344    }
345
346 #ifndef NO_RPC
347    if ( SC_IS_RPC( scp ) )
348       status = activate_rpc( sp ) ;
349    else
350 #endif   /* ! NO_RPC */
351       status = activate_normal( sp ) ;
352    
353    if ( status == FAILED )
354    {
355       (void) Sclose( SVC_FD(sp) ) ;
356       return( FAILED ) ;
357    }
358
359 #ifdef HAVE_MDNS
360    xinetd_mdns_register(scp);
361 #endif
362
363    if ( log_start( sp, &SVC_LOG(sp) ) == FAILED )
364    {
365       deactivate( sp ) ;
366       return( FAILED ) ;
367    }
368
369    /*
370     * Initialize the service data
371     */
372    SVC_RUNNING_SERVERS(sp)   = SVC_RETRIES(sp) = 0 ;
373
374    if ( SC_MUST_LISTEN( scp ) )
375       (void) listen( SVC_FD(sp), LISTEN_BACKLOG ) ;
376
377    ps.rws.descriptors_free-- ;
378
379    SVC_STATE(sp) = SVC_ACTIVE ;
380
381    FD_SET( SVC_FD(sp), &ps.rws.socket_mask ) ;
382    if ( SVC_FD(sp) > ps.rws.mask_max )
383       ps.rws.mask_max = SVC_FD(sp) ;
384
385    ps.rws.active_services++ ;
386    ps.rws.available_services++ ;
387
388    return( OK ) ;
389 }
390
391
392 static void deactivate( const struct service *sp )
393 {
394    (void) Sclose( SVC_FD( sp ) ) ;
395
396 #ifdef HAVE_MDNS
397    xinetd_mdns_deregister(SVC_CONF(sp));
398 #endif
399
400    if (debug.on)
401       msg(LOG_DEBUG, "deactivate", "%d Service %s deactivated", 
402           getpid(), SC_NAME( SVC_CONF(sp) ) );
403
404 #ifndef NO_RPC
405    if ( SC_IS_RPC( SVC_CONF( sp ) ) )
406    {
407       unsigned long vers ;
408       const struct rpc_data *rdp = SC_RPCDATA( SVC_CONF( sp ) ) ;
409
410       for ( vers = RD_MINVERS( rdp ) ; vers <= RD_MAXVERS( rdp ) ; vers++ ) {
411          (void) pmap_unset( RD_PROGNUM( rdp ), vers ) ;
412       }
413    }
414 #endif   /* ! NO_RPC */
415 }
416
417
418 /*
419  * Close the service descriptor.
420  * If this is an RPC service, deregister it.
421  * Close the log.
422  */
423 void svc_deactivate( struct service *sp )
424 {
425    if ( ! SVC_IS_AVAILABLE( sp ) )
426       return ;
427
428    deactivate( sp ) ;
429    ps.rws.descriptors_free++ ;
430
431    if ( SVC_IS_ACTIVE( sp ) )
432    {
433       FD_CLR( SVC_FD( sp ), &ps.rws.socket_mask ) ;
434       ps.rws.active_services-- ;
435    }
436
437    ps.rws.available_services-- ;
438
439    DISABLE( sp ) ;
440 }
441
442
443 /*
444  * Suspend a service
445  */
446 void svc_suspend( struct service *sp )
447 {
448    const char *func = "svc_suspend" ;
449
450    if ( ! SVC_IS_ACTIVE( sp ) )
451    {
452       msg( LOG_ERR, func, "service %s is not active", SVC_ID( sp ) ) ;
453       return ;
454    }
455
456    FD_CLR( SVC_FD( sp ), &ps.rws.socket_mask ) ;
457    ps.rws.active_services-- ;
458    if ( debug.on )
459       msg( LOG_DEBUG, func, "Suspended service %s", SVC_ID( sp ) ) ;
460    
461    SUSPEND( sp ) ;
462 }
463
464
465 /*
466  * Resume a suspended service.
467  */
468 void svc_resume( struct service *sp )
469 {
470    const char *func = "svc_resume" ;
471
472    FD_SET( SVC_FD( sp ), &ps.rws.socket_mask ) ;
473    ps.rws.active_services++ ;
474    if ( debug.on )
475       msg( LOG_DEBUG, func, "Resumed service %s", SVC_ID( sp ) ) ;
476    RESUME( sp ) ;
477 }
478
479
480 /*
481  * Steps:
482  *      1. Deactivate the service
483  *      2. Free all memory used by the service and free the service itself
484  *
485  * Since this function may free all memory associated with the service as
486  * well as the memory pointed by sp, only the value of sp should be used
487  * after this call if the return value is 0 (i.e. no dereferencing of sp).
488  *
489  * Special services are never deactivated.
490  */
491 int svc_release( struct service *sp )
492 {
493    char *sid = SVC_ID( sp ) ;
494    const char *func = "svc_release" ;
495
496    if ( SVC_REFCOUNT(sp) == 0 )
497    {
498       msg( LOG_ERR, func, "%s: svc_release with 0 count", sid ) ;
499       return( 0 ) ;
500    }
501    
502    SVC_REFCOUNT(sp)-- ;
503    if ( SVC_REFCOUNT(sp) == 0 )
504    {
505       if ( debug.on )
506          msg( LOG_DEBUG, func, "ref count of service %s dropped to 0", sid ) ;
507       if ( ! SC_IS_SPECIAL( SVC_CONF( sp ) ) )
508       {
509          if ( SVC_LOG(sp) )
510             log_end( SC_LOG( SVC_CONF( sp ) ), SVC_LOG(sp) ) ;
511          svc_deactivate( sp ) ;
512          svc_free( sp ) ;
513          sp = NULL;
514       }
515       else      /* this shouldn't happen */
516          msg( LOG_WARNING, func,
517             "ref count of special service %s dropped to 0", sid ) ;
518       return( 0 ) ;
519    }
520    else
521       return( SVC_REFCOUNT(sp) ) ;
522 }
523
524
525 void svc_dump( const struct service *sp, int fd )
526 {
527    tabprint( fd, 0, "Service = %s\n", SC_NAME( SVC_CONF( sp ) ) ) ;
528    tabprint( fd, 1, "State = %s\n",
529                         nv_get_name( service_states, (int) SVC_STATE(sp) ) ) ;
530
531    sc_dump( SVC_CONF( sp ), fd, 1, FALSE ) ;
532
533    if ( SVC_IS_ACTIVE(sp) )
534    {
535       tabprint( fd, 1, "running servers = %d\n", SVC_RUNNING_SERVERS(sp) ) ;
536       tabprint( fd, 1, "retry servers = %d\n", SVC_RETRIES(sp) ) ;
537       tabprint( fd, 1, "attempts = %d\n", SVC_ATTEMPTS(sp) ) ;
538       tabprint( fd, 1, "service fd = %d\n", SVC_FD(sp) ) ;
539    }
540    Sputchar( fd, '\n' ) ;
541 }
542
543
544 void svc_request( struct service *sp )
545 {
546    connection_s *cp ;
547    status_e ret_code;
548
549    cp = conn_new( sp ) ;
550    if ( cp == CONN_NULL )
551       return ;
552
553    /*
554     * Output the banner now that the connection is established. The
555     * other banners come later.
556     */
557    banner_always(sp, cp);
558
559    if (SVC_NOT_GENERIC(sp))
560       ret_code = spec_service_handler(sp, cp);
561    else 
562       ret_code = svc_generic_handler(sp, cp);
563
564    if( (SVC_SOCKET_TYPE( sp ) == SOCK_DGRAM) && (SVC_IS_ACTIVE( sp )) ) 
565       drain( cp->co_descriptor ) ; /* Prevents looping next time */
566    
567    if ( ret_code != OK ) 
568    {
569       if ( SVC_LOGS_USERID_ON_FAILURE( sp ) ) {
570          if( spec_service_handler( LOG_SERVICE( ps ), cp ) == FAILED ) 
571             conn_free( cp, 1 ) ;
572          else if (!SC_WAITS( SVC_CONF( sp ) ) ) {
573          /* The logging service will gen SIGCHLD thus freeing connection */
574             CONN_CLOSE(cp) ; 
575          }
576          return;
577       }
578       if (!SC_WAITS( SVC_CONF( sp ) )) 
579          conn_free( cp, 1 );
580       else { 
581          if( (SVC_SOCKET_TYPE( sp ) == SOCK_DGRAM) && (SVC_IS_ACTIVE( sp )) ) 
582             drain( cp->co_descriptor ) ; /* Prevents looping next time */
583          free( cp );
584       }
585    }
586    else if ((SVC_NOT_GENERIC(sp)) || (!SC_FORKS( SVC_CONF( sp ) ) ) )
587      free( cp );
588 }
589
590
591 status_e svc_generic_handler( struct service *sp, connection_s *cp )
592 {
593    if ( svc_parent_access_control( sp, cp ) == OK ) {
594       return( server_run( sp, cp ) ) ;
595    }
596
597    return( FAILED ) ;
598 }
599
600 #define TMPSIZE 1024
601 /* Print the banner that is supposed to always be printed */
602 static int banner_always( const struct service *sp, const connection_s *cp )
603 {
604    const char *func = "banner_always";
605    const struct service_config *scp = SVC_CONF( sp ) ;
606
607    /* print the banner regardless of access control */
608    if ( SC_BANNER(scp) != NULL ) {
609       char tmpbuf[TMPSIZE];
610       int retval;
611       int bannerfd = open(SC_BANNER(scp), O_RDONLY);
612
613       if( bannerfd < 0 ) {
614          msg( LOG_ERR, func, "service = %s, open of banner %s failed", 
615                          SVC_ID( sp ), SC_BANNER(scp));
616          return(-1);
617       }
618
619       while( (retval = read(bannerfd, tmpbuf, sizeof(tmpbuf))) ) {
620          if (retval == -1)
621          {
622             if (errno == EINTR)
623                continue;
624             else
625             {
626                msg(LOG_ERR, func, "service %s, Error %m reading banner %s", 
627                                SVC_ID( sp ), SC_BANNER(scp));
628                break;
629             }
630          }
631          Swrite(cp->co_descriptor, tmpbuf, retval);
632       }
633
634       Sclose(bannerfd);
635       Sflush ( cp->co_descriptor );
636    }
637
638    return(0);
639 }
640
641 static int banner_fail( const struct service *sp, const connection_s *cp )
642 {
643    const char *func = "banner_fail";
644    const struct service_config *scp = SVC_CONF( sp ) ;
645
646
647    if ( SC_BANNER_FAIL(scp) != NULL )
648    {
649       char tmpbuf[TMPSIZE];
650       int retval;
651       int bannerfd = open(SC_BANNER_FAIL(scp), O_RDONLY);
652
653       if( bannerfd < 0 )
654       {
655          msg( LOG_ERR, func, "service = %s, open of banner %s failed", 
656             SVC_ID( sp ), SC_BANNER_FAIL(scp));
657          return(-1);
658       }
659
660       while( (retval = read(bannerfd, tmpbuf, sizeof(tmpbuf))) ) {
661          if (retval == -1)
662          {
663             if (errno == EINTR)
664                continue;
665             else
666             {
667                msg(LOG_ERR, func, "service %s, Error %m reading banner %s", 
668                                SVC_ID( sp ), SC_BANNER(scp));
669                break;
670             }
671          }
672          Swrite(cp->co_descriptor, tmpbuf, retval);
673       }
674
675       Sclose(bannerfd);
676       Sflush ( cp->co_descriptor );
677    }
678
679    return(0);
680 }
681
682 static int banner_success( const struct service *sp, const connection_s *cp )
683 {
684    const char *func = "banner_success";
685    const struct service_config *scp = SVC_CONF( sp ) ;
686
687    /* print the access granted banner */
688    if ( SC_BANNER_SUCCESS(scp) != NULL ) {
689       char tmpbuf[TMPSIZE];
690       int retval;
691       int bannerfd = open(SC_BANNER_SUCCESS(scp), O_RDONLY);
692
693       if( bannerfd < 0 ) {
694          msg( LOG_ERR, func, "service = %s, open of banner %s failed", 
695                          SVC_ID( sp ), SC_BANNER_SUCCESS(scp));
696          return(-1);
697       }
698
699       while( (retval = read(bannerfd, tmpbuf, sizeof(tmpbuf))) ) {
700          if (retval == -1)
701          {
702             if (errno == EINTR)
703                continue;
704             else
705             {
706                msg(LOG_ERR, func, "service %s, Error %m reading banner %s", 
707                                SVC_ID( sp ), SC_BANNER(scp));
708                break;
709             }
710          }
711          Swrite(cp->co_descriptor, tmpbuf, retval);
712       }
713
714       Sclose(bannerfd);
715       Sflush ( cp->co_descriptor );
716    }
717    return(0);
718 }
719
720 static status_e failed_service(struct service *sp, 
721                                 connection_s *cp, 
722                                 access_e result)
723 {
724    struct service_config *scp = SVC_CONF( sp ) ;
725
726    if ( result != AC_OK )
727    {
728       bool_int report_failure = TRUE ;
729
730       /*
731        * Try to avoid reporting multiple times a failed attempt to access
732        * a datagram-based service from a bad address. We do this because
733        * the clients of such services usually send multiple datagrams 
734        * before reporting a timeout (we have no way of telling them that
735        * their request has been denied).
736        */
737       if ( result == AC_ADDRESS && SVC_SOCKET_TYPE( sp ) == SOCK_DGRAM )
738       {
739          if( SC_IPV4( scp ) ) {
740             struct sockaddr_in *sinp = SAIN(CONN_ADDRESS( cp )) ;
741             struct sockaddr_in *last = SAIN(SVC_LAST_DGRAM_ADDR(sp)) ;
742             time_t current_time ;
743
744             if (sinp == NULL )
745                return FAILED;
746
747             if ( last == NULL ) {
748                last = SAIN( calloc( 1, sizeof(union xsockaddr) ) );
749                SVC_LAST_DGRAM_ADDR(sp) = (union xsockaddr *)last;
750             }
751
752             (void) time( &current_time ) ;
753             if ( sinp->sin_addr.s_addr == last->sin_addr.s_addr &&
754                                           sinp->sin_port == last->sin_port )
755             {
756                if( current_time - SVC_LAST_DGRAM_TIME(sp) <= DGRAM_IGNORE_TIME )
757                   report_failure = FALSE ;
758                else
759                   SVC_LAST_DGRAM_TIME(sp) = current_time ;
760             }
761             else
762             {
763                memcpy(SVC_LAST_DGRAM_ADDR(sp), sinp,sizeof(struct sockaddr_in));
764                SVC_LAST_DGRAM_TIME(sp) = current_time ;
765             }
766          } else if( SC_IPV6( scp ) ) {
767             struct sockaddr_in6 *sinp = SAIN6(CONN_ADDRESS( cp )) ;
768             struct sockaddr_in6 *last = SAIN6(SVC_LAST_DGRAM_ADDR(sp)) ;
769             time_t current_time ;
770
771             if (sinp == NULL )
772                return FAILED;
773
774             if( last == NULL ) {
775                last = SAIN6(calloc( 1, sizeof(union xsockaddr) ) );
776                SVC_LAST_DGRAM_ADDR( sp ) = (union xsockaddr *)last;
777             }
778
779             (void) time( &current_time ) ;
780             if ( IN6_ARE_ADDR_EQUAL(&(sinp->sin6_addr), &(last->sin6_addr)) && 
781                  sinp->sin6_port == last->sin6_port )
782             {
783                if((current_time - SVC_LAST_DGRAM_TIME(sp)) <= DGRAM_IGNORE_TIME)
784                   report_failure = FALSE ;
785                else
786                   SVC_LAST_DGRAM_TIME(sp) = current_time ;
787             }
788             else
789             {
790                memcpy(SVC_LAST_DGRAM_ADDR(sp),sinp,sizeof(struct sockaddr_in6));
791                SVC_LAST_DGRAM_TIME(sp) = current_time ;
792             }
793          }
794       }
795
796       if ( report_failure )
797          svc_log_failure( sp, cp, result ) ;
798
799       banner_fail(sp, cp);
800
801       return( FAILED ) ;
802    }
803
804    return( OK );
805 }
806
807 /* Do the "light weight" access control here */
808 status_e svc_parent_access_control( struct service *sp, connection_s *cp )
809 {
810    access_e result;
811
812    result = parent_access_control( sp, cp );
813    if( failed_service(sp, cp, result) == FAILED )
814       return(FAILED);
815
816    return (OK);
817 }
818
819 status_e svc_child_access_control( struct service *sp, connection_s *cp )
820 {
821    access_e result ;
822
823    result = access_control( sp, cp, MASK_NULL ) ;
824    if( failed_service(sp, cp, result) == FAILED )
825       return(FAILED);
826
827    banner_success(sp, cp);
828
829    return( OK ) ;
830 }
831
832 /*
833  * Invoked when a server of the specified service dies
834  */
835 void svc_postmortem( struct service *sp, struct server *serp )
836 {
837    struct service  *co_sp   = SERVER_CONNSERVICE( serp ) ;
838    connection_s    *cp      = SERVER_CONNECTION( serp ) ;
839    const char      *func    = "svc_postmortem" ;
840
841    SVC_DEC_RUNNING_SERVERS( sp ) ;
842
843    /*
844     * Log information about the server that died
845     */
846    if ( SVC_IS_LOGGING( sp ) )
847    {
848       if ( SERVER_WRITES_TO_LOG(serp) )
849       {
850          if ( debug.on )
851             msg( LOG_DEBUG, func,
852                         "Checking log size of %s service", SVC_ID( sp ) ) ;
853          xlog_control( SVC_LOG( sp ), XLOG_SIZECHECK ) ;
854       }
855       svc_log_exit( sp, serp ) ;
856    }
857
858    /*
859     * Now check if we have to check the log size of the service that owns
860     * the connection
861     */
862    if ( co_sp != sp && SVC_IS_LOGGING( co_sp ) )
863       xlog_control( SVC_LOG( co_sp ), XLOG_SIZECHECK ) ;
864
865    if (!SVC_WAITS(sp)) {
866       conn_free( cp, 1 ) ;
867       cp = NULL;
868    } else {
869       if (cp) {
870          if ( SVC_SOCKET_TYPE( sp ) == SOCK_DGRAM )
871             drain( cp->co_descriptor ) ;
872          free(cp);
873          cp = NULL;
874          if( SVC_RELE( sp ) == 0 )
875             svc_release( sp ); /* shouldn't be 0, but should remove from
876                                 * pset if it is... */
877       }
878       svc_resume(sp);
879    }
880 }
881
882 /*
883  * This function closes all service descriptors. This should be called 
884  * for all child processes that fork, but do not exec. This includes
885  * redirect, builtins, and tcpmux. The close on exec flag takes care of
886  * child processes that call exec. Without calling this, the listening 
887  * fd's are not closed and reconfig will fail.
888  */
889 void close_all_svc_descriptors(void)
890 {
891    psi_h                     iter ;
892    struct service            *osp ;
893
894    /* Have to close all other descriptors here */
895    iter = psi_create( SERVICES( ps ) ) ;
896    if ( iter == NULL )
897         out_of_memory( "close_all_svc_descriptors" ) ;
898
899    for ( osp = SP( psi_start( iter ) ) ; osp ; osp = SP( psi_next( iter ) ) )
900         (void) Sclose( SVC_FD( osp ) ) ;
901   
902    psi_destroy( iter ) ;
903 }
904