/python2/smb/SMBConnection.py

https://github.com/JariInc/pysmb · Python · 535 lines · 514 code · 6 blank · 15 comment · 10 complexity · f81c3d3c04b15207b091815d6a3ef840 MD5 · raw file

  1. import os, logging, select, socket, types, struct
  2. from smb_constants import *
  3. from smb_structs import *
  4. from base import SMB, NotConnectedError, NotReadyError, SMBTimeout
  5. class SMBConnection(SMB):
  6. log = logging.getLogger('SMB.SMBConnection')
  7. #: SMB messages will never be signed regardless of remote server's configurations; access errors will occur if the remote server requires signing.
  8. SIGN_NEVER = 0
  9. #: SMB messages will be signed when remote server supports signing but not requires signing.
  10. SIGN_WHEN_SUPPORTED = 1
  11. #: SMB messages will only be signed when remote server requires signing.
  12. SIGN_WHEN_REQUIRED = 2
  13. def __init__(self, username, password, my_name, remote_name, domain = '', use_ntlm_v2 = True, sign_options = SIGN_WHEN_REQUIRED, is_direct_tcp = False):
  14. """
  15. Create a new SMBConnection instance.
  16. *username* and *password* are the user credentials required to authenticate the underlying SMB connection with the remote server.
  17. File operations can only be proceeded after the connection has been authenticated successfully.
  18. Note that you need to call *connect* method to actually establish the SMB connection to the remote server and perform authentication.
  19. The default TCP port for most SMB/CIFS servers using NetBIOS over TCP/IP is 139.
  20. Some newer server installations might also support Direct hosting of SMB over TCP/IP; for these servers, the default TCP port is 445.
  21. :param string my_name: The local NetBIOS machine name that will identify where this connection is originating from.
  22. You can freely choose a name as long as it contains a maximum of 15 alphanumeric characters and does not contain spaces and any of ``\/:*?";|+``
  23. :param string remote_name: The NetBIOS machine name of the remote server.
  24. On windows, you can find out the machine name by right-clicking on the "My Computer" and selecting "Properties".
  25. This parameter must be the same as what has been configured on the remote server, or else the connection will be rejected.
  26. :param string domain: The network domain. On windows, it is known as the workgroup. Usually, it is safe to leave this parameter as an empty string.
  27. :param boolean use_ntlm_v2: Indicates whether pysmb should be NTLMv1 or NTLMv2 authentication algorithm for authentication.
  28. The choice of NTLMv1 and NTLMv2 is configured on the remote server, and there is no mechanism to auto-detect which algorithm has been configured.
  29. Hence, we can only "guess" or try both algorithms.
  30. On Sambda, Windows Vista and Windows 7, NTLMv2 is enabled by default. On Windows XP, we can use NTLMv1 before NTLMv2.
  31. :param int sign_options: Determines whether SMB messages will be signed. Default is *SIGN_WHEN_REQUIRED*.
  32. If *SIGN_WHEN_REQUIRED* (value=2), SMB messages will only be signed when remote server requires signing.
  33. If *SIGN_WHEN_SUPPORTED* (value=1), SMB messages will be signed when remote server supports signing but not requires signing.
  34. If *SIGN_NEVER* (value=0), SMB messages will never be signed regardless of remote server's configurations; access errors will occur if the remote server requires signing.
  35. :param boolean is_direct_tcp: Controls whether the NetBIOS over TCP/IP (is_direct_tcp=False) or the newer Direct hosting of SMB over TCP/IP (is_direct_tcp=True) will be used for the communication.
  36. The default parameter is False which will use NetBIOS over TCP/IP for wider compatibility (TCP port: 139).
  37. """
  38. SMB.__init__(self, username, password, my_name, remote_name, domain, use_ntlm_v2, sign_options, is_direct_tcp)
  39. self.sock = None
  40. self.auth_result = None
  41. self.is_busy = False
  42. self.is_direct_tcp = is_direct_tcp
  43. #
  44. # SMB (and its superclass) Methods
  45. #
  46. def onAuthOK(self):
  47. self.auth_result = True
  48. def onAuthFailed(self):
  49. self.auth_result = False
  50. def write(self, data):
  51. assert self.sock
  52. data_len = len(data)
  53. total_sent = 0
  54. while total_sent < data_len:
  55. sent = self.sock.send(data[total_sent:])
  56. if sent == 0:
  57. raise NotConnectedError('Server disconnected')
  58. total_sent = total_sent + sent
  59. #
  60. # Misc Properties
  61. #
  62. @property
  63. def isUsingSMB2(self):
  64. """A convenient property to return True if the underlying SMB connection is using SMB2 protocol."""
  65. return self.is_using_smb2
  66. #
  67. # Public Methods
  68. #
  69. def connect(self, ip, port = 139, sock_family = socket.AF_INET, timeout = 60):
  70. """
  71. Establish the SMB connection to the remote SMB/CIFS server.
  72. You must call this method before attempting any of the file operations with the remote server.
  73. This method will block until the SMB connection has attempted at least one authentication.
  74. :return: A boolean value indicating the result of the authentication atttempt: True if authentication is successful; False, if otherwise.
  75. """
  76. if self.sock:
  77. self.sock.close()
  78. self.auth_result = None
  79. self.sock = socket.socket(sock_family)
  80. self.sock.connect_ex(( ip, port ))
  81. self.is_busy = True
  82. try:
  83. if not self.is_direct_tcp:
  84. self.requestNMBSession()
  85. else:
  86. self.onNMBSessionOK()
  87. while self.auth_result is None:
  88. self._pollForNetBIOSPacket(timeout)
  89. finally:
  90. self.is_busy = False
  91. return self.auth_result
  92. def close(self):
  93. """
  94. Terminate the SMB connection (if it has been started) and release any sources held by the underlying socket.
  95. """
  96. if self.sock:
  97. self.sock.close()
  98. self.sock = None
  99. def listShares(self, timeout = 30):
  100. """
  101. Retrieve a list of shared resources on remote server.
  102. :return: A list of :doc:`smb.base.SharedDevice<smb_SharedDevice>` instances describing the shared resource
  103. """
  104. if not self.sock:
  105. raise NotConnectedError('Not connected to server')
  106. results = [ ]
  107. def cb(entries):
  108. self.is_busy = False
  109. results.extend(entries)
  110. def eb(failure):
  111. self.is_busy = False
  112. raise failure
  113. self.is_busy = True
  114. try:
  115. self._listShares(cb, eb, timeout)
  116. while self.is_busy:
  117. self._pollForNetBIOSPacket(timeout)
  118. finally:
  119. self.is_busy = False
  120. return results
  121. def listPath(self, service_name, path,
  122. search = SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE,
  123. pattern = '*', timeout = 30):
  124. """
  125. Retrieve a directory listing of files/folders at *path*
  126. :param string/unicode service_name: the name of the shared folder for the *path*
  127. :param string/unicode path: path relative to the *service_name* where we are interested to learn about its files/sub-folders.
  128. :param integer search: integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py).
  129. The default *search* value will query for all read-only, hidden, system, archive files and directories.
  130. :param string/unicode pattern: the filter to apply to the results before returning to the client.
  131. :return: A list of :doc:`smb.base.SharedFile<smb_SharedFile>` instances.
  132. """
  133. if not self.sock:
  134. raise NotConnectedError('Not connected to server')
  135. results = [ ]
  136. def cb(entries):
  137. self.is_busy = False
  138. results.extend(entries)
  139. def eb(failure):
  140. self.is_busy = False
  141. raise failure
  142. self.is_busy = True
  143. try:
  144. self._listPath(service_name, path, cb, eb, search = search, pattern = pattern, timeout = timeout)
  145. while self.is_busy:
  146. self._pollForNetBIOSPacket(timeout)
  147. finally:
  148. self.is_busy = False
  149. return results
  150. def listSnapshots(self, service_name, path, timeout = 30):
  151. """
  152. Retrieve a list of available snapshots (shadow copies) for *path*.
  153. Note that snapshot features are only supported on Windows Vista Business, Enterprise and Ultimate, and on all Windows 7 editions.
  154. :param string/unicode service_name: the name of the shared folder for the *path*
  155. :param string/unicode path: path relative to the *service_name* where we are interested in the list of available snapshots
  156. :return: A list of python *datetime.DateTime* instances in GMT/UTC time zone
  157. """
  158. if not self.sock:
  159. raise NotConnectedError('Not connected to server')
  160. results = [ ]
  161. def cb(entries):
  162. self.is_busy = False
  163. results.extend(entries)
  164. def eb(failure):
  165. self.is_busy = False
  166. raise failure
  167. self.is_busy = True
  168. try:
  169. self._listSnapshots(service_name, path, cb, eb, timeout = timeout)
  170. while self.is_busy:
  171. self._pollForNetBIOSPacket(timeout)
  172. finally:
  173. self.is_busy = False
  174. return results
  175. def getAttributes(self, service_name, path, timeout = 30):
  176. """
  177. Retrieve information about the file at *path* on the *service_name*.
  178. :param string/unicode service_name: the name of the shared folder for the *path*
  179. :param string/unicode path: Path of the file on the remote server. If the file cannot be opened for reading, an :doc:`OperationFailure<smb_exceptions>` will be raised.
  180. :return: A :doc:`smb.base.SharedFile<smb_SharedFile>` instance containing the attributes of the file.
  181. """
  182. if not self.sock:
  183. raise NotConnectedError('Not connected to server')
  184. results = [ ]
  185. def cb(info):
  186. self.is_busy = False
  187. results.append(info)
  188. def eb(failure):
  189. self.is_busy = False
  190. raise failure
  191. self.is_busy = True
  192. try:
  193. self._getAttributes(service_name, path, cb, eb, timeout)
  194. while self.is_busy:
  195. self._pollForNetBIOSPacket(timeout)
  196. finally:
  197. self.is_busy = False
  198. return results[0]
  199. def retrieveFile(self, service_name, path, file_obj, timeout = 30):
  200. """
  201. Retrieve the contents of the file at *path* on the *service_name* and write these contents to the provided *file_obj*.
  202. Use *retrieveFileFromOffset()* method if you wish to specify the offset to read from the remote *path* and/or the number of bytes to write to the *file_obj*.
  203. :param string/unicode service_name: the name of the shared folder for the *path*
  204. :param string/unicode path: Path of the file on the remote server. If the file cannot be opened for reading, an :doc:`OperationFailure<smb_exceptions>` will be raised.
  205. :param file_obj: A file-like object that has a *write* method. Data will be written continuously to *file_obj* until EOF is received from the remote service.
  206. :return: A 2-element tuple of ( file attributes of the file on server, number of bytes written to *file_obj* ).
  207. The file attributes is an integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py)
  208. """
  209. return self.retrieveFileFromOffset(service_name, path, file_obj, 0L, -1L, timeout)
  210. def retrieveFileFromOffset(self, service_name, path, file_obj, offset = 0L, max_length = -1L, timeout = 30):
  211. """
  212. Retrieve the contents of the file at *path* on the *service_name* and write these contents to the provided *file_obj*.
  213. :param string/unicode service_name: the name of the shared folder for the *path*
  214. :param string/unicode path: Path of the file on the remote server. If the file cannot be opened for reading, an :doc:`OperationFailure<smb_exceptions>` will be raised.
  215. :param file_obj: A file-like object that has a *write* method. Data will be written continuously to *file_obj* up to *max_length* number of bytes.
  216. :param integer/long offset: the offset in the remote *path* where the first byte will be read and written to *file_obj*. Must be either zero or a positive integer/long value.
  217. :param integer/long max_length: maximum number of bytes to read from the remote *path* and write to the *file_obj*. Specify a negative value to read from *offset* to the EOF.
  218. If zero, the method returns immediately after the file is opened successfully for reading.
  219. :return: A 2-element tuple of ( file attributes of the file on server, number of bytes written to *file_obj* ).
  220. The file attributes is an integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py)
  221. """
  222. if not self.sock:
  223. raise NotConnectedError('Not connected to server')
  224. results = [ ]
  225. def cb(r):
  226. self.is_busy = False
  227. results.append(r[1:])
  228. def eb(failure):
  229. self.is_busy = False
  230. raise failure
  231. self.is_busy = True
  232. try:
  233. self._retrieveFileFromOffset(service_name, path, file_obj, cb, eb, offset, max_length, timeout = timeout)
  234. while self.is_busy:
  235. self._pollForNetBIOSPacket(timeout)
  236. finally:
  237. self.is_busy = False
  238. return results[0]
  239. def storeFile(self, service_name, path, file_obj, timeout = 30):
  240. """
  241. Store the contents of the *file_obj* at *path* on the *service_name*.
  242. :param string/unicode service_name: the name of the shared folder for the *path*
  243. :param string/unicode path: Path of the file on the remote server. If the file at *path* does not exist, it will be created. Otherwise, it will be overwritten.
  244. If the *path* refers to a folder or the file cannot be opened for writing, an :doc:`OperationFailure<smb_exceptions>` will be raised.
  245. :param file_obj: A file-like object that has a *read* method. Data will read continuously from *file_obj* until EOF.
  246. :return: Number of bytes uploaded
  247. """
  248. if not self.sock:
  249. raise NotConnectedError('Not connected to server')
  250. results = [ ]
  251. def cb(r):
  252. self.is_busy = False
  253. results.append(r[1])
  254. def eb(failure):
  255. self.is_busy = False
  256. raise failure
  257. self.is_busy = True
  258. try:
  259. self._storeFile(service_name, path, file_obj, cb, eb, timeout = timeout)
  260. while self.is_busy:
  261. self._pollForNetBIOSPacket(timeout)
  262. finally:
  263. self.is_busy = False
  264. return results[0]
  265. def deleteFiles(self, service_name, path_file_pattern, timeout = 30):
  266. """
  267. Delete one or more regular files. It supports the use of wildcards in file names, allowing for deletion of multiple files in a single request.
  268. :param string/unicode service_name: Contains the name of the shared folder.
  269. :param string/unicode path_file_pattern: The pathname of the file(s) to be deleted, relative to the service_name.
  270. Wildcards may be used in th filename component of the path.
  271. If your path/filename contains non-English characters, you must pass in an unicode string.
  272. :return: None
  273. """
  274. if not self.sock:
  275. raise NotConnectedError('Not connected to server')
  276. def cb(r):
  277. self.is_busy = False
  278. def eb(failure):
  279. self.is_busy = False
  280. raise failure
  281. self.is_busy = True
  282. try:
  283. self._deleteFiles(service_name, path_file_pattern, cb, eb, timeout = timeout)
  284. while self.is_busy:
  285. self._pollForNetBIOSPacket(timeout)
  286. finally:
  287. self.is_busy = False
  288. def createDirectory(self, service_name, path, timeout = 30):
  289. """
  290. Creates a new directory *path* on the *service_name*.
  291. :param string/unicode service_name: Contains the name of the shared folder.
  292. :param string/unicode path: The path of the new folder (relative to) the shared folder.
  293. If the path contains non-English characters, an unicode string must be used to pass in the path.
  294. :return: None
  295. """
  296. if not self.sock:
  297. raise NotConnectedError('Not connected to server')
  298. def cb(r):
  299. self.is_busy = False
  300. def eb(failure):
  301. self.is_busy = False
  302. raise failure
  303. self.is_busy = True
  304. try:
  305. self._createDirectory(service_name, path, cb, eb, timeout = timeout)
  306. while self.is_busy:
  307. self._pollForNetBIOSPacket(timeout)
  308. finally:
  309. self.is_busy = False
  310. def deleteDirectory(self, service_name, path, timeout = 30):
  311. """
  312. Delete the empty folder at *path* on *service_name*
  313. :param string/unicode service_name: Contains the name of the shared folder.
  314. :param string/unicode path: The path of the to-be-deleted folder (relative to) the shared folder.
  315. If the path contains non-English characters, an unicode string must be used to pass in the path.
  316. :return: None
  317. """
  318. if not self.sock:
  319. raise NotConnectedError('Not connected to server')
  320. def cb(r):
  321. self.is_busy = False
  322. def eb(failure):
  323. self.is_busy = False
  324. raise failure
  325. self.is_busy = True
  326. try:
  327. self._deleteDirectory(service_name, path, cb, eb, timeout = timeout)
  328. while self.is_busy:
  329. self._pollForNetBIOSPacket(timeout)
  330. finally:
  331. self.is_busy = False
  332. def rename(self, service_name, old_path, new_path, timeout = 30):
  333. """
  334. Rename a file or folder at *old_path* to *new_path* shared at *service_name*. Note that this method cannot be used to rename file/folder across different shared folders
  335. *old_path* and *new_path* are string/unicode referring to the old and new path of the renamed resources (relative to) the shared folder.
  336. If the path contains non-English characters, an unicode string must be used to pass in the path.
  337. :param string/unicode service_name: Contains the name of the shared folder.
  338. :return: None
  339. """
  340. if not self.sock:
  341. raise NotConnectedError('Not connected to server')
  342. def cb(r):
  343. self.is_busy = False
  344. def eb(failure):
  345. self.is_busy = False
  346. raise failure
  347. self.is_busy = True
  348. try:
  349. self._rename(service_name, old_path, new_path, cb, eb)
  350. while self.is_busy:
  351. self._pollForNetBIOSPacket(timeout)
  352. finally:
  353. self.is_busy = False
  354. def echo(self, data, timeout = 10):
  355. """
  356. Send an echo command containing *data* to the remote SMB/CIFS server. The remote SMB/CIFS will reply with the same *data*.
  357. :param string data: Data to send to the remote server.
  358. :return: The *data* parameter
  359. """
  360. if not self.sock:
  361. raise NotConnectedError('Not connected to server')
  362. results = [ ]
  363. def cb(r):
  364. self.is_busy = False
  365. results.append(r)
  366. def eb(failure):
  367. self.is_busy = False
  368. raise failure
  369. self.is_busy = True
  370. try:
  371. self._echo(data, cb, eb)
  372. while self.is_busy:
  373. self._pollForNetBIOSPacket(timeout)
  374. finally:
  375. self.is_busy = False
  376. return results[0]
  377. #
  378. # Protected Methods
  379. #
  380. def _pollForNetBIOSPacket(self, timeout):
  381. expiry_time = time.time() + timeout
  382. read_len = 4
  383. data = ''
  384. while read_len > 0:
  385. try:
  386. if expiry_time < time.time():
  387. raise SMBTimeout
  388. ready, _, _ = select.select([ self.sock.fileno() ], [ ], [ ], timeout)
  389. if not ready:
  390. raise SMBTimeout
  391. d = self.sock.recv(read_len)
  392. if len(d) == 0:
  393. raise NotConnectedError
  394. data = data + d
  395. read_len -= len(d)
  396. except select.error, ex:
  397. if type(ex) is types.TupleType:
  398. if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN:
  399. raise ex
  400. else:
  401. raise ex
  402. type, flags, length = struct.unpack('>BBH', data)
  403. if flags & 0x01:
  404. length = length | 0x10000
  405. read_len = length
  406. while read_len > 0:
  407. try:
  408. if expiry_time < time.time():
  409. raise SMBTimeout
  410. ready, _, _ = select.select([ self.sock.fileno() ], [ ], [ ], timeout)
  411. if not ready:
  412. raise SMBTimeout
  413. d = self.sock.recv(read_len)
  414. if len(d) == 0:
  415. raise NotConnectedError
  416. data = data + d
  417. read_len -= len(d)
  418. except select.error, ex:
  419. if type(ex) is types.TupleType:
  420. if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN:
  421. raise ex
  422. else:
  423. raise ex
  424. self.feedData(data)