Thread: file does not open!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    file does not open!

    hi all
    i have been trying to work at this all day but no cigar
    the file does not seem to open
    i have used the following function so if the file has root privileges it will open up another file

    Code:
    int file_permission(int fd)
    {
    	struct stat stat_struct;
    	if(fstat(fd, &stat_struct) == -1)
    		return(1);
    	return (int)stat_struct.st_uid;
    }
    the above is the function i used for the code below which will open the file

    Code:
    if( (fdtest = file_permission(fd1)) == 0 )
    {
    	printf("403 error\n");
    	printf("fdtest is %d\n", fdtest);
    	fdtest = open("/home/me/errors/403.html",O_RDONLY,0);
            printf("fdtest is %d\n", fdtest);
    }
    the file is located in the directory. One thing i noticed was fdtest had changed from 0 to 6 after the open() i am not sure if this is meant to happen!

    i am now beginning to believe its a problem with the file_permission function but then again when i use the following code instead it all works fine!

    Code:
    if( (fdtest = file_permission(fd1)) == 0 )
    {
    	printf("403 error\n");
    	send_new(fd,"HTTP/1.0 403 Not Found\r\n");
    	send_new(fd,"<html><head><title>403 no permission!! :( </head></title>");
    	send_new(fd,"<body><h1>No permission to view</h1><br><p>Sorry user the url you were searching for is permitted!!</p></body></html>");
    }
    all help would be much appreciated... who knew an open statement would be such a headache
    Kind regards
    Last edited by aadil7; 03-17-2011 at 06:52 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    One thing i noticed was fdtest had changed from 0 to 6 after the open() i am not sure if this is meant to happen!
    That means your open() call was successful. 6 is the value of the file descriptor that open() returned. What makes you think the file was not actually opened?

    i have used the following function so if the file has root privileges...
    That's not what you're doing. What you are doing is checking if the file is owned by root.
    Last edited by bithub; 03-17-2011 at 07:02 PM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    well it did not display on the web browser

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by aadil7 View Post
    well it did not display on the web browser
    You aren't making any sense. What is the code supposed to do?

    Code:
    if( (fdtest = file_permission(fd1)) == 0 )
    {
    	printf("403 error\n");
    	printf("fdtest is %d\n", fdtest);
    	fdtest = open("/home/me/errors/403.html",O_RDONLY,0);
            printf("fdtest is %d\n", fdtest);
    }
    All you are doing is opening a file. That's it. How is the web browser involved in any of this?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    it is a webserver and i dont really understand why it works with the send_new statements and not the open()

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your application is a webserver, or there's a webserver running on the same box?

    Is your application just supposed to write data to the file?
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    my application is a webserver and it is meant to retrieve files from a directory defined in the program
    it is meant to retrieve the webpage and display it and if there is an error it is meant to bring up the error page... also defined in the program as it has its own directory

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by aadil7 View Post
    my application is a webserver and it is meant to retrieve files from a directory defined in the program
    it is meant to retrieve the webpage and display it and if there is an error it is meant to bring up the error page... also defined in the program as it has its own directory
    Gotcha. I really really wish you had put this in your first post.

    So let's look at your code again:
    Code:
    if( (fdtest = file_permission(fd1)) == 0 )
    {
    	printf("403 error\n");
    	printf("fdtest is %d\n", fdtest);
    	fdtest = open("/home/me/errors/403.html",O_RDONLY,0);
            printf("fdtest is %d\n", fdtest);
    }
    Nowhere here are you reading from the file or sending out the socket. Therefore I can't tell you where your problem is. It should probably look something like:
    Code:
    printf("403 error\n");
    printf("fdtest is %d\n", fdtest);
    fdtest = open("/home/me/errors/403.html",O_RDONLY,0);
    printf("fdtest is %d\n", fdtest);
    if(fdtest != 0)
    {
        char buffer[512];
        while(read(fdtest, buffer, sizeof(buffer)) > 0)
        {
            // write buffer out to the socket
        }
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    i have emailed you with regards to this

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't really need your file_permission function. Open will fail if the web server doesn't have permission to access the file. If open fails (returns -1), you can look at the value of errno to determine if this was due to permissions. Check the man pages for open(2) to see all the possible errno values.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    this is the problem ive tested it without the file_permission function and it still displays the page!

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Of course it will display, since it successfully opened the file (your file descriptor was 6). The question should be why can your web server open the file?

    What are the user ID and group ID that your web server is running under? What are the owner, group and permissions of your file? Let's see the output of "ls -l" for both the web server executable and file in question.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    well for the webserver its is me me
    and for index.html its me me
    and for index2.html i changed it to root root
    for the 403.html error file its me me
    would having the webserver as root make a difference because i just want to check if the html is root and if it is then display the 403.html error

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It might make a difference. It depends on owner and group of the web server, as well as the owner, group and permissions on the file it's reading. If it's root:root, but permissions 644, than anybody (including the web server) can read the file. That's why I asked for the output of "ls -l" for all files in question. So I can actually see what's going on, not just guess at the permissions. So again, what is the output of "ls -l" for all files in question. Also read a good tutorial on Linux permissions.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    hmm well would you like to know rwx-rwx-rwx of each of the files in question?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM