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/macros.h>
91 #include <mLib/mdup.h>
92 #include <mLib/mdwopt.h>
93 #include <mLib/quis.h>
94 #include <mLib/report.h>
96 #include <mLib/selbuf.h>
100 #include <mLib/sym.h>
103 /*----- Other subtleties --------------------------------------------------*/
105 #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON
106 extern char **environ
;
109 /*----- Resource limit names ----------------------------------------------*/
111 #if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
112 # define RLIMIT_NOFILE RLIMIT_OFILE
116 ;;; The resource-limit name table is very boring to type and less fun to
117 ;;; maintain. To make life less awful, put the names in this list and
118 ;;; evaluate the code to get Emacs to regenerate it.
120 (let ((limits '(as core cpu data fsize locks memlock msgqueue
121 nice nofile nproc rss rtprio sigpending stack
126 (search-forward (concat "***" "BEGIN rlimitlist" "***"))
127 (beginning-of-line 2)
128 (delete-region (point)
130 (search-forward "***END***")
133 (let ((avail (make-marker))
134 (list (make-marker)))
135 (set-marker avail (point))
136 (insert "#define RLIMITS(_)")
137 (set-marker list (point))
138 (dolist (limit (sort (copy-list limits) #'string<))
139 (let* ((name (symbol-name limit))
140 (constant (concat "RLIMIT_" (upcase name)))
141 (have (concat "HAVE_" constant "_P")))
143 (insert-before-markers (format (concat "#ifdef %s\n"
150 (insert-before-markers
151 (format " \\\n MAYBE_ITEM(_, %s, (%s, %s))"
152 have name constant))))
157 /***BEGIN rlimitlist***/
159 # define HAVE_RLIMIT_AS_P t
161 # define HAVE_RLIMIT_AS_P nil
164 # define HAVE_RLIMIT_CORE_P t
166 # define HAVE_RLIMIT_CORE_P nil
169 # define HAVE_RLIMIT_CPU_P t
171 # define HAVE_RLIMIT_CPU_P nil
174 # define HAVE_RLIMIT_DATA_P t
176 # define HAVE_RLIMIT_DATA_P nil
179 # define HAVE_RLIMIT_FSIZE_P t
181 # define HAVE_RLIMIT_FSIZE_P nil
184 # define HAVE_RLIMIT_LOCKS_P t
186 # define HAVE_RLIMIT_LOCKS_P nil
188 #ifdef RLIMIT_MEMLOCK
189 # define HAVE_RLIMIT_MEMLOCK_P t
191 # define HAVE_RLIMIT_MEMLOCK_P nil
193 #ifdef RLIMIT_MSGQUEUE
194 # define HAVE_RLIMIT_MSGQUEUE_P t
196 # define HAVE_RLIMIT_MSGQUEUE_P nil
199 # define HAVE_RLIMIT_NICE_P t
201 # define HAVE_RLIMIT_NICE_P nil
204 # define HAVE_RLIMIT_NOFILE_P t
206 # define HAVE_RLIMIT_NOFILE_P nil
209 # define HAVE_RLIMIT_NPROC_P t
211 # define HAVE_RLIMIT_NPROC_P nil
214 # define HAVE_RLIMIT_RSS_P t
216 # define HAVE_RLIMIT_RSS_P nil
219 # define HAVE_RLIMIT_RTPRIO_P t
221 # define HAVE_RLIMIT_RTPRIO_P nil
223 #ifdef RLIMIT_SIGPENDING
224 # define HAVE_RLIMIT_SIGPENDING_P t
226 # define HAVE_RLIMIT_SIGPENDING_P nil
229 # define HAVE_RLIMIT_STACK_P t
231 # define HAVE_RLIMIT_STACK_P nil
234 # define HAVE_RLIMIT_VMEM_P t
236 # define HAVE_RLIMIT_VMEM_P nil
239 MAYBE_ITEM(_, HAVE_RLIMIT_AS_P, (as, RLIMIT_AS)) \
240 MAYBE_ITEM(_, HAVE_RLIMIT_CORE_P, (core, RLIMIT_CORE)) \
241 MAYBE_ITEM(_, HAVE_RLIMIT_CPU_P, (cpu, RLIMIT_CPU)) \
242 MAYBE_ITEM(_, HAVE_RLIMIT_DATA_P, (data, RLIMIT_DATA)) \
243 MAYBE_ITEM(_, HAVE_RLIMIT_FSIZE_P, (fsize, RLIMIT_FSIZE)) \
244 MAYBE_ITEM(_, HAVE_RLIMIT_LOCKS_P, (locks, RLIMIT_LOCKS)) \
245 MAYBE_ITEM(_, HAVE_RLIMIT_MEMLOCK_P, (memlock, RLIMIT_MEMLOCK)) \
246 MAYBE_ITEM(_, HAVE_RLIMIT_MSGQUEUE_P, (msgqueue, RLIMIT_MSGQUEUE)) \
247 MAYBE_ITEM(_, HAVE_RLIMIT_NICE_P, (nice, RLIMIT_NICE)) \
248 MAYBE_ITEM(_, HAVE_RLIMIT_NOFILE_P, (nofile, RLIMIT_NOFILE)) \
249 MAYBE_ITEM(_, HAVE_RLIMIT_NPROC_P, (nproc, RLIMIT_NPROC)) \
250 MAYBE_ITEM(_, HAVE_RLIMIT_RSS_P, (rss, RLIMIT_RSS)) \
251 MAYBE_ITEM(_, HAVE_RLIMIT_RTPRIO_P, (rtprio, RLIMIT_RTPRIO)) \
252 MAYBE_ITEM(_, HAVE_RLIMIT_SIGPENDING_P, (sigpending, RLIMIT_SIGPENDING)) \
253 MAYBE_ITEM(_, HAVE_RLIMIT_STACK_P, (stack, RLIMIT_STACK)) \
254 MAYBE_ITEM(_, HAVE_RLIMIT_VMEM_P, (vmem, RLIMIT_VMEM))
257 /* --- The unpleasant conditional-output machinery --- */
259 #define MAYBE_ITEM(_, emitp, args) GLUE(MAYBE_ITEM_, emitp)(_, args)
260 #define MAYBE_ITEM_t(_, args) _ args
261 #define MAYBE_ITEM_nil(_, args)
263 /*----- Main program ------------------------------------------------------*/
265 /* --- The global select state --- */
267 extern sel_state
*sel
;
269 /* --- Help text --- */
271 extern const char grammar_text
[];
272 extern const char option_text
[];
274 /* --- Generally useful magic constants --- */
276 #define NOW ((time_t)-1)
278 /* --- @fw_log@ --- *
280 * Arguments: @time_t t@ = when the connection occurred or (@NOW@)
281 * @const char *fmt@ = format string to fill in
282 * @...@ = other arguments
286 * Use: Logs a connection.
289 extern void PRINTF_LIKE(2, 3)
290 fw_log(time_t /*t*/, const char */
*fmt*/
, ...);
292 /* --- @fw_inc@, @fw_dec@ --- *
298 * Use: Increments or decrements the active thing count. `fwd' won't
299 * quit while there are active things.
302 extern void fw_inc(void);
303 extern void fw_dec(void);
305 /*----- Channel management ------------------------------------------------*/
307 /* --- Magic numbers --- */
309 #define CHAN_BUFSZ 4096
311 /* --- Channel structure --- */
313 typedef struct chan
{
314 unsigned base
, len
; /* Base and length of data */
315 unsigned f
; /* Various interesting flags */
316 void (*func
)(void */
*p*/
); /* Function to call on closure */
317 int err
; /* What's wrong with the channel */
318 void *p
; /* Argument to pass function */
319 sel_file r
, w
; /* Reader and writer selectors */
320 char buf
[CHAN_BUFSZ
]; /* The actual data buffer */
323 #define CHANF_CLOSE 1u /* Close channel when buffer empty */
324 #define CHANF_READY 2u /* The channel destination exists */
326 /* --- @chan_close@ --- *
328 * Arguments: @chan *c@ = pointer to channel
332 * Use: Closes down a channel prematurely.
335 extern void chan_close(chan */
*c*/
);
337 /* --- @chan_dest@ --- *
339 * Arguments: @chan *c@ = pointer to channel
340 * @int fd@ = destination file descriptor for channel
344 * Use: Sets the channel's destination so it knows where to put
348 extern void chan_dest(chan */
*c*/
, int /*fd*/);
350 /* --- @chan_open@ --- *
352 * Arguments: @chan *c@ = pointer to channel to open
353 * @int from, to@ = source and destination file descriptors
354 * @void (*func)(void *p)@ = function to call on closure
355 * @void *p@ = argument to pass to function
359 * Use: Opens a channel. Data is copied from the source to the
360 * destination. The @to@ argument may be @-1@ if the file
361 * descriptor isn't known yet.
364 extern void chan_open(chan */
*c*/
, int /*from*/, int /*to*/,
365 void (*/
*func*/
)(void */
*p*/
), void */
*p*/
);
367 /*----- Character scanners ------------------------------------------------*/
369 /* --- A low-level scanner source --- */
371 typedef struct scansrc
{
372 struct scansrc
*next
; /* Next one in the list */
373 struct scansrc_ops
*ops
; /* Pointer to operations table */
374 char *src
; /* Name of this source */
375 int line
; /* Current line number */
376 dstr pushback
; /* Pushback characters */
377 char *tok
; /* Token pushback */
378 unsigned t
; /* Token type pushback */
381 /* --- Scanner source operations --- */
383 typedef struct scansrc_ops
{
384 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
385 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
388 /* --- A character scanner --- */
390 typedef struct scanner
{
391 scansrc
*head
, **tail
; /* Scanner list head and tail */
392 int t
; /* Token type */
393 dstr d
; /* Current token value */
394 const char *wbegin
, *wcont
; /* Parsing exception strings */
397 /* --- @scan_file@ --- *
399 * Arguments: @FILE *fp@ = pointer to file descriptor
400 * @const char *name@ = pointer to source file name
401 * @unsigned f@ = flags
403 * Returns: A scanner source.
405 * Use: Creates a new scanner source for reading from a file.
408 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
410 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
413 /* --- @scan_argv@ --- *
415 * Arguments: @char **av@ = pointer to argument array (null terminated)
417 * Returns: A scanner source.
419 * Use: Creates a new scanner source for reading from an @argv@
423 extern scansrc
*scan_argv(char **/
*av*/
);
427 * Arguments: @scanner *sc@ = pointer to main scanner context
429 * Returns: Character read, or end-of-file.
431 * Use: Scans a character from a source of characters.
434 extern int scan(scanner */
*sc*/
);
436 /* --- @unscan@ --- *
438 * Arguments: @scanner *sc@ = pointer to main scanner context
439 * @int ch@ = character to unscan
443 * Use: Scans a character from a source of characters.
446 extern void unscan(scanner */
*sc*/
, int /*ch*/);
448 /* --- @scan_push@ --- *
450 * Arguments: @scanner *sc@ = pointer to main scanner context
451 * @scansrc *ss@ = souorce to push
455 * Use: Pushes a scanner source onto the front of the queue.
458 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
460 /* --- @scan_add@ --- *
462 * Arguments: @scanner *sc@ = pointer to main scanner context
463 * @scansrc *ss@ = souorce to push
467 * Use: Adds a scanner source onto the end of the queue.
470 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
472 /* --- @scan_create@ --- *
474 * Arguments: @scanner *sc@ = scanner context to initialize
478 * Use: Initializes a scanner block ready for use.
481 extern void scan_create(scanner */
*sc*/
);
483 /* --- @scan_destroy@ --- *
485 * Arguments: @scanner *sc@ = pointer to scanner context
489 * Use: Destroys a scanner and all the sources attached to it.
492 extern void scan_destroy(scanner */
*sc*/
);
494 /*----- Configuration parsing ---------------------------------------------*/
496 /* --- Magical constants --- */
498 #define CTOK_EOF (-1)
499 #define CTOK_WORD 256
501 /* --- @conf_undelim@ --- *
503 * Arguments: @scanner *sc@ = pointer to scanner definition
504 * @const char *d, *dd@ = pointer to characters to escape
508 * Use: Modifies the tokenizer. Characters in the first list will
509 * always be considered to begin a word. Characters in the
510 * second list will always be allowed to continue a word.
513 extern void conf_undelim(scanner */
*sc*/
,
514 const char */
*d*/
, const char */
*dd*/
);
518 * Arguments: @scanner *sc@ = pointer to scanner definition
520 * Returns: Type of token scanned.
522 * Use: Reads the next token from the character scanner.
525 extern int token(scanner */
*sc*/
);
529 * Arguments: @scanner *sc@ = pointer to scanner definition
530 * @const char *msg@ = message skeleton string
531 * @...@ = extra arguments for the skeleton
535 * Use: Reports an error at the current scanner location.
538 extern void PRINTF_LIKE(2, 3) NORETURN
539 error(scanner */
*sc*/
, const char */
*msg*/
, ...);
541 /* --- @pushback@ --- *
543 * Arguments: @scanner *sc@ = pointer to scanner definition
547 * Use: Pushes the current token back. This is normally a precursor
548 * to pushing a new scanner source.
551 extern void pushback(scanner */
*sc*/
);
553 /* --- @conf_enum@ --- *
555 * Arguments: @scanner *sc@ = pointer to a scanner object
556 * @const char *list@ = comma-separated things to allow
557 * @unsigned @f = flags for the search
558 * @const char *err@ = error message if not found
560 * Returns: Index into list, zero-based, or @-1@.
562 * Use: Checks whether the current token is a string which matches
563 * one of the comma-separated items given. The return value is
564 * the index (zero-based) of the matched string in the list.
566 * The flags control the behaviour if no exact match is found.
567 * If @ENUM_ABBREV@ is set, and the current token is a left
568 * substring of exactly one of the possibilities, then that one
569 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
570 * returned; otherwise an error is reported and the program is
574 #define ENUM_ABBREV 1u
577 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
578 unsigned /*flags*/, const char */
*err*/
);
580 /* --- @conf_prefix@ --- *
582 * Arguments: @scanner *sc@ = pointer to a scanner object
583 * @const char *p@ = pointer to prefix string to check
585 * Returns: Nonzero if the prefix matches.
587 * Use: If the current token is a word matching the given prefix
588 * string, then it and an optional `.' character are removed and
589 * a nonzero result is returned. Otherwise the current token is
590 * left as it is, and zero is returned.
592 * Typical options parsing code would remove an expected prefix,
593 * scan an option anyway (since qualifying prefixes are
594 * optional) and if a match is found, claim the option. If no
595 * match is found, and a prefix was stripped, then an error
596 * should be reported.
599 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
601 /* --- @CONF_BEGIN@, @CONF_END@ --- *
603 * Arguments: @sc@ = scanner to read from
604 * @prefix@ = prefix to scan for
605 * @desc@ = description of what we're parsing
607 * Use: Bracket an options parsing routine. The current token is
608 * checked to see whether it matches the prefix. If so, it is
609 * removed and the following token examined. If that's a `.'
610 * then it's removed. If it's a `{' then the enclosed
611 * option-parsing code is executed in a loop until a matching
612 * '}' is found. If the options parser doesn't accept an
613 * option, the behaviour is dependent on whether a prefix was
614 * seen: if so, an error is reported; otherwse a zero return is
623 #define CONF_BEGIN(sc, prefix, desc) do { \
624 scanner *_conf_sc = (sc); \
625 const char *_conf_desc = (desc); \
626 int _conf_state = CS_PLAIN; \
628 /* --- Read the initial prefix --- */ \
630 if (_conf_sc->t == CTOK_WORD && \
631 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
633 _conf_state = CS_PREFIX; \
634 if (_conf_sc->t == '.') \
636 else if (_conf_sc->t == '{') { \
638 _conf_state = CS_BRACE; \
642 /* --- Ensure the next token is a word --- */ \
644 if (_conf_sc->t != CTOK_WORD) \
645 error(_conf_sc, "parse error, expected option keyword"); \
650 /* --- Reject an option --- * \
652 * We could get here as a result of an explicit @CONF_REJECT@ or \
653 * because the option wasn't accepted. \
658 if (_conf_state == CS_PLAIN) \
659 _conf_state = CS_UNKNOWN; \
661 error(_conf_sc, "unknown %s option `%s'", \
662 _conf_desc, _conf_sc->d.buf); \
665 /* --- Accept an option --- * \
667 * It's safe to drop through from above. Either an error will have \
668 * been reported, or the state is not @CS_BRACE@. \
672 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
674 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
676 /* --- Check for a closing brace --- */ \
678 if (_conf_state == CS_BRACE) { \
679 if (_conf_sc->t == '}') \
682 error(_conf_sc, "parse error, expected `}'"); \
685 /* --- Return an appropriate value --- */ \
687 return (_conf_state != CS_UNKNOWN); \
690 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
694 * Use: Within an options parser (between @CONF_BEGIN@ and
695 * @CONF_END@), accept or reject an option.
698 #define CONF_ACCEPT goto _conf_accept
699 #define CONF_REJECT goto _conf_reject
701 /* --- @CONF_QUAL@ --- *
705 * Use: Evaluates to a nonzero value if the current option is
706 * qualified. This can be used to decide whether abbreviations
707 * for options should be accepted.
710 #define CONF_QUAL (_conf_state != CS_PLAIN)
712 /* --- @conf_name@ --- *
714 * Arguments: @scanner *sc@ = pointer to scanner
715 * @char delim@ = delimiter character to look for
716 * @dstr *d@ = pointer to dynamic string for output
720 * Use: Reads in a compound name consisting of words separated by
721 * delimiters. Leading and trailing delimiters are permitted,
722 * although they'll probably cause confusion if used. The name
723 * may be enclosed in square brackets if that helps at all.
725 * Examples of compound names are filenames (delimited by `/')
726 * and IP addresses (delimited by `.').
729 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
731 /* --- @conf_fname@ --- *
733 * Arguments: @scanner *sc@ = pointer to scanner
734 * @dstr *d@ = pointer to dynamic string for output
738 * Use: Reads a file name from the input and stores it in @d@.
741 extern void conf_fname(scanner */
*sc*/
, dstr */
*d*/
);
743 /*----- Reference-counted file descriptors --------------------------------*/
745 typedef struct reffd
{
748 void (*proc
)(void */
*p*/
);
752 /* --- @reffd_init@ --- *
754 * Arguments: @int fd@ = file descriptor
756 * Returns: Reference-counted file descriptor object.
758 * Use: Creates a refcounted file descriptor.
761 extern reffd
*reffd_init(int /*fd*/);
763 /* --- @reffd_handler@ --- *
765 * Arguments: @reffd *r@ = pointer to reference counted filehandle
766 * @void (*proc)(void *p)@ = procedure to call
771 * Use: Sets the reference counted file descriptor to call @proc@
772 * when it is no longer required.
775 extern void reffd_handler(reffd */
*r*/
, void (*/
*proc*/
)(void */
*p*/
),
778 /* --- @reffd_inc@ --- *
780 * Arguments: @reffd *r@ = pointer to reference counted filehandle
784 * Use: Increments the reference count for a file descriptor.
787 #define REFFD_INC(r) do { (r)->ref++; } while (0)
789 extern void reffd_inc(reffd */
*r*/
);
791 /* --- @reffd_dec@ --- *
793 * Arguments: @reffd *r@ = pointer to reference counted filehandle
797 * Use: Decrements the reference count for a file descriptor.
800 #define REFFD_DEC(r) do { \
803 if (_r->ref == 0) { \
811 extern void reffd_dec(reffd */
*r*/
);
813 /*----- Sources, targets and endpoints ------------------------------------*/
815 /* --- Basic endpoint structure --- */
817 typedef struct endpt
{
818 struct endpt_ops
*ops
; /* Pointer to operations table */
819 struct endpt
*other
; /* Pointer to sibling endpoint */
820 unsigned f
; /* Various flags */
821 struct tango
*t
; /* Private data structure */
822 reffd
*in
, *out
; /* File descriptors */
825 /* --- Endpoint flags --- */
827 #define EPF_PENDING 1u /* Endpoint creation in progress */
828 #define EPF_FILE 2u /* Endpoint smells like a file */
830 /* --- Endpoint operations table --- */
832 typedef struct endpt_ops
{
834 /* --- @attach@ --- *
836 * Arguments: @endpt *e@ = pointer to endpoint to be attached
837 * @reffd *in, *out@ = input and output file descriptors
841 * Use: Instructs a non-file endpoint to attach itself to a pair of
845 void (*attach
)(endpt */
*e*/
, reffd */
*in*/
, reffd */
*out*/
);
849 * Arguments: @endpt *e@ = pointer to endpoint in question
850 * @endpt *f@ = pointer to a file endpoint
854 * Use: Informs a non-file endpoint of a file endpoint which will
855 * want to be closed when it's finished with. At that time, the
856 * endpoint should arrange to have both itself and its partner
857 * closed. If no file is registered, the endpoint manager will
858 * close both endpoints itself.
861 void (*file
)(endpt */
*e*/
, endpt */
*f*/
);
863 /* --- @wclose@ --- *
865 * Arguments: @endpt *e@ = endpoint to be partially closed
869 * Use: Announces that the endpoint will not be written to any more.
872 void (*wclose
)(endpt */
*e*/
);
876 * Arguments: @endpt *e@ = endpoint to be closed
880 * Use: Completely closes an endpoint. The endpoint's data may be
881 * freed, although some endpoints may wish to delay freeing for
885 void (*close
)(endpt */
*e*/
);
889 /* --- A basic target object --- */
891 typedef struct target
{
892 struct target_ops
*ops
;
896 /* --- Forwarding target operations --- */
898 typedef struct target_ops
{
899 const char *name
; /* Name of this target */
901 /* --- @option@ --- *
903 * Arguments: @target *t@ = pointer to target object, or zero if global
904 * @scanner *sc@ = scanner to read from
906 * Returns: Nonzero to claim the option.
908 * Use: Handles an option string from the configuration file.
911 int (*option
)(target */
*t*/
, scanner */
*sc*/
);
915 * Arguments: @scanner *sc@ = pointer to scanner to read from
917 * Returns: Pointer to a target object to claim, null to reject.
919 * Use: Parses a target description from the configuration file.
920 * Only the socket target is allowed to omit the prefix on a
921 * target specification.
924 target
*(*read
)(scanner */
*sc*/
);
926 /* --- @confirm@ --- *
928 * Arguments: @target *t@ = pointer to target
932 * Use: Confirms configuration of a target.
935 void (*confirm
)(target */
*t*/
);
937 /* --- @create@ --- *
939 * Arguments: @target *t@ = pointer to target
940 * @const char *desc@ = description of connection
942 * Returns: Pointer to a created endpoint.
944 * Use: Generates a target endpoint for communication.
947 endpt
*(*create
)(target */
*t*/
, const char */
*desc*/
);
949 /* --- @destroy@ --- *
951 * Arguments: @target *t@ = pointer to target
955 * Use: Destroys a target.
958 void (*destroy
)(target */
*t*/
);
962 /* --- A basic source object --- */
964 typedef struct source
{
965 struct source
*next
, *prev
;
966 struct source_ops
*ops
;
970 /* --- Forwarding source operations --- */
972 typedef struct source_ops
{
973 const char *name
; /* Name of this source */
975 /* --- @option@ --- *
977 * Arguments: @scanner *sc@ = scanner to read from
978 * @source *s@ = pointer to source object, or zero if global
980 * Returns: Nonzero to claim the option.
982 * Use: Handles an option string from the configuration file.
985 int (*option
)(source */
*s*/
, scanner */
*sc*/
);
989 * Arguments: @scanner *sc@ = pointer to scanner to read from
991 * Returns: Pointer to a source object to claim, null to reject.
993 * Use: Parses a source description from the configuration file.
994 * Only the socket source is allowed to omit the prefix on a
995 * source specification.
998 source
*(*read
)(scanner */
*sc*/
);
1000 /* --- @attach@ --- *
1002 * Arguments: @source *s@ = pointer to source
1003 * @scanner *sc@ = scanner (for error reporting)
1004 * @target *t@ = pointer to target to attach
1008 * Use: Attaches a target to a source.
1011 void (*attach
)(source */
*s*/
, scanner */
*sc*/
, target */
*t*/
);
1013 /* --- @destroy@ --- *
1015 * Arguments: @source *s@ = pointer to source
1019 * Use: Destroys a source. Used when closing the system down, for
1020 * example as a result of a signal.
1023 void (*destroy
)(source */
*s*/
);
1027 /* --- @endpt_kill@ --- *
1029 * Arguments: @endpt *a@ = an endpoint
1033 * Use: Kills an endpoint. If the endpoint is joined to another, the
1034 * other endpoint is also killed, as is the connection between
1035 * them (and that's the tricky bit).
1038 extern void endpt_kill(endpt */
*a*/
);
1040 /* --- @endpt_killall@ --- *
1046 * Use: Destroys all current endpoint connections. Used when
1050 extern void endpt_killall(void);
1052 /* --- @endpt_join@ --- *
1054 * Arguments: @endpt *a@ = pointer to first endpoint
1055 * @endpt *b@ = pointer to second endpoint
1056 * @const char *desc@ = description of connection
1060 * Use: Joins two endpoints together. It's OK to join endpoints
1061 * which are already joined; in fact, the the right thing to do
1062 * when your endpoint decides that it's not pending any more is
1063 * to join it to its partner again.
1065 * If the endpoints are already connected then the description
1066 * string is ignored. The endpoint manager takes a copy of
1067 * the string, so you don't need to keep it around.
1070 extern void endpt_join(endpt */
*a*/
, endpt */
*b*/
, const char */
*desc*/
);
1072 /* --- @source_add@ --- *
1074 * Arguments: @source *s@ = pointer to a source
1078 * Use: Adds a source to the master list. Only do this for passive
1079 * sources (e.g., listening sockets), not active sources (e.g.,
1080 * executable programs).
1083 extern void source_add(source */
*s*/
);
1085 /* --- @source_remove@ --- *
1087 * Arguments: @source *s@ = pointer to a source
1091 * Use: Removes a source from the master list.
1094 extern void source_remove(source */
*s*/
);
1096 /* --- @source_killall@ --- *
1102 * Use: Frees all sources.
1105 extern void source_killall(void);
1107 /*----- The exec source and target ----------------------------------------*/
1109 extern source_ops xsource_ops
;
1110 extern target_ops xtarget_ops
;
1112 /* --- @exec_init@ --- *
1118 * Use: Initializes the executable problem source and target.
1121 extern void exec_init(void);
1123 /*----- The file source and target ----------------------------------------*/
1125 extern source_ops fsource_ops
;
1126 extern target_ops ftarget_ops
;
1128 /*----- The socket source and target --------------------------------------*/
1130 extern source_ops ssource_ops
;
1131 extern target_ops starget_ops
;
1133 /* --- @starget_connected@ --- *
1135 * Arguments: @int fd@ = file descriptor now ready for use
1136 * @void *p@ = pointer to an endpoint structure
1140 * Use: Handles successful connection of the target endpoint.
1143 extern void starget_connected(int /*fd*/, void */
*p*/
);
1145 /*----- Handling of file attributes ---------------------------------------*/
1147 /* --- File attribute options structure --- */
1149 typedef struct fattr
{
1155 /* --- Shared global options --- */
1157 extern fattr fattr_global
;
1159 /* --- @fattr_init@ --- *
1161 * Arguments: @fattr *f@ = pointer to file attributes
1165 * Use: Initializes a set of file attributes to default values.
1168 extern void fattr_init(fattr */
*f*/
);
1170 /* --- @fattr_option@ --- *
1172 * Arguments: @scanner *sc@ = pointer to scanner to read
1173 * @fattr *f@ = pointer to file attributes to set
1175 * Returns: Whether the option was clamed.
1177 * Use: Reads file attributes from a scanner.
1180 extern int fattr_option(scanner */
*sc*/
, fattr */
*f*/
);
1182 /* --- @fattr_apply@ --- *
1184 * Arguments: @const char *file@ = pointer to filename
1185 * @fattr *f@ = pointer to attribute set
1187 * Returns: @-1@ if it failed.
1189 * Use: Applies file attributes to a file. For best results, try to
1190 * create the file with the right permissions and so on. This
1191 * call will fix everything up, but there are potential races
1192 * which might catch you out if you're not careful.
1195 extern int fattr_apply(const char */
*file*/
, fattr */
*f*/
);
1197 /*----- Making privileged connections -------------------------------------*/
1199 /* --- @privconn_split@ --- *
1201 * Arguments: @sel_state *s@ = select state
1205 * Use: Splits off the privileged binding code into a separate
1209 extern void privconn_split(sel_state */
*s*/
);
1211 /* --- @privconn_adddest@ --- *
1213 * Arguments: @struct in_addr peer@ = address to connect to
1214 * @unsigned port@ = port to connect to
1216 * Returns: Index for this destination address, or @-1@ if not
1219 * Use: Adds a valid destination for a privileged connection.
1222 extern int privconn_adddest(struct in_addr
/*peer*/, unsigned /*port*/);
1224 /* --- @privconn_connect@ --- *
1226 * Arguments: @conn *c@ = connection structure to fill in
1227 * @sel_state *s@ = pointer to select state to attach to
1228 * @int i@ = address index to connect to
1229 * @struct in_addr bind@ = address to bind to
1230 * @void (*func)(int, void *)@ = function to call on connect
1231 * @void *p@ = argument for the function
1233 * Returns: Zero on success, @-1@ on failure.
1235 * Use: Sets up a privileged connection job.
1238 extern int privconn_connect(conn */
*c*/
, sel_state */
*s*/
,
1239 int /*i*/, struct in_addr
/*bind*/,
1240 void (*/
*func*/
)(int, void *), void */
*p*/
);
1242 /*----- Identifying remote clients ----------------------------------------*/
1244 typedef struct id_req
{
1245 struct sockaddr_in lsin
; /* Local address of connection */
1246 struct sockaddr_in rsin
; /* Remote address of connection */
1247 const char *desc
; /* Description of connection */
1248 const char *act
; /* Action taken by server */
1249 reffd
*r
; /* Pointer to file descriptor */
1252 /* --- @identify@ --- *
1254 * Arguments: @const id_req *q@ = pointer to request block
1258 * Use: Starts a background ident lookup and reverse-resolve job
1259 * which will, eventually, report a message to the system log.
1262 extern void identify(const id_req */
*q*/
);
1264 /*----- Host-based access control -----------------------------------------*/
1266 /* --- An access control entry --- */
1268 typedef struct acl_entry
{
1269 struct acl_entry
*next
; /* Next entry in the list */
1270 const struct acl_ops
*ops
; /* Operations for the ACL entry */
1271 unsigned act
; /* What to do with matching hosts */
1274 #define ACL_DENY 0 /* Deny access to matching conns */
1275 #define ACL_ALLOW 1 /* Allow access to matching conns */
1276 #define ACL_PERM 1u /* Bit mask for permission bit */
1278 /* --- Host-based access control --- */
1280 typedef struct acl_host
{
1281 acl_entry a
; /* Base structure */
1282 struct in_addr addr
, mask
; /* Address and netmask */
1285 /* --- ACL methods --- */
1287 typedef struct acl_ops
{
1288 int (*check
)(void */
*a*/
, struct in_addr
/*addr*/, unsigned /*port*/);
1289 void (*dump
)(void */
*a*/
, FILE */
*fp*/
);
1290 void (*free
)(void */
*a*/
);
1293 /* --- @acl_check@ --- *
1295 * Arguments: @acl_entry *a@ = pointer to ACL to check against
1296 * @struct in_addr addr@ = address to check
1297 * @unsigned port@ = port number to check
1298 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
1300 * Returns: Zero if undecided, nonzero if a rule matched.
1302 * Use: Checks an address against an ACL.
1305 extern int acl_check(acl_entry */
*a*/
,
1306 struct in_addr
/*addr*/, unsigned /*port*/,
1309 /* --- @acl_dump@ --- *
1311 * Arguments: @acl_entry *a@ = pointer to ACL to dump
1312 * @FILE *fp@ = pointer to stream to dump on
1316 * Use: Dumps an access control list to an output stream.
1319 extern void acl_dump(acl_entry */
*a*/
, FILE */
*fp*/
);
1321 /* --- @acl_free@ --- *
1323 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
1327 * Use: Frees all of the memory used by an ACL.
1330 extern void acl_free(acl_entry */
*a*/
);
1332 /* --- @acl_addhost@ --- *
1334 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1335 * @unsigned act@ = what to do with matching addresses
1336 * @struct in_addr addr, mask@ = address and mask to match
1340 * Use: Adds a host-authentication entry to the end of an access
1344 extern void acl_addhost(acl_entry
***/
*a*/
, unsigned /*act*/,
1345 struct in_addr
/*addr*/, struct in_addr
/*mask*/);
1347 /* --- @acl_addpriv@ --- *
1349 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1350 * @unsigned act@ = what to do with matching addresses
1354 * Use: Adds a privileged-port check to the end of an access control
1358 extern void acl_addpriv(acl_entry
***/
*a*/
, unsigned /*act*/);
1360 /*----- Network addresses -------------------------------------------------*/
1362 /* --- A generic socket address --- *
1364 * Not all systems understand @sa_len@ fields. (In particular, Linux
1365 * doesn't.) Some fairly ugly hacking is then performed on particular
1369 typedef struct addr
{
1370 struct addr_ops
*ops
;
1374 #define ADDRSZ(sz) (sizeof(addr) + (sz))
1376 /* --- Address configuration --- *
1378 * An address family will want to extend this.
1381 typedef struct addr_opts
{
1385 #define ADDRF_NOLOG 1u
1387 /* --- Address types --- *
1389 * For things like Internet addresses, source and destinations look
1399 /* --- Description of an address type handler --- */
1401 typedef struct addr_ops
{
1402 const char *name
; /* Protocol's internal name */
1406 * Arguments: @scanner *sc@ = pointer to scanner to read from
1407 * @unsigned type@ = type of address to be read
1409 * Returns: A filled-in socket address.
1411 * Use: Parses a textual representation of a socket address.
1414 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
1416 /* --- @destroy@ --- *
1418 * Arguments: @addr *a@ = pointer to an address block
1422 * Use: Disposes of an address block in some suitable fashion.
1425 void (*destroy
)(addr */
*a*/
);
1427 /* --- @print@ --- *
1429 * Arguments: @addr *a@ = pointer to socket address to read
1430 * @unsigned type@ = type of address to be written
1431 * @dstr *d@ = string on which to write the description
1435 * Use: Writes a textual representation of a socket address to
1439 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
1441 /* --- @initsrcopts@ --- *
1445 * Returns: A pointer to a protocol-specific data block for a listener
1447 * Use: Creates a data block for a listener. This is attached to the
1448 * listener data structure. Options can then be requested, and
1449 * are added to the block when necessary.
1452 addr_opts
*(*initsrcopts
)(void);
1454 /* --- @option@ --- *
1456 * Arguments: @scanner *sc@ = pointer to a scanner to read from
1457 * @unsigned type@ = kind of option this is
1458 * @addr_opts *ao@ = data block to modify (from @init@), or null
1460 * Returns: Nonzero to claim the option.
1462 * Use: Parses a source option, either global or listener-specific.
1465 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
1467 /* --- @confirm@ --- *
1469 * Arguments: @addr *a@ = pointer to an address structure
1470 * @unsigned type@ = kind of address this is
1471 * @addr_opts *ao@ = address options
1475 * Use: Called during initialization when an address is fully
1479 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
1481 /* --- @freesrcopts@ --- *
1483 * Arguments: @addr_opts *ao@ = data block to remove
1487 * Use: Throws away all the configuration data for an address type.
1490 void (*freesrcopts
)(addr_opts */
*ao*/
);
1494 * Arguments: @addr *a@ = the address to bind to
1495 * @addr_opts *ao@ = the address options
1497 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
1499 * Use: Binds a listening socket. The tedious stuff with @listen@
1503 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
1505 /* --- @unbind@ --- *
1507 * Arguments: @addr *a@ = pointer to an address
1511 * Use: Unbinds an address. This is used when tidying up. The main
1512 * purpose is to let the Unix-domain handler remove its socket
1513 * node from the filesystem.
1516 void (*unbind
)(addr */
*a*/
);
1518 /* --- @accept@ --- *
1520 * Arguments: @int fd@ = listening file descriptor
1521 * @addr_opts *ao@ = data block to get configuration from
1522 * @const char *desc@ = description of the listener
1524 * Returns: Pointer to a reference counted file descriptor.
1526 * Use: Accepts, verifies and logs an incoming connection.
1529 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
1531 /* --- @inittargopts@ --- *
1535 * Returns: A pointer to a protocol-specific data block for a connecter
1537 * Use: Creates a data block for a target. This is attached to the
1538 * target data structure. Options can then be requested, and
1539 * are added to the block when necessary.
1542 addr_opts
*(*inittargopts
)(void);
1544 /* --- @freetargopts@ --- *
1546 * Arguments: @addr_opts *ao@ = data block to remove
1550 * Use: Throws away all the configuration data for an address type.
1553 void (*freetargopts
)(addr_opts */
*ao*/
);
1555 /* --- @connect@ --- *
1557 * Arguments: @addr *a@ = destination address
1558 * @addr_opts *ao@ = target address options
1559 * @conn *c@ = connection structure
1560 * @endpt *e@ = endpoint structure
1562 * Returns: Zero if OK, @-1@ on some error.
1564 * Use: Requests that a connection be made, or at least set in
1565 * motion. An address may do one of these things:
1569 * * Call @starget_connected@ with @-1@ or a connected file
1570 * descriptor and the pointer @e@.
1572 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
1573 * and @e@ as the function to call.
1576 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
1580 /* --- Address types --- */
1582 extern addr_ops un_ops
;
1583 extern addr_ops inet_ops
;
1585 /*----- That's all, folks -------------------------------------------------*/