miketeo / pysmb

pysmb is an experimental SMB/CIFS library written in Python. It implements the client-side SMB/CIFS protocol (SMB1 and SMB2) which allows your Python application to access and transfer files to/from SMB/CIFS shared folders like your Windows file sharing and Samba folders.
Other
341 stars 94 forks source link

Python3 SMBConnection.StoreFile() the opening mode of file is not checked. #60

Open fayadr opened 8 years ago

fayadr commented 8 years ago

If you do not open a file in binary mode 'rb' instead of 'r' before giving it to StoreFile() you'll get an error like this "TypeError: can't concat bytes to str". Indeed in the "smb2_structs.py", line 466, the prepare method concats the result of struct.pack(...) and self.data. If the file is opened in normal mode then self.data is of type "str". Maybe it could be good to check the format of self.data before sending it ?

dkessel commented 8 years ago

@fayadr - do you have a fix or workaround for this? I also have this problem when using python3.

I am using version 1.1.8 on windows (7, x64).

This also happens when you create a pseudo-file using io.StringIO('hello world'), if you directly want to store content in a file without having a real local file with the content. In this case, the traceback is this:

Traceback (most recent call last):
  File "D:\kessel\dev\python35\lib\site-packages\smb\SMBConnection.py", line 315, in storeFile
    return self.storeFileFromOffset(service_name, path, file_obj, 0, True, timeout)
  File "D:\kessel\dev\python35\lib\site-packages\smb\SMBConnection.py", line 346, in storeFileFromOffset
    self._pollForNetBIOSPacket(timeout)
  File "D:\kessel\dev\python35\lib\site-packages\smb\SMBConnection.py", line 581, in _pollForNetBIOSPacket
    self.feedData(data)
  File "D:\kessel\dev\python35\lib\site-packages\nmb\base.py", line 54, in feedData
    self._processNMBSessionPacket(self.data_nmb)
  File "D:\kessel\dev\python35\lib\site-packages\nmb\base.py", line 75, in _processNMBSessionPacket
    self.onNMBSessionMessage(packet.flags, packet.data)
  File "D:\kessel\dev\python35\lib\site-packages\smb\base.py", line 137, in onNMBSessionMessage
    if self._updateState(self.smb_message):
  File "D:\kessel\dev\python35\lib\site-packages\smb\base.py", line 276, in _updateState_SMB2
    req.callback(message, **req.kwargs)
  File "D:\kessel\dev\python35\lib\site-packages\smb\base.py", line 907, in createCB
    sendWrite(create_message.tid, create_message.payload.fid, starting_offset)
  File "D:\kessel\dev\python35\lib\site-packages\smb\base.py", line 918, in sendWrite
    self._sendSMBMessage(m)
  File "D:\kessel\dev\python35\lib\site-packages\smb\base.py", line 227, in _sendSMBMessage_SMB2
    smb_message.raw_data = smb_message.encode()
  File "D:\kessel\dev\python35\lib\site-packages\smb\smb2_structs.py", line 79, in encode
    self.payload.prepare(self)
  File "D:\kessel\dev\python35\lib\site-packages\smb\smb2_structs.py", line 466, in prepare
    self.flags) + self.data

TypeError: can't concat bytes to str

fayadr commented 8 years ago

Opening the file in binary mode ('rb') was good enough. If you want me to look specifically at your problem, you can send me your piece of code with io.StringIO() that triggers this exception.

dkessel commented 8 years ago

Am 30.06.2016 um 13:54 schrieb Adrien FAY:

Opening the file in binary mode ('rb') was good enough. If you want me to look specifically at your problem, you can send me your piece of code with io.StringIO() that triggers this exception.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/miketeo/pysmb/issues/60#issuecomment-229637102, or mute the thread https://github.com/notifications/unsubscribe/ABUnFl-3dVMXTE57OEUa8NBT46geyRR8ks5qQ65wgaJpZM4I0bws.

Thanks Adrien. Good to know the workaround. In my case, i mounted the SMB location as a local folder, and used the standard IO library for normal file operations. Nevertheless, it would be great to have a real fix for this.