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