1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2006-2008 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2019 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Eric Bollengier May 2006
24  */
25 /**
26  * @file
27  * BAREOS RunScript Structure definition for FileDaemon and Director
28  */
29 
30 #ifndef BAREOS_LIB_RUNSCRIPT_H_
31 #define BAREOS_LIB_RUNSCRIPT_H_ 1
32 
33 #include "jcr.h"
34 #include "lib/bareos_resource.h"
35 
36 class alist;
37 
38 /* Usage:
39  *
40  * #include "lib/runscript.h"
41  *
42  * RunScript *script = new RunScript;
43  * script->SetCommand("/bin/sleep 20");
44  * script->on_failure = true;
45  * script->when = SCRIPT_After;
46  *
47  * script->Run("LabelBefore");  // the label must contain "Before" or "After"
48  * special keyword FreeRunscript(script);
49  */
50 
51 /**
52  * RunScript->when can take following bit values:
53  */
54 enum
55 {
56   SCRIPT_Never = 0,
57   SCRIPT_After = (1 << 0),    /* AfterJob */
58   SCRIPT_Before = (1 << 1),   /* BeforeJob */
59   SCRIPT_AfterVSS = (1 << 2), /* BeforeJob and After VSS */
60   SCRIPT_Any = SCRIPT_Before | SCRIPT_After,
61   SCRIPT_INVALID = 0xff
62 };
63 
64 enum
65 {
66   SHELL_CMD = '|',
67   CONSOLE_CMD = '@'
68 };
69 
70 struct TempParserCommand {
TempParserCommandTempParserCommand71   TempParserCommand(POOLMEM* p, int32_t c) : command_(p), code_(c) {}
72   std::string command_;
73   int32_t code_ = 0;
74 };
75 
76 class RunScript : public BareosResource {
77  public:
78   RunScript() = default;
79   virtual ~RunScript() = default;
80   RunScript(const RunScript& other) = default;
81 
82   std::string command;       /* Command string */
83   std::string target;        /* Host target. Values:
84                                 Empty string: run locally.
85                                 "%c": (Client’s name). Run on client. */
86   int when = SCRIPT_Never;   /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
87   int cmd_type = 0;          /* Command type -- Shell, Console */
88   char level = 0;            /* Base|Full|Incr...|All (NYI) */
89   bool short_form = false;   /* Run Script in short form */
90   bool from_jobdef = false;  /* This RUN script comes from JobDef */
91   bool on_success = true;    /* Execute command on job success (After) */
92   bool on_failure = false;   /* Execute command on job failure (After) */
93   bool fail_on_error = true; /* Abort job on error (Before) */
94   job_code_callback_t job_code_callback = nullptr;
95   std::vector<TempParserCommand> temp_parser_command_container;
96   bool Run(JobControlRecord* job,
97            const char* name
98            = ""); /* name must contain "Before" or "After" keyword */
99   void SetCommand(const std::string& cmd, int cmd_type = SHELL_CMD);
100   void SetTarget(const std::string& client_name);
IsLocal()101   bool IsLocal() const { return target.empty(); } /* true if no target host */
102   void Debug() const;
103 
104   void SetJobCodeCallback(job_code_callback_t job_code_callback);
105 };
106 
107 /* create new RunScript from another */
108 RunScript* DuplicateRunscript(RunScript* src);
109 
110 /* launch each script from runscripts*/
111 int RunScripts(JobControlRecord* jcr,
112                alist* runscripts,
113                const char* name,
114                alist* allowed_script_dirs = NULL);
115 
116 void FreeRunscript(RunScript* script);
117 
118 /* foreach_alist free RunScript */
119 void FreeRunscripts(alist* runscripts); /* you have to free alist */
120 
121 extern bool (*console_command)(JobControlRecord* jcr, const char* cmd);
122 
123 #endif /* BAREOS_LIB_RUNSCRIPT_H_ */
124