Remove use of strcpy() and sprintf().
[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 #ifdef HAVE_SYSTEM
68         char scriptname[PATH_MAX];
69         char *command;
70
71         snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
72
73         /* First check if there is a script */
74
75 #ifdef HAVE_MINGW
76         if(!*scriptextension) {
77                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
78                 size_t pathlen = strlen(pathext);
79                 size_t scriptlen = strlen(scriptname);
80                 char fullname[scriptlen + pathlen + 1];
81                 char *ext = fullname + scriptlen;
82                 strncpy(fullname, scriptname, sizeof fullname);
83
84                 const char *p = pathext;
85                 bool found = false;
86                 while(p && *p) {
87                         const char *q = strchr(p, ';');
88                         if(q) {
89                                 memcpy(ext, p, q - p);
90                                 ext[q - p] = 0;
91                                 q++;
92                         } else {
93                                 strncpy(ext, p, pathlen + 1);
94                         }
95                         if((found = !access(fullname, F_OK)))
96                                 break;
97                         p = q;
98                 }
99                 if(!found)
100                         return true;
101         } else
102 #endif
103
104         if(access(scriptname, F_OK))
105                 return true;
106
107         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
108
109         /* Set environment */
110
111         for(int i = 0; envp[i]; i++)
112                 putenv(envp[i]);
113
114         if(scriptinterpreter)
115                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
116         else
117                 xasprintf(&command, "\"%s\"", scriptname);
118
119         int status = system(command);
120
121         free(command);
122
123         /* Unset environment */
124
125         for(int i = 0; envp[i]; i++)
126                 unputenv(envp[i]);
127
128         if(status != -1) {
129 #ifdef WEXITSTATUS
130                 if(WIFEXITED(status)) {          /* Child exited by itself */
131                         if(WEXITSTATUS(status)) {
132                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
133                                            name, WEXITSTATUS(status));
134                                 return false;
135                         }
136                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
137                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
138                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
139                         return false;
140                 } else {                         /* Something strange happened */
141                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
142                         return false;
143                 }
144 #endif
145         } else {
146                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
147                 return false;
148         }
149 #endif
150         return true;
151 }