Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Restricting File Access

Restricting File Access

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marko B L
    wrote on last edited by
    #1

    Hi, I'm currently writing a small server application for data transfer. Two commands are for direct file transfer (relative path, e.g. test/data.dat) and I want to limit the filenames to be in the directory and subdirectories of the server application. Let's say the server lies in c:\server\server.exe, now I want to be able to e.g. put a file into c:\server\new folder\test.dat, but not outside the server directoy, e.g. c:\test.dat (e.g. with a ../test.dat or new folder/../../test.dat). Is there a good way to parse bad filenames/path? Greetings

    S D K 3 Replies Last reply
    0
    • M Marko B L

      Hi, I'm currently writing a small server application for data transfer. Two commands are for direct file transfer (relative path, e.g. test/data.dat) and I want to limit the filenames to be in the directory and subdirectories of the server application. Let's say the server lies in c:\server\server.exe, now I want to be able to e.g. put a file into c:\server\new folder\test.dat, but not outside the server directoy, e.g. c:\test.dat (e.g. with a ../test.dat or new folder/../../test.dat). Is there a good way to parse bad filenames/path? Greetings

      S Offline
      S Offline
      Sam Hobbs
      wrote on last edited by
      #2

      It is not clear what you need to do. I don't know what a "bad filenames/path" is. I am not sure if you must recognize incorrect syntax of if you need to allow access to something and not allow access to other things. File access is typically controlled using the built-in features of the NTFS.

      1 Reply Last reply
      0
      • M Marko B L

        Hi, I'm currently writing a small server application for data transfer. Two commands are for direct file transfer (relative path, e.g. test/data.dat) and I want to limit the filenames to be in the directory and subdirectories of the server application. Let's say the server lies in c:\server\server.exe, now I want to be able to e.g. put a file into c:\server\new folder\test.dat, but not outside the server directoy, e.g. c:\test.dat (e.g. with a ../test.dat or new folder/../../test.dat). Is there a good way to parse bad filenames/path? Greetings

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Per your subject, you need to read up on Access Control List (ACL). There's a whole API for it.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • M Marko B L

          Hi, I'm currently writing a small server application for data transfer. Two commands are for direct file transfer (relative path, e.g. test/data.dat) and I want to limit the filenames to be in the directory and subdirectories of the server application. Let's say the server lies in c:\server\server.exe, now I want to be able to e.g. put a file into c:\server\new folder\test.dat, but not outside the server directoy, e.g. c:\test.dat (e.g. with a ../test.dat or new folder/../../test.dat). Is there a good way to parse bad filenames/path? Greetings

          K Offline
          K Offline
          kakan
          wrote on last edited by
          #4

          One way, if youv'e got absolute paths, just compare them as strings/char arrays. You can get absolute paths by using _fullpath() or _wfullpath. Then you can do the compare, something like this (beware! Old C syntax here): NOTE! I didn't find the time to set current directory to c:\server before testing the code, so I made a full path name for goodRelPath. Else, I could have used a relative path for goodRelPath. char * projectDir = "c:\\server\\"; char * goodRelPath = "c:\\server\\new folder\\test.dat"; char * badRelPath = "c:\\test.dat"; char tempFullPath[MAX_PATH]; // Test badRelPath (will fail) _fullpath(tempFullPath, badRelPath, MAX_PATH ); if(strstr(tempFullPath, projectDir) == projectDir) { // OK, GO! } else { // Error, no go. } // Test goodRelPath, (OK) _fullpath(tempFullPath, goodRelPath, MAX_PATH ); if(strstr(tempFullPath, projectDir) == projectDir) { // OK, GO! } else { // Error, no go }
          -- modified at 0:43 Tuesday 12th December, 2006 Better code...

          Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

          M 1 Reply Last reply
          0
          • K kakan

            One way, if youv'e got absolute paths, just compare them as strings/char arrays. You can get absolute paths by using _fullpath() or _wfullpath. Then you can do the compare, something like this (beware! Old C syntax here): NOTE! I didn't find the time to set current directory to c:\server before testing the code, so I made a full path name for goodRelPath. Else, I could have used a relative path for goodRelPath. char * projectDir = "c:\\server\\"; char * goodRelPath = "c:\\server\\new folder\\test.dat"; char * badRelPath = "c:\\test.dat"; char tempFullPath[MAX_PATH]; // Test badRelPath (will fail) _fullpath(tempFullPath, badRelPath, MAX_PATH ); if(strstr(tempFullPath, projectDir) == projectDir) { // OK, GO! } else { // Error, no go. } // Test goodRelPath, (OK) _fullpath(tempFullPath, goodRelPath, MAX_PATH ); if(strstr(tempFullPath, projectDir) == projectDir) { // OK, GO! } else { // Error, no go }
            -- modified at 0:43 Tuesday 12th December, 2006 Better code...

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            M Offline
            M Offline
            Marko B L
            wrote on last edited by
            #5

            Thank you for your help and the example. Great :)

            K 1 Reply Last reply
            0
            • M Marko B L

              Thank you for your help and the example. Great :)

              K Offline
              K Offline
              kakan
              wrote on last edited by
              #6

              I'm glad to be able to help you. I have a faint memory of using this technique in a program system in the past (15 years ago, or even longer). It was in the DOS days, and there was a DOS call to convert a relative path to a canonical path name. Well, the principle worked then, and should do so today. Good luck. Kakan

              Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups