Add DEBUG environment variable for scripts.
[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         if(debug_level >= 0)
109                 environment_add(env, "DEBUG=%d", debug_level);
110 }
111
112 void environment_exit(environment_t *env) {
113         for(int i = 0; i < env->n; i++)
114                 free(env->entries[i]);
115         free(env->entries);
116 }
117
118 bool execute_script(const char *name, environment_t *env) {
119         char scriptname[PATH_MAX];
120         char *command;
121
122         snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
123
124         /* First check if there is a script */
125
126 #ifdef HAVE_MINGW
127         if(!*scriptextension) {
128                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
129                 size_t pathlen = strlen(pathext);
130                 size_t scriptlen = strlen(scriptname);
131                 char fullname[scriptlen + pathlen + 1];
132                 char *ext = fullname + scriptlen;
133                 strncpy(fullname, scriptname, sizeof fullname);
134
135                 const char *p = pathext;
136                 bool found = false;
137                 while(p && *p) {
138                         const char *q = strchr(p, ';');
139                         if(q) {
140                                 memcpy(ext, p, q - p);
141                                 ext[q - p] = 0;
142                                 q++;
143                         } else {
144                                 strncpy(ext, p, pathlen + 1);
145                         }
146                         if((found = !access(fullname, F_OK)))
147                                 break;
148                         p = q;
149                 }
150                 if(!found)
151                         return true;
152         } else
153 #endif
154
155         if(access(scriptname, F_OK))
156                 return true;
157
158         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
159
160         /* Set environment */
161
162         for(int i = 0; i < env->n; i++)
163                 putenv(env->entries[i]);
164
165         if(scriptinterpreter)
166                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
167         else
168                 xasprintf(&command, "\"%s\"", scriptname);
169
170         int status = system(command);
171
172         free(command);
173
174         /* Unset environment */
175
176         for(int i = 0; i < env->n; i++)
177                 unputenv(env->entries[i]);
178
179         if(status != -1) {
180 #ifdef WEXITSTATUS
181                 if(WIFEXITED(status)) {          /* Child exited by itself */
182                         if(WEXITSTATUS(status)) {
183                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
184                                            name, WEXITSTATUS(status));
185                                 return false;
186                         }
187                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
188                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
189                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
190                         return false;
191                 } else {                         /* Something strange happened */
192                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
193                         return false;
194                 }
195 #endif
196         } else {
197                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
198                 return false;
199         }
200
201         return true;
202 }