Commit | Line | Data |
---|---|---|
e82f7154 | 1 | /* -*-c-*- |
2 | * | |
e82f7154 | 3 | * Main header file for port forwarder |
4 | * | |
61e3dbdf | 5 | * (c) 1999 Straylight/Edgeware |
e82f7154 | 6 | */ |
7 | ||
206212ca | 8 | /*----- Licensing notice --------------------------------------------------* |
e82f7154 | 9 | * |
9155ea97 | 10 | * This file is part of the `fwd' port forwarder. |
e82f7154 | 11 | * |
9155ea97 | 12 | * `fwd' is free software; you can redistribute it and/or modify |
e82f7154 | 13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
206212ca | 16 | * |
9155ea97 | 17 | * `fwd' is distributed in the hope that it will be useful, |
e82f7154 | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
206212ca | 21 | * |
e82f7154 | 22 | * You should have received a copy of the GNU General Public License |
9155ea97 | 23 | * along with `fwd'; if not, write to the Free Software Foundation, |
e82f7154 | 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ | |
26 | ||
e82f7154 | 27 | #ifndef FW_H |
28 | #define FW_H | |
29 | ||
30 | #ifdef __cplusplus | |
31 | extern "C" { | |
32 | #endif | |
33 | ||
34 | /*----- Header files ------------------------------------------------------*/ | |
35 | ||
47a7631f MW |
36 | /* --- Configuration --- */ |
37 | ||
38 | #include "config.h" | |
39 | #define _GNU_SOURCE | |
40 | ||
41 | /* --- ANSI C --- */ | |
42 | ||
43 | #include <assert.h> | |
44 | #include <ctype.h> | |
45 | #include <errno.h> | |
46 | #include <float.h> | |
47 | #include <limits.h> | |
48 | #include <math.h> | |
49 | #include <signal.h> | |
50 | #include <stdarg.h> | |
51 | #include <stddef.h> | |
52 | #include <stdio.h> | |
53 | #include <stdlib.h> | |
54 | #include <string.h> | |
61e3dbdf | 55 | #include <time.h> |
47a7631f MW |
56 | |
57 | /* --- Unix --- */ | |
58 | ||
61e3dbdf | 59 | #include <fcntl.h> |
47a7631f MW |
60 | #include <unistd.h> |
61 | ||
62 | #include <sys/types.h> | |
63 | #include <sys/stat.h> | |
64 | #include <sys/time.h> | |
65 | #include <sys/uio.h> | |
66 | #include <sys/wait.h> | |
67 | ||
68 | #include <sys/socket.h> | |
69 | #include <sys/un.h> | |
70 | #include <netinet/in.h> | |
71 | #include <arpa/inet.h> | |
72 | #include <netdb.h> | |
73 | ||
74 | #include <pwd.h> | |
75 | #include <grp.h> | |
76 | ||
77 | #include <syslog.h> | |
78 | ||
79 | /* --- mLib --- */ | |
80 | ||
81 | #include <mLib/alloc.h> | |
82 | #include <mLib/bres.h> | |
83 | #include <mLib/conn.h> | |
84 | #include <mLib/darray.h> | |
85 | #include <mLib/dstr.h> | |
86 | #include <mLib/env.h> | |
87 | #include <mLib/fdflags.h> | |
88 | #include <mLib/fdpass.h> | |
89 | #include <mLib/ident.h> | |
76654703 | 90 | #include <mLib/mdup.h> |
47a7631f MW |
91 | #include <mLib/mdwopt.h> |
92 | #include <mLib/quis.h> | |
93 | #include <mLib/report.h> | |
e82f7154 | 94 | #include <mLib/sel.h> |
47a7631f MW |
95 | #include <mLib/selbuf.h> |
96 | #include <mLib/sig.h> | |
97 | #include <mLib/str.h> | |
98 | #include <mLib/sub.h> | |
99 | #include <mLib/sym.h> | |
100 | #include <mLib/tv.h> | |
101 | ||
102 | /*----- Other subtleties --------------------------------------------------*/ | |
103 | ||
104 | #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON | |
105 | extern char **environ; | |
106 | #endif | |
107 | ||
1cd4a3bf MW |
108 | /*----- Resource limit names ----------------------------------------------*/ |
109 | ||
110 | #if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE) | |
111 | # define RLIMIT_NOFILE RLIMIT_OFILE | |
112 | #endif | |
113 | ||
114 | /* | |
115 | ;;; The resource-limit name table is very boring to type and less fun to | |
116 | ;;; maintain. To make life less awful, put the names in this list and | |
117 | ;;; evaluate the code to get Emacs to regenerate it. | |
118 | ||
119 | (let ((limits '(as core cpu data fsize locks memlock msgqueue | |
120 | nice nofile nproc rss rtprio sigpending stack | |
121 | vmem))) | |
122 | (save-excursion | |
123 | (goto-char | |
124 | (point-min)) | |
125 | (search-forward (concat "***" "BEGIN rlimitlist" "***")) | |
126 | (beginning-of-line 2) | |
127 | (delete-region (point) | |
128 | (progn | |
129 | (search-forward "***END***") | |
130 | (beginning-of-line) | |
131 | (point))) | |
132 | (let ((avail (make-marker)) | |
133 | (list (make-marker))) | |
134 | (set-marker avail (point)) | |
135 | (insert "#define RLIMITS(_)") | |
136 | (set-marker list (point)) | |
137 | (dolist (limit (sort (copy-list limits) #'string<)) | |
138 | (let* ((name (symbol-name limit)) | |
139 | (constant (concat "RLIMIT_" (upcase name))) | |
140 | (have (concat "HAVE_" constant "_P"))) | |
141 | (goto-char avail) | |
142 | (insert-before-markers (format (concat "#ifdef %s\n" | |
143 | "# define %s t\n" | |
144 | "#else\n" | |
145 | "# define %s nil\n" | |
146 | "#endif\n") | |
147 | constant have have)) | |
148 | (goto-char list) | |
149 | (insert-before-markers | |
150 | (format " \\\n MAYBE_ITEM(_, %s, (%s, %s))" | |
151 | have name constant)))) | |
152 | (goto-char list) | |
153 | (insert "\n")))) | |
154 | */ | |
155 | ||
156 | /***BEGIN rlimitlist***/ | |
157 | #ifdef RLIMIT_AS | |
158 | # define HAVE_RLIMIT_AS_P t | |
159 | #else | |
160 | # define HAVE_RLIMIT_AS_P nil | |
161 | #endif | |
162 | #ifdef RLIMIT_CORE | |
163 | # define HAVE_RLIMIT_CORE_P t | |
164 | #else | |
165 | # define HAVE_RLIMIT_CORE_P nil | |
166 | #endif | |
167 | #ifdef RLIMIT_CPU | |
168 | # define HAVE_RLIMIT_CPU_P t | |
169 | #else | |
170 | # define HAVE_RLIMIT_CPU_P nil | |
171 | #endif | |
172 | #ifdef RLIMIT_DATA | |
173 | # define HAVE_RLIMIT_DATA_P t | |
174 | #else | |
175 | # define HAVE_RLIMIT_DATA_P nil | |
176 | #endif | |
177 | #ifdef RLIMIT_FSIZE | |
178 | # define HAVE_RLIMIT_FSIZE_P t | |
179 | #else | |
180 | # define HAVE_RLIMIT_FSIZE_P nil | |
181 | #endif | |
182 | #ifdef RLIMIT_LOCKS | |
183 | # define HAVE_RLIMIT_LOCKS_P t | |
184 | #else | |
185 | # define HAVE_RLIMIT_LOCKS_P nil | |
186 | #endif | |
187 | #ifdef RLIMIT_MEMLOCK | |
188 | # define HAVE_RLIMIT_MEMLOCK_P t | |
189 | #else | |
190 | # define HAVE_RLIMIT_MEMLOCK_P nil | |
191 | #endif | |
192 | #ifdef RLIMIT_MSGQUEUE | |
193 | # define HAVE_RLIMIT_MSGQUEUE_P t | |
194 | #else | |
195 | # define HAVE_RLIMIT_MSGQUEUE_P nil | |
196 | #endif | |
197 | #ifdef RLIMIT_NICE | |
198 | # define HAVE_RLIMIT_NICE_P t | |
199 | #else | |
200 | # define HAVE_RLIMIT_NICE_P nil | |
201 | #endif | |
202 | #ifdef RLIMIT_NOFILE | |
203 | # define HAVE_RLIMIT_NOFILE_P t | |
204 | #else | |
205 | # define HAVE_RLIMIT_NOFILE_P nil | |
206 | #endif | |
207 | #ifdef RLIMIT_NPROC | |
208 | # define HAVE_RLIMIT_NPROC_P t | |
209 | #else | |
210 | # define HAVE_RLIMIT_NPROC_P nil | |
211 | #endif | |
212 | #ifdef RLIMIT_RSS | |
213 | # define HAVE_RLIMIT_RSS_P t | |
214 | #else | |
215 | # define HAVE_RLIMIT_RSS_P nil | |
216 | #endif | |
217 | #ifdef RLIMIT_RTPRIO | |
218 | # define HAVE_RLIMIT_RTPRIO_P t | |
219 | #else | |
220 | # define HAVE_RLIMIT_RTPRIO_P nil | |
221 | #endif | |
222 | #ifdef RLIMIT_SIGPENDING | |
223 | # define HAVE_RLIMIT_SIGPENDING_P t | |
224 | #else | |
225 | # define HAVE_RLIMIT_SIGPENDING_P nil | |
226 | #endif | |
227 | #ifdef RLIMIT_STACK | |
228 | # define HAVE_RLIMIT_STACK_P t | |
229 | #else | |
230 | # define HAVE_RLIMIT_STACK_P nil | |
231 | #endif | |
232 | #ifdef RLIMIT_VMEM | |
233 | # define HAVE_RLIMIT_VMEM_P t | |
234 | #else | |
235 | # define HAVE_RLIMIT_VMEM_P nil | |
236 | #endif | |
237 | #define RLIMITS(_) \ | |
238 | MAYBE_ITEM(_, HAVE_RLIMIT_AS_P, (as, RLIMIT_AS)) \ | |
239 | MAYBE_ITEM(_, HAVE_RLIMIT_CORE_P, (core, RLIMIT_CORE)) \ | |
240 | MAYBE_ITEM(_, HAVE_RLIMIT_CPU_P, (cpu, RLIMIT_CPU)) \ | |
241 | MAYBE_ITEM(_, HAVE_RLIMIT_DATA_P, (data, RLIMIT_DATA)) \ | |
242 | MAYBE_ITEM(_, HAVE_RLIMIT_FSIZE_P, (fsize, RLIMIT_FSIZE)) \ | |
243 | MAYBE_ITEM(_, HAVE_RLIMIT_LOCKS_P, (locks, RLIMIT_LOCKS)) \ | |
244 | MAYBE_ITEM(_, HAVE_RLIMIT_MEMLOCK_P, (memlock, RLIMIT_MEMLOCK)) \ | |
245 | MAYBE_ITEM(_, HAVE_RLIMIT_MSGQUEUE_P, (msgqueue, RLIMIT_MSGQUEUE)) \ | |
246 | MAYBE_ITEM(_, HAVE_RLIMIT_NICE_P, (nice, RLIMIT_NICE)) \ | |
247 | MAYBE_ITEM(_, HAVE_RLIMIT_NOFILE_P, (nofile, RLIMIT_NOFILE)) \ | |
248 | MAYBE_ITEM(_, HAVE_RLIMIT_NPROC_P, (nproc, RLIMIT_NPROC)) \ | |
249 | MAYBE_ITEM(_, HAVE_RLIMIT_RSS_P, (rss, RLIMIT_RSS)) \ | |
250 | MAYBE_ITEM(_, HAVE_RLIMIT_RTPRIO_P, (rtprio, RLIMIT_RTPRIO)) \ | |
251 | MAYBE_ITEM(_, HAVE_RLIMIT_SIGPENDING_P, (sigpending, RLIMIT_SIGPENDING)) \ | |
252 | MAYBE_ITEM(_, HAVE_RLIMIT_STACK_P, (stack, RLIMIT_STACK)) \ | |
253 | MAYBE_ITEM(_, HAVE_RLIMIT_VMEM_P, (vmem, RLIMIT_VMEM)) | |
254 | /***END***/ | |
255 | ||
256 | /* --- The unpleasant conditional-output machinery --- */ | |
257 | ||
258 | #define MAYBE_ITEM(_, emitp, args) GLUE(MAYBE_ITEM_, emitp)(_, args) | |
259 | #define MAYBE_ITEM_t(_, args) _ args | |
260 | #define MAYBE_ITEM_nil(_, args) | |
261 | ||
47a7631f | 262 | /*----- Main program ------------------------------------------------------*/ |
e82f7154 | 263 | |
47a7631f | 264 | /* --- The global select state --- */ |
e82f7154 | 265 | |
266 | extern sel_state *sel; | |
4fe31371 | 267 | |
47a7631f MW |
268 | /* --- Help text --- */ |
269 | ||
270 | extern const char grammar_text[]; | |
271 | extern const char option_text[]; | |
61e3dbdf | 272 | |
273 | /* --- @fw_log@ --- * | |
274 | * | |
275 | * Arguments: @time_t t@ = when the connection occurred or (@-1@) | |
276 | * @const char *fmt@ = format string to fill in | |
277 | * @...@ = other arguments | |
278 | * | |
279 | * Returns: --- | |
280 | * | |
281 | * Use: Logs a connection. | |
282 | */ | |
283 | ||
284 | extern void fw_log(time_t /*t*/, const char */*fmt*/, ...); | |
285 | ||
286 | /* --- @fw_inc@, @fw_dec@ --- * | |
287 | * | |
288 | * Arguments: --- | |
289 | * | |
290 | * Returns: --- | |
291 | * | |
9155ea97 | 292 | * Use: Increments or decrements the active thing count. `fwd' won't |
61e3dbdf | 293 | * quit while there are active things. |
294 | */ | |
295 | ||
296 | extern void fw_inc(void); | |
297 | extern void fw_dec(void); | |
e82f7154 | 298 | |
47a7631f MW |
299 | /*----- Channel management ------------------------------------------------*/ |
300 | ||
301 | /* --- Magic numbers --- */ | |
302 | ||
303 | #define CHAN_BUFSZ 4096 | |
304 | ||
305 | /* --- Channel structure --- */ | |
306 | ||
307 | typedef struct chan { | |
308 | unsigned base, len; /* Base and length of data */ | |
309 | unsigned f; /* Various interesting flags */ | |
310 | void (*func)(void */*p*/); /* Function to call on closure */ | |
86c9bc4b | 311 | int err; /* What's wrong with the channel */ |
47a7631f MW |
312 | void *p; /* Argument to pass function */ |
313 | sel_file r, w; /* Reader and writer selectors */ | |
314 | char buf[CHAN_BUFSZ]; /* The actual data buffer */ | |
315 | } chan; | |
316 | ||
317 | #define CHANF_CLOSE 1u /* Close channel when buffer empty */ | |
318 | #define CHANF_READY 2u /* The channel destination exists */ | |
319 | ||
320 | /* --- @chan_close@ --- * | |
321 | * | |
322 | * Arguments: @chan *c@ = pointer to channel | |
323 | * | |
324 | * Returns: --- | |
325 | * | |
326 | * Use: Closes down a channel prematurely. | |
327 | */ | |
328 | ||
329 | extern void chan_close(chan */*c*/); | |
330 | ||
331 | /* --- @chan_dest@ --- * | |
332 | * | |
333 | * Arguments: @chan *c@ = pointer to channel | |
334 | * @int fd@ = destination file descriptor for channel | |
335 | * | |
336 | * Returns: --- | |
337 | * | |
338 | * Use: Sets the channel's destination so it knows where to put | |
339 | * data. | |
340 | */ | |
341 | ||
342 | extern void chan_dest(chan */*c*/, int /*fd*/); | |
343 | ||
344 | /* --- @chan_open@ --- * | |
345 | * | |
346 | * Arguments: @chan *c@ = pointer to channel to open | |
347 | * @int from, to@ = source and destination file descriptors | |
348 | * @void (*func)(void *p)@ = function to call on closure | |
349 | * @void *p@ = argument to pass to function | |
350 | * | |
351 | * Returns: --- | |
352 | * | |
353 | * Use: Opens a channel. Data is copied from the source to the | |
354 | * destination. The @to@ argument may be @-1@ if the file | |
355 | * descriptor isn't known yet. | |
356 | */ | |
357 | ||
358 | extern void chan_open(chan */*c*/, int /*from*/, int /*to*/, | |
359 | void (*/*func*/)(void */*p*/), void */*p*/); | |
360 | ||
361 | /*----- Character scanners ------------------------------------------------*/ | |
362 | ||
363 | /* --- A low-level scanner source --- */ | |
364 | ||
365 | typedef struct scansrc { | |
366 | struct scansrc *next; /* Next one in the list */ | |
367 | struct scansrc_ops *ops; /* Pointer to operations table */ | |
368 | char *src; /* Name of this source */ | |
369 | int line; /* Current line number */ | |
370 | dstr pushback; /* Pushback characters */ | |
371 | char *tok; /* Token pushback */ | |
372 | unsigned t; /* Token type pushback */ | |
373 | } scansrc; | |
374 | ||
375 | /* --- Scanner source operations --- */ | |
376 | ||
377 | typedef struct scansrc_ops { | |
378 | int (*scan)(scansrc */*ss*/); /* Read another character */ | |
379 | void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */ | |
380 | } scansrc_ops; | |
381 | ||
382 | /* --- A character scanner --- */ | |
383 | ||
384 | typedef struct scanner { | |
385 | scansrc *head, **tail; /* Scanner list head and tail */ | |
386 | int t; /* Token type */ | |
387 | dstr d; /* Current token value */ | |
388 | const char *wbegin, *wcont; /* Parsing exception strings */ | |
389 | } scanner; | |
390 | ||
391 | /* --- @scan_file@ --- * | |
392 | * | |
393 | * Arguments: @FILE *fp@ = pointer to file descriptor | |
394 | * @const char *name@ = pointer to source file name | |
395 | * @unsigned f@ = flags | |
396 | * | |
397 | * Returns: A scanner source. | |
398 | * | |
399 | * Use: Creates a new scanner source for reading from a file. | |
400 | */ | |
401 | ||
402 | #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */ | |
403 | ||
404 | extern scansrc *scan_file(FILE */*fp*/, const char */*name*/, | |
405 | unsigned /*f*/); | |
406 | ||
407 | /* --- @scan_argv@ --- * | |
408 | * | |
409 | * Arguments: @char **av@ = pointer to argument array (null terminated) | |
410 | * | |
411 | * Returns: A scanner source. | |
412 | * | |
413 | * Use: Creates a new scanner source for reading from an @argv@ | |
414 | * array. | |
415 | */ | |
416 | ||
417 | extern scansrc *scan_argv(char **/*av*/); | |
418 | ||
419 | /* --- @scan@ --- * | |
420 | * | |
421 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
422 | * | |
423 | * Returns: Character read, or end-of-file. | |
424 | * | |
425 | * Use: Scans a character from a source of characters. | |
426 | */ | |
427 | ||
428 | extern int scan(scanner */*sc*/); | |
429 | ||
430 | /* --- @unscan@ --- * | |
431 | * | |
432 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
433 | * @int ch@ = character to unscan | |
434 | * | |
435 | * Returns: --- | |
436 | * | |
437 | * Use: Scans a character from a source of characters. | |
438 | */ | |
439 | ||
440 | extern void unscan(scanner */*sc*/, int /*ch*/); | |
441 | ||
442 | /* --- @scan_push@ --- * | |
443 | * | |
444 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
445 | * @scansrc *ss@ = souorce to push | |
446 | * | |
447 | * Returns: --- | |
448 | * | |
449 | * Use: Pushes a scanner source onto the front of the queue. | |
450 | */ | |
451 | ||
452 | extern void scan_push(scanner */*sc*/, scansrc */*ss*/); | |
453 | ||
454 | /* --- @scan_add@ --- * | |
455 | * | |
456 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
457 | * @scansrc *ss@ = souorce to push | |
458 | * | |
459 | * Returns: --- | |
460 | * | |
461 | * Use: Adds a scanner source onto the end of the queue. | |
462 | */ | |
463 | ||
464 | extern void scan_add(scanner */*sc*/, scansrc */*ss*/); | |
465 | ||
466 | /* --- @scan_create@ --- * | |
467 | * | |
468 | * Arguments: @scanner *sc@ = scanner context to initialize | |
469 | * | |
470 | * Returns: --- | |
471 | * | |
472 | * Use: Initializes a scanner block ready for use. | |
473 | */ | |
474 | ||
475 | extern void scan_create(scanner */*sc*/); | |
476 | ||
477 | /* --- @scan_destroy@ --- * | |
478 | * | |
479 | * Arguments: @scanner *sc@ = pointer to scanner context | |
480 | * | |
481 | * Returns: --- | |
482 | * | |
483 | * Use: Destroys a scanner and all the sources attached to it. | |
484 | */ | |
485 | ||
486 | extern void scan_destroy(scanner */*sc*/); | |
487 | ||
488 | /*----- Configuration parsing ---------------------------------------------*/ | |
489 | ||
490 | /* --- Magical constants --- */ | |
491 | ||
492 | #define CTOK_EOF (-1) | |
493 | #define CTOK_WORD 256 | |
494 | ||
495 | /* --- @conf_undelim@ --- * | |
496 | * | |
497 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
498 | * @const char *d, *dd@ = pointer to characters to escape | |
499 | * | |
500 | * Returns: --- | |
501 | * | |
502 | * Use: Modifies the tokenizer. Characters in the first list will | |
503 | * always be considered to begin a word. Characters in the | |
504 | * second list will always be allowed to continue a word. | |
505 | */ | |
506 | ||
507 | extern void conf_undelim(scanner */*sc*/, | |
508 | const char */*d*/, const char */*dd*/); | |
509 | ||
510 | /* --- @token@ --- * | |
511 | * | |
512 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
513 | * | |
514 | * Returns: Type of token scanned. | |
515 | * | |
516 | * Use: Reads the next token from the character scanner. | |
517 | */ | |
518 | ||
519 | extern int token(scanner */*sc*/); | |
520 | ||
521 | /* --- @error@ --- * | |
522 | * | |
523 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
524 | * @const char *msg@ = message skeleton string | |
525 | * @...@ = extra arguments for the skeleton | |
526 | * | |
527 | * Returns: Doesn't | |
528 | * | |
529 | * Use: Reports an error at the current scanner location. | |
530 | */ | |
531 | ||
532 | extern void error(scanner */*sc*/, const char */*msg*/, ...); | |
533 | ||
534 | /* --- @pushback@ --- * | |
535 | * | |
536 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
537 | * | |
538 | * Returns: --- | |
539 | * | |
540 | * Use: Pushes the current token back. This is normally a precursor | |
541 | * to pushing a new scanner source. | |
542 | */ | |
543 | ||
544 | extern void pushback(scanner */*sc*/); | |
545 | ||
546 | /* --- @conf_enum@ --- * | |
547 | * | |
548 | * Arguments: @scanner *sc@ = pointer to a scanner object | |
549 | * @const char *list@ = comma-separated things to allow | |
550 | * @unsigned @f = flags for the search | |
551 | * @const char *err@ = error message if not found | |
552 | * | |
553 | * Returns: Index into list, zero-based, or @-1@. | |
554 | * | |
555 | * Use: Checks whether the current token is a string which matches | |
556 | * one of the comma-separated items given. The return value is | |
557 | * the index (zero-based) of the matched string in the list. | |
558 | * | |
559 | * The flags control the behaviour if no exact match is found. | |
560 | * If @ENUM_ABBREV@ is set, and the current token is a left | |
561 | * substring of exactly one of the possibilities, then that one | |
562 | * is chosen. If @ENUM_NONE@ is set, the value @-1@ is | |
563 | * returned; otherwise an error is reported and the program is | |
564 | * terminated. | |
565 | */ | |
566 | ||
567 | #define ENUM_ABBREV 1u | |
568 | #define ENUM_NONE 2u | |
569 | ||
570 | extern int conf_enum(scanner */*sc*/, const char */*list*/, | |
571 | unsigned /*flags*/, const char */*err*/); | |
572 | ||
573 | /* --- @conf_prefix@ --- * | |
574 | * | |
575 | * Arguments: @scanner *sc@ = pointer to a scanner object | |
576 | * @const char *p@ = pointer to prefix string to check | |
577 | * | |
578 | * Returns: Nonzero if the prefix matches. | |
579 | * | |
580 | * Use: If the current token is a word matching the given prefix | |
581 | * string, then it and an optional `.' character are removed and | |
582 | * a nonzero result is returned. Otherwise the current token is | |
583 | * left as it is, and zero is returned. | |
584 | * | |
585 | * Typical options parsing code would remove an expected prefix, | |
586 | * scan an option anyway (since qualifying prefixes are | |
587 | * optional) and if a match is found, claim the option. If no | |
588 | * match is found, and a prefix was stripped, then an error | |
589 | * should be reported. | |
590 | */ | |
591 | ||
592 | extern int conf_prefix(scanner */*sc*/, const char */*p*/); | |
593 | ||
594 | /* --- @CONF_BEGIN@, @CONF_END@ --- * | |
595 | * | |
596 | * Arguments: @sc@ = scanner to read from | |
597 | * @prefix@ = prefix to scan for | |
598 | * @desc@ = description of what we're parsing | |
599 | * | |
600 | * Use: Bracket an options parsing routine. The current token is | |
601 | * checked to see whether it matches the prefix. If so, it is | |
602 | * removed and the following token examined. If that's a `.' | |
603 | * then it's removed. If it's a `{' then the enclosed | |
604 | * option-parsing code is executed in a loop until a matching | |
605 | * '}' is found. If the options parser doesn't accept an | |
606 | * option, the behaviour is dependent on whether a prefix was | |
607 | * seen: if so, an error is reported; otherwse a zero return is | |
608 | * made. | |
609 | */ | |
610 | ||
611 | #define CS_PLAIN 0 | |
612 | #define CS_PREFIX 1 | |
613 | #define CS_BRACE 2 | |
614 | #define CS_UNKNOWN 3 | |
615 | ||
616 | #define CONF_BEGIN(sc, prefix, desc) do { \ | |
617 | scanner *_conf_sc = (sc); \ | |
618 | const char *_conf_desc = (desc); \ | |
619 | int _conf_state = CS_PLAIN; \ | |
620 | \ | |
621 | /* --- Read the initial prefix --- */ \ | |
622 | \ | |
623 | if (_conf_sc->t == CTOK_WORD && \ | |
624 | strcmp(_conf_sc->d.buf, (prefix)) == 0) { \ | |
625 | token(_conf_sc); \ | |
626 | _conf_state = CS_PREFIX; \ | |
627 | if (_conf_sc->t == '.') \ | |
628 | token(_conf_sc); \ | |
629 | else if (_conf_sc->t == '{') { \ | |
630 | token(_conf_sc); \ | |
631 | _conf_state = CS_BRACE; \ | |
632 | } \ | |
633 | } \ | |
634 | \ | |
635 | /* --- Ensure the next token is a word --- */ \ | |
636 | \ | |
637 | if (_conf_sc->t != CTOK_WORD) \ | |
638 | error(_conf_sc, "parse error, expected option keyword"); \ | |
639 | do { | |
640 | ||
641 | #define CONF_END \ | |
642 | \ | |
643 | /* --- Reject an option --- * \ | |
644 | * \ | |
645 | * We could get here as a result of an explicit @CONF_REJECT@ or \ | |
646 | * because the option wasn't accepted. \ | |
647 | */ \ | |
648 | \ | |
649 | goto _conf_reject; \ | |
650 | _conf_reject: \ | |
651 | if (_conf_state == CS_PLAIN) \ | |
652 | _conf_state = CS_UNKNOWN; \ | |
653 | else { \ | |
654 | error(_conf_sc, "unknown %s option `%s'", \ | |
655 | _conf_desc, _conf_sc->d.buf); \ | |
656 | } \ | |
657 | \ | |
658 | /* --- Accept an option --- * \ | |
659 | * \ | |
660 | * It's safe to drop through from above. Either an error will have \ | |
661 | * been reported, or the state is not @CS_BRACE@. \ | |
662 | */ \ | |
663 | \ | |
664 | _conf_accept: \ | |
665 | if (_conf_state == CS_BRACE && _conf_sc->t == ';') \ | |
666 | token(_conf_sc); \ | |
667 | } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \ | |
668 | \ | |
669 | /* --- Check for a closing brace --- */ \ | |
670 | \ | |
671 | if (_conf_state == CS_BRACE) { \ | |
672 | if (_conf_sc->t == '}') \ | |
673 | token(_conf_sc); \ | |
674 | else \ | |
675 | error(_conf_sc, "parse error, expected `}'"); \ | |
676 | } \ | |
677 | \ | |
678 | /* --- Return an appropriate value --- */ \ | |
679 | \ | |
680 | return (_conf_state != CS_UNKNOWN); \ | |
681 | } while (0) | |
682 | ||
683 | /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- * | |
684 | * | |
685 | * Arguments: --- | |
686 | * | |
687 | * Use: Within an options parser (between @CONF_BEGIN@ and | |
688 | * @CONF_END@), accept or reject an option. | |
689 | */ | |
690 | ||
691 | #define CONF_ACCEPT goto _conf_accept | |
692 | #define CONF_REJECT goto _conf_reject | |
693 | ||
694 | /* --- @CONF_QUAL@ --- * | |
695 | * | |
696 | * Arguments: --- | |
697 | * | |
698 | * Use: Evaluates to a nonzero value if the current option is | |
699 | * qualified. This can be used to decide whether abbreviations | |
700 | * for options should be accepted. | |
701 | */ | |
702 | ||
703 | #define CONF_QUAL (_conf_state != CS_PLAIN) | |
704 | ||
705 | /* --- @conf_name@ --- * | |
706 | * | |
707 | * Arguments: @scanner *sc@ = pointer to scanner | |
708 | * @char delim@ = delimiter character to look for | |
709 | * @dstr *d@ = pointer to dynamic string for output | |
710 | * | |
711 | * Returns: --- | |
712 | * | |
713 | * Use: Reads in a compound name consisting of words separated by | |
714 | * delimiters. Leading and trailing delimiters are permitted, | |
715 | * although they'll probably cause confusion if used. The name | |
716 | * may be enclosed in square brackets if that helps at all. | |
717 | * | |
718 | * Examples of compound names are filenames (delimited by `/') | |
719 | * and IP addresses (delimited by `.'). | |
720 | */ | |
721 | ||
722 | extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/); | |
723 | ||
35a142ca MW |
724 | /* --- @conf_fname@ --- * |
725 | * | |
726 | * Arguments: @scanner *sc@ = pointer to scanner | |
727 | * @dstr *d@ = pointer to dynamic string for output | |
728 | * | |
729 | * Returns: --- | |
730 | * | |
731 | * Use: Reads a file name from the input and stores it in @d@. | |
732 | */ | |
733 | ||
734 | extern void conf_fname(scanner */*sc*/, dstr */*d*/); | |
735 | ||
47a7631f MW |
736 | /*----- Reference-counted file descriptors --------------------------------*/ |
737 | ||
738 | typedef struct reffd { | |
739 | int fd; | |
740 | unsigned ref; | |
741 | void (*proc)(void */*p*/); | |
742 | void *p; | |
743 | } reffd; | |
744 | ||
745 | /* --- @reffd_init@ --- * | |
746 | * | |
747 | * Arguments: @int fd@ = file descriptor | |
748 | * | |
749 | * Returns: Reference-counted file descriptor object. | |
750 | * | |
751 | * Use: Creates a refcounted file descriptor. | |
752 | */ | |
753 | ||
754 | extern reffd *reffd_init(int /*fd*/); | |
755 | ||
756 | /* --- @reffd_handler@ --- * | |
757 | * | |
758 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
759 | * @void (*proc)(void *p)@ = procedure to call | |
760 | * @void *p@ | |
761 | * | |
762 | * Returns: --- | |
763 | * | |
764 | * Use: Sets the reference counted file descriptor to call @proc@ | |
765 | * when it is no longer required. | |
766 | */ | |
767 | ||
768 | extern void reffd_handler(reffd */*r*/, void (*/*proc*/)(void */*p*/), | |
769 | void */*p*/); | |
770 | ||
771 | /* --- @reffd_inc@ --- * | |
772 | * | |
773 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
774 | * | |
775 | * Returns: --- | |
776 | * | |
777 | * Use: Increments the reference count for a file descriptor. | |
778 | */ | |
779 | ||
780 | #define REFFD_INC(r) do { (r)->ref++; } while (0) | |
781 | ||
782 | extern void reffd_inc(reffd */*r*/); | |
783 | ||
784 | /* --- @reffd_dec@ --- * | |
785 | * | |
786 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
787 | * | |
788 | * Returns: --- | |
789 | * | |
790 | * Use: Decrements the reference count for a file descriptor. | |
791 | */ | |
792 | ||
793 | #define REFFD_DEC(r) do { \ | |
794 | reffd *_r = (r); \ | |
795 | _r->ref--; \ | |
796 | if (_r->ref == 0) { \ | |
797 | close(_r->fd); \ | |
798 | if (_r->proc) \ | |
799 | _r->proc(_r->p); \ | |
800 | DESTROY(_r); \ | |
801 | } \ | |
802 | } while (0) | |
803 | ||
804 | extern void reffd_dec(reffd */*r*/); | |
805 | ||
806 | /*----- Sources, targets and endpoints ------------------------------------*/ | |
807 | ||
808 | /* --- Basic endpoint structure --- */ | |
809 | ||
810 | typedef struct endpt { | |
811 | struct endpt_ops *ops; /* Pointer to operations table */ | |
812 | struct endpt *other; /* Pointer to sibling endpoint */ | |
813 | unsigned f; /* Various flags */ | |
814 | struct tango *t; /* Private data structure */ | |
815 | reffd *in, *out; /* File descriptors */ | |
816 | } endpt; | |
817 | ||
818 | /* --- Endpoint flags --- */ | |
819 | ||
820 | #define EPF_PENDING 1u /* Endpoint creation in progress */ | |
821 | #define EPF_FILE 2u /* Endpoint smells like a file */ | |
822 | ||
823 | /* --- Endpoint operations table --- */ | |
824 | ||
825 | typedef struct endpt_ops { | |
826 | ||
827 | /* --- @attach@ --- * | |
828 | * | |
829 | * Arguments: @endpt *e@ = pointer to endpoint to be attached | |
830 | * @reffd *in, *out@ = input and output file descriptors | |
831 | * | |
832 | * Returns: --- | |
833 | * | |
834 | * Use: Instructs a non-file endpoint to attach itself to a pair of | |
835 | * files. | |
836 | */ | |
837 | ||
838 | void (*attach)(endpt */*e*/, reffd */*in*/, reffd */*out*/); | |
839 | ||
840 | /* --- @file@ --- * | |
841 | * | |
842 | * Arguments: @endpt *e@ = pointer to endpoint in question | |
843 | * @endpt *f@ = pointer to a file endpoint | |
844 | * | |
845 | * Returns: --- | |
846 | * | |
847 | * Use: Informs a non-file endpoint of a file endpoint which will | |
848 | * want to be closed when it's finished with. At that time, the | |
849 | * endpoint should arrange to have both itself and its partner | |
850 | * closed. If no file is registered, the endpoint manager will | |
851 | * close both endpoints itself. | |
852 | */ | |
853 | ||
854 | void (*file)(endpt */*e*/, endpt */*f*/); | |
855 | ||
856 | /* --- @wclose@ --- * | |
857 | * | |
858 | * Arguments: @endpt *e@ = endpoint to be partially closed | |
859 | * | |
860 | * Returns: --- | |
861 | * | |
862 | * Use: Announces that the endpoint will not be written to any more. | |
863 | */ | |
864 | ||
865 | void (*wclose)(endpt */*e*/); | |
866 | ||
867 | /* --- @close@ --- * | |
868 | * | |
869 | * Arguments: @endpt *e@ = endpoint to be closed | |
870 | * | |
871 | * Returns: --- | |
872 | * | |
873 | * Use: Completely closes an endpoint. The endpoint's data may be | |
874 | * freed, although some endpoints may wish to delay freeing for | |
875 | * some reason. | |
876 | */ | |
877 | ||
878 | void (*close)(endpt */*e*/); | |
879 | ||
880 | } endpt_ops; | |
881 | ||
882 | /* --- A basic target object --- */ | |
883 | ||
884 | typedef struct target { | |
885 | struct target_ops *ops; | |
886 | char *desc; | |
887 | } target; | |
888 | ||
889 | /* --- Forwarding target operations --- */ | |
890 | ||
891 | typedef struct target_ops { | |
892 | const char *name; /* Name of this target */ | |
893 | ||
894 | /* --- @option@ --- * | |
895 | * | |
896 | * Arguments: @target *t@ = pointer to target object, or zero if global | |
897 | * @scanner *sc@ = scanner to read from | |
898 | * | |
899 | * Returns: Nonzero to claim the option. | |
900 | * | |
901 | * Use: Handles an option string from the configuration file. | |
902 | */ | |
903 | ||
904 | int (*option)(target */*t*/, scanner */*sc*/); | |
905 | ||
906 | /* --- @read@ --- * | |
907 | * | |
908 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
909 | * | |
910 | * Returns: Pointer to a target object to claim, null to reject. | |
911 | * | |
912 | * Use: Parses a target description from the configuration file. | |
913 | * Only the socket target is allowed to omit the prefix on a | |
914 | * target specification. | |
915 | */ | |
916 | ||
917 | target *(*read)(scanner */*sc*/); | |
918 | ||
919 | /* --- @confirm@ --- * | |
920 | * | |
921 | * Arguments: @target *t@ = pointer to target | |
922 | * | |
923 | * Returns: --- | |
924 | * | |
925 | * Use: Confirms configuration of a target. | |
926 | */ | |
927 | ||
928 | void (*confirm)(target */*t*/); | |
929 | ||
930 | /* --- @create@ --- * | |
931 | * | |
932 | * Arguments: @target *t@ = pointer to target | |
933 | * @const char *desc@ = description of connection | |
934 | * | |
935 | * Returns: Pointer to a created endpoint. | |
936 | * | |
937 | * Use: Generates a target endpoint for communication. | |
938 | */ | |
939 | ||
940 | endpt *(*create)(target */*t*/, const char */*desc*/); | |
941 | ||
942 | /* --- @destroy@ --- * | |
943 | * | |
944 | * Arguments: @target *t@ = pointer to target | |
945 | * | |
946 | * Returns: --- | |
947 | * | |
948 | * Use: Destroys a target. | |
949 | */ | |
950 | ||
951 | void (*destroy)(target */*t*/); | |
952 | ||
953 | } target_ops; | |
954 | ||
955 | /* --- A basic source object --- */ | |
956 | ||
957 | typedef struct source { | |
958 | struct source *next, *prev; | |
959 | struct source_ops *ops; | |
960 | char *desc; | |
961 | } source; | |
962 | ||
963 | /* --- Forwarding source operations --- */ | |
964 | ||
965 | typedef struct source_ops { | |
966 | const char *name; /* Name of this source */ | |
967 | ||
968 | /* --- @option@ --- * | |
969 | * | |
970 | * Arguments: @scanner *sc@ = scanner to read from | |
971 | * @source *s@ = pointer to source object, or zero if global | |
972 | * | |
973 | * Returns: Nonzero to claim the option. | |
974 | * | |
975 | * Use: Handles an option string from the configuration file. | |
976 | */ | |
977 | ||
978 | int (*option)(source */*s*/, scanner */*sc*/); | |
979 | ||
980 | /* --- @read@ --- * | |
981 | * | |
982 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
983 | * | |
984 | * Returns: Pointer to a source object to claim, null to reject. | |
985 | * | |
986 | * Use: Parses a source description from the configuration file. | |
987 | * Only the socket source is allowed to omit the prefix on a | |
988 | * source specification. | |
989 | */ | |
990 | ||
991 | source *(*read)(scanner */*sc*/); | |
992 | ||
993 | /* --- @attach@ --- * | |
994 | * | |
995 | * Arguments: @source *s@ = pointer to source | |
996 | * @scanner *sc@ = scanner (for error reporting) | |
997 | * @target *t@ = pointer to target to attach | |
998 | * | |
999 | * Returns: --- | |
1000 | * | |
1001 | * Use: Attaches a target to a source. | |
1002 | */ | |
1003 | ||
1004 | void (*attach)(source */*s*/, scanner */*sc*/, target */*t*/); | |
1005 | ||
1006 | /* --- @destroy@ --- * | |
1007 | * | |
1008 | * Arguments: @source *s@ = pointer to source | |
1009 | * | |
1010 | * Returns: --- | |
1011 | * | |
1012 | * Use: Destroys a source. Used when closing the system down, for | |
1013 | * example as a result of a signal. | |
1014 | */ | |
1015 | ||
1016 | void (*destroy)(source */*s*/); | |
1017 | ||
1018 | } source_ops; | |
1019 | ||
1020 | /* --- @endpt_kill@ --- * | |
1021 | * | |
1022 | * Arguments: @endpt *a@ = an endpoint | |
1023 | * | |
1024 | * Returns: --- | |
1025 | * | |
1026 | * Use: Kills an endpoint. If the endpoint is joined to another, the | |
1027 | * other endpoint is also killed, as is the connection between | |
1028 | * them (and that's the tricky bit). | |
1029 | */ | |
1030 | ||
1031 | extern void endpt_kill(endpt */*a*/); | |
1032 | ||
1033 | /* --- @endpt_killall@ --- * | |
1034 | * | |
1035 | * Arguments: --- | |
1036 | * | |
1037 | * Returns: --- | |
1038 | * | |
1039 | * Use: Destroys all current endpoint connections. Used when | |
1040 | * shutting down. | |
1041 | */ | |
1042 | ||
1043 | extern void endpt_killall(void); | |
1044 | ||
1045 | /* --- @endpt_join@ --- * | |
1046 | * | |
1047 | * Arguments: @endpt *a@ = pointer to first endpoint | |
1048 | * @endpt *b@ = pointer to second endpoint | |
86c9bc4b | 1049 | * @const char *desc@ = description of connection |
47a7631f MW |
1050 | * |
1051 | * Returns: --- | |
1052 | * | |
86c9bc4b MW |
1053 | * Use: Joins two endpoints together. It's OK to join endpoints |
1054 | * which are already joined; in fact, the the right thing to do | |
1055 | * when your endpoint decides that it's not pending any more is | |
1056 | * to join it to its partner again. | |
1057 | * | |
1058 | * If the endpoints are already connected then the description | |
1059 | * string is ignored. The endpoint manager takes a copy of | |
1060 | * the string, so you don't need to keep it around. | |
47a7631f MW |
1061 | */ |
1062 | ||
86c9bc4b | 1063 | extern void endpt_join(endpt */*a*/, endpt */*b*/, const char */*desc*/); |
47a7631f MW |
1064 | |
1065 | /* --- @source_add@ --- * | |
1066 | * | |
1067 | * Arguments: @source *s@ = pointer to a source | |
1068 | * | |
1069 | * Returns: --- | |
1070 | * | |
1071 | * Use: Adds a source to the master list. Only do this for passive | |
1072 | * sources (e.g., listening sockets), not active sources (e.g., | |
1073 | * executable programs). | |
1074 | */ | |
1075 | ||
1076 | extern void source_add(source */*s*/); | |
1077 | ||
1078 | /* --- @source_remove@ --- * | |
1079 | * | |
1080 | * Arguments: @source *s@ = pointer to a source | |
1081 | * | |
1082 | * Returns: --- | |
1083 | * | |
1084 | * Use: Removes a source from the master list. | |
1085 | */ | |
1086 | ||
1087 | extern void source_remove(source */*s*/); | |
1088 | ||
1089 | /* --- @source_killall@ --- * | |
1090 | * | |
1091 | * Arguments: --- | |
1092 | * | |
1093 | * Returns: --- | |
1094 | * | |
1095 | * Use: Frees all sources. | |
1096 | */ | |
1097 | ||
1098 | extern void source_killall(void); | |
1099 | ||
1100 | /*----- The exec source and target ----------------------------------------*/ | |
1101 | ||
1102 | extern source_ops xsource_ops; | |
1103 | extern target_ops xtarget_ops; | |
1104 | ||
1105 | /* --- @exec_init@ --- * | |
1106 | * | |
1107 | * Arguments: --- | |
1108 | * | |
1109 | * Returns: --- | |
1110 | * | |
1111 | * Use: Initializes the executable problem source and target. | |
1112 | */ | |
1113 | ||
1114 | extern void exec_init(void); | |
1115 | ||
1116 | /*----- The file source and target ----------------------------------------*/ | |
1117 | ||
1118 | extern source_ops fsource_ops; | |
1119 | extern target_ops ftarget_ops; | |
1120 | ||
1121 | /*----- The socket source and target --------------------------------------*/ | |
1122 | ||
1123 | extern source_ops ssource_ops; | |
1124 | extern target_ops starget_ops; | |
1125 | ||
1126 | /* --- @starget_connected@ --- * | |
1127 | * | |
1128 | * Arguments: @int fd@ = file descriptor now ready for use | |
1129 | * @void *p@ = pointer to an endpoint structure | |
1130 | * | |
1131 | * Returns: --- | |
1132 | * | |
1133 | * Use: Handles successful connection of the target endpoint. | |
1134 | */ | |
1135 | ||
1136 | extern void starget_connected(int /*fd*/, void */*p*/); | |
1137 | ||
1138 | /*----- Handling of file attributes ---------------------------------------*/ | |
1139 | ||
1140 | /* --- File attribute options structure --- */ | |
1141 | ||
1142 | typedef struct fattr { | |
1143 | unsigned mode; | |
1144 | uid_t uid; | |
1145 | gid_t gid; | |
1146 | } fattr; | |
1147 | ||
1148 | /* --- Shared global options --- */ | |
1149 | ||
1150 | extern fattr fattr_global; | |
1151 | ||
1152 | /* --- @fattr_init@ --- * | |
1153 | * | |
1154 | * Arguments: @fattr *f@ = pointer to file attributes | |
1155 | * | |
1156 | * Returns: --- | |
1157 | * | |
1158 | * Use: Initializes a set of file attributes to default values. | |
1159 | */ | |
1160 | ||
1161 | extern void fattr_init(fattr */*f*/); | |
1162 | ||
1163 | /* --- @fattr_option@ --- * | |
1164 | * | |
1165 | * Arguments: @scanner *sc@ = pointer to scanner to read | |
1166 | * @fattr *f@ = pointer to file attributes to set | |
1167 | * | |
1168 | * Returns: Whether the option was clamed. | |
1169 | * | |
1170 | * Use: Reads file attributes from a scanner. | |
1171 | */ | |
1172 | ||
1173 | extern int fattr_option(scanner */*sc*/, fattr */*f*/); | |
1174 | ||
1175 | /* --- @fattr_apply@ --- * | |
1176 | * | |
1177 | * Arguments: @const char *file@ = pointer to filename | |
1178 | * @fattr *f@ = pointer to attribute set | |
1179 | * | |
1180 | * Returns: @-1@ if it failed. | |
1181 | * | |
1182 | * Use: Applies file attributes to a file. For best results, try to | |
1183 | * create the file with the right permissions and so on. This | |
1184 | * call will fix everything up, but there are potential races | |
1185 | * which might catch you out if you're not careful. | |
1186 | */ | |
1187 | ||
1188 | extern int fattr_apply(const char */*file*/, fattr */*f*/); | |
1189 | ||
1190 | /*----- Making privileged connections -------------------------------------*/ | |
1191 | ||
1192 | /* --- @privconn_split@ --- * | |
1193 | * | |
1194 | * Arguments: @sel_state *s@ = select state | |
1195 | * | |
1196 | * Returns: --- | |
1197 | * | |
1198 | * Use: Splits off the privileged binding code into a separate | |
1199 | * process. | |
1200 | */ | |
1201 | ||
1202 | extern void privconn_split(sel_state */*s*/); | |
1203 | ||
1204 | /* --- @privconn_adddest@ --- * | |
1205 | * | |
1206 | * Arguments: @struct in_addr peer@ = address to connect to | |
1207 | * @unsigned port@ = port to connect to | |
1208 | * | |
1209 | * Returns: Index for this destination address, or @-1@ if not | |
1210 | * available. | |
1211 | * | |
1212 | * Use: Adds a valid destination for a privileged connection. | |
1213 | */ | |
1214 | ||
1215 | extern int privconn_adddest(struct in_addr /*peer*/, unsigned /*port*/); | |
1216 | ||
1217 | /* --- @privconn_connect@ --- * | |
1218 | * | |
1219 | * Arguments: @conn *c@ = connection structure to fill in | |
1220 | * @sel_state *s@ = pointer to select state to attach to | |
1221 | * @int i@ = address index to connect to | |
1222 | * @struct in_addr bind@ = address to bind to | |
1223 | * @void (*func)(int, void *)@ = function to call on connect | |
1224 | * @void *p@ = argument for the function | |
1225 | * | |
1226 | * Returns: Zero on success, @-1@ on failure. | |
1227 | * | |
1228 | * Use: Sets up a privileged connection job. | |
1229 | */ | |
1230 | ||
1231 | extern int privconn_connect(conn */*c*/, sel_state */*s*/, | |
1232 | int /*i*/, struct in_addr /*bind*/, | |
1233 | void (*/*func*/)(int, void *), void */*p*/); | |
1234 | ||
1235 | /*----- Identifying remote clients ----------------------------------------*/ | |
1236 | ||
1237 | typedef struct id_req { | |
1238 | struct sockaddr_in lsin; /* Local address of connection */ | |
1239 | struct sockaddr_in rsin; /* Remote address of connection */ | |
1240 | const char *desc; /* Description of connection */ | |
1241 | const char *act; /* Action taken by server */ | |
1242 | reffd *r; /* Pointer to file descriptor */ | |
1243 | } id_req; | |
1244 | ||
1245 | /* --- @identify@ --- * | |
1246 | * | |
1247 | * Arguments: @const id_req *q@ = pointer to request block | |
1248 | * | |
1249 | * Returns: --- | |
1250 | * | |
1251 | * Use: Starts a background ident lookup and reverse-resolve job | |
1252 | * which will, eventually, report a message to the system log. | |
1253 | */ | |
1254 | ||
1255 | extern void identify(const id_req */*q*/); | |
1256 | ||
1257 | /*----- Host-based access control -----------------------------------------*/ | |
1258 | ||
1259 | /* --- An access control entry --- */ | |
1260 | ||
1261 | typedef struct acl_entry { | |
1262 | struct acl_entry *next; /* Next entry in the list */ | |
1263 | const struct acl_ops *ops; /* Operations for the ACL entry */ | |
1264 | unsigned act; /* What to do with matching hosts */ | |
1265 | } acl_entry; | |
1266 | ||
1267 | #define ACL_DENY 0 /* Deny access to matching conns */ | |
1268 | #define ACL_ALLOW 1 /* Allow access to matching conns */ | |
1269 | #define ACL_PERM 1u /* Bit mask for permission bit */ | |
1270 | ||
1271 | /* --- Host-based access control --- */ | |
1272 | ||
1273 | typedef struct acl_host { | |
1274 | acl_entry a; /* Base structure */ | |
1275 | struct in_addr addr, mask; /* Address and netmask */ | |
1276 | } acl_host; | |
1277 | ||
1278 | /* --- ACL methods --- */ | |
1279 | ||
1280 | typedef struct acl_ops { | |
1281 | int (*check)(void */*a*/, struct in_addr /*addr*/, unsigned /*port*/); | |
1282 | void (*dump)(void */*a*/, FILE */*fp*/); | |
1283 | void (*free)(void */*a*/); | |
1284 | } acl_ops; | |
1285 | ||
1286 | /* --- @acl_check@ --- * | |
1287 | * | |
1288 | * Arguments: @acl_entry *a@ = pointer to ACL to check against | |
1289 | * @struct in_addr addr@ = address to check | |
1290 | * @unsigned port@ = port number to check | |
1291 | * @int *act@ = verdict (should initially be @ACT_ALLOW@) | |
1292 | * | |
1293 | * Returns: Zero if undecided, nonzero if a rule matched. | |
1294 | * | |
1295 | * Use: Checks an address against an ACL. | |
1296 | */ | |
1297 | ||
1298 | extern int acl_check(acl_entry */*a*/, | |
1299 | struct in_addr /*addr*/, unsigned /*port*/, | |
1300 | int */*act*/); | |
1301 | ||
1302 | /* --- @acl_dump@ --- * | |
1303 | * | |
1304 | * Arguments: @acl_entry *a@ = pointer to ACL to dump | |
1305 | * @FILE *fp@ = pointer to stream to dump on | |
1306 | * | |
1307 | * Returns: --- | |
1308 | * | |
1309 | * Use: Dumps an access control list to an output stream. | |
1310 | */ | |
1311 | ||
1312 | extern void acl_dump(acl_entry */*a*/, FILE */*fp*/); | |
1313 | ||
1314 | /* --- @acl_free@ --- * | |
1315 | * | |
1316 | * Arguments: @acl_entry *a@ = pointer to a list of ACLs | |
1317 | * | |
1318 | * Returns: --- | |
1319 | * | |
1320 | * Use: Frees all of the memory used by an ACL. | |
1321 | */ | |
1322 | ||
1323 | extern void acl_free(acl_entry */*a*/); | |
1324 | ||
1325 | /* --- @acl_addhost@ --- * | |
1326 | * | |
1327 | * Arguments: @acl_entry ***a@ = address of pointer to list tail | |
1328 | * @unsigned act@ = what to do with matching addresses | |
1329 | * @struct in_addr addr, mask@ = address and mask to match | |
1330 | * | |
1331 | * Returns: --- | |
1332 | * | |
1333 | * Use: Adds a host-authentication entry to the end of an access | |
1334 | * control list. | |
1335 | */ | |
1336 | ||
1337 | extern void acl_addhost(acl_entry ***/*a*/, unsigned /*act*/, | |
1338 | struct in_addr /*addr*/, struct in_addr /*mask*/); | |
1339 | ||
1340 | /* --- @acl_addpriv@ --- * | |
1341 | * | |
1342 | * Arguments: @acl_entry ***a@ = address of pointer to list tail | |
1343 | * @unsigned act@ = what to do with matching addresses | |
1344 | * | |
1345 | * Returns: --- | |
1346 | * | |
1347 | * Use: Adds a privileged-port check to the end of an access control | |
1348 | * list. | |
1349 | */ | |
1350 | ||
1351 | extern void acl_addpriv(acl_entry ***/*a*/, unsigned /*act*/); | |
1352 | ||
1353 | /*----- Network addresses -------------------------------------------------*/ | |
1354 | ||
1355 | /* --- A generic socket address --- * | |
1356 | * | |
1357 | * Not all systems understand @sa_len@ fields. (In particular, Linux | |
1358 | * doesn't.) Some fairly ugly hacking is then performed on particular | |
1359 | * address types. | |
1360 | */ | |
1361 | ||
1362 | typedef struct addr { | |
1363 | struct addr_ops *ops; | |
1364 | size_t sz; | |
1365 | } addr; | |
1366 | ||
1367 | #define ADDRSZ(sz) (sizeof(addr) + (sz)) | |
1368 | ||
1369 | /* --- Address configuration --- * | |
1370 | * | |
1371 | * An address family will want to extend this. | |
1372 | */ | |
1373 | ||
1374 | typedef struct addr_opts { | |
1375 | unsigned f; | |
1376 | } addr_opts; | |
1377 | ||
1378 | #define ADDRF_NOLOG 1u | |
1379 | ||
1380 | /* --- Address types --- * | |
1381 | * | |
1382 | * For things like Internet addresses, source and destinations look | |
1383 | * different. | |
1384 | */ | |
1385 | ||
1386 | enum { | |
1387 | ADDR_SRC, | |
1388 | ADDR_DEST, | |
1389 | ADDR_GLOBAL | |
1390 | }; | |
1391 | ||
1392 | /* --- Description of an address type handler --- */ | |
1393 | ||
1394 | typedef struct addr_ops { | |
1395 | const char *name; /* Protocol's internal name */ | |
1396 | ||
1397 | /* --- @read@ --- * | |
1398 | * | |
1399 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
1400 | * @unsigned type@ = type of address to be read | |
1401 | * | |
1402 | * Returns: A filled-in socket address. | |
1403 | * | |
1404 | * Use: Parses a textual representation of a socket address. | |
1405 | */ | |
1406 | ||
1407 | addr *(*read)(scanner */*sc*/, unsigned /*type*/); | |
1408 | ||
1409 | /* --- @destroy@ --- * | |
1410 | * | |
1411 | * Arguments: @addr *a@ = pointer to an address block | |
1412 | * | |
1413 | * Returns: --- | |
1414 | * | |
1415 | * Use: Disposes of an address block in some suitable fashion. | |
1416 | */ | |
1417 | ||
1418 | void (*destroy)(addr */*a*/); | |
1419 | ||
1420 | /* --- @print@ --- * | |
1421 | * | |
1422 | * Arguments: @addr *a@ = pointer to socket address to read | |
1423 | * @unsigned type@ = type of address to be written | |
1424 | * @dstr *d@ = string on which to write the description | |
1425 | * | |
1426 | * Returns: --- | |
1427 | * | |
1428 | * Use: Writes a textual representation of a socket address to | |
1429 | * a string. | |
1430 | */ | |
1431 | ||
1432 | void (*print)(addr */*a*/, unsigned /*type*/, dstr */*d*/); | |
1433 | ||
1434 | /* --- @initsrcopts@ --- * | |
1435 | * | |
1436 | * Arguments: --- | |
1437 | * | |
1438 | * Returns: A pointer to a protocol-specific data block for a listener | |
1439 | * | |
1440 | * Use: Creates a data block for a listener. This is attached to the | |
1441 | * listener data structure. Options can then be requested, and | |
1442 | * are added to the block when necessary. | |
1443 | */ | |
1444 | ||
1445 | addr_opts *(*initsrcopts)(void); | |
1446 | ||
1447 | /* --- @option@ --- * | |
1448 | * | |
1449 | * Arguments: @scanner *sc@ = pointer to a scanner to read from | |
1450 | * @unsigned type@ = kind of option this is | |
1451 | * @addr_opts *ao@ = data block to modify (from @init@), or null | |
1452 | * | |
1453 | * Returns: Nonzero to claim the option. | |
1454 | * | |
1455 | * Use: Parses a source option, either global or listener-specific. | |
1456 | */ | |
1457 | ||
1458 | int (*option)(scanner */*sc*/, addr_opts */*ao*/, unsigned /*type*/); | |
1459 | ||
1460 | /* --- @confirm@ --- * | |
1461 | * | |
1462 | * Arguments: @addr *a@ = pointer to an address structure | |
1463 | * @unsigned type@ = kind of address this is | |
1464 | * @addr_opts *ao@ = address options | |
1465 | * | |
1466 | * Returns: --- | |
1467 | * | |
1468 | * Use: Called during initialization when an address is fully | |
1469 | * configured. | |
1470 | */ | |
1471 | ||
1472 | void (*confirm)(addr */*a*/, unsigned /*type*/, addr_opts */*ao*/); | |
1473 | ||
1474 | /* --- @freesrcopts@ --- * | |
1475 | * | |
1476 | * Arguments: @addr_opts *ao@ = data block to remove | |
1477 | * | |
1478 | * Returns: --- | |
1479 | * | |
1480 | * Use: Throws away all the configuration data for an address type. | |
1481 | */ | |
1482 | ||
1483 | void (*freesrcopts)(addr_opts */*ao*/); | |
1484 | ||
1485 | /* --- @bind@ --- * | |
1486 | * | |
1487 | * Arguments: @addr *a@ = the address to bind to | |
1488 | * @addr_opts *ao@ = the address options | |
1489 | * | |
1490 | * Returns: File descriptor of bound socket if OK, or @-1@ on error. | |
1491 | * | |
1492 | * Use: Binds a listening socket. The tedious stuff with @listen@ | |
1493 | * isn't necessary. | |
1494 | */ | |
1495 | ||
1496 | int (*bind)(addr */*a*/, addr_opts */*ao*/); | |
1497 | ||
1498 | /* --- @unbind@ --- * | |
1499 | * | |
1500 | * Arguments: @addr *a@ = pointer to an address | |
1501 | * | |
1502 | * Returns: --- | |
1503 | * | |
1504 | * Use: Unbinds an address. This is used when tidying up. The main | |
1505 | * purpose is to let the Unix-domain handler remove its socket | |
1506 | * node from the filesystem. | |
1507 | */ | |
1508 | ||
1509 | void (*unbind)(addr */*a*/); | |
1510 | ||
1511 | /* --- @accept@ --- * | |
1512 | * | |
1513 | * Arguments: @int fd@ = listening file descriptor | |
1514 | * @addr_opts *ao@ = data block to get configuration from | |
1515 | * @const char *desc@ = description of the listener | |
1516 | * | |
1517 | * Returns: Pointer to a reference counted file descriptor. | |
1518 | * | |
1519 | * Use: Accepts, verifies and logs an incoming connection. | |
1520 | */ | |
1521 | ||
1522 | reffd *(*accept)(int /*fd*/, addr_opts */*ao*/, const char */*desc*/); | |
1523 | ||
1524 | /* --- @inittargopts@ --- * | |
1525 | * | |
1526 | * Arguments: --- | |
1527 | * | |
1528 | * Returns: A pointer to a protocol-specific data block for a connecter | |
1529 | * | |
1530 | * Use: Creates a data block for a target. This is attached to the | |
1531 | * target data structure. Options can then be requested, and | |
1532 | * are added to the block when necessary. | |
1533 | */ | |
1534 | ||
1535 | addr_opts *(*inittargopts)(void); | |
1536 | ||
1537 | /* --- @freetargopts@ --- * | |
1538 | * | |
1539 | * Arguments: @addr_opts *ao@ = data block to remove | |
1540 | * | |
1541 | * Returns: --- | |
1542 | * | |
1543 | * Use: Throws away all the configuration data for an address type. | |
1544 | */ | |
1545 | ||
1546 | void (*freetargopts)(addr_opts */*ao*/); | |
1547 | ||
1548 | /* --- @connect@ --- * | |
1549 | * | |
1550 | * Arguments: @addr *a@ = destination address | |
1551 | * @addr_opts *ao@ = target address options | |
1552 | * @conn *c@ = connection structure | |
1553 | * @endpt *e@ = endpoint structure | |
1554 | * | |
1555 | * Returns: Zero if OK, @-1@ on some error. | |
1556 | * | |
1557 | * Use: Requests that a connection be made, or at least set in | |
1558 | * motion. An address may do one of these things: | |
1559 | * | |
1560 | * * Return @-1@. | |
1561 | * | |
1562 | * * Call @starget_connected@ with @-1@ or a connected file | |
1563 | * descriptor and the pointer @e@. | |
1564 | * | |
1565 | * * Call @conn_init@ or @conn_fd@, giving @starget_connected@ | |
1566 | * and @e@ as the function to call. | |
1567 | */ | |
1568 | ||
1569 | int (*connect)(addr */*a*/, addr_opts */*ao*/, conn */*c*/, endpt */*e*/); | |
1570 | ||
1571 | } addr_ops; | |
1572 | ||
1573 | /* --- Address types --- */ | |
1574 | ||
1575 | extern addr_ops un_ops; | |
1576 | extern addr_ops inet_ops; | |
1577 | ||
e82f7154 | 1578 | /*----- That's all, folks -------------------------------------------------*/ |
1579 | ||
1580 | #ifdef __cplusplus | |
1581 | } | |
1582 | #endif | |
1583 | ||
1584 | #endif |