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.
22 #define NEW_SCONF() NEW( struct service_config )
23 #define FREE_SCONF( scp ) FREE( scp )
26 * Conditional free; checks if the pointer is NULL
28 #define COND_FREE( x ) if ( x ) \
31 free( (char *) x ) ; \
36 * Allocate a new service_config and initialize the service name field
37 * with 'name'; the rest of the fields are set to 0 which gives them
38 * their default values.
40 struct service_config *sc_alloc( const char *name )
42 struct service_config *scp ;
43 const char *func = "sc_alloc" ;
48 out_of_memory( func ) ;
52 SC_NAME(scp) = new_string( name ) ;
54 xinetd_mdns_svc_init(scp);
60 static void release_string_pset( pset_h pset )
62 pset_apply( pset, free, NULL ) ;
63 pset_destroy( pset ) ;
68 * Free all malloc'ed memory for the specified service
70 void sc_free( struct service_config *scp )
73 COND_FREE( SC_MDNS_NAME(scp) );
74 xinetd_mdns_svc_free(scp);
77 COND_FREE( SC_LIBWRAP(scp) );
79 COND_FREE( SC_NAME(scp) ) ;
80 COND_FREE( SC_ID(scp) ) ;
81 COND_FREE( SC_PROTONAME(scp) ) ;
82 COND_FREE( SC_SERVER(scp) ) ;
83 COND_FREE( (char *)SC_REDIR_ADDR(scp) ) ;
84 COND_FREE( (char *)SC_BIND_ADDR(scp) ) ;
85 COND_FREE( (char *)SC_ORIG_BIND_ADDR(scp) ) ;
86 COND_FREE( (char *)SC_BANNER(scp) ) ;
87 COND_FREE( (char *)SC_BANNER_SUCCESS(scp) ) ;
88 COND_FREE( (char *)SC_BANNER_FAIL(scp) ) ;
89 if ( SC_SERVER_ARGV(scp) )
94 * argv[ 0 ] is a special case because it may not have been allocated yet
96 if ( SC_SERVER_ARGV(scp)[ 0 ] != NULL)
97 free( SC_SERVER_ARGV(scp)[ 0 ] ) ;
98 for ( pp = &SC_SERVER_ARGV(scp)[ 1 ] ; *pp != NULL ; pp++ )
100 free( (char *) SC_SERVER_ARGV(scp) ) ;
102 COND_FREE( LOG_GET_FILELOG( SC_LOG( scp ) )->fl_filename ) ;
104 if ( SC_ACCESS_TIMES(scp) != NULL )
106 ti_free( SC_ACCESS_TIMES(scp) ) ;
107 pset_destroy( SC_ACCESS_TIMES(scp) ) ;
110 if ( SC_ONLY_FROM(scp) != NULL )
112 addrlist_free( SC_ONLY_FROM(scp) ) ;
113 pset_destroy( SC_ONLY_FROM(scp) ) ;
116 if ( SC_NO_ACCESS(scp) != NULL )
118 addrlist_free( SC_NO_ACCESS(scp) ) ;
119 pset_destroy( SC_NO_ACCESS(scp) ) ;
122 if ( SC_ENV_VAR_DEFS(scp) != NULL )
123 release_string_pset( SC_ENV_VAR_DEFS(scp) ) ;
124 if ( SC_PASS_ENV_VARS(scp) != NULL )
125 release_string_pset( SC_PASS_ENV_VARS(scp) ) ;
126 if ( SC_ENV( scp )->env_type == CUSTOM_ENV &&
127 SC_ENV( scp )->env_handle != ENV_NULL )
128 env_destroy( SC_ENV( scp )->env_handle ) ;
129 if (SC_DISABLED(scp) )
130 release_string_pset( SC_DISABLED(scp) ) ;
131 if (SC_ENABLED(scp) )
132 release_string_pset( SC_ENABLED(scp) ) ;
140 * Create a configuration for one of the special services
142 struct service_config *sc_make_special( const char *service_name,
146 struct service_config *scp ;
147 const char *func = "sc_make" ;
149 if ( ( scp = sc_alloc( service_name ) ) == NULL )
152 SC_ID(scp) = new_string( SC_NAME(scp) ) ;
153 if ( SC_ID(scp) == NULL )
155 out_of_memory( func ) ;
158 SC_SPECIFY( scp, A_ID ) ;
161 * All special services are internal
163 M_SET( SC_TYPE(scp), ST_SPECIAL ) ;
164 M_SET( SC_TYPE(scp), ST_INTERNAL ) ;
165 SC_BUILTIN(scp) = bp ;
166 SC_SPECIFY( scp, A_TYPE ) ;
168 M_SET( SC_XFLAGS(scp), SF_NORETRY ) ;
169 SC_SPECIFY( scp, A_FLAGS ) ;
171 SC_INSTANCES(scp) = instances ;
172 SC_SPECIFY( scp, A_INSTANCES ) ;
175 SC_SPECIFY( scp, A_WAIT ) ;
181 static void dump_log_data( int fd, struct service_config *scp, int tab_level )
183 struct log *lp = SC_LOG( scp ) ;
184 struct filelog *flp ;
187 switch ( LOG_GET_TYPE( lp ) )
190 tabprint( fd, tab_level, "No logging\n" ) ;
194 tabprint( fd, tab_level, "Logging to common log file\n" ) ;
198 flp = LOG_GET_FILELOG( lp ) ;
199 tabprint( fd, tab_level, "Logging to file: %s", flp->fl_filename ) ;
201 if ( FILELOG_SIZE_CONTROL( flp ) )
202 Sprint( fd, " (soft=%d hard=%d)\n",
203 flp->fl_soft_limit, flp->fl_hard_limit ) ;
205 Sprint( fd, " (no limits)\n" ) ;
209 tabprint( fd, tab_level,
210 "Logging to syslog. Facility = %s, level = %s\n",
211 nv_get_name( syslog_facilities, lp->l_sl.sl_facility ),
212 nv_get_name( syslog_levels, lp->l_sl.sl_level ) ) ;
216 tabprint( fd, tab_level, "Log_on_success flags =" ) ;
217 for ( i = 0 ; success_log_options[ i ].name != NULL ; i++ )
218 if ( M_IS_SET( SC_LOG_ON_SUCCESS(scp), success_log_options[ i ].value ) )
219 Sprint( fd, " %s", success_log_options[ i ].name ) ;
220 Sputchar( fd, '\n' ) ;
222 tabprint( fd, tab_level, "Log_on_failure flags =" ) ;
223 for ( i = 0 ; failure_log_options[ i ].name != NULL ; i++ )
224 if ( M_IS_SET( SC_LOG_ON_FAILURE(scp), failure_log_options[ i ].value ) )
225 Sprint( fd, " %s", failure_log_options[ i ].name ) ;
226 Sputchar( fd, '\n' ) ;
231 * Print info about service scp to file descriptor fd
233 void sc_dump( struct service_config *scp,
236 bool_int is_defaults )
238 const struct name_value *nvp ;
243 tabprint( fd, tab_level, "Service defaults\n" ) ;
245 tabprint( fd, tab_level, "Service configuration: %s\n", SC_NAME(scp) ) ;
249 tabprint( fd, tab_level+1, "id = %s\n", SC_ID(scp) ) ;
251 if ( ! M_ARE_ALL_CLEAR( SC_XFLAGS(scp) ) )
253 tabprint( fd, tab_level+1, "flags =" ) ;
254 for ( nvp = &service_flags[ 0 ] ; nvp->name != NULL ; nvp++ )
255 if ( M_IS_SET( SC_XFLAGS(scp), nvp->value ) )
256 Sprint( fd, " %s", nvp->name ) ;
257 Sputchar( fd, '\n' ) ;
260 if ( ! M_ARE_ALL_CLEAR( SC_TYPE(scp) ) )
262 tabprint( fd, tab_level+1, "type =" ) ;
263 for ( nvp = &service_types[ 0 ] ; nvp->name != NULL ; nvp++ )
264 if ( M_IS_SET( SC_TYPE(scp), nvp->value ) )
265 Sprint( fd, " %s", nvp->name ) ;
266 Sputchar( fd, '\n' ) ;
269 tabprint( fd, tab_level+1, "socket_type = %s\n",
270 nv_get_name( socket_types, SC_SOCKET_TYPE(scp) ) ) ;
272 tabprint( fd, tab_level+1, "Protocol (name,number) = (%s,%d)\n",
273 SC_PROTONAME(scp), SC_PROTOVAL(scp) ) ;
275 if ( SC_SPECIFIED( scp, A_PORT ) )
276 tabprint( fd, tab_level+1, "port = %d\n", SC_PORT(scp) ) ;
279 if ( SC_SPECIFIED( scp, A_INSTANCES ) ) {
280 if ( SC_INSTANCES(scp) == UNLIMITED )
281 tabprint( fd, tab_level+1, "Instances = UNLIMITED\n" ) ;
283 tabprint( fd, tab_level+1, "Instances = %d\n", SC_INSTANCES(scp) ) ;
286 if ( SC_SPECIFIED( scp, A_WAIT ) ) {
288 tabprint( fd, tab_level+1, "wait = yes\n" ) ;
290 tabprint( fd, tab_level+1, "wait = no\n" ) ;
293 if ( SC_SPECIFIED( scp, A_USER ) )
294 tabprint( fd, tab_level+1, "user = %d\n", SC_UID(scp) ) ;
296 if ( SC_SPECIFIED( scp, A_GROUP ) )
297 tabprint( fd, tab_level+1, "group = %d\n", SC_GID(scp) ) ;
299 if ( SC_SPECIFIED( scp, A_GROUPS ) )
301 if (SC_GROUPS(scp) == 1)
302 tabprint( fd, tab_level+1, "Groups = yes\n" );
304 tabprint( fd, tab_level+1, "Groups = no\n" );
307 if ( SC_SPECIFIED( scp, A_UMASK ) )
308 tabprint( fd, tab_level+1, "umask = %o\n", SC_UMASK(scp) ) ;
310 if ( SC_SPECIFIED( scp, A_NICE ) )
311 tabprint( fd, tab_level+1, "Nice = %d\n", SC_NICE(scp) ) ;
313 if ( SC_SPECIFIED( scp, A_CPS ) )
314 tabprint( fd, tab_level+1, "CPS = max conn:%lu wait:%lu\n",
315 SC_TIME_CONN_MAX(scp), SC_TIME_WAIT(scp) );
317 if ( SC_SPECIFIED( scp, A_PER_SOURCE ) )
318 tabprint( fd, tab_level+1, "PER_SOURCE = %d\n",
319 SC_PER_SOURCE(scp) );
321 if ( SC_SPECIFIED( scp, A_BIND ) ) {
322 if ( SC_BIND_ADDR(scp) ) {
323 char bindname[NI_MAXHOST];
324 unsigned int len = 0;
325 if( SC_BIND_ADDR(scp)->sa.sa_family == AF_INET )
326 len = sizeof(struct sockaddr_in);
328 len = sizeof(struct sockaddr_in6);
329 memset(bindname, 0, sizeof(bindname));
330 if( getnameinfo(&SC_BIND_ADDR(scp)->sa, len, bindname,
331 NI_MAXHOST, NULL, 0, 0) != 0 )
332 strcpy(bindname, "unknown");
333 tabprint( fd, tab_level+1, "Bind = %s\n", bindname );
335 else if ( SC_ORIG_BIND_ADDR(scp) ) {
336 tabprint( fd, tab_level+1, "Bind = %s\n",
337 SC_ORIG_BIND_ADDR(scp) );
339 else { /* This should NEVER happen */
340 msg(LOG_ERR, "sc_dump", "bad configuration for %s:",
345 tabprint( fd, tab_level+1, "Bind = All addresses.\n" );
349 if ( (! SC_IS_INTERNAL( scp )) && (SC_REDIR_ADDR(scp) == NULL) )
351 tabprint( fd, tab_level+1, "Server = %s\n", SC_SERVER(scp) ) ;
352 tabprint( fd, tab_level+1, "Server argv =" ) ;
353 if ( SC_SERVER_ARGV(scp) )
355 for ( pp = SC_SERVER_ARGV(scp) ; *pp ; pp++ )
356 Sprint( fd, " %s", *pp ) ;
359 Sprint( fd, " (NULL)");
360 Sputchar( fd, '\n' ) ;
364 if ( SC_LIBWRAP(scp) != NULL )
366 tabprint( fd, tab_level + 1, "Libwrap = %s\n", SC_LIBWRAP(scp) );
370 if ( SC_REDIR_ADDR(scp) != NULL )
372 char redirname[NI_MAXHOST];
373 unsigned int len = 0;
374 if( SC_REDIR_ADDR(scp)->sa.sa_family == AF_INET )
375 len = sizeof(struct sockaddr_in);
376 if( SC_REDIR_ADDR(scp)->sa.sa_family == AF_INET6 )
377 len = sizeof(struct sockaddr_in6);
378 memset(redirname, 0, sizeof(redirname));
379 if( getnameinfo(&SC_REDIR_ADDR(scp)->sa, len, redirname, NI_MAXHOST,
381 strcpy(redirname, "unknown");
382 tabprint( fd, tab_level+1, "Redirect = %s:%d\n", redirname,
383 SC_REDIR_ADDR(scp)->sa_in.sin_port );
386 if ( SC_IS_RPC( scp ) )
388 struct rpc_data *rdp = SC_RPCDATA( scp ) ;
390 tabprint( fd, tab_level+1, "RPC data\n" ) ;
391 tabprint( fd, tab_level+2,
392 "program number = %ld\n", rdp->rd_program_number ) ;
393 tabprint( fd, tab_level+2, "rpc_version = " ) ;
394 if ( rdp->rd_min_version == rdp->rd_max_version )
395 Sprint( fd, "%ld\n", rdp->rd_min_version ) ;
397 Sprint( fd, "%ld-%ld\n",
398 rdp->rd_min_version, rdp->rd_max_version ) ;
401 if ( SC_SPECIFIED( scp, A_ACCESS_TIMES ) )
403 tabprint( fd, tab_level+1, "Access times =" ) ;
404 ti_dump( SC_ACCESS_TIMES(scp), fd ) ;
405 Sputchar ( fd, '\n' ) ;
409 /* This is important enough that each service should list it. */
410 tabprint( fd, tab_level+1, "Only from: " ) ;
411 if ( SC_ONLY_FROM(scp) )
412 { /* Next check is done since -= doesn't zero out lists. */
413 if ( pset_count(SC_ONLY_FROM(scp)) == 0)
414 Sprint( fd, "All sites" );
416 addrlist_dump( SC_ONLY_FROM(scp), fd ) ;
419 Sprint( fd, "All sites" );
420 Sputchar( fd, '\n' ) ;
422 /* This is important enough that each service should list it. */
423 tabprint( fd, tab_level+1, "No access: " ) ;
424 if ( SC_NO_ACCESS(scp) )
425 { /* Next check is done since -= doesn't zero out lists. */
426 if ( pset_count(SC_NO_ACCESS(scp)) == 0)
427 Sprint( fd, "No blocked sites" );
429 addrlist_dump( SC_NO_ACCESS(scp), fd ) ;
432 Sprint( fd, "No blocked sites" );
433 Sputchar( fd, '\n' ) ;
435 if ( SC_SENSOR(scp) )
437 tabprint( fd, tab_level+1, "Deny Time: " ) ;
438 Sprint( fd, "%d\n", SC_DENY_TIME(scp));
441 dump_log_data( fd, scp, tab_level+1 ) ;
443 if ( SC_IS_PRESENT( scp, A_PASSENV ) )
445 tabprint( fd, tab_level+1, "Passenv =" ) ;
446 for ( u = 0 ; u < pset_count( SC_PASS_ENV_VARS(scp) ) ; u++ )
448 (char *) pset_pointer( SC_PASS_ENV_VARS(scp), u ) ) ;
449 Sputchar ( fd, '\n' ) ;
453 if ( SC_SPECIFIED( scp, A_ENV ) )
455 tabprint( fd, tab_level+1, "Environment additions:\n" ) ;
456 for ( u = 0 ; u < pset_count( SC_ENV_VAR_DEFS(scp) ) ; u++ )
457 tabprint( fd, tab_level+2,
458 "%s\n", (char *) pset_pointer( SC_ENV_VAR_DEFS(scp), u ) ) ;
461 if ( SC_ENV( scp )->env_type == CUSTOM_ENV )
463 tabprint( fd, tab_level+1, "Environment strings:\n" ) ;
464 for ( pp = env_getvars( SC_ENV( scp )->env_handle ) ; *pp ; pp++ )
465 tabprint( fd, tab_level+2, "%s\n", *pp ) ;
471 #define SC_RPCPROGNUM( s ) RD_PROGNUM( SC_RPCDATA( s ) )
472 #define SAME_RPC( s1, s2 ) ( SC_RPCPROGNUM( s1 ) == SC_RPCPROGNUM( s2 ) )
473 #define SAME_NONRPC( s1, s2 ) ( SC_SOCKET_TYPE((s1)) == SC_SOCKET_TYPE((s2)) \
474 && SC_PORT((s1)) == SC_PORT((s2)) )
477 * Two service configurations are considered different if any of the
479 * 1) only one is unlisted
480 * 2) only one is internal
482 * 4) they have different values for the 'wait' attribute
483 * 5) they use different protocols
484 * 6) they are both RPC services but have different program numbers
485 * 7) neither is an RPC service and they have different socket_types or
488 * This function returns TRUE if the specified configurations are different.
490 * Note that this function is closely related to the 'readjust' function
491 * that is invoked on reconfiguration; that function will not change
492 * attributes that this function checks to determine if two configurations
495 bool_int sc_different_confs( struct service_config *scp1,
496 struct service_config *scp2 )
498 if ( SC_IS_UNLISTED( scp1 ) != SC_IS_UNLISTED( scp2 ) ||
499 SC_IS_INTERNAL( scp1 ) != SC_IS_INTERNAL( scp2 ) ||
500 SC_IS_RPC( scp1 ) != SC_IS_RPC( scp2 ) )
503 if ( SC_WAIT(scp1) != SC_WAIT(scp2) )
506 if ( SC_PROTOVAL(scp1) != SC_PROTOVAL(scp2) )
509 if ( SC_IS_RPC( scp1 ) )
511 if ( ! SAME_RPC( scp1, scp2 ) )
516 if ( ! SAME_NONRPC( scp1, scp2 ) )