Skip to contentSkip to conversation

Change in openvpn[master]: dns: support running up/down script with privsep

Openvpn-devel Superseded d12fk (Code Review) 2 participants 40 messages Dec 12, 2024 – May 17, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1733989644000.I6b67e3a00dd84bf348b6af28115ee11138c3a111@...2715...

#1 lore raw

Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to review the following change.

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 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

---
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

6 files changed, 222 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/1

src/openvpn/dns.c +173 -6
index 2f21ed5..14c1270 100644
@@ -532,13 +532,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 dns_options *o, const struct tuntap *tt)
 {
     struct gc_arena gc = gc_new();
     struct argv argv = argv_new();
     struct env_set *es = 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);
@@ -549,11 +556,171 @@
     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) != 0
+        || pipe(ack_pipe_fd) != 0)
+    {
+        msg(M_ERR, "run_dns_up_down: unable to create pipes");
+        return false;
+    }
+    script_runner->pid = fork();
+    if (script_runner->pid == -1)
+    {
+        msg(M_ERR, "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] = ack_pipe_fd[0];
+        script_runner->fds[1] = dns_pipe_fd[1];
+    }
+    else
+    {
+        /* Script runner process, close unused FDs */
+        for (int fd = 3; fd < 100; ++fd)
+        {
+            if (fd != dns_pipe_fd[0]
+                && fd != 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 = read(dns_pipe_fd[0], &path, sizeof(path));
+            if (rlen < 1)
+            {
+                if (rlen == -1 && errno != EINTR)
+                {
+                    continue;
+                }
+                close(dns_pipe_fd[0]);
+                close(ack_pipe_fd[1]);
+                exit(0);
+            }
+
+            path[sizeof(path) - 1] = '\0';
+            int res = do_run_up_down_script(up, path, &o->dns_options, tt);
+            platform_unlink(path);
+
+            /* Unblock parent process */
+            while (1)
+            {
+                wlen = write(ack_pipe_fd[1], &res, sizeof(res));
+                if ((wlen == -1 && errno != 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 == sizeof(res))
+                {
+                    break;
+                }
+            }
+
+            up = !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 = env_set_create(gc);
+    const char *dvf = 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, struct dns_script_runner_info *script_runner)
 {
     int status;
-    status = do_run_up_down_script(up, &o->dns_options, tt);
+
+    if (!script_runner->required)
+    {
+        /* Run dns script directly */
+        status = 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 = gc_new();
+        int rfd = script_runner->fds[0];
+        int wfd = script_runner->fds[1];
+        const char *dvf = write_dns_vars_file(up, o, tt, &gc);
+        size_t dvf_size = strlen(dvf) + 1;
+
+        while (1)
+        {
+            ssize_t len = write(wfd, dvf, dvf_size);
+            if (len < dvf_size)
+            {
+                if (len == -1 && errno == EINTR)
+                {
+                    continue;
+                }
+                msg(M_ERR, "could not send dns vars filename");
+            }
+            break;
+        }
+
+        while (1)
+        {
+            ssize_t len = read(rfd, &status, sizeof(status));
+            if (len < sizeof(status))
+            {
+                if (len == -1 && errno == EINTR)
+                {
+                    continue;
+                }
+                msg(M_ERR, "could not receive dns script status");
+            }
+            break;
+        }
+
+        gc_free(&gc);
+    }
+
     msg(M_INFO, "dns script exited with status %d", status);
 }
 
@@ -639,7 +806,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, struct dns_script_runner_info *dsri)
 {
     if (!o->dns_options.servers)
     {
@@ -676,6 +843,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 */
 }
src/openvpn/dns.h +11 -1
index 39a3393..47f7e5d 100644
@@ -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;
@@ -153,8 +161,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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +3 -3
index 36a9bca..541915f 100644
@@ -2008,7 +2008,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,
@@ -2048,7 +2048,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)
@@ -2138,7 +2138,7 @@
     adapter_index = 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 == SIGUSR1 && c->options.persist_tun))
     {
src/openvpn/openvpn.h +2 -0
index 8dfcab4..391bf04 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 5:

(2 comments)

Commit Message:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/b4ed22e0_13a57781 :
PS5, Line 18:
"need to be ready"

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/800806d7_0538dae0 :
PS5, Line 573: msg(M_ERR, "run_dns_up_down: unable to fork");
Should use errno?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 5
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Fri, 27 Dec 2024 17:07:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 5:

(2 comments)

Commit Message:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/f22a9aa2_82b332c1 :
PS5, Line 18:

Quoted 1 line

"need to be ready"

Acknowledged

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/63f8b336_269dee2d :
PS5, Line 573: msg(M_ERR, "run_dns_up_down: unable to fork");

Quoted 1 line

Should use errno?

Acknowledged

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 5
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Tue, 31 Dec 2024 00:34:11 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
Gerrit-MessageType: comment

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#6).

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

---
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

6 files changed, 222 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/6

src/openvpn/dns.c +173 -6
index 2f21ed5..199d147 100644
@@ -532,13 +532,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 dns_options *o, const struct tuntap *tt)
 {
     struct gc_arena gc = gc_new();
     struct argv argv = argv_new();
     struct env_set *es = 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);
@@ -549,11 +556,171 @@
     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) != 0
+        || pipe(ack_pipe_fd) != 0)
+    {
+        msg(M_ERR | M_ERRNO, "run_dns_up_down: unable to create pipes");
+        return false;
+    }
+    script_runner->pid = fork();
+    if (script_runner->pid == -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] = ack_pipe_fd[0];
+        script_runner->fds[1] = dns_pipe_fd[1];
+    }
+    else
+    {
+        /* Script runner process, close unused FDs */
+        for (int fd = 3; fd < 100; ++fd)
+        {
+            if (fd != dns_pipe_fd[0]
+                && fd != 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 = read(dns_pipe_fd[0], &path, sizeof(path));
+            if (rlen < 1)
+            {
+                if (rlen == -1 && errno != EINTR)
+                {
+                    continue;
+                }
+                close(dns_pipe_fd[0]);
+                close(ack_pipe_fd[1]);
+                exit(0);
+            }
+
+            path[sizeof(path) - 1] = '\0';
+            int res = do_run_up_down_script(up, path, &o->dns_options, tt);
+            platform_unlink(path);
+
+            /* Unblock parent process */
+            while (1)
+            {
+                wlen = write(ack_pipe_fd[1], &res, sizeof(res));
+                if ((wlen == -1 && errno != 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 == sizeof(res))
+                {
+                    break;
+                }
+            }
+
+            up = !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 = env_set_create(gc);
+    const char *dvf = 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, struct dns_script_runner_info *script_runner)
 {
     int status;
-    status = do_run_up_down_script(up, &o->dns_options, tt);
+
+    if (!script_runner->required)
+    {
+        /* Run dns script directly */
+        status = 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 = gc_new();
+        int rfd = script_runner->fds[0];
+        int wfd = script_runner->fds[1];
+        const char *dvf = write_dns_vars_file(up, o, tt, &gc);
+        size_t dvf_size = strlen(dvf) + 1;
+
+        while (1)
+        {
+            ssize_t len = write(wfd, dvf, dvf_size);
+            if (len < dvf_size)
+            {
+                if (len == -1 && errno == EINTR)
+                {
+                    continue;
+                }
+                msg(M_ERR | M_ERRNO, "could not send dns vars filename");
+            }
+            break;
+        }
+
+        while (1)
+        {
+            ssize_t len = read(rfd, &status, sizeof(status));
+            if (len < sizeof(status))
+            {
+                if (len == -1 && errno == 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);
 }
 
@@ -639,7 +806,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, struct dns_script_runner_info *dsri)
 {
     if (!o->dns_options.servers)
     {
@@ -676,6 +843,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 */
 }
src/openvpn/dns.h +11 -1
index 39a3393..47f7e5d 100644
@@ -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;
@@ -153,8 +161,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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +3 -3
index 36a9bca..541915f 100644
@@ -2008,7 +2008,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,
@@ -2048,7 +2048,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)
@@ -2138,7 +2138,7 @@
     adapter_index = 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 == SIGUSR1 && c->options.persist_tun))
     {
src/openvpn/openvpn.h +2 -0
index 8dfcab4..391bf04 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 6
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#7).

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

---
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/7

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index a4448bd..8c0f997 100644
@@ -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
@@ -26,6 +30,7 @@
 
 conf=/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)
     text="### openvpn ${dev} begin ###\n"
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index d2ee1d7..9be2d57 100644
@@ -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
@@ -24,6 +28,7 @@
 #
 
 set -e +u
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
 : ${script_type:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index a1a4a65..47c8ceb 100644
@@ -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
@@ -26,6 +30,7 @@
 
 conf=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     text="### openvpn ${dev} begin ###\n"
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 5be04c3..d290e5b 100644
@@ -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=""
     [ "$dns_server_1_transport" = "DoT" ] && sni="#$dns_server_1_sni"
src/openvpn/dns.c +173 -6

210 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 39a3393..47f7e5d 100644
@@ -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;
@@ -153,8 +161,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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +3 -3
index 04db9e0..29506b3 100644
@@ -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 = 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 == SIGUSR1 && c->options.persist_tun))
     {
src/openvpn/openvpn.h +2 -0
index df43bba..7d43b2d 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 7
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#6 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 7:

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/fa5e5bc1_2b2282cf :
PS7, Line 709: ssize_t len = read(rfd, &status, sizeof(status));
Do we need some kind of timeout handling here?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 7
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Fri, 17 Jan 2025 13:53:32 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#7 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 7:

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/6e128420_528a4795 :
PS7, Line 709: ssize_t len = read(rfd, &status, sizeof(status));

Quoted 1 line

Do we need some kind of timeout handling here?

Hm, that's a good thought. I'd rather implement the timeout in the script runner process, since the only source for an indefinite "hang" is the script itself. By handling this in the runner, we could also make sure that it is ready for the next request, i.e. unblocked. If we just implement the timeout here next time we need to run the script it will timeout again otherwise.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 7
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Mon, 20 Jan 2025 19:19:40 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
Gerrit-MessageType: comment

#8 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 8:

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/032ef1df_3b6026f8 :
PS7, Line 709: ssize_t len = read(rfd, &status, sizeof(status));

Quoted 1 line

Hm, that's a good thought. […]

So, after careful consideration I think it is doable to add a timeout mechanism to script calls, but I think if we decide to do that it should be done for all script hooks. Currently it is not done, and if a script hangs indefinitely openvpn is block indefinitely. I think accepting that is also a valid outcome, so we need to decide as a group which way to go.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 27 Feb 2025 00:29:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
Comment-In-Reply-To: d12fk <[email protected]...>
Gerrit-MessageType: comment

#9 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 8: Code-Review+2

(2 comments)

Patchset:

PS8:
Seems to work fine in my tests on Linux

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/c8898136_2f4ffd91 :
PS7, Line 709: ssize_t len = read(rfd, &status, sizeof(status));

Quoted 1 line

So, after careful consideration I think it is doable to add a timeout mechanism to script calls, but […]

Fair enough.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Mon, 03 Mar 2025 16:49:11 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: flichtenheld <[email protected]...>
Comment-In-Reply-To: d12fk <[email protected]...>
Gerrit-MessageType: comment

#10 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 8: Code-Review-1

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/633578a0_e70336df :
PS8, Line 625: if (rlen == -1 && errno != EINTR)
Isn't this the wrong way around? If the error is EINTR, we want to continue, if it's a real error we want to exit?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Tue, 04 Mar 2025 17:46:07 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#11 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, plaisthos.

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 8:

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/5ffe9b89_603f7b8b :
PS8, Line 625: if (rlen == -1 && errno != EINTR)

Quoted 1 line

Isn't this the wrong way around? If the error is EINTR, we want to continue, if it's a real error we […]

You are right, good catch.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Wed, 05 Mar 2025 03:34:28 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: MaxF <[email protected]...>
Gerrit-MessageType: comment

#12 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, plaisthos.

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 8:

(1 comment)

File src/openvpn/dns.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/2d0641f5_c24f8b1d :
PS7, Line 709: ssize_t len = read(rfd, &status, sizeof(status));

Quoted 1 line

Fair enough.

Done

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Wed, 05 Mar 2025 03:34:57 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
Comment-In-Reply-To: d12fk <[email protected]...>
Gerrit-MessageType: comment

#13 lore raw
In reply to a message not in the archive

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=email

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~ChecksSubmitRule 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

---
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

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 4a68499..3613073 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 731f9b5..8435893 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 7ee66e9..e0a40d0 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index c8e455e..50a6a9a 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 38ced79..fe00403 100644
@@ -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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +3 -3
index 1935bec..212457e 100644
@@ -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 = 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 == SIGUSR1 && c->options.persist_tun))
     {
src/openvpn/openvpn.h +2 -0
index df43bba..7d43b2d 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 9
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#14 lore raw
In reply to a message not in the archive

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

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 9: Code-Review+1

(1 comment)

Patchset:

PS9:
I didn't do an in-depth review, but the EINTR thing I noticed is fixed.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 9
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 05 Mar 2025 09:32:36 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#15 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 9: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 9
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 05 Mar 2025 10:10:40 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#16 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#10).

The change is no longer submittable: checks~ChecksSubmitRule is 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

---
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/10

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 38ced79..fe00403 100644
@@ -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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +3 -3
index 1935bec..212457e 100644
@@ -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 = 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 == SIGUSR1 && c->options.persist_tun))
     {
src/openvpn/openvpn.h +2 -0
index df43bba..7d43b2d 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 10
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset

#17 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

The change is no longer submittable: Code-Review is unsatisfied now.

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 10: -Code-Review

(1 comment)

Patchset:

PS10:
still testing

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 10
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Thu, 06 Mar 2025 12:54:53 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#18 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 10: Code-Review-2

(1 comment)

Patchset:

PS10:
Doesn't seem to work right now. Script runner is never used.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 10
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Thu, 06 Mar 2025 13:09:42 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#19 lore raw
In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#11).

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

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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/11

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 38ced79..fe00403 100644
@@ -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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..5677ac3 100644
@@ -2026,7 +2026,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,
@@ -2066,7 +2066,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)
@@ -2156,7 +2156,7 @@
     adapter_index = 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 == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.dsri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..055626b 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 11
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#20 lore raw
In reply to a message not in the archive

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

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down script with privsep
......................................................................

Patch Set 12: Code-Review+2

(1 comment)

Patchset:

PS12:
fixed

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 12
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Fri, 07 Mar 2025 10:27:13 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#21 lore raw
In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#13).

The change is no longer submittable: checks~ChecksSubmitRule is 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/13

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 38ced79..fe00403 100644
@@ -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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..5677ac3 100644
@@ -2026,7 +2026,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,
@@ -2066,7 +2066,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)
@@ -2156,7 +2156,7 @@
     adapter_index = 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 == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.dsri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..055626b 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 13
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#22 lore raw
In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#14).

The change is no longer submittable: checks~ChecksSubmitRule is 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/14

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index 38ced79..fe00403 100644
@@ -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.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..5677ac3 100644
@@ -2026,7 +2026,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,
@@ -2066,7 +2066,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)
@@ -2156,7 +2156,7 @@
     adapter_index = 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 == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.dsri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..055626b 100644
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 14
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#23 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#15).

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

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

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

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/15

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index e34e07d..c21b33a 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..9ad64aa 100644
@@ -2026,7 +2026,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2066,7 +2066,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2156,7 +2156,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 15
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#24 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

Patch Set 16: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 16
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Thu, 20 Mar 2025 11:38:56 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#25 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#17).

The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

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

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/17

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index e34e07d..c21b33a 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..9ad64aa 100644
@@ -2026,7 +2026,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2066,7 +2066,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2156,7 +2156,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 17
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#26 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#20).

The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

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

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/20

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..9ad64aa 100644
@@ -2026,7 +2026,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2066,7 +2066,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2156,7 +2156,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 20
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#27 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#21).

The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

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

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/21

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 69bbebf..962310d 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..9ad64aa 100644
@@ -2026,7 +2026,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2066,7 +2066,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2156,7 +2156,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 21
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#28 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#22).

The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

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

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/22

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 1b03e9c..f1472c0 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index 1b218f5..1944e10 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c469490..8ab4969 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index 43c31b9..9961a80 100644
@@ -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=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 9eb8290..9ad64aa 100644
@@ -2026,7 +2026,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2066,7 +2066,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2156,7 +2156,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3968,6 +3968,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = c0->uid_gid_specified;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 22
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#29 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

Patch Set 22:

(1 comment)

File src/openvpn/init.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/cb719551_d3fda8ef :
PS22, Line 3973:
this would tie the necessity for the persistent runner to "user *or* group defined" - but technically if only the GID is changed ("no idea why one would *do* that, but we have those options") the runner isn't needed... so this could be = user_defined.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 22
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Fri, 02 May 2025 20:24:03 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#30 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#23).

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

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

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/23

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 333a89a..5a143fe 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 87b6641..1fe2d90 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 23
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#31 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

Patch Set 22:

(1 comment)

File src/openvpn/init.c:

http://gerrit.openvpn.net/c/openvpn/+/839/comment/3fd9ac63_442de512 :
PS22, Line 3973:

Quoted 1 line

this would tie the necessity for the persistent runner to "user *or* group defined" - but technicall […]

Done

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 22
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Sat, 10 May 2025 12:36:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: cron2 <[email protected]...>
Gerrit-MessageType: comment

#32 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

Patch Set 23: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 23
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Tue, 13 May 2025 11:37:00 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#33 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, cron2, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#24).

The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/24

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 2e70554..abcbc1c 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index c09a78d..4716c00 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 24
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#34 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

Hello MaxF, cron2, flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/839?usp=email

to look at the new patch set (#25).

The following approvals got outdated and were removed:
Code-Review+2 by cron2

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

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---
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, 248 insertions(+), 12 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/25

distro/dns-scripts/haikuos_file-dns-updown.sh +6 -1
index 2e70554..bc00e10 100644
@@ -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=/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=1
@@ -74,7 +79,7 @@
         done
         text="${text}search $domains\n"
     }
-    text="${text}### openvpn ${dev} end ###\n"
+    text="${text}### openvpn ${dev} end ###"
     text="${text}\n$(cat ${conf})"
 
     echo "${text}" > "${conf}"
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +6 -1
index c09a78d..70872c7 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
@@ -74,7 +79,7 @@
         done
         text="${text}search $domains\n"
     }
-    text="${text}### openvpn ${dev} end ###\n"
+    text="${text}### openvpn ${dev} end ###"
     text="${text}\n$(cat ${conf})"
 
     echo "${text}" > "${conf}"
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 25
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#35 lore raw

PATCH v26 1/1 dns: support running up/down command with privsep

In reply to a message not in the archive

From: Heiko Hund <[email protected]...>

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/839
This mail reflects revision 26 of this Change.

Acked-by according to Gerrit (reflected above):

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 748804e..bc00e10 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 567b402..70872c7 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 
#36 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

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

cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

Patch Set 27: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 27
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Sat, 17 May 2025 08:38:14 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#37 lore raw

PATCH v27 1/1 dns: support running up/down command with privsep

In reply to a message not in the archive

From: Heiko Hund <[email protected]...>

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/839
This mail reflects revision 27 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 7239b70..777b72d 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 567b402..70872c7 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 
#38 lore raw

[Openvpn-devel] [PATCH applied] Re: dns: support running up/down command with privsep

So, at last...

I have tested this on Linux and FreeBSD 13, integrated into the t_client framework ("make sure that resolv.conf / resolvectl status look the same after the end of the test as when it started"). On Linux this is easy because "close(tunfd)" will destroy the tun interface and all routes, while on FreeBSD OpenVPN needs privileges to clean up - so "--user nobody" doesn't work very well over there (plugin-down-root helps).

I did try a few nasty things, like "killing the privileged helper while the unprivileged openvpn is running" - of course it will not clean up DNS, then, but it also does not clean up anything else - seems there is a bit of looping ("in the error handler, clean up dns, error again, so give up") - this might be a candidate for a followup patch...

^C2025-05-17 11:12:51 event_wait : Interrupted system call (fd=-1,code=4)

2025-05-17 11:12:51 could not receive dns updown status: Broken pipe (errno=32)
2025-05-17 11:12:51 Exiting due to fatal error
2025-05-17 11:12:51 could not receive dns updown status: Broken pipe (errno=32)
2025-05-17 11:12:51 Exiting due to fatal error

Also, if you kill -STOP the background process ("something with the pipe is awry") the unprivileged process will get stuck, as there is no timeout guarding the pipe handling. Not sure how we do this with other background processes, need to have a closer look...

(but besides this, just don't kill or otherwise mess with random processes)

Your patch has been applied to the master branch.

commit 1dfe8729f6c65812bb2ee8a511c968d48d531840

Author: Heiko Hund
Date:   Sat May 17 10:38:27 2025 +0200
     dns: support running up/down command with privsep

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg31668.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#39 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

cron2 has uploaded a new patch set (#28) to the change originally created by d12fk. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by cron2

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg31668.html

---
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, 246 insertions(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/839/28

distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 7239b70..777b72d 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 567b402..70872c7 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 28
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset

#40 lore raw

[Openvpn-devel] [L] Change in openvpn[master]: dns: support running up/down command with privsep

In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/839?usp=email )

Change subject: dns: support running up/down command with privsep
......................................................................

dns: support running up/down command with privsep

With --user privileges are dropped after init. Unfortunately this affects --dns-updown when undoing previous modifications.

To keep the privileges for just that, the concept of a dns updown 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-updown command when asked to. The parent openvpn process signals this by writing to a pipe the runner is waiting on.

Commands 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

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg31668.html

---
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, 246 insertions(+), 10 deletions(-)
distro/dns-scripts/haikuos_file-dns-updown.sh +5 -0
index 7239b70..777b72d 100644
@@ -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=/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=1
distro/dns-scripts/openresolv-dns-updown.sh +5 -0
index e50398c..1404819 100644
@@ -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:=dns-down}
 case "${script_type}" in
 dns-up)
distro/dns-scripts/resolvconf_file-dns-updown.sh +5 -0
index 567b402..70872c7 100644
@@ -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=/etc/resolv.conf
 test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
distro/dns-scripts/systemd-dns-updown.sh +6 -0
index ecadd29..6eadabc 100644
@@ -15,6 +15,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
@@ -30,6 +34,8 @@
 #   dns_server_1_sni dns.mycorp.in
 #
 
+[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+
 function do_resolved_servers {
     local sni=""
     local transport_var=dns_server_${n}_transport
src/openvpn/dns.c +173 -6

214 lines not on this page · raw message

src/openvpn/dns.h +11 -1
index c4d19ff..4cc1e73 100644
@@ -68,6 +68,14 @@
     const char *sni;
 };
 
+struct dns_updown_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   duri        Pointer to the updown 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_updown_runner_info *duri);
 
 /**
  * Puts the DNS options into an environment set.
src/openvpn/env_set.c +25 -0
index 81ab59e..3fe23fd 100644
@@ -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 = 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 =  es->list;
+        while (item)
+        {
+            fputs(item->string, fp);
+            fputc('\n', fp);
+            item = item->next;
+        }
+    }
+
+    fclose(fp);
+}
+
+void
 env_set_inherit(struct env_set *es, const struct env_set *src)
 {
     const struct env_item *e;
src/openvpn/env_set.h +8 -0
index 4294d6e..70d01e2 100644
@@ -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' */
src/openvpn/init.c +6 -3
index 187c0a9..e0ba255 100644
@@ -2027,7 +2027,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.duri);
 
         /* run the up script */
         run_up_down(c->options.up_script,
@@ -2067,7 +2067,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.duri);
 
         /* run the up script if user specified --up-restart */
         if (c->options.up_restart)
@@ -2157,7 +2157,7 @@
     adapter_index = 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.duri);
 
     if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
     {
@@ -3971,6 +3971,9 @@
 
         c0->uid_gid_specified = user_defined || group_defined;
 
+        /* fork the dns script runner to preserve root? */
+        c->persist.duri.required = user_defined;
+
         /* perform postponed chdir if --daemon */
         if (c->did_we_daemonize && c->options.cd_dir == NULL)
         {
src/openvpn/openvpn.h +2 -0
index 0bbd1a4..1023520 100644
@@ -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_updown_runner_info duri;
 };
 
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/839?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6b67e3a00dd84bf348b6af28115ee11138c3a111
Gerrit-Change-Number: 839
Gerrit-PatchSet: 28
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged