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 #ifndef SCONF_H
9 #define SCONF_H
10 
11 /*
12  * $Id: sconf.h,v 1.11 2007-09-20 14:58:27 bbraun Exp $
13  */
14 #include "config.h"
15 #ifdef HAVE_STDINT_H
16 #include <stdint.h>
17 #endif
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
22 #include <sys/time.h>
23 #ifdef HAVE_SYS_RESOURCE_H
24 #include <sys/resource.h>
25 #endif
26 #include "libportable.h"
27 
28 #include "pset.h"
29 #include "m_env.h"
30 #include "mask.h"
31 #include "defs.h"
32 #include "log.h"
33 #include "builtins.h"
34 #include "attr.h"
35 
36 /*
37  * Service types
38  */
39 #define ST_RPC          1
40 #define ST_INTERNAL     2
41 #define ST_UNLISTED     3
42 #define ST_SPECIAL      4
43 #define ST_DISABLED     5
44 #define ST_TCPMUX	6
45 #define ST_TCPMUXPLUS	7
46 
47 /*
48  * Service flags
49  */
50 #define SF_INTERCEPT    1
51 #define SF_REUSE        2
52 #define SF_NORETRY      3
53 #define SF_IDONLY       4
54 #define SF_NAMEINARGS   5
55 #define SF_NODELAY      6
56 #define SF_KEEPALIVE    7
57 #define SF_NOLIBWRAP    8
58 #define SF_SENSOR       9
59 #define SF_IPV4         10
60 #define SF_IPV6         11
61 #define SF_LABELED      12
62 
63 /*
64  * Values for log options
65  */
66 #define LO_HOST      1
67 #define LO_DURATION  3
68 #define LO_ATTEMPT   4
69 #define LO_EXIT      5
70 #define LO_PID       6
71 #define LO_USERID    7
72 #define LO_TRAFFIC   8
73 
74 struct rpc_data
75 {
76    unsigned long rd_min_version ;
77    unsigned long rd_max_version ;
78    unsigned long rd_program_number ;
79 } ;
80 
81 #define RD_MINVERS( rdp )      (rdp)->rd_min_version
82 #define RD_MAXVERS( rdp )      (rdp)->rd_max_version
83 #define RD_PROGNUM( rdp )      (rdp)->rd_program_number
84 
85 typedef enum { NO_ENV = 0, STD_ENV, DEF_ENV, CUSTOM_ENV } environ_e ;
86 
87 struct environment
88 {
89    environ_e  env_type ;
90    env_h      env_handle ;
91 } ;
92 
93 /*
94  * NOTE: Clearing the structure will give all its fields their default values
95  */
96 struct service_config
97 {
98    mask_t               sc_specified_attributes; /* specified attributes */
99    mask_t               sc_attributes_present; /* includes those from defaults*/
100    mask_t               sc_type;               /* RPC, UNLISTED etc           */
101    mask_t               sc_xflags ;            /* INTERCEPT etc               */
102    char                *sc_name;               /* e g  "echo"                 */
103    char                *sc_id ;                /* e.g. "echo-stream"          */
104    uint16_t             sc_port ;              /* in host byte order          */
105    int                  sc_socket_type ;       /* e.g. SOCK_DGRAM             */
106    struct protocol_name_value sc_protocol ;    /* e.g. "TCP", IPPROTO_TCP     */
107    boolean_e            sc_wait ;
108    uid_t                sc_uid ;
109    gid_t                sc_user_gid ;          /* gid corresponding to uid    */
110    gid_t                sc_gid ;               /* gid corresponding to group  */
111    char                *sc_server ;
112    char               **sc_server_argv ;
113    int                  sc_instances ;
114    int                  sc_nice ;              /* argument for nice(3) */
115    pset_h               sc_env_var_defs ;      /* list of env strings         */
116    pset_h               sc_pass_env_vars ;     /* env vars to pass to server  */
117    pset_h               sc_access_times ;
118    pset_h               sc_only_from ;
119    pset_h               sc_no_access ;
120    mask_t               sc_log_on_success ;
121    mask_t               sc_log_on_failure ;
122    struct log           sc_log ;
123    struct rpc_data      sc_rd ;
124    pset_h               sc_disabled ;     /* used only by the default entry */
125    pset_h               sc_enabled ;      /* used only by the default entry */
126    struct environment   sc_environment ;
127    const builtin_s     *sc_builtin ;
128    union xsockaddr     *sc_redir_addr ;
129    char                *sc_orig_bind_addr ; /* used only when dual stack */
130    union xsockaddr     *sc_bind_addr ;
131    boolean_e            sc_v6only;
132    char                *sc_banner ;
133    int                  sc_per_source ;
134    boolean_e            sc_groups ;
135    char                *sc_banner_success ;
136    char                *sc_banner_fail ;
137    double               sc_max_load ;
138    time_t               sc_time_limit ;
139    time_t               sc_time_conn ;
140    time_t               sc_time_conn_max ;
141    time_t               sc_time_wait ;
142    time_t               sc_time_reenable ;
143    rlim_t               sc_rlim_as;
144    rlim_t               sc_rlim_cpu;
145    rlim_t               sc_rlim_data;
146    rlim_t               sc_rlim_rss;
147    rlim_t               sc_rlim_stack;
148    mode_t               sc_umask;
149    int                  sc_deny_time;         /* Sensor deny access time:
150                                                  -1: forever
151                                                   0: never
152                                                   X: X minutes          */
153 #ifdef HAVE_MDNS
154    char                *sc_mdns_name;
155    boolean_e            sc_mdns;
156    void                *mdns_state;
157 #endif
158 #ifdef LIBWRAP
159    char                *sc_libwrap;
160 #endif
161 } ;
162 
163 #define SCP( p ) ((struct service_config *)(p))
164 
165 /*
166  * Field access macros
167  */
168 #define SC_LOG( scp )            (&(scp)->sc_log)
169 #define SC_RPCDATA( scp )        (&(scp)->sc_rd)
170 #define SC_ENV( scp )            (&(scp)->sc_environment)
171 #define SC_ENV_VAR_DEFS( scp )   (scp)->sc_env_var_defs
172 #define SC_PORT( scp )           (scp)->sc_port
173 #define SC_NICE( scp )           (scp)->sc_nice
174 #define SC_SOCKET_TYPE( scp )    (scp)->sc_socket_type
175 #define SC_ID( scp )             (scp)->sc_id
176 #define SC_NAME( scp )           (scp)->sc_name
177 #define SC_PROTOVAL( scp )       (scp)->sc_protocol.value
178 #define SC_PROTONAME( scp )      (scp)->sc_protocol.name
179 #define SC_INSTANCES( scp )      (scp)->sc_instances
180 #define SC_UID( scp )            (scp)->sc_uid
181 #define SC_GID( scp )            (scp)->sc_gid
182 #define SC_USER_GID( scp )       (scp)->sc_user_gid
183 #define SC_SERVER( scp )         (scp)->sc_server
184 #define SC_SERVER_ARGV( scp )    (scp)->sc_server_argv
185 #define SC_ONLY_FROM( scp )      (scp)->sc_only_from
186 #define SC_NO_ACCESS( scp )      (scp)->sc_no_access
187 #define SC_ACCESS_TIMES( scp )   (scp)->sc_access_times
188 #define SC_LOG_ON_SUCCESS( scp ) (scp)->sc_log_on_success
189 #define SC_LOG_ON_FAILURE( scp ) (scp)->sc_log_on_failure
190 #define SC_PASS_ENV_VARS( scp )  (scp)->sc_pass_env_vars
191 #define SC_RLIM_AS( scp )        (scp)->sc_rlim_as
192 #define SC_RLIM_CPU( scp )       (scp)->sc_rlim_cpu
193 #define SC_RLIM_DATA( scp )      (scp)->sc_rlim_data
194 #define SC_RLIM_RSS( scp )       (scp)->sc_rlim_rss
195 #define SC_RLIM_STACK( scp )     (scp)->sc_rlim_stack
196 #define SC_TYPE( scp )           (scp)->sc_type
197 #define SC_WAIT( scp )           (scp)->sc_wait
198 #define SC_XFLAGS( scp )         (scp)->sc_xflags
199 #define SC_V6ONLY( scp )         (scp)->sc_v6only
200 #define SC_ENABLED( scp )        (scp)->sc_enabled
201 #define SC_DISABLED( scp )       (scp)->sc_disabled
202 #define SC_BUILTIN( scp )        (scp)->sc_builtin
203 #define SC_REDIR_ADDR( scp )     (scp)->sc_redir_addr
204 #define SC_ORIG_BIND_ADDR( scp ) (scp)->sc_orig_bind_addr
205 #define SC_BIND_ADDR( scp )      (scp)->sc_bind_addr
206 #define SC_BANNER( scp )         (scp)->sc_banner
207 #define SC_BANNER_SUCCESS( scp ) (scp)->sc_banner_success
208 #define SC_BANNER_FAIL( scp )    (scp)->sc_banner_fail
209 #define SC_GROUPS( scp )         (scp)->sc_groups
210 #define SC_MAX_LOAD( scp )       (scp)->sc_max_load
211 #define SC_TIME_LIMIT( scp )     (scp)->sc_time_limit
212 #define SC_TIME_CONN( scp )      (scp)->sc_time_conn
213 #define SC_TIME_CONN_MAX( scp )  (scp)->sc_time_conn_max
214 #define SC_TIME_WAIT( scp )      (scp)->sc_time_wait
215 #define SC_TIME_REENABLE( scp )  (scp)->sc_time_reenable
216 #define SC_UMASK( scp )          (scp)->sc_umask
217 #define SC_DENY_TIME( scp )      (scp)->sc_deny_time
218 #define SC_MDNS_NAME( scp )      (scp)->sc_mdns_name
219 #define SC_MDNS( scp )           (scp)->sc_mdns
220 #define SC_PER_SOURCE( scp )     (scp)->sc_per_source
221 #define SC_LIBWRAP( scp )        (scp)->sc_libwrap
222 /*
223  * Field set macros
224  */
225 #define SC_SET_PORT( scp, port ) (scp)->sc_port = (port)
226 
227 /*
228  * Predicate checking macros
229  */
230 #define SC_FORKS( scp )           ( ! SC_IS_INTERNAL( scp ) ||   \
231     BUILTIN_FORKS( (scp)->sc_builtin ) )
232 #define SC_WAITS( scp )           ( (scp)->sc_wait == YES )
233 #define SC_RETRY( scp )           ( M_IS_CLEAR( (scp)->sc_xflags, SF_NORETRY ) )
234 #define SC_MUST_IDENTIFY( scp )   M_IS_SET( (scp)->sc_xflags, SF_IDONLY )
235 #define SC_NAMEINARGS( scp )      M_IS_SET( (scp)->sc_xflags, SF_NAMEINARGS )
236 #define SC_NODELAY( scp )         M_IS_SET( (scp)->sc_xflags, SF_NODELAY )
237 #define SC_KEEPALIVE( scp )       M_IS_SET( (scp)->sc_xflags, SF_KEEPALIVE )
238 #define SC_NOLIBWRAP( scp )       M_IS_SET( (scp)->sc_xflags, SF_NOLIBWRAP )
239 #define SC_IS_INTERCEPTED( scp ) ( M_IS_SET( (scp)->sc_xflags, SF_INTERCEPT ) )
240 #define SC_SENSOR( scp )          M_IS_SET( (scp)->sc_xflags, SF_SENSOR )
241 #define SC_IPV4( scp )            M_IS_SET( (scp)->sc_xflags, SF_IPV4 )
242 #define SC_IPV6( scp )            M_IS_SET( (scp)->sc_xflags, SF_IPV6 )
243 #define SC_LABELED_NET( scp )     M_IS_SET( (scp)->sc_xflags, SF_LABELED )
244 
245 #define SC_IS_RPC( scp )         ( M_IS_SET( (scp)->sc_type, ST_RPC ) )
246 #define SC_IS_INTERNAL( scp )    ( M_IS_SET( (scp)->sc_type, ST_INTERNAL ) )
247 #define SC_IS_SPECIAL( scp )     ( M_IS_SET( (scp)->sc_type, ST_SPECIAL ) )
248 #define SC_IS_UNLISTED( scp )    ( M_IS_SET( (scp)->sc_type, ST_UNLISTED ) )
249 #define SC_IS_DISABLED( scp )    ( M_IS_SET( (scp)->sc_type, ST_DISABLED ) )
250 #define SC_DISABLE(scp)          ( M_SET( (scp)->sc_type, ST_DISABLED ) )
251 #define SC_ENABLE(scp)           ( M_CLEAR( (scp)->sc_type, ST_DISABLED ) )
252 #define SC_IS_MUXCLIENT( scp )   ( M_IS_SET( (scp)->sc_type, ST_TCPMUX ) || \
253 				   M_IS_SET( (scp)->sc_type, ST_TCPMUXPLUS ) )
254 #define SC_IS_MUXPLUSCLIENT(scp) ( M_IS_SET( (scp)->sc_type, ST_TCPMUXPLUS ) )
255 #define SC_IS_TCPMUX( scp )	 ( (scp)->sc_builtin &&                     \
256                                    (BUILTIN_HANDLER( (scp)->sc_builtin ) == \
257 				   (void *)tcpmux_handler ) )
258 
259 #define LOGS_USERID( scp, flags ) \
260    ( M_IS_SET( (scp)->flags, LO_USERID ) && SC_ACCEPTS_CONNECTIONS( scp ) )
261 
262 #define LOGS_ANY( scp, flags )   ( ! M_ARE_ALL_CLEAR( (scp)->flags ) )
263 
264 #define SC_LOGS_ON_SUCCESS( scp )        LOGS_ANY( scp, sc_log_on_success )
265 #define SC_LOGS_ON_FAILURE( scp )        LOGS_ANY( scp, sc_log_on_failure )
266 #define SC_LOGS_USERID_ON_FAILURE( scp ) LOGS_USERID( scp, sc_log_on_failure )
267 #define SC_LOGS_USERID_ON_SUCCESS( scp ) LOGS_USERID( scp, sc_log_on_success )
268 #define SC_LOGS_ON_EXIT( scp )  \
269    ( M_IS_SET( (scp)->sc_log_on_success, LO_DURATION ) || \
270       M_IS_SET( (scp)->sc_log_on_success, LO_EXIT ) )
271 #define SC_LOGS_PID( scp )   M_IS_SET( (scp)->sc_log_on_success, LO_PID )
272 #define SC_LOGS_EXITS( scp ) M_IS_SET( (scp)->sc_log_on_success, LO_EXIT )
273 #define SC_LOGS_DURATION( scp ) \
274    M_IS_SET( (scp)->sc_log_on_success, LO_DURATION )
275 
276 
277 #define SC_MUST_LISTEN( scp )   ( (scp)->sc_socket_type == SOCK_STREAM )
278 
279 #define SC_ACCEPTS_CONNECTIONS( scp )     \
280       ( (scp)->sc_wait == NO && (scp)->sc_socket_type == SOCK_STREAM )
281 
282 #define SC_SPECIFIED( scp, attr )   \
283                M_IS_SET( (scp)->sc_specified_attributes, (attr) )
284 #define SC_SPECIFY( scp, attr )     \
285                {                                                     \
286                   M_SET( (scp)->sc_specified_attributes, (attr) ) ;  \
287                   SC_PRESENT( (scp), (attr) ) ;                      \
288                }
289 
290 #define SC_IS_PRESENT( scp, attr )  \
291                M_IS_SET( (scp)->sc_attributes_present, (attr) )
292 #define SC_PRESENT( scp, attr )     \
293                M_SET( (scp)->sc_attributes_present, (attr) )
294 
295 #define SC_GETGID( scp ) ( SC_SPECIFIED( scp, A_GROUP ) \
296    ? (scp)->sc_gid : (scp)->sc_user_gid )
297 #define SC_INTERNAL( scp, serp )  BUILTIN_INVOKE( (scp)->sc_builtin, serp )
298 #define SC_MAKE_EXTERNAL( scp )   M_CLEAR( (scp)->sc_type, ST_INTERNAL )
299 
300 struct service_config *sc_alloc(const char *name);
301 void sc_free(struct service_config *scp);
302 struct service_config *sc_make_special(const char *service_name,const builtin_s *bp,int instances);
303 void sc_dump(struct service_config *scp,int fd,int tab_level,bool_int is_defaults);
304 bool_int sc_different_confs(struct service_config *scp1,struct service_config *scp2);
305 
306 
307 #endif   /* SCONF_H */
308