1

I have a python program that runs as root one if its functions at the click of a button is to open a webpage. It kept opening the webpage in firefox as root so I try to use setuid with no avail. Here's what I've tried to get to open the link as a normal user:

from PyQt4 import QtGui, QtCore
import pwd
import os
import webbrowser


def username():
    euid = os.geteuid()
    print euid
    if 'PKEXEC_UID' in os.environ:
        user = pwd.getpwuid(int(os.environ["PKEXEC_UID"])).pw_name
        print user
        return user
    elif euid == 0 and 'PKEXEC_UID' not in os.environ:
        user = os.environ["SUDO_USER"]
        print user
        return user

def openLinks():
    uidChange = pwd.getpwnam(username()).pw_uid
    pidx = os.fork()
    if pidx == 0:
        try:
            os.setuid(uidChange)
            QtGui.QDesktopServices.openUrl(QtCore.QUrl("http://stackoverflow.com"))
            #webbrowser.open("http://stackoverflow.com")

        finally:
            os._exit(0)
    os.waitpid(pidx, 0)

openLinks()
  • Have you verified what uidChange is? – Stephen Rauch Mar 28 '17 at 5:02
  • yes, and It's 1000 – answerSeeker Mar 28 '17 at 5:13
0

I don't know what QtGui.QDesktopServices.openUrl does, but I strongly suspect it opens a URL in an appropriate application by communicating with some KDE daemon through the X11 session, not by launching a browser directly as a child of itself. You can verify this by looking at what the parent process ID of the resulting browser is. Due to this, it doesn't matter what UID your script has switched to — only the uid of the daemon matters.

Running X11 sessions as root is usually a bad idea. May I suggest running an X11 session as a regular user and having only your Python script run as root (only if it's vetted and only if you really have to)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.