Fix potential memory leaks found by the Clang static analyzer.
[tinc] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2015 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 #ifdef HAVE_PUTENV
30 static void unputenv(const char *p) {
31         const char *e = strchr(p, '=');
32         if(!e)
33                 return;
34         int len = e - p;
35 #ifndef HAVE_UNSETENV
36 #ifdef HAVE_MINGW
37         // Windows requires putenv("FOO=") to unset %FOO%
38         len++;
39 #endif
40 #endif
41         char var[len + 1];
42         strncpy(var, p, len);
43         var[len] = 0;
44 #ifdef HAVE_UNSETENV
45         unsetenv(var);
46 #else
47         // We must keep what we putenv() around in memory.
48         // To do this without memory leaks, keep things in a list and reuse if possible.
49         static list_t list = {};
50         for list_each(char, data, &list) {
51                 if(!strcmp(data, var)) {
52                         putenv(data);
53                         return;
54                 }
55         }
56         char *data = xstrdup(var);
57         list_insert_tail(&list, data);
58         putenv(data);
59 #endif
60 }
61 #else
62 static void putenv(const char *p) {}
63 static void unputenv(const char *p) {}
64 #endif
65
66 bool execute_script(const char *name, char **envp) {
67         char scriptname[PATH_MAX];
68         char *command;
69
70         snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
71
72         /* First check if there is a script */
73
74 #ifdef HAVE_MINGW
75         if(!*scriptextension) {
76                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
77                 size_t pathlen = strlen(pathext);
78                 size_t scriptlen = strlen(scriptname);
79                 char fullname[scriptlen + pathlen + 1];
80                 char *ext = fullname + scriptlen;
81                 strncpy(fullname, scriptname, sizeof fullname);
82
83                 const char *p = pathext;
84                 bool found = false;
85                 while(p && *p) {
86                         const char *q = strchr(p, ';');
87                         if(q) {
88                                 memcpy(ext, p, q - p);
89                                 ext[q - p] = 0;
90                                 q++;
91                         } else {
92                                 strncpy(ext, p, pathlen + 1);
93                         }
94                         if((found = !access(fullname, F_OK)))
95                                 break;
96                         p = q;
97                 }
98                 if(!found)
99                         return true;
100         } else
101 #endif
102
103         if(access(scriptname, F_OK))
104                 return true;
105
106         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
107
108         /* Set environment */
109
110         for(int i = 0; envp[i]; i++)
111                 putenv(envp[i]);
112
113         if(scriptinterpreter)
114                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
115         else
116                 xasprintf(&command, "\"%s\"", scriptname);
117
118         int status = system(command);
119
120         free(command);
121
122         /* Unset environment */
123
124         for(int i = 0; envp[i]; i++)
125                 unputenv(envp[i]);
126
127         if(status != -1) {
128 #ifdef WEXITSTATUS
129                 if(WIFEXITED(status)) {          /* Child exited by itself */
130                         if(WEXITSTATUS(status)) {
131                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
132                                            name, WEXITSTATUS(status));
133                                 return false;
134                         }
135                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
136                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
137                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
138                         return false;
139                 } else {                         /* Something strange happened */
140                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
141                         return false;
142                 }
143 #endif
144         } else {
145                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
146                 return false;
147         }
148
149         return true;
150 }