Add the ListenAddress option.
[tinc] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 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 "logger.h"
25 #include "names.h"
26 #include "script.h"
27 #include "xalloc.h"
28
29 bool execute_script(const char *name, char **envp) {
30 #ifdef HAVE_SYSTEM
31         char *scriptname;
32         char *command;
33
34         xasprintf(&scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
35
36         /* First check if there is a script */
37
38 #ifdef HAVE_MINGW
39         if(!*scriptextension) {
40                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
41                 char fullname[strlen(scriptname) + strlen(pathext)];
42                 char *ext = fullname + strlen(scriptname);
43                 strcpy(fullname, scriptname);
44
45                 const char *p = pathext;
46                 bool found = false;
47                 while(p && *p) {
48                         const char *q = strchr(p, ';');
49                         if(q) {
50                                 memcpy(ext, p, q - p);
51                                 ext[q - p] = 0;
52                                 *q++;
53                         } else {
54                                 strcpy(ext, p);
55                         }
56                         if((found = !access(fullname, F_OK)))
57                                 break;
58                         p = q;
59                 }
60                 if(!found) {
61                         free(scriptname);
62                         return true;
63                 }
64         } else
65 #endif
66
67         if(access(scriptname, F_OK)) {
68                 free(scriptname);
69                 return true;
70         }
71
72         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
73
74 #ifdef HAVE_PUTENV
75         /* Set environment */
76
77         for(int i = 0; envp[i]; i++)
78                 putenv(envp[i]);
79 #endif
80
81         if(scriptinterpreter)
82                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
83         else
84                 xasprintf(&command, "\"%s\"", scriptname);
85
86         int status = system(command);
87
88         free(command);
89         free(scriptname);
90
91         /* Unset environment */
92
93         for(int i = 0; envp[i]; i++) {
94                 char *e = strchr(envp[i], '=');
95                 if(e) {
96                         char p[e - envp[i] + 1];
97                         strncpy(p, envp[i], e - envp[i]);
98                         p[e - envp[i]] = '\0';
99                         putenv(p);
100                 }
101         }
102
103         if(status != -1) {
104 #ifdef WEXITSTATUS
105                 if(WIFEXITED(status)) {          /* Child exited by itself */
106                         if(WEXITSTATUS(status)) {
107                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
108                                            name, WEXITSTATUS(status));
109                                 return false;
110                         }
111                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
112                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
113                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
114                         return false;
115                 } else {                         /* Something strange happened */
116                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
117                         return false;
118                 }
119 #endif
120         } else {
121                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
122                 return false;
123         }
124 #endif
125         return true;
126 }