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