| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# Bacula(R) - The Network Backup Solution |
| 3 |
# |
| 4 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 5 |
# |
| 6 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 7 |
# from many others, a complete list can be found in the file AUTHORS. |
| 8 |
# |
| 9 |
# You may use this file and others of this release according to the |
| 10 |
# license defined in the LICENSE file, which includes the Affero General |
| 11 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 12 |
# terms pursuant to its AGPLv3 Section 7. |
| 13 |
# |
| 14 |
# This notice must be preserved when any source code is |
| 15 |
# conveyed and/or propagated. |
| 16 |
# |
| 17 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 18 |
import sys |
| 19 |
|
| 20 |
from baculak8s.io.log import Log |
| 21 |
from baculak8s.io.packet_definitions import * |
| 22 |
from baculak8s.plugins.plugin import * |
| 23 |
|
| 24 |
CONNECTION_ERROR_TEMPLATE = "Error connecting to the chosen Data Source. {error}." |
| 25 |
HOST_NOT_FOUND_CONNECTION_ERROR = "404 Not found. Name or service not known" |
| 26 |
HOST_TIMEOUT_ERROR = "Host connection timeout. Maximum retries exceeded" |
| 27 |
AUTH_FAILED_CONNECTION_ERROR = "Authentication Failed" |
| 28 |
UNEXPECTED_CONNECTION_ERROR = "Unrecognized connection error" |
| 29 |
SSL_ERROR = "SSL verification failed" |
| 30 |
CONNECTION_REFUSED_TEMPLATE = "Max retry exceeded or Connection refused" |
| 31 |
|
| 32 |
|
| 33 |
class DefaultIO(object): |
| 34 |
""" |
| 35 |
Default Class for performing IO. |
| 36 |
It contains helper methods do read and send packets. |
| 37 |
""" |
| 38 |
|
| 39 |
def send_connection_error(self, error_code, strerror=None): |
| 40 |
if strerror is None: |
| 41 |
message = self.__get_connection_error_message(error_code) |
| 42 |
else: |
| 43 |
message = strerror |
| 44 |
self.send_error(message) |
| 45 |
|
| 46 |
def send_connection_abort(self, error_code, strerror=None): |
| 47 |
if strerror is None: |
| 48 |
message = self.__get_connection_error_message(error_code) |
| 49 |
else: |
| 50 |
message = strerror |
| 51 |
self.send_abort(message) |
| 52 |
|
| 53 |
def __get_connection_error_message(self, error_code): |
| 54 |
if error_code == ERROR_HOST_NOT_FOUND: |
| 55 |
return CONNECTION_ERROR_TEMPLATE.format(error=HOST_NOT_FOUND_CONNECTION_ERROR) |
| 56 |
elif error_code == ERROR_HOST_TIMEOUT: |
| 57 |
return CONNECTION_ERROR_TEMPLATE.format(error=HOST_TIMEOUT_ERROR) |
| 58 |
elif error_code == ERROR_AUTH_FAILED: |
| 59 |
return CONNECTION_ERROR_TEMPLATE.format(error=AUTH_FAILED_CONNECTION_ERROR) |
| 60 |
elif error_code == ERROR_SSL_FAILED: |
| 61 |
return CONNECTION_ERROR_TEMPLATE.format(error=SSL_ERROR) |
| 62 |
elif error_code == ERROR_CONNECTION_REFUSED: |
| 63 |
return CONNECTION_ERROR_TEMPLATE.format(error=CONNECTION_REFUSED_TEMPLATE) |
| 64 |
else: |
| 65 |
return CONNECTION_ERROR_TEMPLATE.format(error=UNEXPECTED_CONNECTION_ERROR) |
| 66 |
|
| 67 |
def send_eod(self): |
| 68 |
packet_header = EOD_PACKET + b"\n" |
| 69 |
sys.stdout.buffer.write(packet_header) |
| 70 |
sys.stdout.flush() |
| 71 |
Log.save_sent_eod(packet_header.decode()) |
| 72 |
|
| 73 |
def send_abort(self, message): |
| 74 |
self.send_packet(STATUS_ABORT, message) |
| 75 |
|
| 76 |
def send_error(self, message): |
| 77 |
self.send_packet(STATUS_ERROR, message) |
| 78 |
|
| 79 |
def send_non_fatal_error(self, message): |
| 80 |
self.send_packet(STATUS_NON_FATAL_ERROR, message) |
| 81 |
|
| 82 |
def send_warning(self, message): |
| 83 |
self.send_packet(STATUS_WARNING, message) |
| 84 |
|
| 85 |
def send_info(self, message): |
| 86 |
self.send_packet(STATUS_INFO, message) |
| 87 |
|
| 88 |
def send_command(self, message): |
| 89 |
self.send_packet(STATUS_COMMAND, message) |
| 90 |
|
| 91 |
def send_data(self, data): |
| 92 |
self.send_packet(STATUS_DATA, data, raw=True) |
| 93 |
|
| 94 |
def send_packet(self, status, packet_content, raw=False): |
| 95 |
""" |
| 96 |
Prints a packet to stdout. A packet has the format |
| 97 |
|
| 98 |
$status$ + $packet_length$ + \n |
| 99 |
$packet_content$ + \n |
| 100 |
|
| 101 |
where $status$ represents the type of packet sent |
| 102 |
and $packet_length$ has 6 decimal chars. |
| 103 |
|
| 104 |
If $raw$ is True, $packet_content$ will be handled |
| 105 |
as a Byte String |
| 106 |
""" |
| 107 |
|
| 108 |
bytes_content = packet_content |
| 109 |
|
| 110 |
if not raw: |
| 111 |
packet_content += "\n" |
| 112 |
bytes_content = packet_content.encode() |
| 113 |
|
| 114 |
packet_length = str(len(bytes_content)).zfill(6) |
| 115 |
packet_header = "{}{}\n".format(status, packet_length) |
| 116 |
|
| 117 |
sys.stdout.buffer.write(packet_header.encode()) |
| 118 |
sys.stdout.buffer.write(bytes_content) |
| 119 |
sys.stdout.flush() |
| 120 |
|
| 121 |
if not raw: |
| 122 |
Log.save_sent_packet(packet_header, packet_content) |
| 123 |
else: |
| 124 |
Log.save_sent_data(packet_header) |
| 125 |
|
| 126 |
def send_file_info(self, info): |
| 127 |
""" |
| 128 |
Prints four packages into stdout: |
| 129 |
1 - The files FNAME packet |
| 130 |
2 - The files STAT packet |
| 131 |
3 - The files TSTAMP packet |
| 132 |
4 - A EOD packet |
| 133 |
""" |
| 134 |
|
| 135 |
self.send_command("FNAME:{}".format(info.name)) |
| 136 |
self.send_command("TSTAMP:{} {} {}".format(info.accessed_at, info.modified_at, info.created_at)) |
| 137 |
self.send_command("STAT:{} {} {} {} {} {}".format(info.type, info.size, info.uid, info.gid, info.mode, info.nlink)) |
| 138 |
self.send_eod() |
| 139 |
|
| 140 |
def send_plugin_object(self, podata): |
| 141 |
""" |
| 142 |
Prints PluginObject class information into stdout |
| 143 |
|
| 144 |
Args: |
| 145 |
podata (class PluginObject): Entity representing information about a Plugin Object |
| 146 |
""" |
| 147 |
self.send_command("PLUGINOBJ:{}".format(podata.path)) |
| 148 |
self.send_command("PLUGINOBJ_NAME:{}".format(podata.name)) |
| 149 |
if podata.cat is not None: |
| 150 |
self.send_command("PLUGINOBJ_CAT:{}".format(podata.cat)) |
| 151 |
if podata.type is not None: |
| 152 |
self.send_command("PLUGINOBJ_TYPE:{}".format(podata.type)) |
| 153 |
if podata.src is not None: |
| 154 |
self.send_command("PLUGINOBJ_SRC:{}".format(podata.src)) |
| 155 |
if podata.uuid is not None: |
| 156 |
self.send_command("PLUGINOBJ_UUID:{}".format(podata.uuid)) |
| 157 |
if podata.size is not None: |
| 158 |
self.send_command("PLUGINOBJ_SIZE:{}".format(podata.size)) |
| 159 |
if podata.count is not None: |
| 160 |
self.send_command("PLUGINOBJ_COUNT:{}".format(podata.count)) |
| 161 |
self.send_eod() |
| 162 |
|
| 163 |
def send_query_response(self, response): |
| 164 |
key = response[0] |
| 165 |
value = response[1] |
| 166 |
self.send_command("{}={}".format(key, value)) |
| 167 |
|
| 168 |
def read_line(self): |
| 169 |
""" |
| 170 |
Reads a line from the stdin buffer |
| 171 |
|
| 172 |
:return: The line read, as a Byte String, without the newline char |
| 173 |
""" |
| 174 |
return sys.stdin.buffer.readline().strip() |
| 175 |
|
| 176 |
def read_packet(self): |
| 177 |
""" |
| 178 |
Reads a packet from the stdin buffer |
| 179 |
|
| 180 |
:return: 1- The packet header, as a Byte String, without the newline char |
| 181 |
2- The packet content, as a String, without the newline char |
| 182 |
""" |
| 183 |
packet_header = sys.stdin.buffer.readline().strip() |
| 184 |
packet_content = sys.stdin.buffer.readline().strip().decode() |
| 185 |
Log.save_received_packet(packet_header, packet_content) |
| 186 |
return packet_header, packet_content |
| 187 |
|
| 188 |
def read_eod(self): |
| 189 |
packet_header = self.read_line() |
| 190 |
if not packet_header or packet_header != EOD_PACKET: |
| 191 |
raise ValueError("EOD packet not found") |
| 192 |
Log.save_received_eod(packet_header) |