1 /* 2 BAREOS® - Backup Archiving REcovery Open Sourced 3 4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V. 5 Copyright (C) 2016-2020 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 * Kern Sibbald, May MM 24 */ 25 /** 26 * @file 27 * BAREOS Sock Class definition 28 * Note, the old non-class code is in bnet.c, and the 29 * new class code associated with this file is in bsock.c 30 * 31 * Zero message_length from other end indicates soft eof (usually 32 * end of some binary data stream, but not end of conversation). 33 * 34 * Negative message_length, is special "signal" (no data follows). 35 * See below for SIGNAL codes. 36 */ 37 38 #ifndef BAREOS_LIB_BSOCK_H_ 39 #define BAREOS_LIB_BSOCK_H_ 40 41 #include <include/bareos.h> 42 #include <mutex> 43 #include <functional> 44 #include <cassert> 45 #include "lib/address_conf.h" 46 #include "lib/bnet_network_dump.h" 47 #include "lib/tls.h" 48 #include "lib/s_password.h" 49 #include "lib/tls_conf.h" 50 #include "include/version_numbers.h" 51 52 struct btimer_t; /* forward reference */ 53 class BareosSocket; 54 class Tls; 55 class BStringList; 56 class QualifiedResourceNameTypeConverter; 57 class dlist; 58 btimer_t* StartBsockTimer(BareosSocket* bs, uint32_t wait); 59 void StopBsockTimer(btimer_t* wid); 60 61 class BareosSocket { 62 /* 63 * Note, keep this public part before the private otherwise 64 * bat breaks on some systems such as RedHat. 65 */ 66 public: 67 int fd_; /* Socket file descriptor */ 68 uint64_t read_seqno; /* Read sequence number */ 69 POOLMEM* msg; /* Message pool buffer */ 70 POOLMEM* errmsg; /* Edited error message */ 71 int spool_fd_; /* Spooling file */ 72 IPADDR* src_addr; /* IP address to source connections from */ 73 uint32_t in_msg_no; /* Input message number */ 74 uint32_t out_msg_no; /* Output message number */ 75 int32_t message_length; /* Message length */ 76 volatile time_t timer_start; /* Time started read/write */ 77 int b_errno; /* BareosSocket errno */ 78 int blocking_; /* Blocking state (0 = nonblocking, 1 = blocking) */ 79 volatile int errors; /* Incremented for each error on socket */ 80 volatile bool suppress_error_msgs_; /* Set to suppress error messages */ 81 int sleep_time_after_authentication_error; 82 83 struct sockaddr client_addr; /* Client's IP address */ 84 struct sockaddr_in peer_addr; /* Peer's IP address */ SetTlsEstablished()85 void SetTlsEstablished() { tls_established_ = true; } TlsEstablished()86 bool TlsEstablished() const { return tls_established_; } 87 std::shared_ptr<Tls> tls_conn; /* Associated tls connection */ 88 std::unique_ptr<Tls> tls_conn_init; /* during initialization */ 89 BareosVersionNumber connected_daemon_version_; 90 91 protected: 92 JobControlRecord* jcr_; /* JobControlRecord or NULL for error msgs */ 93 std::shared_ptr<std::mutex> mutex_; 94 char* who_; /* Name of daemon to which we are talking */ 95 char* host_; /* Host name/IP */ 96 int port_; /* Desired port */ 97 btimer_t* tid_; /* Timer id */ 98 boffset_t data_end_; /* Offset of last valid data written */ 99 int32_t FileIndex_; /* Last valid attr spool FI */ 100 bool timed_out_; /* Timed out in read/write */ 101 bool terminated_; /* Set when BNET_TERMINATE arrives */ 102 bool cloned_; /* Set if cloned BareosSocket */ 103 bool spool_; /* Set for spooling */ 104 bool use_bursting_; /* Set to use bandwidth bursting */ 105 bool use_keepalive_; /* Set to use keepalive on the socket */ 106 int64_t bwlimit_; /* Set to limit bandwidth */ 107 int64_t nb_bytes_; /* Bytes sent/recv since the last tick */ 108 btime_t last_tick_; /* Last tick used by bwlimit */ 109 bool tls_established_; /* is true when tls connection is established */ 110 std::unique_ptr<BnetDump> bnet_dump_; 111 112 virtual void FinInit(JobControlRecord* jcr, 113 int sockfd, 114 const char* who, 115 const char* host, 116 int port, 117 struct sockaddr* lclient_addr) 118 = 0; 119 virtual bool open(JobControlRecord* jcr, 120 const char* name, 121 const char* host, 122 char* service, 123 int port, 124 utime_t heart_beat, 125 int* fatal) 126 = 0; 127 128 private: 129 bool TwoWayAuthenticate(JobControlRecord* jcr, 130 const std::string own_qualified_name, 131 const char* identity, 132 s_password& password, 133 TlsResource* tls_resource, 134 bool initiated_by_remote); 135 bool DoTlsHandshakeWithClient(TlsConfigCert* local_tls_cert, 136 JobControlRecord* jcr); 137 bool DoTlsHandshakeWithServer(TlsConfigCert* local_tls_cert, 138 const char* identity, 139 const char* password, 140 JobControlRecord* jcr); 141 void ParameterizeTlsCert(Tls* tls_conn, TlsResource* tls_resource); SetBnetDump(std::unique_ptr<BnetDump> && bnet_dump)142 void SetBnetDump(std::unique_ptr<BnetDump>&& bnet_dump) 143 { 144 // do not set twice 145 assert(!bnet_dump_); 146 bnet_dump_ = std::move(bnet_dump); 147 } 148 149 public: 150 BareosSocket(); 151 BareosSocket(const BareosSocket& other); 152 virtual ~BareosSocket(); 153 154 /* Methods -- in bsock.c */ 155 // void free_bsock(); 156 void CloseTlsConnectionAndFreeMemory(); 157 virtual BareosSocket* clone() = 0; 158 virtual bool connect(JobControlRecord* jcr, 159 int retry_interval, 160 utime_t max_retry_time, 161 utime_t heart_beat, 162 const char* name, 163 const char* host, 164 char* service, 165 int port, 166 bool verbose) 167 = 0; 168 virtual int32_t recv() = 0; 169 virtual bool send() = 0; 170 virtual int32_t read_nbytes(char* ptr, int32_t nbytes) = 0; 171 virtual int32_t write_nbytes(char* ptr, int32_t nbytes) = 0; 172 virtual void close() = 0; /* close connection and destroy packet */ 173 virtual void destroy() = 0; /* destroy socket packet */ 174 virtual int GetPeer(char* buf, socklen_t buflen) = 0; 175 virtual bool SetBufferSize(uint32_t size, int rw) = 0; 176 virtual int SetNonblocking() = 0; 177 virtual int SetBlocking() = 0; 178 virtual void RestoreBlocking(int flags) = 0; 179 virtual bool ConnectionReceivedTerminateSignal() = 0; 180 /* 181 * Returns: 1 if data available, 0 if timeout, -1 if error 182 */ 183 virtual int WaitData(int sec, int usec = 0) = 0; 184 virtual int WaitDataIntr(int sec, int usec = 0) = 0; 185 bool fsend(const char*, ...); 186 bool vfsend(const char* fmt, va_list ap); 187 bool send(const char* msg_in, uint32_t nbytes); 188 void SetKillable(bool killable); 189 bool signal(int signal); 190 const char* bstrerror(); /* last error on socket */ 191 bool despool(void UpdateAttrSpoolSize(ssize_t size), ssize_t tsize); 192 bool ConsoleAuthenticateWithDirector(JobControlRecord* jcr, 193 const char* name, 194 s_password& password, 195 TlsResource* tls_resource, 196 const std::string& own_qualified_name, 197 BStringList& response_args, 198 uint32_t& response_id); 199 bool ParameterizeAndInitTlsConnection(TlsResource* tls_resource, 200 const char* identity, 201 const char* password, 202 bool initiated_by_remote); 203 bool ParameterizeAndInitTlsConnectionAsAServer(ConfigurationParser* config); 204 bool DoTlsHandshake(TlsPolicy remote_tls_policy, 205 TlsResource* tls_resource, 206 bool initiated_by_remote, 207 const char* identity, 208 const char* password, 209 JobControlRecord* jcr); 210 bool DoTlsHandshakeAsAServer(ConfigurationParser* config, 211 JobControlRecord* jcr = nullptr); 212 bool SetLocking(); /* in bsock.c */ 213 void ClearLocking(); /* in bsock.c */ 214 void SetSourceAddress(dlist* src_addr_list); 215 void ControlBwlimit(int bytes); /* in bsock.c */ 216 bool EvaluateCleartextBareosHello(bool& cleartext, 217 std::string& client_name_out, 218 std::string& r_code_str_out, 219 BareosVersionNumber& version_out) const; 220 void OutputCipherMessageString(std::function<void(const char*)>); 221 void GetCipherMessageString(std::string& str) const; 222 bool ReceiveAndEvaluateResponseMessage(uint32_t& id_out, 223 BStringList& args_out); 224 bool FormatAndSendResponseMessage(uint32_t id, 225 const BStringList& list_of_agruments); 226 bool FormatAndSendResponseMessage(uint32_t id, const std::string& str); 227 228 bool AuthenticateOutboundConnection(JobControlRecord* jcr, 229 const std::string own_qualified_name, 230 const char* identity, 231 s_password& password, 232 TlsResource* tls_resource); 233 234 bool AuthenticateInboundConnection(JobControlRecord* jcr, 235 ConfigurationParser* my_config, 236 const char* name, 237 s_password& password, 238 TlsResource* tls_resource); 239 SetJcr(JobControlRecord * jcr)240 void SetJcr(JobControlRecord* jcr) { jcr_ = jcr; } SetWho(char * who)241 void SetWho(char* who) { who_ = who; } SetHost(char * host)242 void SetHost(char* host) { host_ = host; } SetPort(int port)243 void SetPort(int port) { port_ = port; } who()244 char* who() { return who_; } host()245 char* host() { return host_; } port()246 int port() { return port_; } jcr()247 JobControlRecord* jcr() { return jcr_; } get_jcr()248 JobControlRecord* get_jcr() { return jcr_; } IsSpooling()249 bool IsSpooling() { return spool_; } IsTerminated()250 bool IsTerminated() { return terminated_; } IsTimedOut()251 bool IsTimedOut() { return timed_out_; } IsStop()252 bool IsStop() { return errors || IsTerminated(); } IsError()253 bool IsError() 254 { 255 errno = b_errno; 256 return errors; 257 } SetDataEnd(int32_t FileIndex)258 void SetDataEnd(int32_t FileIndex) 259 { 260 if (spool_ && FileIndex > FileIndex_) { 261 FileIndex_ = FileIndex - 1; 262 data_end_ = lseek(spool_fd_, 0, SEEK_CUR); 263 } 264 } get_data_end()265 boffset_t get_data_end() { return data_end_; } get_FileIndex()266 int32_t get_FileIndex() { return FileIndex_; } SetBwlimit(int64_t maxspeed)267 void SetBwlimit(int64_t maxspeed) { bwlimit_ = maxspeed; } UseBwlimit()268 bool UseBwlimit() { return bwlimit_ > 0; } SetBwlimitBursting()269 void SetBwlimitBursting() { use_bursting_ = true; } clear_bwlimit_bursting()270 void clear_bwlimit_bursting() { use_bursting_ = false; } SetSpooling()271 void SetSpooling() { spool_ = true; } ClearSpooling()272 void ClearSpooling() { spool_ = false; } SetTimedOut()273 void SetTimedOut() { timed_out_ = true; } ClearTimedOut()274 void ClearTimedOut() { timed_out_ = false; } SetTerminated()275 void SetTerminated() { terminated_ = true; } StartTimer(int sec)276 void StartTimer(int sec) { tid_ = StartBsockTimer(this, sec); } StopTimer()277 void StopTimer() { StopBsockTimer(tid_); } 278 void LockMutex(); 279 void UnlockMutex(); 280 void InitBnetDump(std::string own_qualified_name); 281 void SetBnetDumpDestinationQualifiedName( 282 std::string destination_qualified_name); IsBnetDumpEnabled()283 bool IsBnetDumpEnabled() const { return bnet_dump_.get() != nullptr; } 284 }; 285 286 /** 287 * Signal definitions for use in BnetSig() 288 * Note! These must be negative. There are signals that are generated 289 * by the bsock software not by the OS ... 290 */ 291 enum 292 { 293 BNET_EOD = -1, /* End of data stream, new data may follow */ 294 BNET_EOD_POLL = -2, /* End of data and poll all in one */ 295 BNET_STATUS = -3, /* Send full status */ 296 BNET_TERMINATE = -4, /* Conversation terminated, doing close() */ 297 BNET_POLL = -5, /* Poll request, I'm hanging on a read */ 298 BNET_HEARTBEAT = -6, /* Heartbeat Response requested */ 299 BNET_HB_RESPONSE = -7, /* Only response permited to HB */ 300 BNET_xxxxxxPROMPT = -8, /* No longer used -- Prompt for subcommand */ 301 BNET_BTIME = -9, /* Send UTC btime */ 302 BNET_BREAK = -10, /* Stop current command -- ctl-c */ 303 BNET_START_SELECT = -11, /* Start of a selection list */ 304 BNET_END_SELECT = -12, /* End of a select list */ 305 BNET_INVALID_CMD = -13, /* Invalid command sent */ 306 BNET_CMD_FAILED = -14, /* Command failed */ 307 BNET_CMD_OK = -15, /* Command succeeded */ 308 BNET_CMD_BEGIN = -16, /* Start command execution */ 309 BNET_MSGS_PENDING = -17, /* Messages pending */ 310 BNET_MAIN_PROMPT = -18, /* Server ready and waiting */ 311 BNET_SELECT_INPUT = -19, /* Return selection input */ 312 BNET_WARNING_MSG = -20, /* Warning message */ 313 BNET_ERROR_MSG = -21, /* Error message -- command failed */ 314 BNET_INFO_MSG = -22, /* Info message -- status line */ 315 BNET_RUN_CMD = -23, /* Run command follows */ 316 BNET_YESNO = -24, /* Request yes no response */ 317 BNET_START_RTREE = -25, /* Start restore tree mode */ 318 BNET_END_RTREE = -26, /* End restore tree mode */ 319 BNET_SUB_PROMPT = -27, /* Indicate we are at a subprompt */ 320 BNET_TEXT_INPUT = -28 /* Get text input from user */ 321 }; 322 323 #define BNET_SETBUF_READ 1 /* Arg for BnetSetBufferSize */ 324 #define BNET_SETBUF_WRITE 2 /* Arg for BnetSetBufferSize */ 325 326 /** 327 * Return status from BnetRecv() 328 * Note, the HARDEOF and ERROR refer to comm status/problems 329 * rather than the BNET_xxx above, which are software signals. 330 */ 331 enum 332 { 333 BNET_SIGNAL = -1, 334 BNET_HARDEOF = -2, 335 BNET_ERROR = -3 336 }; 337 338 #endif /* BAREOS_LIB_BSOCK_H_ */ 339