From 1177f5de8b5d3ba42e05964d0a2cd928e89c1e55 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Wed, 9 Sep 2020 11:50:37 +0200 Subject: [PATCH] filed: adapt ldap plugin to new python api --- .../filed/python/ldap/BareosFdPluginLDAP.py | 167 ++++++++---------- .../filed/python/ldap/bareos-fd-ldap.py | 6 +- 2 files changed, 79 insertions(+), 94 deletions(-) diff --git a/core/src/plugins/filed/python/ldap/BareosFdPluginLDAP.py b/core/src/plugins/filed/python/ldap/BareosFdPluginLDAP.py index 33f133bc75b..05eb0c17bce 100644 --- a/core/src/plugins/filed/python/ldap/BareosFdPluginLDAP.py +++ b/core/src/plugins/filed/python/ldap/BareosFdPluginLDAP.py @@ -3,7 +3,7 @@ # BAREOS - Backup Archiving REcovery Open Sourced # # Copyright (C) 2015-2015 Planets Communications B.V. -# Copyright (C) 2015-2015 Bareos GmbH & Co. KG +# Copyright (C) 2015-2020 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 @@ -26,8 +26,7 @@ # import bareosfd -from bareos_fd_consts import bJobMessageType, bFileType, bRCs, bIOPS -from bareos_fd_consts import bCFs + import BareosFdPluginBaseclass from StringIO import StringIO from stat import S_IFREG, S_IFDIR, S_IRWXU @@ -58,19 +57,18 @@ def parse_plugin_definition(self, plugindef): Parses the plugin arguments and validates the options. """ bareosfd.DebugMessage( - 100, - "parse_plugin_definition() was called in module %s\n" % (__name__), + 100, "parse_plugin_definition() was called in module %s\n" % (__name__) ) super(BareosFdPluginLDAP, self).parse_plugin_definition(plugindef) - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def start_backup_job(self): """ Start of Backup Job """ - check_option_bRC = self.check_options(context) - if check_option_bRC != bRCs["bRC_OK"]: + check_option_bRC = self.check_options() + if check_option_bRC != bareosfd.bRC_OK: return check_option_bRC return self.ldap.prepare_backup(self.options) @@ -79,9 +77,7 @@ def start_backup_file(self, savepkt): """ Defines the file to backup and creates the savepkt. """ - bareosfd.DebugMessage( - 100, "BareosFdPluginLDAP:start_backup_file() called\n" - ) + bareosfd.DebugMessage(100, "BareosFdPluginLDAP:start_backup_file() called\n") return self.ldap.get_next_file_to_backup(savepkt) @@ -89,16 +85,15 @@ def start_restore_job(self): """ Start of Restore Job """ - check_option_bRC = self.check_options(context) - if check_option_bRC != bRCs["bRC_OK"]: + check_option_bRC = self.check_options() + if check_option_bRC != bareosfd.bRC_OK: return check_option_bRC return self.ldap.prepare_restore(self.options) def start_restore_file(self, cmd): bareosfd.DebugMessage( - 100, - "BareosFdPluginLDAP:start_restore_file() called with %s\n" % (cmd), + 100, "BareosFdPluginLDAP:start_restore_file() called with %s\n" % (cmd) ) # It can happen that this is the first entry point on a restore @@ -106,9 +101,9 @@ def start_restore_file(self, cmd): # that event after it aready fired. If that is true self.ldap.ld will # not be set and we call start_restore_job() from here. if not self.ldap.ld: - return self.start_restore_job(context) + return self.start_restore_job() else: - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def create_file(self, restorepkt): """ @@ -116,39 +111,37 @@ def create_file(self, restorepkt): to get the actual DN of the LDAP record """ bareosfd.DebugMessage( - 100, - "BareosFdPluginVMware:create_file() called with %s\n" % (restorepkt), + 100, "BareosFdPluginVMware:create_file() called with %s\n" % (restorepkt) ) - if restorepkt.type == bFileType["FT_DIREND"]: - restorepkt.create_status = bCFs["CF_SKIP"] - elif restorepkt.type == bFileType["FT_REG"]: + if restorepkt.type == bareosfd.bFileType["FT_DIREND"]: + restorepkt.create_status = bareosfd.bCFs["CF_SKIP"] + elif restorepkt.type == bareosfd.bFileType["FT_REG"]: self.ldap.set_new_dn(restorepkt.ofname) - restorepkt.create_status = bCFs["CF_EXTRACT"] + restorepkt.create_status = bareosfd.bCFs["CF_EXTRACT"] else: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Request to restore illegal filetype %s\n" % (restorepkt.type), ) - return bRCs["bRC_Error"] + return bareosfd.bRC_Error - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def plugin_io(self, IOP): bareosfd.DebugMessage( - 100, - "BareosFdPluginLDAP:plugin_io() called with function %s\n" % (IOP.func), + 100, "BareosFdPluginLDAP:plugin_io() called with function %s\n" % (IOP.func) ) - if IOP.func == bIOPS["IO_OPEN"]: - return bRCs["bRC_OK"] + if IOP.func == bareosfd.bIOPS["IO_OPEN"]: + return bareosfd.bRC_OK - elif IOP.func == bIOPS["IO_CLOSE"]: - return bRCs["bRC_OK"] + elif IOP.func == bareosfd.bIOPS["IO_CLOSE"]: + return bareosfd.bRC_OK - elif IOP.func == bIOPS["IO_SEEK"]: - return bRCs["bRC_OK"] + elif IOP.func == bareosfd.bIOPS["IO_SEEK"]: + return bareosfd.bRC_OK - elif IOP.func == bIOPS["IO_READ"]: + elif IOP.func == bareosfd.bIOPS["IO_READ"]: if self.ldap.ldif: IOP.buf = bytearray(self.ldap.ldif) IOP.status = self.ldap.ldif_len @@ -157,29 +150,25 @@ def plugin_io(self, IOP): IOP.status = 0 IOP.io_errno = 0 - return bRCs["bRC_OK"] + return bareosfd.bRC_OK - elif IOP.func == bIOPS["IO_WRITE"]: + elif IOP.func == bareosfd.bIOPS["IO_WRITE"]: self.ldap.ldif = str(IOP.buf) self.ldap.ldif_len = IOP.count IOP.status = IOP.count IOP.io_errno = 0 - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def end_backup_file(self): - bareosfd.DebugMessage( - 100, "BareosFdPluginLDAP:end_backup_file() called\n" - ) + bareosfd.DebugMessage(100, "BareosFdPluginLDAP:end_backup_file() called\n") - return self.ldap.has_next_file(context) + return self.ldap.has_next_file() def end_restore_file(self): - bareosfd.DebugMessage( - 100, "BareosFdPluginLDAP:end_restore_file() called\n" - ) + bareosfd.DebugMessage(100, "BareosFdPluginLDAP:end_restore_file() called\n") - return self.ldap.restore_entry(context) + return self.ldap.restore_entry() # Overload ldap class and ldap.resiter class for bulk retrieval @@ -222,41 +211,40 @@ def connect_and_bind(self, options, bulk=False): self.ld.simple_bind_s("", "") except ldap.INVALID_CREDENTIALS: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Failed to bind to LDAP uri %s\n" % (options["uri"]), ) - return bRCs["bRC_Error"] + return bareosfd.bRC_Error except ldap.LDAPError as e: if type(e.message) == dict and "desc" in e.message: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Failed to bind to LDAP uri %s: %s\n" % (options["uri"], e.message["desc"]), ) else: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Failed to bind to LDAP uri %s: %s\n" % (options["uri"], e), ) - return bRCs["bRC_Error"] + return bareosfd.bRC_Error bareosfd.DebugMessage(100, "connected to LDAP server\n") - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def prepare_backup(self, options): """ Prepare a LDAP backup """ connect_bRC = self.connect_and_bind(options, True) - if connect_bRC != bRCs["bRC_OK"]: + if connect_bRC != bareosfd.bRC_OK: return connect_bRC bareosfd.DebugMessage( - 100, - "Creating search filter and attribute filter to perform LDAP search\n", + 100, "Creating search filter and attribute filter to perform LDAP search\n" ) # Get a subtree @@ -279,32 +267,32 @@ def prepare_backup(self, options): except ldap.LDAPError as e: if type(e.message) == dict and "desc" in e.message: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Failed to execute LDAP search on LDAP uri %s: %s\n" % (options["uri"], e.message["desc"]), ) else: bareosfd.JobMessage( - bJobMessageType["M_FATAL"], + bareosfd.bJobMessageType["M_FATAL"], "Failed to execute LDAP search on LDAP uri %s: %s\n" % (options["uri"], e), ) - return bRCs["bRC_Error"] + return bareosfd.bRC_Error bareosfd.DebugMessage(100, "Successfully performed LDAP search\n") - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def prepare_restore(self, options): """ Prepare a LDAP restore """ connect_bRC = self.connect_and_bind(options) - if connect_bRC != bRCs["bRC_OK"]: + if connect_bRC != bareosfd.bRC_OK: return connect_bRC - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def to_unix_timestamp(self, timestamp): if timestamp: @@ -338,15 +326,15 @@ def get_next_file_to_backup(self, savepkt): ldif_dump.close() statp = bareosfd.StatPacket() - statp.mode = S_IRWXU | S_IFREG - statp.size = self.ldif_len + statp.st_mode = S_IRWXU | S_IFREG + statp.st_size = self.ldif_len if self.unix_create_time: - statp.ctime = self.unix_create_time + statp.st_ctime = self.unix_create_time if self.unix_modify_time: - statp.mtime = self.unix_modify_time + statp.st_mtime = self.unix_modify_time savepkt.statp = statp - savepkt.type = bFileType["FT_REG"] + savepkt.type = bareosfd.bFileType["FT_REG"] savepkt.fname = self.file_to_backup + "/data.ldif" # Read the content of a file savepkt.no_read = False @@ -363,9 +351,9 @@ def get_next_file_to_backup(self, savepkt): res_type, res_data, res_msgid, res_controls = self.resultset.next() self.ldap_entries = res_data except ldap.NO_SUCH_OBJECT: - return bRCs["bRC_Error"] + return bareosfd.bRC_Error except StopIteration: - return bRCs["bRC_Error"] + return bareosfd.bRC_Error # Get the next entry from the result set. if self.ldap_entries: @@ -380,9 +368,7 @@ def get_next_file_to_backup(self, savepkt): except KeyError: pass else: - self.unix_create_time = self.to_unix_timestamp( - createTimestamp - ) + self.unix_create_time = self.to_unix_timestamp(createTimestamp) self.unix_modify_time = None try: @@ -390,9 +376,7 @@ def get_next_file_to_backup(self, savepkt): except KeyError: pass else: - self.unix_modify_time = self.to_unix_timestamp( - modifyTimestamp - ) + self.unix_modify_time = self.to_unix_timestamp(modifyTimestamp) # Convert the DN into a PATH e.g. reverse the elements. dn_sliced = self.dn.split(",") @@ -401,21 +385,22 @@ def get_next_file_to_backup(self, savepkt): ) statp = bareosfd.StatPacket() - statp.mode = S_IRWXU | S_IFDIR + statp.st_mode = S_IRWXU | S_IFDIR if self.unix_create_time: - statp.ctime = self.unix_create_time + statp.st_ctime = self.unix_create_time if self.unix_modify_time: - statp.mtime = self.unix_modify_time + statp.st_mtime = self.unix_modify_time savepkt.statp = statp - savepkt.type = bFileType["FT_DIREND"] + savepkt.type = bareosfd.bFileType["FT_DIREND"] savepkt.fname = self.file_to_backup - # A directory has a link field which contains the fname + a trailing '/' + # A directory has a link field which contains + # the fname + a trailing '/' savepkt.link = self.file_to_backup + "/" # Don't read the content of a directory savepkt.no_read = True - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def set_new_dn(self, fname): path = fname[: -len("/ldif.data")] @@ -430,7 +415,7 @@ def has_next_file(self): # if there is still data in the result set if self.file_to_backup or self.ldap_entries: bareosfd.DebugMessage(100, "has_next_file(): returning bRC_More\n") - return bRCs["bRC_More"] + return bareosfd.bRC_More else: # See if there are more result sets. if self.resultset: @@ -444,11 +429,11 @@ def has_next_file(self): bareosfd.DebugMessage( 100, "has_next_file(): returning bRC_More\n" ) - return bRCs["bRC_More"] + return bareosfd.bRC_More except StopIteration: pass - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def restore_entry(self): # Restore the entry @@ -462,7 +447,7 @@ def restore_entry(self): if self.dn != dn: bareosfd.JobMessage( - bJobMessageType["M_INFO"], + bareosfd.bJobMessageType["M_INFO"], "Restoring original DN %s as %s\n" % (dn, self.dn), ) @@ -480,36 +465,36 @@ def restore_entry(self): except ldap.LDAPError as e: if type(e.message) == dict and "desc" in e.message: bareosfd.JobMessage( - bJobMessageType["M_ERROR"], + bareosfd.bJobMessageType["M_ERROR"], "Failed to restore LDAP DN %s: %s\n" % (self.dn, e.message["desc"]), ) else: bareosfd.JobMessage( - bJobMessageType["M_ERROR"], + bareosfd.bJobMessageType["M_ERROR"], "Failed to restore LDAP DN %s: %s\n" % (self.dn, e), ) self.ldif = None - return bRCs["bRC_Error"] + return bareosfd.bRC_Error else: bareosfd.JobMessage( - bJobMessageType["M_ERROR"], + bareosfd.bJobMessageType["M_ERROR"], "Failed to restore LDAP DN %s no writable binding to LDAP exists\n" % (self.dn), ) self.ldif = None - return bRCs["bRC_Error"] + return bareosfd.bRC_Error # Processed ldif self.ldif = None - return bRCs["bRC_OK"] + return bareosfd.bRC_OK def cleanup(self): if self.ld: self.ld.unbind_s() - return bRCs["bRC_OK"] + return bareosfd.bRC_OK # vim: ts=4 tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/core/src/plugins/filed/python/ldap/bareos-fd-ldap.py b/core/src/plugins/filed/python/ldap/bareos-fd-ldap.py index 249a7cee85c..6f9b9b09199 100644 --- a/core/src/plugins/filed/python/ldap/bareos-fd-ldap.py +++ b/core/src/plugins/filed/python/ldap/bareos-fd-ldap.py @@ -3,7 +3,7 @@ # BAREOS - Backup Archiving REcovery Open Sourced # # Copyright (C) 2015-2015 Planets Communications B.V. -# Copyright (C) 2015-2015 Bareos GmbH & Co. KG +# Copyright (C) 2015-2020 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 @@ -27,7 +27,7 @@ # # Provided by the Bareos FD Python plugin interface -from bareos_fd_consts import bRCs +from bareosfd import bRC_OK # This module contains the wrapper functions called by the Bareos-FD, the # functions call the corresponding @@ -48,7 +48,7 @@ def load_bareos_plugin(plugindef): plugindef ) - return bRCs["bRC_OK"] + return bRC_OK # the rest is done in the Plugin module