/
runscript.cc
348 lines (289 loc) · 9.59 KB
/
runscript.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Manipulation routines for RunScript list
*
* Eric Bollengier, May 2006
*/
#include "include/bareos.h"
#include "include/jcr.h"
#include "runscript.h"
#include "lib/util.h"
/*
* This function pointer is set only by the Director (dird.c),
* and is not set in the File daemon, because the File
* daemon cannot run console commands.
*/
bool (*console_command)(JobControlRecord *jcr, const char *cmd) = NULL;
RunScript *NewRunscript()
{
Dmsg0(500, "runscript: creating new RunScript object\n");
RunScript *cmd = (RunScript *)malloc(sizeof(RunScript));
memset(cmd, 0, sizeof(RunScript));
cmd->ResetDefault();
return cmd;
}
void RunScript::ResetDefault(bool free_strings)
{
if (free_strings && command) {
FreePoolMemory(command);
}
if (free_strings && target) {
FreePoolMemory(target);
}
target = NULL;
command = NULL;
on_success = true;
on_failure = false;
fail_on_error = true;
when = SCRIPT_Never;
job_code_callback = NULL;
}
RunScript *copy_runscript(RunScript *src)
{
Dmsg0(500, "runscript: creating new RunScript object from other\n");
RunScript *dst = (RunScript *)malloc(sizeof(RunScript));
memcpy(dst, src, sizeof(RunScript));
dst->command = NULL;
dst->target = NULL;
dst->SetCommand(src->command, src->cmd_type);
dst->SetTarget(src->target);
return dst;
}
void FreeRunscript(RunScript *script)
{
Dmsg0(500, "runscript: freeing RunScript object\n");
if (script->command) {
FreePoolMemory(script->command);
}
if (script->target) {
FreePoolMemory(script->target);
}
free(script);
}
static inline bool ScriptDirAllowed(JobControlRecord *jcr, RunScript *script, alist *allowed_script_dirs)
{
char *bp, *allowed_script_dir;
bool allowed = false;
PoolMem script_dir(PM_FNAME);
/*
* If there is no explicit list of allowed dirs allow any dir.
*/
if (!allowed_script_dirs) {
return true;
}
/*
* Determine the dir the script is in.
*/
PmStrcpy(script_dir, script->command);
if ((bp = strrchr(script_dir.c_str(), '/'))) {
*bp = '\0';
}
/*
* Make sure there are no relative path elements in script dir by which the
* user tries to escape the allowed dir checking. For scripts we only allow
* absolute paths.
*/
if (strstr(script_dir.c_str(), "..")) {
Dmsg1(200, "ScriptDirAllowed: relative pathnames not allowed: %s\n", script_dir.c_str());
return false;
}
/*
* Match the path the script is in against the list of allowed script directories.
*/
foreach_alist(allowed_script_dir, allowed_script_dirs) {
if (Bstrcasecmp(script_dir.c_str(), allowed_script_dir)) {
allowed = true;
break;
}
}
Dmsg2(200, "ScriptDirAllowed: script %s %s allowed by Allowed Script Dir setting",
script->command, (allowed) ? "" : "NOT");
return allowed;
}
int RunScripts(JobControlRecord *jcr, alist *runscripts, const char *label, alist *allowed_script_dirs)
{
RunScript *script;
bool runit;
int when;
Dmsg2(200, "runscript: running all RunScript object (%s) JobStatus=%c\n", label, jcr->JobStatus);
if (strstr(label, NT_("Before"))) {
when = SCRIPT_Before;
} else if (bstrcmp(label, NT_("ClientAfterVSS"))) {
when = SCRIPT_AfterVSS;
} else {
when = SCRIPT_After;
}
if (runscripts == NULL) {
Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
return 0;
}
foreach_alist(script, runscripts) {
Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
runit = false;
if ((script->when & SCRIPT_Before) && (when & SCRIPT_Before)) {
if ((script->on_success && (jcr->JobStatus == JS_Running || jcr->JobStatus == JS_Created)) ||
(script->on_failure && (JobCanceled(jcr) || jcr->JobStatus == JS_Differences))) {
Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n",
script->command, script->on_success, script->on_failure, jcr->JobStatus );
runit = true;
}
}
if ((script->when & SCRIPT_AfterVSS) && (when & SCRIPT_AfterVSS)) {
if ((script->on_success && (jcr->JobStatus == JS_Blocked)) ||
(script->on_failure && JobCanceled(jcr))) {
Dmsg4(200, "runscript: Run it because SCRIPT_AfterVSS (%s,%i,%i,%c)\n",
script->command, script->on_success, script->on_failure, jcr->JobStatus );
runit = true;
}
}
if ((script->when & SCRIPT_After) && (when & SCRIPT_After)) {
if ((script->on_success && jcr->IsTerminatedOk()) ||
(script->on_failure && (JobCanceled(jcr) || jcr->JobStatus == JS_Differences))) {
Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n",
script->command, script->on_success, script->on_failure, jcr->JobStatus );
runit = true;
}
}
if (!script->IsLocal()) {
runit = false;
}
/*
* We execute it
*/
if (runit) {
if (!ScriptDirAllowed(jcr, script, allowed_script_dirs)) {
Dmsg1(200, "runscript: Not running script %s because its not in one of the allowed scripts dirs\n",
script->command);
Jmsg(jcr, M_ERROR, 0, _("Runscript: run %s \"%s\" could not execute, "
"not in one of the allowed scripts dirs\n"), label, script->command);
jcr->setJobStatus(JS_ErrorTerminated);
goto bail_out;
}
script->run(jcr, label);
}
}
bail_out:
return 1;
}
bool RunScript::IsLocal()
{
if (!target || bstrcmp(target, "")) {
return true;
} else {
return false;
}
}
/* set this->command to cmd */
void RunScript::SetCommand(const char *cmd, int acmd_type)
{
Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
if (!cmd) {
return;
}
if (!command) {
command = GetPoolMemory(PM_FNAME);
}
PmStrcpy(command, cmd);
cmd_type = acmd_type;
}
/* set this->target to client_name */
void RunScript::SetTarget(const char *client_name)
{
Dmsg1(500, "runscript: setting target = %s\n", NPRT(client_name));
if (!client_name) {
return;
}
if (!target) {
target = GetPoolMemory(PM_FNAME);
}
PmStrcpy(target, client_name);
}
bool RunScript::run(JobControlRecord *jcr, const char *name)
{
Dmsg1(100, "runscript: running a RunScript object type=%d\n", cmd_type);
POOLMEM *ecmd = GetPoolMemory(PM_FNAME);
int status;
Bpipe *bpipe;
PoolMem line(PM_NAME);
ecmd = edit_job_codes(jcr, ecmd, this->command, "", this->job_code_callback);
Dmsg1(100, "runscript: running '%s'...\n", ecmd);
Jmsg(jcr, M_INFO, 0, _("%s: run %s \"%s\"\n"),
cmd_type==SHELL_CMD?"shell command":"console command", name, ecmd);
switch (cmd_type) {
case SHELL_CMD:
bpipe = OpenBpipe(ecmd, 0, "r");
FreePoolMemory(ecmd);
if (bpipe == NULL) {
BErrNo be;
Jmsg(jcr, M_ERROR, 0, _("Runscript: %s could not execute. ERR=%s\n"), name,
be.bstrerror());
goto bail_out;
}
while (fgets(line.c_str(), line.size(), bpipe->rfd)) {
StripTrailingJunk(line.c_str());
Jmsg(jcr, M_INFO, 0, _("%s: %s\n"), name, line.c_str());
}
status = CloseBpipe(bpipe);
if (status != 0) {
BErrNo be;
Jmsg(jcr, M_ERROR, 0, _("Runscript: %s returned non-zero status=%d. ERR=%s\n"), name,
be.code(status), be.bstrerror(status));
goto bail_out;
}
Dmsg0(100, "runscript OK\n");
break;
case CONSOLE_CMD:
if (console_command) { /* can we run console command? */
if (!console_command(jcr, ecmd)) { /* yes, do so */
goto bail_out;
}
}
break;
}
return true;
bail_out:
/* cancel running job properly */
if (fail_on_error) {
jcr->setJobStatus(JS_ErrorTerminated);
}
Dmsg1(100, "runscript failed. fail_on_error=%d\n", fail_on_error);
return false;
}
void FreeRunscripts(alist *runscripts)
{
Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n");
RunScript *elt;
foreach_alist(elt, runscripts) {
FreeRunscript(elt);
}
}
void RunScript::debug()
{
Dmsg0(200, "runscript: debug\n");
Dmsg0(200, _(" --> RunScript\n"));
Dmsg1(200, _(" --> Command=%s\n"), NPRT(command));
Dmsg1(200, _(" --> Target=%s\n"), NPRT(target));
Dmsg1(200, _(" --> RunOnSuccess=%u\n"), on_success);
Dmsg1(200, _(" --> RunOnFailure=%u\n"), on_failure);
Dmsg1(200, _(" --> FailJobOnError=%u\n"), fail_on_error);
Dmsg1(200, _(" --> RunWhen=%u\n"), when);
}
void RunScript::SetJobCodeCallback(job_code_callback_t arg_job_code_callback)
{
this->job_code_callback = arg_job_code_callback;
}