3 * Source and target for executable programs
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.
29 /*----- Data structures ---------------------------------------------------*/
31 /* --- Resource usage --- */
35 typedef struct xlimit
{
36 #define XLIMIT_ENTRY(name, constant) struct rlimit name;
43 /* --- Environment variable modifications --- */
55 /* --- Program options --- */
57 typedef struct xopts
{
73 /* --- Executable program arguments --- */
75 typedef struct xargs
{
81 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
83 /* --- Executable endpoints --- */
87 struct xept
*next
, *prev
;
100 /* --- Executable program data --- */
102 typedef struct xdata
{
107 /* --- Executable source block --- */
109 typedef struct xsource
{
114 /* --- Executable target block --- */
116 typedef struct xtarget
{
121 /*----- Static variables --------------------------------------------------*/
123 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
124 static xept
*xept_list
;
126 static sym_table env
;
128 /*----- Fiddling about with resource limits -------------------------------*/
130 #ifdef HAVE_SETRLIMIT
132 /* --- Table mapping user-level names to OS interface bits --- */
134 typedef struct rlimit_ent
{
141 static rlimit_ent rlimits
[] = {
142 #define TABLE_ENTRY(name, constant) \
143 { #name, #constant, constant, offsetof(xlimit, name) },
149 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
151 /* --- @rlimit_get@ --- *
153 * Arguments: @xlimit *xl@ = pointer to limit structure
157 * Use: Initializes a limit structure from the current limits.
160 static void rlimit_get(xlimit
*xl
)
164 for (r
= rlimits
; r
->name
; r
++) {
165 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
166 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
172 /* --- @rlimit_set@ --- *
174 * Arguments: @xlimit *xl@ = pointer to limit structure
178 * Use: Sets resource limits from the supplied limits structure.
181 static void rlimit_set(xlimit
*xl
)
185 for (r
= rlimits
; r
->name
; r
++) {
186 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
187 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
193 /* --- @rlimit_option@ --- */
195 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
197 CONF_BEGIN(sc
, "rlimit", "resource limit")
198 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
203 /* --- Find out which resource is being fiddled --- */
209 for (r
= rlimits
; r
->name
; r
++) {
210 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
211 if (r
->name
[sc
->d
.len
] == 0) {
215 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
223 rl
= RLIMIT(xl
, chosen
->off
);
226 /* --- Look for hard or soft restrictions --- */
232 if (sc
->t
== CTOK_WORD
) {
233 if ((i
= conf_enum(sc
, "soft,hard",
234 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
239 /* --- Now read the new value --- */
243 if (sc
->t
!= CTOK_WORD
)
244 error(sc
, "parse error, expected limit value");
246 if (conf_enum(sc
, "unlimited,infinity",
247 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
252 v
= strtol(sc
->d
.buf
, &p
, 0);
254 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
255 switch (tolower((unsigned char)*p
)) {
257 case 'b': v
*= 512; break;
260 case 'k': v
*= 1024; break;
261 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
266 /* --- Store the limit value away --- */
274 if (v
> rl
->rlim_max
)
275 error(sc
, "soft limit %lu exceeds hard limit %lu for %s",
276 (unsigned long)v
, (unsigned long)rl
->rlim_max
,
282 if (rl
->rlim_cur
> v
)
293 /*----- Environment fiddling ----------------------------------------------*/
295 /* --- @xenv_option@ --- *
297 * Arguments: @xopts *xo@ = pointer to options block
298 * @scanner *sc@ = pointer to scanner
300 * Returns: Nonzero if claimed
302 * Use: Parses environment variable assignments.
305 static int xenv_option(xopts
*xo
, scanner
*sc
)
307 CONF_BEGIN(sc
, "env", "environment")
310 /* --- Unset a variable --- */
312 if (strcmp(sc
->d
.buf
, "unset") == 0) {
314 if (sc
->t
!= CTOK_WORD
)
315 error(sc
, "parse error, expected environment variable name");
317 xe
->name
= xstrdup(sc
->d
.buf
);
324 /* --- Clear the entire environment --- */
326 if (strcmp(sc
->d
.buf
, "clear") == 0) {
333 /* --- Allow `set' to be omitted if there's a prefix --- */
335 if (strcmp(sc
->d
.buf
, "set") == 0)
340 /* --- Set a variable --- */
342 if (sc
->t
!= CTOK_WORD
)
343 error(sc
, "parse error, expected environment variable name");
345 xe
->name
= xstrdup(sc
->d
.buf
);
349 if (sc
->t
!= CTOK_WORD
)
350 error(sc
, "parse error, expected environment variable value");
351 xe
->value
= xstrdup(sc
->d
.buf
);
359 xo
->etail
= &xe
->next
;
362 /* --- Nothing else to try --- */
367 /* --- @xenv_apply@ --- *
369 * Arguments: @xenv *xe@ = pointer to a variable change list
373 * Use: Modifies the environment (in @env@) according to the list.
376 static void xenv_apply(xenv
*xe
)
381 env_put(&env
, xe
->name
, xe
->value
);
392 /* --- @xenv_destroy@ --- *
394 * Arguments: @xenv *xe@ = pointer to a variable change list
398 * Use: Frees the memory used by an environment variable change list.
401 static void xenv_destroy(xenv
*xe
)
413 /*----- Miscellaneous good things -----------------------------------------*/
415 /* --- @x_tidy@ --- *
417 * Arguments: @xargs *xa@ = pointer to an arguments block
418 * @xopts *xo@ = pointer to an options block
422 * Use: Releases a reference to argument and options blocks.
425 static void x_tidy(xargs
*xa
, xopts
*xo
)
433 xenv_destroy(xo
->env
);
438 /*----- Executable endpoints ----------------------------------------------*/
440 /* --- @attach@ --- */
442 static void xept_error(char */
*p*/
, size_t /*len*/, void */
*v*/
);
444 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
446 xept
*xe
= (xept
*)e
;
450 /* --- Make a pipe for standard error --- */
453 fw_log(NOW
, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
456 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
458 /* --- Fork a child, and handle an error if there was one --- */
460 if ((kid
= fork()) == -1) {
461 fw_log(NOW
, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
467 /* --- Do the child thing --- */
473 /* --- Fiddle with the file descriptors --- *
475 * Attach the other endpoint's descriptors to standard input and output.
476 * Attach my pipe to standard error. Mark everything as blocking and
477 * not-to-be-closed-on-exec at this end.
481 md
[0].cur
= in
->fd
; md
[0].want
= STDIN_FILENO
;
482 md
[1].cur
= out
->fd
; md
[1].want
= STDOUT_FILENO
;
483 md
[2].cur
= fd
[1]; md
[2].want
= STDERR_FILENO
;
485 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
489 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
490 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
491 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
493 /* --- First of all set the @chroot@ prison --- */
495 if (xo
->root
&& chroot(xo
->root
)) {
496 moan("couldn't set `%s' as filesystem root: %s",
497 xo
->root
, strerror(errno
));
501 /* --- Now set the current directory --- */
503 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
504 moan("couldn't set `%s' as current directory: %s",
505 xo
->dir ? xo
->dir
: "/", strerror(errno
));
509 /* --- Set the resource limits --- */
511 #ifdef HAVE_SETRLIMIT
515 /* --- Set group id --- */
517 if (xo
->gid
!= (gid_t
)-1) {
518 if (setgid(xo
->gid
)) {
519 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
522 #ifdef HAVE_SETGROUPS
523 if (setgroups(1, &xo
->gid
))
524 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
529 /* --- Set uid --- */
531 if (xo
->uid
!= (uid_t
)-1) {
532 if (setuid(xo
->uid
)) {
533 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
538 /* --- Play with signal dispositions --- */
540 signal(SIGPIPE
, SIG_DFL
);
542 /* --- Fiddle with the environment --- */
544 xenv_apply(exec_opts
.env
);
545 xenv_apply(xe
->xo
->env
);
546 environ
= env_export(&env
);
548 /* --- Run the program --- */
550 execvp(xe
->xa
->file
, xe
->xa
->argv
);
551 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
555 /* --- The child's done; see to the parent --- */
558 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
560 xe
->next
= xept_list
;
563 xept_list
->prev
= xe
;
565 if (!(xe
->xo
->f
& XF_NOLOG
))
566 fw_log(NOW
, "[%s] started with pid %i", xe
->desc
, kid
);
571 /* --- @xept_file@ --- */
573 static void xept_file(endpt
*e
, endpt
*f
)
575 xept
*xe
= (xept
*)e
;
579 /* --- @xept_close@ --- */
581 static void xept_close(endpt
*e
)
583 xept
*xe
= (xept
*)e
;
586 xe
->f
->ops
->close(xe
->f
);
587 x_tidy(xe
->xa
, xe
->xo
);
593 /* --- @xept_destroy@ --- */
595 static void xept_destroy(xept
*xe
)
597 /* --- First emit the news about the process --- */
599 if (xe
->xo
->f
& XF_NOLOG
)
601 else if (WIFEXITED(xe
->st
)) {
602 if (WEXITSTATUS(xe
->st
) == 0)
603 fw_log(NOW
, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
605 fw_log(NOW
, "[%s] pid %i failed: status %i",
606 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
608 } else if (WIFSIGNALED(xe
->st
)) {
610 #ifdef HAVE_STRSIGNAL
611 s
= strsignal(WTERMSIG(xe
->st
));
612 #elif HAVE__SYS_SIGLIST
613 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
616 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
619 fw_log(NOW
, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
621 fw_log(NOW
, "[%s] pid %i failed: unrecognized status",
624 /* --- Free up the parent-side resources --- */
627 xe
->next
->prev
= xe
->prev
;
629 xe
->prev
->next
= xe
->next
;
631 xept_list
= xe
->next
;
635 xe
->f
->ops
->close(xe
->f
);
636 x_tidy(xe
->xa
, xe
->xo
);
641 /* --- @xept_chld@ --- *
643 * Arguments: @int n@ = signal number
644 * @void *p@ = uninteresting pointer
648 * Use: Deals with child death situations.
651 static void xept_chld(int n
, void *p
)
656 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
657 xept
*xe
= xept_list
;
661 if (kid
== xxe
->kid
) {
663 xxe
->e
.f
|= XEF_EXIT
;
664 if (xxe
->e
.f
& XEF_CLOSE
)
672 /* --- @xept_error@ --- *
674 * Arguments: @char *p@ = pointer to string read from stderr
675 * @size_t len@ = length of the string
676 * @void *v@ = pointer to by endpoint
680 * Use: Handles error reports from a child process.
683 static void xept_error(char *p
, size_t len
, void *v
)
687 fw_log(NOW
, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
689 close(xe
->err
.reader
.fd
);
690 selbuf_destroy(&xe
->err
);
691 xe
->e
.f
|= XEF_CLOSE
;
692 if (xe
->e
.f
& XEF_EXIT
)
697 /* --- Endpoint operations --- */
699 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
701 /*----- General operations on sources and targets -------------------------*/
703 /* --- @exec_init@ --- *
709 * Use: Initializes the executable problem source and target.
714 #ifdef HAVE_SETRLIMIT
715 rlimit_get(&exec_opts
.xl
);
717 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
719 env_import(&env
, environ
);
722 /* --- @exec_option@ --- */
724 static int exec_option(xdata
*x
, scanner
*sc
)
726 xopts
*xo
= x ? x
->xo
: &exec_opts
;
728 CONF_BEGIN(sc
, "exec", "executable");
730 /* --- Logging settings --- */
732 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
733 strcmp(sc
->d
.buf
, "log") == 0) {
737 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
744 /* --- Current directory setting --- *
746 * Lots of possibilities to guard against possible brainoes.
749 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
750 strcmp(sc
->d
.buf
, "cd") == 0 ||
751 strcmp(sc
->d
.buf
, "chdir") == 0 ||
752 strcmp(sc
->d
.buf
, "cwd") == 0) {
758 xo
->dir
= xstrdup(d
.buf
);
763 /* --- Set a chroot prison --- */
765 if (strcmp(sc
->d
.buf
, "root") == 0 ||
766 strcmp(sc
->d
.buf
, "chroot") == 0) {
772 xo
->root
= xstrdup(d
.buf
);
777 /* --- Set the target user id --- */
779 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
780 strcmp(sc
->d
.buf
, "user") == 0) {
784 if (sc
->t
!= CTOK_WORD
)
785 error(sc
, "parse error, expected user name or uid");
786 if (isdigit((unsigned char)*sc
->d
.buf
))
787 xo
->uid
= atoi(sc
->d
.buf
);
789 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
791 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
792 xo
->uid
= pw
->pw_uid
;
798 /* --- Set the target group id --- */
800 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
801 strcmp(sc
->d
.buf
, "group") == 0) {
805 if (sc
->t
!= CTOK_WORD
)
806 error(sc
, "parse error, expected group name or gid");
807 if (isdigit((unsigned char)*sc
->d
.buf
))
808 xo
->gid
= atoi(sc
->d
.buf
);
810 struct group
*gr
= getgrnam(sc
->d
.buf
);
812 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
813 xo
->gid
= gr
->gr_gid
;
819 /* --- Now try resource limit settings --- */
821 #ifdef HAVE_SETRLIMIT
822 if (rlimit_option(&xo
->xl
, sc
))
826 /* --- And then environment settings --- */
828 if (xenv_option(xo
, sc
))
831 /* --- Nothing found --- */
836 /* --- @exec_desc@ --- */
838 static void exec_desc(xdata
*x
, dstr
*d
)
842 dstr_puts(d
, "exec ");
843 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
844 dstr_puts(d
, x
->xa
->file
);
847 for (p
= x
->xa
->argv
; *p
; p
++) {
856 /* --- @exec_read@ --- */
858 static void exec_read(xdata
*x
, scanner
*sc
)
864 /* --- Read the first word --- *
866 * This is either a shell command or the actual program to run.
869 if (sc
->t
== CTOK_WORD
) {
870 dstr_putd(&d
, &sc
->d
); d
.len
++;
875 /* --- See if there's a list of arguments --- *
877 * If not, then the thing I saw was a shell command, so build the proper
878 * arguments for that.
884 error(sc
, "parse error, expected shell command or argument list");
885 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
886 p
= (char *)(xa
->argv
+ 4);
889 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
890 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
891 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
895 /* --- Snarf in a list of arguments --- */
902 /* --- Strip off the leading `[' --- *
904 * Allow various handy filename characters to be entered without quoting.
907 conf_undelim(sc
, "=:/.", "=:/.");
910 /* --- Read a sequence of arguments --- */
912 while (sc
->t
== CTOK_WORD
) {
913 dstr_putd(&d
, &sc
->d
); d
.len
++;
917 conf_undelim(sc
, 0, 0);
919 /* --- Expect the closing `]' --- */
922 error(sc
, "parse error, missing `]'");
925 /* --- If there are no arguments, whinge --- */
928 error(sc
, "must specify at least one argument");
930 /* --- Allocate a lump of memory for the array --- */
932 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
935 p
= (char *)(v
+ argc
+ 1);
936 memcpy(p
, d
.buf
, d
.len
);
941 /* --- Start dumping addresses into the @argv@ array --- */
945 while (*p
++ && p
< q
)
953 /* --- Do some other setting up --- */
957 x
->xo
= CREATE(xopts
);
963 /* --- @exec_endpt@ --- */
965 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
967 xept
*xe
= CREATE(xept
);
968 xe
->e
.ops
= &xept_ops
;
972 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
973 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
976 xe
->desc
= xstrdup(desc
);
980 /* --- @exec_destroy@ --- */
982 static void exec_destroy(xdata
*x
)
984 x_tidy(x
->xa
, x
->xo
);
987 /*----- Source definition -------------------------------------------------*/
989 /* --- @option@ --- */
991 static int xsource_option(source
*s
, scanner
*sc
)
993 xsource
*xs
= (xsource
*)s
;
994 return (exec_option(xs ?
&xs
->x
: 0, sc
));
999 static source
*xsource_read(scanner
*sc
)
1003 if (!conf_prefix(sc
, "exec"))
1005 xs
= CREATE(xsource
);
1006 xs
->s
.ops
= &xsource_ops
;
1008 exec_read(&xs
->x
, sc
);
1012 /* --- @attach@ --- */
1014 static void xsource_destroy(source */
*s*/
);
1016 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1018 xsource
*xs
= (xsource
*)s
;
1021 /* --- Set up the source description string --- */
1025 exec_desc(&xs
->x
, &d
);
1026 dstr_puts(&d
, " -> ");
1027 dstr_puts(&d
, t
->desc
);
1028 xs
->s
.desc
= xstrdup(d
.buf
);
1032 /* --- Create the endpoints --- */
1034 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1036 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1040 endpt_join(e
, ee
, xs
->s
.desc
);
1042 /* --- Dispose of source and target --- */
1046 xsource_destroy(&xs
->s
);
1049 /* --- @destroy@ --- */
1051 static void xsource_destroy(source
*s
)
1053 xsource
*xs
= (xsource
*)s
;
1055 exec_destroy(&xs
->x
);
1059 /* --- Executable source operations --- */
1061 source_ops xsource_ops
= {
1063 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1066 /*----- Exec target description -------------------------------------------*/
1068 /* --- @option@ --- */
1070 static int xtarget_option(target
*t
, scanner
*sc
)
1072 xtarget
*xt
= (xtarget
*)t
;
1073 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1076 /* --- @read@ --- */
1078 static target
*xtarget_read(scanner
*sc
)
1083 if (!conf_prefix(sc
, "exec"))
1085 xt
= CREATE(xtarget
);
1086 xt
->t
.ops
= &xtarget_ops
;
1087 exec_read(&xt
->x
, sc
);
1088 exec_desc(&xt
->x
, &d
);
1089 xt
->t
.desc
= xstrdup(d
.buf
);
1094 /* --- @create@ --- */
1096 static endpt
*xtarget_create(target
*t
, const char *desc
)
1098 xtarget
*xt
= (xtarget
*)t
;
1099 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1103 /* --- @destroy@ --- */
1105 static void xtarget_destroy(target
*t
)
1107 xtarget
*xt
= (xtarget
*)t
;
1109 exec_destroy(&xt
->x
);
1113 /* --- Exec target operations --- */
1115 target_ops xtarget_ops
= {
1117 xtarget_option
, xtarget_read
, 0, xtarget_create
, xtarget_destroy
1120 /*----- That's all, folks -------------------------------------------------*/