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