Python exec permission denied

Python exec() Permission Denied in Apache server: /cgi_bin/*.py

I have 2 python env in my server. One path is /usr/bin and other one is the anaconda path as shown below. I am getting Permission denied error when I try executing a basic Python script on my Apache server. The python interpreter path in my .py file is as follows

When my python interpreter path in .py is changed to i.e #!/usr/bin it works well. The problem is I have installed all my packages in /home/cloudera/anaconda3/bin I tried setting this path to be accessible to execute in apache server by adding the below line of code in /etc/httpd/conf/httpd.conf file as follows

  AllowOverride None Options ExecCGI AddHandler cgi-script .py #None Order allow,deny Allow from all 

I still get Permission Denied. It still does not work. I am new to cgi, any help would be really grateful. Thanks in advance.

Ha, I thought this was related to privilege separation. but yes, calling exec on a dir as an interpreter would also get EACCESS . Should have read it more carefully. 🙂

«When my python interpreter path in .py is changed to i.e #!/usr/bin it works well.» I doubt it. please review those comments, add «python» and see what happens

@Jean-FrançoisFabre the changes to python interpreter path #!home/cloudera/anaconda3/bin to #. /bin/python seems to be same, Thanks 🙂

@OndrejK. I understand the error message by your response, but i am not sure how to access the path, the configuration was just a trial, I am not sire about it, any leads would be really helpful.

Читайте также:  Android studio java lang nullpointerexception

1 Answer 1

Admittedly I’ve completely missed the fact that the interpret was specified as directory and the actual binary file as pointed out by Jean-François Fabre. That would indeed cause the same error. But since the OP fixed that and still got the same error, I am undeleting the answer extended as requested.

Also come to think of it, it may be more of a Server Fault question as it would appear.

This error ( 13: EACCES ) is raised by and propagated from the OS.

HTTPd servers usually employ privilege separation and once bound to a reserved port drop their EUID to an unprivileged user. Usually a dedicate one or nobody (on my system it would be user apache , group apache ). You should be able to find these in your httpd.conf under User and Group resp.

Make sure this user has access and can execute the binary you’re calling.

Give you’ve fixed the interpreted (and the file would execute on its own). Go find your httpd.conf file (I think different distros may package it differently, on mine it would reside here: /etc/httpd/httpd.conf ). You will find User and Group entries there. You must be able to access and execute the script (incl. the interpreter) as this user, because that’s how Apache will try to call it. I.e. this user (or group) must be able to access this file (along the complete path) and execute it. If for instance /home/cloudera/ us restricted to ( 0700: rwx—— ) and owned by user other then the one listed int your httpd.conf (very likely), OS would prevent your Apache user to access and execute the file resulting in EACCES error you are seeing. The easiest might be to become that user and see how far your access goes, but you’d need to temporarily tweak few bits for that as the Apache account likely is locked and has not shell set (or rather /bin/false as shell).

One more thing, but I hope it’s not as obvious as that. The corresponding user must have execute permission on the script itself.

Источник

PHP exec(python.py), permission denied writing file

I’m trying to execute a python script using the exec() function of PHP. The .php page is loaded over a browser:

# -*- encoding: UTF-8 -*- import os import httplib2 import io import sys from httplib2 import Http from oauth2client import client from oauth2client import tools from apiclient import discovery from oauth2client.file import Storage from apiclient.discovery import build from oauth2client.client import OAuth2WebServerFlow from apiclient.http import MediaIoBaseDownload from oauth2client.service_account import ServiceAccountCredentials scopes = ['https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.from_json_keyfile_name('/opt/scripts/full-patator-preprod.json', scopes) delegated_credentials = credentials.create_delegated('#############') http_auth = delegated_credentials.authorize(Http()) drive_service = build('drive', 'v2', http=http_auth) def get_file(service,fileId): file = service.files().get(fileId=fileId).execute(http=http_auth); return file item = get_file(drive_service,"#############") print(item.get('title')) delegated_credentials = credentials.create_delegated("#############") http_auth = delegated_credentials.authorize(Http()) request = drive_service.files().export_media(fileId="#############", mimeType='application/pdf') fh = io.FileIO("api/test.pdf","wb") downloader = MediaIoBaseDownload(fh, request) done = False counter=0; while done is False: status, done = downloader.next_chunk() print "Download %d%%." % int(status.progress() * 100) counter+=1 if counter > 10: done=True with open("test.pdf") as f: ## PDF File print(type(f)) ## Open file is TextIOWrapper bw=io.TextIOWrapper(fh) ## Conversion to TextIOWrapper print(type(bw)) ## Just to confirm 

Everything works if execute the .py or the .php as Apache user. But using my browser, when it comes to write file:

Traceback (most recent call last): File «/var/www/cgi-bin/api/download.py», line 46, in fh = io.FileIO(«test.pdf»,»wb») IOError: [Errno 13] Permission denied: ‘test.pdf’

Well, i was pretty sure that it was due to the permission of the parent folder, then the permission of the parent of the parent. But i set a 777 permission to these folders and it did nothing. My php script is in /var/www/html/manager/execute.php My .py script is in /var/www/cgi-bin/api/download.py folders /www/, /cgi-bin/, /api/ has 777 rights. I know it’s not a good practice but i would like to solve the issue before doing something cleaner. Thanks in advance

Источник

Python CGIHTTPServer crashes with «OSError: [Errno 13] Permission denied»

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.

4 Answers 4

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).

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.

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).

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:

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

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:

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:

and the input file via normal:

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.

Источник

Errno 13 Permission denied

In python, I am currently experimenting with what I can do with open command. I tried to open a file, and got an error message. Here’s my code:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\****\\Desktop\\File1' 

I looked on the website to try and find some answers and I saw a post where somebody mentioned chmod . 1. I’m not sure what this is and 2. I don’t know how to use it, and thats why I’ve come here.

6 Answers 6

For future searchers, if none of the above worked, for me, python was trying to open a folder as a file.

Check at the location where you try to open the file, if you have a folder with exactly the same name as the file you try to open (the file extension is part of the file name).

And that will perhaps also solve the question. It seems as if C:\Users\****\Desktop\File1 is written as a directory path, not with actually as a file path, since there is no file extension.

Your user don’t have the right permissions to read the file, since you used open() without specifying a mode.

Since you’re using Windows, you should read a little more about File and Folder Permissions.

Also, if you want to play with your file permissions, you should right-click it, choose Properties and select Security tab.

Or if you want to be a little more hardcore, you can run your script as admin.

SO Related Questions:

If nothing worked for you, make sure the file is not open in another program. I was trying to import an xlsx file and Excel was blocking me from doing so.

The problem here is your user doesn’t have proper rights/permissions to open the file this means that you’d need to grant some administrative privileges to your python ide before you run that command.

As you are a windows user you just need to right click on python ide => select option ‘Run as Administrator’ and then run your command.

And if you are using the command line to run the codes, do the same open the command prompt with admin rights. Hope it helps

Источник

Оцените статью