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 XLIMIT_ENTRY(name, constant) struct rlimit name;
43 /* --- Environment variable modifications --- */
55 /* --- Program options --- */
57 typedef struct xopts
{
73 /* --- Executable program arguments --- */
75 typedef struct xargs
{
81 #define XARGS_SZ(n) (sizeof(xargs) + sizeof(char *) * (n))
83 /* --- Executable endpoints --- */
87 struct xept
*next
, *prev
;
100 /* --- Executable program data --- */
102 typedef struct xdata
{
107 /* --- Executable source block --- */
109 typedef struct xsource
{
114 /* --- Executable target block --- */
116 typedef struct xtarget
{
121 /*----- Static variables --------------------------------------------------*/
123 static xopts exec_opts
= { 1, 0, 0, 0, -1, -1, 0, &exec_opts
.env
};
124 static xept
*xept_list
;
126 static sym_table env
;
128 /*----- Fiddling about with resource limits -------------------------------*/
130 #ifdef HAVE_SETRLIMIT
132 /* --- Table mapping user-level names to OS interface bits --- */
134 typedef struct rlimit_ent
{
141 static rlimit_ent rlimits
[] = {
142 #define TABLE_ENTRY(name, constant) \
143 { #name, #constant, constant, offsetof(xlimit, name) },
149 #define RLIMIT(xl, o) ((struct rlimit *)((char *)(xl) + (o)))
151 /* --- @rlimit_get@ --- *
153 * Arguments: @xlimit *xl@ = pointer to limit structure
157 * Use: Initializes a limit structure from the current limits.
160 static void rlimit_get(xlimit
*xl
)
164 for (r
= rlimits
; r
->name
; r
++) {
165 if (getrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
166 moan("couldn't read %s: %s", r
->rname
, strerror(errno
));
172 /* --- @rlimit_set@ --- *
174 * Arguments: @xlimit *xl@ = pointer to limit structure
178 * Use: Sets resource limits from the supplied limits structure.
181 static void rlimit_set(xlimit
*xl
)
185 for (r
= rlimits
; r
->name
; r
++) {
186 if (setrlimit(r
->r
, RLIMIT(xl
, r
->off
))) {
187 moan("couldn't set %s: %s", r
->rname
, strerror(errno
));
193 /* --- @rlimit_option@ --- */
195 static int rlimit_option(xlimit
*xl
, scanner
*sc
)
197 CONF_BEGIN(sc
, "rlimit", "resource limit")
198 enum { w_soft
, w_hard
, w_both
} which
= w_both
;
203 /* --- Find out which resource is being fiddled --- */
209 for (r
= rlimits
; r
->name
; r
++) {
210 if (strncmp(sc
->d
.buf
, r
->name
, sc
->d
.len
) == 0) {
211 if (r
->name
[sc
->d
.len
] == 0) {
215 error(sc
, "ambiguous resource limit name `%s'", sc
->d
.buf
);
223 rl
= RLIMIT(xl
, chosen
->off
);
226 /* --- Look for hard or soft restrictions --- */
232 if (sc
->t
== CTOK_WORD
) {
233 if ((i
= conf_enum(sc
, "soft,hard",
234 ENUM_ABBREV
| ENUM_NONE
, "limit type")) != -1)
239 /* --- Now read the new value --- */
243 if (sc
->t
!= CTOK_WORD
)
244 error(sc
, "parse error, expected limit value");
246 if (conf_enum(sc
, "unlimited,infinity",
247 ENUM_ABBREV
| ENUM_NONE
, "limit value") > -1)
252 v
= strtol(sc
->d
.buf
, &p
, 0);
254 error(sc
, "parse error, invalid limit value `%s'", sc
->d
.buf
);
255 switch (tolower((unsigned char)*p
)) {
257 case 'b': v
*= 512; break;
260 case 'k': v
*= 1024; break;
261 default: error(sc
, "parse error, invalid limit scale `%c'", *p
);
266 /* --- Store the limit value away --- */
274 if (v
> rl
->rlim_max
)
275 error(sc
, "soft limit %lu exceeds hard limit %lu for %s",
276 (unsigned long)v
, (unsigned long)rl
->rlim_max
,
282 if (rl
->rlim_cur
> v
)
293 /*----- Environment fiddling ----------------------------------------------*/
295 /* --- @xenv_option@ --- *
297 * Arguments: @xopts *xo@ = pointer to options block
298 * @scanner *sc@ = pointer to scanner
300 * Returns: Nonzero if claimed
302 * Use: Parses environment variable assignments.
305 static int xenv_option(xopts
*xo
, scanner
*sc
)
307 CONF_BEGIN(sc
, "env", "environment")
310 /* --- Unset a variable --- */
312 if (strcmp(sc
->d
.buf
, "unset") == 0) {
314 if (sc
->t
!= CTOK_WORD
)
315 error(sc
, "parse error, expected environment variable name");
317 xe
->name
= xstrdup(sc
->d
.buf
);
324 /* --- Clear the entire environment --- */
326 if (strcmp(sc
->d
.buf
, "clear") == 0) {
333 /* --- Allow `set' to be omitted if there's a prefix --- */
335 if (strcmp(sc
->d
.buf
, "set") == 0)
340 /* --- Set a variable --- */
342 if (sc
->t
!= CTOK_WORD
)
343 error(sc
, "parse error, expected environment variable name");
345 xe
->name
= xstrdup(sc
->d
.buf
);
349 if (sc
->t
!= CTOK_WORD
)
350 error(sc
, "parse error, expected environment variable value");
351 xe
->value
= xstrdup(sc
->d
.buf
);
359 xo
->etail
= &xe
->next
;
362 /* --- Nothing else to try --- */
367 /* --- @xenv_apply@ --- *
369 * Arguments: @xenv *xe@ = pointer to a variable change list
373 * Use: Modifies the environment (in @env@) according to the list.
376 static void xenv_apply(xenv
*xe
)
381 env_put(&env
, xe
->name
, xe
->value
);
392 /* --- @xenv_destroy@ --- *
394 * Arguments: @xenv *xe@ = pointer to a variable change list
398 * Use: Frees the memory used by an environment variable change list.
401 static void xenv_destroy(xenv
*xe
)
413 /*----- Miscellaneous good things -----------------------------------------*/
415 /* --- @x_tidy@ --- *
417 * Arguments: @xargs *xa@ = pointer to an arguments block
418 * @xopts *xo@ = pointer to an options block
422 * Use: Releases a reference to argument and options blocks.
425 static void x_tidy(xargs
*xa
, xopts
*xo
)
433 xenv_destroy(xo
->env
);
438 /*----- Executable endpoints ----------------------------------------------*/
440 /* --- @attach@ --- */
442 static void xept_error(char */
*p*/
, size_t /*len*/, void */
*v*/
);
444 static void xept_attach(endpt
*e
, reffd
*in
, reffd
*out
)
446 xept
*xe
= (xept
*)e
;
450 /* --- Make a pipe for standard error --- */
453 fw_log(-1, "[%s] couldn't create pipe: %s", xe
->desc
, strerror(errno
));
456 fdflags(fd
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
458 /* --- Fork a child, and handle an error if there was one --- */
460 if ((kid
= fork()) == -1) {
461 fw_log(-1, "[%s] couldn't fork: %s", xe
->desc
, strerror(errno
));
467 /* --- Do the child thing --- */
473 /* --- Fiddle with the file descriptors --- *
475 * Attach the other endpoint's descriptors to standard input and output.
476 * Attach my pipe to standard error. Mark everything as blocking and
477 * not-to-be-closed-on-exec at this end.
481 md
[0].cur
= in
->fd
; md
[0].want
= STDIN_FILENO
;
482 md
[1].cur
= out
->fd
; md
[1].want
= STDOUT_FILENO
;
483 md
[2].cur
= fd
[1]; md
[2].want
= STDERR_FILENO
;
485 moan("couldn't manipulate file descriptors: %s", strerror(errno
));
489 fdflags(STDIN_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
490 fdflags(STDOUT_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
491 fdflags(STDERR_FILENO
, O_NONBLOCK
, 0, FD_CLOEXEC
, 0);
493 /* --- First of all set the @chroot@ prison --- */
495 if (xo
->root
&& chroot(xo
->root
)) {
496 moan("couldn't set `%s' as filesystem root: %s",
497 xo
->root
, strerror(errno
));
501 /* --- Now set the current directory --- */
503 if (xo
->dir ?
chdir(xo
->dir
) : xo
->root ?
chdir("/") : 0) {
504 moan("couldn't set `%s' as current directory: %s",
505 xo
->dir ? xo
->dir
: "/", strerror(errno
));
509 /* --- Set the resource limits --- */
511 #ifdef HAVE_SETRLIMIT
515 /* --- Set group id --- */
517 if (xo
->gid
!= (gid_t
)-1) {
518 if (setgid(xo
->gid
)) {
519 moan("couldn't set gid %i: %s", xo
->gid
, strerror(errno
));
522 #ifdef HAVE_SETGROUPS
523 if (setgroups(1, &xo
->gid
))
524 moan("warning: couldn't set group list to %i: %s", xo
->gid
,
529 /* --- Set uid --- */
531 if (xo
->uid
!= (uid_t
)-1) {
532 if (setuid(xo
->uid
)) {
533 moan("couldn't set uid %i: %s", xo
->uid
, strerror(errno
));
538 /* --- Play with signal dispositions --- */
540 signal(SIGPIPE
, SIG_DFL
);
542 /* --- Fiddle with the environment --- */
544 xenv_apply(exec_opts
.env
);
545 xenv_apply(xe
->xo
->env
);
546 environ
= env_export(&env
);
548 /* --- Run the program --- */
550 execvp(xe
->xa
->file
, xe
->xa
->argv
);
551 moan("couldn't execute `%s': %s", xe
->xa
->file
, strerror(errno
));
555 /* --- The child's done; see to the parent --- */
558 selbuf_init(&xe
->err
, sel
, fd
[0], xept_error
, xe
);
560 xe
->next
= xept_list
;
563 xept_list
->prev
= xe
;
565 if (!(xe
->xo
->f
& XF_NOLOG
))
566 fw_log(-1, "[%s] started with pid %i", xe
->desc
, kid
);
571 /* --- @xept_file@ --- */
573 static void xept_file(endpt
*e
, endpt
*f
)
575 xept
*xe
= (xept
*)e
;
579 /* --- @xept_close@ --- */
581 static void xept_close(endpt
*e
)
583 xept
*xe
= (xept
*)e
;
586 xe
->f
->ops
->close(xe
->f
);
587 x_tidy(xe
->xa
, xe
->xo
);
593 /* --- @xept_destroy@ --- */
595 static void xept_destroy(xept
*xe
)
597 /* --- First emit the news about the process --- */
599 if (xe
->xo
->f
& XF_NOLOG
)
601 else if (WIFEXITED(xe
->st
)) {
602 if (WEXITSTATUS(xe
->st
) == 0)
603 fw_log(-1, "[%s] pid %i exited successfully", xe
->desc
, xe
->kid
);
605 fw_log(-1, "[%s] pid %i failed: status %i",
606 xe
->desc
, xe
->kid
, WEXITSTATUS(xe
->st
));
608 } else if (WIFSIGNALED(xe
->st
)) {
610 #ifdef HAVE_STRSIGNAL
611 s
= strsignal(WTERMSIG(xe
->st
));
612 #elif HAVE__SYS_SIGLIST
613 s
= _sys_siglist
[WTERMSIG(xe
->st
)];
616 sprintf(buf
, "signal %i", WTERMSIG(xe
->st
));
619 fw_log(-1, "[%s] pid %i failed: %s", xe
->desc
, xe
->kid
, s
);
621 fw_log(-1, "[%s] pid %i failed: unrecognized status", xe
->desc
, xe
->kid
);
623 /* --- Free up the parent-side resources --- */
626 xe
->next
->prev
= xe
->prev
;
628 xe
->prev
->next
= xe
->next
;
630 xept_list
= xe
->next
;
634 xe
->f
->ops
->close(xe
->f
);
635 x_tidy(xe
->xa
, xe
->xo
);
640 /* --- @xept_chld@ --- *
642 * Arguments: @int n@ = signal number
643 * @void *p@ = uninteresting pointer
647 * Use: Deals with child death situations.
650 static void xept_chld(int n
, void *p
)
655 while ((kid
= waitpid(-1, &st
, WNOHANG
)) > 0) {
656 xept
*xe
= xept_list
;
660 if (kid
== xxe
->kid
) {
662 xxe
->e
.f
|= XEF_EXIT
;
663 if (xxe
->e
.f
& XEF_CLOSE
)
671 /* --- @xept_error@ --- *
673 * Arguments: @char *p@ = pointer to string read from stderr
674 * @size_t len@ = length of the string
675 * @void *v@ = pointer to by endpoint
679 * Use: Handles error reports from a child process.
682 static void xept_error(char *p
, size_t len
, void *v
)
686 fw_log(-1, "[%s] pid %i: %s", xe
->desc
, xe
->kid
, p
);
688 close(xe
->err
.reader
.fd
);
689 selbuf_destroy(&xe
->err
);
690 xe
->e
.f
|= XEF_CLOSE
;
691 if (xe
->e
.f
& XEF_EXIT
)
696 /* --- Endpoint operations --- */
698 static endpt_ops xept_ops
= { xept_attach
, xept_file
, 0, xept_close
};
700 /*----- General operations on sources and targets -------------------------*/
702 /* --- @exec_init@ --- *
708 * Use: Initializes the executable problem source and target.
713 #ifdef HAVE_SETRLIMIT
714 rlimit_get(&exec_opts
.xl
);
716 sig_add(&xept_sig
, SIGCHLD
, xept_chld
, 0);
718 env_import(&env
, environ
);
721 /* --- @exec_option@ --- */
723 static int exec_option(xdata
*x
, scanner
*sc
)
725 xopts
*xo
= x ? x
->xo
: &exec_opts
;
727 CONF_BEGIN(sc
, "exec", "executable");
729 /* --- Logging settings --- */
731 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
732 strcmp(sc
->d
.buf
, "log") == 0) {
736 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
743 /* --- Current directory setting --- *
745 * Lots of possibilities to guard against possible brainoes.
748 if (strcmp(sc
->d
.buf
, "dir") == 0 ||
749 strcmp(sc
->d
.buf
, "cd") == 0 ||
750 strcmp(sc
->d
.buf
, "chdir") == 0 ||
751 strcmp(sc
->d
.buf
, "cwd") == 0) {
757 xo
->dir
= xstrdup(d
.buf
);
762 /* --- Set a chroot prison --- */
764 if (strcmp(sc
->d
.buf
, "root") == 0 ||
765 strcmp(sc
->d
.buf
, "chroot") == 0) {
771 xo
->root
= xstrdup(d
.buf
);
776 /* --- Set the target user id --- */
778 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
779 strcmp(sc
->d
.buf
, "user") == 0) {
783 if (sc
->t
!= CTOK_WORD
)
784 error(sc
, "parse error, expected user name or uid");
785 if (isdigit((unsigned char)*sc
->d
.buf
))
786 xo
->uid
= atoi(sc
->d
.buf
);
788 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
790 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
791 xo
->uid
= pw
->pw_uid
;
797 /* --- Set the target group id --- */
799 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
800 strcmp(sc
->d
.buf
, "group") == 0) {
804 if (sc
->t
!= CTOK_WORD
)
805 error(sc
, "parse error, expected group name or gid");
806 if (isdigit((unsigned char)*sc
->d
.buf
))
807 xo
->gid
= atoi(sc
->d
.buf
);
809 struct group
*gr
= getgrnam(sc
->d
.buf
);
811 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
812 xo
->gid
= gr
->gr_gid
;
818 /* --- Now try resource limit settings --- */
820 #ifdef HAVE_SETRLIMIT
821 if (rlimit_option(&xo
->xl
, sc
))
825 /* --- And then environment settings --- */
827 if (xenv_option(xo
, sc
))
830 /* --- Nothing found --- */
835 /* --- @exec_desc@ --- */
837 static void exec_desc(xdata
*x
, dstr
*d
)
841 dstr_puts(d
, "exec ");
842 if (strcmp(x
->xa
->file
, x
->xa
->argv
[0]) != 0) {
843 dstr_puts(d
, x
->xa
->file
);
846 for (p
= x
->xa
->argv
; *p
; p
++) {
855 /* --- @exec_read@ --- */
857 static void exec_read(xdata
*x
, scanner
*sc
)
863 /* --- Read the first word --- *
865 * This is either a shell command or the actual program to run.
868 if (sc
->t
== CTOK_WORD
) {
869 dstr_putd(&d
, &sc
->d
); d
.len
++;
874 /* --- See if there's a list of arguments --- *
876 * If not, then the thing I saw was a shell command, so build the proper
877 * arguments for that.
883 error(sc
, "parse error, expected shell command or argument list");
884 xa
= xmalloc(XARGS_SZ(3) + 8 + 3 + d
.len
);
885 p
= (char *)(xa
->argv
+ 4);
888 xa
->argv
[0] = p
; memcpy(p
, "/bin/sh", 8); p
+= 8;
889 xa
->argv
[1] = p
; memcpy(p
, "-c", 3); p
+= 3;
890 xa
->argv
[2] = p
; memcpy(p
, d
.buf
, d
.len
); p
+= d
.len
;
894 /* --- Snarf in a list of arguments --- */
901 /* --- Strip off the leading `[' --- *
903 * Allow various handy filename characters to be entered without quoting.
906 conf_undelim(sc
, "=:/.", "=:/.");
909 /* --- Read a sequence of arguments --- */
911 while (sc
->t
== CTOK_WORD
) {
912 dstr_putd(&d
, &sc
->d
); d
.len
++;
916 conf_undelim(sc
, 0, 0);
918 /* --- Expect the closing `]' --- */
921 error(sc
, "parse error, missing `]'");
924 /* --- If there are no arguments, whinge --- */
927 error(sc
, "must specify at least one argument");
929 /* --- Allocate a lump of memory for the array --- */
931 xa
= xmalloc(XARGS_SZ(argc
) + d
.len
);
934 p
= (char *)(v
+ argc
+ 1);
935 memcpy(p
, d
.buf
, d
.len
);
940 /* --- Start dumping addresses into the @argv@ array --- */
944 while (*p
++ && p
< q
)
952 /* --- Do some other setting up --- */
956 x
->xo
= CREATE(xopts
);
962 /* --- @exec_endpt@ --- */
964 static endpt
*exec_endpt(xdata
*x
, const char *desc
)
966 xept
*xe
= CREATE(xept
);
967 xe
->e
.ops
= &xept_ops
;
971 xe
->xa
= x
->xa
; xe
->xa
->ref
++;
972 xe
->xo
= x
->xo
; xe
->xo
->ref
++;
975 xe
->desc
= xstrdup(desc
);
979 /* --- @exec_destroy@ --- */
981 static void exec_destroy(xdata
*x
)
983 x_tidy(x
->xa
, x
->xo
);
986 /*----- Source definition -------------------------------------------------*/
988 /* --- @option@ --- */
990 static int xsource_option(source
*s
, scanner
*sc
)
992 xsource
*xs
= (xsource
*)s
;
993 return (exec_option(xs ?
&xs
->x
: 0, sc
));
998 static source
*xsource_read(scanner
*sc
)
1002 if (!conf_prefix(sc
, "exec"))
1004 xs
= CREATE(xsource
);
1005 xs
->s
.ops
= &xsource_ops
;
1007 exec_read(&xs
->x
, sc
);
1011 /* --- @attach@ --- */
1013 static void xsource_destroy(source */
*s*/
);
1015 static void xsource_attach(source
*s
, scanner
*sc
, target
*t
)
1017 xsource
*xs
= (xsource
*)s
;
1020 /* --- Set up the source description string --- */
1024 exec_desc(&xs
->x
, &d
);
1025 dstr_puts(&d
, " -> ");
1026 dstr_puts(&d
, t
->desc
);
1027 xs
->s
.desc
= xstrdup(d
.buf
);
1031 /* --- Create the endpoints --- */
1033 if ((ee
= t
->ops
->create(t
, xs
->s
.desc
)) == 0)
1035 if ((e
= exec_endpt(&xs
->x
, xs
->s
.desc
)) == 0) {
1039 endpt_join(e
, ee
, xs
->s
.desc
);
1041 /* --- Dispose of source and target --- */
1045 xsource_destroy(&xs
->s
);
1048 /* --- @destroy@ --- */
1050 static void xsource_destroy(source
*s
)
1052 xsource
*xs
= (xsource
*)s
;
1054 exec_destroy(&xs
->x
);
1058 /* --- Executable source operations --- */
1060 source_ops xsource_ops
= {
1062 xsource_option
, xsource_read
, xsource_attach
, xsource_destroy
1065 /*----- Exec target description -------------------------------------------*/
1067 /* --- @option@ --- */
1069 static int xtarget_option(target
*t
, scanner
*sc
)
1071 xtarget
*xt
= (xtarget
*)t
;
1072 return (exec_option(xt ?
&xt
->x
: 0, sc
));
1075 /* --- @read@ --- */
1077 static target
*xtarget_read(scanner
*sc
)
1082 if (!conf_prefix(sc
, "exec"))
1084 xt
= CREATE(xtarget
);
1085 xt
->t
.ops
= &xtarget_ops
;
1086 exec_read(&xt
->x
, sc
);
1087 exec_desc(&xt
->x
, &d
);
1088 xt
->t
.desc
= xstrdup(d
.buf
);
1093 /* --- @create@ --- */
1095 static endpt
*xtarget_create(target
*t
, const char *desc
)
1097 xtarget
*xt
= (xtarget
*)t
;
1098 endpt
*e
= exec_endpt(&xt
->x
, desc
);
1102 /* --- @destroy@ --- */
1104 static void xtarget_destroy(target
*t
)
1106 xtarget
*xt
= (xtarget
*)t
;
1108 exec_destroy(&xt
->x
);
1112 /* --- Exec target operations --- */
1114 target_ops xtarget_ops
= {
1116 xtarget_option
, xtarget_read
, 0, xtarget_create
, xtarget_destroy
1119 /*----- That's all, folks -------------------------------------------------*/