<?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>Playing on the frontier &#187; linux</title>
	<atom:link href="http://siphon9.net/loune/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://siphon9.net/loune</link>
	<description></description>
	<lastBuildDate>Thu, 15 Jul 2010 12:58:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>iptsafe &#8211; iptables with dead man&#8217;s switch</title>
		<link>http://siphon9.net/loune/2009/12/iptsafe-iptables-with-dead-mans-switch/</link>
		<comments>http://siphon9.net/loune/2009/12/iptsafe-iptables-with-dead-mans-switch/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 03:35:54 +0000</pubDate>
		<dc:creator>Loune</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://siphon9.net/loune/?p=85</guid>
		<description><![CDATA[When dealing with iptables remotely, you can easily set a firewall rule which would lock yourself out of machine. After that, the only way to unlock yourself is to physically go the machine and unset the firewall rules it through the terminal. If this was a VPS or dedicated server, chances are you can&#8217;t physically [...]]]></description>
			<content:encoded><![CDATA[<p>When dealing with iptables remotely, you can easily set a firewall rule which would lock yourself out of machine. After that, the only way to unlock yourself is to physically go the machine and unset the firewall rules it through the terminal. If this was a VPS or dedicated server, chances are you can&#8217;t physically access the machine and have to contact the service provider to reset the firewall rules.</p>
<p>This is an instance where a dead man&#8217;s switch would help. The theory goes that if an operator is detected incapacitated, then an certain action will occur. In our case, the action is to undo our firewall changes. How does it know we are incapacitated? Well if we don&#8217;t report back in a certain amount of time, then we&#8217;re probably dead. A long time application of this is actually found when you try to change your monitor&#8217;s resolution, it ask you if you want to keep it. If it gets no response, it&#8217;ll revert back automatically.</p>
<p><img src="http://siphon9.net/loune/wp-content/uploads/2009/12/keepsettings.png" alt="Do you want to keep these settings" title="Do you want to keep these settings" width="475" height="360" class="alignnone size-full wp-image-145" /></p>
<p>The iptsafe script works on the same principle. It is a wrapper around the iptables command. It takes the same command parameters as iptables, with the exception that if you only specify one parameter, then it&#8217;ll assume it is an iptables-save&#8217;d file and use it on iptables-restore. Once iptsafe is run, it&#8217;ll first use iptables-save to store a copy of the current iptables state, then apply the changes you requested. After that, it prompts you to keep the changes, and if you don&#8217;t respond within 15 seconds, it&#8217;ll revert back to the original state.</p>
<p>Here&#8217;s <a href="/loune/f/iptsafe">iptsafe</a></p>
<p>Sample usage:<br />
<code># iptsafe -A INPUT -i eth0 -p tcp -s 192.168.0.1 -j ACCEPT</code><br />
or<br />
<code># iptsafe my-saved-iptables</code></p>
]]></content:encoded>
			<wfw:commentRss>http://siphon9.net/loune/2009/12/iptsafe-iptables-with-dead-mans-switch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pondering per user accounting in Linux</title>
		<link>http://siphon9.net/loune/2009/07/pondering-per-user-accounting-in-linux/</link>
		<comments>http://siphon9.net/loune/2009/07/pondering-per-user-accounting-in-linux/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 08:58:39 +0000</pubDate>
		<dc:creator>Loune</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://siphon9.net/loune/?p=39</guid>
		<description><![CDATA[I&#8217;ve been researching for the better part of the day on what the best method to account for bandwidth (and cpu/memory) used by a particular user is. This is useful if you run a hosting business and give out shell access. At first I was looking for a way to meter SSH. There seems to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been researching for the better part of the day on what the best method to account for bandwidth (and cpu/memory) used by a particular user is. This is useful if you run a hosting business and give out shell access. At first I was looking for a way to meter SSH. There seems to be an old <a href="http://www.google.com.au/search?q=openssh+traffic+accounting+patch&amp;btnG=Search&amp;meta=">patch</a> for it, but as I continued reading, a old mailing from a mailing list pointed out that there are heaps of ways to generate traffic when you have a shell account (ie wget). In fact you don&#8217;t even need shell access &#8211; any scripting language that could download will consume bandwidth that may not be accounted for.</p>
<p>So this began my quest to find the best solution to per user accounting in linux. The basic concept is that since the bandwidth consumption is triggered by a process, and owned by a specific user, we should be able to trace traffic to a user and record as such. The advantage is even greater if you run peruser mpm apache or suexec&#8217;d php.</p>
<p>I began looking at netfilter/iptables, which had a match -m owner uid. This works only on the OUTPUT chain and will tell you who sent the packet, but unfortunately doesn&#8217;t tell you who a packet was destined for.</p>
<p>iptables has a connection tracking feature, that tracks active connections, allowing for stateful packet inspection. If you have the kernel feature enabled, it will also count the traffic numbers, which you can then view in /proc/net/ip_conntrack (or /proc/net/nf_conntrack for newer installations). Using that, and cross referencing it with the netstat -anp and process table will give you an idea of which user owns the connection. This is assuming of course that the process doesn&#8217;t setuid to change users.</p>
<p>But then, how are we going to collect all the data? Polling would be extremely slow and tedious and you might miss short lived connections. It seems that using <a href="http://www.netfilter.org/projects/libnetfilter_conntrack/index.html">libnetfilter_conntrack</a>, you can subscribe to an event that notifies when connection states have changed (CONFIG_IP_NF_CONNTRACK_EVENTS). Using that, you can record when connections are opened and when they are closed as they happen.</p>
<p>What about processes? Processing accounting can be easily taken care of by the <a href="http://www.google.com.au/search?q=unix+acct">unix acct</a> tools, which monitors processes as they are created and destroyed, provided you have the correct kernel options enabled. But what if you don&#8217;t have this option, ie on a VPS &#8211; Is there an alternative? The answer is yes, but ugly. You might remember that process information can be access via /proc. What if I set inotify, the file system change mechanism to tell me when /proc has changed? Somebody already <a href="http://kerneltrap.org/mailarchive/linux-kernel/2007/8/1/124586">thought of this</a> and found it didn&#8217;t work quite as expected. The reason for this was mentioned in the linked thread, but the responders did give a good alternative &#8211; using ptrace ().</p>
<p>The ptrace command is a powerful unix system call that can manipulate processes it has attached to. It is what the debugger gdb uses to debug running applications. Using the ptrace function, you can set an option to notify the controlling process via SIGTRAP that the ptrace&#8217;d process has terminated, or forked/execed. Using this, you can potentially hook into every process and closely monitor their lifecycle. The downside is that you cannot have two ptrace active on the same process, which means application like gdb will fail if your monitoring system is active. Since ptrace is primarily used for debugging, it may also degrade performance of application it has been attached to. So the bottom line is that it looks like it is too extravagant and thus the wrong way to go for implementing a process accounting/monitoring system.</p>
<p>Looks like my quest to find a viable way of accounting per user accounting has so far eluded me. Perhaps the old ways of individual accounting in every application service &#8211; apache/ftp/imap/smtp is here to stay.</p>
]]></content:encoded>
			<wfw:commentRss>http://siphon9.net/loune/2009/07/pondering-per-user-accounting-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
