Skip to content

Commit

Permalink
Implemented wildcards in 'rm' command, resolves #38
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jun 17, 2024
1 parent edfe377 commit dfa4983
Show file tree
Hide file tree
Showing 6 changed files with 4,271 additions and 3,606 deletions.
2 changes: 1 addition & 1 deletion documentation/search.js

Large diffs are not rendered by default.

105 changes: 69 additions & 36 deletions documentation/smbclientng/core/CommandCompleter.html

Large diffs are not rendered by default.

1,000 changes: 534 additions & 466 deletions documentation/smbclientng/core/InteractiveShell.html

Large diffs are not rendered by default.

6,711 changes: 3,619 additions & 3,092 deletions documentation/smbclientng/core/SMBSession.html

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion smbclientng/core/InteractiveShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ def command_rm(self, arguments, command):
# SMB share needed : Yes

path = ' '.join(arguments)
if self.smbSession.path_exists(path):
if '*' in path:
self.smbSession.rm(path=path)
elif self.smbSession.path_exists(path):
if self.smbSession.path_isfile(path):
try:
self.smbSession.rm(path=path)
Expand Down
55 changes: 45 additions & 10 deletions smbclientng/core/SMBSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,17 @@ def get_file(self, path=None, keepRemotePath=False):
# Filter the entries
matching_entries = []
for entry in matches:
if entry.is_directory():
# Skip directories
continue
if entry.get_longname() == filename:
matching_entries.append(entry)
elif '*' in filename:
regexp = filename.replace('.', '\\.').replace('*', '.*')
if re.match(regexp, entry.get_longname()):
matching_entries.append(entry)

matching_entries = sorted(list(set(matching_entries)))
matching_entries = sorted(list(set(matching_entries)), key=lambda x: x.get_longname())

for entry in matching_entries:
if entry.is_directory():
Expand Down Expand Up @@ -919,15 +922,47 @@ def rm(self, path=None):
Args:
path (str, optional): The path of the file to be removed on the SMB share. Defaults to None.
"""
try:
self.smbClient.deleteFile(
shareName=self.smb_share,
pathName=ntpath.normpath(self.smb_cwd + ntpath.sep + path),
)
except Exception as err:
print("[!] Failed to remove file '%s': %s" % (path, err))
if self.config.debug:
traceback.print_exc()

# Parse path
path = path.replace('/', ntpath.sep)
if ntpath.sep in path:
tmp_search_path = ntpath.normpath(self.smb_cwd + ntpath.sep + ntpath.dirname(path))
else:
tmp_search_path = ntpath.normpath(self.smb_cwd + ntpath.sep)
# Parse filename
filename = ntpath.basename(path)

# Search for the file
matches = self.smbClient.listPath(
shareName=self.smb_share,
path=tmp_search_path + ntpath.sep + '*'
)

# Filter the entries
matching_entries = []
for entry in matches:
if entry.is_directory():
# Skip directories
continue
if entry.get_longname() == filename:
matching_entries.append(entry)
elif '*' in filename:
regexp = filename.replace('.', '\\.').replace('*', '.*')
if re.match(regexp, entry.get_longname()):
matching_entries.append(entry)

matching_entries = sorted(list(set(matching_entries)), key=lambda x: x.get_longname())

for entry in matching_entries:
try:
self.smbClient.deleteFile(
shareName=self.smb_share,
pathName=ntpath.normpath(tmp_search_path + ntpath.sep + entry.get_longname()),
)
except Exception as err:
print("[!] Failed to remove file '%s': %s" % (path, err))
if self.config.debug:
traceback.print_exc()

def tree(self, path=None):
"""
Expand Down

0 comments on commit dfa4983

Please sign in to comment.