<?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>Straylight Run &#187; rsync</title> <atom:link href="http://blog.straylightrun.net/tag/rsync/feed/" rel="self" type="application/rss+xml" /><link>http://blog.straylightrun.net</link> <description>Software, Technology, PHP</description> <lastBuildDate>Mon, 07 Nov 2011 19:26:59 +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>Securely Running A Command As Root</title><link>http://blog.straylightrun.net/2010/02/22/securely-running-a-command-as-root/</link> <comments>http://blog.straylightrun.net/2010/02/22/securely-running-a-command-as-root/#comments</comments> <pubDate>Mon, 22 Feb 2010 22:21:19 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Sysadmin]]></category> <category><![CDATA[deploy]]></category> <category><![CDATA[root]]></category> <category><![CDATA[rsync]]></category> <category><![CDATA[setuid]]></category> <category><![CDATA[sudo]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2010/02/22/securely-running-a-command-as-root/</guid> <description><![CDATA[As much as I wish we deployed builds from our continuous integration server, all but one of our products is deployed with good ol’ `svn up`.&#160; Developers generally have access to only one web server, so I needed an rsync command to propagate new code to the rest of the web servers.&#160; I wanted normal [...]]]></description> <content:encoded><![CDATA[<p>As much as I wish we deployed builds from our <a
href="http://martinfowler.com/articles/continuousIntegration.html">continuous integration</a> server, all but one of our products is deployed with good ol’ <code>`svn up`</code>.&#160; Developers generally have access to only one web server, so I needed an <code>rsync </code>command to propagate new code to the rest of the web servers.&#160; I wanted normal user accounts to be able to run it at any time in any directory with one command.&#160; Then developers would be instructed to run this command after updating any files.</p><p>So I whipped up an shell script that called <code>rsync</code> with some predefined options and targets.&#160; Unfortunately, in order to preserve ownership and permissions in the destination, <code>rsync </code>needed to be run as <code>root</code>.</p><p>At first, I looked at the <code>setuid </code>bit. By changing the ownership of the <code>rsync </code>shell script and running <code>`chmod u+s`</code> on the script, setting the setuid, any user could execute it and it would run as <code>root</code>. Well, it turns out that the kernel will not honor <code>setuid </code>on shell scripts for security reasons. But what if I wrote a C program instead of a shell script? That actually worked, and ran with <code>root </code><em>privileges</em>, but it still did not <code>rsync </code><em>as</em> root for some reason. So that was out.</p><p>The second solution was to insert <code>sudo </code>before the <code>rsync </code>command in the script. I modified <code>/etc/sudoers</code> to allow the users group to run <code>rsync </code>under <code>sudo</code>. That worked perfectly. So if I put this script in <code>/usr/local/bin</code>, I would be done. But I had already written this magnificent (two-line) C program.&#160; Why not make it even more secure (<code>sudo </code>does not work on shell scripts either)?&#160; Instead of allowing all users to run <code>rsync </code>under <code>sudo</code>, I could limit them to running only <em>my </em>C program under <code>sudo</code>, instead of <code>rsync </code>in general. Then, in my script, I could replace <code>rsync </code>with my C program. So that’s what I did. I again modified <code>/etc/sudoers</code> and my shell script, threw both the script and C executable in <code>/usr/local/bin </code>and I was done.</p><p>I named the final command <code>`zipsync`</code>. Here is the shell script for that, anonymized a bit.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts
&nbsp;
<span style="color: #666666; font-style: italic;"># repeat for each web server</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> zipsync.bin \
   <span style="color: #660033;">-av</span> <span style="color: #660033;">--delete</span> \
   <span style="color: #660033;">--exclude</span>=<span style="color: #ff0000;">&quot;.svn&quot;</span> \
   <span style="color: #660033;">--exclude</span>=<span style="color: #ff0000;">&quot;logs&quot;</span> \
   <span style="color: #660033;">--exclude</span>=<span style="color: #ff0000;">&quot;tmp&quot;</span> \
   <span style="color: #660033;">--exclude</span>=<span style="color: #ff0000;">&quot;cache&quot;</span> \
   <span style="color: #660033;">--exclude</span>=<span style="color: #ff0000;">&quot;*.swp&quot;</span> \
   <span style="color: #000000; font-weight: bold;">*</span> 192.168.1.101:<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> -</pre></td></tr></table></div><p>And the C program, <code>zipsync.bin</code>.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;unistd.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #339933;">**</span> argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #339933;">*</span>argv <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;rsync&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> execvp<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>argv<span style="color: #339933;">,</span> argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2010/02/22/securely-running-a-command-as-root/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
