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