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 R(r, n) struct rlimit n;
42 /* --- Environment variable modifications --- */
54 /* --- Program options --- */
56 typedef struct xopts
{
72 /* --- Executable program arguments --- */
74 typedef struct xargs
{
80 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
82 /* --- Executable endpoints --- */
86 struct xept
*next
, *prev
;
99 /* --- Executable program data --- */
101 typedef struct xdata
{
106 /* --- Executable source block --- */
108 typedef struct xsource
{
113 /* --- Executable target block --- */
115 typedef struct xtarget
{
120 /*----- Static variables --------------------------------------------------*/
122 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
123 static xept
*xept_list
;
125 static sym_table env
;
127 /*----- Fiddling about with resource limits -------------------------------*/
129 #ifdef HAVE_SETRLIMIT
131 /* --- Table mapping user-level names to OS interface bits --- */
133 typedef struct rlimit_ent
{
140 static rlimit_ent rlimits
[] = {
141 #define R(r, n) { #n, #r, r, offsetof(xlimit, n) },
146 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
148 /* --- @rlimit_get@ --- *
150 * Arguments: @xlimit *xl@ = pointer to limit structure
154 * Use: Initializes a limit structure from the current limits.
157 static void rlimit_get(xlimit
*xl
)
161 for (r
= rlimits
; r
->name
; r
++) {
162 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
163 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
169 /* --- @rlimit_set@ --- *
171 * Arguments: @xlimit *xl@ = pointer to limit structure
175 * Use: Sets resource limits from the supplied limits structure.
178 static void rlimit_set(xlimit
*xl
)
182 for (r
= rlimits
; r
->name
; r
++) {
183 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
184 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
190 /* --- @rlimit_option@ --- */
192 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
194 CONF_BEGIN(sc
, "rlimit", "resource limit")
195 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
200 /* --- Find out which resource is being fiddled --- */
206 for (r
= rlimits
; r
->name
; r
++) {
207 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
208 if (r
->name
[sc
->d
.len
] == 0) {
212 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
220 rl
= RLIMIT(xl
, chosen
->off
);
223 /* --- Look for hard or soft restrictions --- */
229 if (sc
->t
== CTOK_WORD
) {
230 if ((i
= conf_enum(sc
, "soft,hard",
231 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
236 /* --- Now read the new value --- */
240 if (sc
->t
!= CTOK_WORD
)
241 error(sc
, "parse error, expected limit value");
243 if (conf_enum(sc
, "unlimited,infinity",
244 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
249 v
= strtol(sc
->d
.buf
, &p
, 0);
251 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
252 switch (tolower((unsigned char)*p
)) {
254 case 'b': v
*= 512; break;
257 case 'k': v
*= 1024; break;
258 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
263 /* --- Store the limit value away --- */
271 if (v
> rl
->rlim_max
)
272 error(sc
, "soft limit %l exceeds hard limit %l for %s",
273 v
, rl
->rlim_max
, chosen
->rname
);
278 if (rl
->rlim_cur
> v
)
289 /*----- Environment fiddling ----------------------------------------------*/
291 /* --- @xenv_option@ --- *
293 * Arguments: @xopts *xo@ = pointer to options block
294 * @scanner *sc@ = pointer to scanner
296 * Returns: Nonzero if claimed
298 * Use: Parses environment variable assignments.
301 static int xenv_option(xopts
*xo
, scanner
*sc
)
303 CONF_BEGIN(sc
, "env", "environment")
306 /* --- Unset a variable --- */
308 if (strcmp(sc
->d
.buf
, "unset") == 0) {
310 if (sc
->t
!= CTOK_WORD
)
311 error(sc
, "parse error, expected environment variable name");
313 xe
->name
= xstrdup(sc
->d
.buf
);
320 /* --- Clear the entire environment --- */
322 if (strcmp(sc
->d
.buf
, "clear") == 0) {
329 /* --- Allow `set' to be omitted if there's a prefix --- */
331 if (strcmp(sc
->d
.buf
, "set") == 0)
336 /* --- Set a variable --- */
338 if (sc
->t
!= CTOK_WORD
)
339 error(sc
, "parse error, expected environment variable name");
341 xe
->name
= xstrdup(sc
->d
.buf
);
345 if (sc
->t
!= CTOK_WORD
)
346 error(sc
, "parse error, expected environment variable value");
347 xe
->value
= xstrdup(sc
->d
.buf
);
355 xo
->etail
= &xe
->next
;
358 /* --- Nothing else to try --- */
363 /* --- @xenv_apply@ --- *
365 * Arguments: @xenv *xe@ = pointer to a variable change list
369 * Use: Modifies the environment (in @env@) according to the list.
372 static void xenv_apply(xenv
*xe
)
377 env_put(&env
, xe
->name
, xe
->value
);
388 /* --- @xenv_destroy@ --- *
390 * Arguments: @xenv *xe@ = pointer to a variable change list
394 * Use: Frees the memory used by an environment variable change list.
397 static void xenv_destroy(xenv
*xe
)
409 /*----- Miscellaneous good things -----------------------------------------*/
411 /* --- @x_tidy@ --- *
413 * Arguments: @xargs *xa@ = pointer to an arguments block
414 * @xopts *xo@ = pointer to an options block
418 * Use: Releases a reference to argument and options blocks.
421 static void x_tidy(xargs
*xa
, xopts
*xo
)
429 xenv_destroy(xo
->env
);
434 /*----- Executable endpoints ----------------------------------------------*/
436 /* --- @attach@ --- */
438 static void xept_error(char */
*p*/
, size_t /*len*/, void */
*v*/
);
440 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
442 xept
*xe
= (xept
*)e
;
446 /* --- Make a pipe for standard error --- */
449 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
452 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
454 /* --- Fork a child, and handle an error if there was one --- */
456 if ((kid
= fork()) == -1) {
457 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
463 /* --- Do the child thing --- */
469 /* --- Fiddle with the file descriptors --- *
471 * Attach the other endpoint's descriptors to standard input and output.
472 * Attach my pipe to standard error. Mark everything as blocking and
473 * not-to-be-closed-on-exec at this end.
477 md
[0].cur
= in
->fd
; md
[0].want
= STDIN_FILENO
;
478 md
[1].cur
= out
->fd
; md
[1].want
= STDOUT_FILENO
;
479 md
[2].cur
= fd
[1]; md
[2].want
= STDERR_FILENO
;
481 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
485 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
486 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
487 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
489 /* --- First of all set the @chroot@ prison --- */
491 if (xo
->root
&& chroot(xo
->root
)) {
492 moan("couldn't set `%s' as filesystem root: %s",
493 xo
->root
, strerror(errno
));
497 /* --- Now set the current directory --- */
499 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
500 moan("couldn't set `%s' as current directory: %s",
501 xo
->dir ? xo
->dir
: "/", strerror(errno
));
505 /* --- Set the resource limits --- */
507 #ifdef HAVE_SETRLIMIT
511 /* --- Set group id --- */
513 if (xo
->gid
!= (gid_t
)-1) {
514 if (setgid(xo
->gid
)) {
515 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
518 #ifdef HAVE_SETGROUPS
519 if (setgroups(1, &xo
->gid
))
520 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
525 /* --- Set uid --- */
527 if (xo
->uid
!= (uid_t
)-1) {
528 if (setuid(xo
->uid
)) {
529 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
534 /* --- Play with signal dispositions --- */
536 signal(SIGPIPE
, SIG_DFL
);
538 /* --- Fiddle with the environment --- */
540 xenv_apply(exec_opts
.env
);
541 xenv_apply(xe
->xo
->env
);
542 environ
= env_export(&env
);
544 /* --- Run the program --- */
546 execvp(xe
->xa
->file
, xe
->xa
->argv
);
547 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
551 /* --- The child's done; see to the parent --- */
554 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
556 xe
->next
= xept_list
;
559 xept_list
->prev
= xe
;
561 if (!(xe
->xo
->f
& XF_NOLOG
))
562 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
567 /* --- @xept_file@ --- */
569 static void xept_file(endpt
*e
, endpt
*f
)
571 xept
*xe
= (xept
*)e
;
575 /* --- @xept_close@ --- */
577 static void xept_close(endpt
*e
)
579 xept
*xe
= (xept
*)e
;
582 xe
->f
->ops
->close(xe
->f
);
583 x_tidy(xe
->xa
, xe
->xo
);
589 /* --- @xept_destroy@ --- */
591 static void xept_destroy(xept
*xe
)
593 /* --- First emit the news about the process --- */
595 if (xe
->xo
->f
& XF_NOLOG
)
597 else if (WIFEXITED(xe
->st
)) {
598 if (WEXITSTATUS(xe
->st
) == 0)
599 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
601 fw_log(-1, "[%s] pid %i failed: status %i",
602 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
604 } else if (WIFSIGNALED(xe
->st
)) {
606 #ifdef HAVE_STRSIGNAL
607 s
= strsignal(WTERMSIG(xe
->st
));
608 #elif HAVE__SYS_SIGLIST
609 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
612 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
615 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
617 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
619 /* --- Free up the parent-side resources --- */
622 xe
->next
->prev
= xe
->prev
;
624 xe
->prev
->next
= xe
->next
;
626 xept_list
= xe
->next
;
630 xe
->f
->ops
->close(xe
->f
);
631 x_tidy(xe
->xa
, xe
->xo
);
636 /* --- @xept_chld@ --- *
638 * Arguments: @int n@ = signal number
639 * @void *p@ = uninteresting pointer
643 * Use: Deals with child death situations.
646 static void xept_chld(int n
, void *p
)
651 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
652 xept
*xe
= xept_list
;
656 if (kid
== xxe
->kid
) {
658 xxe
->e
.f
|= XEF_EXIT
;
659 if (xxe
->e
.f
& XEF_CLOSE
)
667 /* --- @xept_error@ --- *
669 * Arguments: @char *p@ = pointer to string read from stderr
670 * @size_t len@ = length of the string
671 * @void *v@ = pointer to by endpoint
675 * Use: Handles error reports from a child process.
678 static void xept_error(char *p
, size_t len
, void *v
)
682 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
684 close(xe
->err
.reader
.fd
);
685 selbuf_destroy(&xe
->err
);
686 xe
->e
.f
|= XEF_CLOSE
;
687 if (xe
->e
.f
& XEF_EXIT
)
692 /* --- Endpoint operations --- */
694 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
696 /*----- General operations on sources and targets -------------------------*/
698 /* --- @exec_init@ --- *
704 * Use: Initializes the executable problem source and target.
709 #ifdef HAVE_SETRLIMIT
710 rlimit_get(&exec_opts
.xl
);
712 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
714 env_import(&env
, environ
);
717 /* --- @exec_option@ --- */
719 static int exec_option(xdata
*x
, scanner
*sc
)
721 xopts
*xo
= x ? x
->xo
: &exec_opts
;
723 CONF_BEGIN(sc
, "exec", "executable");
725 /* --- Logging settings --- */
727 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
728 strcmp(sc
->d
.buf
, "log") == 0) {
732 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
739 /* --- Current directory setting --- *
741 * Lots of possibilities to guard against possible brainoes.
744 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
745 strcmp(sc
->d
.buf
, "cd") == 0 ||
746 strcmp(sc
->d
.buf
, "chdir") == 0 ||
747 strcmp(sc
->d
.buf
, "cwd") == 0) {
753 xo
->dir
= xstrdup(d
.buf
);
758 /* --- Set a chroot prison --- */
760 if (strcmp(sc
->d
.buf
, "root") == 0 ||
761 strcmp(sc
->d
.buf
, "chroot") == 0) {
767 xo
->root
= xstrdup(d
.buf
);
772 /* --- Set the target user id --- */
774 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
775 strcmp(sc
->d
.buf
, "user") == 0) {
779 if (sc
->t
!= CTOK_WORD
)
780 error(sc
, "parse error, expected user name or uid");
781 if (isdigit((unsigned char)*sc
->d
.buf
))
782 xo
->uid
= atoi(sc
->d
.buf
);
784 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
786 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
787 xo
->uid
= pw
->pw_uid
;
793 /* --- Set the target group id --- */
795 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
796 strcmp(sc
->d
.buf
, "group") == 0) {
800 if (sc
->t
!= CTOK_WORD
)
801 error(sc
, "parse error, expected group name or gid");
802 if (isdigit((unsigned char)*sc
->d
.buf
))
803 xo
->gid
= atoi(sc
->d
.buf
);
805 struct group
*gr
= getgrnam(sc
->d
.buf
);
807 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
808 xo
->gid
= gr
->gr_gid
;
814 /* --- Now try resource limit settings --- */
816 #ifdef HAVE_SETRLIMIT
817 if (rlimit_option(&xo
->xl
, sc
))
821 /* --- And then environment settings --- */
823 if (xenv_option(xo
, sc
))
826 /* --- Nothing found --- */
831 /* --- @exec_desc@ --- */
833 static void exec_desc(xdata
*x
, dstr
*d
)
837 dstr_puts(d
, "exec ");
838 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
839 dstr_puts(d
, x
->xa
->file
);
842 for (p
= x
->xa
->argv
; *p
; p
++) {
851 /* --- @exec_read@ --- */
853 static void exec_read(xdata
*x
, scanner
*sc
)
859 /* --- Read the first word --- *
861 * This is either a shell command or the actual program to run.
864 if (sc
->t
== CTOK_WORD
) {
865 dstr_putd(&d
, &sc
->d
); d
.len
++;
870 /* --- See if there's a list of arguments --- *
872 * If not, then the thing I saw was a shell command, so build the proper
873 * arguments for that.
879 error(sc
, "parse error, expected shell command or argument list");
880 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
881 p
= (char *)(xa
->argv
+ 4);
884 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
885 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
886 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
890 /* --- Snarf in a list of arguments --- */
897 /* --- Strip off the leading `[' --- *
899 * Allow various handy filename characters to be entered without quoting.
902 conf_undelim(sc
, "=:/.", "=:/.");
905 /* --- Read a sequence of arguments --- */
907 while (sc
->t
== CTOK_WORD
) {
908 dstr_putd(&d
, &sc
->d
); d
.len
++;
912 conf_undelim(sc
, 0, 0);
914 /* --- Expect the closing `]' --- */
917 error(sc
, "parse error, missing `]'");
920 /* --- If there are no arguments, whinge --- */
923 error(sc
, "must specify at least one argument");
925 /* --- Allocate a lump of memory for the array --- */
927 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
930 p
= (char *)(v
+ argc
+ 1);
931 memcpy(p
, d
.buf
, d
.len
);
936 /* --- Start dumping addresses into the @argv@ array --- */
940 while (*p
++ && p
< q
)
948 /* --- Do some other setting up --- */
952 x
->xo
= CREATE(xopts
);
958 /* --- @exec_endpt@ --- */
960 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
962 xept
*xe
= CREATE(xept
);
963 xe
->e
.ops
= &xept_ops
;
967 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
968 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
971 xe
->desc
= xstrdup(desc
);
975 /* --- @exec_destroy@ --- */
977 static void exec_destroy(xdata
*x
)
979 x_tidy(x
->xa
, x
->xo
);
982 /*----- Source definition -------------------------------------------------*/
984 /* --- @option@ --- */
986 static int xsource_option(source
*s
, scanner
*sc
)
988 xsource
*xs
= (xsource
*)s
;
989 return (exec_option(xs ?
&xs
->x
: 0, sc
));
994 static source
*xsource_read(scanner
*sc
)
998 if (!conf_prefix(sc
, "exec"))
1000 xs
= CREATE(xsource
);
1001 xs
->s
.ops
= &xsource_ops
;
1003 exec_read(&xs
->x
, sc
);
1007 /* --- @attach@ --- */
1009 static void xsource_destroy(source */
*s*/
);
1011 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1013 xsource
*xs
= (xsource
*)s
;
1016 /* --- Set up the source description string --- */
1020 exec_desc(&xs
->x
, &d
);
1021 dstr_puts(&d
, " -> ");
1022 dstr_puts(&d
, t
->desc
);
1023 xs
->s
.desc
= xstrdup(d
.buf
);
1027 /* --- Create the endpoints --- */
1029 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1031 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1035 endpt_join(e
, ee
, xs
->s
.desc
);
1037 /* --- Dispose of source and target --- */
1041 xsource_destroy(&xs
->s
);
1044 /* --- @destroy@ --- */
1046 static void xsource_destroy(source
*s
)
1048 xsource
*xs
= (xsource
*)s
;
1050 exec_destroy(&xs
->x
);
1054 /* --- Executable source operations --- */
1056 source_ops xsource_ops
= {
1058 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1061 /*----- Exec target description -------------------------------------------*/
1063 /* --- @option@ --- */
1065 static int xtarget_option(target
*t
, scanner
*sc
)
1067 xtarget
*xt
= (xtarget
*)t
;
1068 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1071 /* --- @read@ --- */
1073 static target
*xtarget_read(scanner
*sc
)
1078 if (!conf_prefix(sc
, "exec"))
1080 xt
= CREATE(xtarget
);
1081 xt
->t
.ops
= &xtarget_ops
;
1082 exec_read(&xt
->x
, sc
);
1083 exec_desc(&xt
->x
, &d
);
1084 xt
->t
.desc
= xstrdup(d
.buf
);
1089 /* --- @create@ --- */
1091 static endpt
*xtarget_create(target
*t
, const char *desc
)
1093 xtarget
*xt
= (xtarget
*)t
;
1094 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1098 /* --- @destroy@ --- */
1100 static void xtarget_destroy(target
*t
)
1102 xtarget
*xt
= (xtarget
*)t
;
1104 exec_destroy(&xt
->x
);
1108 /* --- Exec target operations --- */
1110 target_ops xtarget_ops
= {
1112 xtarget_option
, xtarget_read
, 0, xtarget_create
, xtarget_destroy
1115 /*----- That's all, folks -------------------------------------------------*/