Solved

Cannot write to a text file in my cgi-bin folder

Posted on 2008-06-16
16
670 Views
Last Modified: 2013-12-25
Im going crazy guys.

I have a file called hi.txt that I have placed in my cgi-bin folder.

If I place this file in any other location thatn the cgi-bin folder, I can write to it.

I just cant if it is placed in my cgi-bin folder.

I have checked that this file has chmod 0777 permissions, along with the cgi-bin folder itself.

Is this an apache server config issue?

I have changed httpd.conf to include the following...

    569 <Directory "/var/www/cgi-bin">
    570     AllowOverride None
    571     Options +ExecCGI
    572     AddHandler cgi-script .cgi
    573     Order allow,deny
    574     Allow from all
    575 </Directory>

Any help greatly appreciated.

I can execute cgi scripts from this folder, but cannot write to them.

[root@localhost www]# ls -ltra cgi-bin
total 1788
-rwxrwxrwx 1 root   root   370997 Jun 10 18:13 filechucker.cgi
-rwxrwxrwx 1 root   root   370996 Jun 16 04:56 filechucker_focushere.cgi
-rwxr-xr-x 1 root   root      276 Jun 16 06:19 focushere_functions.php
-rwxrwxrwx 1 root   root   150127 Jun 16 12:50 filechucker_focushere_prefs.cgi
drwxrwxrwx 3 root   root     4096 Jun 16 12:52 menu4
drwxrwxrwx 2 root   root     4096 Jun 16 13:17 templates
-rwxr-xr-x 1 root   root   370997 Jun 16 17:29 filechucker.cgi.backup
-rwxr-xr-x 1 root   root   149742 Jun 16 17:29 filechucker_prefs.cgi.backup
-rwxr-xr-x 1 root   root   151466 Jun 17 08:19 filechucker_prefs.cgi.latest
drwxrwxrwx 8 apache apache   4096 Jun 17 08:39 ..
-rwxrwxrwx 1 root   root   151466 Jun 17 08:43 filechucker_prefs.cgi
-rwxrwxrwx 1 root   root       71 Jun 17 09:45 hi.txt
drwxrwxrwx 4 apache apache   4096 Jun 17 09:45 .



0
Comment
Question by:Simon336697
16 Comments
 
LVL 4

Expert Comment

by:trippy1976
Comment Utility
hi.txt is owned by root - what user does your PHP process run as?  I.e. if you look at a similar file listing in another directory - is the owner something like "WEB" or still "root"?  If your PHP process does not have root permissions that may be the issue.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Hi trippy1976,
Mate thank you for your help on this im going nuts.

For this to work, should I be able to do a directory listing through a browser for the cgi-bin folder?

At the moment, i cant do that, even if change the cgi-bin folder to be owned by apache.

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Other folders are owned by the same user.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Is the owner of the php process the same owner who runs apache?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
Comment Utility
Show us the code that is supposed to write to it and the error message you are getting when it fails.
0
 
LVL 4

Accepted Solution

by:
trippy1976 earned 300 total points
Comment Utility
I'd write out a test file like test.txt to some directory other than cgi-bin and then go on ssh and do an ls -al in that dir to see who owns the created file.  If the user is not root, that's probably the issue - php does not have root access.

So then you need to ssh in as root and chmod the hi.txt file to the group/user of the php process.

Can you give info about where you are hosting this too?  What kind of hosting?  Is this your own server, do you have root, etc?

If it's a paid hosting package, call your hosting provider and confirm whether you have can get write access into cgi-bin.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Hi trippy, guys,

It is my own server.

Ive tried changing the owner from:

chown -R root.root cgi-bin
chown -R apache.apache cgi-bin

Both dont work on the cgi-bin folder with regards to wrting to hi.txt.
But as soon as I try to write on hi.txt in a different location, it works.

In my question, you can see I have already done an ls on the cgi-bin folder :>)

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Is the cgi-bin a special folder where different permissions are set on this?

I was thinking there might be a problem with the cgi-bin folder in terms of apache and the httpd.conf file that Im overlooking perhaps?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Tintin, the code is php code that writes to a txt file in the cgi-bin folder.
Im getting a Permission denied writing to it.
But the folder and files within the fodler are owned by root....
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 100 total points
Comment Utility
is it possible the file is open by another user at the time?
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Adam, just testing your theory now....rebooting my server.

Im wondering guys if it could be because in my httpd.conf file, i have the following setup...

Document Root  =  /var/www/html

my cgi-bin folder =  /var/www/cgi-bin

The cgi-bin folder is OUTSIDE the document root.

Could this be it?

0
 
LVL 4

Expert Comment

by:trippy1976
Comment Utility
As far as I know, given an appropriate path, PHP can write a file anywhere on the server.

If you are getting a permissions issue, I think that is it.

What I suggested was to write a file NOT in the cgi-bin directory and ls that OTHER directory to see what permissions and owners are set.  You should be able to replicate the same.

Have the PHP script write to a path like:
/root/mytest/hi.txt

Then
cd /root/mytest
ls -al

See what the permissions are.

What is the need to have the file in cgi-bin?  Have you tried maybe a symlink or other method so that the "file" in cgi-bin is a symlink to file in some alternate, less problematic area?

Also go back one level in your directories and see what the permissions / owners are on the cgi-dir.  That could be a snag too.

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Adam, no luck.
Im checking the apache logs and nothing recorded for it.

View source errors are:

<b>Warning</b>:  fopen(../../cgi-bin/hi.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied in <b>/var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php</b> on line <b>42</b><br />
<br />
<b>Warning</b>:  fwrite(): supplied argument is not a valid stream resource in <b>/var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php</b> on line <b>43</b><br />
<br />
<b>Warning</b>:  fclose(): supplied argument is not a valid stream resource in <b>/var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php</b> on line <b>44</b><br />

Tintin, here is my code....

=========================== /var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php
See attach code snippet

If I try and go to the following page...

http://192.168.2.100/cgi-bin/
You don't have permission to access /cgi-bin/ on this server.




	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.oxhtml1/DTD/xhtml1-strict.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

 <head><!-- Put IE into quirks mode -->

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 <link rel="stylesheet" type="text/css" href="includes/menu.css" />

 <?php

 include("includes/sitewide.php");

 ?>

 <title><?php echo $title ?></title>
 

 <?php
 

 function getFolderPath($filename) {

    $fileparts = explode('_', $filename);

    return '/simeswiki/'.implode(array_slice($fileparts, 1, count($fileparts)-2));

 }
 

 ?>
 

 <?php

 // Part A:

 //Get the folderPath variable for input into /cgi-bin/filechucker_prefs.cgi

 $folderpath = getFolderPath($_SERVER['SCRIPT_FILENAME']);

 ?>
 

 <?php

 // Part B:

 // With $folderpath, replace in /cgi-bin/filechucker_prefs.cgi
 

 // $lines = file('/var/www/cgi-bin/filechucker_prefs.cgi');
 

 $lines = file('/var/www/cgi-bin/hi.txt');

 foreach($lines as $index => $line) {

   if(trim($line) == '# ooPLACEHOLDERoo #') {

      $lines[$index+1] = '$PREF{uploaded_files_dir} = \''.$folderpath."'\n";

      break;

   }

 }
 

 // $f = fopen('/var/www/cgi-bin/filechucker_prefs.cgi','w');
 

 $f = fopen('/var/www/cgi-bin/hi.txt','w');

 fwrite($f, implode('', $lines));

 fclose($f);
 

 ?>
 

 </head>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Trippy,
Thank you mate.
I have done what you said, and the owner was root, and on the parent directories, as they are for cgi-bin.
I think im having an issue writing to the cgi-bin folder through a web page when the cgi-bin folder is outside the document root, but not sure what security other than having the owner of cgi-bin as root, or apache, and its parent folders.
Im stumped.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Here is my httpd.conf file (relevant parts )
DocumentRoot "/var/www/html"
 

#

# Each directory to which Apache has access can be configured with respect

# to which services and features are allowed and/or disabled in that

# directory (and its subdirectories). 

#

# First, we configure the "default" to be a very restrictive set of 

# features.  

#

<Directory />

    Options FollowSymLinks

    AllowOverride None

</Directory>
 

#

# Note that from this point forward you must specifically allow

# particular features to be enabled - so if something's not working as

# you might expect, make sure that you have specifically enabled it

# below.

#
 

#

# This should be changed to whatever you set DocumentRoot to.

#

<Directory "/var/www/html">
 

#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    Options Indexes FollowSymLinks
 

#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride None
 

#

# Controls who can get stuff from this server.

#

    Order allow,deny

    Allow from all
 

</Directory>
 

#

# UserDir: The name of the directory that is appended onto a user's home

# directory if a ~user request is received.

#

# The path to the end user account 'public_html' directory must be

# accessible to the webserver userid.  This usually means that ~userid

# must have permissions of 711, ~userid/public_html must have permissions

# of 755, and documents contained therein must be world-readable.

# Otherwise, the client will only receive a "403 Forbidden" message.

#

# See also: http://httpd.apache.org/docs/misc/FAQ.html#forbidden

#

<IfModule mod_userdir.c>

    #

    # UserDir is disabled by default since it can confirm the presence

    # of a username on the system (depending on home directory

    # permissions).

    #

    UserDir disable
 

    #

    # To enable requests to /~user/ to serve the user's public_html

    # directory, remove the "UserDir disable" line above, and uncomment

    # the following line instead:

    # 

    #UserDir public_html
 

</IfModule>
 

#

# Control access to UserDir directories.  The following is an example

# for a site where these directories are restricted to read-only.

#

#<Directory /home/*/public_html>

#    AllowOverride FileInfo AuthConfig Limit

#    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

#    <Limit GET POST OPTIONS>

#        Order allow,deny

#        Allow from all

#    </Limit>

#    <LimitExcept GET POST OPTIONS>

#        Order deny,allow

#        Deny from all

#    </LimitExcept>

#</Directory>
 

#

# DirectoryIndex: sets the file that Apache will serve if a directory

# is requested.

#

# The index.html.var file (a type-map) is used to deliver content-

# negotiated documents.  The MultiViews Option can be used for the 

# same purpose, but it is much slower.

#

DirectoryIndex index.html index.html.var
 

#

# AccessFileName: The name of the file to look for in each directory

# for additional configuration directives.  See also the AllowOverride

# directive.

#

AccessFileName .htaccess
 

#

# The following lines prevent .htaccess and .htpasswd files from being 

# viewed by Web clients. 

#

<Files ~ "^\.ht">

    Order allow,deny

    Deny from all

</Files>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
trippy1976, Tintin, Adam314

Guys thanks so much for your help.

The problem with writing to the cgi-bin folder was the following.....

From a command line, I ran the following...

# getenforce
Enforcing

My redhat server turns out that it is running selinux in enforcing mode.

As soon as I did the following.....

# setenforce 0
then
#getenforce
Permissive

and then service httpd restart.

I could write the cgi-bin folder

Thanks to all of you.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 26
while loop in html mail format 5 32
php connect() failed error 25 12
File not deleting using unlink 6 0
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now