Prevent access (FILES)

This is a discussion on Prevent access (FILES) within the C Programming forums, part of the General Programming Boards category; humm does it possible to prevent access for editing a text file? I am talking about access for writing not ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Prevent access (FILES)

    humm does it possible to prevent access for editing a text file?
    I am talking about access for writing not about erasure of the file.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, make the file read-only, and make sure the user that would want to delete the file is different from the file owner, and that the user hasn't got rights to change the files protection.

    How you do that depends on which OS you are using, and of course has nothing (directly) to do with programming in C (although if you plan on doing this in C, you may want to tell us which OS it is you are using, and this post can be moved to the relevant OS discussion group).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    To deny access to a single file, have it opened exclusively in your program.
    So long as you don't close the file, no other applications can access the file.
    (It isn't fool proof, because it's possible to terminate your program and get access to the file.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    To deny access to a single file, have it opened exclusively in your program.
    So long as you don't close the file, no other applications can access the file.
    (It isn't fool proof, because it's possible to terminate your program and get access to the file.)
    I took the OP to want to prevent a file from being edited even though the OP doesn't want to hold the file open - could have it wrong however.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    I don't know. It's a little ambiguous about that issue.
    Anyway, there are two solutions. The OP can simply pick one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Maybe you both stop using acronyms? XD
    Look I have a file with highscore (and name) and I don't want the users to change it manually so I must prevent access to the file (no matter if the game is running or not) or I should encrypte the score and the name (adding more rows) so I prefer not doing this.
    And I am using XP (unfortunately).
    gavra.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    I don't think you need to be that ruthless, but if you want to protect it if not running your program, set it to read-only (will prevent modifying it) or use file permission security to prevent opening/modifying file (requires NTFS file system).
    The former is easy. The latter is difficult.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Maybe you both stop using acronyms? XD
    Look I have a file with highscore (and name) and I don't want the users to change it manually so I must prevent access to the file (no matter if the game is running or not) or I should encrypte the score and the name (adding more rows) so I prefer not doing this.
    And I am using XP (unfortunately).
    Sorry, no can do. If your application can open the file, then the user can, because your application will be running under that users privileges - so anything your app can do, the user can do with another application (e.g. notepad).

    Storing the data in an encrypted form, binary form or checksumming the information would reduce the likelyhood of anyone cheating this way.

    Of course, the only one the user is cheating is him/herself unless this is a commercial game, so it's not, in my view, particularly important.

    You could of course make the file read only and/or hidden (and change it's attribute when you are about to open the file, then change it back when you close the file), then it would require the user to undo the read-only bit before being able to save the file - but unlikely to prevent anyone with more than a couple of weeks experience with computers.

    Here's the Windows API for SetFileAttribute

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    So I can or not?
    XD
    thanks anyway
    gavra.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    I mentioned two options. The latter being somewhat unreasonable because your app would be denied access, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    So I can or not?
    XD
    thanks anyway
    You can't prevent the user from accessing the file - you can make it more difficult to find and edit by hiding the file (so the user won't know it's there, just like you may or may not know that there is a boot.ini file in your C:\ directory - if you didn't know it was there, you wouldn't try to modify it, right?). Or you can make the file read-only - then the user can see the file, but won't be able to edit it without changing it's attributes.

    Neither would stop me from modifying the file if I wished to do so, since I know how to unhide a file and also how to un-read-only a file (and no, I'm not talking about any clever tricks, just tools that are delivered with the Windows XP installation by default, such as notepad and widnows explorer or the attrib.exe that is available from command line). Anyone reasonably familiar with Windows would be able to bypass this sort of thing.

    To more thoroughly prevent modification, you could add a checksum that is at the end of the file - if the checksum of the file content doesn't match the stored checksum, then the file is rejected. This means that the user would have to figure out how to calculate the chosen checksum - which may be quite tricky if the user doesn't even know what the checksum calculation is, and even if it's known, the user would have to be able to write a program of some sort that can regenerate the checksum. CRC-32, MD5sum are two potential candidates here.

    Or you can encrypt the entire file content.

    Or you could store the data not in a text file, but as binary. That will prevent MOST people from messing with the file, because you can't just load it in a text editor and edit it, but it wouldn't stop me for very long, as I know how to use a hex editor (or write a small program that reads/writes the relevant byte(s) of a binary file).

    An additional trick would be to pad the actual data with some other garbage, e.g. random numbers to hide where the actual data is being held. That way, it makes the finding out what is the valid data in the file.

    Storing the data in a "disorganized fashion" (e.g. you first copy your data to a large array, then use rand() with a set srand() seed to "jumble" the data - since you use a set srand() seed [or store the seed in the data file], you are guaranteed to get the same random number sequence next time around, so you can restore the data into correct order.

    To make this more efficient, you may want to store some more random garbage in the file, so that it's always a fixed size [or a multiple of a fixed block size].

    You can of course combine any and all of the above. And encrypted, checksummed, binary content (using sufficiently strong encryption) would stop most people from tampering with the file. Encrypted files are quite difficult to deal with as you need to first decrypt the encryption to be able to figure out what and where the content is held.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Users can range from Genius's to total morons.
    The morons won't even know what a file is, let alone how to change it; but the genius's will always be able to find a way (if they actually care enough about it to waste their time trying).
    The best you can do is encrypt the file to prevent 99% of people from modifying it.

  13. #13
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    First option - how can I hide the file?
    Second - how can I make the file for read only?
    "disorganized fashion" - I thought about this way and maybe I'll implement this way.
    Binary seems to be cool.
    Now I have so many options so I have to think over..
    thank you all specially for you mats.
    gavra.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    First option - how can I hide the file?
    Second - how can I make the file for read only?
    "disorganized fashion" - I thought about this way and maybe I'll implement this way.
    Binary seems to be cool.
    Now I have so many options so I have to think over..
    thank you all specially for you mats.
    I already gave a link to SetFileAttributes, which can be used to set the hidden/read-only flags for the a file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Yep I have seen it but I keep asking my self "WTF?"
    Sorry it's just I didn't understand where is the function and how do I use it..
    gavra.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Access Files...ugh.
    By equss89 in forum C Programming
    Replies: 5
    Last Post: 07-21-2008, 02:25 PM
  2. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  3. How to Access a Function from Multiple Files?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2002, 06:24 PM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 08:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21