1

I have a simple html file which accepts the repository path as argument and passes it over to the python cgi script, which then clones the repository using the below command..

pr = subprocess.Popen(['/usr/bin/git clone ' + str(repoPath)],
   cwd=os.path.dirname('/clone/here/'),
   stdout=subprocess.PIPE,
   stderr=subprocess.PIPE,
   shell=True)
(out, error) = pr.communicate()
print out
print error

wherein, repoPath is the variable whose value i read from a html form. When i execute the above script, i get the below error

fatal: could not create work tree dir 'repository'.: Permission denied

But when i create a python script and directly run the above command, it works fine. Am i missing something here?

1
  • It seems that the commands in the subprocess.popen function are getting called as 'apache' user, since the cgi files are deployed in apache. Any idea how to create files/folders as non-apache user? Commented Oct 15, 2013 at 12:06

2 Answers 2

1

The directory already exists, and is owned by a different user (most likely your shell login if you tested manually).

What you're doing is really unsafe. Instead you should create a unique directory and clone in there. I found this example tempdir function. (Note: there's a risk of endless loop if you can't write to $dir)

That way you will avoid any name collisions.

Sign up to request clarification or add additional context in comments.

5 Comments

hmmm.. makes sense, will try out the solution and will let u know.. :)(
tried creating a dir using mkdir inside the cgi script. Its throwing "permission denied error.". Can you pls explain why this error is popping up and how it works when i try running only the python script from command line?
Your web server process most likely doesn't have write permissions to the given directory in that case.
so how do i run commands as a different user from the cgi script?
You don't, fix the permissions instead, or use a different directory. A typical permission for a temp directory is 1777, chmod 1777 dir
1

Should be doing something like:

subprocess.Popen(['git', 'clone', str(repoPath), '/clone/here'])

I don't really understand what is the deal with the cwd statement you have there, but you need to close into the right location, otherwise the cwd could be anything, like '/'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.