Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a password to be callable (e.g. a secretbox abstraction) #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python3/smb/SMBConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, username, password, my_name, remote_name, domain = '', use_nt
Create a new SMBConnection instance.

*username* and *password* are the user credentials required to authenticate the underlying SMB connection with the remote server.
*password* can be a string or a callable returning a string.
File operations can only be proceeded after the connection has been authenticated successfully.

Note that you need to call *connect* method to actually establish the SMB connection to the remote server and perform authentication.
Expand Down
8 changes: 6 additions & 2 deletions python3/smb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SMB(NMBSession):
def __init__(self, username, password, my_name, remote_name, domain = '', use_ntlm_v2 = True, sign_options = SIGN_WHEN_REQUIRED, is_direct_tcp = False, smb_support_mask = SMB_SUPPORT_DEFAULTS):
NMBSession.__init__(self, my_name, remote_name, is_direct_tcp = is_direct_tcp)
self.username = username
self.password = password
self._password = password
self.domain = domain
self.sign_options = sign_options
self.is_direct_tcp = is_direct_tcp
Expand Down Expand Up @@ -112,7 +112,11 @@ def __init__(self, username, password, my_name, remote_name, domain = '', use_nt
(self.use_ntlm_v2 and 'v2') or 'v1',
(SUPPORT_EXTENDED_SECURITY and 'with') or 'without')


@property
def password(self):
if callable(self._password):
return self._password()
return self._password
#
# NMBSession Methods
#
Expand Down
14 changes: 14 additions & 0 deletions python3/tests/DirectSMBConnectionTests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def test_NTLMv1_auth_SMB1():
conn3 = SMBConnection('INVALIDUSER', 'wrongPass', info['client_name'], info['server_name'], use_ntlm_v2 = False, is_direct_tcp = True)
assert not conn3.connect(info['server_ip'], info['server_port'])

@with_setup(teardown = teardown_func)
def test_NTLMv1_auth_SMB1_callable_password():
global conn, conn2, conn3
smb_structs.SUPPORT_SMB2 = False
info = getConnectionInfo()
conn = SMBConnection(info['user'], lambda: info['password'], info['client_name'], info['server_name'], use_ntlm_v2 = False, is_direct_tcp = True)
assert conn.connect(info['server_ip'], info['server_port'])

conn2 = SMBConnection(info['user'], lambda: 'wrongPass', info['client_name'], info['server_name'], use_ntlm_v2 = False, is_direct_tcp = True)
assert not conn2.connect(info['server_ip'], info['server_port'])

conn3 = SMBConnection('INVALIDUSER', lambda: 'wrongPass', info['client_name'], info['server_name'], use_ntlm_v2 = False, is_direct_tcp = True)
assert not conn3.connect(info['server_ip'], info['server_port'])

@with_setup(teardown = teardown_func)
def test_NTLMv2_auth_SMB1():
global conn
Expand Down