3 * Source and target for executable programs
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' 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 * `fw' 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 `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
47 # include <sys/resource.h>
51 extern char **environ
;
59 #include <mLib/alloc.h>
60 #include <mLib/dstr.h>
62 #include <mLib/fdflags.h>
63 #include <mLib/report.h>
65 #include <mLib/selbuf.h>
80 /*----- Data structures ---------------------------------------------------*/
82 /* --- Resource usage --- */
86 typedef struct xlimit
{
87 #define R(r, n) struct rlimit n;
93 /* --- Environment variable modifications --- */
105 /* --- Program options --- */
107 typedef struct xopts
{
116 #ifdef HAVE_SETRLIMIT
123 /* --- Executable program arguments --- */
125 typedef struct xargs
{
131 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
133 /* --- Executable endpoints --- */
135 typedef struct xept
{
137 struct xept
*next
, *prev
;
147 #define XEF_CLOSE 16u
150 /* --- Executable program data --- */
152 typedef struct xdata
{
157 /* --- Executable source block --- */
159 typedef struct xsource
{
164 /* --- Executable target block --- */
166 typedef struct xtarget
{
171 /*----- Static variables --------------------------------------------------*/
173 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
174 static xept
*xept_list
;
176 static sym_table env
;
178 /*----- Fiddling about with resource limits -------------------------------*/
180 #ifdef HAVE_SETRLIMIT
182 /* --- Table mapping user-level names to OS interface bits --- */
184 typedef struct rlimit_ent
{
191 static rlimit_ent rlimits
[] = {
192 #define R(r, n) { #n, #r, r, offsetof(xlimit, n) },
197 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
199 /* --- @rlimit_get@ --- *
201 * Arguments: @xlimit *xl@ = pointer to limit structure
205 * Use: Initializes a limit structure from the current limits.
208 static void rlimit_get(xlimit
*xl
)
212 for (r
= rlimits
; r
->name
; r
++) {
213 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
214 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
220 /* --- @rlimit_set@ --- *
222 * Arguments: @xlimit *xl@ = pointer to limit structure
226 * Use: Sets resource limits from the supplied limits structure.
229 static void rlimit_set(xlimit
*xl
)
233 for (r
= rlimits
; r
->name
; r
++) {
234 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
235 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
241 /* --- @rlimit_option@ --- */
243 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
245 CONF_BEGIN(sc
, "rlimit", "resource limit")
246 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
251 /* --- Find out which resource is being fiddled --- */
257 for (r
= rlimits
; r
->name
; r
++) {
258 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
259 if (r
->name
[sc
->d
.len
] == 0) {
263 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
271 rl
= RLIMIT(xl
, chosen
->off
);
274 /* --- Look for hard or soft restrictions --- */
280 if (sc
->t
== CTOK_WORD
) {
281 if ((i
= conf_enum(sc
, "soft,hard",
282 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
287 /* --- Now read the new value --- */
291 if (sc
->t
!= CTOK_WORD
)
292 error(sc
, "parse error, expected limit value");
294 if (conf_enum(sc
, "unlimited,infinity",
295 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
300 v
= strtol(sc
->d
.buf
, &p
, 0);
302 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
303 switch (tolower((unsigned char)*p
)) {
305 case 'b': v
*= 512; break;
308 case 'k': v
*= 1024; break;
309 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
314 /* --- Store the limit value away --- */
322 if (v
> rl
->rlim_max
)
323 error(sc
, "soft limit %l exceeds hard limit %l for %s",
324 v
, rl
->rlim_max
, chosen
->rname
);
329 if (rl
->rlim_cur
> v
)
340 /*----- Environment fiddling ----------------------------------------------*/
342 /* --- @xenv_option@ --- *
344 * Arguments: @xopts *xo@ = pointer to options block
345 * @scanner *sc@ = pointer to scanner
347 * Returns: Nonzero if claimed
349 * Use: Parses environment variable assignments.
352 static int xenv_option(xopts
*xo
, scanner
*sc
)
354 CONF_BEGIN(sc
, "env", "environment")
357 /* --- Unset a variable --- */
359 if (strcmp(sc
->d
.buf
, "unset") == 0) {
361 if (sc
->t
!= CTOK_WORD
)
362 error(sc
, "parse error, expected environment variable name");
364 xe
->name
= xstrdup(sc
->d
.buf
);
371 /* --- Clear the entire environment --- */
373 if (strcmp(sc
->d
.buf
, "clear") == 0) {
380 /* --- Allow `set' to be omitted if there's a prefix --- */
382 if (strcmp(sc
->d
.buf
, "set") == 0)
387 /* --- Set a variable --- */
389 if (sc
->t
!= CTOK_WORD
)
390 error(sc
, "parse error, expected environment variable name");
392 xe
->name
= xstrdup(sc
->d
.buf
);
396 if (sc
->t
!= CTOK_WORD
)
397 error(sc
, "parse error, expected environment variable value");
398 xe
->value
= xstrdup(sc
->d
.buf
);
406 xo
->etail
= &xe
->next
;
409 /* --- Nothing else to try --- */
414 /* --- @xenv_apply@ --- *
416 * Arguments: @xenv *xe@ = pointer to a variable change list
420 * Use: Modifies the environment (in @env@) according to the list.
423 static void xenv_apply(xenv
*xe
)
428 env_put(&env
, xe
->name
, xe
->value
);
439 /* --- @xenv_destroy@ --- *
441 * Arguments: @xenv *xe@ = pointer to a variable change list
445 * Use: Frees the memory used by an environment variable change list.
448 static void xenv_destroy(xenv
*xe
)
460 /*----- Miscellaneous good things -----------------------------------------*/
462 /* --- @x_tidy@ --- *
464 * Arguments: @xargs *xa@ = pointer to an arguments block
465 * @xopts *xo@ = pointer to an options block
469 * Use: Releases a reference to argument and options blocks.
472 static void x_tidy(xargs
*xa
, xopts
*xo
)
480 xenv_destroy(xo
->env
);
485 /*----- Executable endpoints ----------------------------------------------*/
487 /* --- @attach@ --- */
489 static void xept_error(char */
*p*/
, size_t /*len*/, void */
*v*/
);
491 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
493 xept
*xe
= (xept
*)e
;
497 /* --- Make a pipe for standard error --- */
500 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
503 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
505 /* --- Fork a child, and handle an error if there was one --- */
507 if ((kid
= fork()) == -1) {
508 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
514 /* --- Do the child thing --- */
519 /* --- Fiddle with the file descriptors --- *
521 * Attach the other endpoint's descriptors to standard input and output.
522 * Attach my pipe to standard error. Mark everything as blocking and
523 * not-to-be-closed-on-exec at this end.
527 if (dup2(in
->fd
, STDIN_FILENO
) < 0 ||
528 dup2(out
->fd
, STDOUT_FILENO
) < 0 ||
529 dup2(fd
[1], STDERR_FILENO
) < 0) {
530 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
539 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
540 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
541 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
543 /* --- First of all set the @chroot@ prison --- */
545 if (xo
->root
&& chroot(xo
->root
)) {
546 moan("couldn't set `%s' as filesystem root: %s",
547 xo
->root
, strerror(errno
));
551 /* --- Now set the current directory --- */
553 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
554 moan("couldn't set `%s' as current directory: %s",
555 xo
->dir ? xo
->dir
: "/", strerror(errno
));
559 /* --- Set the resource limits --- */
561 #ifdef HAVE_SETRLIMIT
565 /* --- Set group id --- */
567 if (xo
->gid
!= (gid_t
)-1) {
568 if (setgid(xo
->gid
)) {
569 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
572 #ifdef HAVE_SETGROUPS
573 if (setgroups(1, &xo
->gid
))
574 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
579 /* --- Set uid --- */
581 if (xo
->uid
!= (uid_t
)-1) {
582 if (setuid(xo
->uid
)) {
583 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
588 /* --- Play with signal dispositions --- */
590 signal(SIGPIPE
, SIG_DFL
);
592 /* --- Fiddle with the environment --- */
594 xenv_apply(exec_opts
.env
);
595 xenv_apply(xe
->xo
->env
);
596 environ
= env_export(&env
);
598 /* --- Run the program --- */
600 execvp(xe
->xa
->file
, xe
->xa
->argv
);
601 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
605 /* --- The child's done; see to the parent --- */
608 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
610 xe
->next
= xept_list
;
613 xept_list
->prev
= xe
;
615 if (!(xe
->xo
->f
& XF_NOLOG
))
616 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
621 /* --- @xept_file@ --- */
623 static void xept_file(endpt
*e
, endpt
*f
)
625 xept
*xe
= (xept
*)e
;
629 /* --- @xept_close@ --- */
631 static void xept_close(endpt
*e
)
633 xept
*xe
= (xept
*)e
;
636 xe
->f
->ops
->close(xe
->f
);
637 x_tidy(xe
->xa
, xe
->xo
);
643 /* --- @xept_destroy@ --- */
645 static void xept_destroy(xept
*xe
)
647 /* --- First emit the news about the process --- */
649 if (xe
->xo
->f
& XF_NOLOG
)
651 else if (WIFEXITED(xe
->st
)) {
652 if (WEXITSTATUS(xe
->st
) == 0)
653 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
655 fw_log(-1, "[%s] pid %i failed: status %i",
656 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
658 } else if (WIFSIGNALED(xe
->st
)) {
660 #ifdef HAVE_STRSIGNAL
661 s
= strsignal(WTERMSIG(xe
->st
));
662 #elif HAVE__SYS_SIGLIST
663 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
666 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
669 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
671 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
673 /* --- Free up the parent-side resources --- */
676 xe
->next
->prev
= xe
->prev
;
678 xe
->prev
->next
= xe
->next
;
680 xept_list
= xe
->next
;
684 xe
->f
->ops
->close(xe
->f
);
685 x_tidy(xe
->xa
, xe
->xo
);
690 /* --- @xept_chld@ --- *
692 * Arguments: @int n@ = signal number
693 * @void *p@ = uninteresting pointer
697 * Use: Deals with child death situations.
700 static void xept_chld(int n
, void *p
)
705 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
706 xept
*xe
= xept_list
;
710 if (kid
== xxe
->kid
) {
712 xxe
->e
.f
|= XEF_EXIT
;
713 if (xxe
->e
.f
& XEF_CLOSE
)
721 /* --- @xept_error@ --- *
723 * Arguments: @char *p@ = pointer to string read from stderr
724 * @size_t len@ = length of the string
725 * @void *v@ = pointer to by endpoint
729 * Use: Handles error reports from a child process.
732 static void xept_error(char *p
, size_t len
, void *v
)
736 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
738 close(xe
->err
.reader
.fd
);
739 selbuf_destroy(&xe
->err
);
740 xe
->e
.f
|= XEF_CLOSE
;
741 if (xe
->e
.f
& XEF_EXIT
)
746 /* --- Endpoint operations --- */
748 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
750 /*----- General operations on sources and targets -------------------------*/
752 /* --- @exec_init@ --- *
758 * Use: Initializes the executable problem source and target.
763 #ifdef HAVE_SETRLIMIT
764 rlimit_get(&exec_opts
.xl
);
766 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
768 env_import(&env
, environ
);
771 /* --- @exec_option@ --- */
773 static int exec_option(xdata
*x
, scanner
*sc
)
775 xopts
*xo
= x ? x
->xo
: &exec_opts
;
777 CONF_BEGIN(sc
, "exec", "executable");
779 /* --- Logging settings --- */
781 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
782 strcmp(sc
->d
.buf
, "log") == 0) {
786 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
793 /* --- Current directory setting --- *
795 * Lots of possibilities to guard against possible brainoes.
798 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
799 strcmp(sc
->d
.buf
, "cd") == 0 ||
800 strcmp(sc
->d
.buf
, "chdir") == 0 ||
801 strcmp(sc
->d
.buf
, "cwd") == 0) {
806 conf_name(sc
, '/', &d
);
807 xo
->dir
= xstrdup(d
.buf
);
812 /* --- Set a chroot prison --- */
814 if (strcmp(sc
->d
.buf
, "root") == 0 ||
815 strcmp(sc
->d
.buf
, "chroot") == 0) {
820 conf_name(sc
, '/', &d
);
821 xo
->root
= xstrdup(d
.buf
);
826 /* --- Set the target user id --- */
828 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
829 strcmp(sc
->d
.buf
, "user") == 0) {
833 if (sc
->t
!= CTOK_WORD
)
834 error(sc
, "parse error, expected user name or uid");
835 if (isdigit((unsigned char)*sc
->d
.buf
))
836 xo
->uid
= atoi(sc
->d
.buf
);
838 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
840 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
841 xo
->uid
= pw
->pw_uid
;
847 /* --- Set the target group id --- */
849 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
850 strcmp(sc
->d
.buf
, "group") == 0) {
854 if (sc
->t
!= CTOK_WORD
)
855 error(sc
, "parse error, expected group name or gid");
856 if (isdigit((unsigned char)*sc
->d
.buf
))
857 xo
->gid
= atoi(sc
->d
.buf
);
859 struct group
*gr
= getgrnam(sc
->d
.buf
);
861 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
862 xo
->gid
= gr
->gr_gid
;
868 /* --- Now try resource limit settings --- */
870 #ifdef HAVE_SETRLIMIT
871 if (rlimit_option(&xo
->xl
, sc
))
875 /* --- And then environment settings --- */
877 if (xenv_option(xo
, sc
))
880 /* --- Nothing found --- */
885 /* --- @exec_desc@ --- */
887 static void exec_desc(xdata
*x
, dstr
*d
)
891 dstr_puts(d
, "exec ");
892 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
893 dstr_puts(d
, x
->xa
->file
);
896 for (p
= x
->xa
->argv
; *p
; p
++) {
905 /* --- @exec_read@ --- */
907 static void exec_read(xdata
*x
, scanner
*sc
)
913 /* --- Read the first word --- *
915 * This is either a shell command or the actual program to run.
918 if (sc
->t
== CTOK_WORD
) {
919 dstr_putd(&d
, &sc
->d
); d
.len
++;
924 /* --- See if there's a list of arguments --- *
926 * If not, then the thing I saw was a shell command, so build the proper
927 * arguments for that.
933 error(sc
, "parse error, expected shell command or argument list");
934 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
935 p
= (char *)(xa
->argv
+ 4);
938 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
939 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
940 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
944 /* --- Snarf in a list of arguments --- */
951 /* --- Strip off the leading `[' --- *
953 * Allow various handy filename characters to be entered without quoting.
956 conf_undelim(sc
, "=:/.", "=:/.");
959 /* --- Read a sequence of arguments --- */
961 while (sc
->t
== CTOK_WORD
) {
962 dstr_putd(&d
, &sc
->d
); d
.len
++;
966 conf_undelim(sc
, 0, 0);
968 /* --- Expect the closing `]' --- */
971 error(sc
, "parse error, missing `]'");
974 /* --- If there are no arguments, whinge --- */
977 error(sc
, "must specify at least one argument");
979 /* --- Allocate a lump of memory for the array --- */
981 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
984 p
= (char *)(v
+ argc
+ 1);
985 memcpy(p
, d
.buf
, d
.len
);
990 /* --- Start dumping addresses into the @argv@ array --- */
994 while (*p
++ && p
< q
)
1002 /* --- Do some other setting up --- */
1006 x
->xo
= CREATE(xopts
);
1012 /* --- @exec_endpt@ --- */
1014 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
1016 xept
*xe
= CREATE(xept
);
1017 xe
->e
.ops
= &xept_ops
;
1021 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
1022 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
1025 xe
->desc
= xstrdup(desc
);
1029 /* --- @exec_destroy@ --- */
1031 static void exec_destroy(xdata
*x
)
1033 x_tidy(x
->xa
, x
->xo
);
1036 /*----- Source definition -------------------------------------------------*/
1038 /* --- @option@ --- */
1040 static int xsource_option(source
*s
, scanner
*sc
)
1042 xsource
*xs
= (xsource
*)s
;
1043 return (exec_option(xs ?
&xs
->x
: 0, sc
));
1046 /* --- @read@ --- */
1048 static source
*xsource_read(scanner
*sc
)
1052 if (!conf_prefix(sc
, "exec"))
1054 xs
= CREATE(xsource
);
1055 xs
->s
.ops
= &xsource_ops
;
1057 exec_read(&xs
->x
, sc
);
1061 /* --- @attach@ --- */
1063 static void xsource_destroy(source */
*s*/
);
1065 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1067 xsource
*xs
= (xsource
*)s
;
1070 /* --- Set up the source description string --- */
1074 exec_desc(&xs
->x
, &d
);
1075 dstr_puts(&d
, " -> ");
1076 dstr_puts(&d
, t
->desc
);
1077 xs
->s
.desc
= xstrdup(d
.buf
);
1081 /* --- Create the endpoints --- */
1083 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1085 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1091 /* --- Dispose of source and target --- */
1095 xsource_destroy(&xs
->s
);
1098 /* --- @destroy@ --- */
1100 static void xsource_destroy(source
*s
)
1102 xsource
*xs
= (xsource
*)s
;
1104 exec_destroy(&xs
->x
);
1108 /* --- Executable source operations --- */
1110 source_ops xsource_ops
= {
1112 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1115 /*----- Exec target description -------------------------------------------*/
1117 /* --- @option@ --- */
1119 static int xtarget_option(target
*t
, scanner
*sc
)
1121 xtarget
*xt
= (xtarget
*)t
;
1122 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1125 /* --- @read@ --- */
1127 static target
*xtarget_read(scanner
*sc
)
1132 if (!conf_prefix(sc
, "exec"))
1134 xt
= CREATE(xtarget
);
1135 xt
->t
.ops
= &xtarget_ops
;
1136 exec_read(&xt
->x
, sc
);
1137 exec_desc(&xt
->x
, &d
);
1138 xt
->t
.desc
= xstrdup(d
.buf
);
1143 /* --- @create@ --- */
1145 static endpt
*xtarget_create(target
*t
, const char *desc
)
1147 xtarget
*xt
= (xtarget
*)t
;
1148 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1152 /* --- @destroy@ --- */
1154 static void xtarget_destroy(target
*t
)
1156 xtarget
*xt
= (xtarget
*)t
;
1158 exec_destroy(&xt
->x
);
1162 /* --- Exec target operations --- */
1164 target_ops xtarget_ops
= {
1166 xtarget_option
, xtarget_read
, 0, xtarget_create
, xtarget_destroy
1169 /*----- That's all, folks -------------------------------------------------*/