1

I've used Commons IO to write a program that copy files and other things. But I want to copy a file to a local ip address \\10.x.x.x, but the user doesn't have rights to do the copy, so I need to put an ID and password to access it. However I cannot find a way I can do that.

To move file I use :

FileUtils.moveFileToDirectory(fichier, destDir,true);

But my directory is something like \\10.x.x.x\files and only a few users can write in that directory so I have an ID & password that let you move files there. I want that even if the users don't have rights to move files to that directory my program can do it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Youssef Saih
  • 63
  • 10

1 Answers1

1

It is not really the way Windows security works. If you really want to do it that way, you will have to use Java Native Interface or Java Native Access, and manage to call the WNetAddConnection function from Mpr.dll (and do not forget to call WNetCancelConnection when done).

But you would have to store a password in your program, which is poor security practice.

The standard way to do that would be to start a service that would run under a user that has access to the desired directory, and have your program to communicate with it using whatever you want, the simplest way being probably TCP/IP. But unless you have special requirement for that I would not recommend to use Jave for those kinds of program.

A more Java alternative would be to start a Tomcat service on server machine running under a user having access to the directory. That way you just have to develop a standard Java Web Application able to upload files that would save the files to the proper directory. But it would be a traditionnal and portable Java application with no need for JNI nor JNA.

If cannot use a Tomcat and do not want to invest to much on it, you could split the program in pieces :

  • one client program that copies files on a directory (on server machine) with File creation rights for everybody - can decays to the copy utility if nothing more has to be done or can easily written in Java
  • one server program that will run on server machine under a user that has full write permissions on target directory. This one can also be easily written in Java
  • you can easily install the server program as a service on the server machine with sc and srvany according to this answer on ServerFault

If you use a client program, you could easily add a digital signature file with each copied file, but as I said above, it is poor security practice and add little if any security. At least the program should be executable and not readable, and the sources should be kept hidden. It is better to log the users that copied the file and ask them what happened is you find a problem.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I can't start a Tomcat service so my only options are using JNI/JNA and the standard way seems a little bit complicated . And now I need to write a program in C that copy files , right ? – Youssef Saih Apr 28 '15 at 08:33
  • Thanks , I've found this solution in C# and I'm not sure I can do that in java [link](http://stackoverflow.com/a/8271759/4810161) – Youssef Saih Apr 28 '15 at 09:50