28c9cd8df43d83d8c2a06070701b15b2a1df0c95
3 * $Id: exec.c,v 1.5 2002/01/13 14:49:03 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.5 2002/01/13 14:49:03 mdw
33 * Track @lbuf@ changes in mLib.
35 * Revision 1.4 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
38 * Revision 1.3 2000/07/01 11:28:52 mdw
39 * Use new mLib selbuf features.
41 * Revision 1.2 1999/10/22 22:46:17 mdw
42 * When a non-file endpoint is attached to a file, keep the file endpoint
43 * open until the nonfile is done. This stops socket sources from
44 * resetting their connection limits too early.
46 * Revision 1.1 1999/07/26 23:33:32 mdw
47 * New sources and targets.
51 /*----- Header files ------------------------------------------------------*/
64 #include <sys/types.h>
71 # include <sys/resource.h>
75 extern char **environ
;
83 #include <mLib/alloc.h>
84 #include <mLib/dstr.h>
86 #include <mLib/fdflags.h>
87 #include <mLib/report.h>
89 #include <mLib/selbuf.h>
104 /*----- Data structures ---------------------------------------------------*/
106 /* --- Resource usage --- */
108 #ifdef HAVE_SETRLIMIT
110 typedef struct xlimit
{
111 #define R(r, n) struct rlimit n;
117 /* --- Environment variable modifications --- */
119 typedef struct xenv
{
129 /* --- Program options --- */
131 typedef struct xopts
{
140 #ifdef HAVE_SETRLIMIT
147 /* --- Executable program arguments --- */
149 typedef struct xargs
{
155 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
157 /* --- Executable endpoints --- */
159 typedef struct xept
{
161 struct xept
*next
, *prev
;
171 #define XEF_CLOSE 16u
174 /* --- Executable program data --- */
176 typedef struct xdata
{
181 /* --- Executable source block --- */
183 typedef struct xsource
{
188 /* --- Executable target block --- */
190 typedef struct xtarget
{
195 /*----- Static variables --------------------------------------------------*/
197 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
198 static xept
*xept_list
;
200 static sym_table env
;
202 /*----- Fiddling about with resource limits -------------------------------*/
204 #ifdef HAVE_SETRLIMIT
206 /* --- Table mapping user-level names to OS interface bits --- */
208 typedef struct rlimit_ent
{
215 static rlimit_ent rlimits
[] = {
216 #define R(r, n) { #n, #r, r, offsetof(xlimit, n) },
221 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
223 /* --- @rlimit_get@ --- *
225 * Arguments: @xlimit *xl@ = pointer to limit structure
229 * Use: Initializes a limit structure from the current limits.
232 static void rlimit_get(xlimit
*xl
)
236 for (r
= rlimits
; r
->name
; r
++) {
237 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
238 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
244 /* --- @rlimit_set@ --- *
246 * Arguments: @xlimit *xl@ = pointer to limit structure
250 * Use: Sets resource limits from the supplied limits structure.
253 static void rlimit_set(xlimit
*xl
)
257 for (r
= rlimits
; r
->name
; r
++) {
258 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
259 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
265 /* --- @rlimit_option@ --- */
267 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
269 CONF_BEGIN(sc
, "rlimit", "resource limit")
270 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
275 /* --- Find out which resource is being fiddled --- */
281 for (r
= rlimits
; r
->name
; r
++) {
282 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
283 if (r
->name
[sc
->d
.len
] == 0) {
287 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
295 rl
= RLIMIT(xl
, chosen
->off
);
298 /* --- Look for hard or soft restrictions --- */
304 if (sc
->t
== CTOK_WORD
) {
305 if ((i
= conf_enum(sc
, "soft,hard",
306 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
311 /* --- Now read the new value --- */
315 if (sc
->t
!= CTOK_WORD
)
316 error(sc
, "parse error, expected limit value");
318 if (conf_enum(sc
, "unlimited,infinity",
319 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
324 v
= strtol(sc
->d
.buf
, &p
, 0);
326 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
327 switch (tolower((unsigned char)*p
)) {
329 case 'b': v
*= 512; break;
332 case 'k': v
*= 1024; break;
333 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
338 /* --- Store the limit value away --- */
346 if (v
> rl
->rlim_max
)
347 error(sc
, "soft limit %l exceeds hard limit %l for %s",
348 v
, rl
->rlim_max
, chosen
->rname
);
353 if (rl
->rlim_cur
> v
)
364 /*----- Environment fiddling ----------------------------------------------*/
366 /* --- @xenv_option@ --- *
368 * Arguments: @xopts *xo@ = pointer to options block
369 * @scanner *sc@ = pointer to scanner
371 * Returns: Nonzero if claimed
373 * Use: Parses environment variable assignments.
376 static int xenv_option(xopts
*xo
, scanner
*sc
)
378 CONF_BEGIN(sc
, "env", "environment")
381 /* --- Unset a variable --- */
383 if (strcmp(sc
->d
.buf
, "unset") == 0) {
385 if (sc
->t
!= CTOK_WORD
)
386 error(sc
, "parse error, expected environment variable name");
388 xe
->name
= xstrdup(sc
->d
.buf
);
395 /* --- Clear the entire environment --- */
397 if (strcmp(sc
->d
.buf
, "clear") == 0) {
404 /* --- Allow `set' to be omitted if there's a prefix --- */
406 if (strcmp(sc
->d
.buf
, "set") == 0)
411 /* --- Set a variable --- */
413 if (sc
->t
!= CTOK_WORD
)
414 error(sc
, "parse error, expected environment variable name");
416 xe
->name
= xstrdup(sc
->d
.buf
);
420 if (sc
->t
!= CTOK_WORD
)
421 error(sc
, "parse error, expected environment variable value");
422 xe
->value
= xstrdup(sc
->d
.buf
);
430 xo
->etail
= &xe
->next
;
433 /* --- Nothing else to try --- */
438 /* --- @xenv_apply@ --- *
440 * Arguments: @xenv *xe@ = pointer to a variable change list
444 * Use: Modifies the environment (in @env@) according to the list.
447 static void xenv_apply(xenv
*xe
)
452 env_put(&env
, xe
->name
, xe
->value
);
463 /* --- @xenv_destroy@ --- *
465 * Arguments: @xenv *xe@ = pointer to a variable change list
469 * Use: Frees the memory used by an environment variable change list.
472 static void xenv_destroy(xenv
*xe
)
484 /*----- Miscellaneous good things -----------------------------------------*/
486 /* --- @x_tidy@ --- *
488 * Arguments: @xargs *xa@ = pointer to an arguments block
489 * @xopts *xo@ = pointer to an options block
493 * Use: Releases a reference to argument and options blocks.
496 static void x_tidy(xargs
*xa
, xopts
*xo
)
504 xenv_destroy(xo
->env
);
509 /*----- Executable endpoints ----------------------------------------------*/
511 /* --- @attach@ --- */
513 static void xept_error(char */
*p*/
, size_t /*len*/, void */
*v*/
);
515 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
517 xept
*xe
= (xept
*)e
;
521 /* --- Make a pipe for standard error --- */
524 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
527 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
529 /* --- Fork a child, and handle an error if there was one --- */
531 if ((kid
= fork()) == -1) {
532 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
538 /* --- Do the child thing --- */
543 /* --- Fiddle with the file descriptors --- *
545 * Attach the other endpoint's descriptors to standard input and output.
546 * Attach my pipe to standard error. Mark everything as blocking and
547 * not-to-be-closed-on-exec at this end.
551 if (dup2(in
->fd
, STDIN_FILENO
) < 0 ||
552 dup2(out
->fd
, STDOUT_FILENO
) < 0 ||
553 dup2(fd
[1], STDERR_FILENO
) < 0) {
554 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
563 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
564 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
565 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
567 /* --- First of all set the @chroot@ prison --- */
569 if (xo
->root
&& chroot(xo
->root
)) {
570 moan("couldn't set `%s' as filesystem root: %s",
571 xo
->root
, strerror(errno
));
575 /* --- Now set the current directory --- */
577 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
578 moan("couldn't set `%s' as current directory: %s",
579 xo
->dir ? xo
->dir
: "/", strerror(errno
));
583 /* --- Set the resource limits --- */
585 #ifdef HAVE_SETRLIMIT
589 /* --- Set group id --- */
592 if (setgid(xo
->gid
)) {
593 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
596 #ifdef HAVE_SETGROUPS
597 if (setgroups(1, &xo
->gid
))
598 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
603 /* --- Set uid --- */
606 if (setuid(xo
->uid
)) {
607 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
612 /* --- Play with signal dispositions --- */
614 signal(SIGPIPE
, SIG_DFL
);
616 /* --- Fiddle with the environment --- */
618 xenv_apply(exec_opts
.env
);
619 xenv_apply(xe
->xo
->env
);
620 environ
= env_export(&env
);
622 /* --- Run the program --- */
624 execvp(xe
->xa
->file
, xe
->xa
->argv
);
625 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
629 /* --- The child's done; see to the parent --- */
632 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
634 xe
->next
= xept_list
;
637 xept_list
->prev
= xe
;
639 if (!(xe
->xo
->f
& XF_NOLOG
))
640 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
645 /* --- @xept_file@ --- */
647 static void xept_file(endpt
*e
, endpt
*f
)
649 xept
*xe
= (xept
*)e
;
653 /* --- @xept_close@ --- */
655 static void xept_close(endpt
*e
)
657 xept
*xe
= (xept
*)e
;
660 xe
->f
->ops
->close(xe
->f
);
661 x_tidy(xe
->xa
, xe
->xo
);
667 /* --- @xept_destroy@ --- */
669 static void xept_destroy(xept
*xe
)
671 /* --- First emit the news about the process --- */
673 if (xe
->xo
->f
& XF_NOLOG
)
675 else if (WIFEXITED(xe
->st
)) {
676 if (WEXITSTATUS(xe
->st
) == 0)
677 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
679 fw_log(-1, "[%s] pid %i failed: status %i",
680 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
682 } else if (WIFSIGNALED(xe
->st
)) {
684 #ifdef HAVE_STRSIGNAL
685 s
= strsignal(WTERMSIG(xe
->st
));
686 #elif HAVE__SYS_SIGLIST
687 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
690 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
693 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
695 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
697 /* --- Free up the parent-side resources --- */
700 xe
->next
->prev
= xe
->prev
;
702 xe
->prev
->next
= xe
->next
;
704 xept_list
= xe
->next
;
708 xe
->f
->ops
->close(xe
->f
);
709 x_tidy(xe
->xa
, xe
->xo
);
714 /* --- @xept_chld@ --- *
716 * Arguments: @int n@ = signal number
717 * @void *p@ = uninteresting pointer
721 * Use: Deals with child death situations.
724 static void xept_chld(int n
, void *p
)
729 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
730 xept
*xe
= xept_list
;
734 if (kid
== xxe
->kid
) {
736 xxe
->e
.f
|= XEF_EXIT
;
737 if (xxe
->e
.f
& XEF_CLOSE
)
745 /* --- @xept_error@ --- *
747 * Arguments: @char *p@ = pointer to string read from stderr
748 * @size_t len@ = length of the string
749 * @void *v@ = pointer to by endpoint
753 * Use: Handles error reports from a child process.
756 static void xept_error(char *p
, size_t len
, void *v
)
760 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
762 close(xe
->err
.reader
.fd
);
763 selbuf_destroy(&xe
->err
);
764 xe
->e
.f
|= XEF_CLOSE
;
765 if (xe
->e
.f
& XEF_EXIT
)
770 /* --- Endpoint operations --- */
772 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
774 /*----- General operations on sources and targets -------------------------*/
776 /* --- @exec_init@ --- *
782 * Use: Initializes the executable problem source and target.
787 rlimit_get(&exec_opts
.xl
);
788 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
790 env_import(&env
, environ
);
793 /* --- @exec_option@ --- */
795 static int exec_option(xdata
*x
, scanner
*sc
)
797 xopts
*xo
= x ? x
->xo
: &exec_opts
;
799 CONF_BEGIN(sc
, "exec", "executable");
801 /* --- Logging settings --- */
803 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
804 strcmp(sc
->d
.buf
, "log") == 0) {
808 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
815 /* --- Current directory setting --- *
817 * Lots of possibilities to guard against possible brainoes.
820 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
821 strcmp(sc
->d
.buf
, "cd") == 0 ||
822 strcmp(sc
->d
.buf
, "chdir") == 0 ||
823 strcmp(sc
->d
.buf
, "cwd") == 0) {
828 conf_name(sc
, '/', &d
);
829 xo
->dir
= xstrdup(d
.buf
);
834 /* --- Set a chroot prison --- */
836 if (strcmp(sc
->d
.buf
, "root") == 0 ||
837 strcmp(sc
->d
.buf
, "chroot") == 0) {
842 conf_name(sc
, '/', &d
);
843 xo
->root
= xstrdup(d
.buf
);
848 /* --- Set the target user id --- */
850 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
851 strcmp(sc
->d
.buf
, "user") == 0) {
855 if (sc
->t
!= CTOK_WORD
)
856 error(sc
, "parse error, expected user name or uid");
857 if (isdigit((unsigned char)*sc
->d
.buf
))
858 xo
->uid
= atoi(sc
->d
.buf
);
860 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
862 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
863 xo
->uid
= pw
->pw_uid
;
869 /* --- Set the target group id --- */
871 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
872 strcmp(sc
->d
.buf
, "group") == 0) {
876 if (sc
->t
!= CTOK_WORD
)
877 error(sc
, "parse error, expected group name or gid");
878 if (isdigit((unsigned char)*sc
->d
.buf
))
879 xo
->gid
= atoi(sc
->d
.buf
);
881 struct group
*gr
= getgrnam(sc
->d
.buf
);
883 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
884 xo
->gid
= gr
->gr_gid
;
890 /* --- Now try resource limit settings --- */
892 if (rlimit_option(&xo
->xl
, sc
))
895 /* --- And then environment settings --- */
897 if (xenv_option(xo
, sc
))
900 /* --- Nothing found --- */
905 /* --- @exec_desc@ --- */
907 static void exec_desc(xdata
*x
, dstr
*d
)
911 dstr_puts(d
, "exec ");
912 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
913 dstr_puts(d
, x
->xa
->file
);
916 for (p
= x
->xa
->argv
; *p
; p
++) {
925 /* --- @exec_read@ --- */
927 static void exec_read(xdata
*x
, scanner
*sc
)
933 /* --- Read the first word --- *
935 * This is either a shell command or the actual program to run.
938 if (sc
->t
== CTOK_WORD
) {
939 dstr_putd(&d
, &sc
->d
); d
.len
++;
944 /* --- See if there's a list of arguments --- *
946 * If not, then the thing I saw was a shell command, so build the proper
947 * arguments for that.
953 error(sc
, "parse error, expected shell command or argument list");
954 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
955 p
= (char *)(xa
->argv
+ 4);
958 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
959 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
960 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
964 /* --- Snarf in a list of arguments --- */
971 /* --- Strip off the leading `[' --- */
975 /* --- Read a sequence of arguments --- */
977 while (sc
->t
== CTOK_WORD
) {
978 dstr_putd(&d
, &sc
->d
); d
.len
++;
983 /* --- Expect the closing `]' --- */
986 error(sc
, "parse error, missing `]'");
989 /* --- If there are no arguments, whinge --- */
992 error(sc
, "must specify at least one argument");
994 /* --- Allocate a lump of memory for the array --- */
996 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
999 p
= (char *)(v
+ argc
+ 1);
1000 memcpy(p
, d
.buf
, d
.len
);
1005 /* --- Start dumping addresses into the @argv@ array --- */
1009 while (*p
++ && p
< q
)
1017 /* --- Do some other setting up --- */
1021 x
->xo
= CREATE(xopts
);
1027 /* --- @exec_endpt@ --- */
1029 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
1031 xept
*xe
= CREATE(xept
);
1032 xe
->e
.ops
= &xept_ops
;
1036 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
1037 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
1040 xe
->desc
= xstrdup(desc
);
1044 /* --- @exec_destroy@ --- */
1046 static void exec_destroy(xdata
*x
)
1048 x_tidy(x
->xa
, x
->xo
);
1051 /*----- Source definition -------------------------------------------------*/
1053 /* --- @option@ --- */
1055 static int xsource_option(source
*s
, scanner
*sc
)
1057 xsource
*xs
= (xsource
*)s
;
1058 return (exec_option(xs ?
&xs
->x
: 0, sc
));
1061 /* --- @read@ --- */
1063 static source
*xsource_read(scanner
*sc
)
1067 if (!conf_prefix(sc
, "exec"))
1069 xs
= CREATE(xsource
);
1070 xs
->s
.ops
= &xsource_ops
;
1072 exec_read(&xs
->x
, sc
);
1076 /* --- @attach@ --- */
1078 static void xsource_destroy(source */
*s*/
);
1080 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1082 xsource
*xs
= (xsource
*)s
;
1085 /* --- Set up the source description string --- */
1089 exec_desc(&xs
->x
, &d
);
1090 dstr_puts(&d
, " -> ");
1091 dstr_puts(&d
, t
->desc
);
1092 xs
->s
.desc
= xstrdup(d
.buf
);
1096 /* --- Create the endpoints --- */
1098 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1100 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1106 /* --- Dispose of source and target --- */
1110 xsource_destroy(&xs
->s
);
1113 /* --- @destroy@ --- */
1115 static void xsource_destroy(source
*s
)
1117 xsource
*xs
= (xsource
*)s
;
1119 exec_destroy(&xs
->x
);
1123 /* --- Executable source operations --- */
1125 source_ops xsource_ops
= {
1127 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1130 /*----- Exec target description -------------------------------------------*/
1132 /* --- @option@ --- */
1134 static int xtarget_option(target
*t
, scanner
*sc
)
1136 xtarget
*xt
= (xtarget
*)t
;
1137 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1140 /* --- @read@ --- */
1142 static target
*xtarget_read(scanner
*sc
)
1147 if (!conf_prefix(sc
, "exec"))
1149 xt
= CREATE(xtarget
);
1150 xt
->t
.ops
= &xtarget_ops
;
1151 exec_read(&xt
->x
, sc
);
1152 exec_desc(&xt
->x
, &d
);
1153 xt
->t
.desc
= xstrdup(d
.buf
);
1158 /* --- @create@ --- */
1160 static endpt
*xtarget_create(target
*t
, const char *desc
)
1162 xtarget
*xt
= (xtarget
*)t
;
1163 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1167 /* --- @destroy@ --- */
1169 static void xtarget_destroy(target
*t
)
1171 xtarget
*xt
= (xtarget
*)t
;
1173 exec_destroy(&xt
->x
);
1177 /* --- Exec target operations --- */
1179 target_ops xtarget_ops
= {
1181 xtarget_option
, xtarget_read
, xtarget_create
, xtarget_destroy
1184 /*----- That's all, folks -------------------------------------------------*/