views:

472

answers:

5

I have made a small application in C. It does some file manipulation, basically searches and changes some files names. But if it is used in Win Vista/7 in the program files folder the program says permission denied. Is there anyway to bypass this?

Also, can Java bypass this?

+3  A: 

There wouldn't be much point to file permissions if they could be easily bypassed.

You could bypass them if you wrote a kernel driver that accessed disk blocks directly, but then you need to be an Administrator to install kernel drivers anyway.

At the end of the day, the only way to "bypass" file permissions is to run your program as Administrator.

Dean Harding
+3  A: 

Don't bypass, use "Application Data" directory.

Program Files is intended to store files that do not change during run of application. Files stored there should be changed only during installation/update.

Files to be changed during application run (profiles, settings etc.) should be stored "Application Data" directory in users folder.

To obtain the path use SHGetFolderPath function passing:
CSIDL_APPDATA - to obtain current user "Application Data" directory
CSIDL_COMMON_APPDATA - to obtain all users "Application Data" directory
(and eventually) CSIDL_LOCAL_APPDATA - to obtain current user "Local Application Data" directory which is intended to store informations specific only to local machine that cannot be moved to other machines, in opposition to "Application Data" (also called "Roaming Application Data") where files can be freely moved from machine to machine (i.e. you can preserve them during OS reinstallation).

If your application do really need to alter Program Files directory then asking the user for permission is that what you should do. You can give your application special manifest so it'll try to obtain administrator rights every time it's run. You can also obtain privileges on-the-fly.

If you are writing specific application that hardly interferes with the OS, then you can create a service (daemon). Every time the service is started it will obtain privileges given during service registration. For more information see "DLLs, Processes, and Threads -> Services" on MSDN

// EDIT
You can also easily crate a service in .NET (C# is most suitable). Service application can communicate with client application written in other language. But to register a service you will need administrator rights. With this kind of application you must consider all security issues.

adf88
+1. using Program Files only for static code and data has been a best practice at least as far back as XP (maybe the earlier NTs too?). Developers who didn't follow this advice are at least partially responsible for many of the early Vista woes. ;)
Cogwheel - Matthew Orlando
It's since Windows 2000 that normal Users don't have any write right on Program Files and %windir%, as well as on the root directory of the system drive, IIRC. Morover, I'm quite sure that the guidelines about the correct usage of the user profile directory hierarchy date back to earlier NTs, since a correct usage of the profile directory is fundamental for profile roaming in domain environment.
Matteo Italia
+1  A: 

Is there anyway to bypass this?

You should not do that. Even if there is a way, then it is a security hole, and will be fixed eventually.

If your software is running from non-admin account, it should behave properly, and shouldn't try to mess system folders (renaming file in program files is exactly that - potentially damaging the system). That's the whole point of permissions and separation between user and admin - program running from user account should not be able to corrupt or destroy system files, unless administrator permits that.

So if you can't rename files due to permissions, report "permission denied" to user. Let the user handle situation. Silently bypassing permissions (even if it is possible) is extremely dangerous - eventually some not-very-bright user will kill the system using your software AND blame YOU for his problems.

SigTerm
+1  A: 

As many said before, you shouldn't try to bypass restrictions imposed by the Operating System.

If your application needs to change files that only the Administrator can change, your program requires Administrator privileges. Now, if you want to consider this, there are some useful APIs to elevate an application's privileges.

In Vista/Win7/Server2k8, for instance, you can use the UAC. I'm not citing the specific APIs here because there are different ways to elevate or gain privileges through the UAC. An important thing to note is that UAC relies on the user interaction for granting this, and this is a positive thing :-)

Hopefully, this will guide you, and others as well, to the right direction.

jweyrich
A: 

I'm sorry for the confusion but that's not what I meant. What I want to bypass is the need to "Run as Administrator" even if you're in the admin acc. You see, UAC sucks, in the way that even if you're admin you have to run stuff as admin. So the problem goes like this: I have a exe file that's being called from a bat file. Everything should go smoothly if you're admin, but it doesn't. You have to check "Run as admin" on the exe properties. That's what I want to bypass.

tr-raziel, thanks for the clarification. You should either edit your question or add this clarification as a comment to the question. You have it hear as an answer to your question, and it clearly isn't that.
Oddthinking
yes. Thank you. I did that.