1. Randy Mathews
  2. PowerBuilder
  3. Thursday, 14 May 2020 23:15 PM UTC

I am trying to follow this (rather old) instruction for how to copy files to a network share with elevated credentails, but I cannot get it to work.

 

http://codeverge.com/sybase.powerbuilder.general/copy-files-to-network-with-elevat/1038363

 

My code is below.

/**************************************************************************************
// External Functions
public function boolean RevertToSelf() LIBRARY "advapi32.dll" alias for "RevertToSelf;Ansi"
public function boolean LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, long dwLogonType, long dwLogonProvider, ref long phToken) LIBRARY "advapi32.dll" alias for "LogonUserA;Ansi"
public FUNCTION boolean ImpersonateLoggedOnUser(long IntPtr) LIBRARY "advapi32.dll" alias for "ImpersonateLoggedOnUser;Ansi"



// Function parameters as_user string, as_domain string, as_password string
boolean lb_ret
int li_return
long ll_token
string as_user, as_domain, as_password

as_user = "aUser"
as_password = "aPassword"
as_domain = "myDomain"

ll_token = 0
lb_ret = LogonUser(as_user, as_domain, as_password, 3, 0, ll_token)
lb_ret = ImpersonateLoggedOnUser(ll_token)


li_return = FileCopy('c:\test.pdf', 'S:\temp\restricted\test.pdf', True) 

RevertToSelf()
//CloseHandle(ll_token)

/**************************************************************************************

 

If I create a restricted folder on my local machine and attempt to copy a file to it via the above it works. If I runt he same code and try to copy to a network share, it fails.

 

Is there an issue with the above I'm not seeing, or maybe a better way to accomplish my goal?

 

Thanks!

Olan Knight Accepted Answer Pending Moderation
  1. Thursday, 14 May 2020 23:45 PM UTC
  2. PowerBuilder
  3. # 1

Randy -
 
   I have noticed that in Windows 10, you simply CANNOT copy a file from a network drive into one of the "protected" folders on the C drive, like "Program Files" or "Program Files (x86)". You must copy the file to a non-protected folder on your C drive, and THEN you can copy into the protected folders.

   I have not seen the reverse to be true, but this sure looks and feels like something similar.


Meanwhile, in the "is it plugged in" arena of questions:

1. Have you tried a manual copy to be sure that the copy itself is not restricted by some other rule(s)?

2.  Have you tried using a batch file to copy the file from C to X?

3. Have you tried copying the file using regular code; i.e. without the elevated credentials?

   If either technique #2 or #3 works, it might be a candidate for implementation.


Later -

Olan

Comment
  1. Randy Mathews
  2. Thursday, 21 May 2020 17:49 PM UTC
I can copy the file manually if I log on as the user account that I have granted access to said folder, and which I am using in code. That works fine. I haven't tried doing the batch file as I need to do some sub-folder creation and naming in-code depending on circumstances and that's more difficult to model in a batch file. Copying with non-elevated credentials works, yes.



What I'm trying to get away from is opening up my network share permissions to match that of the user(s) who are running the application. I want to avoid intentional or unintentional meddling in the network share folder by these users to as to keep sub-folders and file names predictable. That's why I figured I would lock down file permissions to a service account only and have my app use that account when doing I/O.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.