Privileged subprocess starts in the wrong working directory
Question:
Im trying to start a few python scripts from another, unprivileged one, using subprocess.Popen. Works good for the most part, but the ones that need root permissions dont work, i have narrowed the problem to pkexec, that is not staying at the working directory nor accepting it as parameter, thats because
This:
subprocess.Popen(['kdesudo','pwd'],cwd=sys.path[0])
#also works with sudo
effectively prints the cwd, whereas:
subprocess.Popen(['pkexec','pwd'],cwd=sys.path[0])
always stays at /root. (also tried passing env=os.environ to no avail)
i need to prompt the user with a gui, and the portability that pkexec offers over kdesudo/gksu. Any ideas?
Edit: Since its not possible to change pkexec’s working directory, the following can be used to prompt the user for the root password across gtk and kde environments:
try:
check_call('which gksu',shell=True)
sudo = 'gksu'
except:
print "gksu frontend not found, using kdesudo instead"
sudo = 'kdesudo'
prompt = Popen([sudo, '<privileged command/script to run>'])
Answers:
There’s no way to get pkexec
to keep the old directory when invoking it’s command because it always changes to the target user’s home directory.
It changes to pw->pw_dir
, which is the home directory of the target user and there’s no override for this.
I can’t see a documented reason for this, but it could be simply a matter of attempting to ensure that the user executing the program can access their current working directory. It’s been there since the creation of pkexec and I don’t see any bugs relating to this behavior.
I run pcmanfm file-manager as privileged user from current dir using pkexec
like this :
pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY pcmanfm $PWD
User must belong to sudo group.
just use --keep-cwd
in newest version of pkexec
Im trying to start a few python scripts from another, unprivileged one, using subprocess.Popen. Works good for the most part, but the ones that need root permissions dont work, i have narrowed the problem to pkexec, that is not staying at the working directory nor accepting it as parameter, thats because
This:
subprocess.Popen(['kdesudo','pwd'],cwd=sys.path[0])
#also works with sudo
effectively prints the cwd, whereas:
subprocess.Popen(['pkexec','pwd'],cwd=sys.path[0])
always stays at /root. (also tried passing env=os.environ to no avail)
i need to prompt the user with a gui, and the portability that pkexec offers over kdesudo/gksu. Any ideas?
Edit: Since its not possible to change pkexec’s working directory, the following can be used to prompt the user for the root password across gtk and kde environments:
try:
check_call('which gksu',shell=True)
sudo = 'gksu'
except:
print "gksu frontend not found, using kdesudo instead"
sudo = 'kdesudo'
prompt = Popen([sudo, '<privileged command/script to run>'])
There’s no way to get pkexec
to keep the old directory when invoking it’s command because it always changes to the target user’s home directory.
It changes to pw->pw_dir
, which is the home directory of the target user and there’s no override for this.
I can’t see a documented reason for this, but it could be simply a matter of attempting to ensure that the user executing the program can access their current working directory. It’s been there since the creation of pkexec and I don’t see any bugs relating to this behavior.
I run pcmanfm file-manager as privileged user from current dir using pkexec
like this :
pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY pcmanfm $PWD
User must belong to sudo group.
just use --keep-cwd
in newest version of pkexec