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