Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runscript.cc « lib « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57ab1a2f369c09a2f3fe5e82bd2c4f0c836278db (plain)
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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
   Copyright (C) 2019-2022 Bareos GmbH & Co. KG

   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/berrno.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* DuplicateRunscript(RunScript* src)
{
  Dmsg0(500, "runscript: creating new RunScript object from other\n");

  RunScript* copy = new RunScript(*src);

  copy->command.clear();
  copy->SetCommand(src->command, src->cmd_type);
  copy->SetTarget(src->target);

  return copy;
}

void FreeRunscript(RunScript* script)
{
  Dmsg0(500, "runscript: freeing RunScript object\n");
  delete script;
}

static bool ScriptDirAllowed(JobControlRecord*,
                             RunScript* script,
                             alist<const char*>* allowed_script_dirs)
{
  const char* allowed_script_dir = nullptr;
  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.c_str());
  if (char* bp = strrchr(script_dir.c_str(), '/'); bp != nullptr) {
    *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.c_str(), (allowed) ? "" : "NOT");

  return allowed;
}

int RunScripts(JobControlRecord* jcr,
               alist<RunScript*>* runscripts,
               const char* label,
               alist<const char*>* allowed_script_dirs)
{
  RunScript* script = nullptr;
  bool runit;
  int when;

  Dmsg2(200, "runscript: running all RunScript object (%s) JobStatus=%c\n",
        label, jcr->getJobStatus());

  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) {
    Dmsg5(200,
          "runscript: try to run (Target=%s, OnSuccess=%i, OnFailure=%i, "
          "CurrentJobStatus=%c, command=%s)\n",
          NSTDPRNT(script->target), script->on_success, script->on_failure,
          jcr->getJobStatus(), NSTDPRNT(script->command));
    runit = false;

    if (!script->IsLocal()) {
      if (jcr->is_JobType(JT_ADMIN)) {
        Jmsg(jcr, M_WARNING, 0,
             "Invalid runscript definition (command=%s). Admin Jobs only "
             "support local runscripts.\n",
             script->command.c_str());
      }
    } else {
      if ((script->when & SCRIPT_Before) && (when & SCRIPT_Before)) {
        if ((script->on_success
             && (jcr->getJobStatus() == JS_Running
                 || jcr->getJobStatus() == JS_Created))
            || (script->on_failure
                && (JobCanceled(jcr)
                    || jcr->getJobStatus() == JS_Differences))) {
          Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n",
                script->command.c_str(), script->on_success, script->on_failure,
                jcr->getJobStatus());
          runit = true;
        }
      }

      if ((script->when & SCRIPT_AfterVSS) && (when & SCRIPT_AfterVSS)) {
        if ((script->on_success && (jcr->getJobStatus() == JS_Blocked))
            || (script->on_failure && JobCanceled(jcr))) {
          Dmsg4(200,
                "runscript: Run it because SCRIPT_AfterVSS (%s,%i,%i,%c)\n",
                script->command.c_str(), script->on_success, script->on_failure,
                jcr->getJobStatus());
          runit = true;
        }
      }

      if ((script->when & SCRIPT_After) && (when & SCRIPT_After)) {
        if ((script->on_success && jcr->IsTerminatedOk())
            || (script->on_failure
                && (JobCanceled(jcr)
                    || jcr->getJobStatus() == JS_Differences))) {
          Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n",
                script->command.c_str(), script->on_success, script->on_failure,
                jcr->getJobStatus());
          runit = true;
        }
      }
    }

    // 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.c_str());
        Jmsg(jcr, M_ERROR, 0,
             _("Runscript: run %s \"%s\" could not execute, "
               "not in one of the allowed scripts dirs\n"),
             label, script->command.c_str());
        jcr->setJobStatusWithPriorityCheck(JS_ErrorTerminated);
        goto bail_out;
      }

      script->Run(jcr, label);
    }
  }

bail_out:
  return 1;
}

void RunScript::SetCommand(const std::string& cmd, int acmd_type)
{
  Dmsg1(500, "runscript: setting command = %s\n", NSTDPRNT(cmd));

  if (cmd.empty()) { return; }

  command = cmd;
  cmd_type = acmd_type;
}

void RunScript::SetTarget(const std::string& client_name)
{
  Dmsg1(500, "runscript: setting target = %s\n", NSTDPRNT(client_name));

  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, command.c_str(), "", 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->setJobStatusWithPriorityCheck(JS_ErrorTerminated); }
  Dmsg1(100, "runscript failed. fail_on_error=%d\n", fail_on_error);
  return false;
}

void FreeRunscripts(alist<RunScript*>* runscripts)
{
  Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n");

  RunScript* r = nullptr;
  foreach_alist (r, runscripts) {
    FreeRunscript(r);
  }
}

void RunScript::Debug() const
{
  Dmsg0(200, "runscript: debug\n");
  Dmsg0(200, _(" --> RunScript\n"));
  Dmsg1(200, _("  --> Command=%s\n"), NSTDPRNT(command));
  Dmsg1(200, _("  --> Target=%s\n"), NSTDPRNT(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;
}