Python CGIHTTPServer crashes with "OSError: [Errno 13] Permission denied"
Asked Answered
C

4

9

I am running the following command from my home directory:

python -m CGIHTTPServer

This runs the server, but when I try to access a script in the cgi-bin directory I get:

Traceback (most recent call last):
  File "/usr/lib/python2.7/CGIHTTPServer.py", line 251, in run_cgi
    os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied

Running as root does not make a difference. The files seem to have all the right permissions:

student@bandersnatch:~$ ls -lhR
.:
total 12K
drwxr-xr-x 2 student student 4.0K Jun 13 18:38 cgi-bin
drwxr--r-- 2 student student 4.0K Jun 10  2004 kalpy
-rwxrwxrwx 1 student student 2.0K Jun 13 12:37 test.html

./cgi-bin:
total 8.0K
-rwxr-xr-x 1 student student 31 Jun 13 18:38 test.py

Edit: The content of test.py is:

#!/usr/bin/python
print "test"

The shebang is valid:

~$ which python
/usr/bin/python
Chronological answered 13/6, 2012 at 18:45 Comment(2)
It would be useful to see the shebang line from test.py. That error may arise if the defined interpreter is not a valid executable.Voyeur
Thanks @rodrigo, I edited my question to include this information.Chronological
V
11

Are you, by any chance, running the process as root?

If you use the source, you will see in CGIHTTPServer.py, just before calling execve:

try:
    os.setuid(nobody)
except os.error:
    pass

That is, it will run the CGI script as nobody, if it is able to change the UID, that is if it is root. If it is not root, this call will most likely fail, and pass on.

So my guess is that you are running the server as root, so the script is run as nobody, but this user doesn't have access to the script. Which is expected, as you say that it is in your home dir.

Two solutions that I can think of:

  • The recommended: do not run the server as root!
  • The workaround: copy the script to a directory where nobody can read it (/tmp for example).
Voyeur answered 13/6, 2012 at 21:55 Comment(0)
K
1

Personally, unless there's some reason I'm unaware of, I'd recommend using subprocess.Popen instead of os.execve. I have run into Errno 13 before, trying to start a .app with Popen(['open execName.app']). I had to use Popen(['execName.app/Contents/MacOS/execName', 'arg1', 'arg2'...]) instead. Don't know if that helps, but give it a shot.

Kele answered 13/6, 2012 at 19:46 Comment(1)
I'm not sure I follow you, @Kele , are you suggesting I patch CGIHTTPServer?Chronological
E
1

I ran into the same problem from ubuntu Linux. Followed the solution by "Mike", with modification. Instead doing chmod of the "/usr" which has several folders, change the permissions of the folder containing executable file that was denied. (you can check that server would run fine when loading a static html file in the same location, and shows error only when script is run).

cd /pathto/folder/with/deniedscript
sudo chmod -R 755 ./

Now the script has permission, so should run fine. Note that -R gives the permission to all files in this folder(and subfolders if any).

Electrograph answered 4/9, 2016 at 14:15 Comment(0)
D
0

When running on Windows the files run right out of the command prompts.

For Linux and Windows users that's not the case!

I get the following error:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/server.py", line 1158, in run_cgi os.execve(scriptfile, args, env) PermissionError: [Errno 13] Permission denied:

You'll need to the following to resolve these issues:

For Linux Users:

1) Ensure shebang is adjusted for Python 3 running on Linux and Mac OSX systems:

#!/usr/bin/env python3

2) Since the original executable files were written on windows they'll have hidden '\r' in the files that must be removed. Here are three possible ways: a) In terminal command line type: tr -d ‘\r’ < input file name > output file name (just rename the output file a new name --> erase old file --> then rechange output filename back to original) b) In terminal command line type: cat inputfile | col -b > outputfile (just rename the output file a new name --> erase old file --> then rechange output filename back to original) c) Download dos2unix, then type in terminal command line: dos2unix input file name

3) Make file executable: In terminal command line type: a) chmod 755 filename or b) chmod +x filename or chmod a+x filename

For Mac OSX users it's almost the same:

  1. Repeat step 1) from Linux
  2. Repeat step 2) from Linux

For step 3 things change:

Based on the apache.org wiki page: https://wiki.apache.org/httpd/13PermissionDenied It says that you have to make every executable from file location traversing all the away up to the /Users root directory.

You'll have to do the following.

3) In terminal command line:

a) type command: `cd /Users`
b) type command: `sudo chmod -R 755`

Now you can run your server .py file via:

sudo webserver.py

and the input file via normal:

python3 inputfile.py

Now you should be all good to go with no more permission errors! You can make necessary adjustments to shebang and command line if running python 2.

Dade answered 18/3, 2015 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.