cleanup: specify never-interactive option for flex scanner
[secnet] / secnet.h
CommitLineData
2fe58dfd
SE
1/* Core interface of secnet, to be used by all modules */
2
3#ifndef secnet_h
4#define secnet_h
5
59635212 6#include "config.h"
2fe58dfd 7#include <stdlib.h>
2fe58dfd 8#include <stdarg.h>
4f5e39ec 9#include <stdio.h>
2fe58dfd 10#include <sys/poll.h>
8689b3a9
SE
11#include <sys/types.h>
12#include <sys/time.h>
2fe58dfd 13#include <netinet/in.h>
2fe58dfd 14
3b83c932
SE
15/*
16 * Macros added by SGT for endianness-independence
17 */
18#define GET_32BIT_MSB_FIRST(cp) \
19 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
20 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
21 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
22 ((unsigned long)(unsigned char)(cp)[3]))
23
24#define PUT_32BIT_MSB_FIRST(cp, value) ( \
25 (cp)[0] = (char)((value) >> 24), \
26 (cp)[1] = (char)((value) >> 16), \
27 (cp)[2] = (char)((value) >> 8), \
28 (cp)[3] = (char)(value) )
29
2fe58dfd 30typedef char *string_t;
fe5e9cc4 31typedef const char *cstring_t;
2fe58dfd
SE
32typedef enum {False,True} bool_t;
33
794f2398 34#define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
4f5e39ec 35 __FILE__,__LINE__); } } while(0)
2fe58dfd 36
fcbc5905
IJ
37/* from version.c */
38
39extern char version[];
40
08ee90a2
IJ
41/* from logmsg.c */
42extern uint32_t message_level;
43extern bool_t secnet_is_daemon;
44extern struct log_if *system_log;
45
46/* from process.c */
47extern void start_signal_handling(void);
48
2fe58dfd
SE
49/***** CONFIGURATION support *****/
50
baa06aeb
SE
51extern bool_t just_check_config; /* If True then we're going to exit after
52 reading the configuration file */
b2a56f7c 53extern bool_t background; /* If True then we'll eventually run as a daemon */
baa06aeb 54
2fe58dfd
SE
55typedef struct dict dict_t; /* Configuration dictionary */
56typedef struct closure closure_t;
57typedef struct item item_t;
58typedef struct list list_t; /* A list of items */
59
60/* Configuration file location, for error-reporting */
61struct cloc {
fe5e9cc4 62 cstring_t file;
2fe58dfd
SE
63 uint32_t line;
64};
65
66/* Modules export closures, which can be invoked from the configuration file.
67 "Invoking" a closure usually returns another closure (of a different
68 type), but can actually return any configuration object. */
69typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
70 dict_t *context, list_t *data);
71struct closure {
fe5e9cc4 72 cstring_t description; /* For debugging */
2fe58dfd
SE
73 uint32_t type; /* Central registry... */
74 apply_fn *apply;
75 void *interface; /* Interface for use inside secnet; depends on type */
76};
77
78enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
79struct item {
80 enum types type;
81 union {
82 bool_t bool;
83 string_t string;
84 uint32_t number;
85 dict_t *dict;
86 closure_t *closure;
87 } data;
88 struct cloc loc;
89};
90
ff05a229
SE
91/* Note that it is unwise to use this structure directly; use the list
92 manipulation functions instead. */
2fe58dfd
SE
93struct list {
94 item_t *item;
95 struct list *next;
96};
97
98/* In the following two lookup functions, NULL means 'not found' */
99/* Lookup a value in the specified dictionary, or its parents */
fe5e9cc4 100extern list_t *dict_lookup(dict_t *dict, cstring_t key);
2fe58dfd 101/* Lookup a value in just the specified dictionary */
fe5e9cc4 102extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key);
2fe58dfd 103/* Add a value to the specified dictionary */
fe5e9cc4 104extern void dict_add(dict_t *dict, cstring_t key, list_t *val);
2fe58dfd 105/* Obtain an array of keys in the dictionary. malloced; caller frees */
fe5e9cc4 106extern cstring_t *dict_keys(dict_t *dict);
2fe58dfd
SE
107
108/* List-manipulation functions */
109extern list_t *list_new(void);
b2a56f7c 110extern uint32_t list_length(list_t *a);
2fe58dfd
SE
111extern list_t *list_append(list_t *a, item_t *i);
112extern list_t *list_append_list(list_t *a, list_t *b);
113/* Returns an item from the list (index starts at 0), or NULL */
114extern item_t *list_elem(list_t *l, uint32_t index);
115
116/* Convenience functions */
117extern list_t *new_closure(closure_t *cl);
fe5e9cc4
SE
118extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply);
119extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type,
120 bool_t fail_if_invalid, cstring_t desc,
2fe58dfd 121 struct cloc loc);
fe5e9cc4
SE
122extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required,
123 cstring_t desc, struct cloc loc);
124extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required,
125 cstring_t desc, struct cloc loc);
126extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required,
127 cstring_t desc, struct cloc loc,
128 uint32_t def);
129extern bool_t dict_read_bool(dict_t *dict, cstring_t key, bool_t required,
130 cstring_t desc, struct cloc loc, bool_t def);
9d3a4132 131struct flagstr {
fe5e9cc4 132 cstring_t name;
9d3a4132
SE
133 uint32_t value;
134};
fe5e9cc4
SE
135extern uint32_t string_to_word(cstring_t s, struct cloc loc,
136 struct flagstr *f, cstring_t desc);
9d3a4132 137extern uint32_t string_list_to_word(list_t *l, struct flagstr *f,
fe5e9cc4 138 cstring_t desc);
2fe58dfd
SE
139
140/***** END of configuration support *****/
141
7138d0c5
SE
142/***** UTILITY functions *****/
143
fe5e9cc4
SE
144extern char *safe_strdup(const char *string, const char *message);
145extern void *safe_malloc(size_t size, const char *message);
2fe58dfd 146
fe5e9cc4 147extern int sys_cmd(const char *file, const char *argc, ...);
4efd681a 148
2fe58dfd
SE
149/***** END of utility functions *****/
150
151/***** SCHEDULING support */
152
153/* "now" is current program time, in milliseconds. It is derived
154 (once) from tv_now. If nfds_io is insufficient for your needs, set
155 it to the required number and return ERANGE. timeout is in milliseconds;
156 if it is too high then lower it. It starts at -1 (==infinite) */
157typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
158 int *timeout_io, const struct timeval *tv_now,
159 uint64_t *now);
160typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds,
161 const struct timeval *tv_now, uint64_t *now);
162
163/* Register interest in the main loop of the program. Before a call
164 to poll() your supplied beforepoll function will be called. After
165 the call to poll() the supplied afterpoll function will be called.
166 max_nfds is a _hint_ about the maximum number of struct pollfd
167 structures you may require - you can always ask for more in
168 *nfds_io. */
169extern void register_for_poll(void *st, beforepoll_fn *before,
170 afterpoll_fn *after, uint32_t max_nfds,
fe5e9cc4 171 cstring_t desc);
2fe58dfd
SE
172
173/***** END of scheduling support */
174
175/***** PROGRAM LIFETIME support */
176
177/* The secnet program goes through a number of phases in its lifetime.
178 Module code may arrange to be called just as various phases are
179 entered. */
180
181#define PHASE_INIT 0
182#define PHASE_GETOPTS 1 /* Process command-line arguments */
183#define PHASE_READCONFIG 2 /* Parse and process configuration file */
184#define PHASE_SETUP 3 /* Process information in configuration */
baa06aeb
SE
185#define PHASE_GETRESOURCES 4 /* Obtain all external resources */
186#define PHASE_DROPPRIV 5 /* Last chance for privileged operations */
187#define PHASE_RUN 6
188#define PHASE_SHUTDOWN 7 /* About to die; delete key material, etc. */
189#define NR_PHASES 8
2fe58dfd
SE
190
191typedef void hook_fn(void *self, uint32_t newphase);
192bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
193bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
194
7138d0c5
SE
195extern uint32_t current_phase;
196extern void enter_phase(uint32_t new_phase);
197
ff05a229
SE
198/* Some features (like netlink 'soft' routes) require that secnet
199 retain root privileges. They should indicate that here when
200 appropriate. */
201extern bool_t require_root_privileges;
fe5e9cc4 202extern cstring_t require_root_privileges_explanation;
9d3a4132 203
2fe58dfd
SE
204/***** END of program lifetime support *****/
205
206/***** MODULE support *****/
207
208/* Module initialisation function type - modules export one function of
209 this type which is called to initialise them. For dynamically loaded
210 modules it's called "secnet_module". */
469fd1d9 211typedef void init_module(dict_t *dict);
2fe58dfd 212
08ee90a2
IJ
213extern void init_builtin_modules(dict_t *dict);
214
215extern init_module resolver_module;
216extern init_module random_module;
217extern init_module udp_module;
218extern init_module util_module;
219extern init_module site_module;
220extern init_module transform_module;
221extern init_module netlink_module;
222extern init_module rsa_module;
223extern init_module dh_module;
224extern init_module md5_module;
225extern init_module slip_module;
226extern init_module tun_module;
227extern init_module sha1_module;
228extern init_module log_module;
229
2fe58dfd
SE
230/***** END of module support *****/
231
232/***** CLOSURE TYPES and interface definitions *****/
233
469fd1d9
SE
234#define CL_PURE 0
235#define CL_RESOLVER 1
236#define CL_RANDOMSRC 2
237#define CL_RSAPUBKEY 3
238#define CL_RSAPRIVKEY 4
239#define CL_COMM 5
240#define CL_IPIF 6
241#define CL_LOG 7
242#define CL_SITE 8
243#define CL_TRANSFORM 9
244#define CL_DH 11
245#define CL_HASH 12
246#define CL_BUFFER 13
247#define CL_NETLINK 14
2fe58dfd
SE
248
249struct buffer_if;
250
251/* PURE closure requires no interface */
252
253/* RESOLVER interface */
254
255/* Answers to queries are delivered to a function of this
256 type. 'address' will be NULL if there was a problem with the query. It
257 will be freed once resolve_answer_fn returns. It is in network byte
258 order. */
ff05a229 259/* XXX extend to be able to provide multiple answers */
2fe58dfd 260typedef void resolve_answer_fn(void *st, struct in_addr *addr);
fe5e9cc4 261typedef bool_t resolve_request_fn(void *st, cstring_t name,
2fe58dfd
SE
262 resolve_answer_fn *cb, void *cst);
263struct resolver_if {
264 void *st;
265 resolve_request_fn *request;
266};
267
268/* RANDOMSRC interface */
269
270/* Return some random data. Returns TRUE for success. */
271typedef bool_t random_fn(void *st, uint32_t bytes, uint8_t *buff);
272
273struct random_if {
274 void *st;
275 bool_t blocking;
276 random_fn *generate;
277};
278
279/* RSAPUBKEY interface */
280
281typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, uint32_t datalen,
fe5e9cc4 282 cstring_t signature);
2fe58dfd
SE
283struct rsapubkey_if {
284 void *st;
285 rsa_checksig_fn *check;
286};
287
288/* RSAPRIVKEY interface */
289
290typedef string_t rsa_makesig_fn(void *st, uint8_t *data, uint32_t datalen);
291struct rsaprivkey_if {
292 void *st;
293 rsa_makesig_fn *sign;
294};
295
296/* COMM interface */
297
298/* Return True if the packet was processed, and shouldn't be passed to
299 any other potential receivers. */
300typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
301 struct sockaddr_in *source);
302typedef void comm_request_notify_fn(void *commst, void *nst,
303 comm_notify_fn *fn);
304typedef void comm_release_notify_fn(void *commst, void *nst,
305 comm_notify_fn *fn);
306typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
307 struct sockaddr_in *dest);
308struct comm_if {
309 void *st;
ff05a229
SE
310 uint32_t min_start_pad;
311 uint32_t min_end_pad;
2fe58dfd
SE
312 comm_request_notify_fn *request_notify;
313 comm_release_notify_fn *release_notify;
314 comm_sendmsg_fn *sendmsg;
315};
316
317/* LOG interface */
318
fe5e9cc4
SE
319typedef void log_msg_fn(void *st, int class, const char *message, ...);
320typedef void log_vmsg_fn(void *st, int class, const char *message,
321 va_list args);
2fe58dfd
SE
322struct log_if {
323 void *st;
324 log_msg_fn *log;
325 log_vmsg_fn *vlog;
326};
327/* (convenience function, defined in util.c) */
040ee979 328extern void slilog(struct log_if *lf, int class, const char *message, ...)
4f5e39ec 329FORMAT(printf,3,4);
2fe58dfd
SE
330
331/* SITE interface */
332
333/* Pretty much a placeholder; allows starting and stopping of processing,
334 key expiry, etc. */
335typedef void site_control_fn(void *st, bool_t run);
336typedef uint32_t site_status_fn(void *st);
337struct site_if {
338 void *st;
339 site_control_fn *control;
340 site_status_fn *status;
341};
342
343/* TRANSFORM interface */
344
345/* A reversable transformation. Transforms buffer in-place; may add
346 data to start or end. Maximum amount of data to be added specified
347 in max_start_pad and max_end_pad. (Reverse transformations decrease
348 length, of course.) Transformations may be key-dependent, in which
349 case key material is passed in at initialisation time. They may
350 also depend on internal factors (eg. time) and keep internal
351 state. A struct transform_if only represents a particular type of
352 transformation; instances of the transformation (eg. with
353 particular key material) have a different C type. */
354
355typedef struct transform_inst_if *transform_createinstance_fn(void *st);
356typedef bool_t transform_setkey_fn(void *st, uint8_t *key, uint32_t keylen);
357typedef void transform_delkey_fn(void *st);
358typedef void transform_destroyinstance_fn(void *st);
359/* Returns 0 for 'all is well', any other value for a problem */
360typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf,
fe5e9cc4 361 const char **errmsg);
2fe58dfd
SE
362
363struct transform_inst_if {
364 void *st;
365 transform_setkey_fn *setkey;
366 transform_delkey_fn *delkey;
367 transform_apply_fn *forwards;
368 transform_apply_fn *reverse;
369 transform_destroyinstance_fn *destroy;
370};
371
372struct transform_if {
373 void *st;
374 uint32_t max_start_pad;
375 uint32_t max_end_pad;
376 uint32_t keylen;
377 transform_createinstance_fn *create;
378};
379
380/* NETLINK interface */
381
70dc107b
SE
382/* Used by netlink to deliver to site, and by site to deliver to
383 netlink. cid is the client identifier returned by
384 netlink_regnets_fn. If buf has size 0 then the function is just
385 being called for its site-effects (eg. making the site code attempt
386 to bring up a network link) */
469fd1d9 387typedef void netlink_deliver_fn(void *st, struct buffer_if *buf);
4efd681a 388/* site code can tell netlink when outgoing packets will be dropped,
70dc107b
SE
389 so netlink can generate appropriate ICMP and make routing decisions */
390#define LINK_QUALITY_DOWN 0 /* No chance of a packet being delivered */
391#define LINK_QUALITY_DOWN_STALE_ADDRESS 1 /* Link down, old address information */
392#define LINK_QUALITY_DOWN_CURRENT_ADDRESS 2 /* Link down, current address information */
393#define LINK_QUALITY_UP 3 /* Link active */
394#define MAXIMUM_LINK_QUALITY 3
469fd1d9
SE
395typedef void netlink_link_quality_fn(void *st, uint32_t quality);
396typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver,
397 void *dst, uint32_t max_start_pad,
398 uint32_t max_end_pad);
794f2398
SE
399typedef void netlink_output_config_fn(void *st, struct buffer_if *buf);
400typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf);
d3fe100d 401typedef void netlink_set_mtu_fn(void *st, uint32_t new_mtu);
2fe58dfd
SE
402struct netlink_if {
403 void *st;
469fd1d9 404 netlink_register_fn *reg;
2fe58dfd 405 netlink_deliver_fn *deliver;
70dc107b 406 netlink_link_quality_fn *set_quality;
794f2398
SE
407 netlink_output_config_fn *output_config;
408 netlink_check_config_fn *check_config;
d3fe100d 409 netlink_set_mtu_fn *set_mtu;
2fe58dfd
SE
410};
411
412/* DH interface */
413
414/* Returns public key as a malloced hex string */
415typedef string_t dh_makepublic_fn(void *st, uint8_t *secret,
416 uint32_t secretlen);
417/* Fills buffer (up to buflen) with shared secret */
418typedef void dh_makeshared_fn(void *st, uint8_t *secret,
fe5e9cc4 419 uint32_t secretlen, cstring_t rempublic,
2fe58dfd
SE
420 uint8_t *sharedsecret, uint32_t buflen);
421struct dh_if {
422 void *st;
423 uint32_t len; /* Approximate size of modulus in bytes */
424 dh_makepublic_fn *makepublic;
425 dh_makeshared_fn *makeshared;
426};
427
428/* HASH interface */
429
430typedef void *hash_init_fn(void);
431typedef void hash_update_fn(void *st, uint8_t const *buf, uint32_t len);
432typedef void hash_final_fn(void *st, uint8_t *digest);
433struct hash_if {
434 uint32_t len; /* Hash output length in bytes */
435 hash_init_fn *init;
436 hash_update_fn *update;
437 hash_final_fn *final;
438};
439
440/* BUFFER interface */
441
442struct buffer_if {
443 bool_t free;
fe5e9cc4 444 cstring_t owner; /* Set to constant string */
2fe58dfd
SE
445 uint32_t flags; /* How paranoid should we be? */
446 struct cloc loc; /* Where we were defined */
447 uint8_t *base;
448 uint8_t *start;
449 uint32_t size; /* Size of buffer contents */
450 uint32_t len; /* Total length allocated at base */
451};
452
4f5e39ec
SE
453/***** LOG functions *****/
454
455#define M_DEBUG_CONFIG 0x001
456#define M_DEBUG_PHASE 0x002
457#define M_DEBUG 0x004
458#define M_INFO 0x008
459#define M_NOTICE 0x010
460#define M_WARNING 0x020
461#define M_ERR 0x040
462#define M_SECURITY 0x080
463#define M_FATAL 0x100
464
465/* The fatal() family of functions require messages that do not end in '\n' */
fe5e9cc4
SE
466extern NORETURN(fatal(const char *message, ...));
467extern NORETURN(fatal_perror(const char *message, ...));
468extern NORETURN(fatal_status(int status, const char *message, ...));
469extern NORETURN(fatal_perror_status(int status, const char *message, ...));
4f5e39ec
SE
470
471/* The cfgfatal() family of functions require messages that end in '\n' */
fe5e9cc4
SE
472extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,
473 const char *message, ...));
4f5e39ec
SE
474extern void cfgfile_postreadcheck(struct cloc loc, FILE *f);
475extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 476 cstring_t facility, const char *message,
4f5e39ec
SE
477 va_list));
478extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4
SE
479 cstring_t facility,
480 const char *message, ...));
4f5e39ec 481
fe5e9cc4
SE
482extern void Message(uint32_t class, const char *message, ...)
483 FORMAT(printf,2,3);
484extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
4f5e39ec
SE
485
486/***** END of log functions *****/
487
2fe58dfd 488#endif /* secnet_h */