<?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>Smörgåsbord &#187; Code</title>
	<atom:link href="http://smorgasbord.gavagai.nl/topics/tech/code-tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://smorgasbord.gavagai.nl</link>
	<description>Ambachtelijk bereide beschouwingen.</description>
	<lastBuildDate>Fri, 06 Jan 2012 21:30:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>File slack space calculator</title>
		<link>http://smorgasbord.gavagai.nl/2012/01/file-slack-space-calculator/</link>
		<comments>http://smorgasbord.gavagai.nl/2012/01/file-slack-space-calculator/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 21:30:46 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[block size]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[filesystem overhead]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[slack]]></category>
		<category><![CDATA[slack space]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1464</guid>
		<description><![CDATA[I wanted to know whether I could use some tricks to make more efficient use of the 4GB Compact Flash storage i have in my Alix (running Gentoo Linux). I would like to keep a local portage tree on it — it currently mounts the tree over NFS due to space concerns.
Why no harddisk? Because [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to know whether I could use some tricks to make more efficient use of the 4GB Compact Flash storage i have in my Alix (running Gentoo Linux). I would like to keep a local portage tree on it — it currently mounts the tree over NFS due to space concerns.<br />
Why no harddisk? Because that goes against the idea of having a small, reliable, always-on, energy-efficient home server.<br />
Techies like pictures of hardware with the cover off so here&#8217;s my Alix:</p>
<p><img src="http://smorgasbord.gavagai.nl/wp-content/uploads/2012/01/alixje-300x199.jpg" alt="alixje" title="alixje" width="300" height="199" class="aligncenter size-medium wp-image-1468" /></p>
<p>Such a setup can lead to unfortunate situations, such as needing a package to restore connectivity to the network on which the fileserver resides that contains the package that I need to restore connectivity to the network on which… see where I&#8217;m going? Nowhere! ;-)<br />
The portage tree fulfills the role of a package database for us Gentoo ricers. I need it locally.</p>
<p>Diego, one of the Gentoo devs, wrote a blog post <a href="http://blog.flameeyes.eu/2009/10/12/and-finally-the-portage-tree-overhead-data">about space inefficiency incurred through the use of many small files in the portage tree</a>. He puts the Portage tree on different filesystems to arrive at an accurate and detailed picture of incurred overhead.</p>
<p>I wanted to quickly find out what filesystem block size would suit different parts of my filesystem (such as /etc/, /usr/src/linux/, /usr/portage/, /var/db/pkg/) best, and how much I could save. Testing all block sizes with all parts of my FS was not very appealing, so I decided to simply calculate the file slack and wrote this simple Python script:</p>
<p><a href="http://smormedia.gavagai.nl/dist/foobar/slacktastic.py"><code>slacktastic.py</code></a></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python3</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
slacktastic.py - calculate filesystem file slack for different block sizes.
Invoke me thusly:
    find /path/to/tree -xdev -type f -printf &quot;%s<span style="color: #000099; font-weight: bold;">\\</span>n&quot; | slacktastic.py 4096
for a calculation using the contents below /path/to/tree with a block size of 4096 bytes.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>, functools, <span style="color: #dc143c;">math</span>
&nbsp;
blksz = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
  blksz = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: black;">&#40;</span><span style="color: #008000;">IndexError</span>,<span style="color: #008000;">ValueError</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>__doc__, <span style="color: #008000;">file</span>=<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'I need a blocksize as the first argument.<span style="color: #000099; font-weight: bold;">\n</span>'</span>, <span style="color: #008000;">file</span>=<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: black;">&#41;</span>
  <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
sizes = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>strsize<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> strsize <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
sumslack = functools.<span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> sumslack, sz: sumslack + <span style="color: black;">&#40;</span>blksz - <span style="color: black;">&#40;</span>sz <span style="color: #66cc66;">%</span> blksz<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, sizes, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
sumblks = functools.<span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> sumblks, sz: sumblks + <span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>sz / blksz<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, sizes, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
sumsizes = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'{:n} total slack'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>sumslack<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'{:n} bytes in files'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>sumsizes<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'{0:n} total blocks of {1} bytes'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>sumblks,blksz<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'{:.2%} inefficiency'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span> sumslack / <span style="color: black;">&#40;</span>sumblks<span style="color: #66cc66;">*</span>blksz<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This doesn&#8217;t take into account things such as tail packing (à la ReiserFS), compression (Btrfs), or directory slack. Just file slack.<br />
On my laptop filesystem, with a 4096 block size, this leads to the following observation:</p>
<pre>
find /var/db/pkg -xdev -type f -printf "%s\n" | slacktastic.py 4096
171942364 total slack
92655140 bytes in files
64506 total blocks of 4096 bytes
65.08% inefficiency
</pre>
<p>My <code>/var/db/pkg</code>, Gentoo&#8217;s &#8216;database&#8217; of installed packages (containing their build environment and all kinds of stuff you wouldn&#8217;t need on a binary distro) contains 65% air! That&#8217;s 170 megs of waste which I don&#8217;t want that on my Alix&#8217;s 4GB CF card. With a 1024 byte block size — Ext4&#8217;s minimum — the situation is better, but it&#8217;s still over 40 megs of hot air.</p>
<p>I ended up choosing btrfs with compression. It has a fixed 4096 byte leaf/node size but it does tail packing (good for small files) and compression (for my /var/log). My script is useless for estimations on such a filesystem so I ran some actual tests and it turns out I can fit my /usr/src/linux, /var/db/pkg, /var/log and /usr/portage on a 1GB btrfs filesystem. They didn&#8217;t fit on an bs=1024b Ext4 FS.</p>
]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2012/01/file-slack-space-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Homemade dynamic DNS service</title>
		<link>http://smorgasbord.gavagai.nl/2011/08/homemade-dynamic-dns-service/</link>
		<comments>http://smorgasbord.gavagai.nl/2011/08/homemade-dynamic-dns-service/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 17:35:38 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[dyndns]]></category>
		<category><![CDATA[English]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1382</guid>
		<description><![CDATA[For a little while I&#8217;ve been running a DIY dynamic DNS service. My peers think it&#8217;s convenient and pretty sweet, so in this post I&#8217;ll set things straight — a full disclosure on the mess that it really is ;-)
What is a dynamic DNS service?
Since the dial-up days of yore I have occasionaly been using [...]]]></description>
			<content:encoded><![CDATA[<p>For a little while I&#8217;ve been running a DIY dynamic DNS service. My peers think it&#8217;s convenient and pretty sweet, so in this post I&#8217;ll set things straight — a full disclosure on the mess that it really is ;-)</p>
<h2>What is a dynamic DNS service?</h2>
<p>Since the dial-up days of yore I have occasionaly been using &#8220;dynamic DNS&#8221; services. What is a &#8220;dynamic DNS&#8221; service, you ask? Well, it&#8217;s a marketing term, not a technical term, but most providers of such services allow for having some CNAME or A record point to an IP, and this pairing is updatable over HTTP. Usually they use a freemium business model where you can get a couple of subdomains on their second level domains.</p>
<h3>Why use one?</h3>
<p>An example of a use case: You run a development server on your laptop. You want someone to connect to it. Instead of saying to this person &#8220;now just go to&#8230; errr&#8230; hold on&#8221; (and now you pop your shell and run <code>ip a s dev wlan0</code>)<A NAME="tex4html1"  HREF="#tex4foot1"><SUP>1</SUP></A> &#8220;yes I&#8217;m back, you need to go to 110.34.56.250&#8243; you can just say &#8220;go to kleinebeer.ath.cx&#8221;. Much easier. And that&#8217;s with IPv4. Typing an IPv6 address that someone just yelled at you from across the room is an even less joyful experience.<br />
To enable this use case you configured your machine in such a way that every time your interface aquires a new IP, a program contacts the &#8220;dynamic DNS service&#8221; to update the &#8216;kleinebeer&#8217; DNS record to reflect this IP. There are many clients for such services out there. I have even seen domestic ADSL modem/routers on which the factory firmware contains such a client.</p>
<h2>Doing it differently</h2>
<p>But some time ago I got fed up with all the &#8220;account expiration warning&#8221; emails I had been getting from the service that I used. And since I&#8217;m quite familiar with the components you need for building such a service yourself, I set out to do so. The thing I ended up with is for personal use, specific to my needs, KISS, and sinful.</p>
<h3>Sins!</h3>
<p>It allows for instant creation/updating of DNS records through a very simple HTTP GET. That is sinful for two reasons:</p>
<ol>
<li>No authentication and authorization.
<p>Anyone can create any record. And anyone can update any record he or she knows the name of. You understand how this could be a bad thing. Don&#8217;t have such names as MX records unless you don&#8217;t mind that mail intended for you ends up on someone else&#8217;s server.<br />
At the same time it is a good thing, as I will show in the use case example below.
</p>
</li>
<li>The HTTP GET alters state.
<p>And that&#8217;s just plain Bad Taste. If you&#8217;re designing a web service you&#8217;ll usually try to make it RESTful. GET should be a safe method. But for this application, at the frontend, it is actually desirable — because grandma can GET, as you will see in the use case below.
</p>
</li>
</ol>
<h3>Use cases</h3>
<ul>
<li>
<h4>Case A:</h4>
<p>
Imagine you&#8217;re on the phone with your grandma, and after discussing apple pie recipes something else comes up and you need to know her IP (let&#8217;s say you need to SSH in and help her with her crontab). She hasn&#8217;t registered with any dynamic DNS service, she has a hard time determining her IP, she has a hard time reading it out loud without making mistakes, and you are fed up with typing IPs anyway. But she has a web browser. Wouldn&#8217;t it be nice if you could just tell her to open her browser and head over to http://grandma.reg-a-record.tld/reg.php ? Nothing to install, nothing to look up, and something reasonably simple for grandma to do. The script on http://grandma.reg-a-record.tld/reg.php creates a DNS A record named &#8216;grandma&#8217; on dyndomain.tld, and this record points to your grandma&#8217;s IP. Now you can just do &#8217;ssh grandma.dyndomain.tld&#8217;.
</p>
</li>
<li>
<h4>Case B:</h4>
<p>
Imagine you and your friends are on a coding binge, all running their Django/Rails/Node.js/Thing-du-jour instances on laptops, in the same room, and you all want to connect to each other&#8217;s development servers.<br />
You could all call out your IPs across the room and let everyone type in everyone else&#8217;s IPs. Now you all have N tabs open in your browsers. Who was 123.231.101.45 again?<br />
Or, everyone could just point their browser (or curl, wget&#8230;) to http://theirfirstname.reg-a-record.tld/reg.php<A NAME="tex4html2"  HREF="#tex4foot2"><SUP>2</SUP></A> and be done with it. If you want to view Fred&#8217;s progress you just go to fred.dyndomain.tld.
</p>
</li>
</ul>
<p>So, sin #1 &#038; #2 allow for use cases that were not possible with the provider I was using.</p>
<p>You don&#8217;t have to register the records up front with some service provider. You can make up records as you go, and you want to be able to let anyone create any record. Hence sin #1. But it&#8217;s a good thing. You don&#8217;t have to reuse records, which is better for your privacy. With the commercial service I could only use a couple of names, so anyone whom I once had told to go to kleinebeer.ath.cx could kinda virtually follow me around and determine whether I was at work, home, uni or girlfriend by resolving this record. When creating new records is incredibly easy, there&#8217;s no reason to keep using the same name every time.</p>
<h2>On to the code</h2>
<p>It&#8217;s very, very simple! You need very little code. Things can be made much simpler than I have done. For byzantine reasons I use two webservers, one has a python CGI which creates the actual records, and the other one runs the public-facing PHP script (through mod_php) which you actually connect to.</p>
<h3>DNS server</h3>
<p>You need a domain for which you run the authoritative name server. I&#8217;m using the <a href="http://threading.2038bug.com/sheerdns/">SheerDNS</a> DNS server package on mine, for no good reason. <a href="http://mestvaelt.gavagai.nl:8080/raw-file/ba5d1168cb33/net-dns/sheerdns/files/sheerdns-1.0.1-cmode.patch">Here&#8217;s a patch</a> I made so that <code>sheerdnshash</code> creates directories with some group permission bits set, something I need in my setup.<br />
You can use other DNS servers, of course. I discovered that the <a href="http://unbound.net/">Unbound</a> DNS server has a socket protocol which you can possibly use to add records on the fly. But I use sheerdns now, and here&#8217;s a Python module I made to write A-records in sheerdns&#8217;s directory structure. Public domain, use it as you see fit. You need to modify the constants to reflect your config, and you can test it by running the module from a shell.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>,<span style="color: #dc143c;">os</span>,<span style="color: #dc143c;">string</span>,<span style="color: #dc143c;">subprocess</span>,<span style="color: #dc143c;">sys</span>
&nbsp;
SHEERDNS_DIR=<span style="color: #483d8b;">&quot;/var/sheerdns&quot;</span>
SHEERDNS_HASH=<span style="color: #483d8b;">&quot;/usr/sbin/sheerdnshash&quot;</span>
DYNDOMAIN=<span style="color: #483d8b;">&quot;dyndomain.tld&quot;</span>
ALLOWCHARS=<span style="color: #dc143c;">string</span>.<span style="color: black;">ascii_letters</span> + <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span> <span style="color: #483d8b;">&quot;%d&quot;</span> <span style="color: #66cc66;">%</span> i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'-'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> mk_A<span style="color: black;">&#40;</span>name,ip<span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;">#are host and ip allright?</span>
	host = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span> c <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> name <span style="color: #ff7700;font-weight:bold;">if</span> c <span style="color: #ff7700;font-weight:bold;">in</span> ALLOWCHARS <span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> host:
	  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
	<span style="color: #ff7700;font-weight:bold;">try</span>:
	  <span style="color: #dc143c;">socket</span>.<span style="color: black;">inet_aton</span><span style="color: black;">&#40;</span>ip<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">socket</span>.<span style="color: black;">error</span>:
	  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
	fqdn = <span style="color: #483d8b;">&quot;%s.%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>host, DYNDOMAIN<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#set the umask</span>
	oldumask = <span style="color: #dc143c;">os</span>.<span style="color: black;">umask</span><span style="color: black;">&#40;</span>0002<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#get the hash</span>
	<span style="color: #008000;">hash</span> = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>SHEERDNS_HASH, fqdn<span style="color: black;">&#93;</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>.<span style="color: black;">communicate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#construct the dirpath</span>
	path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>SHEERDNS_DIR,<span style="color: #008000;">hash</span>,fqdn<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#chmod it for everyone to read</span>
	<span style="color: #dc143c;">os</span>.<span style="color: black;">chmod</span><span style="color: black;">&#40;</span>path,0755<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#construct the A-record-filepath</span>
	apath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>path,<span style="color: #483d8b;">'A'</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#open the record file</span>
	<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>apath,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> record:
		record.<span style="color: black;">write</span><span style="color: black;">&#40;</span>ip<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #dc143c;">os</span>.<span style="color: black;">umask</span><span style="color: black;">&#40;</span>oldumask<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> name
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">3</span>:
          <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>mk_A<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,<span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Web &#8217;service&#8217; to create the records: a CGI</h3>
<p>This is a Python CGI which uses the above module. I stick it behind HTTP Basic authentication which is handled by the web server. You call it like this: http://server_that_has/the_CGI?name=therecordname&#038;ip=theip .<br />
There&#8217;s no reason that this CGI should be guilty of Sin #2, except laziness on my behalf. If I&#8217;d be following every convention even for silly little solo projects I&#8217;d never get anything done, all right?<br />
It doesn&#8217;t propagate any errors from the sheerdns library, but it prints the name as it is registered — stripped of disallowed characters, or nothing if something failed along the way (invalid IP, for instance).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> sheerdns
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cgi</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Content-type: text/plain<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">;</span>
params = <span style="color: #dc143c;">cgi</span>.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
name, ip = params.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span><span style="color: black;">&#41;</span>, params.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ip'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> ip <span style="color: #ff7700;font-weight:bold;">and</span> name:
  dnsname = sheerdns.<span style="color: black;">mk_A</span><span style="color: black;">&#40;</span>name<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,ip<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> dnsname</pre></div></div>

<h3>The accessible part: reg.php</h3>
<p>This is in PHP. It lives in the default virtual host of my Apache config, and for good reasons. I have a CNAME record that points *.reg-a-record.tld to this web server. I then use whatever is in * to construct the name for the dyndomain.tld-record. The wildcard record is essential.<br />
You need to modify this script to reflect your config.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hostparts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$cha</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://server_that_has/the_CGI?name='</span><span style="color: #339933;">.</span><span style="color: #000088;">$hostparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;ip='</span><span style="color: #339933;">.</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYPEER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">True</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #339933;">,</span> CURLOPT_USERPWD<span style="color: #339933;">,</span> <span style="color: #0000ff;">'bite:me'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$resp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cha</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Presto</h3>
<p>If you visit http://kitten.reg-a-record.tld/reg.php, the IP address that the web server sees you coming from gets registered as kitten.dyndomain.tld. If everything went well, the script responds with this mapping.<br />
You could set <code>DirectoryIndex reg.php</code> here, or rename reg.pgp to index.php, if you don&#8217;t want users to type &#8216;reg.php&#8217;. I <b>did</b> make it explicit, because web sites on this server come and go and I don&#8217;t want people to stumble onto reg.php every time they hit the default vhost because their web site doesn&#8217;t exist any more.</p>
<p><A NAME="tex4foot1"></A><A HREF="#tex4html1">1)</A>Let&#8217;s assume you&#8217;re not NATed.<br />
<A NAME="tex4foot2"></A><A HREF="#tex4html2">2)</A>Try to only have friends with distinct names that can be expressed in ASCII.</p>
]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2011/08/homemade-dynamic-dns-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Short scripts and kludges</title>
		<link>http://smorgasbord.gavagai.nl/2011/04/short-scripts-and-kludges/</link>
		<comments>http://smorgasbord.gavagai.nl/2011/04/short-scripts-and-kludges/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 22:14:42 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[English]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1307</guid>
		<description><![CDATA[While decrufting my homedir I found some scripts that might be of public* interest ­— either because of their amusement value, or their utility ;-)
Some are so tiny that you&#8217;d actually be better off just defining them as functions in your ~/.{ba,z}shrc .
 *) For unixoid values of &#8216;public&#8217;.
groe
Open a file (argument 2) in joe [...]]]></description>
			<content:encoded><![CDATA[<p>While decrufting my homedir I found some scripts that might be of public* interest ­— either because of their amusement value, or their utility ;-)<br />
Some are so tiny that you&#8217;d actually be better off just defining them as functions in your ~/.{ba,z}shrc .</p>
<p><span style="font-size: smaller;"> *) For unixoid values of &#8216;public&#8217;.</span></p>
<h3>groe</h3>
<p>Open a file (argument 2) in joe (works with vim, too) with the cursor on the first line matching the regex in argument 1.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -eu</span>
<span style="color: #007800;">THEL</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-m</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${1}</span> <span style="color: #800000;">${2}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">':'</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$THEL</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> joe +<span style="color: #007800;">$THEL</span> $<span style="color: #000000;">2</span></pre></div></div>

<h3>paserv</h3>
<p>Set pulseaudio server (in X11, for display in $DISPLAY env var) to argument 1. Useful if you use several pulseaudio network audio servers. Without arguments, resets to default.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> pax11publish <span style="color: #660033;">-e</span> <span style="color: #660033;">-S</span> <span style="color: #800000;">${1}</span>
<span style="color: #000000; font-weight: bold;">else</span> pax11publish <span style="color: #660033;">-e</span> -r; <span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<h3>gmrun</h3>
<p>Wrapper launcher for the excellent &#8220;<a href="http://sourceforge.net/projects/gmrun/">gmrun</a>&#8221; program launcher. (queue &#8220;yo dawg&#8221; meme).<br />
Raises and/or pulls any existing gmrun windows to your current desktop.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>wmctrl <span style="color: #660033;">-x</span> <span style="color: #660033;">-b</span> add,raise <span style="color: #660033;">-R</span> <span style="color: #ff0000;">&quot;gmrun.Gmrun&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;/home/boer/bin:<span style="color: #007800;">$PATH</span>&quot;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>gmrun</pre></div></div>

<h3>getx</h3>
<p>Get an X11 authorization cookie for a display on some other host, over SSH.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #800000;">${1}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #007800;">$(basename ${0})</span> sshstanza&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">SSH</span>=<span style="color: #800000;">${1}</span>
<span style="color: #007800;">XHO</span>=<span style="color: #800000;">${1##*@}</span>
<span style="color: #007800;">DIS</span>=<span style="color: #800000;">${2-&quot;:0&quot;}</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #800000;">${SSH}</span> <span style="color: #ff0000;">&quot;xauth list <span style="color: #007800;">${XHO}</span><span style="color: #007800;">${DIS}</span> | sort -u&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line; <span style="color: #000000; font-weight: bold;">do</span> xauth add <span style="color: #800000;">${line}</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<h3>gaap</h3>
<p>Ghetto power management. We don&#8217;t need no stinkin&#8217; gnome-power-manager, we&#8217;ll just grep our keyboard/trackpad interrupt counter thankyouverymuch (I got that idea from reading the xscreensaver man page).<br />
Comes in two parts.<br />
<code>is_user_alive</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -eu</span>
<span style="color: #666666; font-style: italic;">#Succeeds if keystrokes or touchpad input has taken place since last time this was run, or if there is no record of the last run.</span>
&nbsp;
<span style="color: #007800;">RECORD</span>=<span style="color: #800000;">${1:-/dev/shm/i8042_intcnt}</span>
&nbsp;
<span style="color: #007800;">RETVAL</span>=<span style="color: #ff0000;">&quot;1&quot;</span>
&nbsp;
upd_intcnt<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #c20cb9; font-weight: bold;">grep</span> i8042 <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>interrupts <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${RECORD}</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
is_alive<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #c20cb9; font-weight: bold;">grep</span> i8042 <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>interrupts <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">diff</span> - <span style="color: #800000;">${RECORD}</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">RETVAL</span>=<span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${RECORD}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> upd_intcnt <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
is_alive
upd_intcnt
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #007800;">$RETVAL</span></pre></div></div>

<p>Run this one (<code>gaap.sh</code>) from cron:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -eu</span>
&nbsp;
misschien_slapen<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>koffie <span style="color: #000000; font-weight: bold;">||</span> on_ac_power <span style="color: #000000; font-weight: bold;">||</span> echt_slapen
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
echt_slapen<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>is_user_alive <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>shm<span style="color: #000000; font-weight: bold;">/</span>gaap_alive <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;Hibernating in 30 secs, unless you prove you're alive by pressing<span style="color: #000099; font-weight: bold;">\n</span>a key or touching the touchpad. Physically. On <span style="color: #007800;">${HOSTNAME}</span>.&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wall</span>
  <span style="color: #000000; font-weight: bold;">for</span> sec <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">0</span> <span style="color: #000000;">3</span> <span style="color: #000000;">99</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$sec</span>; <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">DISPLAY</span>=<span style="color: #ff0000;">&quot;:0&quot;</span> <span style="color: #007800;">XAUTHORITY</span>=<span style="color: #ff0000;">&quot;/home/boer/.Xauthority&quot;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-u</span> boer zenity <span style="color: #660033;">--progress</span> <span style="color: #660033;">--auto-close</span> <span style="color: #660033;">--text</span> <span style="color: #ff0000;">&quot;hibernate?&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
  <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>is_user_alive <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>shm<span style="color: #000000; font-weight: bold;">/</span>gaap_alive <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>pm-hibernate
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>is_user_alive <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>shm<span style="color: #000000; font-weight: bold;">/</span>gaap_alive <span style="color: #000000; font-weight: bold;">||</span> misschien_slapen</pre></div></div>

<p>You might want to install <a href="http://live.gnome.org/Zenity">zenity</a>, and you&#8217;ll have to adjust the username. As you can see my laptop is a single-user system. The sudo is therefore a bit silly, but hey, it&#8217;s good practice to not present privilege escalation attack surfaces to yourself ;-)<br />
I still need to find a good way of iterating over all X11 displays and finding the user who started the associated X server. Does anyone know of a not-too-hackish approach?</p>
<h3>Aphorism slideshow</h3>
<p><div id="attachment_1318" class="wp-caption alignleft" style="width: 160px"><img src="http://smorgasbord.gavagai.nl/wp-content/uploads/2011/04/moetgaat-150x150.jpg" alt=" " title="moetgaat" width="150" height="150" class="size-thumbnail wp-image-1318" /><p class="wp-caption-text"> </p></div><br />
A friend of mine got married this summer and I got the newlyweds one of those digital photo frames. I preloaded it with a bunch (5.5K) of images generated from my <a href="http://en.wikipedia.org/wiki/Fortune_%28Unix%29">unix fortune</a> database. So it&#8217;s kind of a modern incarnation of those tiles that older Dutch generations used to cement onto their walls (<a href="http://nl.wikipedia.org/wiki/Tegeltjeswijsheid">Wikipedia entry [Dutch]</a>). Terrible as they are, they&#8217;re making a (campy) comeback. Personally I can&#8217;t wait for the day that camp culture itself becomes camp (queue another &#8220;yo dawg&#8221; meme).<br />
<br/><br />
Anyway, I made myself this sed script, &#8220;str2cu.sed&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/^%$/d
s/&quot;\([^&quot;]*\)&quot;/“\1”/g</pre></div></div>

<p>and then did this. Which, the better part of a year later, proves quite hard to reverse-engineer:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> CATG <span style="color: #000000; font-weight: bold;">in</span> art definitions education fightclub food hitchhiker humorists kids literature love medicine men-women news paradoxum pets platitudes politics science smac strangelove wisdom work; \
<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span>; <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span>; \
csplit <span style="color: #660033;">-q</span> <span style="color: #660033;">-z</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>fortune<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span> <span style="color: #ff0000;">'/%/'</span> <span style="color: #ff0000;">'{*}'</span> ; <span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span><span style="color: #000000; font-weight: bold;">/*</span>; \
<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$f</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span>str2cu.sed <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">fmt</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">2500</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #660033;">-s</span> <span style="color: #ff0000;">'\t'</span> <span style="color: #000000; font-weight: bold;">|</span> expand <span style="color: #660033;">-t</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortune; \
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortune <span style="color: #007800;">$f</span>; <span style="color: #000000; font-weight: bold;">done</span>; <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> f <span style="color: #660033;">-size</span> +200c <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">'{}'</span> \; ; \
<span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>boer<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>fortunes<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$CATG</span><span style="color: #000000; font-weight: bold;">/*</span>; <span style="color: #000000; font-weight: bold;">do</span> convert <span style="color: #660033;">-background</span> pink <span style="color: #660033;">-fill</span> white <span style="color: #660033;">-size</span> 480x234 \
<span style="color: #660033;">-stroke</span> black <span style="color: #660033;">-gravity</span> Center <span style="color: #660033;">-font</span> Essays1743-Bold caption:<span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$f</span> <span style="color: #007800;">$f</span>.jpg; <span style="color: #000000; font-weight: bold;">done</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>GNU textutils are fun&#8230; but holy cow. What an obtuse kludge.<br />
A simple python script could have done all the work up until the image generation (which is done with imagemagick, it has a nice auto text scaling feature, did you know?).</p>
]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2011/04/short-scripts-and-kludges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Social social networking with Facemix</title>
		<link>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 16:50:52 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WWW]]></category>
		<category><![CDATA[aprilfools]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[squid]]></category>
		<category><![CDATA[upsidedownternet]]></category>
		<category><![CDATA[url_rewrite_program]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1007</guid>
		<description><![CDATA[It&#8217;s not too late for posts about April Fool&#8217;s Day pranks I hope?
In the tradition of the Upsidedownternet this April 1st I had some fun with Facebook addicts.
You may not be aware of the fact that any picture on facebook is publicly accessible. Yes, it is. There&#8217;s no authentication &#038; authorisation whatsoever. Handling those in [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not too late for posts about April Fool&#8217;s Day pranks I hope?<br />
In the tradition of <a href="http://www.ex-parrot.com/pete/upside-down-ternet.html">the Upsidedownternet</a> this April 1st I had some fun with Facebook addicts.</p>
<p>You may not be aware of the fact that any picture on facebook is publicly accessible. Yes, it is. There&#8217;s no authentication &#038; authorisation whatsoever. Handling those in a scalable way would ramp up costs. Your privacy is not worth those costs. Contrary to the impression you are trying to deliver through your profile, you are not important. Happy shareholders are important!</p>
<p>Due to this fact I just need to know the URLs of your pictures. From the URL I can determine whether it&#8217;s a profile picture, profile picture thumbnail, photo, photo thumbnail, etc.</p>
<p>Wouldn&#8217;t it be fun to mix the pictures of the facebook page you are currently viewing with those from facebook pages others are viewing? So when you&#8217;re browsing your friend&#8217;s albums, you not only see his pictures but pictures from other peoples&#8217; albums too, and vice versa?<br />
The pictures may be requested by the guy across the bar, or by the girl one floor down in the library, or by anyone on the same network as you are — all of you are browsing together with the people in your physical vicinity, sharing whatever pictures you encounter! It&#8217;s beyond Facebook. It&#8217;s crowdbrowsing. It&#8217;s Megafacebook.<br />
While you may not know these newly inserted friends, you might get to. Maybe you bump into one another at the toilets, or at the counter.<br />
<i>&#8220;Why is everyone staring at me like that?&#8221;</i> you naively wonder. (They&#8217;ve seen those pictures).<br />
<i>&#8220;Does she know that I know about those pictures of her and her friends? But wait&#8230; what might she know about me?&#8221;</i>, your paranoid mind ponders.<br />
It&#8217;s all about what you think of others and what others think of you. Total absorption. Now that&#8217;s what I call social networking. All hail Facebook <b><i>Social!</i></b></p>
<p><a href="http://smorgasbord.gavagai.nl/wp-content/uploads/2010/04/FacebookSocial3.png"><img src="http://smorgasbord.gavagai.nl/wp-content/uploads/2010/04/FacebookSocial3.png" alt="Facebook Social" title="Facebook Social" width="90" height="20" class="aligncenter size-full wp-image-1009" /></a></p>
<p>Give the wifi crowd at your local coffeeshop the pleasure of learning a little bit more about eachothers lives and friends.</p>
<h3>Get to work</h3>
<p>You need:</p>
<ul>
<li>one network vulnerable to ARP poison routing (that&#8217;s most of them) or one network which you already control anyway. Make everyone route their traffic through your machine.
</li>
<li>one installment of the Nginx web server, configured with <code>--with-http_random_index_module</code>. I use the 0.8.3x series.
</li>
<li>one installment of the Squid http proxy server. I use the 3.1 series.
</li>
<li>Perl and LWP::Simple.
</li>
</ul>
<h4>Set up Nginx</h4>
<p>Create some directories to hold the images:<br />
<code><br />
mkdir /var/www/facemix/{albums,photos,photosthumb,smoelen,smoelenthumb}<br />
</code><br />
Tell Nginx to respond to requests for those directories by randomly serving one of the files in them:<br />
<code><br />
        location ~ ^/facemix/([^/]+)(/?.*)$ {<br />
                alias /var/www/facemix/$1/$2;<br />
                random_index on;<br />
                expires -1;<br />
                }<br />
</code><br />
You need the &#8216;expires -1&#8242; to avoid caching. If proxies or user agents were to cache the results, they wouldn&#8217;t be very random anymore now would they.</p>
<p>Stick some files in there and test your installation.</p>
<h4>Set up Squid</h4>
<p>Set up squid in interception mode. If you&#8217;re not NATting the routed traffic, set it to run on port 80. If Nginx is already listening on that socket, make Nginx listen on some other port, or localhost only, while running squid on port 80 but only on the external interface.</p>
<h4>Set up networking</h4>
<p>This is for iptables.</p>
<ul>
<li>You&#8217;re NATting the pwned hosts. Run something along the lines of<br />
<code>iptables -t nat -A PREROUTING -i $INTERFACE -p tcp --dport 80 -j REDIRECT --to-port 8080</code><br />
to redirect all traffic incoming on $INTERFACE and destined for port 80 to port 8080, which is where you need squid to listen on.</li>
<li>You&#8217;re doing 2-way ARP poisoning (cheers!). Run something along the lines of<br />
<code>iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j DNAT --to-destination $YOURIP</code><br />
Squid needs to run on port 80 on interface with IP $YOURIP.
</li>
</ul>
<p>Check Squid&#8217;s logs to verify that requests are intercepted successfully.</p>
<h4>Run the redirection script</h4>
<p>I don&#8217;t touch Perl very often, and cobbling together this script made me remember why that is. It&#8217;s very usable as a means of frightening little kids.<br />
In a nutshell, what my redirector script does is</p>
<ol>
<li>determine whether the URL fed to it by Squid is a facebook picture url;
</li>
<li>if so, and if we don&#8217;t have that picture yet, fork off to download it;
</li>
<li>point Squid to a random picture of the same type (served by Nginx).
</li>
</ol>
<p>I like the forking. I dislike the iffed regexes which could probably be condensed into one but then it wouldn&#8217;t be &#8216;cobbling together&#8217; anymore. </p>
<p>Adjust the variables for your setup and tell Squid about the script (eg <code>url_rewrite_program /usr/local/lib/facemix-squidredir.pl</code>).</p>
<p>The Facebook logo will change to reflect the fact that the users are now browsing facebook in <b><i>Social!</i></b> mode.</p>
<p>One further note: This is privacy-invasive. I brush away my moral doubts by stating that anyone who signed away their privacy rights when joining facebook AND AT THE SAME TIME entertains any expectations with respect to privacy,<br />
« inhale »<br />
&#8230; is utterly mental and has completely lost any and all sense of proportionality. If you care about privacy, why use a service which lets you view <b>any</b> picture of <b>any</b> user <i>regardless</i> of who you are? Who are you kidding?</p>
<p>If you&#8217;re still reading, here&#8217;s the script:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$WEBROOT</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'http://localhost/facemix/'</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$WEBDIR</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'/var/www/facemix/'</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$CHANCE</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#One in X requests gets mixed</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>CHLD<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'IGNORE'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$|</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">@reqfrags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@reqfrags</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span>    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/.*.fbcdn.net\/rsrc.php\/z7VU4\/hash\/66ad7upf.png$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;http://smormedia.gavagai.nl/2010/04/FacebookSocial2.png<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span>    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_n.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/n.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'photos/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_s.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/s.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'photosthumb/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/.*_n.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/n.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'smoelen/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/.*_q.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/q.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'smoelenthumb/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_a.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/a.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'albums/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$url</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> mixurl <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">#args: subdir, url</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$vork</span> <span style="color: #339933;">=</span> <span style="color: #000066;">fork</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vork</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&amp;getit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$CHANCE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$WEBROOT</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getit <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">#args: subdir, url</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$storedir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$WEBDIR</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">@urlfrags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\//</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@urlfrags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066;">stat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      getstore<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #ff0000;">'._tmp-'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">rename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #ff0000;">'._tmp-'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Network security 101: &#8216;Stealth mode&#8217; + ARP cache inoculation</title>
		<link>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:28:48 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[arp spoofing]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=820</guid>
		<description><![CDATA[There are times you need to connect to &#8216;dirty&#8217; networks such as public WiFi hotspots. Hopefully you&#8217;re ensuring that sensitive information is encapsulated in transport layer security enabled protocols such as SSL, because anyone on the same link (in the case of WiFi, that&#8217;s the air surrounding you. A vacuum will do, too, but that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>There are times you need to connect to &#8216;dirty&#8217; networks such as public WiFi hotspots. Hopefully you&#8217;re ensuring that sensitive information is encapsulated in transport layer security enabled protocols such as SSL, because anyone on the same link (in the case of WiFi, that&#8217;s the air surrounding you. A vacuum will do, too, but that&#8217;s less common) can listen in on the traffic you&#8217;re sending. With SSL encapsulation such as HTTP over SSL (https://), your traffic can still be read — but for those who do it&#8217;s an extremely boring read because they don&#8217;t know the session key, only you and the other endpoint do. Hopefully.</p>
<p>One particularly nasty thing that can happen to you is when your machine is subverted into using the attacker&#8217;s machine as the router.  That is known as <a href="http://en.wikipedia.org/wiki/Arp_poisoning">ARP poison routing</a>. The attacker can proceed to not only read the traffic coming from your machine (which, on a shared medium, could be done anyway), or read the traffic going into your machine (again: on a shared medium, that could be done anyway), but the attacker can now also <em><strong>modify</strong></em> the traffic between you and the rest of the non-local network, e.g., the internet, in both directions. And that&#8217;s when he can really go to town with your traffic. Injecting a <a href="http://code.google.com/p/middler/">javascript keylogger</a> into all the webpages you visit. &#8216;<a href="http://hamster.erratasec.com/help/index.html">Sidejacking</a>&#8216; your sessions, so he does not even need to know your passwords, just your session cookies — which you happen to transmit with every page request.</p>
<p>All possible unless you use transport layer security, which is tamper-proof once properly set up. <em><strong>Once properly set up</strong></em>. But setting up can have problems of itself — there are ways of preventing you ever going from HTTP to HTTPS. If you know a thing or two about HTTP and SSL you&#8217;ll be <a href="http://securitytube.net/Defeating-SSL-using-SSLStrip-%28Marlinspike-Blackhat%29-video.aspx">delighted</a> to learn about</a> Moxie&#8217;s <a href="http://www.thoughtcrime.org/software/sslstrip/">very evil but very clever ways</a> of doing so.</p>
<p>Anyway, some level of security can be achieved if you tell your machine to ignore any messages sent to you from the other machines on the local network. That includes messages that will make your machine believe that the router has suddenly changed its physical address — which is quite unlikely to happen, but those messages are exactly the type of message an impersonator would send you. Of course we&#8217;d need to whitelist the routers of the network, otherwise we can&#8217;t get traffic out of it and onto other networks. DNS resolvers will need whitelisting too, unless you&#8217;re running one on your own machine (probably not).<br />
Not openly announcing your presence may also be something you wish for. If you have ever been on a network with a Mac user you have probably seen them popping up in your Zeroconf service browser as &#8220;Firstname Lastname&#8217;s iSomething&#8221;. Let&#8217;s cut down on that kind of promiscuity, too. But you should understand now that you can not actually hide unless you turn off your WiFi. Shared medium, remember?</p>
<p>I prepared a simple script to accomplish the above. I&#8217;ve used <code>ip</code> from the iproute2 package instead of sticking to old-school <code>route</code>, <code>ifconfig</code>, <code>arp</code> &#038; co. And I must say <code>ip neigh flush nud stale</code> has a poetic ring to it, <a href="http://ars.userfriendly.org/cartoons/?id=20100124">wouldn&#8217;t you agree?</a></p>
<p>Take note: this will only protect you from some kind of attacks, and only partially. An attacker has a window of opportunity between your machine getting assigned a DHCP lease and you running this script, for instance. Or maybe the access point <a href="http://www.viddler.com/explore/hak5/videos/56/">is rigged</a>. Actually all protection other than end-to-end encryption combined with mutual authentication is pretty useless on shared networks ;-)</p>
<p>Here&#8217;s the script. Linux-only. If you want to use it, get the latest version from <a href="http://smormedia.gavagai.nl/dist">my public repository</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># arpshield 0.2</span>
<span style="color: #666666; font-style: italic;"># Protects against ARP poisoning and cloaks your machine for all </span>
<span style="color: #666666; font-style: italic;"># local link devices but the router(s) and the DNS server(s).</span>
<span style="color: #666666; font-style: italic;"># Whitelisting DHCP servers also works if you use the dhcpcd program</span>
<span style="color: #666666; font-style: italic;"># to obtain DHCP leases.</span>
<span style="color: #666666; font-style: italic;"># This program is of no help if your setup is already poisoned.</span>
<span style="color: #666666; font-style: italic;"># Have a look at ArpON (http://arpon.sourceforge.net/manpage.html) if</span>
<span style="color: #666666; font-style: italic;"># you need more extensive protection.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Needs 'ip', 'awk', 'sed', 'arptables', and 'arping' and expects</span>
<span style="color: #666666; font-style: italic;"># them on $PATH. Needs appropriate privileges (so use sudo).</span>
<span style="color: #666666; font-style: italic;"># Takes a network interface as an argument. The network interface</span>
<span style="color: #666666; font-style: italic;"># should be up and configured. If no argument is given, clear all</span>
<span style="color: #666666; font-style: italic;"># rules. Obviously you should do that before connecting to a new</span>
<span style="color: #666666; font-style: italic;"># network.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Copyright 2010 Wicher Minnaard (wicher@gavagai.eu)</span>
<span style="color: #666666; font-style: italic;"># License: Creative Commons Attribution-Share Alike 3.0</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Do you use dhcpcd for aquiring DHCP leases? And is it running?</span>
<span style="color: #007800;">dhcpcdLEASEFILE</span>=<span style="color: #ff0000;">&quot;/var/lib/dhcpcd-<span style="color: #007800;">${1}</span>.info&quot;</span>
<span style="color: #007800;">dhcpcdPIDFILE</span>=<span style="color: #ff0000;">&quot;/var/run/dhcpcd-<span style="color: #007800;">${1}</span>.pid&quot;</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${dhcpcdLEASEFILE}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${dhcpcdPIDFILE}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #800000;">${dhcpcdLEASEFILE}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># In case you lack the luxury of dhcpcd, where is your resolv.conf?</span>
<span style="color: #007800;">RESOLV</span>=<span style="color: #ff0000;">&quot;/etc/resolv.conf&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># No user-servicable parts below this line.</span>
<span style="color: #007800;">DEV</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># I know, I know. But if your routing table contains 0.333.456.789 you have bigger problems ;-)</span>
<span style="color: #007800;">IPREGEX</span>=<span style="color: #ff0000;">&quot;\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Register</span>
<span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If not run as root, bail</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(id -u)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You need root privileges to modify networking parameters. Exiting.&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
&nbsp;
getmac<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;"># sets MAC register by IP. Sets to nil, if the MAC is not on the local link. </span>
  <span style="color: #007800;">getMAC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip neigh show <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $5}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${getMAC}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    arping <span style="color: #660033;">-c1</span> <span style="color: #660033;">-I</span> <span style="color: #800000;">${DEV}</span> <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
    <span style="color: #007800;">getMAC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip neigh show <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $5}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #007800;">MACreg</span>=<span style="color: #800000;">${getMAC}</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
allow<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Whitelists traffic to and from particular IP+MAC pairings and</span>
  <span style="color: #666666; font-style: italic;"># adds them to static ARP.</span>
  <span style="color: #007800;">IP</span>=<span style="color: #800000;">${1}</span>
  <span style="color: #007800;">MAC</span>=<span style="color: #800000;">${2}</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${IP}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MAC}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    arptables <span style="color: #660033;">-A</span> INPUT  <span style="color: #660033;">-s</span> <span style="color: #800000;">${IP}</span> <span style="color: #660033;">--source-mac</span>      <span style="color: #800000;">${MAC}</span> <span style="color: #660033;">-j</span> ACCEPT
    arptables <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-d</span> <span style="color: #800000;">${IP}</span> <span style="color: #660033;">--destination-mac</span> <span style="color: #800000;">${MAC}</span> <span style="color: #660033;">-j</span> ACCEPT
    ip neigh replace <span style="color: #800000;">${IP}</span> lladdr <span style="color: #800000;">${MAC}</span> nud permanent dev <span style="color: #800000;">${DEV}</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${DEV}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #666666; font-style: italic;"># whitelist the routers</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-z</span> <span style="color: #800000;">${GATEWAYS}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">GATEWAYS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip route show dev <span style="color: #800000;">${DEV}</span><span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;s:.* via \(<span style="color: #007800;">${IPREGEX}</span>\).*:\1:p&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">for</span> GWIP <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${GATEWAYS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    getmac <span style="color: #800000;">${GWIP}</span>
    allow <span style="color: #800000;">${GWIP}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #666666; font-style: italic;"># whitelist the DNS servers</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-z</span> <span style="color: #800000;">${DNSSERVERS}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">DNSSERVERS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;s:^nameserver \(<span style="color: #007800;">${IPREGEX}</span>\):\1:p&quot;</span> <span style="color: #800000;">${RESOLV}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">for</span> DNS <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DNSSERVERS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    getmac <span style="color: #800000;">${DNS}</span>
    allow <span style="color: #800000;">${DNS}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #666666; font-style: italic;"># if using dhcpcd, we can whitelist the DHCP server too</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${DHCPSID}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> getmac <span style="color: #800000;">${DHCPSID}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> allow <span style="color: #800000;">${DHCPSID}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #666666; font-style: italic;"># set default policy to DROP    </span>
  arptables <span style="color: #660033;">-P</span> INPUT DROP
  arptables <span style="color: #660033;">-P</span> OUTPUT DROP
  <span style="color: #666666; font-style: italic;"># clear out non-hardcoded ARP cache entries</span>
  ip neigh flush nud reachable
  ip neigh flush nud stale
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># No argument given, so clean up.</span>
  arptables <span style="color: #660033;">-F</span>
  arptables <span style="color: #660033;">-P</span> INPUT ACCEPT
  arptables <span style="color: #660033;">-P</span> OUTPUT ACCEPT
  ip neigh flush nud permanent
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LIRC plugin for Exaile</title>
		<link>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 01:44:58 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[exaile]]></category>
		<category><![CDATA[lirc]]></category>
		<category><![CDATA[lircaile]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[remote]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=718</guid>
		<description><![CDATA[Just finished up a 0.1 version of a LIRC (Linux Infrared Control) plugin for the Exaile media player. Now you can use your remote with Exaile efficiently. The plugin is in the public repository and is called Lircaile.
I haven&#8217;t touched Python much as of yet, but I&#8217;m pleased with it: it appears to be a [...]]]></description>
			<content:encoded><![CDATA[<p>Just finished up a 0.1 version of a <a href="http://lirc.org">LIRC</a> (Linux Infrared Control) plugin for the <a href="http://exaile.org/">Exaile media player</a>. Now you can use your remote with Exaile efficiently. The plugin is in <a href="http://smormedia.gavagai.nl/dist/">the public repository</a> and is called Lircaile.<br />
I haven&#8217;t touched Python much as of yet, but I&#8217;m pleased with it: it appears to be a consistent language. Well, here&#8217;s my 0.1 effort. I desperately wanted to have some fun with introspection, but I have the feeling the nested exception logic is a bit&#8230; unusual.</p>
<p>Update #20110222: Updated the inline code preview below to 0.3.0.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># A LIRC plugin for Exaile. Depends on pylirc from http://sourceforge.net/projects/pylirc/</span>
<span style="color: #808080; font-style: italic;"># Copyright (C) 2009-2011 Wicher Minnaard, http://smorgasbord.gavagai.nl / wicher@gavagai.eu</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># This program is free software: you can redistribute it and/or modify</span>
<span style="color: #808080; font-style: italic;"># it under the terms of the GNU General Public License as published by</span>
<span style="color: #808080; font-style: italic;"># the Free Software Foundation, either version 3 of the License, or</span>
<span style="color: #808080; font-style: italic;"># (at your option) any later version.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> pylirc, <span style="color: #dc143c;">logging</span>, <span style="color: #dc143c;">threading</span>, <span style="color: #dc143c;">select</span>
&nbsp;
LIRCAILE = <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> enable<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>:
  _enable<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, exaile, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _enable<span style="color: black;">&#40;</span>eventname, exaile, nothing<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">global</span> LIRCAILE
  LIRCAILE = Lircaile<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> disable<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>:
  pylirc.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Lircaile<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, exaile<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">exaile</span> = exaile
    <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><span style="color: black;">&#40;</span>__name__<span style="color: black;">&#41;</span>
    sock_fd = pylirc.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'lircaile'</span><span style="color: black;">&#41;</span>
    waitlirc = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=<span style="color: #008000;">self</span>.<span style="color: black;">wait_lircevent</span>, args=<span style="color: black;">&#40;</span>sock_fd,<span style="color: black;">&#41;</span>, name=<span style="color: #483d8b;">'Thread-lircaile-waitlirc'</span><span style="color: black;">&#41;</span>
    waitlirc.<span style="color: black;">daemon</span> = <span style="color: #008000;">True</span>
    waitlirc.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> wait_lircevent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,sock_fd<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
      <span style="color: #483d8b;">&quot;&quot;&quot;Pops all queued signals off of the LIRC queue and hands them to
      handleCode() for further processing.&quot;&quot;&quot;</span>
      <span style="color: #dc143c;">select</span>.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>sock_fd<span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: black;">&#91;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#93;</span> = pylirc.<span style="color: black;">nextcode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">handleCode</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #dc143c;">code</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">TypeError</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span> <span style="color: #808080; font-style: italic;">#empty queue</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> handleCode<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, command, <span style="color: #66cc66;">*</span>arg<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Takes LIRC signals and uses introspection to try to find appropriate 
    exaile functions to call based on the name of the signal. &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>command == <span style="color: #483d8b;">'chvol'</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">set_volume</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">get_volume</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>command == <span style="color: #483d8b;">'seek'</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">get_position</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">1000000000</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      func = <span style="color: #008000;">None</span>
      <span style="color: #808080; font-style: italic;"># Look for a matching playlist function</span>
      <span style="color: #ff7700;font-weight:bold;">try</span>:
        func = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">queue</span>, command<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
        <span style="color: #808080; font-style: italic;"># No? Then look for a matching player function</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          func = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>, command<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
          <span style="color: #808080; font-style: italic;"># No? Then we're out of options</span>
          <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">warning</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'No function to handle the &quot;%s&quot; LIRC event'</span> <span style="color: #66cc66;">%</span> command<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>:
        func<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>how I fixed my Firefox hiccups</title>
		<link>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/#comments</comments>
		<pubDate>Thu, 07 May 2009 17:59:39 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[inotify]]></category>
		<category><![CDATA[tmpfs]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=373</guid>
		<description><![CDATA[The other day I compiled Firefox 3.5-beta4, and, apart from many improvements, I noticed that I am now affected by the infamous &#8216;hiccups&#8217;. Firefox will stall for seconds at a time on my poor netbook. Details on how this relates to the many fsync() calls made by the persistance layer (SQLite) can be found all [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I compiled Firefox 3.5-beta4, and, apart from many improvements, I noticed that I am now affected by the infamous &#8216;hiccups&#8217;. Firefox will stall for seconds at a time on my poor netbook. Details on how this relates to the many fsync() calls made by the persistance layer (SQLite) can be found all over the net.<br />
But I don&#8217;t want Firefox to stall and I don&#8217;t want to keep my harddisk awake with all these writes when I&#8217;m on battery power.<br />
Luckily, both these problems go away if you put your Firefox profile on a ramdisk. There are numerous guides out there that save you from working out the details; I used <a href="http://forums.gentoo.org/viewtopic-t-717117.html">this one from the Gentoo forums</a>.<br />
The guide uses cron to sync the (presumably) modified contents of the ramdisk &#8211; bookmarks, cookies, whatever &#8211; back to permanent storage (harddisk). You and I both know that while it&#8217;s often convenient to use cron, it&#8217;s not always the Right Way of Doing Things®. Why sync if nothing&#8217;s changed? I wrote a script that employs the inotify system to do the syncing only when necessary. You&#8217;ll find it in the forum thread I linked to earlier, but I post it here &#8220;ter lering ende vermaeck&#8221;. Depending on your browser there might be a vertical scrollbar at the bottom which lets you read up to the EOL&#8217;s ;-)<br />
You can find the latest version at <a href="http://smormedia.gavagai.nl/dist/">my public repo</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Packfox, a tool to facilitate running Firefox with its profile stored</span>
<span style="color: #666666; font-style: italic;"># in RAM (tmpfs). Copyright 2008-2009 Wicher Minnaard, wicher@gavagai.eu .</span>
<span style="color: #666666; font-style: italic;"># Distributed under the WTFPL, http://smormedia.gavagai.nl/dist/packfox/COPYING</span>
<span style="color: #666666; font-style: italic;"># Latest version available at http://smormedia.gavagai.nl/dist/packfox/</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Change this to match your profile</span>
<span style="color: #007800;">PROFILE</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">hostname</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">PFDIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${HOME}</span>/.mozilla/firefox&quot;</span>
<span style="color: #666666; font-style: italic;"># Tar every .. seconds (regardless of changes)</span>
<span style="color: #007800;">TMOUT</span>=<span style="color: #ff0000;">&quot;1800&quot;</span>
<span style="color: #666666; font-style: italic;"># But not more often than every .. seconds (regardless of changes)</span>
<span style="color: #007800;">TMMIN</span>=<span style="color: #ff0000;">&quot;60&quot;</span>
<span style="color: #666666; font-style: italic;"># Regex for which files not to act on when they're changed.</span>
<span style="color: #666666; font-style: italic;"># Use inotifywait -m -e modify -e move -e create -e delete --exclude '(/Cache/)' -r your_profile_dir</span>
<span style="color: #666666; font-style: italic;"># and watch the output while browsing to determine which regex will be right for YOU.</span>
<span style="color: #007800;">IEXCL</span>=<span style="color: #ff0000;">&quot;(.sqlite-journal$)|(\-log.txt$)|(cookies.sqlite$)|(sessionstore\-[0-9].js$)|(/weave/)|(/Cache/)&quot;</span>
<span style="color: #666666; font-style: italic;"># Have you read everything and have you made the necessary adjustments? Then remove the line below ;-)</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;I should read the README and adjust the script variables before running this.&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># No user servicable parts below this line.</span>
<span style="color: #007800;">TGT</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Global vars</span>
<span style="color: #007800;">INOTYPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #007800;">SLEEPPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Cleanup function</span>
terminate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># If we are the daemon and we get SIGINTed/SIGTERMed, kill our children</span>
  <span style="color: #666666; font-style: italic;"># and if not already packing, do one last round of packing.</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox-daemon&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${INOTYPID}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #800000;">${INOTYPID}</span>; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SLEEPPID}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #800000;">${SLEEPPID}</span>; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PACKLOCK}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<span style="color: #000000; font-weight: bold;">then</span> packup; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># For cleaning up </span>
<span style="color: #7a0874; font-weight: bold;">trap</span> terminate SIGINT SIGTERM
&nbsp;
<span style="color: #666666; font-style: italic;"># Suicide with goodbye note. If gxmessage is installed, use that.</span>
seppuku<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
  <span style="color: #c20cb9; font-weight: bold;">which</span> gxmessage <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> gxmessage <span style="color: #660033;">-nofocus</span> <span style="color: #660033;">-title</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> xmessage <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Checks and setup</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Profile dir doesn't exist&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(mount -t tmpfs | grep -F &quot;${TGT}&quot; )</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Mounting of profile's tmpfs failed. Check /etc/fstab and the output of 'dmesg'.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TGT}</span>/.unpacked&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xpf</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span> <span style="color: #660033;">-C</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span> \
<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TGT}</span>/.unpacked&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Error unpacking the profile tarball. You might want to use the backup tarball located in <span style="color: #007800;">${PFDIR}</span>.&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This tars up the profile</span>
packup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;locked&quot;</span>  
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">'.unpacked'</span> <span style="color: #660033;">-cpf</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tmp.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PROFILE}</span>&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar.old&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tmp.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span>
  <span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># No daemon, just packing</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> packup; <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The daemon loop</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox-daemon&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">which</span> inotifywait <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot; You'll need the 'inotify-tools' package for this script. Get it at http://inotify-tools.sourceforge.net or from your distro's repos&quot;</span>.
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
    <span style="color: #000000; font-weight: bold;">do</span> inotifywait <span style="color: #660033;">-q</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-t</span> <span style="color: #800000;">${TMOUT}</span> <span style="color: #660033;">-e</span> modify <span style="color: #660033;">-e</span> move <span style="color: #660033;">-e</span> create <span style="color: #660033;">-e</span> delete <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${IEXCL}</span>&quot;</span> \
    <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
    <span style="color: #007800;">INOTYPID</span>=<span style="color: #800000;">${!}</span>
    <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #800000;">${INOTYPID}</span>; <span style="color: #007800;">INOTIFYPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
    packup
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #800000;">${TMMIN}</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
    <span style="color: #007800;">SLEEPPID</span>=<span style="color: #800000;">${!}</span>
    <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #800000;">${SLEEPPID}</span>; <span style="color: #007800;">SLEEPPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>OmroepStreamDump</title>
		<link>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 21:28:18 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OmroepStreamDump]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=359</guid>
		<description><![CDATA[[Update @20090607: De code moet regelmatig bijgewerkt worden om veranderingen op player.omroep.nl te reflecteren. De laatste versie kun je altijd downloaden van de officiële pagina, daar kun je ook pingen (via een issue) als het script aangepast moet worden aan een nieuwe versie van player.omroep.nl (m.a.w., als het niet meer werkt).]
Ik heb een Greasemonkey-scriptje geschreven [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:x-small; color:orange;">[Update @20090607: De code moet regelmatig bijgewerkt worden om veranderingen op player.omroep.nl te reflecteren. De laatste versie kun je altijd downloaden van <a href="http://userscripts.org/scripts/show/47693">de officiële pagina</a>, daar kun je ook pingen (via een issue) als het script aangepast moet worden aan een nieuwe versie van player.omroep.nl (m.a.w., als het niet meer werkt).]</span></p>
<p>Ik heb een <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a>-scriptje geschreven dat op player.omroep.nl een dialoogje geeft waarvan je de inhoud direct in een shell (Bash-shell, wellicht ook de standaard Bourne-shell) kan pasten. Je krijgt iets dergelijks voorgeschoteld:<br />
<code><br />
export TIEFDIR="${PWD}"; export TEMPDIR=$(mktemp -d); cd ${TEMPDIR} &#038;&#038; mplayer -dumpstream -user-agent 'Windows-Media-Player/11.0.6001.7000' 'http://cgi.omroep.nl/legacy/player?/ceres/vara/rest/2009/VARA_101192917/bb.20090424.asf' &#038;&#038; mv stream.dump ${TIEFDIR}/'De Wereld Draait Door - 25-04-2009.wmv' &#038;&#038; cd - &#038;&#038; rmdir ${TEMPDIR}<br />
</code><br />
Dat maakt het downloaden van videomateriaal van uitzendinggemist.nl een stukje makkelijker. Natuurlijk moet je wel Firefox + Greasemonkey-extensie, een shell en MPlayer hebben, maar wie heeft dat nou niet ;-)<br />
<a href="http://userscripts.org/scripts/review/47693">Hier kun je de source lezen</a> en als je <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a> hebt geïnstalleerd kun je <a href="http://userscripts.org/scripts/show/47693">hier op &#8220;Install&#8221; klikken</a>.<br />
Surf vervolgens naar <a href="http://www.uitzendinggemist.nl/">uitzendinggemist.nl</a> en fair-use er op los.<br />
En hier natuurlijk het leukste stuk van de source. Ik schrijf haast nooit Javascript dus opmerkingen/aanvullingen zijn zeer welkom.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> playert <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;player&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Listener for node insertions (generated by omroep.nl's javascript)</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>playert<span style="color: #009900;">&#41;</span> playert.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMNodeInserted&quot;</span><span style="color: #339933;">,</span> genAlert<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> genAlert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> playert <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;player&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> embedelement <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MediaPlayer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>embedelement<span style="color: #009900;">&#41;</span>
  <span style="color: #006600; font-style: italic;">//The embed element got inserted - now we have all required parameters for dumping</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pastetext<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> source <span style="color: #339933;">=</span> embedelement.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//These URL's shouldn't have &quot;'&quot; in them but we escape them just to be sure.</span>
    <span style="color: #006600; font-style: italic;">//Create a copy of the title &amp; date info so we can use DOM functions to separate the two</span>
    <span style="color: #003366; font-weight: bold;">var</span> worktitle <span style="color: #339933;">=</span> document.<span style="color: #660066;">importNode</span><span style="color: #009900;">&#40;</span>playert.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;h3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> worktitledatespan <span style="color: #339933;">=</span> worktitle.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> datum <span style="color: #339933;">=</span> worktitledatespan.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Escape ' to make shell-safe</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>datum<span style="color: #009900;">&#41;</span> datum <span style="color: #339933;">=</span> <span style="color: #3366CC;">'nodate'</span><span style="color: #339933;">;</span>
    worktitle.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>worktitledatespan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> titel <span style="color: #339933;">=</span> worktitle.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Escape ' to make shell-safe</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>titel <span style="color: #339933;">&amp;&amp;</span> source<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
      playert.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMNodeInserted&quot;</span><span style="color: #339933;">,</span> genAlert<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      pastetext <span style="color: #339933;">=</span> <span style="color: #3366CC;">'export TIEFDIR=&quot;${PWD}&quot;; export TEMPDIR=$(mktemp -d); cd ${TEMPDIR} &amp;&amp; '</span>
      pastetext<span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;mplayer -dumpstream -user-agent 'Windows-Media-Player/11.0.6001.7000' '&quot;</span><span style="color: #339933;">+</span>source<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;' &amp;&amp; &quot;</span><span style="color: #339933;">;</span>
      pastetext<span style="color: #339933;">+=</span> <span style="color: #3366CC;">'mv stream.dump ${TIEFDIR}/<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span>titel<span style="color: #339933;">+</span><span style="color: #3366CC;">' - '</span><span style="color: #339933;">+</span>datum<span style="color: #339933;">+</span><span style="color: #3366CC;">'.wmv<span style="color: #000099; font-weight: bold;">\'</span> &amp;&amp; cd - &amp;&amp; rmdir ${TEMPDIR}'</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>pastetext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://cgi.omroep.nl/legacy/player?/ceres/vara/rest/2009/VARA_101192917/bb.20090424.asf" length="168" type="video/x-ms-wmv" />
		</item>
	</channel>
</rss>

