]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Refuse to start on -u/-g misconfig
authorPieter Lexis <pieter.lexis@powerdns.com>
Mon, 20 May 2019 13:51:52 +0000 (15:51 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 20 May 2019 13:51:52 +0000 (15:51 +0200)
pdns/dnsdist.cc
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/dnsdist-systemd.cc [new file with mode: 0644]
pdns/dnsdistdist/dnsdist-systemd.hh [new file with mode: 0644]

index 98d646d54f73515a3357e5c4f677136ee52e2d06..cc233636420fb54e11b356a060e3ed8ac32d3fe5 100644 (file)
@@ -37,6 +37,7 @@
 #include <editline/readline.h>
 #endif
 
+#include "dnsdist-systemd.hh"
 #ifdef HAVE_SYSTEMD
 #include <systemd/sd-daemon.h>
 #endif
@@ -2659,10 +2660,21 @@ try
   if(!g_cmdLine.uid.empty())
     newuid = strToUID(g_cmdLine.uid.c_str());
 
-  if (getegid() != newgid)
+  if (getegid() != newgid) {
+    if (running_in_service_mgr()) {
+      errlog("--gid/-g set on command-line, but dnsdist was started as a systemd service. Use the 'Group' setting in the systemd unit file to set the group to run as");
+      _exit(EXIT_FAILURE);
+    }
     dropGroupPrivs(newgid);
-  if (geteuid() != newuid)
+  }
+
+  if (geteuid() != newuid) {
+    if (running_in_service_mgr()) {
+      errlog("--uid/-u set on command-line, but dnsdist was started as a systemd service. Use the 'User' setting in the systemd unit file to set the user to run as");
+      _exit(EXIT_FAILURE);
+    }
     dropUserPrivs(newuid);
+  }
 
   try {
     /* we might still have capabilities remaining,
index 36177aabbe1323de1e02b303e462b41a17695f3c..5f54b0babb6d74850d01b348a2b47fc0bca6e355 100644 (file)
@@ -127,6 +127,7 @@ dnsdist_SOURCES = \
        dnsdist-rules.hh \
        dnsdist-secpoll.cc dnsdist-secpoll.hh \
        dnsdist-snmp.cc dnsdist-snmp.hh \
+       dnsdist-systemd.cc dnsdist-systemd.hh \
        dnsdist-tcp.cc \
        dnsdist-web.cc \
        dnsdist-xpf.cc dnsdist-xpf.hh \
diff --git a/pdns/dnsdistdist/dnsdist-systemd.cc b/pdns/dnsdistdist/dnsdist-systemd.cc
new file mode 100644 (file)
index 0000000..6f9f890
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include "config.h"
+#include "dnsdist-systemd.hh"
+#include <cstdlib>
+
+bool running_in_service_mgr() {
+#ifdef HAVE_SYSTEMD
+  char *c;
+  c = getenv("NOTIFY_SOCKET"); // XXX Ideally we'd check for INVOCATION_ID (systemd.exec(5)), but that was introduced in systemd 232, and Debian Jessie has 215
+  if (c != nullptr) {
+    return true;
+  }
+#endif
+  return false;
+}
diff --git a/pdns/dnsdistdist/dnsdist-systemd.hh b/pdns/dnsdistdist/dnsdist-systemd.hh
new file mode 100644 (file)
index 0000000..046905c
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#pragma once
+
+bool running_in_service_mgr();