5ca56737df8eed573a7bc3de36425422aedfff44
[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 #ifdef HAVE_PUTENV
30 static void unputenv(const char *p) {
31         const char *e = strchr(p, '=');
32         if(!e)
33                 return;
34         int len = e - p;
35 #ifndef HAVE_UNSETENV
36 #ifdef HAVE_MINGW
37         // Windows requires putenv("FOO=") to unset %FOO%
38         len++;
39 #endif
40 #endif
41         char var[len + 1];
42         strncpy(var, p, len);
43         var[len] = 0;
44 #ifdef HAVE_UNSETENV
45         unsetenv(var);
46 #else
47         // We must keep what we putenv() around in memory.
48         // To do this without memory leaks, keep things in a list and reuse if possible.
49         static list_t list = {};
50         for list_each(char, data, &list) {
51                 if(!strcmp(data, var)) {
52                         putenv(data);
53                         return;
54                 }
55         }
56         char *data = xstrdup(var);
57         list_insert_tail(&list, data);
58         putenv(data);
59 #endif
60 }
61 #else
62 static void putenv(const char *p) {}
63 static void unputenv(const char *p) {}
64 #endif
65
66 bool execute_script(const char *name, char **envp) {
67 #ifdef HAVE_SYSTEM
68         char scriptname[PATH_MAX];
69         char *command;
70
71         snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
72
73         /* First check if there is a script */
74
75 #ifdef HAVE_MINGW
76         if(!*scriptextension) {
77                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
78                 char fullname[strlen(scriptname) + strlen(pathext)];
79                 char *ext = fullname + strlen(scriptname);
80                 strcpy(fullname, scriptname);
81
82                 const char *p = pathext;
83                 bool found = false;
84                 while(p && *p) {
85                         const char *q = strchr(p, ';');
86                         if(q) {
87                                 memcpy(ext, p, q - p);
88                                 ext[q - p] = 0;
89                                 q++;
90                         } else {
91                                 strcpy(ext, p);
92                         }
93                         if((found = !access(fullname, F_OK)))
94                                 break;
95                         p = q;
96                 }
97                 if(!found)
98                         return true;
99         } else
100 #endif
101
102         if(access(scriptname, F_OK))
103                 return true;
104
105         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
106
107         /* Set environment */
108
109         for(int i = 0; envp[i]; i++)
110                 putenv(envp[i]);
111
112         if(scriptinterpreter)
113                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
114         else
115                 xasprintf(&command, "\"%s\"", scriptname);
116
117         int status = system(command);
118
119         free(command);
120
121         /* Unset environment */
122
123         for(int i = 0; envp[i]; i++)
124                 unputenv(envp[i]);
125
126         if(status != -1) {
127 #ifdef WEXITSTATUS
128                 if(WIFEXITED(status)) {          /* Child exited by itself */
129                         if(WEXITSTATUS(status)) {
130                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
131                                            name, WEXITSTATUS(status));
132                                 return false;
133                         }
134                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
135                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
136                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
137                         return false;
138                 } else {                         /* Something strange happened */
139                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
140                         return false;
141                 }
142 #endif
143         } else {
144                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
145                 return false;
146         }
147 #endif
148         return true;
149 }