Running one Python program from another as a different user

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dwelch91@gmail.com

    #1

    Running one Python program from another as a different user

    Greetings-
    This is on Linux... I have a daemon running as root and I want to
    execute another Python program as another user (a regular user). I have
    the name of the user and can use the 'pwd' and 'grp' modules to get
    that user's user and group ids. What I don't understand is how to then
    go about launching that new program. I had considered having the
    launched program switch itself back to the target user (somehow), but
    the launched program is graphical in nature (PyQt), and I am afraid of
    X11 locking out the display to user root (many distros seem to ship
    with server access for user root turned off). That might prevent the
    launched program from even starting?

    Any ideas? My Google searching was not successful in figuring this
    out...

    Thanks,

    Don

  • Jeff Schwab

    #2
    Re: Running one Python program from another as a different user

    dwelch91@gmail. com wrote:[color=blue]
    > Greetings-
    > This is on Linux... I have a daemon running as root and I want to
    > execute another Python program as another user (a regular user). I have
    > the name of the user and can use the 'pwd' and 'grp' modules to get
    > that user's user and group ids. What I don't understand is how to then
    > go about launching that new program. I had considered having the
    > launched program switch itself back to the target user (somehow), but
    > the launched program is graphical in nature (PyQt), and I am afraid of
    > X11 locking out the display to user root (many distros seem to ship
    > with server access for user root turned off). That might prevent the
    > launched program from even starting?
    >
    > Any ideas? My Google searching was not successful in figuring this
    > out...[/color]

    from subprocess import *

    def run_as(username ):
    pipe = Popen(["su", username], stdin=PIPE, stdout=PIPE)
    (out, err) = pipe.communicat e("whoami")
    print out,


    if __name__ == "__main__":
    import sys
    for user in sys.argv[1:]:
    run_as(user)

    Comment

    • dwelch91@gmail.com

      #3
      Re: Running one Python program from another as a different user

      Thanks, that looks very promising...
      Is there a solution for pre-Python v2.4? I have to have code that works
      on 2.x, 0<=x<=4. Do I just use the os.popen instead?

      -Don

      Comment

      • Paul Rubin

        #4
        Re: Running one Python program from another as a different user

        "dwelch91@gmail .com" <dwelch91@gmail .com> writes:[color=blue]
        > This is on Linux... I have a daemon running as root and I want to
        > execute another Python program as another user (a regular user). I have
        > the name of the user and can use the 'pwd' and 'grp' modules to get
        > that user's user and group ids. What I don't understand is how to then
        > go about launching that new program.[/color]

        Use os.fork to start a new process and have the new process do
        os.setuid to the new user. Then os.execl (or whatever) to run the
        target program.

        Comment

        • Mike Meyer

          #5
          Re: Running one Python program from another as a different user

          "dwelch91@gmail .com" <dwelch91@gmail .com> writes:
          [color=blue]
          > Greetings-
          > This is on Linux... I have a daemon running as root and I want to
          > execute another Python program as another user (a regular user). I have
          > the name of the user and can use the 'pwd' and 'grp' modules to get
          > that user's user and group ids. What I don't understand is how to then
          > go about launching that new program. I had considered having the
          > launched program switch itself back to the target user (somehow), but
          > the launched program is graphical in nature (PyQt), and I am afraid of
          > X11 locking out the display to user root (many distros seem to ship
          > with server access for user root turned off). That might prevent the
          > launched program from even starting?
          >
          > Any ideas? My Google searching was not successful in figuring this
          > out...[/color]

          Well, Jeff already pointed out running su. You might also check the
          os.setuid docs, and the setuid man page.

          <mike
          --
          Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
          Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

          Comment

          • Jeff Schwab

            #6
            Re: Running one Python program from another as a different user

            dwelch91@gmail. com wrote:[color=blue]
            > Thanks, that looks very promising...
            > Is there a solution for pre-Python v2.4? I have to have code that works
            > on 2.x, 0<=x<=4. Do I just use the os.popen instead?[/color]

            import os

            def run_as(username ):
            pipe = os.popen("su %s" % username, 'w')
            pipe.write("who ami")


            if __name__ == "__main__":
            import sys
            for user in sys.argv[1:]:
            run_as(user)

            Comment

            • Alessandro Bottoni

              #7
              Re: Running one Python program from another as a different user

              Jeff Schwab wrote:
              [color=blue]
              > dwelch91@gmail. com wrote:[color=green]
              >> Thanks, that looks very promising...
              >> Is there a solution for pre-Python v2.4? I have to have code that works
              >> on 2.x, 0<=x<=4. Do I just use the os.popen instead?[/color]
              >
              > import os
              >
              > def run_as(username ):
              > pipe = os.popen("su %s" % username, 'w')
              > pipe.write("who ami")
              >
              >
              > if __name__ == "__main__":
              > import sys
              > for user in sys.argv[1:]:
              > run_as(user)[/color]

              Jeff, may I suggest you to write down a new recipe for the ASPN Python
              Cookbook based on these code snippets? This is exactly the kind of thing
              the people is happy to find at ASPN.

              CU
              -----------------------------------
              Alessandro Bottoni

              Comment

              Working...