Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write/upload a file using Samba/JCIFS issue (SmbAuthException: Access is denied)

So I'm trying to write a file from android device to windows shared folder. I'm using latest version of JCIFS and code which displays available network shares works fine. So I assume everything is ok with JCIFS and with my LAN, WiFi etc. Here is the code for file upload (actually I just want to write a text Sring to a File):

    public boolean save2Samba(String text, String fileName) {
        try {

            // My Windows shares doesn't require any login/password
            // String name="login";//my windows username
            // String password="password1";//my windows password

            // sSambaFolder contains a path like MYPC/E/SharedFolderName/
            String url = "smb://" + sSambaFolder.toLowerCase()+fileName;

            SmbFile file = null;
            try {
                // assume ANONYMOUS is my case but there is no description of this in JCIFS API
                NtlmPasswordAuthentication auth = NtlmPasswordAuthentication.ANONYMOUS;
                file = new SmbFile(url, auth);
                android.util.Log.i("TestApp",url);
                // output is like smb://mypc/e/sharedfoldername/file.txt;
                SmbFileOutputStream out = new SmbFileOutputStream(file);
                out.write(text.getBytes());
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

Since the url is logged I'm sure it is correct (also I checked the url using the code I mentioned above and it browses the folder's contain).
But the problem is Im always getting the same:

W/System.err(3214): jcifs.smb.SmbAuthException: Access is denied.

Shares are not password protected, so I don't need any username/password to get access. I could read/write/delete files from another WinPC and no authorization is required. Also I tried to create a password for user on WinPC with shares but result was the same. So I tried several versions of NtlmPasswordAuthentication with no luck:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(":");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("Administrator:"); //actual username on WinPC with shares
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("Administrator");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,"Administrator","");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,"","");

So what am I doing wrong and how to achive my goal when there is no auth is required to get access to shared folder?
BTW my Samsung TV which is linux based and uses samba client is accessing the same shared folder with no problem and plays MP3 from there (well, yes, it reads only). Since my AOS device is accessing my LAN via WiFi (instead of TV which is connected via Ethernet) I also checked access to shared folder using notebook+WiFi and found no problems.
Added:
I'm trying now to execute following lines:

file = new SmbFile(url, auth);
android.util.Log.i("save2Samba", "file.exists(): " + file.exists());

and getting the same Access denied. I'm not even trying to write file...

like image 666
Stan Avatar asked Jan 28 '13 09:01

Stan


2 Answers

OMG!!! Solution was so simple!!! To access network which is not login/password protected and thus doesn't need any authorization is not NtlmPasswordAuthentication.ANONYMOUS BUT it is:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, null, null);

damn it wasn't that obvious!

like image 76
Stan Avatar answered Nov 14 '22 23:11

Stan


try with this code

 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
                            username, password);
    sharepath = "smb://" + pathtosharefolder + test.txt;  


 sFile = new SmbFile(sharepath, auth);
    SmbFileOutputStream out = new SmbFileOutputStream(file, true);
    out.write(text.getBytes());

Also check you have share the folder path that you are accessing. Also check it has given write permission to the folder

like image 2
rwe Avatar answered Nov 14 '22 22:11

rwe