81216a3ab043585b04733308442f4a7809a13440
[tinc] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2017 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 "device.h"
25 #include "logger.h"
26 #include "names.h"
27 #include "script.h"
28 #include "xalloc.h"
29
30 #ifdef HAVE_PUTENV
31 static void unputenv(const char *p) {
32         const char *e = strchr(p, '=');
33
34         if(!e) {
35                 return;
36         }
37
38         int len = e - p;
39 #ifndef HAVE_UNSETENV
40 #ifdef HAVE_MINGW
41         // Windows requires putenv("FOO=") to unset %FOO%
42         len++;
43 #endif
44 #endif
45         char var[len + 1];
46         strncpy(var, p, len);
47         var[len] = 0;
48 #ifdef HAVE_UNSETENV
49         unsetenv(var);
50 #else
51         // We must keep what we putenv() around in memory.
52         // To do this without memory leaks, keep things in a list and reuse if possible.
53         static list_t list = {};
54
55         for list_each(char, data, &list) {
56                 if(!strcmp(data, var)) {
57                         putenv(data);
58                         return;
59                 }
60         }
61
62         char *data = xstrdup(var);
63         list_insert_tail(&list, data);
64         putenv(data);
65 #endif
66 }
67 #else
68 static void putenv(const char *p) {}
69 static void unputenv(const char *p) {}
70 #endif
71
72 static const int min_env_size = 10;
73
74 int environment_add(environment_t *env, const char *format, ...) {
75         if(env->n >= env->size) {
76                 env->size = env->n ? env->n * 2 : min_env_size;
77                 env->entries = xrealloc(env->entries, env->size * sizeof(*env->entries));
78         }
79
80         if(format) {
81                 va_list ap;
82                 va_start(ap, format);
83                 vasprintf(&env->entries[env->n], format, ap);
84                 va_end(ap);
85         } else {
86                 env->entries[env->n] = NULL;
87         }
88
89         return env->n++;
90 }
91
92 void environment_update(environment_t *env, int pos, const char *format, ...) {
93         free(env->entries[pos]);
94         va_list ap;
95         va_start(ap, format);
96         vasprintf(&env->entries[pos], format, ap);
97         va_end(ap);
98 }
99
100 void environment_init(environment_t *env) {
101         env->n = 0;
102         env->size = min_env_size;
103         env->entries = xzalloc(env->size * sizeof(*env->entries));
104
105         if(netname) {
106                 environment_add(env, "NETNAME=%s", netname);
107         }
108
109         if(myname) {
110                 environment_add(env, "NAME=%s", myname);
111         }
112
113         if(device) {
114                 environment_add(env, "DEVICE=%s", device);
115         }
116
117         if(iface) {
118                 environment_add(env, "INTERFACE=%s", iface);
119         }
120
121         if(debug_level >= 0) {
122                 environment_add(env, "DEBUG=%d", debug_level);
123         }
124 }
125
126 void environment_exit(environment_t *env) {
127         for(int i = 0; i < env->n; i++) {
128                 free(env->entries[i]);
129         }
130
131         free(env->entries);
132 }
133
134 bool execute_script(const char *name, environment_t *env) {
135         char scriptname[PATH_MAX];
136         char *command;
137
138         snprintf(scriptname, sizeof(scriptname), "%s" SLASH "%s%s", confbase, name, scriptextension);
139
140         /* First check if there is a script */
141
142 #ifdef HAVE_MINGW
143
144         if(!*scriptextension) {
145                 const char *pathext = getenv("PATHEXT") ? : ".COM;.EXE;.BAT;.CMD";
146                 size_t pathlen = strlen(pathext);
147                 size_t scriptlen = strlen(scriptname);
148                 char fullname[scriptlen + pathlen + 1];
149                 char *ext = fullname + scriptlen;
150                 strncpy(fullname, scriptname, sizeof(fullname));
151
152                 const char *p = pathext;
153                 bool found = false;
154
155                 while(p && *p) {
156                         const char *q = strchr(p, ';');
157
158                         if(q) {
159                                 memcpy(ext, p, q - p);
160                                 ext[q - p] = 0;
161                                 q++;
162                         } else {
163                                 strncpy(ext, p, pathlen + 1);
164                         }
165
166                         if((found = !access(fullname, F_OK))) {
167                                 break;
168                         }
169
170                         p = q;
171                 }
172
173                 if(!found) {
174                         return true;
175                 }
176         } else
177 #endif
178
179                 if(access(scriptname, F_OK)) {
180                         return true;
181                 }
182
183         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
184
185         /* Set environment */
186
187         for(int i = 0; i < env->n; i++) {
188                 putenv(env->entries[i]);
189         }
190
191         if(scriptinterpreter) {
192                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
193         } else {
194                 xasprintf(&command, "\"%s\"", scriptname);
195         }
196
197         int status = system(command);
198
199         free(command);
200
201         /* Unset environment */
202
203         for(int i = 0; i < env->n; i++) {
204                 unputenv(env->entries[i]);
205         }
206
207         if(status != -1) {
208 #ifdef WEXITSTATUS
209
210                 if(WIFEXITED(status)) {          /* Child exited by itself */
211                         if(WEXITSTATUS(status)) {
212                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
213                                        name, WEXITSTATUS(status));
214                                 return false;
215                         }
216                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
217                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
218                                name, WTERMSIG(status), strsignal(WTERMSIG(status)));
219                         return false;
220                 } else {                         /* Something strange happened */
221                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
222                         return false;
223                 }
224
225 #endif
226         } else {
227                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
228                 return false;
229         }
230
231         return true;
232 }