5868fdae799d46f6a25ff0bf85785bd735387fb9
3 * Main header file for port forwarder
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
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.
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.
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.
34 /*----- Header files ------------------------------------------------------*/
36 /* --- Configuration --- */
62 #include <sys/types.h>
68 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
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>
87 #include <mLib/fdflags.h>
88 #include <mLib/fdpass.h>
89 #include <mLib/ident.h>
90 #include <mLib/mdup.h>
91 #include <mLib/mdwopt.h>
92 #include <mLib/quis.h>
93 #include <mLib/report.h>
95 #include <mLib/selbuf.h>
102 /*----- Other subtleties --------------------------------------------------*/
104 #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON
105 extern char **environ
;
108 /*----- Resource limit names ----------------------------------------------*/
110 #if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
111 # define RLIMIT_NOFILE RLIMIT_OFILE
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.
119 (let ((limits '(as core cpu data fsize locks memlock msgqueue
120 nice nofile nproc rss rtprio sigpending stack
125 (search-forward (concat "***" "BEGIN rlimitlist" "***"))
126 (beginning-of-line 2)
127 (delete-region (point)
129 (search-forward "***END***")
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")))
142 (insert-before-markers (format (concat "#ifdef %s\n"
149 (insert-before-markers
150 (format " \\\n MAYBE_ITEM(_, %s, (%s, %s))"
151 have name constant))))
156 /***BEGIN rlimitlist***/
158 # define HAVE_RLIMIT_AS_P t
160 # define HAVE_RLIMIT_AS_P nil
163 # define HAVE_RLIMIT_CORE_P t
165 # define HAVE_RLIMIT_CORE_P nil
168 # define HAVE_RLIMIT_CPU_P t
170 # define HAVE_RLIMIT_CPU_P nil
173 # define HAVE_RLIMIT_DATA_P t
175 # define HAVE_RLIMIT_DATA_P nil
178 # define HAVE_RLIMIT_FSIZE_P t
180 # define HAVE_RLIMIT_FSIZE_P nil
183 # define HAVE_RLIMIT_LOCKS_P t
185 # define HAVE_RLIMIT_LOCKS_P nil
187 #ifdef RLIMIT_MEMLOCK
188 # define HAVE_RLIMIT_MEMLOCK_P t
190 # define HAVE_RLIMIT_MEMLOCK_P nil
192 #ifdef RLIMIT_MSGQUEUE
193 # define HAVE_RLIMIT_MSGQUEUE_P t
195 # define HAVE_RLIMIT_MSGQUEUE_P nil
198 # define HAVE_RLIMIT_NICE_P t
200 # define HAVE_RLIMIT_NICE_P nil
203 # define HAVE_RLIMIT_NOFILE_P t
205 # define HAVE_RLIMIT_NOFILE_P nil
208 # define HAVE_RLIMIT_NPROC_P t
210 # define HAVE_RLIMIT_NPROC_P nil
213 # define HAVE_RLIMIT_RSS_P t
215 # define HAVE_RLIMIT_RSS_P nil
218 # define HAVE_RLIMIT_RTPRIO_P t
220 # define HAVE_RLIMIT_RTPRIO_P nil
222 #ifdef RLIMIT_SIGPENDING
223 # define HAVE_RLIMIT_SIGPENDING_P t
225 # define HAVE_RLIMIT_SIGPENDING_P nil
228 # define HAVE_RLIMIT_STACK_P t
230 # define HAVE_RLIMIT_STACK_P nil
233 # define HAVE_RLIMIT_VMEM_P t
235 # define HAVE_RLIMIT_VMEM_P nil
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))
256 /* --- The unpleasant conditional-output machinery --- */
258 #define MAYBE_ITEM(_, emitp, args) GLUE(MAYBE_ITEM_, emitp)(_, args)
259 #define MAYBE_ITEM_t(_, args) _ args
260 #define MAYBE_ITEM_nil(_, args)
262 /*----- Main program ------------------------------------------------------*/
264 /* --- The global select state --- */
266 extern sel_state
*sel
;
268 /* --- Help text --- */
270 extern const char grammar_text
[];
271 extern const char option_text
[];
273 /* --- @fw_log@ --- *
275 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
276 * @const char *fmt@ = format string to fill in
277 * @...@ = other arguments
281 * Use: Logs a connection.
284 extern void fw_log(time_t /*t*/, const char */
*fmt*/
, ...);
286 /* --- @fw_inc@, @fw_dec@ --- *
292 * Use: Increments or decrements the active thing count. `fwd' won't
293 * quit while there are active things.
296 extern void fw_inc(void);
297 extern void fw_dec(void);
299 /*----- Channel management ------------------------------------------------*/
301 /* --- Magic numbers --- */
303 #define CHAN_BUFSZ 4096
305 /* --- Channel structure --- */
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 */
311 int err
; /* What's wrong with the channel */
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 */
317 #define CHANF_CLOSE 1u /* Close channel when buffer empty */
318 #define CHANF_READY 2u /* The channel destination exists */
320 /* --- @chan_close@ --- *
322 * Arguments: @chan *c@ = pointer to channel
326 * Use: Closes down a channel prematurely.
329 extern void chan_close(chan */
*c*/
);
331 /* --- @chan_dest@ --- *
333 * Arguments: @chan *c@ = pointer to channel
334 * @int fd@ = destination file descriptor for channel
338 * Use: Sets the channel's destination so it knows where to put
342 extern void chan_dest(chan */
*c*/
, int /*fd*/);
344 /* --- @chan_open@ --- *
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
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.
358 extern void chan_open(chan */
*c*/
, int /*from*/, int /*to*/,
359 void (*/
*func*/
)(void */
*p*/
), void */
*p*/
);
361 /*----- Character scanners ------------------------------------------------*/
363 /* --- A low-level scanner source --- */
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 */
375 /* --- Scanner source operations --- */
377 typedef struct scansrc_ops
{
378 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
379 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
382 /* --- A character scanner --- */
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 */
391 /* --- @scan_file@ --- *
393 * Arguments: @FILE *fp@ = pointer to file descriptor
394 * @const char *name@ = pointer to source file name
395 * @unsigned f@ = flags
397 * Returns: A scanner source.
399 * Use: Creates a new scanner source for reading from a file.
402 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
404 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
407 /* --- @scan_argv@ --- *
409 * Arguments: @char **av@ = pointer to argument array (null terminated)
411 * Returns: A scanner source.
413 * Use: Creates a new scanner source for reading from an @argv@
417 extern scansrc
*scan_argv(char **/
*av*/
);
421 * Arguments: @scanner *sc@ = pointer to main scanner context
423 * Returns: Character read, or end-of-file.
425 * Use: Scans a character from a source of characters.
428 extern int scan(scanner */
*sc*/
);
430 /* --- @unscan@ --- *
432 * Arguments: @scanner *sc@ = pointer to main scanner context
433 * @int ch@ = character to unscan
437 * Use: Scans a character from a source of characters.
440 extern void unscan(scanner */
*sc*/
, int /*ch*/);
442 /* --- @scan_push@ --- *
444 * Arguments: @scanner *sc@ = pointer to main scanner context
445 * @scansrc *ss@ = souorce to push
449 * Use: Pushes a scanner source onto the front of the queue.
452 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
454 /* --- @scan_add@ --- *
456 * Arguments: @scanner *sc@ = pointer to main scanner context
457 * @scansrc *ss@ = souorce to push
461 * Use: Adds a scanner source onto the end of the queue.
464 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
466 /* --- @scan_create@ --- *
468 * Arguments: @scanner *sc@ = scanner context to initialize
472 * Use: Initializes a scanner block ready for use.
475 extern void scan_create(scanner */
*sc*/
);
477 /* --- @scan_destroy@ --- *
479 * Arguments: @scanner *sc@ = pointer to scanner context
483 * Use: Destroys a scanner and all the sources attached to it.
486 extern void scan_destroy(scanner */
*sc*/
);
488 /*----- Configuration parsing ---------------------------------------------*/
490 /* --- Magical constants --- */
492 #define CTOK_EOF (-1)
493 #define CTOK_WORD 256
495 /* --- @conf_undelim@ --- *
497 * Arguments: @scanner *sc@ = pointer to scanner definition
498 * @const char *d, *dd@ = pointer to characters to escape
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.
507 extern void conf_undelim(scanner */
*sc*/
,
508 const char */
*d*/
, const char */
*dd*/
);
512 * Arguments: @scanner *sc@ = pointer to scanner definition
514 * Returns: Type of token scanned.
516 * Use: Reads the next token from the character scanner.
519 extern int token(scanner */
*sc*/
);
523 * Arguments: @scanner *sc@ = pointer to scanner definition
524 * @const char *msg@ = message skeleton string
525 * @...@ = extra arguments for the skeleton
529 * Use: Reports an error at the current scanner location.
532 extern void error(scanner */
*sc*/
, const char */
*msg*/
, ...);
534 /* --- @pushback@ --- *
536 * Arguments: @scanner *sc@ = pointer to scanner definition
540 * Use: Pushes the current token back. This is normally a precursor
541 * to pushing a new scanner source.
544 extern void pushback(scanner */
*sc*/
);
546 /* --- @conf_enum@ --- *
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
553 * Returns: Index into list, zero-based, or @-1@.
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.
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
567 #define ENUM_ABBREV 1u
570 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
571 unsigned /*flags*/, const char */
*err*/
);
573 /* --- @conf_prefix@ --- *
575 * Arguments: @scanner *sc@ = pointer to a scanner object
576 * @const char *p@ = pointer to prefix string to check
578 * Returns: Nonzero if the prefix matches.
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.
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.
592 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
594 /* --- @CONF_BEGIN@, @CONF_END@ --- *
596 * Arguments: @sc@ = scanner to read from
597 * @prefix@ = prefix to scan for
598 * @desc@ = description of what we're parsing
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
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; \
621 /* --- Read the initial prefix --- */ \
623 if (_conf_sc->t == CTOK_WORD && \
624 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
626 _conf_state = CS_PREFIX; \
627 if (_conf_sc->t == '.') \
629 else if (_conf_sc->t == '{') { \
631 _conf_state = CS_BRACE; \
635 /* --- Ensure the next token is a word --- */ \
637 if (_conf_sc->t != CTOK_WORD) \
638 error(_conf_sc, "parse error, expected option keyword"); \
643 /* --- Reject an option --- * \
645 * We could get here as a result of an explicit @CONF_REJECT@ or \
646 * because the option wasn't accepted. \
651 if (_conf_state == CS_PLAIN) \
652 _conf_state = CS_UNKNOWN; \
654 error(_conf_sc, "unknown %s option `%s'", \
655 _conf_desc, _conf_sc->d.buf); \
658 /* --- Accept an option --- * \
660 * It's safe to drop through from above. Either an error will have \
661 * been reported, or the state is not @CS_BRACE@. \
665 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
667 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
669 /* --- Check for a closing brace --- */ \
671 if (_conf_state == CS_BRACE) { \
672 if (_conf_sc->t == '}') \
675 error(_conf_sc, "parse error, expected `}'"); \
678 /* --- Return an appropriate value --- */ \
680 return (_conf_state != CS_UNKNOWN); \
683 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
687 * Use: Within an options parser (between @CONF_BEGIN@ and
688 * @CONF_END@), accept or reject an option.
691 #define CONF_ACCEPT goto _conf_accept
692 #define CONF_REJECT goto _conf_reject
694 /* --- @CONF_QUAL@ --- *
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.
703 #define CONF_QUAL (_conf_state != CS_PLAIN)
705 /* --- @conf_name@ --- *
707 * Arguments: @scanner *sc@ = pointer to scanner
708 * @char delim@ = delimiter character to look for
709 * @dstr *d@ = pointer to dynamic string for output
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.
718 * Examples of compound names are filenames (delimited by `/')
719 * and IP addresses (delimited by `.').
722 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
724 /* --- @conf_fname@ --- *
726 * Arguments: @scanner *sc@ = pointer to scanner
727 * @dstr *d@ = pointer to dynamic string for output
731 * Use: Reads a file name from the input and stores it in @d@.
734 extern void conf_fname(scanner */
*sc*/
, dstr */
*d*/
);
736 /*----- Reference-counted file descriptors --------------------------------*/
738 typedef struct reffd
{
741 void (*proc
)(void */
*p*/
);
745 /* --- @reffd_init@ --- *
747 * Arguments: @int fd@ = file descriptor
749 * Returns: Reference-counted file descriptor object.
751 * Use: Creates a refcounted file descriptor.
754 extern reffd
*reffd_init(int /*fd*/);
756 /* --- @reffd_handler@ --- *
758 * Arguments: @reffd *r@ = pointer to reference counted filehandle
759 * @void (*proc)(void *p)@ = procedure to call
764 * Use: Sets the reference counted file descriptor to call @proc@
765 * when it is no longer required.
768 extern void reffd_handler(reffd */
*r*/
, void (*/
*proc*/
)(void */
*p*/
),
771 /* --- @reffd_inc@ --- *
773 * Arguments: @reffd *r@ = pointer to reference counted filehandle
777 * Use: Increments the reference count for a file descriptor.
780 #define REFFD_INC(r) do { (r)->ref++; } while (0)
782 extern void reffd_inc(reffd */
*r*/
);
784 /* --- @reffd_dec@ --- *
786 * Arguments: @reffd *r@ = pointer to reference counted filehandle
790 * Use: Decrements the reference count for a file descriptor.
793 #define REFFD_DEC(r) do { \
796 if (_r->ref == 0) { \
804 extern void reffd_dec(reffd */
*r*/
);
806 /*----- Sources, targets and endpoints ------------------------------------*/
808 /* --- Basic endpoint structure --- */
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 */
818 /* --- Endpoint flags --- */
820 #define EPF_PENDING 1u /* Endpoint creation in progress */
821 #define EPF_FILE 2u /* Endpoint smells like a file */
823 /* --- Endpoint operations table --- */
825 typedef struct endpt_ops
{
827 /* --- @attach@ --- *
829 * Arguments: @endpt *e@ = pointer to endpoint to be attached
830 * @reffd *in, *out@ = input and output file descriptors
834 * Use: Instructs a non-file endpoint to attach itself to a pair of
838 void (*attach
)(endpt */
*e*/
, reffd */
*in*/
, reffd */
*out*/
);
842 * Arguments: @endpt *e@ = pointer to endpoint in question
843 * @endpt *f@ = pointer to a file endpoint
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.
854 void (*file
)(endpt */
*e*/
, endpt */
*f*/
);
856 /* --- @wclose@ --- *
858 * Arguments: @endpt *e@ = endpoint to be partially closed
862 * Use: Announces that the endpoint will not be written to any more.
865 void (*wclose
)(endpt */
*e*/
);
869 * Arguments: @endpt *e@ = endpoint to be closed
873 * Use: Completely closes an endpoint. The endpoint's data may be
874 * freed, although some endpoints may wish to delay freeing for
878 void (*close
)(endpt */
*e*/
);
882 /* --- A basic target object --- */
884 typedef struct target
{
885 struct target_ops
*ops
;
889 /* --- Forwarding target operations --- */
891 typedef struct target_ops
{
892 const char *name
; /* Name of this target */
894 /* --- @option@ --- *
896 * Arguments: @target *t@ = pointer to target object, or zero if global
897 * @scanner *sc@ = scanner to read from
899 * Returns: Nonzero to claim the option.
901 * Use: Handles an option string from the configuration file.
904 int (*option
)(target */
*t*/
, scanner */
*sc*/
);
908 * Arguments: @scanner *sc@ = pointer to scanner to read from
910 * Returns: Pointer to a target object to claim, null to reject.
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.
917 target
*(*read
)(scanner */
*sc*/
);
919 /* --- @confirm@ --- *
921 * Arguments: @target *t@ = pointer to target
925 * Use: Confirms configuration of a target.
928 void (*confirm
)(target */
*t*/
);
930 /* --- @create@ --- *
932 * Arguments: @target *t@ = pointer to target
933 * @const char *desc@ = description of connection
935 * Returns: Pointer to a created endpoint.
937 * Use: Generates a target endpoint for communication.
940 endpt
*(*create
)(target */
*t*/
, const char */
*desc*/
);
942 /* --- @destroy@ --- *
944 * Arguments: @target *t@ = pointer to target
948 * Use: Destroys a target.
951 void (*destroy
)(target */
*t*/
);
955 /* --- A basic source object --- */
957 typedef struct source
{
958 struct source
*next
, *prev
;
959 struct source_ops
*ops
;
963 /* --- Forwarding source operations --- */
965 typedef struct source_ops
{
966 const char *name
; /* Name of this source */
968 /* --- @option@ --- *
970 * Arguments: @scanner *sc@ = scanner to read from
971 * @source *s@ = pointer to source object, or zero if global
973 * Returns: Nonzero to claim the option.
975 * Use: Handles an option string from the configuration file.
978 int (*option
)(source */
*s*/
, scanner */
*sc*/
);
982 * Arguments: @scanner *sc@ = pointer to scanner to read from
984 * Returns: Pointer to a source object to claim, null to reject.
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.
991 source
*(*read
)(scanner */
*sc*/
);
993 /* --- @attach@ --- *
995 * Arguments: @source *s@ = pointer to source
996 * @scanner *sc@ = scanner (for error reporting)
997 * @target *t@ = pointer to target to attach
1001 * Use: Attaches a target to a source.
1004 void (*attach
)(source */
*s*/
, scanner */
*sc*/
, target */
*t*/
);
1006 /* --- @destroy@ --- *
1008 * Arguments: @source *s@ = pointer to source
1012 * Use: Destroys a source. Used when closing the system down, for
1013 * example as a result of a signal.
1016 void (*destroy
)(source */
*s*/
);
1020 /* --- @endpt_kill@ --- *
1022 * Arguments: @endpt *a@ = an endpoint
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).
1031 extern void endpt_kill(endpt */
*a*/
);
1033 /* --- @endpt_killall@ --- *
1039 * Use: Destroys all current endpoint connections. Used when
1043 extern void endpt_killall(void);
1045 /* --- @endpt_join@ --- *
1047 * Arguments: @endpt *a@ = pointer to first endpoint
1048 * @endpt *b@ = pointer to second endpoint
1049 * @const char *desc@ = description of connection
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.
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.
1063 extern void endpt_join(endpt */
*a*/
, endpt */
*b*/
, const char */
*desc*/
);
1065 /* --- @source_add@ --- *
1067 * Arguments: @source *s@ = pointer to a source
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).
1076 extern void source_add(source */
*s*/
);
1078 /* --- @source_remove@ --- *
1080 * Arguments: @source *s@ = pointer to a source
1084 * Use: Removes a source from the master list.
1087 extern void source_remove(source */
*s*/
);
1089 /* --- @source_killall@ --- *
1095 * Use: Frees all sources.
1098 extern void source_killall(void);
1100 /*----- The exec source and target ----------------------------------------*/
1102 extern source_ops xsource_ops
;
1103 extern target_ops xtarget_ops
;
1105 /* --- @exec_init@ --- *
1111 * Use: Initializes the executable problem source and target.
1114 extern void exec_init(void);
1116 /*----- The file source and target ----------------------------------------*/
1118 extern source_ops fsource_ops
;
1119 extern target_ops ftarget_ops
;
1121 /*----- The socket source and target --------------------------------------*/
1123 extern source_ops ssource_ops
;
1124 extern target_ops starget_ops
;
1126 /* --- @starget_connected@ --- *
1128 * Arguments: @int fd@ = file descriptor now ready for use
1129 * @void *p@ = pointer to an endpoint structure
1133 * Use: Handles successful connection of the target endpoint.
1136 extern void starget_connected(int /*fd*/, void */
*p*/
);
1138 /*----- Handling of file attributes ---------------------------------------*/
1140 /* --- File attribute options structure --- */
1142 typedef struct fattr
{
1148 /* --- Shared global options --- */
1150 extern fattr fattr_global
;
1152 /* --- @fattr_init@ --- *
1154 * Arguments: @fattr *f@ = pointer to file attributes
1158 * Use: Initializes a set of file attributes to default values.
1161 extern void fattr_init(fattr */
*f*/
);
1163 /* --- @fattr_option@ --- *
1165 * Arguments: @scanner *sc@ = pointer to scanner to read
1166 * @fattr *f@ = pointer to file attributes to set
1168 * Returns: Whether the option was clamed.
1170 * Use: Reads file attributes from a scanner.
1173 extern int fattr_option(scanner */
*sc*/
, fattr */
*f*/
);
1175 /* --- @fattr_apply@ --- *
1177 * Arguments: @const char *file@ = pointer to filename
1178 * @fattr *f@ = pointer to attribute set
1180 * Returns: @-1@ if it failed.
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.
1188 extern int fattr_apply(const char */
*file*/
, fattr */
*f*/
);
1190 /*----- Making privileged connections -------------------------------------*/
1192 /* --- @privconn_split@ --- *
1194 * Arguments: @sel_state *s@ = select state
1198 * Use: Splits off the privileged binding code into a separate
1202 extern void privconn_split(sel_state */
*s*/
);
1204 /* --- @privconn_adddest@ --- *
1206 * Arguments: @struct in_addr peer@ = address to connect to
1207 * @unsigned port@ = port to connect to
1209 * Returns: Index for this destination address, or @-1@ if not
1212 * Use: Adds a valid destination for a privileged connection.
1215 extern int privconn_adddest(struct in_addr
/*peer*/, unsigned /*port*/);
1217 /* --- @privconn_connect@ --- *
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
1226 * Returns: Zero on success, @-1@ on failure.
1228 * Use: Sets up a privileged connection job.
1231 extern int privconn_connect(conn */
*c*/
, sel_state */
*s*/
,
1232 int /*i*/, struct in_addr
/*bind*/,
1233 void (*/
*func*/
)(int, void *), void */
*p*/
);
1235 /*----- Identifying remote clients ----------------------------------------*/
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 */
1245 /* --- @identify@ --- *
1247 * Arguments: @const id_req *q@ = pointer to request block
1251 * Use: Starts a background ident lookup and reverse-resolve job
1252 * which will, eventually, report a message to the system log.
1255 extern void identify(const id_req */
*q*/
);
1257 /*----- Host-based access control -----------------------------------------*/
1259 /* --- An access control entry --- */
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 */
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 */
1271 /* --- Host-based access control --- */
1273 typedef struct acl_host
{
1274 acl_entry a
; /* Base structure */
1275 struct in_addr addr
, mask
; /* Address and netmask */
1278 /* --- ACL methods --- */
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*/
);
1286 /* --- @acl_check@ --- *
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@)
1293 * Returns: Zero if undecided, nonzero if a rule matched.
1295 * Use: Checks an address against an ACL.
1298 extern int acl_check(acl_entry */
*a*/
,
1299 struct in_addr
/*addr*/, unsigned /*port*/,
1302 /* --- @acl_dump@ --- *
1304 * Arguments: @acl_entry *a@ = pointer to ACL to dump
1305 * @FILE *fp@ = pointer to stream to dump on
1309 * Use: Dumps an access control list to an output stream.
1312 extern void acl_dump(acl_entry */
*a*/
, FILE */
*fp*/
);
1314 /* --- @acl_free@ --- *
1316 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
1320 * Use: Frees all of the memory used by an ACL.
1323 extern void acl_free(acl_entry */
*a*/
);
1325 /* --- @acl_addhost@ --- *
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
1333 * Use: Adds a host-authentication entry to the end of an access
1337 extern void acl_addhost(acl_entry
***/
*a*/
, unsigned /*act*/,
1338 struct in_addr
/*addr*/, struct in_addr
/*mask*/);
1340 /* --- @acl_addpriv@ --- *
1342 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1343 * @unsigned act@ = what to do with matching addresses
1347 * Use: Adds a privileged-port check to the end of an access control
1351 extern void acl_addpriv(acl_entry
***/
*a*/
, unsigned /*act*/);
1353 /*----- Network addresses -------------------------------------------------*/
1355 /* --- A generic socket address --- *
1357 * Not all systems understand @sa_len@ fields. (In particular, Linux
1358 * doesn't.) Some fairly ugly hacking is then performed on particular
1362 typedef struct addr
{
1363 struct addr_ops
*ops
;
1367 #define ADDRSZ(sz) (sizeof(addr) + (sz))
1369 /* --- Address configuration --- *
1371 * An address family will want to extend this.
1374 typedef struct addr_opts
{
1378 #define ADDRF_NOLOG 1u
1380 /* --- Address types --- *
1382 * For things like Internet addresses, source and destinations look
1392 /* --- Description of an address type handler --- */
1394 typedef struct addr_ops
{
1395 const char *name
; /* Protocol's internal name */
1399 * Arguments: @scanner *sc@ = pointer to scanner to read from
1400 * @unsigned type@ = type of address to be read
1402 * Returns: A filled-in socket address.
1404 * Use: Parses a textual representation of a socket address.
1407 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
1409 /* --- @destroy@ --- *
1411 * Arguments: @addr *a@ = pointer to an address block
1415 * Use: Disposes of an address block in some suitable fashion.
1418 void (*destroy
)(addr */
*a*/
);
1420 /* --- @print@ --- *
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
1428 * Use: Writes a textual representation of a socket address to
1432 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
1434 /* --- @initsrcopts@ --- *
1438 * Returns: A pointer to a protocol-specific data block for a listener
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.
1445 addr_opts
*(*initsrcopts
)(void);
1447 /* --- @option@ --- *
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
1453 * Returns: Nonzero to claim the option.
1455 * Use: Parses a source option, either global or listener-specific.
1458 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
1460 /* --- @confirm@ --- *
1462 * Arguments: @addr *a@ = pointer to an address structure
1463 * @unsigned type@ = kind of address this is
1464 * @addr_opts *ao@ = address options
1468 * Use: Called during initialization when an address is fully
1472 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
1474 /* --- @freesrcopts@ --- *
1476 * Arguments: @addr_opts *ao@ = data block to remove
1480 * Use: Throws away all the configuration data for an address type.
1483 void (*freesrcopts
)(addr_opts */
*ao*/
);
1487 * Arguments: @addr *a@ = the address to bind to
1488 * @addr_opts *ao@ = the address options
1490 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
1492 * Use: Binds a listening socket. The tedious stuff with @listen@
1496 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
1498 /* --- @unbind@ --- *
1500 * Arguments: @addr *a@ = pointer to an address
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.
1509 void (*unbind
)(addr */
*a*/
);
1511 /* --- @accept@ --- *
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
1517 * Returns: Pointer to a reference counted file descriptor.
1519 * Use: Accepts, verifies and logs an incoming connection.
1522 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
1524 /* --- @inittargopts@ --- *
1528 * Returns: A pointer to a protocol-specific data block for a connecter
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.
1535 addr_opts
*(*inittargopts
)(void);
1537 /* --- @freetargopts@ --- *
1539 * Arguments: @addr_opts *ao@ = data block to remove
1543 * Use: Throws away all the configuration data for an address type.
1546 void (*freetargopts
)(addr_opts */
*ao*/
);
1548 /* --- @connect@ --- *
1550 * Arguments: @addr *a@ = destination address
1551 * @addr_opts *ao@ = target address options
1552 * @conn *c@ = connection structure
1553 * @endpt *e@ = endpoint structure
1555 * Returns: Zero if OK, @-1@ on some error.
1557 * Use: Requests that a connection be made, or at least set in
1558 * motion. An address may do one of these things:
1562 * * Call @starget_connected@ with @-1@ or a connected file
1563 * descriptor and the pointer @e@.
1565 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
1566 * and @e@ as the function to call.
1569 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
1573 /* --- Address types --- */
1575 extern addr_ops un_ops
;
1576 extern addr_ops inet_ops
;
1578 /*----- That's all, folks -------------------------------------------------*/