3 * $Id: exec.c,v 1.3 2000/07/01 11:28:52 mdw Exp $
5 * Source and target for executable programs
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 2000/07/01 11:28:52 mdw
33 * Use new mLib selbuf features.
35 * Revision 1.2 1999/10/22 22:46:17 mdw
36 * When a non-file endpoint is attached to a file, keep the file endpoint
37 * open until the nonfile is done. This stops socket sources from
38 * resetting their connection limits too early.
40 * Revision 1.1 1999/07/26 23:33:32 mdw
41 * New sources and targets.
45 /*----- Header files ------------------------------------------------------*/
58 #include <sys/types.h>
65 # include <sys/resource.h>
69 extern char **environ
;
77 #include <mLib/alloc.h>
78 #include <mLib/dstr.h>
80 #include <mLib/fdflags.h>
81 #include <mLib/report.h>
83 #include <mLib/selbuf.h>
98 /*----- Data structures ---------------------------------------------------*/
100 /* --- Resource usage --- */
102 #ifdef HAVE_SETRLIMIT
104 typedef struct xlimit
{
105 #define R(r, n) struct rlimit n;
111 /* --- Environment variable modifications --- */
113 typedef struct xenv
{
123 /* --- Program options --- */
125 typedef struct xopts
{
134 #ifdef HAVE_SETRLIMIT
141 /* --- Executable program arguments --- */
143 typedef struct xargs
{
149 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
151 /* --- Executable endpoints --- */
153 typedef struct xept
{
155 struct xept
*next
, *prev
;
165 #define XEF_CLOSE 16u
168 /* --- Executable program data --- */
170 typedef struct xdata
{
175 /* --- Executable source block --- */
177 typedef struct xsource
{
182 /* --- Executable target block --- */
184 typedef struct xtarget
{
189 /*----- Static variables --------------------------------------------------*/
191 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
192 static xept
*xept_list
;
194 static sym_table env
;
196 /*----- Fiddling about with resource limits -------------------------------*/
198 #ifdef HAVE_SETRLIMIT
200 /* --- Table mapping user-level names to OS interface bits --- */
202 typedef struct rlimit_ent
{
209 static rlimit_ent rlimits
[] = {
210 #define R(r, n) { #n, #r, r, offsetof(xlimit, n) },
215 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
217 /* --- @rlimit_get@ --- *
219 * Arguments: @xlimit *xl@ = pointer to limit structure
223 * Use: Initializes a limit structure from the current limits.
226 static void rlimit_get(xlimit
*xl
)
230 for (r
= rlimits
; r
->name
; r
++) {
231 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
232 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
238 /* --- @rlimit_set@ --- *
240 * Arguments: @xlimit *xl@ = pointer to limit structure
244 * Use: Sets resource limits from the supplied limits structure.
247 static void rlimit_set(xlimit
*xl
)
251 for (r
= rlimits
; r
->name
; r
++) {
252 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
253 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
259 /* --- @rlimit_option@ --- */
261 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
263 CONF_BEGIN(sc
, "rlimit", "resource limit")
264 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
269 /* --- Find out which resource is being fiddled --- */
275 for (r
= rlimits
; r
->name
; r
++) {
276 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
277 if (r
->name
[sc
->d
.len
] == 0) {
281 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
289 rl
= RLIMIT(xl
, chosen
->off
);
292 /* --- Look for hard or soft restrictions --- */
298 if (sc
->t
== CTOK_WORD
) {
299 if ((i
= conf_enum(sc
, "soft,hard",
300 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
305 /* --- Now read the new value --- */
309 if (sc
->t
!= CTOK_WORD
)
310 error(sc
, "parse error, expected limit value");
312 if (conf_enum(sc
, "unlimited,infinity",
313 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
318 v
= strtol(sc
->d
.buf
, &p
, 0);
320 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
321 switch (tolower((unsigned char)*p
)) {
323 case 'b': v
*= 512; break;
326 case 'k': v
*= 1024; break;
327 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
332 /* --- Store the limit value away --- */
340 if (v
> rl
->rlim_max
)
341 error(sc
, "soft limit %l exceeds hard limit %l for %s",
342 v
, rl
->rlim_max
, chosen
->rname
);
347 if (rl
->rlim_cur
> v
)
358 /*----- Environment fiddling ----------------------------------------------*/
360 /* --- @xenv_option@ --- *
362 * Arguments: @xopts *xo@ = pointer to options block
363 * @scanner *sc@ = pointer to scanner
365 * Returns: Nonzero if claimed
367 * Use: Parses environment variable assignments.
370 static int xenv_option(xopts
*xo
, scanner
*sc
)
372 CONF_BEGIN(sc
, "env", "environment")
375 /* --- Unset a variable --- */
377 if (strcmp(sc
->d
.buf
, "unset") == 0) {
379 if (sc
->t
!= CTOK_WORD
)
380 error(sc
, "parse error, expected environment variable name");
382 xe
->name
= xstrdup(sc
->d
.buf
);
389 /* --- Clear the entire environment --- */
391 if (strcmp(sc
->d
.buf
, "clear") == 0) {
398 /* --- Allow `set' to be omitted if there's a prefix --- */
400 if (strcmp(sc
->d
.buf
, "set") == 0)
405 /* --- Set a variable --- */
407 if (sc
->t
!= CTOK_WORD
)
408 error(sc
, "parse error, expected environment variable name");
410 xe
->name
= xstrdup(sc
->d
.buf
);
414 if (sc
->t
!= CTOK_WORD
)
415 error(sc
, "parse error, expected environment variable value");
416 xe
->value
= xstrdup(sc
->d
.buf
);
424 xo
->etail
= &xe
->next
;
427 /* --- Nothing else to try --- */
432 /* --- @xenv_apply@ --- *
434 * Arguments: @xenv *xe@ = pointer to a variable change list
438 * Use: Modifies the environment (in @env@) according to the list.
441 static void xenv_apply(xenv
*xe
)
446 env_put(&env
, xe
->name
, xe
->value
);
457 /* --- @xenv_destroy@ --- *
459 * Arguments: @xenv *xe@ = pointer to a variable change list
463 * Use: Frees the memory used by an environment variable change list.
466 static void xenv_destroy(xenv
*xe
)
478 /*----- Miscellaneous good things -----------------------------------------*/
480 /* --- @x_tidy@ --- *
482 * Arguments: @xargs *xa@ = pointer to an arguments block
483 * @xopts *xo@ = pointer to an options block
487 * Use: Releases a reference to argument and options blocks.
490 static void x_tidy(xargs
*xa
, xopts
*xo
)
498 xenv_destroy(xo
->env
);
503 /*----- Executable endpoints ----------------------------------------------*/
505 /* --- @attach@ --- */
507 static void xept_error(char */
*p*/
, void */
*v*/
);
509 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
511 xept
*xe
= (xept
*)e
;
515 /* --- Make a pipe for standard error --- */
518 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
521 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
523 /* --- Fork a child, and handle an error if there was one --- */
525 if ((kid
= fork()) == -1) {
526 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
532 /* --- Do the child thing --- */
537 /* --- Fiddle with the file descriptors --- *
539 * Attach the other endpoint's descriptors to standard input and output.
540 * Attach my pipe to standard error. Mark everything as blocking and
541 * not-to-be-closed-on-exec at this end.
545 if (dup2(in
->fd
, STDIN_FILENO
) < 0 ||
546 dup2(out
->fd
, STDOUT_FILENO
) < 0 ||
547 dup2(fd
[1], STDERR_FILENO
) < 0) {
548 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
557 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
558 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
559 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
561 /* --- First of all set the @chroot@ prison --- */
563 if (xo
->root
&& chroot(xo
->root
)) {
564 moan("couldn't set `%s' as filesystem root: %s",
565 xo
->root
, strerror(errno
));
569 /* --- Now set the current directory --- */
571 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
572 moan("couldn't set `%s' as current directory: %s",
573 xo
->dir ? xo
->dir
: "/", strerror(errno
));
577 /* --- Set the resource limits --- */
579 #ifdef HAVE_SETRLIMIT
583 /* --- Set group id --- */
586 if (setgid(xo
->gid
)) {
587 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
590 #ifdef HAVE_SETGROUPS
591 if (setgroups(1, &xo
->gid
))
592 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
597 /* --- Set uid --- */
600 if (setuid(xo
->uid
)) {
601 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
606 /* --- Play with signal dispositions --- */
608 signal(SIGPIPE
, SIG_DFL
);
610 /* --- Fiddle with the environment --- */
612 xenv_apply(exec_opts
.env
);
613 xenv_apply(xe
->xo
->env
);
614 environ
= env_export(&env
);
616 /* --- Run the program --- */
618 execvp(xe
->xa
->file
, xe
->xa
->argv
);
619 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
623 /* --- The child's done; see to the parent --- */
626 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
628 xe
->next
= xept_list
;
631 xept_list
->prev
= xe
;
633 if (!(xe
->xo
->f
& XF_NOLOG
))
634 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
639 /* --- @xept_file@ --- */
641 static void xept_file(endpt
*e
, endpt
*f
)
643 xept
*xe
= (xept
*)e
;
647 /* --- @xept_close@ --- */
649 static void xept_close(endpt
*e
)
651 xept
*xe
= (xept
*)e
;
654 xe
->f
->ops
->close(xe
->f
);
655 x_tidy(xe
->xa
, xe
->xo
);
661 /* --- @xept_destroy@ --- */
663 static void xept_destroy(xept
*xe
)
665 /* --- First emit the news about the process --- */
667 if (xe
->xo
->f
& XF_NOLOG
)
669 else if (WIFEXITED(xe
->st
)) {
670 if (WEXITSTATUS(xe
->st
) == 0)
671 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
673 fw_log(-1, "[%s] pid %i failed: status %i",
674 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
676 } else if (WIFSIGNALED(xe
->st
)) {
678 #ifdef HAVE_STRSIGNAL
679 s
= strsignal(WTERMSIG(xe
->st
));
680 #elif HAVE__SYS_SIGLIST
681 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
684 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
687 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
689 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
691 /* --- Free up the parent-side resources --- */
694 xe
->next
->prev
= xe
->prev
;
696 xe
->prev
->next
= xe
->next
;
698 xept_list
= xe
->next
;
701 xe
->f
->ops
->close(xe
->f
);
702 x_tidy(xe
->xa
, xe
->xo
);
707 /* --- @xept_chld@ --- *
709 * Arguments: @int n@ = signal number
710 * @void *p@ = uninteresting pointer
714 * Use: Deals with child death situations.
717 static void xept_chld(int n
, void *p
)
722 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
723 xept
*xe
= xept_list
;
727 if (kid
== xxe
->kid
) {
729 xxe
->e
.f
|= XEF_EXIT
;
730 if (xxe
->e
.f
& XEF_CLOSE
)
738 /* --- @xept_error@ --- *
740 * Arguments: @char *p@ = pointer to string read from stderr
741 * @void *v@ = pointer to by endpoint
745 * Use: Handles error reports from a child process.
748 static void xept_error(char *p
, void *v
)
752 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
754 close(xe
->err
.reader
.fd
);
755 selbuf_destroy(&xe
->err
);
756 xe
->e
.f
|= XEF_CLOSE
;
757 if (xe
->e
.f
& XEF_EXIT
)
762 /* --- Endpoint operations --- */
764 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
766 /*----- General operations on sources and targets -------------------------*/
768 /* --- @exec_init@ --- *
774 * Use: Initializes the executable problem source and target.
779 rlimit_get(&exec_opts
.xl
);
780 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
782 env_import(&env
, environ
);
785 /* --- @exec_option@ --- */
787 static int exec_option(xdata
*x
, scanner
*sc
)
789 xopts
*xo
= x ? x
->xo
: &exec_opts
;
791 CONF_BEGIN(sc
, "exec", "executable");
793 /* --- Logging settings --- */
795 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
796 strcmp(sc
->d
.buf
, "log") == 0) {
800 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
807 /* --- Current directory setting --- *
809 * Lots of possibilities to guard against possible brainoes.
812 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
813 strcmp(sc
->d
.buf
, "cd") == 0 ||
814 strcmp(sc
->d
.buf
, "chdir") == 0 ||
815 strcmp(sc
->d
.buf
, "cwd") == 0) {
820 conf_name(sc
, '/', &d
);
821 xo
->dir
= xstrdup(d
.buf
);
826 /* --- Set a chroot prison --- */
828 if (strcmp(sc
->d
.buf
, "root") == 0 ||
829 strcmp(sc
->d
.buf
, "chroot") == 0) {
834 conf_name(sc
, '/', &d
);
835 xo
->root
= xstrdup(d
.buf
);
840 /* --- Set the target user id --- */
842 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
843 strcmp(sc
->d
.buf
, "user") == 0) {
847 if (sc
->t
!= CTOK_WORD
)
848 error(sc
, "parse error, expected user name or uid");
849 if (isdigit((unsigned char)*sc
->d
.buf
))
850 xo
->uid
= atoi(sc
->d
.buf
);
852 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
854 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
855 xo
->uid
= pw
->pw_uid
;
861 /* --- Set the target group id --- */
863 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
864 strcmp(sc
->d
.buf
, "group") == 0) {
868 if (sc
->t
!= CTOK_WORD
)
869 error(sc
, "parse error, expected group name or gid");
870 if (isdigit((unsigned char)*sc
->d
.buf
))
871 xo
->gid
= atoi(sc
->d
.buf
);
873 struct group
*gr
= getgrnam(sc
->d
.buf
);
875 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
876 xo
->gid
= gr
->gr_gid
;
882 /* --- Now try resource limit settings --- */
884 if (rlimit_option(&xo
->xl
, sc
))
887 /* --- And then environment settings --- */
889 if (xenv_option(xo
, sc
))
892 /* --- Nothing found --- */
897 /* --- @exec_desc@ --- */
899 static void exec_desc(xdata
*x
, dstr
*d
)
903 dstr_puts(d
, "exec ");
904 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
905 dstr_puts(d
, x
->xa
->file
);
908 for (p
= x
->xa
->argv
; *p
; p
++) {
917 /* --- @exec_read@ --- */
919 static void exec_read(xdata
*x
, scanner
*sc
)
925 /* --- Read the first word --- *
927 * This is either a shell command or the actual program to run.
930 if (sc
->t
== CTOK_WORD
) {
931 dstr_putd(&d
, &sc
->d
); d
.len
++;
936 /* --- See if there's a list of arguments --- *
938 * If not, then the thing I saw was a shell command, so build the proper
939 * arguments for that.
945 error(sc
, "parse error, expected shell command or argument list");
946 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
947 p
= (char *)(xa
->argv
+ 4);
950 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
951 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
952 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
956 /* --- Snarf in a list of arguments --- */
963 /* --- Strip off the leading `[' --- */
967 /* --- Read a sequence of arguments --- */
969 while (sc
->t
== CTOK_WORD
) {
970 dstr_putd(&d
, &sc
->d
); d
.len
++;
975 /* --- Expect the closing `]' --- */
978 error(sc
, "parse error, missing `]'");
981 /* --- If there are no arguments, whinge --- */
984 error(sc
, "must specify at least one argument");
986 /* --- Allocate a lump of memory for the array --- */
988 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
991 p
= (char *)(v
+ argc
+ 1);
992 memcpy(p
, d
.buf
, d
.len
);
997 /* --- Start dumping addresses into the @argv@ array --- */
1001 while (*p
++ && p
< q
)
1009 /* --- Do some other setting up --- */
1013 x
->xo
= CREATE(xopts
);
1019 /* --- @exec_endpt@ --- */
1021 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
1023 xept
*xe
= CREATE(xept
);
1024 xe
->e
.ops
= &xept_ops
;
1028 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
1029 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
1036 /* --- @exec_destroy@ --- */
1038 static void exec_destroy(xdata
*x
)
1040 x_tidy(x
->xa
, x
->xo
);
1043 /*----- Source definition -------------------------------------------------*/
1045 /* --- @option@ --- */
1047 static int xsource_option(source
*s
, scanner
*sc
)
1049 xsource
*xs
= (xsource
*)s
;
1050 return (exec_option(xs ?
&xs
->x
: 0, sc
));
1053 /* --- @read@ --- */
1055 static source
*xsource_read(scanner
*sc
)
1059 if (!conf_prefix(sc
, "exec"))
1061 xs
= CREATE(xsource
);
1062 xs
->s
.ops
= &xsource_ops
;
1064 exec_read(&xs
->x
, sc
);
1068 /* --- @attach@ --- */
1070 static void xsource_destroy(source */
*s*/
);
1072 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1074 xsource
*xs
= (xsource
*)s
;
1077 /* --- Set up the source description string --- */
1081 exec_desc(&xs
->x
, &d
);
1082 dstr_puts(&d
, " -> ");
1083 dstr_puts(&d
, t
->desc
);
1084 xs
->s
.desc
= xstrdup(d
.buf
);
1088 /* --- Create the endpoints --- */
1090 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1092 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1098 /* --- Dispose of source and target --- */
1102 xsource_destroy(&xs
->s
);
1105 /* --- @destroy@ --- */
1107 static void xsource_destroy(source
*s
)
1109 xsource
*xs
= (xsource
*)s
;
1110 exec_destroy(&xs
->x
);
1114 /* --- Executable source operations --- */
1116 source_ops xsource_ops
= {
1118 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1121 /*----- Exec target description -------------------------------------------*/
1123 /* --- @option@ --- */
1125 static int xtarget_option(target
*t
, scanner
*sc
)
1127 xtarget
*xt
= (xtarget
*)t
;
1128 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1131 /* --- @read@ --- */
1133 static target
*xtarget_read(scanner
*sc
)
1138 if (!conf_prefix(sc
, "exec"))
1140 xt
= CREATE(xtarget
);
1141 xt
->t
.ops
= &xtarget_ops
;
1142 exec_read(&xt
->x
, sc
);
1143 exec_desc(&xt
->x
, &d
);
1144 xt
->t
.desc
= xstrdup(d
.buf
);
1149 /* --- @create@ --- */
1151 static endpt
*xtarget_create(target
*t
, const char *desc
)
1153 xtarget
*xt
= (xtarget
*)t
;
1154 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1158 /* --- @destroy@ --- */
1160 static void xtarget_destroy(target
*t
)
1162 xtarget
*xt
= (xtarget
*)t
;
1163 exec_destroy(&xt
->x
);
1167 /* --- Exec target operations --- */
1169 target_ops xtarget_ops
= {
1171 xtarget_option
, xtarget_read
, xtarget_create
, xtarget_destroy
1174 /*----- That's all, folks -------------------------------------------------*/