55473b1f95e6ea595fe1664740aceefee8684e6e
[tinc] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2015 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25 #include "names.h"
26 #include "script.h"
27 #include "xalloc.h"
28
29 bool execute_script(const char *name, char **envp) {
30 #ifdef HAVE_SYSTEM
31         char scriptname[PATH_MAX];
32         char *command;
33
34         snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
35
36         /* First check if there is a script */
37
38 #ifdef HAVE_MINGW
39         if(!*scriptextension) {
40                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
41                 char fullname[strlen(scriptname) + strlen(pathext)];
42                 char *ext = fullname + strlen(scriptname);
43                 strcpy(fullname, scriptname);
44
45                 const char *p = pathext;
46                 bool found = false;
47                 while(p && *p) {
48                         const char *q = strchr(p, ';');
49                         if(q) {
50                                 memcpy(ext, p, q - p);
51                                 ext[q - p] = 0;
52                                 q++;
53                         } else {
54                                 strcpy(ext, p);
55                         }
56                         if((found = !access(fullname, F_OK)))
57                                 break;
58                         p = q;
59                 }
60                 if(!found)
61                         return true;
62         } else
63 #endif
64
65         if(access(scriptname, F_OK))
66                 return true;
67
68         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
69
70 #ifdef HAVE_PUTENV
71         /* Set environment */
72
73         for(int i = 0; envp[i]; i++)
74                 putenv(envp[i]);
75 #endif
76
77         if(scriptinterpreter)
78                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
79         else
80                 xasprintf(&command, "\"%s\"", scriptname);
81
82         int status = system(command);
83
84         free(command);
85
86         /* Unset environment */
87
88         for(int i = 0; envp[i]; i++) {
89                 char *e = strchr(envp[i], '=');
90                 if(e) {
91                         char p[e - envp[i] + 1];
92                         strncpy(p, envp[i], e - envp[i]);
93                         p[e - envp[i]] = '\0';
94                         putenv(p);
95                 }
96         }
97
98         if(status != -1) {
99 #ifdef WEXITSTATUS
100                 if(WIFEXITED(status)) {          /* Child exited by itself */
101                         if(WEXITSTATUS(status)) {
102                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
103                                            name, WEXITSTATUS(status));
104                                 return false;
105                         }
106                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
107                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
108                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
109                         return false;
110                 } else {                         /* Something strange happened */
111                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
112                         return false;
113                 }
114 #endif
115         } else {
116                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
117                 return false;
118         }
119 #endif
120         return true;
121 }