1 // Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC") 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, v. 2.0. If a copy of the MPL was not distributed with this 5 // file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 #ifndef RUN_SCRIPT_H 8 #define RUN_SCRIPT_H 9 10 #include <asiolink/process_spawn.h> 11 #include <dhcp/duid.h> 12 #include <dhcp/hwaddr.h> 13 #include <dhcp/option6_ia.h> 14 #include <dhcp/pkt4.h> 15 #include <dhcp/pkt6.h> 16 #include <dhcpsrv/lease.h> 17 #include <dhcpsrv/subnet.h> 18 #include <hooks/library_handle.h> 19 #include <string> 20 21 namespace isc { 22 namespace run_script { 23 24 /// @brief Run Script implementation. 25 class RunScriptImpl { 26 public: 27 /// @brief Constructor. 28 RunScriptImpl(); 29 30 /// @brief Destructor. 31 ~RunScriptImpl() = default; 32 33 /// @brief Sets IO service to be used by the @ref ProcessSpawn instance. 34 /// 35 /// @param io_service The IOService object, used for all ASIO operations. setIOService(const isc::asiolink::IOServicePtr & io_service)36 static void setIOService(const isc::asiolink::IOServicePtr& io_service) { 37 io_service_ = io_service; 38 } 39 40 /// @brief Gets IO service to be used by the @ref ProcessSpawn instance. 41 /// 42 /// @return The IOService object, used for all ASIO operations. getIOService()43 static isc::asiolink::IOServicePtr getIOService() { 44 return (io_service_); 45 } 46 47 /// @brief Extract boolean data and append to environment. 48 /// 49 /// @param value The value to be exported to target script environment. 50 /// @param prefix The prefix for the name of the environment variable. 51 /// @param suffix The suffix for the name of the environment variable. 52 static void extractBoolean(isc::asiolink::ProcessEnvVars& vars, 53 const bool value, 54 const std::string& prefix = "", 55 const std::string& suffix = ""); 56 57 /// @brief Extract integer data and append to environment. 58 /// 59 /// @param value The value to be exported to target script environment. 60 /// @param prefix The prefix for the name of the environment variable. 61 /// @param suffix The suffix for the name of the environment variable. 62 static void extractInteger(isc::asiolink::ProcessEnvVars& vars, 63 const uint64_t value, 64 const std::string& prefix = "", 65 const std::string& suffix = ""); 66 67 /// @brief Extract string data and append to environment. 68 /// 69 /// @param value The value to be exported to target script environment. 70 /// @param prefix The prefix for the name of the environment variable. 71 /// @param suffix The suffix for the name of the environment variable. 72 static void extractString(isc::asiolink::ProcessEnvVars& vars, 73 const std::string& value, 74 const std::string& prefix = "", 75 const std::string& suffix = ""); 76 77 /// @brief Extract HWAddr data and append to environment. 78 /// 79 /// @param hwaddr The hwaddr to be exported to target script environment. 80 /// @param prefix The prefix for the name of the environment variable. 81 /// @param suffix The suffix for the name of the environment variable. 82 static void extractHWAddr(isc::asiolink::ProcessEnvVars& vars, 83 const isc::dhcp::HWAddrPtr& hwaddr, 84 const std::string& prefix = "", 85 const std::string& suffix = ""); 86 87 /// @brief Extract DUID data and append to environment. 88 /// 89 /// @param duid The duid to be exported to target script environment. 90 /// @param prefix The prefix for the name of the environment variable. 91 /// @param suffix The suffix for the name of the environment variable. 92 static void extractDUID(isc::asiolink::ProcessEnvVars& vars, 93 const isc::dhcp::DuidPtr duid, 94 const std::string& prefix = "", 95 const std::string& suffix = ""); 96 97 /// @brief Extract Option data and append to environment. 98 /// 99 /// @param option The option to be exported to target script environment. 100 /// @param prefix The prefix for the name of the environment variable. 101 /// @param suffix The suffix for the name of the environment variable. 102 static void extractOption(isc::asiolink::ProcessEnvVars& vars, 103 const isc::dhcp::OptionPtr option, 104 const std::string& prefix = "", 105 const std::string& suffix = ""); 106 107 /// @brief Extract Option SubOption data and append to environment. 108 /// 109 /// @param option The parent option of the suboption to be exported to 110 /// target script environment. 111 /// @param code The code of the suboption. 112 /// @param prefix The prefix for the name of the environment variable. 113 /// @param suffix The suffix for the name of the environment variable. 114 static void extractSubOption(isc::asiolink::ProcessEnvVars& vars, 115 const isc::dhcp::OptionPtr option, 116 uint16_t code, 117 const std::string& prefix = "", 118 const std::string& suffix = ""); 119 120 /// @brief Extract Option6IA data and append to environment. 121 /// 122 /// @param option6IA The option6IA to be exported to target script environment. 123 /// @param prefix The prefix for the name of the environment variable. 124 /// @param suffix The suffix for the name of the environment variable. 125 static void extractOptionIA(isc::asiolink::ProcessEnvVars& vars, 126 const isc::dhcp::Option6IAPtr option6IA, 127 const std::string& prefix = "", 128 const std::string& suffix = ""); 129 130 /// @brief Extract Subnet4 data and append to environment. 131 /// 132 /// @param subnet4 The subnet4 to be exported to target script environment. 133 /// @param prefix The prefix for the name of the environment variable. 134 /// @param suffix The suffix for the name of the environment variable. 135 static void extractSubnet4(isc::asiolink::ProcessEnvVars& vars, 136 const isc::dhcp::Subnet4Ptr subnet4, 137 const std::string& prefix = "", 138 const std::string& suffix = ""); 139 140 /// @brief Extract Subnet6 data and append to environment. 141 /// 142 /// @param subnet6 The subnet6 to be exported to target script environment. 143 /// @param prefix The prefix for the name of the environment variable. 144 /// @param suffix The suffix for the name of the environment variable. 145 static void extractSubnet6(isc::asiolink::ProcessEnvVars& vars, 146 const isc::dhcp::Subnet6Ptr subnet6, 147 const std::string& prefix = "", 148 const std::string& suffix = ""); 149 150 /// @brief Extract Lease4 data and append to environment. 151 /// 152 /// @param lease4 The lease4 to be exported to target script environment. 153 /// @param prefix The prefix for the name of the environment variable. 154 /// @param suffix The suffix for the name of the environment variable. 155 static void extractLease4(isc::asiolink::ProcessEnvVars& vars, 156 const isc::dhcp::Lease4Ptr& lease4, 157 const std::string& prefix = "", 158 const std::string& suffix = ""); 159 160 /// @brief Extract Lease6 data and append to environment. 161 /// 162 /// @param lease6 The lease6 to be exported to target script environment. 163 /// @param prefix The prefix for the name of the environment variable. 164 /// @param suffix The suffix for the name of the environment variable. 165 static void extractLease6(isc::asiolink::ProcessEnvVars& vars, 166 const isc::dhcp::Lease6Ptr& lease6, 167 const std::string& prefix = "", 168 const std::string& suffix = ""); 169 170 /// @brief Extract Lease4Collection data and append to environment. 171 /// 172 /// @param leases4 The leases4 to be exported to target script environment. 173 /// @param prefix The prefix for the name of the environment variable. 174 /// @param suffix The suffix for the name of the environment variable. 175 static void extractLeases4(isc::asiolink::ProcessEnvVars& vars, 176 const isc::dhcp::Lease4CollectionPtr& leases4, 177 const std::string& prefix = "", 178 const std::string& suffix = ""); 179 180 /// @brief Extract Lease6Collection data and append to environment. 181 /// 182 /// @param leases6 The leases6 to be exported to target script environment. 183 /// @param prefix The prefix for the name of the environment variable. 184 /// @param suffix The suffix for the name of the environment variable. 185 static void extractLeases6(isc::asiolink::ProcessEnvVars& vars, 186 const isc::dhcp::Lease6CollectionPtr& leases6, 187 const std::string& prefix = "", 188 const std::string& suffix = ""); 189 190 /// @brief Extract Pkt4 data and append to environment. 191 /// 192 /// @param pkt4 The pkt4 to be exported to target script environment. 193 /// @param prefix The prefix for the name of the environment variable. 194 /// @param suffix The suffix for the name of the environment variable. 195 static void extractPkt4(isc::asiolink::ProcessEnvVars& vars, 196 const isc::dhcp::Pkt4Ptr& pkt4, 197 const std::string& prefix = "", 198 const std::string& suffix = ""); 199 200 /// @brief Extract Pkt6 data and append to environment. 201 /// 202 /// @param pkt6 The pkt6 to be exported to target script environment. 203 /// @param prefix The prefix for the name of the environment variable. 204 /// @param suffix The suffix for the name of the environment variable. 205 static void extractPkt6(isc::asiolink::ProcessEnvVars& vars, 206 const isc::dhcp::Pkt6Ptr& pkt6, 207 const std::string& prefix = "", 208 const std::string& suffix = ""); 209 210 /// @brief Run Script with specified arguments and environment parameters. 211 /// 212 /// @param args The arguments for the target script. 213 /// @param vars The environment variables made available for the target 214 /// script. 215 void runScript(const isc::asiolink::ProcessArgs& args, 216 const isc::asiolink::ProcessEnvVars& vars); 217 218 /// @brief Set name of the target script. 219 /// 220 /// @param name The name of the target script. setName(const std::string & name)221 void setName(const std::string& name) { 222 name_ = name; 223 } 224 225 /// @brief Get name of the target script. 226 /// 227 /// @return The name of the target script. getName()228 std::string getName() const { 229 return (name_); 230 } 231 232 /// @brief Set the synchronous call mode for the target script. 233 /// 234 /// @param name The synchronous call mode for the target script. setSync(const bool sync)235 void setSync(const bool sync) { 236 sync_ = sync; 237 } 238 239 /// @brief Get the synchronous call mode for the target script. 240 /// 241 /// @return The synchronous call mode for the target script. getSync()242 bool getSync() const { 243 return (sync_); 244 } 245 246 /// @brief This function parses and applies configuration parameters. 247 void configure(isc::hooks::LibraryHandle& handle); 248 249 private: 250 /// @brief Script name. 251 std::string name_; 252 253 /// @brief Sync flag 254 /// 255 /// When set to true, the call to @ref runScript blocks until the script 256 /// exits, otherwise the call will return immediately after the script is 257 /// started. 258 bool sync_; 259 260 /// @brief The IOService object, used for all ASIO operations. 261 static isc::asiolink::IOServicePtr io_service_; 262 }; 263 264 /// @brief The type of shared pointers to Run Script implementations. 265 typedef boost::shared_ptr<RunScriptImpl> RunScriptImplPtr; 266 267 } // namespace run_script 268 } // namespace isc 269 #endif 270