home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / reconfig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  15.0 KB  |  617 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: reconfig.c,v 5.2 1992/11/10 08:18:25 panos Exp $" ;
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <syslog.h>
  12. #include <signal.h>
  13. #include <memory.h>
  14.  
  15. #include "access.h"
  16. #include "defs.h"
  17. #include "state.h"
  18. #include "service.h"
  19. #include "server.h"
  20. #include "conf.h"
  21.  
  22. void exit() ;
  23.  
  24. void msg() ;
  25. void out_of_memory() ;
  26.  
  27. #define RECONFIG_SOFT            1
  28. #define RECONFIG_HARD            2
  29.  
  30. #define SWAP( x, y, temp )            (temp) = (x), (x) = (y), (y) = (temp)
  31.  
  32.  
  33. PRIVATE void do_reconfig() ;
  34.  
  35.  
  36. void soft_reconfig()
  37. {
  38.     do_reconfig( RECONFIG_SOFT ) ;
  39. }
  40.  
  41.  
  42. void hard_reconfig()
  43. {
  44.     do_reconfig( RECONFIG_HARD ) ;
  45. }
  46.  
  47.  
  48. /*
  49.  * Reconfigure the server by rereading the configuration file.
  50.  * Services may be added, deleted or have their attributes changed.
  51.  * All syslog output uses the LOG_NOTICE priority level (except for
  52.  * errors).
  53.  */
  54. PRIVATE void do_reconfig( reconfig_type )
  55.     int reconfig_type ;
  56. {
  57.     struct service *osp, *nsp ;
  58.     struct configuration new_conf ;
  59.     psi_h iter ;
  60.     unsigned old_services = 0 ;
  61.     unsigned new_services ;
  62.     unsigned dropped_services = 0 ;
  63.     char *func = "do_reconfig" ;
  64.     status_e readjust() ;
  65.     void terminate_servers() ;
  66.     void cancel_service_retries() ;
  67.     void destroy_services() ;
  68.     void check_servers() ;
  69.     unsigned start_services() ;
  70.     void swap_defaults() ;
  71.     struct service *service_lookup() ;
  72.  
  73.     msg( LOG_NOTICE, func, "Starting %s reconfiguration",
  74.                 ( reconfig_type == RECONFIG_SOFT ) ? "soft" : "hard" ) ;
  75.  
  76.     if ( get_configuration( &new_conf ) == FAILED )
  77.     {
  78.         msg( LOG_WARNING, func, "reconfiguration failed" ) ;
  79.         return ;
  80.     }
  81.  
  82.     iter = psi_create( SERVICES( ps ) ) ;
  83.     if ( iter == NULL )
  84.     {
  85.         out_of_memory( func ) ;
  86.         free_configuration( &new_conf ) ;
  87.         return ;
  88.     }
  89.  
  90.     swap_defaults( &new_conf ) ;
  91.  
  92.     /*
  93.      * There are 3 possibilities for a service:
  94.      *
  95.      *        A) it is in the current service table but not in the new one
  96.      *        B) it is in the new service table but not in the current one
  97.      *        C) it is both in the current service table and the new one
  98.      *
  99.      * Services of type A are dropped from the active services. Their
  100.      * running servers are either left alone (soft reconfiguration) or
  101.      * they are terminated (hard reconfiguration).
  102.      *
  103.      * Services of type B are added to the active services.
  104.      *
  105.      * Services of type C have some of their attributes readjusted.
  106.      */
  107.  
  108.     /*
  109.      * Step 1: find the services NOT in the new service table
  110.      */
  111.     for ( osp = SP( psi_start( iter ) ) ; osp ; osp = SP( psi_next( iter ) ) )
  112.     {
  113.         char *sid = CONF( osp )->id ;
  114.         boolean_e drop_service ;
  115.  
  116.         if ( ! IS_AVAILABLE( osp ) )
  117.             continue ;
  118.  
  119.         if ( ( nsp = service_lookup( new_conf.services, osp ) ) == NULL ||
  120.                         fundamental_change( CONF( osp ), CONF( nsp ) ) )
  121.         {
  122.             /*
  123.              * Procedure for disabling a service:
  124.              *
  125.              *        a. Terminate running servers/cancel retry attempts, in case
  126.              *            of hard reconfiguration
  127.              *        b. Clear the fd of the service from the socket_mask
  128.              *        c. Delete the service from the service table, if possible
  129.              */
  130.  
  131.             if ( reconfig_type == RECONFIG_HARD )
  132.             {
  133.                 terminate_servers( osp ) ;
  134.                 cancel_service_retries( osp ) ;
  135.             }
  136.             drop_service = YES ;
  137.         }
  138.         else
  139.         {
  140.             /*
  141.              * Readjust service
  142.              */
  143.             if ( readjust( osp, CONF( nsp ), reconfig_type ) == OK )
  144.             {
  145.                 /*
  146.                  * Do the access control again
  147.                  */
  148.                 if ( reconfig_type == RECONFIG_HARD )
  149.                     check_servers( osp ) ;
  150.  
  151.                 /*
  152.                  * Change the state so we know which ones among the new services 
  153.                  * were readjusted and which ones are new
  154.                  */
  155.                 nsp->state = SVC_DISABLED ;
  156.                 old_services++ ;
  157.                 drop_service = NO ;
  158.             }
  159.             else
  160.             {
  161.                 /*
  162.                  * The readjustment failed. If this is a hard reconfiguration,
  163.                  * cancel retries and terminate servers.
  164.                  */
  165.                 if ( reconfig_type == RECONFIG_HARD )
  166.                 {
  167.                     cancel_service_retries( osp ) ;
  168.                     terminate_servers( osp ) ;
  169.                 }
  170.                 drop_service = YES ;
  171.             }
  172.         }
  173.  
  174.         if ( drop_service == YES )
  175.         {
  176.             /*
  177.              * The service will be deleted only if it is not being referenced 
  178.              * In either case, it is deactivated
  179.              */
  180.             svc_deactivate( osp ) ;
  181.             msg( LOG_NOTICE, func, "service %s deactivated", sid ) ;
  182.             if ( SVC_RELE( osp ) == 0 )
  183.                 psi_remove( iter ) ;
  184.             dropped_services++ ;
  185.         }
  186.     }
  187.  
  188.     /*
  189.      * Step 2: Add new services to the service table
  190.      *
  191.      * At this point, all services in the new_services table are split in
  192.      * two groups depending on their state: SVC_NOT_STARTED and SVC_DISABLED
  193.      * The start_services function will activate those whose state is
  194.      * SVC_NOT_STARTED
  195.      */
  196.     
  197.     new_services = start_services( new_conf.services ) ;
  198.  
  199.     msg( LOG_NOTICE, func,
  200.         "Reconfigured: new=%d old=%d dropped=%d (services)",
  201.             new_services, old_services, dropped_services ) ;
  202.  
  203.     if ( ps.rws.available_services == 0 )
  204.     {
  205.         msg( LOG_CRIT, func, "No available services. Exiting" ) ;
  206.         exit( 1 ) ;
  207.     }
  208.  
  209.     free_configuration( &new_conf ) ;
  210. }
  211.  
  212.  
  213. PRIVATE void swap_defaults( confp )
  214.     struct configuration *confp ;
  215. {
  216.     struct service *temp ;
  217.     void end_log() ;
  218.  
  219.     /*
  220.      * Close the previous common log file, if one was specified
  221.      */
  222.     if ( DEFAULT_LOG( ps ) != NULL )
  223.     {
  224.         end_log( LOG( DEFAULTS( ps ) )->log_type, DEFAULT_LOG( ps ) ) ;
  225.         DEFAULT_LOG( ps ) = NULL ;
  226.     }
  227.     DEFAULT_LOG_ERROR( ps ) = FALSE ;
  228.  
  229.     /*
  230.      * We set the reference count of the old defaults to 0, so that
  231.      * it will be free'd when the configuration is free'd
  232.      * Setting the reference count of the new defaults to 1 is done
  233.      * for aesthetic reasons.
  234.      */
  235.     confp->defaults->ref_count = 1 ;
  236.     ps.rws.cs.defaults->ref_count = 0 ;
  237.  
  238.     SWAP( ps.rws.cs.defaults, confp->defaults, temp ) ;
  239. }
  240.  
  241.  
  242. /*
  243.  * This functions returns TRUE if there has been a change in the values of
  244.  * the following attributes:
  245.  *        wait, type, socket_type, protocol.value
  246.  */
  247. PRIVATE bool_int fundamental_change( old_conf, new_conf )
  248.     struct service_config *old_conf ;
  249.     struct service_config *new_conf ;
  250. {
  251.     if ( IS_UNLISTED( old_conf ) != IS_UNLISTED( new_conf ) ||
  252.                         IS_INTERNAL( old_conf ) != IS_INTERNAL( new_conf ) )
  253.         return( TRUE ) ;
  254.  
  255.     if ( old_conf->wait != new_conf->wait )
  256.         return( TRUE ) ;
  257.     
  258.     if ( IS_RPC( old_conf ) )
  259.     {
  260.         if ( old_conf->protocol.value != new_conf->protocol.value )
  261.             return( TRUE ) ;
  262.     }
  263.     else
  264.     {
  265.         if ( old_conf->socket_type != new_conf->socket_type || 
  266.                 old_conf->protocol.value != new_conf->protocol.value )
  267.             return( TRUE ) ;
  268.  
  269.         if ( IS_UNLISTED( old_conf ) && old_conf->port != new_conf->port )
  270.             return( TRUE ) ;
  271.     }
  272.     return( FALSE ) ;
  273. }
  274.  
  275.  
  276.  
  277. /*
  278.  * Two RPC services are considered the same if they have the same program
  279.  * number.
  280.  * Two non-RPC services are considered the same if they use the same
  281.  * protocol,port pair
  282.  */
  283. #define SAME_RPC( s1, s2 )    \
  284.         ( RPCDATA( s1 )->program_number == RPCDATA( s2 )->program_number )
  285. #define SAME_NONRPC( s1, s2 ) \
  286.         ( (s1)->socket_type == (s2)->socket_type && (s1)->port == (s2)->port )
  287.  
  288. PRIVATE struct service *service_lookup( stab, sp )
  289.     pset_h stab ;
  290.     struct service *sp ;
  291. {
  292.     register struct service_config *scp = CONF( sp ) ;
  293.     register int is_rpc_service = IS_RPC( scp ) ;
  294.     register unsigned u ;
  295.  
  296.     for ( u = 0 ; u < pset_count( stab ) ; u++ )
  297.     {
  298.         struct service *isp = SP( pset_pointer( stab, u ) ) ;
  299.         register struct service_config *iscp = CONF( isp ) ;
  300.  
  301.         if ( ! EQ( scp->id, iscp->id ) )
  302.             continue ;
  303.  
  304.         /*
  305.          * Two services are the same if:
  306.          *
  307.          *        they are RPC services and they have the same program number
  308.          *        they are not RPC and they use the same protocol,port pair
  309.          */
  310.         if ( is_rpc_service && IS_RPC( iscp ) && SAME_RPC( scp, iscp ) ||
  311.                 ! is_rpc_service && ! IS_RPC( iscp ) && SAME_NONRPC( scp, iscp ) )
  312.             return( isp ) ;
  313.     }
  314.     return( NULL ) ;
  315. }
  316.  
  317.  
  318.  
  319. PRIVATE void sendsig( serp, sig )
  320.     register struct server *serp ;
  321.     int sig ;
  322. {
  323.     struct service *sp = SERVER_SERVICE( serp ) ;
  324.     char *func = "sendsig" ;
  325.  
  326.     /*
  327.      * Always use a positive pid, because of the semantics of kill(2)
  328.      */
  329.     if ( serp->pid > 0 )
  330.     {
  331.         msg( LOG_WARNING, func, "Sending signal %d to %s server %d",
  332.                                                 sig, CONF( sp )->id, serp->pid ) ;
  333.         (void) kill( serp->pid, sig ) ;
  334.     }
  335.     else
  336.         msg( LOG_ERR, func,
  337.             "Negative server pid = %d. Service %s", serp->pid, CONF( sp )->id ) ;
  338. }
  339.  
  340.  
  341. /*
  342.  * Send signal sig to all running servers of service sp
  343.  */
  344. PRIVATE void deliver_signal( sp, sig )
  345.     register struct service *sp ;
  346.     int sig ;
  347. {
  348.     register unsigned u ;
  349.  
  350.     for ( u = 0 ; u < pset_count( ps.rws.servers ) ; u++ )
  351.     {
  352.         register struct server *serp ;
  353.  
  354.         serp = SERP( pset_pointer( ps.rws.servers, u ) ) ;
  355.         if ( SERVER_SERVICE( serp ) == sp )
  356.             sendsig( serp, sig ) ;
  357.     }
  358. }
  359.  
  360.  
  361. /*
  362.  * Terminate all servers of the specified service
  363.  */
  364. void terminate_servers( sp )
  365.     register struct service *sp ;
  366. {
  367.     int sig = IS_INTERNAL( CONF( sp ) ) ? SIGTERM : SIGKILL ;
  368.  
  369.     deliver_signal( sp, sig ) ;
  370. }
  371.  
  372.  
  373. PRIVATE void stop_interception( sp )
  374.     struct service *sp ;
  375. {
  376.     deliver_signal( sp, INTERCEPT_SIG ) ;
  377. }
  378.  
  379.  
  380. PRIVATE char *termination_reason( res )
  381.     access_e res ;
  382. {
  383.     if ( res == AC_TIME )
  384.         return( "time" ) ;
  385.     else if ( res == AC_ADDRESS )
  386.         return( "address" ) ;
  387.     else if ( res == AC_SERVICE_LIMIT )
  388.         return( "service instances" ) ;
  389.     else
  390.         return( "UNKNOWN" ) ;
  391. }
  392.  
  393.  
  394.  
  395. /*
  396.  * Do access control on all servers of the specified service.
  397.  * If "limit" is 0, we don't mind if the service limit is exceeded.
  398.  * If "limit" is non-zero, we kill servers that exceed the service limit
  399.  * (but at most "limit" servers are killed because of exceeding the 
  400.  * instances limit)
  401.  *
  402.  * Return value: number of servers that failed the check (and were killed)
  403.  */
  404. PRIVATE int server_check( sp, limit )
  405.     register struct service *sp ;
  406.     int limit ;
  407. {
  408.     register unsigned u ;
  409.     int killed_servers = 0 ;
  410.     pset_h server_set = ps.rws.servers ;
  411.     char *func = "server_check" ;
  412.     access_e access_control() ;
  413.  
  414.     if ( SDATA( sp )->running_servers == 0 )
  415.         return( 0 ) ;
  416.  
  417.     for ( u = 0 ; u < pset_count( server_set ) ; u++ )
  418.     {
  419.         register struct server *serp = SERP( pset_pointer( server_set, u ) ) ;
  420.  
  421.         if ( SERVER_SERVICE( serp ) == sp )
  422.         {
  423.             access_e result ;
  424.             mask_t check_mask = MASK( CF_TIME ) ;
  425.  
  426.             if ( CONF( sp )->socket_type != SOCK_DGRAM )
  427.                 M_OR( check_mask, check_mask, MASK( CF_ADDRESS ) ) ;
  428.             if ( limit != 0 && killed_servers < limit )
  429.                 M_OR( check_mask, check_mask, MASK( CF_SERVICE_LIMIT ) ) ;
  430.             
  431.             result = access_control( sp, SERVER_CONNECTION( serp ), &check_mask );
  432.  
  433.             if ( result == AC_OK )
  434.                 continue ;
  435.         
  436.             msg( LOG_NOTICE, func, "%s server %d failed %s check",
  437.                 CONF( sp )->id, serp->pid, termination_reason( result ) ) ;
  438.             sendsig( serp, SIGKILL ) ;
  439.             killed_servers++ ;
  440.         }
  441.     }
  442.     return( killed_servers ) ;
  443. }
  444.  
  445.  
  446. /*
  447.  * Check if all the running servers of the specified service fit the 
  448.  * new access control specifications. The ones that don't fit are killed.
  449.  * 
  450.  * We go through the server table twice. During the first
  451.  * pass we ignore overruns of the server limit. During the second pass
  452.  * we don't ignore such overruns until a certain number of servers are
  453.  * terminated.
  454.  */
  455. PRIVATE void check_servers( sp )
  456.     register struct service *sp ;
  457. {
  458.     int existant_servers = SDATA( sp )->running_servers ;
  459.     int limit ;
  460.  
  461.     existant_servers -= server_check( sp, 0 ) ;
  462.  
  463.     limit = existant_servers - CONF( sp )->instances ;
  464.     if ( limit < 0 )
  465.         limit = 0 ;
  466.     (void) server_check( sp, limit ) ;
  467. }
  468.  
  469.  
  470.  
  471. /*
  472.  * Readjust service attributes. 
  473.  *
  474.  * We assume that the following attributes are the same:
  475.  *            wait
  476.  *            socket_type
  477.  *            type
  478.  *            protocol
  479.  *
  480.  * Readjustment happens in 3 steps:
  481.  *        1) We swap the service.conf fields
  482.  *                This has the side-effect of free'ing the memory associated
  483.  *                with the old service configuration when the new configuration
  484.  *                is destroyed.
  485.  *        2) We readjust the fields that require some action to be taken:
  486.  *                RPC mapping
  487.  *                log file open
  488.  *        3) We update the service_data
  489.  */
  490. PRIVATE status_e readjust( sp, new_conf, type )
  491.     register struct service *sp ;
  492.     register struct service_config *new_conf ;
  493.     int type ;
  494. {
  495.     struct service_config *old_conf ;
  496.     struct service_config temp_conf ;
  497.     char *sid = CONF( sp )->id ;
  498.     char *func = "readjust" ;
  499.     status_e readjust_rpc_service() ;
  500.     status_e restart_log() ;
  501.  
  502.     msg( LOG_NOTICE, func, "readjusting service %s", sid ) ;
  503.  
  504.     SWAP( *CONF( sp ), *new_conf, temp_conf ) ;
  505.     old_conf = new_conf ;
  506.     new_conf = CONF( sp ) ;
  507.  
  508.     if ( IS_RPC( old_conf ) &&
  509.                         readjust_rpc_service( old_conf, new_conf ) == FAILED )
  510.         return( FAILED ) ;
  511.     
  512.     /*
  513.      * This is what happens if the INTERCEPT flag is toggled and an
  514.      * interceptor is running:
  515.      *
  516.      * Case 1: clear->set
  517.      *        Wait until the server dies (soft reconfig) or
  518.      *        terminate the server (hard reconfig)
  519.      *
  520.      * Case 2: set->clear
  521.     *    Send a signal to the interceptor to tell it to stop intercepting
  522.      */
  523.     if ( IS_INTERCEPTED( old_conf ) != IS_INTERCEPTED( new_conf ) )
  524.     {
  525.         if ( IS_INTERCEPTED( new_conf ) )            /* case 1 */
  526.         {
  527.             if ( type == RECONFIG_HARD )
  528.                 terminate_servers( sp ) ;
  529.         }
  530.         else                                                    /* case 2 */
  531.         {
  532.             stop_interception( sp ) ;
  533.             msg( LOG_NOTICE, func, "Stopping interception for %s", sid ) ;
  534.         }
  535.     }
  536.     svc_setup_address_control( sp ) ;
  537.     return( restart_log( sp, old_conf ) ) ;
  538. }
  539.  
  540.  
  541.  
  542. /*
  543.  * Stop any logging and restart if necessary.
  544.  * Note that this has the side-effect of using the new common_log
  545.  * handle (in ps.rws.conf) as it should.
  546.  */
  547. PRIVATE status_e restart_log( sp, old_conf )
  548.     struct service *sp ;
  549.     struct service_config *old_conf ;
  550. {
  551.     logtype_e old_logtype = LOG( old_conf )->log_type ;
  552.     xlog_h old_log_handle = SDATA( sp )->log_handle ;
  553.     void end_log() ;
  554.     status_e start_log() ;
  555.  
  556.     if ( old_logtype != L_NONE && old_log_handle != NULL )
  557.         end_log( old_logtype, old_log_handle ) ;
  558.     
  559.     return( start_log( sp ) ) ;
  560. }
  561.  
  562.  
  563. /*
  564.  * Unregister past versions, register new ones
  565.  * We do it the dumb way: first unregister; then register
  566.  * We try to be a little smart by checking if there has
  567.  * been any change in version numbers (if not, we do nothing).
  568.  * Also, we save the port number
  569.  */
  570. PRIVATE status_e readjust_rpc_service( old_scp, new_scp )
  571.     register struct service_config *old_scp ;
  572.     register struct service_config *new_scp ;
  573. {
  574.     unsigned long vers ;
  575.     u_short port = old_scp->port ;
  576.     struct rpc_data *new_rdp = &new_scp->rd ;
  577.     struct rpc_data *old_rdp = &old_scp->rd ;
  578.     unsigned registered_versions = 0 ;
  579.     char *func = "readjust_rpc_service" ;
  580.  
  581. #ifndef NO_RPC
  582.     new_scp->port = old_scp->port ;
  583.  
  584.     if ( old_rdp->min_version == new_rdp->min_version &&
  585.                     old_rdp->max_version == new_rdp->max_version )
  586.         return( OK ) ;
  587.  
  588.     for ( vers = old_rdp->min_version ; vers <= old_rdp->max_version ; vers++ )
  589.          (void) pmap_unset( old_rdp->program_number, vers ) ;
  590.  
  591.     for ( vers = new_rdp->min_version ; vers <= new_rdp->max_version ; vers++ )
  592.         if ( pmap_set( new_rdp->program_number,
  593.                                         vers, new_scp->protocol.value, port ) )
  594.             registered_versions++ ;
  595.         else
  596.             msg( LOG_ERR, func, 
  597.                 "pmap_set failed. service=%s, program=%ld, version = %ld",
  598.                     new_scp->id, new_rdp->program_number, vers ) ;
  599.     /*
  600.      * If we didn't manage to register any versions, we will
  601.      * have to deactivate the service
  602.      */
  603.     if ( registered_versions == 0 )
  604.     {
  605.         msg( LOG_ERR, func,
  606.                 "No versions registered for RPC service %s", new_scp->id ) ;
  607.         /*
  608.          * Avoid the pmap_unset
  609.          */
  610.         new_rdp->min_version = new_rdp->max_version + 1 ;
  611.         return( FAILED ) ;
  612.     }
  613. #endif    /* ! NO_RPC */
  614.     return( OK ) ;
  615. }
  616.  
  617.