+macro editor ALT-w: wrap text (subject line) to " [WAS: &]"
[nethome.git] / src / ndc-reload-short.c
1 #include <stdlib.h>
2 #include <syslog.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 extern const char **environ;
8
9
10 #define ZONE "kratochvil.vellum.cz"
11
12 #define SETUID 0 /* user  UID */
13 #define SETGID 0 /* group GID */
14 #define PERMITTED_ENV_LIST "PWD"
15 #define EXEC_PATHNAME "/usr/sbin/ndc"
16 #define EXEC_ARGV     "/usr/sbin/ndc","reload",ZONE,NULL
17
18
19 #ifndef G_GNUC_NORETURN
20 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
21 #define G_GNUC_NORETURN __attribute__((noreturn))
22 #else   /* !__GNUC__ */
23 #define G_GNUC_NORETURN
24 #endif  /* !__GNUC__ */
25 #endif  /* !G_GNUC_NORETURN */
26
27 #define EXITLOG(msg...) do {\
28         openlog("pserverchroot",LOG_PID,LOG_DAEMON); \
29         syslog(LOG_CRIT,msg); \
30         closelog(); \
31         exit(EXIT_FAILURE); \
32         } while (0)
33
34 #define LENGTH(x) (sizeof((x))/sizeof(*(x)))
35
36 #define FUNCCHK(funcname,args...) do { \
37         if (errno=0,funcname(args)) \
38                 funcfail( #funcname ); \
39         } while (0)
40
41 static void funcfail(const char *funcname) G_GNUC_NORETURN;
42 static void funcfail(const char *funcname)
43 {
44         EXITLOG("Unable to %s(2): %s",funcname,strerror(errno));
45 }
46
47 int main(int argc,char **argv)
48 {
49 int total=0;
50 const char *allowed[]={ PERMITTED_ENV_LIST };
51 const char **allp,**envp;
52 char *dup=NULL,*s;
53
54 retry:
55         for (envp=environ;*envp;envp++) {
56                 if (dup) free(dup);
57                 dup=strdup(*envp);
58                 if ((s=strchr(dup,'='))) *s='\0';
59                 for (allp=allowed;allp<allowed+LENGTH(allowed);allp++)
60                         if (!strcmp(dup,*allp)) goto ok;
61                 if (total++>1000)
62                         EXITLOG("Unable to clean environment for 'ndc': %s",*envp);
63                 unsetenv(dup);
64                 goto retry;
65 ok:;
66                 }
67         FUNCCHK(setenv,"PATH","/usr/bin",1);
68         FUNCCHK(chdir,"/");
69         FUNCCHK(setgid,SETGID);
70         FUNCCHK(setuid,SETUID);
71         errno=0,execl(EXEC_PATHNAME,EXEC_ARGV);
72         funcfail("execl");
73 }