1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-02 09:47:23 +03:00

samba-tool: Move smb_connection to a common file

This is in preparation for needing it here later.

Signed-off-by: David Mulder <dmulder@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
David Mulder 2022-12-07 11:32:30 -07:00 committed by Andrew Bartlett
parent d6194600c1
commit a0f8d7ca05
2 changed files with 21 additions and 20 deletions

View File

@ -16,6 +16,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from samba.credentials import SMB_SIGNING_REQUIRED
from samba.samba3 import param as s3param
from samba.samba3 import libsmb_samba_internal as libsmb
from samba.netcmd import CommandError
def create_directory_hier(conn, remotedir):
elems = remotedir.replace('/', '\\').split('\\')
@ -24,3 +28,19 @@ def create_directory_hier(conn, remotedir):
path = path + '\\' + e
if not conn.chkpath(path):
conn.mkdir(path)
def smb_connection(dc_hostname, service, lp, creds):
# SMB connect to DC
# Force signing for the smb connection
saved_signing_state = creds.get_smb_signing()
creds.set_smb_signing(SMB_SIGNING_REQUIRED)
try:
# the SMB bindings rely on having a s3 loadparm
s3_lp = s3param.get_context()
s3_lp.load(lp.configfile)
conn = libsmb.Conn(dc_hostname, service, lp=s3_lp, creds=creds)
except Exception:
raise CommandError("Error connecting to '%s' using SMB" % dc_hostname)
# Reset signing state
creds.set_smb_signing(saved_signing_state)
return conn

View File

@ -43,7 +43,6 @@ import samba.auth
from samba.auth import AUTH_SESSION_INFO_DEFAULT_GROUPS, AUTH_SESSION_INFO_AUTHENTICATED, AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
from samba.netcmd.common import netcmd_finddc
from samba import policy
from samba.samba3 import param as s3param
from samba.samba3 import libsmb_samba_internal as libsmb
from samba import NTSTATUSError
import uuid
@ -62,7 +61,6 @@ from samba.gp_parse.gp_csv import GPAuditCsvParser
from samba.gp_parse.gp_inf import GptTmplInfParser
from samba.gp_parse.gp_aas import GPAasParser
from samba import param
from samba.credentials import SMB_SIGNING_REQUIRED
from samba.netcmd.common import attr_default
from samba.common import get_bytes, get_string
from configparser import ConfigParser
@ -77,7 +75,7 @@ from samba.ntstatus import (
NT_STATUS_OBJECT_PATH_NOT_FOUND,
NT_STATUS_ACCESS_DENIED
)
from samba.netcmd.gpcommon import create_directory_hier
from samba.netcmd.gpcommon import create_directory_hier, smb_connection
def gpo_flags_string(value):
@ -383,23 +381,6 @@ def copy_directory_local_to_remote(conn, localdir, remotedir,
conn.savefile(r_name, data)
def smb_connection(dc_hostname, service, lp, creds):
# SMB connect to DC
# Force signing for the smb connection
saved_signing_state = creds.get_smb_signing()
creds.set_smb_signing(SMB_SIGNING_REQUIRED)
try:
# the SMB bindings rely on having a s3 loadparm
s3_lp = s3param.get_context()
s3_lp.load(lp.configfile)
conn = libsmb.Conn(dc_hostname, service, lp=s3_lp, creds=creds)
except Exception:
raise CommandError("Error connecting to '%s' using SMB" % dc_hostname)
# Reset signing state
creds.set_smb_signing(saved_signing_state)
return conn
class GPOCommand(Command):
def construct_tmpdir(self, tmpdir, gpo):
"""Ensure that the temporary directory structure used in fetch,