Skip to content

Commit

Permalink
Raise original BadNetworkName error on DFS failure (#204)
Browse files Browse the repository at this point in the history
When attempting to resolve the DFS path on a BadNetworkName error it may
error with a DFS related error message. This error should be ignored if
the error was stating DFS is unavailable. The whole check can also be
avoided entirely if the server does not indicate it supports DFS in the
capabilities it sends back.
  • Loading branch information
jborean93 authored Nov 8, 2022
1 parent a885356 commit f1ca936
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.10.1 - TBD

* Raise the original `BadNetworkName` error if the server doesn't indicate it supports DFS or `FSDriverRequired` was raised trying to lookup the DFS information - https://github.com/jborean93/smbprotocol/issues/196

## 1.10.0 - 2022-11-07

* Require Python 3.7 or newer (dropped 3.6)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "smbprotocol"
version = "1.10.0"
version = "1.10.1"
description = "Interact with a server using the SMB 2/3 Protocol"
readme = "README.md"
requires-python = ">=3.7"
Expand Down
28 changes: 24 additions & 4 deletions src/smbclient/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import warnings

from smbprotocol._text import to_text
from smbprotocol.connection import Connection
from smbprotocol.connection import Capabilities, Connection
from smbprotocol.dfs import (
DFSReferralEntryFlags,
DFSReferralRequest,
DFSReferralResponse,
DomainEntry,
ReferralEntry,
)
from smbprotocol.exceptions import BadNetworkName, InvalidParameter, ObjectPathNotFound
from smbprotocol.exceptions import (
BadNetworkName,
FSDriverRequired,
InvalidParameter,
ObjectPathNotFound,
)
from smbprotocol.ioctl import CtlCode, IOCTLFlags, SMB2IOCTLRequest, SMB2IOCTLResponse
from smbprotocol.session import Session
from smbprotocol.tree import TreeConnect
Expand Down Expand Up @@ -313,14 +318,29 @@ def get_smb_tree(
tree = TreeConnect(session, share_path)
try:
tree.connect(require_secure_negotiate=client_config.require_secure_negotiate)
except BadNetworkName:
except BadNetworkName as err:
# If the server doesn't mention it supports DFS then don't try to
# resolve the DFS path.
if not session.connection.server_capabilities.has_flag(Capabilities.SMB2_GLOBAL_CAP_DFS):
raise

ipc_path = "\\\\%s\\IPC$" % server
if path == ipc_path: # In case we already tried connecting to IPC$ but that failed.
raise

# The share could be a DFS root, issue a root referral request to the hostname and cache the result.
ipc_tree = get_smb_tree(ipc_path, **get_kwargs)[0]
referral = dfs_request(ipc_tree, "\\%s\\%s" % (path_split[0], path_split[1]))
try:
referral = dfs_request(ipc_tree, "\\%s\\%s" % (path_split[0], path_split[1]))
except FSDriverRequired:
# If the DFS Request fails with STATUS_FS_DRIVER_REQUIRED then
# the server doesn't support DFS requests and the original
# BadNetworkName error should be raised instead of this one.
# This provides better context as to why a failure occured, i.e.
# a bad share path was provided.
# https://github.com/jborean93/smbprotocol/issues/196
raise err

client_config.cache_referral(referral)

# Sometimes a DFS referral may return 0 referrals, this needs to be checked here to avoid repeats.
Expand Down

0 comments on commit f1ca936

Please sign in to comment.