Escalating privileges in Python Script

I am looking for a way to escalate privileges in a python GUI application that I am developing. My desired model is GUFW. I love how you don't need to run the main program as root, but when you do something "rooty" you need to "unlock" it.

I have a program where I am developing text files that need to be stored either in the user's home directory tree or in a directory under the / usr directory (which needs the height of the root).

I don't want to run the program as root, but invoke root access when / if I need to save the file for a user other than the user running the program (in this case, root).

I don't want to save it to temporary locations and fall back to calling (ba) sh, for example:

os.system('gksu cp /tmp/myfile /usr/systemdir/myfile')


I would like to do this programmatically in Python if possible. In the future, I may need operations that don't just create the file, but may need some additions in the middle of the file.

Also, after this save operation is complete, I want the identity to be uninstalled.

Can anyone point me to a tutorial or some sample code for this endevour?




source to share

1 answer

You want PolicyKit - the FreeDesktop specification and software to do just that. is a tutorial and sample code for using this with Python (although I haven't personally used PolicyKit, I can't say how good it is or not). Note that for newer dbus and glib, you will want to use the self-presentation GObject ( from gi.repository import DBus

) instead of the old bindings python-dbus

, and this will almost certainly have flow changes compared to the sample code.



All Articles