3 * $Id: exec.c,v 1.1 1999/07/26 23:33:32 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.1 1999/07/26 23:33:32 mdw
33 * New sources and targets.
37 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
57 # include <sys/resource.h>
61 extern char **environ
;
69 #include <mLib/alloc.h>
70 #include <mLib/dstr.h>
72 #include <mLib/fdflags.h>
73 #include <mLib/report.h>
75 #include <mLib/selbuf.h>
90 /*----- Data structures ---------------------------------------------------*/
92 /* --- Resource usage --- */
96 typedef struct xlimit
{
97 #define R(r, n) struct rlimit n;
103 /* --- Environment variable modifications --- */
105 typedef struct xenv
{
115 /* --- Program options --- */
117 typedef struct xopts
{
126 #ifdef HAVE_SETRLIMIT
133 /* --- Executable program arguments --- */
135 typedef struct xargs
{
141 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
143 /* --- Executable endpoints --- */
145 typedef struct xept
{
147 struct xept
*next
, *prev
;
156 #define XEF_CLOSE 16u
159 /* --- Executable program data --- */
161 typedef struct xdata
{
166 /* --- Executable source block --- */
168 typedef struct xsource
{
173 /* --- Executable target block --- */
175 typedef struct xtarget
{
180 /*----- Static variables --------------------------------------------------*/
182 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
183 static xept
*xept_list
;
185 static sym_table env
;
187 /*----- Fiddling about with resource limits -------------------------------*/
189 #ifdef HAVE_SETRLIMIT
191 /* --- Table mapping user-level names to OS interface bits --- */
193 typedef struct rlimit_ent
{
200 static rlimit_ent rlimits
[] = {
201 #define R(r, n) { #n, #r, r, offsetof(xlimit, n) },
206 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
208 /* --- @rlimit_get@ --- *
210 * Arguments: @xlimit *xl@ = pointer to limit structure
214 * Use: Initializes a limit structure from the current limits.
217 static void rlimit_get(xlimit
*xl
)
221 for (r
= rlimits
; r
->name
; r
++) {
222 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
223 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
229 /* --- @rlimit_set@ --- *
231 * Arguments: @xlimit *xl@ = pointer to limit structure
235 * Use: Sets resource limits from the supplied limits structure.
238 static void rlimit_set(xlimit
*xl
)
242 for (r
= rlimits
; r
->name
; r
++) {
243 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
244 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
250 /* --- @rlimit_option@ --- */
252 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
254 CONF_BEGIN(sc
, "rlimit", "resource limit")
255 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
260 /* --- Find out which resource is being fiddled --- */
266 for (r
= rlimits
; r
->name
; r
++) {
267 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
268 if (r
->name
[sc
->d
.len
] == 0) {
272 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
280 rl
= RLIMIT(xl
, chosen
->off
);
283 /* --- Look for hard or soft restrictions --- */
289 if (sc
->t
== CTOK_WORD
) {
290 if ((i
= conf_enum(sc
, "soft,hard",
291 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
296 /* --- Now read the new value --- */
300 if (sc
->t
!= CTOK_WORD
)
301 error(sc
, "parse error, expected limit value");
303 if (conf_enum(sc
, "unlimited,infinity",
304 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
309 v
= strtol(sc
->d
.buf
, &p
, 0);
311 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
312 switch (tolower((unsigned char)*p
)) {
314 case 'b': v
*= 512; break;
317 case 'k': v
*= 1024; break;
318 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
323 /* --- Store the limit value away --- */
331 if (v
> rl
->rlim_max
)
332 error(sc
, "soft limit %l exceeds hard limit %l for %s",
333 v
, rl
->rlim_max
, chosen
->rname
);
338 if (rl
->rlim_cur
> v
)
349 /*----- Environment fiddling ----------------------------------------------*/
351 /* --- @xenv_option@ --- *
353 * Arguments: @xopts *xo@ = pointer to options block
354 * @scanner *sc@ = pointer to scanner
356 * Returns: Nonzero if claimed
358 * Use: Parses environment variable assignments.
361 static int xenv_option(xopts
*xo
, scanner
*sc
)
363 CONF_BEGIN(sc
, "env", "environment")
366 /* --- Unset a variable --- */
368 if (strcmp(sc
->d
.buf
, "unset") == 0) {
370 if (sc
->t
!= CTOK_WORD
)
371 error(sc
, "parse error, expected environment variable name");
373 xe
->name
= xstrdup(sc
->d
.buf
);
380 /* --- Clear the entire environment --- */
382 if (strcmp(sc
->d
.buf
, "clear") == 0) {
389 /* --- Allow `set' to be omitted if there's a prefix --- */
391 if (strcmp(sc
->d
.buf
, "set") == 0)
396 /* --- Set a variable --- */
398 if (sc
->t
!= CTOK_WORD
)
399 error(sc
, "parse error, expected environment variable name");
401 xe
->name
= xstrdup(sc
->d
.buf
);
405 if (sc
->t
!= CTOK_WORD
)
406 error(sc
, "parse error, expected environment variable value");
407 xe
->value
= xstrdup(sc
->d
.buf
);
415 xo
->etail
= &xe
->next
;
418 /* --- Nothing else to try --- */
423 /* --- @xenv_apply@ --- *
425 * Arguments: @xenv *xe@ = pointer to a variable change list
429 * Use: Modifies the environment (in @env@) according to the list.
432 static void xenv_apply(xenv
*xe
)
437 env_put(&env
, xe
->name
, xe
->value
);
448 /* --- @xenv_destroy@ --- *
450 * Arguments: @xenv *xe@ = pointer to a variable change list
454 * Use: Frees the memory used by an environment variable change list.
457 static void xenv_destroy(xenv
*xe
)
469 /*----- Miscellaneous good things -----------------------------------------*/
471 /* --- @x_tidy@ --- *
473 * Arguments: @xargs *xa@ = pointer to an arguments block
474 * @xopts *xo@ = pointer to an options block
478 * Use: Releases a reference to argument and options blocks.
481 static void x_tidy(xargs
*xa
, xopts
*xo
)
489 xenv_destroy(xo
->env
);
494 /*----- Executable endpoints ----------------------------------------------*/
496 /* --- @attach@ --- */
498 static void xept_error(char */
*p*/
, void */
*v*/
);
500 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
502 xept
*xe
= (xept
*)e
;
506 /* --- Make a pipe for standard error --- */
509 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
512 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
514 /* --- Fork a child, and handle an error if there was one --- */
516 if ((kid
= fork()) == -1) {
517 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
523 /* --- Do the child thing --- */
528 /* --- Fiddle with the file descriptors --- *
530 * Attach the other endpoint's descriptors to standard input and output.
531 * Attach my pipe to standard error. Mark everything as blocking and
532 * not-to-be-closed-on-exec at this end.
536 if (dup2(in
->fd
, STDIN_FILENO
) < 0 ||
537 dup2(out
->fd
, STDOUT_FILENO
) < 0 ||
538 dup2(fd
[1], STDERR_FILENO
) < 0) {
539 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
548 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
549 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
550 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
552 /* --- First of all set the @chroot@ prison --- */
554 if (xo
->root
&& chroot(xo
->root
)) {
555 moan("couldn't set `%s' as filesystem root: %s",
556 xo
->root
, strerror(errno
));
560 /* --- Now set the current directory --- */
562 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
563 moan("couldn't set `%s' as current directory: %s",
564 xo
->dir ? xo
->dir
: "/", strerror(errno
));
568 /* --- Set the resource limits --- */
570 #ifdef HAVE_SETRLIMIT
574 /* --- Set group id --- */
577 if (setgid(xo
->gid
)) {
578 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
581 #ifdef HAVE_SETGROUPS
582 if (setgroups(1, &xo
->gid
))
583 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
588 /* --- Set uid --- */
591 if (setuid(xo
->uid
)) {
592 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
597 /* --- Play with signal dispositions --- */
599 signal(SIGPIPE
, SIG_DFL
);
601 /* --- Fiddle with the environment --- */
603 xenv_apply(exec_opts
.env
);
604 xenv_apply(xe
->xo
->env
);
605 environ
= env_export(&env
);
607 /* --- Run the program --- */
609 execvp(xe
->xa
->file
, xe
->xa
->argv
);
610 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
614 /* --- The child's done; see to the parent --- */
617 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
619 xe
->next
= xept_list
;
622 xept_list
->prev
= xe
;
624 if (!(xe
->xo
->f
& XF_NOLOG
))
625 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
630 /* --- @xept_close@ --- */
632 static void xept_close(endpt
*e
)
634 xept
*xe
= (xept
*)e
;
636 x_tidy(xe
->xa
, xe
->xo
);
642 /* --- @xept_destroy@ --- */
644 static void xept_destroy(xept
*xe
)
646 /* --- First emit the news about the process --- */
648 if (xe
->xo
->f
& XF_NOLOG
)
650 else if (WIFEXITED(xe
->st
)) {
651 if (WEXITSTATUS(xe
->st
) == 0)
652 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
654 fw_log(-1, "[%s] pid %i failed: status %i",
655 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
657 } else if (WIFSIGNALED(xe
->st
)) {
659 #ifdef HAVE_STRSIGNAL
660 s
= strsignal(WTERMSIG(xe
->st
));
661 #elif HAVE__SYS_SIGLIST
662 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
665 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
668 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
670 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
672 /* --- Free up the parent-side resources --- */
675 xe
->next
->prev
= xe
->prev
;
677 xe
->prev
->next
= xe
->next
;
679 xept_list
= xe
->next
;
681 x_tidy(xe
->xa
, xe
->xo
);
686 /* --- @xept_chld@ --- *
688 * Arguments: @int n@ = signal number
689 * @void *p@ = uninteresting pointer
693 * Use: Deals with child death situations.
696 static void xept_chld(int n
, void *p
)
701 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
702 xept
*xe
= xept_list
;
706 if (kid
== xxe
->kid
) {
708 xxe
->e
.f
|= XEF_EXIT
;
709 if (xxe
->e
.f
& XEF_CLOSE
)
717 /* --- @xept_error@ --- *
719 * Arguments: @char *p@ = pointer to string read from stderr
720 * @void *v@ = pointer to by endpoint
724 * Use: Handles error reports from a child process.
727 static void xept_error(char *p
, void *v
)
731 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
733 selbuf_disable(&xe
->err
);
734 close(xe
->err
.reader
.fd
);
735 xe
->e
.f
|= XEF_CLOSE
;
736 if (xe
->e
.f
& XEF_EXIT
)
741 /* --- Endpoint operations --- */
743 static endpt_ops xept_ops
= { xept_attach
, 0, xept_close
};
745 /*----- General operations on sources and targets -------------------------*/
747 /* --- @exec_init@ --- *
753 * Use: Initializes the executable problem source and target.
758 rlimit_get(&exec_opts
.xl
);
759 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
761 env_import(&env
, environ
);
764 /* --- @exec_option@ --- */
766 static int exec_option(xdata
*x
, scanner
*sc
)
768 xopts
*xo
= x ? x
->xo
: &exec_opts
;
770 CONF_BEGIN(sc
, "exec", "executable");
772 /* --- Logging settings --- */
774 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
775 strcmp(sc
->d
.buf
, "log") == 0) {
779 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
786 /* --- Current directory setting --- *
788 * Lots of possibilities to guard against possible brainoes.
791 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
792 strcmp(sc
->d
.buf
, "cd") == 0 ||
793 strcmp(sc
->d
.buf
, "chdir") == 0 ||
794 strcmp(sc
->d
.buf
, "cwd") == 0) {
799 conf_name(sc
, '/', &d
);
800 xo
->dir
= xstrdup(d
.buf
);
805 /* --- Set a chroot prison --- */
807 if (strcmp(sc
->d
.buf
, "root") == 0 ||
808 strcmp(sc
->d
.buf
, "chroot") == 0) {
813 conf_name(sc
, '/', &d
);
814 xo
->root
= xstrdup(d
.buf
);
819 /* --- Set the target user id --- */
821 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
822 strcmp(sc
->d
.buf
, "user") == 0) {
826 if (sc
->t
!= CTOK_WORD
)
827 error(sc
, "parse error, expected user name or uid");
828 if (isdigit((unsigned char)*sc
->d
.buf
))
829 xo
->uid
= atoi(sc
->d
.buf
);
831 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
833 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
834 xo
->uid
= pw
->pw_uid
;
840 /* --- Set the target group id --- */
842 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
843 strcmp(sc
->d
.buf
, "group") == 0) {
847 if (sc
->t
!= CTOK_WORD
)
848 error(sc
, "parse error, expected group name or gid");
849 if (isdigit((unsigned char)*sc
->d
.buf
))
850 xo
->gid
= atoi(sc
->d
.buf
);
852 struct group
*gr
= getgrnam(sc
->d
.buf
);
854 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
855 xo
->gid
= gr
->gr_gid
;
861 /* --- Now try resource limit settings --- */
863 if (rlimit_option(&xo
->xl
, sc
))
866 /* --- And then environment settings --- */
868 if (xenv_option(xo
, sc
))
871 /* --- Nothing found --- */
876 /* --- @exec_desc@ --- */
878 static void exec_desc(xdata
*x
, dstr
*d
)
882 dstr_puts(d
, "exec ");
883 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
884 dstr_puts(d
, x
->xa
->file
);
887 for (p
= x
->xa
->argv
; *p
; p
++) {
896 /* --- @exec_read@ --- */
898 static void exec_read(xdata
*x
, scanner
*sc
)
904 /* --- Read the first word --- *
906 * This is either a shell command or the actual program to run.
909 if (sc
->t
== CTOK_WORD
) {
910 dstr_putd(&d
, &sc
->d
); d
.len
++;
915 /* --- See if there's a list of arguments --- *
917 * If not, then the thing I saw was a shell command, so build the proper
918 * arguments for that.
924 error(sc
, "parse error, expected shell command or argument list");
925 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
926 p
= (char *)(xa
->argv
+ 4);
929 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
930 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
931 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
935 /* --- Snarf in a list of arguments --- */
942 /* --- Strip off the leading `[' --- */
946 /* --- Read a sequence of arguments --- */
948 while (sc
->t
== CTOK_WORD
) {
949 dstr_putd(&d
, &sc
->d
); d
.len
++;
954 /* --- Expect the closing `]' --- */
957 error(sc
, "parse error, missing `]'");
960 /* --- If there are no arguments, whinge --- */
963 error(sc
, "must specify at least one argument");
965 /* --- Allocate a lump of memory for the array --- */
967 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
970 p
= (char *)(v
+ argc
+ 1);
971 memcpy(p
, d
.buf
, d
.len
);
976 /* --- Start dumping addresses into the @argv@ array --- */
980 while (*p
++ && p
< q
)
988 /* --- Do some other setting up --- */
992 x
->xo
= CREATE(xopts
);
998 /* --- @exec_endpt@ --- */
1000 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
1002 xept
*xe
= CREATE(xept
);
1003 xe
->e
.ops
= &xept_ops
;
1007 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
1008 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
1014 /* --- @exec_destroy@ --- */
1016 static void exec_destroy(xdata
*x
)
1018 x_tidy(x
->xa
, x
->xo
);
1021 /*----- Source definition -------------------------------------------------*/
1023 /* --- @option@ --- */
1025 static int xsource_option(source
*s
, scanner
*sc
)
1027 xsource
*xs
= (xsource
*)s
;
1028 return (exec_option(xs ?
&xs
->x
: 0, sc
));
1031 /* --- @read@ --- */
1033 static source
*xsource_read(scanner
*sc
)
1037 if (!conf_prefix(sc
, "exec"))
1039 xs
= CREATE(xsource
);
1040 xs
->s
.ops
= &xsource_ops
;
1042 exec_read(&xs
->x
, sc
);
1046 /* --- @attach@ --- */
1048 static void xsource_destroy(source */
*s*/
);
1050 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1052 xsource
*xs
= (xsource
*)s
;
1055 /* --- Set up the source description string --- */
1059 exec_desc(&xs
->x
, &d
);
1060 dstr_puts(&d
, " -> ");
1061 dstr_puts(&d
, t
->desc
);
1062 xs
->s
.desc
= xstrdup(d
.buf
);
1066 /* --- Create the endpoints --- */
1068 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1070 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1076 /* --- Dispose of source and target --- */
1080 xsource_destroy(&xs
->s
);
1083 /* --- @destroy@ --- */
1085 static void xsource_destroy(source
*s
)
1087 xsource
*xs
= (xsource
*)s
;
1088 exec_destroy(&xs
->x
);
1092 /* --- Executable source operations --- */
1094 source_ops xsource_ops
= {
1096 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1099 /*----- Exec target description -------------------------------------------*/
1101 /* --- @option@ --- */
1103 static int xtarget_option(target
*t
, scanner
*sc
)
1105 xtarget
*xt
= (xtarget
*)t
;
1106 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1109 /* --- @read@ --- */
1111 static target
*xtarget_read(scanner
*sc
)
1116 if (!conf_prefix(sc
, "exec"))
1118 xt
= CREATE(xtarget
);
1119 xt
->t
.ops
= &xtarget_ops
;
1120 exec_read(&xt
->x
, sc
);
1121 exec_desc(&xt
->x
, &d
);
1122 xt
->t
.desc
= xstrdup(d
.buf
);
1127 /* --- @create@ --- */
1129 static endpt
*xtarget_create(target
*t
, const char *desc
)
1131 xtarget
*xt
= (xtarget
*)t
;
1132 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1136 /* --- @destroy@ --- */
1138 static void xtarget_destroy(target
*t
)
1140 xtarget
*xt
= (xtarget
*)t
;
1141 exec_destroy(&xt
->x
);
1145 /* --- Exec target operations --- */
1147 target_ops xtarget_ops
= {
1149 xtarget_option
, xtarget_read
, xtarget_create
, xtarget_destroy
1152 /*----- That's all, folks -------------------------------------------------*/