title: setuid/setgid wrapper for python script
body: <p>I have a Python script that I wish to be able to be run as the system user <code>guybrush</code> with UID 200 and group <code>guybrush</code> with GID 200.</p>
  
  <p>At the moment my Python script (located in <code>/path/to/script.py</code>) looks like this:</p>
  
  <pre><code>#!/usr/bin/env python2
  import os
  
  print "uid: %s" % os.getuid()
  print "euid: %s" % os.getgid()
  print "gid: %s" % os.geteuid()
  print "egid: %s" % os.getegid()
  </code></pre>
  
  <p>My attempted C wrapper (<code>scriptwrap.c</code>) looks like this:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  
  int main(int argc, char *argv[]) {
      setuid(geteuid());
      setgid(getegid());
      return execv("/path/to/script.py", argv);
  }
  </code></pre>
  
  <p>I then compile, chown, and chmod the wrapper as follows:</p>
  
  <pre><code>$ gcc scriptwrap.c -o scriptwrap
  $ chown guybrush:guybrush scriptwrap
  $ chmod 6755 scriptwrap
  </code></pre>
  
  <p>Yet when I run scriptwrap, I get the following output:</p>
  
  <pre><code>uid: 1000
  euid: 1000
  gid: 200
  egid: 200
  </code></pre>
  
  <p>So for some reason only the GID is being set (my normal UID is 1000). What can I do to fix this?</p>
  
  <p><strong>Edit:</strong> If I chown the script to <code>root:root</code> and run it, the UID, eUID, GID, and eGID are all set to 0.</p>
  
  <p>Also, this is on Ubuntu 12.04.4 LTS.</p>