1 /*        $NetBSD: ntp_config.h,v 1.15 2024/08/18 20:46:50 christos Exp $       */
2 
3 #ifndef NTP_CONFIG_H
4 #define NTP_CONFIG_H
5 
6 #ifdef HAVE_SYS_RESOURCE_H
7 # include <sys/resource.h>
8 #endif /* HAVE_SYS_RESOURCE_H */
9 
10 #include "ntp_machine.h"
11 #include "ntp_psl.h"
12 #include "ntpsim.h"
13 
14 
15 /*
16  * Configuration file name
17  */
18 #ifndef CONFIG_FILE
19 # ifndef SYS_WINNT
20 #  define CONFIG_FILE "/etc/ntp.conf"
21 # else /* SYS_WINNT */
22 #  define CONFIG_FILE         "%windir%\\system32\\drivers\\etc\\ntp.conf"
23 #  define ALT_CONFIG_FILE "%windir%\\ntp.conf"
24 #  define NTP_KEYSDIR         "%windir%\\system32\\drivers\\etc"
25 # endif /* SYS_WINNT */
26 #endif /* not CONFIG_FILE */
27 
28 
29 /*
30  * We keep config trees around for possible saveconfig use.  When
31  * built with configure --disable-saveconfig, and when built with
32  * debugging enabled, include the free_config_*() routines.  In the
33  * DEBUG case, they are used in an atexit() cleanup routine to make
34  * postmortem leak check reports more interesting.
35  */
36 #if !defined(FREE_CFG_T) && (!defined(SAVECONFIG) || defined(DEBUG))
37 #define FREE_CFG_T
38 #endif
39 
40 /* Limits */
41 #define MAXLINE 1024
42 
43 /* Configuration sources */
44 
45 #define CONF_SOURCE_FILE                0
46 #define CONF_SOURCE_NTPQ                1
47 
48 /* list of servers from command line for config_peers() */
49 extern    int       cmdline_server_count;
50 extern    char **   cmdline_servers;
51 
52 /* set to zero if we're not locking memory */
53 extern    int       cur_memlock;
54 
55 typedef struct int_range_tag {
56           int       first;
57           int       last;
58 } int_range;
59 
60 /* generic list node */
61 typedef struct any_node_tag any_node;
62 struct any_node_tag {
63           any_node *          link;
64 };
65 
66 typedef DECL_FIFO_ANCHOR(any_node) any_node_fifo;
67 
68 /* Structure for storing an attribute-value pair */
69 typedef struct attr_val_tag attr_val;
70 struct attr_val_tag {
71           attr_val *          link;
72           int                 attr;
73           int                 type;     /* T_String, T_Integer, ... */
74           int                 flag;     /* auxiliary flags */
75           union val {
76                     double              d;        /* T_Double */
77                     int                 i;        /* T_Integer */
78                     int_range r;        /* T_Intrange */
79                     char *              s;        /* T_String */
80                     u_int               u;        /* T_U_int */
81           } value;
82 };
83 
84 typedef DECL_FIFO_ANCHOR(attr_val) attr_val_fifo;
85 
86 /* Structure for nodes on the syntax tree */
87 typedef struct address_node_tag address_node;
88 struct address_node_tag {
89           address_node *      link;
90           char *              address;
91           u_short             type;     /* family, AF_UNSPEC (0), AF_INET[6] */
92 };
93 
94 typedef DECL_FIFO_ANCHOR(address_node) address_fifo;
95 
96 typedef struct int_node_tag int_node;
97 struct int_node_tag {
98           int_node *          link;
99           int                 i;
100 };
101 
102 typedef DECL_FIFO_ANCHOR(int_node) int_fifo;
103 
104 typedef struct string_node_tag string_node;
105 struct string_node_tag {
106           string_node *       link;
107           char *              s;
108 };
109 
110 typedef DECL_FIFO_ANCHOR(string_node) string_fifo;
111 
112 typedef struct restrict_node_tag restrict_node;
113 struct restrict_node_tag {
114           restrict_node *     link;
115           address_node *      addr;
116           address_node *      mask;
117           attr_val_fifo *     flag_tok_fifo;
118           int/*BOOL*/         remove;
119           int                 line_no;
120           int                 column;
121           short               ippeerlimit;
122           short               srvfuzrft;
123 };
124 
125 typedef DECL_FIFO_ANCHOR(restrict_node) restrict_fifo;
126 
127 typedef struct peer_node_tag peer_node;
128 struct peer_node_tag {
129           peer_node *         link;
130           int                 host_mode;
131           address_node *      addr;
132           attr_val_fifo *     peerflags;
133           u_char              minpoll;
134           u_char              maxpoll;
135           u_int32             ttl;
136           u_char              peerversion;
137           keyid_t             peerkey;
138           char *              group;
139 };
140 
141 typedef DECL_FIFO_ANCHOR(peer_node) peer_fifo;
142 
143 typedef struct unpeer_node_tag unpeer_node;
144 struct unpeer_node_tag {
145           unpeer_node *       link;
146           associd_t assocID;
147           address_node *      addr;
148 };
149 
150 typedef DECL_FIFO_ANCHOR(unpeer_node) unpeer_fifo;
151 
152 typedef struct auth_node_tag auth_node;
153 struct auth_node_tag {
154           int                 control_key;
155           int                 cryptosw;
156           attr_val_fifo *     crypto_cmd_list;
157           char *              keys;
158           char *              keysdir;
159           int                 request_key;
160           int                 revoke;
161           attr_val_fifo *     trusted_key_list;
162           char *              ntp_signd_socket;
163 };
164 
165 typedef struct filegen_node_tag filegen_node;
166 struct filegen_node_tag {
167           filegen_node *      link;
168           int                 filegen_token;
169           attr_val_fifo *     options;
170 };
171 
172 typedef DECL_FIFO_ANCHOR(filegen_node) filegen_fifo;
173 
174 typedef struct setvar_node_tag setvar_node;
175 struct setvar_node_tag {
176           setvar_node *       link;
177           char *              var;
178           char *              val;
179           int                 isdefault;
180 };
181 
182 typedef DECL_FIFO_ANCHOR(setvar_node) setvar_fifo;
183 
184 typedef struct nic_rule_node_tag nic_rule_node;
185 struct nic_rule_node_tag {
186           nic_rule_node *     link;
187           int                 match_class;
188           char *              if_name;  /* or numeric address */
189           int                 action;
190 };
191 
192 typedef DECL_FIFO_ANCHOR(nic_rule_node) nic_rule_fifo;
193 
194 typedef struct addr_opts_node_tag addr_opts_node;
195 struct addr_opts_node_tag {
196           addr_opts_node *link;
197           address_node *      addr;
198           attr_val_fifo *     options;
199 };
200 
201 typedef DECL_FIFO_ANCHOR(addr_opts_node) addr_opts_fifo;
202 
203 typedef struct sim_node_tag sim_node;
204 struct sim_node_tag {
205           sim_node *                    link;
206           attr_val_fifo *               init_opts;
207           server_info_fifo *  servers;
208 };
209 
210 typedef DECL_FIFO_ANCHOR(sim_node) sim_fifo;
211 
212 /* The syntax tree */
213 typedef struct config_tree_tag config_tree;
214 struct config_tree_tag {
215           config_tree *       link;
216 
217           attr_val  source;
218           time_t              timestamp;
219 
220           peer_fifo *         peers;
221           unpeer_fifo *       unpeers;
222 
223           /* Other Modes */
224           int                 broadcastclient;
225           address_fifo *      manycastserver;
226           address_fifo *      multicastclient;
227 
228           attr_val_fifo *     orphan_cmds;        /* s/b renamed tos_options */
229 
230           /* Monitoring Configuration */
231           int_fifo *          stats_list;
232           char *              stats_dir;
233           filegen_fifo *      filegen_opts;
234 
235           /* Access Control Configuration */
236           attr_val_fifo *     discard_opts;
237           attr_val_fifo *     mru_opts;
238           restrict_fifo *     restrict_opts;
239 
240           addr_opts_fifo *fudge;
241           addr_opts_fifo *device;
242           attr_val_fifo *     rlimit;
243           attr_val_fifo *     tinker;
244           attr_val_fifo *     enable_opts;
245           attr_val_fifo *     disable_opts;
246 
247           auth_node auth;
248 
249           attr_val_fifo *     logconfig;
250           string_fifo *       phone;
251           setvar_fifo *       setvar;
252           int_fifo *          ttl;
253           addr_opts_fifo *trap;
254           attr_val_fifo *     vars;
255           nic_rule_fifo *     nic_rules;
256           int_fifo *          reset_counters;
257           attr_val_fifo *     pollskewlist;
258 
259           sim_fifo *          sim_details;
260           int                 mdnstries;
261 };
262 
263 
264 /* Structure for holding a remote configuration command */
265 struct REMOTE_CONFIG_INFO {
266           char buffer[MAXLINE];
267           char err_msg[MAXLINE];
268           int pos;
269           int err_pos;
270           int no_errors;
271 };
272 
273 
274 /*
275  * context for trap_name_resolved() to call ctlsettrap() once the
276  * name->address resolution completes.
277  */
278 typedef struct settrap_parms_tag {
279           sockaddr_u          ifaddr;
280           int                 ifaddr_nonnull;
281 } settrap_parms;
282 
283 
284 /*
285 ** Data Minimization Items
286 */
287 
288 /* Serverresponse fuzz reftime: stored in 'restrict' fifos */
289 
290 
291 /* get text from T_ tokens */
292 const char * token_name(int token);
293 
294 /* generic fifo routines for structs linked by 1st member */
295 typedef void (*fifo_deleter)(void*);
296 void *    destroy_gen_fifo(void *fifo, fifo_deleter func);
297 void *    append_gen_fifo(void *fifo, void *entry);
298 void *    concat_gen_fifos(void *first, void *second);
299 #define DESTROY_G_FIFO(pf, func)        \
300           ((pf) = destroy_gen_fifo((pf), (fifo_deleter)(func)))
301 #define APPEND_G_FIFO(pf, pe)           \
302           ((pf) = append_gen_fifo((pf), (pe)))
303 #define CONCAT_G_FIFOS(first, second)   \
304           ((first) = concat_gen_fifos((first), (second)))
305 #define HEAD_PFIFO(pf)                            \
306           (((pf) != NULL)                         \
307                 ? HEAD_FIFO(*(pf))      \
308                 : NULL)
309 
310 peer_node *create_peer_node(int hmode, address_node *addr,
311                                   attr_val_fifo *options);
312 unpeer_node *create_unpeer_node(address_node *addr);
313 address_node *create_address_node(char *addr, int type);
314 void destroy_address_node(address_node *my_node);
315 attr_val *create_attr_dval(int attr, double value);
316 attr_val *create_attr_ival(int attr, int value);
317 attr_val *create_attr_rval(int attr, int first, int last);
318 attr_val *create_attr_sval(int attr, const char *s);
319 attr_val *create_attr_uval(int attr, u_int value);
320 void        destroy_attr_val(attr_val *node);
321 filegen_node *create_filegen_node(int filegen_token,
322                                           attr_val_fifo *options);
323 string_node *create_string_node(char *str);
324 restrict_node *create_restrict_node(address_node *          addr,
325                                             address_node *  mask,
326                                             short           ippeerlimit,
327                                             attr_val_fifo * flag_tok_fifo,
328                                             int/*BOOL*/               remove,
329                                             int                       nline,
330                                             int                       ncol);
331 int_node *create_int_node(int val);
332 addr_opts_node *create_addr_opts_node(address_node *addr,
333                                               attr_val_fifo *options);
334 sim_node *create_sim_node(attr_val_fifo *init_opts,
335                                 server_info_fifo *servers);
336 setvar_node *create_setvar_node(char *var, char *val, int isdefault);
337 nic_rule_node *create_nic_rule_node(int match_class, char *if_name,
338                                             int action);
339 
340 script_info *create_sim_script_info(double duration,
341                                             attr_val_fifo *script_queue);
342 server_info *create_sim_server(address_node *addr, double server_offset,
343                                      script_info_fifo *script);
344 
345 extern struct REMOTE_CONFIG_INFO remote_config;
346 void config_remotely(sockaddr_u *);
347 
348 #ifdef SAVECONFIG
349 int dump_config_tree(config_tree *ptree, FILE *df, int comment);
350 int dump_all_config_trees(FILE *df, int comment);
351 #endif
352 
353 #if defined(HAVE_SETRLIMIT)
354 void ntp_rlimit(int, rlim_t, int, const char *);
355 #endif
356 
357 #endif    /* !defined(NTP_CONFIG_H) */
358