<?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>Libre Things &#187; SFTP</title>
	<atom:link href="http://positon.org/tag/sftp/feed" rel="self" type="application/rss+xml" />
	<link>http://positon.org</link>
	<description></description>
	<lastBuildDate>Tue, 23 Feb 2016 20:01:11 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.7.1</generator>
	<item>
		<title>A solution to the umask problem: inotify to force permissions</title>
		<link>http://positon.org/a-solution-to-the-umask-problem-inotify-to-force-permissions</link>
		<comments>http://positon.org/a-solution-to-the-umask-problem-inotify-to-force-permissions#comments</comments>
		<pubDate>Thu, 21 Oct 2010 23:29:00 +0000</pubDate>
		<dc:creator><![CDATA[dooblem]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[inotify]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SFTP]]></category>
		<category><![CDATA[umask]]></category>

		<guid isPermaLink="false">http://positon.org:81/?p=102</guid>
		<description><![CDATA[Finding a good solution for sharing files between Linux users is a nightmare. If using a unique UID is not a problem, it&#8217;s the most simple solution. All clients access files with the same UID. This way you cannot know who does what, and users cannot fine tune access rights. The problem: default umask is [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Finding a good solution for sharing files between Linux users is a nightmare.</p>
<p>If using a unique UID is not a problem, it&#8217;s the most simple solution. All clients access files with the same UID. This way you cannot know who does what, and users cannot fine tune access rights.</p>
<p>The problem: default <a href="http://en.wikipedia.org/wiki/Umask">umask</a> is ALWAYS 0022, so that any created file will get <code>rw– r–– r––</code> permissions. Only the owner can write. Nobody else. To share files, a group must have write access.</p>
<p>You can change the umask. For command line, you set it in <code>.bashrc</code> or <code>.profile</code>, or <code>/etc/profile</code> for all users. For a <a href="http://en.wikipedia.org/wiki/SSH_file_transfer_protocol">SFTP</a> share, you can <a href="http://jeff.robbins.ws/articles/setting-the-umask-for-sftp-transactions">set it with a trick</a>. For Apache HTTP server, you can set it with <code>/etc/apache2/envvars</code> under Debian.</p>
<p>If file sharing is only done via on service, changing umask is simple, otherwise it&#8217;s not that easy. And even if you change umask for all services, nothing is perfect: for example it doesn&#8217;t work with <a href="http://live.gnome.org/Nautilus">Nautilus</a> and SFTP. Some clients drop files and issue a chmod right after: the hell. You can also try the power of <a href="http://www.suse.de/~agruen/acl/linux-acls/online/">POSIX ACL</a> to force permissions. But problems still remain with some clients.</p>
<p>And for the umask, maybe you don&#8217;t want all files to be dropped group writable. Maybe you want more granularity on permissions.</p>
<p>So I abandonned the idea of fixing the problem at the source in favor of some trick AFTER file creation.<br />
The most simple solution is the cron task: every X minutes, run <code>chmod -R g+w</code> on the directory. This way permissions are not fixed immediately, but asynchronously. And it adds a (very) little more load to your system.</p>
<p>My solution uses <a href="http://en.wikipedia.org/wiki/Inotify">inotify</a> to listen for file changes and force permissions when files are created:</p>
<pre>
aptitude install inotify-tools
</pre>
<p><strong>And the magical command:</strong></p>
<pre>
inotifywait -mrq -e CREATE --format %w%f /tmp/mytest/ | while read FILE; do chmod g=u &quot;$FILE&quot;; done
</pre>
<p><strong>UPDATE 2010-10-30</strong><br />
To support spaces at the end of filenames, and backslashes, use:</p>
<pre>
inotifywait -mrq -e CREATE --format %w%f /tmp/mytest/ | while IFS= read -r FILE; do chmod g=u &quot;$FILE&quot;; done
</pre>
<p>Thanks to vitoreiji (see comments)</p>
<p><code>inotifywait</code> listens for events in the <code>/tmp/mytest</code> directory. When a file is created, it&#8217;s displayed on standard output. Then each fileline is read by the <code>while</code> loop and permissions are changed. <code>g=u</code> gives the group the user&#8217;s permissions (with <code>g+w</code>, if the user drops a file with <code>rw– ––– –––</code>, permissions will be <code>rw– –w– –––</code>).</p>
<p>You can now test file/directory creation and copy. <code>mkdir -p a/b/c/d/e</code> shoud also work.</p>
<p>Finally, add it in a boot script:</p>
<pre>
vi /usr/local/bin/inotifywait.sh &amp;&amp; chmod +x /usr/local/bin/inotifywait.sh
#!/bin/sh
# Take the directory name as argument

inotifywait -mrq -e CREATE --format %w%f &quot;$1&quot; | while read FILE
do
	chmod g=u &quot;$FILE&quot;
done
</pre>
<pre>
vi /etc/init.d/inotifywait.sh &amp;&amp; chmod +x /etc/init.d/inotifywait.sh
#! /bin/sh

case &quot;$1&quot; in
  start|&quot;&quot;)

	rm -f /tmp/inotifywait.log
	/usr/local/bin/inotifywait.sh /path/to/dir/ &gt;/tmp/inotifywait.log 2&gt;&amp;1 &amp;
	
	;;
  restart|reload|force-reload)
	echo &quot;Error: argument '$1' not supported&quot; &gt;&amp;2
	exit 3
	;;
  stop)
	# killall inotifywait ???
	;;
  *)
	echo &quot;Usage: inotifywait.sh [start|stop]&quot; &gt;&amp;2
	exit 3
	;;
esac

:
</pre>
<p>(Debian way)</p>
<pre>
update-rc.d inotifywait.sh defaults
</pre>
<p>Note: a drawback: there is a limit on the number of tracked files. See <code>-r</code> option in <code>man inotifywait</code>.</p>
<p>Then the final touch in order for the new files to be created with the same group as their parent: <a href="http://en.wikipedia.org/wiki/Setuid">setgid bit</a> for all directories.</p>
<pre>
find /path/to/dir -type d -exec chmod g+s {} \;
</pre>
<p><ins>Links</ins>:</p>
<ul>
<li><code>man inotifywait</code></li>
<li><a href="http://github.com/rvoicilas/inotify-tools/wiki">inotify-tools</a></li>
<li><a href="http://en.wikipedia.org/wiki/Inotify" title="http://en.wikipedia.org/wiki/Inotify">http://en.wikipedia.org/wiki/Inotify</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://positon.org/a-solution-to-the-umask-problem-inotify-to-force-permissions/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>SFTP chroot + rsync</title>
		<link>http://positon.org/sftp-chroot-rsync</link>
		<comments>http://positon.org/sftp-chroot-rsync#comments</comments>
		<pubDate>Fri, 09 Oct 2009 16:12:00 +0000</pubDate>
		<dc:creator><![CDATA[dooblem]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[chroot]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[SFTP]]></category>
		<category><![CDATA[SSH]]></category>

		<guid isPermaLink="false">http://positon.org:81/?p=62</guid>
		<description><![CDATA[Here is howto make sftp shares with chroot. In /etc/ssh/sshd_config: # we use openssh internal sftp # because /usr/lib/openssh/sftp-server won't be available in chroot Subsystem sftp internal-sftp Match group sftp ChrootDirectory %h X11Forwarding no AllowTcpForwarding no ForceCommand internal-sftp UPDATE 17/06/2010: Beware with the syntax! Comments must start at the line beginning, and no spaces at [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Here is howto make sftp shares with chroot.</p>
<p>In <code>/etc/ssh/sshd_config</code>:</p>
<pre>
# we use openssh internal sftp
# because /usr/lib/openssh/sftp-server won't be available in chroot
Subsystem sftp internal-sftp

Match group sftp
        ChrootDirectory %h
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp
</pre>
<p><strong>UPDATE 17/06/2010:</strong> Beware with the syntax! Comments must start at the line beginning, and no spaces at the end of the <code>ForceCommand internal-sftp</code> line.</p>
<p>Now just create users belonging to sftp group, and that&#8217;s it.<br />
Test it with:</p>
<pre>
sftp user@myserver.com
</pre>
<p><strong>Problem: we cannot use the rsync command to send files</strong>, because rsync is not available in the chroot.</p>
<p>First, we allow other commands, commenting the line:</p>
<pre>
#ForceCommand internal-sftp
</pre>
<p>Then, we build the following tree in the chroot directory:</p>
<pre>
bin/
bin/bash
bin/rsync
lib/
lib/libncurses.so.5
lib/ld-linux.so.2
lib/libacl.so.1
lib/libpopt.so.0
lib/libattr.so.1
lib/i686
lib/i686/cmov
lib/i686/cmov/libdl.so.2
lib/i686/cmov/libc.so.6
</pre>
<p>We must put both <code>bash</code> and <code>rsync</code> commands, and all their librairies (you can display them with the <code>ldd</code> command).</p>
<p>Note: the user must have <code>/bin/bash</code> as default shell.</p>
<p>Note2: the chroot dir must belong to root, even if it&#8217;s the user&#8217;s folder. To allow the user to write in it, you have to create a subfolder with appropriate permissions. According to OpenSSH programers, it&#8217;s a big constraint, but very important for a chroot&#8217;s security.</p>
<p><ins>References</ins> :</p>
<ul>
<li><code>man sshd_config</code></li>
<li><a href="http://www.debian-administration.org/articles/590" title="http://www.debian-administration.org/articles/590">http://www.debian-administration.org/articles/590</a></li>
<li><a href="http://www.howtoforge.org/chrooted-ssh-sftp-tutorial-debian-lenny" title="http://www.howtoforge.org/chrooted-ssh-sftp-tutorial-debian-lenny">http://www.howtoforge.org/chrooted-ssh-sftp-tutorial-debian-lenny</a></li>
<li><a href="http://www.fuschlberger.net/programs/ssh-scp-sftp-chroot-jail/" title="http://www.fuschlberger.net/programs/ssh-scp-sftp-chroot-jail/">http://www.fuschlberger.net/programs/ssh-scp-sftp-chroot-jail/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://positon.org/sftp-chroot-rsync/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
