<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Unwinding the Stack &#187; suexec</title>
	<atom:link href="http://thestaticvoid.com/tag/suexec/feed/" rel="self" type="application/rss+xml" />
	<link>http://thestaticvoid.com</link>
	<description>Explorations in Computing</description>
	<lastBuildDate>Wed, 07 Dec 2011 16:15:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>suEXEC on OpenSolaris</title>
		<link>http://thestaticvoid.com/post/2009/07/27/suexec-on-opensolaris/</link>
		<comments>http://thestaticvoid.com/post/2009/07/27/suexec-on-opensolaris/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 03:24:59 +0000</pubDate>
		<dc:creator>James Lee</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[opensolaris]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[suexec]]></category>

		<guid isPermaLink="false">http://thestaticvoid.com/?p=96</guid>
		<description><![CDATA[One nice thing about having all dynamic content being generated by CGI is that you can use suEXEC to run the scripts as a different user. This is primarily used for systems where you have multiple untrusted users who run sites in one HTTP server. Then no one can interfere with anyone else. It can [...]]]></description>
			<content:encoded><![CDATA[<p>One nice thing about having all dynamic content being generated by CGI is that you can use <a href="http://httpd.apache.org/docs/2.2/suexec.html">suEXEC</a> to run the scripts as a different user.  This is primarily used for systems where you have multiple untrusted users who run sites in one HTTP server.  Then no one can interfere with anyone else.  It can also be used simply for separating the application from the server.</p>
<p>I&#8217;m the only user on my server so I don&#8217;t necessarily have any of these security concerns, but I have enabled suEXEC for convenience.  For example, WordPress will allow you to modify the stylesheets from the admin interface as long as it can write to them.  With suEXEC, the admin interface can run as my Unix user, so I can edit the files from both the web interface and the command line without having wide-open permissions or switching to root.</p>
<p>Same applies for <a href="http://iriverter.thestaticvoid.com/">Trac</a> where I can manage the project with the web interface or <tt>trac-admin</tt> on the command line.  The same effect could pretty much be obtained by using Unix groups properly:</p>
<pre class="terminal"># <kbd>groupadd wordpress</kbd>
# <kbd>usermod -G wordpress webservd</kbd>
# <kbd>usermod -G wordpress jlee</kbd>  <span class="comment"># my username</span>
# <kbd>chgrp -R wordpress /docs/thestaticvoid.com</kbd>  <span class="comment"># virtualhost document root</span>
# <kbd>chmod -R g+ws /docs/thestaticvoid.com</kbd>  <span class="comment"># make directory writable and always owned by
                                           the wordpress group</pre>
<p>Then <tt>umask 002</tt> would have to be set in Apache&#8217;s and my profile so any files that get created can be written to by the other users in the group.  That&#8217;s all well and good, but it seems like a bit of work and I don&#8217;t like the idea of messing with the default umask.</p>
<p>On to suEXEC.  First, let&#8217;s show the current user that PHP executes as.  Create a file <tt>test.php</tt> containing <code class="codecolorer php default"><span class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span></code>.  Accessing the script from your web browser should show something like <tt>uid=80(webservd) gid=80(webservd)</tt>.</p>
<p>Next, in OpenSolaris, the <tt>suexec</tt> binary must be enabled:</p>
<pre class="terminal"># <kbd>cd /usr/apache2/2.2/bin/</kbd>  <span class="comment"># go one directory further into the amd64 dir
                              if you're running 64-bit</span>
# <kbd>mv suexec.disabled suexec</kbd>
# <kbd>chown root:webservd suexec</kbd>
# <kbd>chmod 4750 suexec</kbd>
# <kbd>./suexec -V</kbd>
 -D AP_DOC_ROOT="/var/apache2/2.2/htdocs"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="webservd"
 -D AP_LOG_EXEC="/var/apache2/2.2/logs/suexec_log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=100
 -D AP_USERDIR_SUFFIX="public_html"
</pre>
<p>These variables were set at compile time and cannot be changed.  They ensure that certain conditions must be met in order to use the binary.  That&#8217;s very important because it&#8217;s setuid root.  The first thing I had to do was move everything from my old document root to the one specified above in <tt>AP_DOC_ROOT</tt>.  Then you can add <code class="codecolorer apache default"><span class="apache"><span style="color: #00007f;">SuexecUserGroup</span> jlee jlee</span></code> (with whatever username and group you want the scripts to run as) to your <code class="codecolorer apache default"><span class="apache">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</span></code> section of the Apache configuration.  At this point if you try to execute <tt>test.php</tt> you&#8217;ll probably see one of a couple errors in the suEXEC log (<tt>/var/apache2/2.2/logs/suexec_log</tt>):</p>
<ul>
<li><tt>[2009-07-27 11:08:02]: uid: (1000/jlee) gid: (1000/jlee) cmd: php-cgi<br />
[2009-07-27 11:08:02]: command not in docroot (/usr/php/bin/php-cgi)</tt><br />
In this case, <tt>php-cgi</tt> is going to have to be moved to the document root:</p>
<pre class="terminal">$ <kbd>cp /usr/php/bin/php-cgi /var/apache2/2.2/htdocs/</kbd>
$ <kbd>pfexec vi /etc/apache2/2.2/conf.d/php-cgi.conf</kbd>  <span class="comment"># modify the ScriptAlias appropriately</span>
$ <kbd>svcadm restart http</kbd></pre>
</li>
<li><tt>[2009-07-27 11:11:07]: uid: (1000/jlee) gid: (1000/jlee) cmd: php-cgi<br />
[2009-07-27 11:11:07]: target uid/gid (1000/1000) mismatch with directory (0/2) or program (0/0)</tt><br />
Make sure everything that <tt>suexec</tt> is to execute is owned by the same user and group as specified in the <code class="codecolorer apache default"><span class="apache"><span style="color: #00007f;">SuexecUserGroup</span></span></code> line of your Apache configuration.</li>
</ul>
<p>Now, running <tt>test.php</tt> should give the correct results: <tt>uid=1000(jlee) gid=1000(jlee)</tt>.  Done!</p>
<p>As a side note, I lose all frame of reference while I write so I can&#8217;t remember if I&#8217;m writing this for <strong>you</strong> or <strong>me</strong>, explaining what I&#8217;ve <strong>done</strong> or what you <strong>should do</strong>.  Sorry <img src='http://thestaticvoid.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://thestaticvoid.com/post/2009/07/27/suexec-on-opensolaris/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

