Import release 0.09
[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
SE
8#include <stdarg.h>
9#include <syslog.h>
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
2fe58dfd
SE
15typedef char *string_t;
16typedef enum {False,True} bool_t;
17
18#define ASSERT(x) do { if (!(x)) { fatal("assertion failed line " __LINE__ \
19 " file " __FILE__ "\n"); } while(0)
20
21/***** SHARED types *****/
22
23/* These are stored in HOST byte order */
24struct subnet {
25 uint32_t prefix;
26 uint32_t mask;
27};
28
29struct subnet_list {
30 uint32_t entries;
31 struct subnet *list;
32};
33
34/* Match an address (in HOST byte order) with a subnet list.
35 Returns True if matched. */
36extern bool_t subnet_match(struct subnet_list *list, uint32_t address);
37
38/***** END of shared types *****/
39
40/***** CONFIGURATION support *****/
41
42typedef struct dict dict_t; /* Configuration dictionary */
43typedef struct closure closure_t;
44typedef struct item item_t;
45typedef struct list list_t; /* A list of items */
46
47/* Configuration file location, for error-reporting */
48struct cloc {
49 string_t file;
50 uint32_t line;
51};
52
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. */
56typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
57 dict_t *context, list_t *data);
58struct closure {
59 string_t description; /* For debugging */
60 uint32_t type; /* Central registry... */
61 apply_fn *apply;
62 void *interface; /* Interface for use inside secnet; depends on type */
63};
64
65enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
66struct item {
67 enum types type;
68 union {
69 bool_t bool;
70 string_t string;
71 uint32_t number;
72 dict_t *dict;
73 closure_t *closure;
74 } data;
75 struct cloc loc;
76};
77
78struct list {
79 item_t *item;
80 struct list *next;
81};
82
83/* In the following two lookup functions, NULL means 'not found' */
84/* Lookup a value in the specified dictionary, or its parents */
85extern list_t *dict_lookup(dict_t *dict, string_t key);
86/* Lookup a value in just the specified dictionary */
87extern list_t *dict_lookup_primitive(dict_t *dict, string_t key);
88/* Add a value to the specified dictionary */
89extern void dict_add(dict_t *dict, string_t key, list_t *val);
90/* Obtain an array of keys in the dictionary. malloced; caller frees */
91extern string_t *dict_keys(dict_t *dict);
92
93/* List-manipulation functions */
94extern list_t *list_new(void);
95extern list_t *list_append(list_t *a, item_t *i);
96extern list_t *list_append_list(list_t *a, list_t *b);
97/* Returns an item from the list (index starts at 0), or NULL */
98extern item_t *list_elem(list_t *l, uint32_t index);
99
100/* Convenience functions */
101extern list_t *new_closure(closure_t *cl);
102extern void add_closure(dict_t *dict, string_t name, apply_fn apply);
103extern void *find_cl_if(dict_t *dict, string_t name, uint32_t type,
104 bool_t fail_if_invalid, string_t desc,
105 struct cloc loc);
106extern item_t *dict_find_item(dict_t *dict, string_t key, bool_t required,
107 string_t desc, struct cloc loc);
108extern string_t dict_read_string(dict_t *dict, string_t key, bool_t required,
109 string_t desc, struct cloc loc);
110extern uint32_t dict_read_number(dict_t *dict, string_t key, bool_t required,
111 string_t desc, struct cloc loc, uint32_t def);
112extern bool_t dict_read_bool(dict_t *dict, string_t key, bool_t required,
113 string_t desc, struct cloc loc, bool_t def);
114extern 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);
117extern uint32_t string_to_ipaddr(item_t *i, string_t desc);
118
119/***** END of configuration support *****/
120
121/***** UTILITY functions *****/
122
123#define M_WARNING 1
124#define M_ERROR 2
125#define M_FATAL 4
126#define M_INFO 8
127#define M_DEBUG_CONFIG 16
128#define M_DEBUG_PHASE 32
129
130extern void fatal(char *message, ...);
131extern void fatal_perror(char *message, ...);
132extern void fatal_status(int status, char *message, ...);
133extern void fatal_perror_status(int status, char *message, ...);
134extern void cfgfatal(struct cloc loc, string_t facility, char *message, ...);
135
136extern char *safe_strdup(char *string, char *message);
137extern void *safe_malloc(size_t size, char *message);
138
139extern void Message(uint32_t class, char *message, ...);
140
141extern string_t ipaddr_to_string(uint32_t addr);
142extern string_t subnet_to_string(struct subnet *sn);
143
4efd681a
SE
144extern int sys_cmd(const char *file, char *argc, ...);
145
2fe58dfd
SE
146/***** END of utility functions *****/
147
148/***** SCHEDULING support */
149
150/* "now" is current program time, in milliseconds. It is derived
151 (once) from tv_now. If nfds_io is insufficient for your needs, set
152 it to the required number and return ERANGE. timeout is in milliseconds;
153 if it is too high then lower it. It starts at -1 (==infinite) */
154typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
155 int *timeout_io, const struct timeval *tv_now,
156 uint64_t *now);
157typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds,
158 const struct timeval *tv_now, uint64_t *now);
159
160/* Register interest in the main loop of the program. Before a call
161 to poll() your supplied beforepoll function will be called. After
162 the call to poll() the supplied afterpoll function will be called.
163 max_nfds is a _hint_ about the maximum number of struct pollfd
164 structures you may require - you can always ask for more in
165 *nfds_io. */
166extern void register_for_poll(void *st, beforepoll_fn *before,
167 afterpoll_fn *after, uint32_t max_nfds,
168 string_t desc);
169
170/***** END of scheduling support */
171
172/***** PROGRAM LIFETIME support */
173
174/* The secnet program goes through a number of phases in its lifetime.
175 Module code may arrange to be called just as various phases are
176 entered. */
177
178#define PHASE_INIT 0
179#define PHASE_GETOPTS 1 /* Process command-line arguments */
180#define PHASE_READCONFIG 2 /* Parse and process configuration file */
181#define PHASE_SETUP 3 /* Process information in configuration */
182#define PHASE_DROPPRIV 4 /* Last chance for privileged operations */
183#define PHASE_RUN 5
184#define PHASE_SHUTDOWN 6 /* About to die; delete key material, etc. */
185#define NR_PHASES 7
186
187typedef void hook_fn(void *self, uint32_t newphase);
188bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
189bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
190
191/***** END of program lifetime support *****/
192
193/***** MODULE support *****/
194
195/* Module initialisation function type - modules export one function of
196 this type which is called to initialise them. For dynamically loaded
197 modules it's called "secnet_module". */
198typedef void (init_module)(dict_t *dict);
199
200/***** END of module support *****/
201
202/***** CLOSURE TYPES and interface definitions *****/
203
204#define CL_PURE 0
205#define CL_RESOLVER 1
206#define CL_RANDOMSRC 2
207#define CL_RSAPUBKEY 3
208#define CL_RSAPRIVKEY 4
209#define CL_COMM 5
210#define CL_IPIF 6
211#define CL_LOG 7
212#define CL_SITE 8
213#define CL_TRANSFORM 9
214#define CL_NETLINK 10
215#define CL_DH 11
216#define CL_HASH 12
217#define CL_BUFFER 13
218
219struct buffer_if;
220
221/* PURE closure requires no interface */
222
223/* RESOLVER interface */
224
225/* Answers to queries are delivered to a function of this
226 type. 'address' will be NULL if there was a problem with the query. It
227 will be freed once resolve_answer_fn returns. It is in network byte
228 order. */
229typedef void resolve_answer_fn(void *st, struct in_addr *addr);
230typedef bool_t resolve_request_fn(void *st, string_t name,
231 resolve_answer_fn *cb, void *cst);
232struct resolver_if {
233 void *st;
234 resolve_request_fn *request;
235};
236
237/* RANDOMSRC interface */
238
239/* Return some random data. Returns TRUE for success. */
240typedef bool_t random_fn(void *st, uint32_t bytes, uint8_t *buff);
241
242struct random_if {
243 void *st;
244 bool_t blocking;
245 random_fn *generate;
246};
247
248/* RSAPUBKEY interface */
249
250typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, uint32_t datalen,
251 string_t signature);
252struct rsapubkey_if {
253 void *st;
254 rsa_checksig_fn *check;
255};
256
257/* RSAPRIVKEY interface */
258
259typedef string_t rsa_makesig_fn(void *st, uint8_t *data, uint32_t datalen);
260struct rsaprivkey_if {
261 void *st;
262 rsa_makesig_fn *sign;
263};
264
265/* COMM interface */
266
267/* Return True if the packet was processed, and shouldn't be passed to
268 any other potential receivers. */
269typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
270 struct sockaddr_in *source);
271typedef void comm_request_notify_fn(void *commst, void *nst,
272 comm_notify_fn *fn);
273typedef void comm_release_notify_fn(void *commst, void *nst,
274 comm_notify_fn *fn);
275typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
276 struct sockaddr_in *dest);
277struct comm_if {
278 void *st;
279 comm_request_notify_fn *request_notify;
280 comm_release_notify_fn *release_notify;
281 comm_sendmsg_fn *sendmsg;
282};
283
284/* LOG interface */
285
286typedef void log_msg_fn(void *st, int priority, char *message, ...);
287typedef void log_vmsg_fn(void *st, int priority, char *message, va_list args);
288struct log_if {
289 void *st;
290 log_msg_fn *log;
291 log_vmsg_fn *vlog;
292};
293/* (convenience function, defined in util.c) */
294extern void log(struct log_if *lf, int priority, char *message, ...);
295
296/* SITE interface */
297
298/* Pretty much a placeholder; allows starting and stopping of processing,
299 key expiry, etc. */
300typedef void site_control_fn(void *st, bool_t run);
301typedef uint32_t site_status_fn(void *st);
302struct site_if {
303 void *st;
304 site_control_fn *control;
305 site_status_fn *status;
306};
307
308/* TRANSFORM interface */
309
310/* A reversable transformation. Transforms buffer in-place; may add
311 data to start or end. Maximum amount of data to be added specified
312 in max_start_pad and max_end_pad. (Reverse transformations decrease
313 length, of course.) Transformations may be key-dependent, in which
314 case key material is passed in at initialisation time. They may
315 also depend on internal factors (eg. time) and keep internal
316 state. A struct transform_if only represents a particular type of
317 transformation; instances of the transformation (eg. with
318 particular key material) have a different C type. */
319
320typedef struct transform_inst_if *transform_createinstance_fn(void *st);
321typedef bool_t transform_setkey_fn(void *st, uint8_t *key, uint32_t keylen);
322typedef void transform_delkey_fn(void *st);
323typedef void transform_destroyinstance_fn(void *st);
324/* Returns 0 for 'all is well', any other value for a problem */
325typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf,
326 char **errmsg);
327
328struct transform_inst_if {
329 void *st;
330 transform_setkey_fn *setkey;
331 transform_delkey_fn *delkey;
332 transform_apply_fn *forwards;
333 transform_apply_fn *reverse;
334 transform_destroyinstance_fn *destroy;
335};
336
337struct transform_if {
338 void *st;
339 uint32_t max_start_pad;
340 uint32_t max_end_pad;
341 uint32_t keylen;
342 transform_createinstance_fn *create;
343};
344
345/* NETLINK interface */
346
347/* Used by netlink to deliver to site, and by site to deliver to netlink.
348 cid is the client identifier returned by netlink_regnets_fn */
349typedef void netlink_deliver_fn(void *st, void *cid, struct buffer_if *buf);
4efd681a
SE
350/* site code can tell netlink when outgoing packets will be dropped,
351 so netlink can generate appropriate ICMP */
352typedef void netlink_can_deliver_fn(void *st, void *cid, bool_t can_deliver);
2fe58dfd
SE
353/* Register for packets from specified networks. Return value is client
354 identifier. */
355typedef void *netlink_regnets_fn(void *st, struct subnet_list *networks,
356 netlink_deliver_fn *deliver, void *dst,
4efd681a
SE
357 uint32_t max_start_pad, uint32_t max_end_pad,
358 string_t client_name);
2fe58dfd
SE
359
360struct netlink_if {
361 void *st;
362 netlink_regnets_fn *regnets;
363 netlink_deliver_fn *deliver;
4efd681a 364 netlink_can_deliver_fn *set_delivery;
2fe58dfd
SE
365};
366
367/* DH interface */
368
369/* Returns public key as a malloced hex string */
370typedef string_t dh_makepublic_fn(void *st, uint8_t *secret,
371 uint32_t secretlen);
372/* Fills buffer (up to buflen) with shared secret */
373typedef void dh_makeshared_fn(void *st, uint8_t *secret,
374 uint32_t secretlen, string_t rempublic,
375 uint8_t *sharedsecret, uint32_t buflen);
376struct dh_if {
377 void *st;
378 uint32_t len; /* Approximate size of modulus in bytes */
379 dh_makepublic_fn *makepublic;
380 dh_makeshared_fn *makeshared;
381};
382
383/* HASH interface */
384
385typedef void *hash_init_fn(void);
386typedef void hash_update_fn(void *st, uint8_t const *buf, uint32_t len);
387typedef void hash_final_fn(void *st, uint8_t *digest);
388struct hash_if {
389 uint32_t len; /* Hash output length in bytes */
390 hash_init_fn *init;
391 hash_update_fn *update;
392 hash_final_fn *final;
393};
394
395/* BUFFER interface */
396
397struct buffer_if {
398 bool_t free;
399 string_t owner; /* Set to constant string */
400 uint32_t flags; /* How paranoid should we be? */
401 struct cloc loc; /* Where we were defined */
402 uint8_t *base;
403 uint8_t *start;
404 uint32_t size; /* Size of buffer contents */
405 uint32_t len; /* Total length allocated at base */
406};
407
408#endif /* secnet_h */