<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2650.12">
<TITLE>Re: Switch to a different uid/gid after binding reserved ports (Glyph Lefkowitz)</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; You can specify keyword arguments to Application's constructor.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = Application(uid=1001, gid=1001)</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Then, call either the setUID or setEUID method of your Application </FONT>
<BR><FONT SIZE=2>&gt; instance.&nbsp; You will need to call bindPorts before calling these </FONT>
<BR><FONT SIZE=2>&gt; methods, however.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
</P>

<P><FONT SIZE=2>Hi Glyph,</FONT>
</P>

<P><FONT SIZE=2>I have now had a chance to test this on HP-UX11i and have discovered a problem.</FONT>
</P>

<P><FONT SIZE=2>The code for Application.setUID() calls os.getgid() expecting it to return 0 for root. However, on HP-UX it returns 3.</FONT>
</P>

<P><FONT SIZE=2># id</FONT>
<BR><FONT SIZE=2>uid=0(root) gid=3(sys) groups=0(root),1(other),2(bin),4(adm),5(daemon),6(mail),7(lp),20(users)</FONT>
</P>

<P><FONT SIZE=2># /opt/python/bin/python&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>Python 2.2.1 (#5, Jan 23 2003, 13:18:26) [C] on hp-ux11</FONT>
<BR><FONT SIZE=2>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; import os</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; os.getgid()</FONT>
<BR><FONT SIZE=2>3</FONT>
</P>

<P><FONT SIZE=2>But:</FONT>
</P>

<P><FONT SIZE=2>&gt;&gt;&gt; os.getgroups()</FONT>
<BR><FONT SIZE=2>[3, 0, 1, 2, 4, 5, 6, 7, 20]</FONT>
</P>

<P><FONT SIZE=2>A possible solution would be:</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def setUID(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Retrieve persistent uid/gid pair (if possible) and set the current process's uid/gid</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hasattr(os, 'getgroups'):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if 0 in os.getgroups():</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.setgid(self.gid)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.setuid(self.uid)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log.msg('set uid/gid %s/%s' % (self.uid, self.gid))</FONT>
</P>
<BR>

<P><FONT SIZE=2>Alternatively, you might use try/except &amp; ignore OSError exceptions?</FONT>
</P>

<P><FONT SIZE=2>cheers,</FONT>
<BR><FONT SIZE=2>Richard Townsend</FONT>
</P>

</BODY>
</HTML>