From: "d12fk (Code Review)" Date: Wed, 5 Mar 2025 03:44:49 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: <60563730d8e159c6d9afd1df219f4d59957ebd77-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="yv03ecHYOwY="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down script with privsep List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld , MaxF Cc: openvpn-devel --yv03ecHYOwY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: MaxF, flichtenheld, plaisthos. Hello MaxF, flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=3Demail to look at the new patch set (#9). The following approvals got outdated and were removed: Code-Review+2 by flichtenheld, Code-Review-1 by MaxF The change is no longer submittable: Code-Review and checks~ChecksSubmitRul= e are unsatisfied now. Change subject: dns: support running up/down script with privsep ...................................................................... dns: support running up/down script with privsep With --user / --group privileges are dropped after init. Unfortunately this affects --dns-script when tearing down previous modifications. To keep the privileges for just that, the concept of a dns script runner in introduced. It's basically a fork of openvpn at the time the modifications to DNS are made. Its only capability is running the --dns-script up/down when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on. Scripts need to be ready to receive variables from a file instead of the process environment. A shameless and effective workaround to keep the protocol between the two processes simple. Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111 Signed-off-by: Heiko Hund --- M distro/dns-scripts/haikuos_file-dns-updown.sh M distro/dns-scripts/openresolv-dns-updown.sh M distro/dns-scripts/resolvconf_file-dns-updown.sh M distro/dns-scripts/systemd-dns-updown.sh M src/openvpn/dns.c M src/openvpn/dns.h M src/openvpn/env_set.c M src/openvpn/env_set.h M src/openvpn/init.c M src/openvpn/openvpn.h 10 files changed, 243 insertions(+), 10 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/9 diff --git a/distro/dns-scripts/haikuos_file-dns-updown.sh b/distro/dns-scr= ipts/haikuos_file-dns-updown.sh index 4a68499..3613073 100644 --- a/distro/dns-scripts/haikuos_file-dns-updown.sh +++ b/distro/dns-scripts/haikuos_file-dns-updown.sh @@ -7,6 +7,10 @@ # # Example env from openvpn (most are not applied): # +# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp +# +# or +# # dev tun0 # script-type dns-up # dns_search_domain_1 mycorp.in @@ -39,6 +43,7 @@ onf=3D/boot/system/settings/network/resolv.conf test -e "$conf" || exit 1 +test -z "${dns_vars_file}" || . "${dns_vars_file}" case "${script_type}" in dns-up) n=3D1 diff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scrip= ts/openresolv-dns-updown.sh index 731f9b5..8435893 100644 --- a/distro/dns-scripts/openresolv-dns-updown.sh +++ b/distro/dns-scripts/openresolv-dns-updown.sh @@ -8,6 +8,10 @@ # # Example env from openvpn (most are not applied): # +# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp +# +# or +# # dev tun0 # script-type dns-up # dns_search_domain_1 mycorp.in @@ -38,6 +42,7 @@ done } +[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" : ${script_type:=3Ddns-down} case "${script_type}" in dns-up) diff --git a/distro/dns-scripts/resolvconf_file-dns-updown.sh b/distro/dns-= scripts/resolvconf_file-dns-updown.sh index 7ee66e9..e0a40d0 100644 --- a/distro/dns-scripts/resolvconf_file-dns-updown.sh +++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh @@ -7,6 +7,10 @@ # # Example env from openvpn (most are not applied): # +# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp +# +# or +# # dev tun0 # script-type dns-up # dns_search_domain_1 mycorp.in @@ -39,6 +43,7 @@ conf=3D/etc/resolv.conf test -e "$conf" || exit 1 +test -z "${dns_vars_file}" || . "${dns_vars_file}" case "${script_type}" in dns-up) n=3D1 diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/distro/dns-scripts/= systemd-dns-updown.sh index c8e455e..50a6a9a 100644 --- a/distro/dns-scripts/systemd-dns-updown.sh +++ b/distro/dns-scripts/systemd-dns-updown.sh @@ -14,6 +14,10 @@ # # Example env from openvpn (not all are always applied): # +# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp +# +# or +# # dev tun0 # script-type dns-up # dns_search_domain_1 mycorp.in @@ -29,6 +33,8 @@ # dns_server_1_sni dns.mycorp.in # +[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" + function do_resolved_servers { local sni=3D"" local transport_var=3Ddns_server_${n}_transport diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 4e728e9..4c5c22a 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -533,13 +533,20 @@ } static int -do_run_up_down_script(bool up, const struct dns_options *o, const struct t= untap *tt) +do_run_up_down_script(bool up, const char *vars_file, const struct dns_opt= ions *o, const struct tuntap *tt) { struct gc_arena gc =3D gc_new(); struct argv argv =3D argv_new(); struct env_set *es =3D env_set_create(&gc); - script_env_set(up, o, tt, es); + if (vars_file) + { + setenv_str(es, "dns_vars_file", vars_file); + } + else + { + script_env_set(up, o, tt, es); + } argv_printf(&argv, "%s", o->script); argv_msg(M_INFO, &argv); @@ -557,8 +564,115 @@ return res; } +static bool +run_script_runner(bool up, struct options *o, const struct tuntap *tt, str= uct dns_script_runner_info *script_runner) +{ + int dns_pipe_fd[2]; + int ack_pipe_fd[2]; + if (pipe(dns_pipe_fd) !=3D 0 + || pipe(ack_pipe_fd) !=3D 0) + { + msg(M_ERR | M_ERRNO, "run_dns_up_down: unable to create pipes"); + return false; + } + script_runner->pid =3D fork(); + if (script_runner->pid =3D=3D -1) + { + msg(M_ERR | M_ERRNO, "run_dns_up_down: unable to fork"); + close(dns_pipe_fd[0]); + close(dns_pipe_fd[1]); + close(ack_pipe_fd[0]); + close(ack_pipe_fd[1]); + return false; + } + else if (script_runner->pid > 0) + { + /* Parent process */ + close(dns_pipe_fd[0]); + close(ack_pipe_fd[1]); + script_runner->fds[0] =3D ack_pipe_fd[0]; + script_runner->fds[1] =3D dns_pipe_fd[1]; + } + else + { + /* Script runner process, close unused FDs */ + for (int fd =3D 3; fd < 100; ++fd) + { + if (fd !=3D dns_pipe_fd[0] + && fd !=3D ack_pipe_fd[1]) + { + close(fd); + } + } + + /* Ignore signals */ + signal(SIGINT, SIG_IGN); + signal(SIGHUP, SIG_IGN); + signal(SIGTERM, SIG_IGN); + signal(SIGUSR1, SIG_IGN); + signal(SIGUSR2, SIG_IGN); + signal(SIGPIPE, SIG_IGN); + + while (1) + { + ssize_t rlen, wlen; + char path[PATH_MAX]; + + /* Block here until parent sends a path */ + rlen =3D read(dns_pipe_fd[0], &path, sizeof(path)); + if (rlen < 1) + { + if (rlen =3D=3D -1 && errno =3D=3D EINTR) + { + continue; + } + close(dns_pipe_fd[0]); + close(ack_pipe_fd[1]); + exit(0); + } + + path[sizeof(path) - 1] =3D '\0'; + int res =3D do_run_up_down_script(up, path, &o->dns_options, t= t); + platform_unlink(path); + + /* Unblock parent process */ + while (1) + { + wlen =3D write(ack_pipe_fd[1], &res, sizeof(res)); + if ((wlen =3D=3D -1 && errno !=3D EINTR) || wlen < sizeof(= res)) + { + /* Not much we can do about errors but exit */ + close(dns_pipe_fd[0]); + close(ack_pipe_fd[1]); + exit(0); + } + else if (wlen =3D=3D sizeof(res)) + { + break; + } + } + + up =3D !up; /* do the opposite next time */ + } + } + + return true; +} + +static const char * +write_dns_vars_file(bool up, const struct options *o, const struct tuntap = *tt, struct gc_arena *gc) +{ + struct env_set *es =3D env_set_create(gc); + const char *dvf =3D platform_create_temp_file(o->tmp_dir, "dvf", gc); + + script_env_set(up, &o->dns_options, tt, es); + env_set_write_file(dvf, es); + + return dvf; +} + static void -run_up_down_script(bool up, struct options *o, const struct tuntap *tt) +run_up_down_script(bool up, struct options *o, const struct tuntap *tt, st= ruct dns_script_runner_info *script_runner) { if (!o->dns_options.script) { @@ -566,7 +680,60 @@ } int status; - status =3D do_run_up_down_script(up, &o->dns_options, tt); + + if (!script_runner->required) + { + /* Run dns script directly */ + status =3D do_run_up_down_script(up, NULL, &o->dns_options, tt); + } + else + { + if (script_runner->pid < 1) + { + /* Need to set up privilege preserving child first */ + if (!run_script_runner(up, o, tt, script_runner)) + { + return; + } + } + + struct gc_arena gc =3D gc_new(); + int rfd =3D script_runner->fds[0]; + int wfd =3D script_runner->fds[1]; + const char *dvf =3D write_dns_vars_file(up, o, tt, &gc); + size_t dvf_size =3D strlen(dvf) + 1; + + while (1) + { + ssize_t len =3D write(wfd, dvf, dvf_size); + if (len < dvf_size) + { + if (len =3D=3D -1 && errno =3D=3D EINTR) + { + continue; + } + msg(M_ERR | M_ERRNO, "could not send dns vars filename"); + } + break; + } + + while (1) + { + ssize_t len =3D read(rfd, &status, sizeof(status)); + if (len < sizeof(status)) + { + if (len =3D=3D -1 && errno =3D=3D EINTR) + { + continue; + } + msg(M_ERR | M_ERRNO, "could not receive dns script status"= ); + } + break; + } + + gc_free(&gc); + } + msg(M_INFO, "dns script exited with status %d", status); } @@ -652,7 +819,7 @@ } void -run_dns_up_down(bool up, struct options *o, const struct tuntap *tt) +run_dns_up_down(bool up, struct options *o, const struct tuntap *tt, struc= t dns_script_runner_info *dsri) { if (!o->dns_options.servers) { @@ -689,6 +856,6 @@ #ifdef _WIN32 run_up_down_service(up, o, tt); #else - run_up_down_script(up, o, tt); + run_up_down_script(up, o, tt, dsri); #endif /* ifdef _WIN32 */ } diff --git a/src/openvpn/dns.h b/src/openvpn/dns.h index 38ced79..fe00403 100644 --- a/src/openvpn/dns.h +++ b/src/openvpn/dns.h @@ -68,6 +68,14 @@ const char *sni; }; +struct dns_script_runner_info { + bool required; + int fds[2]; +#if !defined(_WIN32) + pid_t pid; +#endif +}; + struct dns_options { struct dns_domain *search_domains; struct dns_server *servers_prepull; @@ -154,8 +162,10 @@ * @param up Boolean to set this call to "up" when true * @param o Pointer to the program options * @param tt Pointer to the connection's tuntap struct + * @param dsri Pointer to the script runner info struct */ -void run_dns_up_down(bool up, struct options *o, const struct tuntap *tt); +void run_dns_up_down(bool up, struct options *o, const struct tuntap *tt, + struct dns_script_runner_info *dsri); /** * Puts the DNS options into an environment set. diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c index 81ab59e..3fe23fd 100644 --- a/src/openvpn/env_set.c +++ b/src/openvpn/env_set.c @@ -33,6 +33,7 @@ #include "env_set.h" #include "run_command.h" +#include "platform.h" /* * Set environmental variable (int or string). @@ -235,6 +236,30 @@ } void +env_set_write_file(const char *path, const struct env_set *es) +{ + FILE *fp =3D platform_fopen(path, "w"); + if (!fp) + { + msg(M_ERR, "could not write env set to '%s'", path); + return; + } + + if (es) + { + const struct env_item *item =3D es->list; + while (item) + { + fputs(item->string, fp); + fputc('\n', fp); + item =3D item->next; + } + } + + fclose(fp); +} + +void env_set_inherit(struct env_set *es, const struct env_set *src) { const struct env_item *e; diff --git a/src/openvpn/env_set.h b/src/openvpn/env_set.h index 4294d6e..70d01e2 100644 --- a/src/openvpn/env_set.h +++ b/src/openvpn/env_set.h @@ -91,6 +91,14 @@ void env_set_print(int msglevel, const struct env_set *es); +/** + * Write a struct env_set to a file. Each item on one line. + * + * @param path The filepath to write to. + * @param es Pointer to the env_set to write. + */ +void env_set_write_file(const char *path, const struct env_set *es); + void env_set_inherit(struct env_set *es, const struct env_set *src); /* returns true if environmental variable name starts with 'password' */ diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 1935bec..212457e 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2025,7 +2025,7 @@ c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx); } - run_dns_up_down(true, &c->options, c->c1.tuntap); + run_dns_up_down(true, &c->options, c->c1.tuntap, &c->persist.dsri); /* run the up script */ run_up_down(c->options.up_script, @@ -2065,7 +2065,7 @@ /* explicitly set the ifconfig_* env vars */ do_ifconfig_setenv(c->c1.tuntap, c->c2.es); - run_dns_up_down(true, &c->options, c->c1.tuntap); + run_dns_up_down(true, &c->options, c->c1.tuntap, &c->persist.dsri); /* run the up script if user specified --up-restart */ if (c->options.up_restart) @@ -2155,7 +2155,7 @@ adapter_index =3D c->c1.tuntap->adapter_index; #endif - run_dns_up_down(false, &c->options, c->c1.tuntap); + run_dns_up_down(false, &c->options, c->c1.tuntap, &c->persist.dsri); if (force || !(c->sig->signal_received =3D=3D SIGUSR1 && c->options.pe= rsist_tun)) { diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index df43bba..7d43b2d 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -45,6 +45,7 @@ #include "pool.h" #include "plugin.h" #include "manage.h" +#include "dns.h" /* * Our global key schedules, packaged thusly @@ -120,6 +121,7 @@ struct context_persist { int restart_sleep_seconds; + struct dns_script_runner_info dsri; }; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111 Gerrit-Change-Number: 839 Gerrit-PatchSet: 9 Gerrit-Owner: d12fk Gerrit-Reviewer: MaxF Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: MaxF Gerrit-MessageType: newpatchset --yv03ecHYOwY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: MaxF, flichtenheld, plaisthos.

d12fk uploaded patch set #9 to this change.

View Change

The following approvals got outdated and were removed: Code-Review+2 by flichtenheld, Code-Review-1 by MaxF

The change is no= longer submittable: Code-Review and checks~ChecksSubmitRule are unsatisfie= d now.

dns: support running up/down script with privsep

With --user = / --group privileges are dropped after init. Unfortunately
this affects = --dns-script when tearing down previous modifications.

To keep the p= rivileges for just that, the concept of a dns script runner
in introduce= d. It's basically a fork of openvpn at the time the
modifications to= DNS are made. Its only capability is running the
--dns-script up/down w= hen asked to. The parent openvpn process signals
this by writing to a pi= pe the runner is waiting on.

Scripts need to be ready to receive var= iables from a file instead of the
process environment. A shameless and e= ffective workaround to keep the
protocol between the two processes simpl= e.

Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Signed-of= f-by: Heiko Hund <heiko@...2662...>
---
M distro/dns-scripts/ha= ikuos_file-dns-updown.sh
M distro/dns-scripts/openresolv-dns-updown.shM distro/dns-scripts/resolvconf_file-dns-updown.sh
M distro/dns-script= s/systemd-dns-updown.sh
M src/openvpn/dns.c
M src/openvpn/dns.h
M = src/openvpn/env_set.c
M src/openvpn/env_set.h
M src/openvpn/init.cM src/openvpn/openvpn.h
10 files changed, 243 insertions(+), 10 deletio= ns(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/change=
s/39/839/9
diff --git a/distro/dns-scripts/haikuos_file-dns-updown.=
sh b/distro/dns-scripts/haikuos_file-dns-updown.sh
index 4a= 68499..3613073 100644
--- a/distro/dns-scripts/haikuos_file= -dns-updown.sh
+++ b/distro/dns-scripts/haikuos_file-dns-up= down.sh
@@ -7,6 +7,10 @@
#
# Example env from openvpn (most are not applied):
#
+# dns_vars_file /tmp/= openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp
+#
+# or
= +#
# dev tun0
# script-type dns-up
# dns_search_domain_1 mycorp.in
@@ -39,6 += 43,7 @@

onf=3D/boot/system/settings/netw= ork/resolv.conf
test -e "$conf" || exit 1=
+test -z "${dns_vars_f= ile}" || . "${dns_vars_file}"
case "${= script_type}" in
dns-up)
n=3D1
diff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/d= istro/dns-scripts/openresolv-dns-updown.sh
index 731f9b5..8= 435893 100644
--- a/distro/dns-scripts/openresolv-dns-updow= n.sh
+++ b/distro/dns-scripts/openresolv-dns-updown.sh
@@ -8,6 +8,10 @@
#
# Example = env from openvpn (most are not applied):
#
+# dns_vars_file /tmp/openvpn_dvf_= 58b95c0c97b2db43afb5d745f986c53c.tmp

+#
+#= or
+# # dev tun0
# script-type dns-up
# dns_search_domain_1 mycorp.in

@@ -38,6 +42,7 @@
done
}

+[ -z "${dns_vars_file}" ] = || . "${dns_vars_file}"
: ${script_type:=3Ddns-d= own}
case "${script_type}" in
d= ns-up)
diff --git a/distro/dns-scripts/resolvconf_file-dns-= updown.sh b/distro/dns-scripts/resolvconf_file-dns-updown.sh
index 7ee66e9..e0a40d0 100644

--- a/distro/dns-scripts/re= solvconf_file-dns-updown.sh
+++ b/distro/dns-scripts/resolv= conf_file-dns-updown.sh
@@ -7,6 +7,10 @@
#=
# Example env from openvpn (most are not applied):=
#
+# dns= _vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp
= +#
+# or
+#
# dev tun0
# scrip= t-type dns-up
# dns_search_domain_1 mycorp.in
= @@ -39,6 +43,7 @@

conf=3D/etc/reso= lv.conf
test -e "$conf" || exit 1
+test -z "${dns_vars_file}&quo= t; || . "${dns_vars_file}"

case "${script_t= ype}" in
dns-up)
n=3D1diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/distro/dns-s= cripts/systemd-dns-updown.sh
index c8e455e..50a6a9a 100644<= /span>
--- a/distro/dns-scripts/systemd-dns-updown.sh
+++ b/distro/dns-scripts/systemd-dns-updown.sh

@@ -14,6= +14,10 @@
#
# Example env from openvpn (= not all are always applied):
#
+# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2= db43afb5d745f986c53c.tmp
+#
+# or
+#
# = dev tun0
# script-type dns-up
# dns_s= earch_domain_1 mycorp.in
@@ -29,6 +33,8 @@
= # dns_server_1_sni dns.mycorp.in
#

+[ -z "${dns_vars_= file}" ] || . "${dns_vars_file}"
+
function do_resolved_servers = {
local sni=3D""
local = transport_var=3Ddns_server_${n}_transport
diff --git a/src/= openvpn/dns.c b/src/openvpn/dns.c
index 4e728e9..4c5c22a 10= 0644
--- a/src/openvpn/dns.c
+++ b/src/open= vpn/dns.c
@@ -533,13 +533,20 @@
}
static int
-do_run_up_down_script(bool up, const struct dns_options *o,= const struct tuntap *tt)
+do_run_up_down_script(bool up, const char *vars_file, const struct dn= s_options *o, const struct tuntap *tt)
{
= struct gc_arena gc =3D gc_new();
struct argv argv = =3D argv_new();
struct env_set *es =3D env_set_create(= &gc);

- script_env_set(up, o, tt, es);
+ if (vars_file)
+ {
+ setenv_str(es, "dns_vars_file", vars_file);<= br>+ }
+ else
+ {
+ script_env_set(up, o, tt, es);
+ }

ar= gv_printf(&argv, "%s", o->script);
ar= gv_msg(M_INFO, &argv);
@@ -557,8 +564,115 @@
= return res;
}

+static bool

+run_script_runner(bool up, struct options= *o, const struct tuntap *tt, struct dns_script_runner_info *script_runner)=
+{
+ int dns_pipe_fd[2];
+ int ack_pipe_fd[2];
+ if (pipe(dns_pipe_fd) != =3D 0
+ || pip= e(ack_pipe_fd) !=3D 0)
+ msg= (M_ERR | M_ERRNO, "run_dns_up_down: unable to create pipes");
+ return false;
+ }
+ script_runner->pid =3D fork(= );

+ if (script_ru= nner->pid =3D=3D -1)
+ {
+ ms= g(M_ERR | M_ERRNO, "run_dns_up_down: unable to fork");
= + close(dns_pipe_fd[0]);=
+ close(dns_p= ipe_fd[1]);
+ = close(ack_pipe_fd[0]);
+ return false;
+ }
+ else if (script_runner->pid > 0)
+ {
+ /* Parent process */
+ close(dns_pipe_fd[0]);
+ close(ack_pipe_fd[1]);
+ script_runner->fds[0] = =3D ack_pipe_fd[0];

+= script_runner->fds[1] =3D dns_pipe_fd[1];
+ }
+ else
+ {
+ = /* Script runner process, close unused FDs */
+ for (int fd =3D 3; fd < 100; ++fd)<= /span>
+ {
= + if (fd !=3D dns_pi= pe_fd[0]
+ = && fd !=3D ack_pipe_fd[1])
+ {
+ close(fd);
+ }
+ }
+
+ /* Ign= ore signals */
+ = signal(SIGINT, SIG_IGN);
+ signal(SIGHUP, SIG_IGN);
+ signal(SIGTERM, SIG_IGN);
+ signal(SIGUSR1, SIG_IGN);=
+ signal(SIGUSR2, SI= G_IGN);
+ sign= al(SIGPIPE, SIG_IGN);
+
+ while (1)=
+ {+ ssize_t rlen, wle= n;
+ char = path[PATH_MAX];
+
+ /* Block he= re until parent sends a path */
+ rlen =3D read(dns_pipe_fd[0], &path, sizeof(pat= h));
+ if = (rlen < 1)
+ = {
+ = if (rlen =3D=3D -1 && errno =3D=3D EINTR)
+ {
+ continue;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ close(dns_pipe_fd= [0]);

+ = close(ack_pipe_fd[1]);
+ exit(0);
+ }
+
+ pat= h[sizeof(path) - 1] =3D '\0';
+ int res =3D do_run_up_down_script(up, path, &= amp;o->dns_options, tt);
+ platform_unlink(path);
+
+ /* Unblock parent process */
+ while (1)
+ {
+ wlen =3D write(ack_pipe_fd[1], &re= s, sizeof(res));
+ = if ((wlen =3D=3D -1 && errno !=3D EINTR) || wlen < = sizeof(res))
+ = {
+ = /* Not much we can do about errors but exit */
+ close(dns_pipe_fd= [0]);
+ = close(ack_pipe_fd[1]);
+ exit(0);
+ }
+ else if (wlen =3D=3D sizeof(res))+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ break;
+ }=
+ }
+

+ up =3D !up; /* do the opposite next tim= e */
+ }

+ }
+
+ return true;
+}
+
+static const char *
+write_dns_vars_file(boo= l up, const struct options *o, const struct tuntap *tt, struct gc_arena *gc= )
+{
+ struct env_set *es =3D env_set_c= reate(gc);
+ const= char *dvf =3D platform_create_temp_file(o->tmp_dir, "dvf", gc= );
+
+ script_env_set(up, &o->dn= s_options, tt, es);
+= env_set_write_file(dvf, es);
+
+ re= turn dvf;
+}+
static voi= d
-run_up_down_script(b= ool up, struct options *o, const struct tuntap *tt)
+run_up_down_script(bool up, struct option= s *o, const struct tuntap *tt, struct dns_script_runner_info *script_runner= )
{
if (!o->dns_options.script)
{
@@ -566,7 +680,60 @@
= }

int status;
- status =3D do_run_up_down_script(up,= &o->dns_options, tt);
+
+ if (!= script_runner->required)
+ {
+ = /* Run dns script directly */
+ status =3D do_run_up_down_script(up, NULL, &o->d= ns_options, tt);
+ = }
+ else+ {
+ if (script_runner->pid < 1)=
+ {+ /* Need to set up= privilege preserving child first */
+ if (!run_script_runner(up, o, tt, script_runne= r))
+ {
+ return= ;
+ }
+ }
+

+ struct gc_arena gc =3D gc_new();
+ int rfd =3D script_runne= r->fds[0];

+ = int wfd =3D script_runner->fds[1];
+ const char *dvf =3D write_dns_vars_file(up, o, = tt, &gc);
+ = size_t dvf_size =3D strlen(dvf) + 1;
+
+= while (1)
+ = {
+ = ssize_t len =3D write(wfd, dvf, dvf_size);
+ if (len < dvf_size)
+ {
+ if (len =3D=3D -1 &&a= mp; errno =3D=3D EINTR)
+ {
+ continue;
+ }
+ msg(M_ERR | M_ERRNO, "could not send dns var= s filename");
+ = }
+ = break;
+ = }
+
+ while (1)
+ {
+ ssize_t len =3D read(rfd, &status= , sizeof(status));
+ = if (len < sizeof(status))
+ {
+ if (len =3D=3D -1 && errno =3D=3D EIN= TR)
+ = {
+ = continue;
+ = }
+ = msg(M_ERR | M_ERRNO, "could not receive dns script status")= ;
+ }
+ break;
+ }
+

+ gc_free(&gc);
+ }
+
msg(M_INFO, "dns script exited with= status %d", status);
}

<= span>@@ -652,7 +819,7 @@

}

void

-run_dns_up_do= wn(bool up, struct options *o, const struct tuntap *tt)
+run_dns_up_down(bool up, struct options= *o, const struct tuntap *tt, struct dns_script_runner_info *dsri) {
if (!o->dns_options.servers)
<= span> {

@@ -689,6 +856,6 @@
#ifdef _WI= N32
run_up_down_service(up, o, tt);
#= else
- run_up_down_s= cript(up, o, tt);
+ = run_up_down_script(up, o, tt, dsri);
#endif /* ifdef _WI= N32 */
}
diff --git a/src/openvpn/dns.h b/= src/openvpn/dns.h
index 38ced79..fe00403 100644
<= span>--- a/src/openvpn/dns.h

+++ b/src/openvpn/dns.h=
@@ -68,6 +68,14 @@
const char *sni; };

+struct dns_script_runner_info {
+ bool required;
+ int fds[2];
+#if !defined(_WIN32)
+ pid_t pid;
+#endif
+= };
+
= struct dns_options {
struct dns_domain *search_domain= s;
struct dns_server *servers_prepull;
@@ -154,8 +162,10 @@
* @param up Boolean to s= et this call to "up" when true
* @param o = Pointer to the program options
* @param tt = Pointer to the connection's tuntap struct
+ * @param dsri Pointer to the script= runner info struct
*/
-void run_dns_up_down(bool up, struct options *o, const s= truct tuntap *tt);
+v= oid run_dns_up_down(bool up, struct options *o, const struct tuntap *tt,
+ s= truct dns_script_runner_info *dsri);

/**=
* Puts the DNS options into an environment set.diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.cindex 81ab59e..3fe23fd 100644
--- a/src/openvpn/env= _set.c
+++ b/src/openvpn/env_set.c
@@ -33,6= +33,7 @@
#include "env_set.h"
=
#include "run_command.h"
+#include "platform.h"
/*
* Set environmental variable= (int or string).
@@ -235,6 +236,30 @@
}

void
+env_set_write_file(const char *path, const struct env_se= t *es)
+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ FILE *fp =3D platform_fopen= (path, "w");

+ = {
+ msg(M_ERR,= "could not write env set to '%s'", path);
+ return;

+ }
+
+ if (es)
+ = {
+ const stru= ct env_item *item =3D es->list;
+ while (item)
+ {
+ fputs(item->string, fp);
+ fputc('\n', fp);
<= span style=3D"color: hsl(120, 100%, 40%);">+ item =3D item->n= ext;
+ }

+ }
+
+ fclose(fp);
+}
+
+void
en= v_set_inherit(struct env_set *es, const struct env_set *src)
{

const struct env_item *e;
diff --= git a/src/openvpn/env_set.h b/src/openvpn/env_set.h
index 4= 294d6e..70d01e2 100644
--- a/src/openvpn/env_set.h+++ b/src/openvpn/env_set.h
@@ -91,6 +91,14 @@

void env_set_print(int msglevel, const struc= t env_set *es);

+/**
+ *= Write a struct env_set to a file. Each item on one line.
+ *
+ * @param path The filepath to write to.
+ * @param es Pointer to the e= nv_set to write.

+ */=
+void env_set_write_= file(const char *path, const struct env_set *es);
+
void env_set_inherit(struct= env_set *es, const struct env_set *src);

/* returns true if environmental variable name starts with 'password&= #39; */
diff --git a/src/openvpn/init.c b/src/openvpn/init.= c
index 1935bec..212457e 100644
--- a/src/o= penvpn/init.c
+++ b/src/openvpn/init.c
@@ -= 2025,7 +2025,7 @@
c->c2.frame.t= un_mtu, c->c2.es, &c->net_ctx);
}=

- ru= n_dns_up_down(true, &c->options, c->c1.tuntap);
+ run_dns_up_down(true, &c-= >options, c->c1.tuntap, &c->persist.dsri);

/* run the up script */
ru= n_up_down(c->options.up_script,
@@ -2065,7 +2065,7 @@
/* explicitly set the ifconfig_* env vars */<= br> do_ifconfig_setenv(c->c1.tuntap, c->c2.es);<= br>
- run= _dns_up_down(true, &c->options, c->c1.tuntap);
+ run_dns_up_down(true, &c-&= gt;options, c->c1.tuntap, &c->persist.dsri);

/* run the up script if user specified --up-restart = */
if (c->options.up_restart)
@= @ -2155,7 +2155,7 @@
adapter_index =3D c->c1.tuntap= ->adapter_index;
#endif

- run_dns_up_down(false, &c-&= gt;options, c->c1.tuntap);

+ run_dns_up_down(false, &c->options, c->c1.tuntap, &= amp;c->persist.dsri);

if (force |= | !(c->sig->signal_received =3D=3D SIGUSR1 && c->options.p= ersist_tun))
{
diff --git a/src/openvp= n/openvpn.h b/src/openvpn/openvpn.h
index df43bba..7d43b2d = 100644
--- a/src/openvpn/openvpn.h
+++ b/sr= c/openvpn/openvpn.h
@@ -45,6 +45,7 @@
#inc= lude "pool.h"
#include "plugin.h"
#include "manage.h"
+#include "dns.h"
<= br> /*
* Our global key schedules, packaged thusly<= /span>
@@ -120,6 +121,7 @@
struct context_persist=
{
int restart_sleep_seconds;<= br>+ struct dns_script_runne= r_info dsri;
};

=

To view, visit change 839. To unsubscribe, or for = help writing mail filters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115= ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 9
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: MaxF <max@...2606...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: MaxF <max@...2606...> =
Gerrit-MessageType: newpatchset
--yv03ecHYOwY=--