1 /* Core interface of secnet, to be used by all modules */
11 #include <netinet/in.h>
15 typedef char *string_t
;
16 typedef enum {False
,True
} bool_t
;
18 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line " __LINE__ \
19 " file " __FILE__ "\n"); } while(0)
21 /***** SHARED types *****/
23 /* These are stored in HOST byte order */
34 /* Match an address (in HOST byte order) with a subnet list.
35 Returns True if matched. */
36 extern bool_t
subnet_match(struct subnet_list
*list
, uint32_t address
);
38 /***** END of shared types *****/
40 /***** CONFIGURATION support *****/
42 typedef struct dict dict_t
; /* Configuration dictionary */
43 typedef struct closure closure_t
;
44 typedef struct item item_t
;
45 typedef struct list list_t
; /* A list of items */
47 /* Configuration file location, for error-reporting */
53 /* Modules export closures, which can be invoked from the configuration file.
54 "Invoking" a closure usually returns another closure (of a different
55 type), but can actually return any configuration object. */
56 typedef list_t
*(apply_fn
)(closure_t
*self
, struct cloc loc
,
57 dict_t
*context
, list_t
*data
);
59 string_t description
; /* For debugging */
60 uint32_t type
; /* Central registry... */
62 void *interface
; /* Interface for use inside secnet; depends on type */
65 enum types
{ t_null
, t_bool
, t_string
, t_number
, t_dict
, t_closure
};
83 /* In the following two lookup functions, NULL means 'not found' */
84 /* Lookup a value in the specified dictionary, or its parents */
85 extern list_t
*dict_lookup(dict_t
*dict
, string_t key
);
86 /* Lookup a value in just the specified dictionary */
87 extern list_t
*dict_lookup_primitive(dict_t
*dict
, string_t key
);
88 /* Add a value to the specified dictionary */
89 extern void dict_add(dict_t
*dict
, string_t key
, list_t
*val
);
90 /* Obtain an array of keys in the dictionary. malloced; caller frees */
91 extern string_t
*dict_keys(dict_t
*dict
);
93 /* List-manipulation functions */
94 extern list_t
*list_new(void);
95 extern list_t
*list_append(list_t
*a
, item_t
*i
);
96 extern list_t
*list_append_list(list_t
*a
, list_t
*b
);
97 /* Returns an item from the list (index starts at 0), or NULL */
98 extern item_t
*list_elem(list_t
*l
, uint32_t index
);
100 /* Convenience functions */
101 extern list_t
*new_closure(closure_t
*cl
);
102 extern void add_closure(dict_t
*dict
, string_t name
, apply_fn apply
);
103 extern void *find_cl_if(dict_t
*dict
, string_t name
, uint32_t type
,
104 bool_t fail_if_invalid
, string_t desc
,
106 extern item_t
*dict_find_item(dict_t
*dict
, string_t key
, bool_t required
,
107 string_t desc
, struct cloc loc
);
108 extern string_t
dict_read_string(dict_t
*dict
, string_t key
, bool_t required
,
109 string_t desc
, struct cloc loc
);
110 extern uint32_t dict_read_number(dict_t
*dict
, string_t key
, bool_t required
,
111 string_t desc
, struct cloc loc
, uint32_t def
);
112 extern bool_t
dict_read_bool(dict_t
*dict
, string_t key
, bool_t required
,
113 string_t desc
, struct cloc loc
, bool_t def
);
114 extern void dict_read_subnet_list(dict_t
*dict
, string_t key
, bool_t required
,
115 string_t desc
, struct cloc loc
,
116 struct subnet_list
*sl
);
117 extern uint32_t string_to_ipaddr(item_t
*i
, string_t desc
);
119 /***** END of configuration support *****/
121 /***** UTILITY functions *****/
127 #define M_DEBUG_CONFIG 16
128 #define M_DEBUG_PHASE 32
130 extern void fatal(char *message
, ...);
131 extern void fatal_perror(char *message
, ...);
132 extern void fatal_status(int status
, char *message
, ...);
133 extern void fatal_perror_status(int status
, char *message
, ...);
134 extern void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...);
136 extern char *safe_strdup(char *string
, char *message
);
137 extern void *safe_malloc(size_t size
, char *message
);
139 extern void Message(uint32_t class, char *message
, ...);
141 extern string_t
ipaddr_to_string(uint32_t addr
);
142 extern string_t
subnet_to_string(struct subnet
*sn
);
144 /***** END of utility functions *****/
146 /***** SCHEDULING support */
148 /* "now" is current program time, in milliseconds. It is derived
149 (once) from tv_now. If nfds_io is insufficient for your needs, set
150 it to the required number and return ERANGE. timeout is in milliseconds;
151 if it is too high then lower it. It starts at -1 (==infinite) */
152 typedef int beforepoll_fn(void *st
, struct pollfd
*fds
, int *nfds_io
,
153 int *timeout_io
, const struct timeval
*tv_now
,
155 typedef void afterpoll_fn(void *st
, struct pollfd
*fds
, int nfds
,
156 const struct timeval
*tv_now
, uint64_t *now
);
158 /* Register interest in the main loop of the program. Before a call
159 to poll() your supplied beforepoll function will be called. After
160 the call to poll() the supplied afterpoll function will be called.
161 max_nfds is a _hint_ about the maximum number of struct pollfd
162 structures you may require - you can always ask for more in
164 extern void register_for_poll(void *st
, beforepoll_fn
*before
,
165 afterpoll_fn
*after
, uint32_t max_nfds
,
168 /***** END of scheduling support */
170 /***** PROGRAM LIFETIME support */
172 /* The secnet program goes through a number of phases in its lifetime.
173 Module code may arrange to be called just as various phases are
177 #define PHASE_GETOPTS 1 /* Process command-line arguments */
178 #define PHASE_READCONFIG 2 /* Parse and process configuration file */
179 #define PHASE_SETUP 3 /* Process information in configuration */
180 #define PHASE_DROPPRIV 4 /* Last chance for privileged operations */
182 #define PHASE_SHUTDOWN 6 /* About to die; delete key material, etc. */
185 typedef void hook_fn(void *self
, uint32_t newphase
);
186 bool_t
add_hook(uint32_t phase
, hook_fn
*f
, void *state
);
187 bool_t
remove_hook(uint32_t phase
, hook_fn
*f
, void *state
);
189 /***** END of program lifetime support *****/
191 /***** MODULE support *****/
193 /* Module initialisation function type - modules export one function of
194 this type which is called to initialise them. For dynamically loaded
195 modules it's called "secnet_module". */
196 typedef void (init_module
)(dict_t
*dict
);
198 /***** END of module support *****/
200 /***** CLOSURE TYPES and interface definitions *****/
203 #define CL_RESOLVER 1
204 #define CL_RANDOMSRC 2
205 #define CL_RSAPUBKEY 3
206 #define CL_RSAPRIVKEY 4
211 #define CL_TRANSFORM 9
212 #define CL_NETLINK 10
219 /* PURE closure requires no interface */
221 /* RESOLVER interface */
223 /* Answers to queries are delivered to a function of this
224 type. 'address' will be NULL if there was a problem with the query. It
225 will be freed once resolve_answer_fn returns. It is in network byte
227 typedef void resolve_answer_fn(void *st
, struct in_addr
*addr
);
228 typedef bool_t
resolve_request_fn(void *st
, string_t name
,
229 resolve_answer_fn
*cb
, void *cst
);
232 resolve_request_fn
*request
;
235 /* RANDOMSRC interface */
237 /* Return some random data. Returns TRUE for success. */
238 typedef bool_t
random_fn(void *st
, uint32_t bytes
, uint8_t *buff
);
246 /* RSAPUBKEY interface */
248 typedef bool_t
rsa_checksig_fn(void *st
, uint8_t *data
, uint32_t datalen
,
250 struct rsapubkey_if
{
252 rsa_checksig_fn
*check
;
255 /* RSAPRIVKEY interface */
257 typedef string_t
rsa_makesig_fn(void *st
, uint8_t *data
, uint32_t datalen
);
258 struct rsaprivkey_if
{
260 rsa_makesig_fn
*sign
;
265 /* Return True if the packet was processed, and shouldn't be passed to
266 any other potential receivers. */
267 typedef bool_t
comm_notify_fn(void *state
, struct buffer_if
*buf
,
268 struct sockaddr_in
*source
);
269 typedef void comm_request_notify_fn(void *commst
, void *nst
,
271 typedef void comm_release_notify_fn(void *commst
, void *nst
,
273 typedef bool_t
comm_sendmsg_fn(void *commst
, struct buffer_if
*buf
,
274 struct sockaddr_in
*dest
);
277 comm_request_notify_fn
*request_notify
;
278 comm_release_notify_fn
*release_notify
;
279 comm_sendmsg_fn
*sendmsg
;
284 typedef void log_msg_fn(void *st
, int priority
, char *message
, ...);
285 typedef void log_vmsg_fn(void *st
, int priority
, char *message
, va_list args
);
291 /* (convenience function, defined in util.c) */
292 extern void log(struct log_if
*lf
, int priority
, char *message
, ...);
296 /* Pretty much a placeholder; allows starting and stopping of processing,
298 typedef void site_control_fn(void *st
, bool_t run
);
299 typedef uint32_t site_status_fn(void *st
);
302 site_control_fn
*control
;
303 site_status_fn
*status
;
306 /* TRANSFORM interface */
308 /* A reversable transformation. Transforms buffer in-place; may add
309 data to start or end. Maximum amount of data to be added specified
310 in max_start_pad and max_end_pad. (Reverse transformations decrease
311 length, of course.) Transformations may be key-dependent, in which
312 case key material is passed in at initialisation time. They may
313 also depend on internal factors (eg. time) and keep internal
314 state. A struct transform_if only represents a particular type of
315 transformation; instances of the transformation (eg. with
316 particular key material) have a different C type. */
318 typedef struct transform_inst_if
*transform_createinstance_fn(void *st
);
319 typedef bool_t
transform_setkey_fn(void *st
, uint8_t *key
, uint32_t keylen
);
320 typedef void transform_delkey_fn(void *st
);
321 typedef void transform_destroyinstance_fn(void *st
);
322 /* Returns 0 for 'all is well', any other value for a problem */
323 typedef uint32_t transform_apply_fn(void *st
, struct buffer_if
*buf
,
326 struct transform_inst_if
{
328 transform_setkey_fn
*setkey
;
329 transform_delkey_fn
*delkey
;
330 transform_apply_fn
*forwards
;
331 transform_apply_fn
*reverse
;
332 transform_destroyinstance_fn
*destroy
;
335 struct transform_if
{
337 uint32_t max_start_pad
;
338 uint32_t max_end_pad
;
340 transform_createinstance_fn
*create
;
343 /* NETLINK interface */
345 /* Used by netlink to deliver to site, and by site to deliver to netlink.
346 cid is the client identifier returned by netlink_regnets_fn */
347 typedef void netlink_deliver_fn(void *st
, void *cid
, struct buffer_if
*buf
);
348 /* Register for packets from specified networks. Return value is client
350 typedef void *netlink_regnets_fn(void *st
, struct subnet_list
*networks
,
351 netlink_deliver_fn
*deliver
, void *dst
,
352 uint32_t max_start_pad
, uint32_t max_end_pad
);
356 netlink_regnets_fn
*regnets
;
357 netlink_deliver_fn
*deliver
;
362 /* Returns public key as a malloced hex string */
363 typedef string_t
dh_makepublic_fn(void *st
, uint8_t *secret
,
365 /* Fills buffer (up to buflen) with shared secret */
366 typedef void dh_makeshared_fn(void *st
, uint8_t *secret
,
367 uint32_t secretlen
, string_t rempublic
,
368 uint8_t *sharedsecret
, uint32_t buflen
);
371 uint32_t len
; /* Approximate size of modulus in bytes */
372 dh_makepublic_fn
*makepublic
;
373 dh_makeshared_fn
*makeshared
;
378 typedef void *hash_init_fn(void);
379 typedef void hash_update_fn(void *st
, uint8_t const *buf
, uint32_t len
);
380 typedef void hash_final_fn(void *st
, uint8_t *digest
);
382 uint32_t len
; /* Hash output length in bytes */
384 hash_update_fn
*update
;
385 hash_final_fn
*final
;
388 /* BUFFER interface */
392 string_t owner
; /* Set to constant string */
393 uint32_t flags
; /* How paranoid should we be? */
394 struct cloc loc
; /* Where we were defined */
397 uint32_t size
; /* Size of buffer contents */
398 uint32_t len
; /* Total length allocated at base */
401 #endif /* secnet_h */