2 script.c -- call an external script
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
32 static void unputenv(const char *p) {
33 const char *e = strchr(p, '=');
39 ptrdiff_t len = e - p;
42 // Windows requires putenv("FOO=") to unset %FOO%
46 char *var = alloca(len + 1);
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};
56 for list_each(char, data, &list) {
57 if(!strcmp(data, var)) {
63 char *data = xstrdup(var);
64 list_insert_tail(&list, data);
69 static void putenv(const char *p) {}
70 static void unputenv(const char *p) {}
73 static const int min_env_size = 10;
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));
85 if(vasprintf(&env->entries[env->n], format, ap) == -1) {
86 // Assume we are out of memory.
92 env->entries[env->n] = NULL;
98 void environment_update(environment_t *env, int pos, const char *format, ...) {
99 free(env->entries[pos]);
101 va_start(ap, format);
103 if(vasprintf(&env->entries[pos], format, ap) == -1) {
110 void environment_init(environment_t *env) {
112 env->size = min_env_size;
113 env->entries = xzalloc(env->size * sizeof(*env->entries));
116 environment_add(env, "NETNAME=%s", netname);
120 environment_add(env, "NAME=%s", myname);
124 environment_add(env, "DEVICE=%s", device);
128 environment_add(env, "INTERFACE=%s", iface);
131 if(debug_level >= 0) {
132 environment_add(env, "DEBUG=%d", debug_level);
136 void environment_exit(environment_t *env) {
137 for(int i = 0; i < env->n; i++) {
138 free_string(env->entries[i]);
144 bool execute_script(const char *name, environment_t *env) {
145 if(!sandbox_can(START_PROCESSES, RIGHT_NOW)) {
149 char scriptname[PATH_MAX];
152 snprintf(scriptname, sizeof(scriptname), "%s" SLASH "%s%s", confbase, name, scriptextension);
154 /* First check if there is a script */
158 if(!*scriptextension) {
159 const char *pathext = getenv("PATHEXT");
162 pathext = ".COM;.EXE;.BAT;.CMD";
165 size_t pathlen = strlen(pathext);
166 size_t scriptlen = strlen(scriptname);
168 const size_t fullnamelen = scriptlen + pathlen + 1;
169 char *fullname = alloca(fullnamelen);
170 char *ext = fullname + scriptlen;
171 strncpy(fullname, scriptname, fullnamelen);
173 const char *p = pathext;
177 const char *q = strchr(p, ';');
180 memcpy(ext, p, q - p);
184 strncpy(ext, p, pathlen + 1);
187 if((found = !access(fullname, F_OK))) {
200 if(access(scriptname, F_OK)) {
204 logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
206 /* Set environment */
208 for(int i = 0; i < env->n; i++) {
209 putenv(env->entries[i]);
212 if(scriptinterpreter) {
213 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
215 xasprintf(&command, "\"%s\"", scriptname);
218 int status = system(command);
222 /* Unset environment */
224 for(int i = 0; i < env->n; i++) {
225 unputenv(env->entries[i]);
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));
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)));
241 } else { /* Something strange happened */
242 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
248 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));