Skip to content

Instantly share code, notes, and snippets.

@sweenzor
Created January 26, 2012 23:08
Show Gist options
  • Save sweenzor/1685717 to your computer and use it in GitHub Desktop.
Save sweenzor/1685717 to your computer and use it in GitHub Desktop.

Revisions

  1. sweenzor revised this gist Jan 26, 2012. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions subprocessdemote.py
    Original file line number Diff line number Diff line change
    @@ -3,30 +3,35 @@
    import os
    import subprocess

    # > python subprocessdemote.py
    # > sudo python subprocessdemote.py


    def check_username():
    """Check who is running this script"""
    """Check who is running this script"""

    print os.getuid()
    print os.getgid()


    def check_id():
    """Run the command 'id' in a subprocess, return the result"""
    """Run the command 'id' in a subprocess, return the result"""

    cmd = ['id']
    return subprocess.check_output(cmd)


    def check_id_as_user():
    """Run the command 'id' in a subprocess as user 1000, return the result"""
    """Run the command 'id' in a subprocess as user 1000,
    return the result"""

    cmd = ['id']
    return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000))


    def demote(user_uid, user_gid):
    """Pass the function 'set_ids' to preexec_fn, rather than just calling
    setuid and setgid. This will change the ids for that subprocess only"""
    """Pass the function 'set_ids' to preexec_fn, rather than just calling
    setuid and setgid. This will change the ids for that subprocess only"""

    def set_ids():
    os.setgid(user_gid)
    @@ -41,4 +46,4 @@ def set_ids():

    print check_id()
    print check_id_as_user()
    print check_id()
    print check_id()
  2. sweenzor revised this gist Jan 26, 2012. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions subprocessdemote.py
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,38 @@
    #!/usr/bin/env python

    import os, pwd
    import os
    import subprocess

    def check_username():
    """Check who is running this script"""

    print os.getuid()
    print os.getgid()


    def check_id():
    """Run the command 'id' in a subprocess, return the result"""

    cmd = ['id']
    return subprocess.check_output(cmd)


    def check_id_as_user():
    """Run the command 'id' in a subprocess as user 1000, return the result"""

    cmd = ['id']
    return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000))


    def demote(user_uid, user_gid):
    """Pass the function 'set_ids' to preexec_fn, rather than just calling
    setuid and setgid. This will change the ids for that subprocess only"""

    def result():
    def set_ids():
    os.setgid(user_gid)
    os.setuid(user_uid)

    return result
    return set_ids


    if __name__=='__main__':
    @@ -36,4 +41,4 @@ def result():

    print check_id()
    print check_id_as_user()
    print check_id()
    print check_id()
  3. sweenzor created this gist Jan 26, 2012.
    39 changes: 39 additions & 0 deletions subprocessdemote.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/usr/bin/env python

    import os, pwd
    import subprocess

    def check_username():

    print os.getuid()
    print os.getgid()


    def check_id():

    cmd = ['id']
    return subprocess.check_output(cmd)


    def check_id_as_user():

    cmd = ['id']
    return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000))


    def demote(user_uid, user_gid):

    def result():
    os.setgid(user_gid)
    os.setuid(user_uid)

    return result


    if __name__=='__main__':

    check_username()

    print check_id()
    print check_id_as_user()
    print check_id()