Wipe (some) secrets from memory after use
[tinc] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2022 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         ptrdiff_t len = e - p;
39 #ifndef HAVE_UNSETENV
40 #ifdef HAVE_WINDOWS
41         // Windows requires putenv("FOO=") to unset %FOO%
42         len++;
43 #endif
44 #endif
45         char *var = alloca(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 = {0};
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
84                 if(vasprintf(&env->entries[env->n], format, ap) == -1) {
85                         // Assume we are out of memory.
86                         abort();
87                 }
88
89                 va_end(ap);
90         } else {
91                 env->entries[env->n] = NULL;
92         }
93
94         return env->n++;
95 }
96
97 void environment_update(environment_t *env, int pos, const char *format, ...) {
98         free(env->entries[pos]);
99         va_list ap;
100         va_start(ap, format);
101
102         if(vasprintf(&env->entries[pos], format, ap) == -1) {
103                 abort();
104         }
105
106         va_end(ap);
107 }
108
109 void environment_init(environment_t *env) {
110         env->n = 0;
111         env->size = min_env_size;
112         env->entries = xzalloc(env->size * sizeof(*env->entries));
113
114         if(netname) {
115                 environment_add(env, "NETNAME=%s", netname);
116         }
117
118         if(myname) {
119                 environment_add(env, "NAME=%s", myname);
120         }
121
122         if(device) {
123                 environment_add(env, "DEVICE=%s", device);
124         }
125
126         if(iface) {
127                 environment_add(env, "INTERFACE=%s", iface);
128         }
129
130         if(debug_level >= 0) {
131                 environment_add(env, "DEBUG=%d", debug_level);
132         }
133 }
134
135 void environment_exit(environment_t *env) {
136         for(int i = 0; i < env->n; i++) {
137                 free_string(env->entries[i]);
138         }
139
140         free(env->entries);
141 }
142
143 bool execute_script(const char *name, environment_t *env) {
144         char scriptname[PATH_MAX];
145         char *command;
146
147         snprintf(scriptname, sizeof(scriptname), "%s" SLASH "%s%s", confbase, name, scriptextension);
148
149         /* First check if there is a script */
150
151 #ifdef HAVE_WINDOWS
152
153         if(!*scriptextension) {
154                 const char *pathext = getenv("PATHEXT");
155
156                 if(!pathext) {
157                         pathext = ".COM;.EXE;.BAT;.CMD";
158                 }
159
160                 size_t pathlen = strlen(pathext);
161                 size_t scriptlen = strlen(scriptname);
162
163                 const size_t fullnamelen = scriptlen + pathlen + 1;
164                 char *fullname = alloca(fullnamelen);
165                 char *ext = fullname + scriptlen;
166                 strncpy(fullname, scriptname, fullnamelen);
167
168                 const char *p = pathext;
169                 bool found = false;
170
171                 while(p && *p) {
172                         const char *q = strchr(p, ';');
173
174                         if(q) {
175                                 memcpy(ext, p, q - p);
176                                 ext[q - p] = 0;
177                                 q++;
178                         } else {
179                                 strncpy(ext, p, pathlen + 1);
180                         }
181
182                         if((found = !access(fullname, F_OK))) {
183                                 break;
184                         }
185
186                         p = q;
187                 }
188
189                 if(!found) {
190                         return true;
191                 }
192         } else
193 #endif
194
195                 if(access(scriptname, F_OK)) {
196                         return true;
197                 }
198
199         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
200
201         /* Set environment */
202
203         for(int i = 0; i < env->n; i++) {
204                 putenv(env->entries[i]);
205         }
206
207         if(scriptinterpreter) {
208                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
209         } else {
210                 xasprintf(&command, "\"%s\"", scriptname);
211         }
212
213         int status = system(command);
214
215         free(command);
216
217         /* Unset environment */
218
219         for(int i = 0; i < env->n; i++) {
220                 unputenv(env->entries[i]);
221         }
222
223         if(status != -1) {
224 #ifdef WEXITSTATUS
225
226                 if(WIFEXITED(status)) {          /* Child exited by itself */
227                         if(WEXITSTATUS(status)) {
228                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
229                                        name, WEXITSTATUS(status));
230                                 return false;
231                         }
232                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
233                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
234                                name, WTERMSIG(status), strsignal(WTERMSIG(status)));
235                         return false;
236                 } else {                         /* Something strange happened */
237                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
238                         return false;
239                 }
240
241 #endif
242         } else {
243                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
244                 return false;
245         }
246
247         return true;
248 }