Zone "kratochvil.vellum.cz" reloading wrapper, submitted to Klokan
authorshort <>
Sun, 22 Jul 2001 22:28:30 +0000 (22:28 +0000)
committershort <>
Sun, 22 Jul 2001 22:28:30 +0000 (22:28 +0000)
src/ndc-reload-short.c [new file with mode: 0644]

diff --git a/src/ndc-reload-short.c b/src/ndc-reload-short.c
new file mode 100644 (file)
index 0000000..f495927
--- /dev/null
@@ -0,0 +1,73 @@
+#include <stdlib.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+extern const char **environ;
+
+
+#define ZONE "kratochvil.vellum.cz"
+
+#define SETUID 0 /* user  UID */
+#define SETGID 0 /* group GID */
+#define PERMITTED_ENV_LIST "PWD"
+#define EXEC_PATHNAME "/usr/sbin/ndc"
+#define EXEC_ARGV     "/usr/sbin/ndc","reload",ZONE,NULL
+
+
+#ifndef G_GNUC_NORETURN
+#if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+#define G_GNUC_NORETURN __attribute__((noreturn))
+#else   /* !__GNUC__ */
+#define G_GNUC_NORETURN
+#endif  /* !__GNUC__ */
+#endif  /* !G_GNUC_NORETURN */
+
+#define EXITLOG(msg...) do {\
+       openlog("pserverchroot",LOG_PID,LOG_DAEMON); \
+       syslog(LOG_CRIT,msg); \
+       closelog(); \
+       exit(EXIT_FAILURE); \
+       } while (0)
+
+#define LENGTH(x) (sizeof((x))/sizeof(*(x)))
+
+#define FUNCCHK(funcname,args...) do { \
+       if (errno=0,funcname(args)) \
+               funcfail( #funcname ); \
+       } while (0)
+
+static void funcfail(const char *funcname) G_GNUC_NORETURN;
+static void funcfail(const char *funcname)
+{
+       EXITLOG("Unable to %s(2): %s",funcname,strerror(errno));
+}
+
+int main(int argc,char **argv)
+{
+int total=0;
+const char *allowed[]={ PERMITTED_ENV_LIST };
+const char **allp,**envp;
+char *dup=NULL,*s;
+
+retry:
+       for (envp=environ;*envp;envp++) {
+               if (dup) free(dup);
+               dup=strdup(*envp);
+               if ((s=strchr(dup,'='))) *s='\0';
+               for (allp=allowed;allp<allowed+LENGTH(allowed);allp++)
+                       if (!strcmp(dup,*allp)) goto ok;
+               if (total++>1000)
+                       EXITLOG("Unable to clean environment for 'ndc': %s",*envp);
+               unsetenv(dup);
+               goto retry;
+ok:;
+               }
+       FUNCCHK(setenv,"PATH","/usr/bin",1);
+       FUNCCHK(chdir,"/");
+       FUNCCHK(setgid,SETGID);
+       FUNCCHK(setuid,SETUID);
+       errno=0,execl(EXEC_PATHNAME,EXEC_ARGV);
+       funcfail("execl");
+}