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 #ifdef HAVE_NETDB_H
10 #include <netdb.h>
11 #endif
12
13 #include "str.h"
14 #include "sio.h"
15 #include "sconf.h"
16 #include "timex.h"
17 #include "addr.h"
18 #include "nvlists.h"
19 #include "xmdns.h"
20
21
22 #define NEW_SCONF() NEW( struct service_config )
23 #define FREE_SCONF( scp ) FREE( scp )
24
25 /*
26 * Conditional free; checks if the pointer is NULL
27 */
28 #define COND_FREE( x ) if ( x ) \
29 { \
30 *x = NUL ; \
31 free( (char *) x ) ; \
32 }
33
34
35 /*
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.
39 */
sc_alloc(const char * name)40 struct service_config *sc_alloc( const char *name )
41 {
42 struct service_config *scp ;
43 const char *func = "sc_alloc" ;
44
45 scp = NEW_SCONF() ;
46 if ( scp == NULL )
47 {
48 out_of_memory( func ) ;
49 return( NULL ) ;
50 }
51 CLEAR( *scp ) ;
52 SC_NAME(scp) = new_string( name ) ;
53 #ifdef HAVE_MDNS
54 xinetd_mdns_svc_init(scp);
55 #endif
56 return( scp ) ;
57 }
58
59
release_string_pset(pset_h pset)60 static void release_string_pset( pset_h pset )
61 {
62 pset_apply( pset, free, NULL ) ;
63 pset_destroy( pset ) ;
64 }
65
66
67 /*
68 * Free all malloc'ed memory for the specified service
69 */
sc_free(struct service_config * scp)70 void sc_free( struct service_config *scp )
71 {
72 #ifdef HAVE_MDNS
73 COND_FREE( SC_MDNS_NAME(scp) );
74 xinetd_mdns_svc_free(scp);
75 #endif
76 #ifdef LIBWRAP
77 COND_FREE( SC_LIBWRAP(scp) );
78 #endif
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) )
90 {
91 char **pp ;
92
93 /*
94 * argv[ 0 ] is a special case because it may not have been allocated yet
95 */
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++ )
99 free( *pp ) ;
100 free( (char *) SC_SERVER_ARGV(scp) ) ;
101 }
102 COND_FREE( LOG_GET_FILELOG( SC_LOG( scp ) )->fl_filename ) ;
103
104 if ( SC_ACCESS_TIMES(scp) != NULL )
105 {
106 ti_free( SC_ACCESS_TIMES(scp) ) ;
107 pset_destroy( SC_ACCESS_TIMES(scp) ) ;
108 }
109
110 if ( SC_ONLY_FROM(scp) != NULL )
111 {
112 addrlist_free( SC_ONLY_FROM(scp) ) ;
113 pset_destroy( SC_ONLY_FROM(scp) ) ;
114 }
115
116 if ( SC_NO_ACCESS(scp) != NULL )
117 {
118 addrlist_free( SC_NO_ACCESS(scp) ) ;
119 pset_destroy( SC_NO_ACCESS(scp) ) ;
120 }
121
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) ) ;
133
134 CLEAR( *scp ) ;
135 FREE_SCONF( scp ) ;
136 }
137
138
139 /*
140 * Create a configuration for one of the special services
141 */
sc_make_special(const char * service_name,const builtin_s * bp,int instances)142 struct service_config *sc_make_special( const char *service_name,
143 const builtin_s *bp,
144 int instances )
145 {
146 struct service_config *scp ;
147 const char *func = "sc_make" ;
148
149 if ( ( scp = sc_alloc( service_name ) ) == NULL )
150 return( NULL ) ;
151
152 SC_ID(scp) = new_string( SC_NAME(scp) ) ;
153 if ( SC_ID(scp) == NULL )
154 {
155 out_of_memory( func ) ;
156 return( NULL ) ;
157 }
158 SC_SPECIFY( scp, A_ID ) ;
159
160 /*
161 * All special services are internal
162 */
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 ) ;
167
168 M_SET( SC_XFLAGS(scp), SF_NORETRY ) ;
169 SC_SPECIFY( scp, A_FLAGS ) ;
170
171 SC_INSTANCES(scp) = instances ;
172 SC_SPECIFY( scp, A_INSTANCES ) ;
173
174 SC_WAIT(scp) = NO ;
175 SC_SPECIFY( scp, A_WAIT ) ;
176
177 return( scp ) ;
178 }
179
180
dump_log_data(int fd,struct service_config * scp,int tab_level)181 static void dump_log_data( int fd, struct service_config *scp, int tab_level )
182 {
183 struct log *lp = SC_LOG( scp ) ;
184 struct filelog *flp ;
185 int i ;
186
187 switch ( LOG_GET_TYPE( lp ) )
188 {
189 case L_NONE:
190 tabprint( fd, tab_level, "No logging\n" ) ;
191 return ;
192
193 case L_COMMON_FILE:
194 tabprint( fd, tab_level, "Logging to common log file\n" ) ;
195 break ;
196
197 case L_FILE:
198 flp = LOG_GET_FILELOG( lp ) ;
199 tabprint( fd, tab_level, "Logging to file: %s", flp->fl_filename ) ;
200
201 if ( FILELOG_SIZE_CONTROL( flp ) )
202 Sprint( fd, " (soft=%d hard=%d)\n",
203 flp->fl_soft_limit, flp->fl_hard_limit ) ;
204 else
205 Sprint( fd, " (no limits)\n" ) ;
206 break ;
207
208 case L_SYSLOG:
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 ) ) ;
213 break ;
214 }
215
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' ) ;
221
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' ) ;
227 }
228
229
230 /*
231 * Print info about service scp to file descriptor fd
232 */
sc_dump(struct service_config * scp,int fd,int tab_level,bool_int is_defaults)233 void sc_dump( struct service_config *scp,
234 int fd,
235 int tab_level,
236 bool_int is_defaults )
237 {
238 const struct name_value *nvp ;
239 unsigned u ;
240 char **pp ;
241
242 if ( is_defaults )
243 tabprint( fd, tab_level, "Service defaults\n" ) ;
244 else
245 tabprint( fd, tab_level, "Service configuration: %s\n", SC_NAME(scp) ) ;
246
247 if ( ! is_defaults )
248 {
249 tabprint( fd, tab_level+1, "id = %s\n", SC_ID(scp) ) ;
250
251 if ( ! M_ARE_ALL_CLEAR( SC_XFLAGS(scp) ) )
252 {
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' ) ;
258 }
259
260 if ( ! M_ARE_ALL_CLEAR( SC_TYPE(scp) ) )
261 {
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' ) ;
267 }
268
269 tabprint( fd, tab_level+1, "socket_type = %s\n",
270 nv_get_name( socket_types, SC_SOCKET_TYPE(scp) ) ) ;
271
272 tabprint( fd, tab_level+1, "Protocol (name,number) = (%s,%d)\n",
273 SC_PROTONAME(scp), SC_PROTOVAL(scp) ) ;
274
275 if ( SC_SPECIFIED( scp, A_PORT ) )
276 tabprint( fd, tab_level+1, "port = %d\n", SC_PORT(scp) ) ;
277 }
278
279 if ( SC_SPECIFIED( scp, A_INSTANCES ) ) {
280 if ( SC_INSTANCES(scp) == UNLIMITED )
281 tabprint( fd, tab_level+1, "Instances = UNLIMITED\n" ) ;
282 else
283 tabprint( fd, tab_level+1, "Instances = %d\n", SC_INSTANCES(scp) ) ;
284 }
285
286 if ( SC_SPECIFIED( scp, A_WAIT ) ) {
287 if ( SC_WAIT(scp) )
288 tabprint( fd, tab_level+1, "wait = yes\n" ) ;
289 else
290 tabprint( fd, tab_level+1, "wait = no\n" ) ;
291 }
292
293 if ( SC_SPECIFIED( scp, A_USER ) )
294 tabprint( fd, tab_level+1, "user = %d\n", SC_UID(scp) ) ;
295
296 if ( SC_SPECIFIED( scp, A_GROUP ) )
297 tabprint( fd, tab_level+1, "group = %d\n", SC_GID(scp) ) ;
298
299 if ( SC_SPECIFIED( scp, A_GROUPS ) )
300 {
301 if (SC_GROUPS(scp) == 1)
302 tabprint( fd, tab_level+1, "Groups = yes\n" );
303 else
304 tabprint( fd, tab_level+1, "Groups = no\n" );
305 }
306
307 if ( SC_SPECIFIED( scp, A_UMASK ) )
308 tabprint( fd, tab_level+1, "umask = %o\n", SC_UMASK(scp) ) ;
309
310 if ( SC_SPECIFIED( scp, A_NICE ) )
311 tabprint( fd, tab_level+1, "Nice = %d\n", SC_NICE(scp) ) ;
312
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) );
316
317 if ( SC_SPECIFIED( scp, A_PER_SOURCE ) )
318 tabprint( fd, tab_level+1, "PER_SOURCE = %d\n",
319 SC_PER_SOURCE(scp) );
320
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);
327 else
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 );
334 }
335 else if ( SC_ORIG_BIND_ADDR(scp) ) {
336 tabprint( fd, tab_level+1, "Bind = %s\n",
337 SC_ORIG_BIND_ADDR(scp) );
338 }
339 else { /* This should NEVER happen */
340 msg(LOG_ERR, "sc_dump", "bad configuration for %s:",
341 SC_NAME(scp));
342 }
343 }
344 else
345 tabprint( fd, tab_level+1, "Bind = All addresses.\n" );
346
347 if ( ! is_defaults )
348 {
349 if ( (! SC_IS_INTERNAL( scp )) && (SC_REDIR_ADDR(scp) == NULL) )
350 {
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) )
354 {
355 for ( pp = SC_SERVER_ARGV(scp) ; *pp ; pp++ )
356 Sprint( fd, " %s", *pp ) ;
357 }
358 else
359 Sprint( fd, " (NULL)");
360 Sputchar( fd, '\n' ) ;
361 }
362
363 #ifdef LIBWRAP
364 if ( SC_LIBWRAP(scp) != NULL )
365 {
366 tabprint( fd, tab_level + 1, "Libwrap = %s\n", SC_LIBWRAP(scp) );
367 }
368 #endif
369
370 if ( SC_REDIR_ADDR(scp) != NULL )
371 {
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,
380 NULL, 0, 0) != 0 )
381 strcpy(redirname, "unknown");
382 tabprint( fd, tab_level+1, "Redirect = %s:%d\n", redirname,
383 SC_REDIR_ADDR(scp)->sa_in.sin_port );
384 }
385
386 if ( SC_IS_RPC( scp ) )
387 {
388 struct rpc_data *rdp = SC_RPCDATA( scp ) ;
389
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 ) ;
396 else
397 Sprint( fd, "%ld-%ld\n",
398 rdp->rd_min_version, rdp->rd_max_version ) ;
399 }
400
401 if ( SC_SPECIFIED( scp, A_ACCESS_TIMES ) )
402 {
403 tabprint( fd, tab_level+1, "Access times =" ) ;
404 ti_dump( SC_ACCESS_TIMES(scp), fd ) ;
405 Sputchar ( fd, '\n' ) ;
406 }
407 }
408
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" );
415 else
416 addrlist_dump( SC_ONLY_FROM(scp), fd ) ;
417 }
418 else
419 Sprint( fd, "All sites" );
420 Sputchar( fd, '\n' ) ;
421
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" );
428 else
429 addrlist_dump( SC_NO_ACCESS(scp), fd ) ;
430 }
431 else
432 Sprint( fd, "No blocked sites" );
433 Sputchar( fd, '\n' ) ;
434
435 if ( SC_SENSOR(scp) )
436 {
437 tabprint( fd, tab_level+1, "Deny Time: " ) ;
438 Sprint( fd, "%d\n", SC_DENY_TIME(scp));
439 }
440
441 dump_log_data( fd, scp, tab_level+1 ) ;
442
443 if ( SC_IS_PRESENT( scp, A_PASSENV ) )
444 {
445 tabprint( fd, tab_level+1, "Passenv =" ) ;
446 for ( u = 0 ; u < pset_count( SC_PASS_ENV_VARS(scp) ) ; u++ )
447 Sprint( fd, " %s",
448 (char *) pset_pointer( SC_PASS_ENV_VARS(scp), u ) ) ;
449 Sputchar ( fd, '\n' ) ;
450 }
451
452 if ( ! is_defaults )
453 if ( SC_SPECIFIED( scp, A_ENV ) )
454 {
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 ) ) ;
459 }
460
461 if ( SC_ENV( scp )->env_type == CUSTOM_ENV )
462 {
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 ) ;
466 }
467 Sflush( fd ) ;
468 }
469
470
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)) )
475
476 /*
477 * Two service configurations are considered different if any of the
478 * following is TRUE:
479 * 1) only one is unlisted
480 * 2) only one is internal
481 * 3) only one is RPC
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
486 * use diffent ports
487 *
488 * This function returns TRUE if the specified configurations are different.
489 *
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
493 * are different.
494 */
sc_different_confs(struct service_config * scp1,struct service_config * scp2)495 bool_int sc_different_confs( struct service_config *scp1,
496 struct service_config *scp2 )
497 {
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 ) )
501 return( TRUE ) ;
502
503 if ( SC_WAIT(scp1) != SC_WAIT(scp2) )
504 return( TRUE ) ;
505
506 if ( SC_PROTOVAL(scp1) != SC_PROTOVAL(scp2) )
507 return( TRUE ) ;
508
509 if ( SC_IS_RPC( scp1 ) )
510 {
511 if ( ! SAME_RPC( scp1, scp2 ) )
512 return( TRUE ) ;
513 }
514 else
515 {
516 if ( ! SAME_NONRPC( scp1, scp2 ) )
517 return( TRUE ) ;
518 }
519 return( FALSE ) ;
520 }
521
522