
Posted by
Wicher
Topic:
Code
January 6th
2012
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 that goes against the idea of having a small, reliable, always-on, energy-efficient home server.
Techies like pictures of hardware with the cover off so here’s my Alix:

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’m going? Nowhere! ;-)
The portage tree fulfills the role of a package database for us Gentoo ricers. I need it locally.
Diego, one of the Gentoo devs, wrote a blog post about space inefficiency incurred through the use of many small files in the portage tree. He puts the Portage tree on different filesystems to arrive at an accurate and detailed picture of incurred overhead.
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:
slacktastic.py
#!/usr/bin/env python3
"""
slacktastic.py - calculate filesystem file slack for different block sizes.
Invoke me thusly:
find /path/to/tree -xdev -type f -printf "%s\\n" | slacktastic.py 4096
for a calculation using the contents below /path/to/tree with a block size of 4096 bytes.
"""
import sys, functools, math
blksz = None
try:
blksz = int(sys.argv[1])
except (IndexError,ValueError):
print(__doc__, file=sys.stderr)
print('I need a blocksize as the first argument.\n', file=sys.stderr)
sys.exit(1)
sizes = [int(strsize) for strsize in sys.stdin.read().strip().split('\n')]
sumslack = functools.reduce(lambda sumslack, sz: sumslack + (blksz - (sz % blksz)), sizes, 0)
sumblks = functools.reduce(lambda sumblks, sz: sumblks + (math.ceil(sz / blksz)), sizes, 0)
sumsizes = sum(sizes)
print('{:n} total slack'.format(sumslack))
print('{:n} bytes in files'.format(sumsizes))
print('{0:n} total blocks of {1} bytes'.format(sumblks,blksz))
print('{:.2%} inefficiency'.format( sumslack / (sumblks*blksz) ))
This doesn’t take into account things such as tail packing (à la ReiserFS), compression (Btrfs), or directory slack. Just file slack.
On my laptop filesystem, with a 4096 block size, this leads to the following observation:
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
My /var/db/pkg, Gentoo’s ‘database’ of installed packages (containing their build environment and all kinds of stuff you wouldn’t need on a binary distro) contains 65% air! That’s 170 megs of waste which I don’t want that on my Alix’s 4GB CF card. With a 1024 byte block size — Ext4’s minimum — the situation is better, but it’s still over 40 megs of hot air.
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’t fit on an bs=1024b Ext4 FS.
Tags: block size, English, filesystem overhead, python, slack, slack space —
August 22nd
2011
For a little while I’ve been running a DIY dynamic DNS service. My peers think it’s convenient and pretty sweet, so in this post I’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 “dynamic DNS” services. What is a “dynamic DNS” service, you ask? Well, it’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.
Why use one?
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 “now just go to… errr… hold on” (and now you pop your shell and run ip a s dev wlan0)1 “yes I’m back, you need to go to 110.34.56.250″ you can just say “go to kleinebeer.ath.cx”. Much easier. And that’s with IPv4. Typing an IPv6 address that someone just yelled at you from across the room is an even less joyful experience.
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 “dynamic DNS service” to update the ‘kleinebeer’ 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.
Doing it differently
But some time ago I got fed up with all the “account expiration warning” emails I had been getting from the service that I used. And since I’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.
Sins!
It allows for instant creation/updating of DNS records through a very simple HTTP GET. That is sinful for two reasons:
- No authentication and authorization.
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’t have such names as MX records unless you don’t mind that mail intended for you ends up on someone else’s server.
At the same time it is a good thing, as I will show in the use case example below.
- The HTTP GET alters state.
And that’s just plain Bad Taste. If you’re designing a web service you’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.
Use cases
-
Case A:
Imagine you’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’s say you need to SSH in and help her with her crontab). She hasn’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’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 ‘grandma’ on dyndomain.tld, and this record points to your grandma’s IP. Now you can just do ’ssh grandma.dyndomain.tld’.
-
Case B:
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’s development servers.
You could all call out your IPs across the room and let everyone type in everyone else’s IPs. Now you all have N tabs open in your browsers. Who was 123.231.101.45 again?
Or, everyone could just point their browser (or curl, wget…) to http://theirfirstname.reg-a-record.tld/reg.php2 and be done with it. If you want to view Fred’s progress you just go to fred.dyndomain.tld.
So, sin #1 & #2 allow for use cases that were not possible with the provider I was using.
You don’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’s a good thing. You don’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’s no reason to keep using the same name every time.
On to the code
It’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.
DNS server
You need a domain for which you run the authoritative name server. I’m using the SheerDNS DNS server package on mine, for no good reason. Here’s a patch I made so that sheerdnshash creates directories with some group permission bits set, something I need in my setup.
You can use other DNS servers, of course. I discovered that the Unbound DNS server has a socket protocol which you can possibly use to add records on the fly. But I use sheerdns now, and here’s a Python module I made to write A-records in sheerdns’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.
#!/bin/env python
import socket,os,string,subprocess,sys
SHEERDNS_DIR="/var/sheerdns"
SHEERDNS_HASH="/usr/sbin/sheerdnshash"
DYNDOMAIN="dyndomain.tld"
ALLOWCHARS=string.ascii_letters + ''.join([ "%d" % i for i in range(10)]) + '-'
def mk_A(name,ip):
#are host and ip allright?
host = ''.join([ c for c in name if c in ALLOWCHARS ]).lower()
if not host:
return None
try:
socket.inet_aton(ip)
except socket.error:
return None
fqdn = "%s.%s" % (host, DYNDOMAIN)
#set the umask
oldumask = os.umask(0002)
#get the hash
hash = subprocess.Popen([SHEERDNS_HASH, fqdn], stdout=subprocess.PIPE).communicate()[0].strip()
#construct the dirpath
path = os.path.join(SHEERDNS_DIR,hash,fqdn)
#chmod it for everyone to read
os.chmod(path,0755)
#construct the A-record-filepath
apath = os.path.join(path,'A')
#open the record file
with open(apath,'w') as record:
record.write(ip)
os.umask(oldumask)
return name
if __name__ == '__main__':
if len(sys.argv) == 3:
print(mk_A(sys.argv[1],sys.argv[2]))
Web ’service’ to create the records: a CGI
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&ip=theip .
There’s no reason that this CGI should be guilty of Sin #2, except laziness on my behalf. If I’d be following every convention even for silly little solo projects I’d never get anything done, all right?
It doesn’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).
#!/usr/bin/python
import sheerdns
import cgi
print "Content-type: text/plain\n\n";
params = cgi.parse()
name, ip = params.get('name'), params.get('ip')
if ip and name:
dnsname = sheerdns.mk_A(name[0],ip[0])
print dnsname
The accessible part: reg.php
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.
You need to modify this script to reflect your config.
<?php
header('Content-Type: text/plain');
$hostparts = explode('.', $_SERVER['SERVER_NAME'], 2);
$ip = $_SERVER['REMOTE_ADDR'];
print $ip;
$cha = curl_init('https://server_that_has/the_CGI?name='.$hostparts[0].'&ip='.$ip);
curl_setopt($cha, CURLOPT_SSL_VERIFYPEER, False);
curl_setopt($cha, CURLOPT_TIMEOUT, 5);
curl_setopt($cha, CURLOPT_RETURNTRANSFER, True);
curl_setopt($cha, CURLOPT_USERPWD, 'bite:me');
$resp = curl_exec($cha);
curl_close($cha);
print($resp);
?>
Presto
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.
You could set DirectoryIndex reg.php here, or rename reg.pgp to index.php, if you don’t want users to type ‘reg.php’. I did make it explicit, because web sites on this server come and go and I don’t want people to stumble onto reg.php every time they hit the default vhost because their web site doesn’t exist any more.
1)Let’s assume you’re not NATed.
2)Try to only have friends with distinct names that can be expressed in ASCII.
Tags: code, dns, dyndns, English —

Posted by
Wicher
Topic:
Code
April 18th
2011
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’d actually be better off just defining them as functions in your ~/.{ba,z}shrc .
*) For unixoid values of ‘public’.
groe
Open a file (argument 2) in joe (works with vim, too) with the cursor on the first line matching the regex in argument 1.
#!/bin/bash -eu
THEL=$(grep -m 1 -n ${1} ${2} | cut -d ':' -f 1)
[ -n "$THEL" ] && joe +$THEL $2
paserv
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.
#!/bin/bash
if [ -n "${1}" ]; then pax11publish -e -S ${1}
else pax11publish -e -r; fi
gmrun
Wrapper launcher for the excellent “gmrun” program launcher. (queue “yo dawg” meme).
Raises and/or pulls any existing gmrun windows to your current desktop.
#!/bin/bash
/usr/bin/wmctrl -x -b add,raise -R "gmrun.Gmrun" || PATH="/home/boer/bin:$PATH" /usr/bin/gmrun
getx
Get an X11 authorization cookie for a display on some other host, over SSH.
#!/bin/bash
if [ -z ${1} ]; then
echo "Usage: $(basename ${0}) sshstanza"
echo ""
exit 2
fi
SSH=${1}
XHO=${1##*@}
DIS=${2-":0"}
ssh ${SSH} "xauth list ${XHO}${DIS} | sort -u" | while read line; do xauth add ${line}; done
gaap
Ghetto power management. We don’t need no stinkin’ gnome-power-manager, we’ll just grep our keyboard/trackpad interrupt counter thankyouverymuch (I got that idea from reading the xscreensaver man page).
Comes in two parts.
is_user_alive:
#!/bin/bash -eu
#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.
RECORD=${1:-/dev/shm/i8042_intcnt}
RETVAL="1"
upd_intcnt() {
grep i8042 /proc/interrupts > ${RECORD}
}
is_alive() {
grep i8042 /proc/interrupts | diff - ${RECORD} > /dev/null 2>&1 || RETVAL=0
}
[ ! -f ${RECORD} ] && upd_intcnt && exit 0
is_alive
upd_intcnt
exit $RETVAL
Run this one (gaap.sh) from cron:
#!/bin/bash -eu
misschien_slapen() {
test -f /tmp/koffie || on_ac_power || echt_slapen
}
echt_slapen() {
/usr/local/sbin/is_user_alive /dev/shm/gaap_alive || true
echo -e "Hibernating in 30 secs, unless you prove you're alive by pressing\na key or touching the touchpad. Physically. On ${HOSTNAME}." | wall
for sec in `seq 0 3 99`; do echo $sec; sleep 1; done | DISPLAY=":0" XAUTHORITY="/home/boer/.Xauthority" sudo -u boer zenity --progress --auto-close --text "hibernate?" || exit 0
/usr/local/sbin/is_user_alive /dev/shm/gaap_alive || /usr/sbin/pm-hibernate
}
/usr/local/sbin/is_user_alive /dev/shm/gaap_alive || misschien_slapen
You might want to install zenity, and you’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’s good practice to not present privilege escalation attack surfaces to yourself ;-)
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?
Aphorism slideshow
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
unix fortune database. So it’s kind of a modern incarnation of those tiles that older Dutch generations used to cement onto their walls (
Wikipedia entry [Dutch]). Terrible as they are, they’re making a (campy) comeback. Personally I can’t wait for the day that camp culture itself becomes camp (queue another “yo dawg” meme).
Anyway, I made myself this sed script, “str2cu.sed”:
/^%$/d
s/"\([^"]*\)"/“\1”/g
and then did this. Which, the better part of a year later, proves quite hard to reverse-engineer:
for CATG in art definitions education fightclub food hitchhiker humorists kids literature love medicine men-women news paradoxum pets platitudes politics science smac strangelove wisdom work; \
do mkdir /home/boer/tmp/fortunes/$CATG; cd /home/boer/tmp/fortunes/$CATG; \
csplit -q -z /usr/share/fortune/$CATG '/%/' '{*}' ; for f in /home/boer/tmp/fortunes/$CATG/*; \
do cat $f | sed -f /home/boer/tmp/fortunes/str2cu.sed | fmt -w 2500 | tr -s '\t' | expand -t 2 > /tmp/fortune; \
mv /tmp/fortune $f; done; find /home/boer/tmp/fortunes/$CATG/ -type f -size +200c -exec rm '{}' \; ; \
for f in /home/boer/tmp/fortunes/$CATG/*; do convert -background pink -fill white -size 480x234 \
-stroke black -gravity Center -font Essays1743-Bold caption:@$f $f.jpg; done; done
GNU textutils are fun… but holy cow. What an obtuse kludge.
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?).
Tags: bash, English —

Posted by
Wicher
Topic:
Howto
February 9th
2011
When SQLite is not enough
SQLite is great for ad-hoc SQL DB-ing, but it’s not so great if you need to serve multiple processes (on writing, it locks the complete database).
It just so happens that, for a certain project, I need the ad-hocishness of SQLite *and* multiprocess write concurrency.
It appears that PostgreSQL can easily be made to run in “hassle-free” mode. Meaning: no need to ask your mum or your sysadmin anything, no need to write any config files.
Minimalism
Well, almost. I put in some extra options to disable the TCP socket, and I set some permissions on the unix socket.
For these examples, I assume bash or zsh and a unixish system. And PostgreSQL 9.0.
create a database:
initdb ~/mypostgres
You now have a directory ‘mypostgres’ in your homedir. It contains some config files with defaults. We don’t care.
start a server:
postgres -D ~/mypostgres/ -k ~/mypostgres/ --listen_addresses='' \
--unix_socket_permissions=0660 --unix_socket_group=$(id -g)
Let’s go over these options:
-D ~/mypostgres/ — Use ~/mypostgres as datadir (it’s where you created the database, so.)
-k ~/mypostgres/ — Use ~/mypostgres as the directory to store the unix socket for clients to connect to. Look closely and you’ll discover it (it starts with a dot for no good reason). Put it somewhere else (/tmp springs to mind) if you want other users to connect to your db, more on that later.
These are not strictly necessary:
--listen_addresses='' — Don’t listen on any TCP sockets. We don’t really authenticate users. So if you expose a TCP socket (default, on the localhost interface) any user can do anything to your databases just by running something like psql -h 127.0.0.1 -d postgres -U yourusername
--unix_socket_permissions=0660 — By default, permissions are o=rwx. If you put the socket in a public place such as /tmp, then with our authentication-free mode of operation, you probably do not want any and all users to be able to access your DB.
--unix_socket_group=$(id -g) — This is completely superfluous but it’s here so you don’t shoot yourself in the foot when blindly copy-pasting teh codez. $(id -g), of course, is your primary group ID and is what the group ownership of the socket will have been set to without this specification anyway (hence the superfluousness). So when would you use this? You’d use it when you want to let other users access your DB. You’d set the gid to some appropriate group that you and the other users belong to (and you’d put the socket in a public place, that much should be clear now).
some goodies:
--silent-mode=true — Daemon mode. To quit the server, get the PID from ~/mydb/postmaster.pid and send it a SIGTERM.
-F — Wicked fast writes mode. Also, wickedly trashed DB mode if the server is interrupted abnormally.
Create and connect
Now the server is up and running, create a database:
createdb -h ~/mypostgres mydb
and connect to it:
psql -h ~/mypostgres -d mydb
By default psql will try to log you in to the DB using the same username as your current unix username. Convenient, because that user has full privileges on the DB.
Should you need to specify the user as whom to connect, you can do so with -U.
Wrapper?
Just like with sqlite, you determine access to the DB by setting unix permissions. We do it on the socket, with SQLite you do it on the DB file itself.
So, how about a wrapper Python module to make all this just as easy as
import sqlite3
conn = connect("create_me_if_I_don't_exist.db")
Yes, one day. Fork off a db server process, terminate it using the atexit decorator. It just might be that simple.
Tags: English, postgreSQL, python, sqlite —

Posted by
Wicher
Topic:
Tech
December 4th
2010
Argh, soft switches
So what’s a soft power switch anyway? Let’s start with the converse: a regular light switch, say, of the tumbler type. You switch it in the ‘on’ position and voila, light. There’s a power cut and off it goes. But the switch maintains ’state’, so when the mains come back on, so does your light!
This doesn’t happen with soft switches. Pushing a soft switch momentarily closes a contact, thereby generating a signal. Typically this signal is interpreted by some circuit, turning on your laptop. Or if your laptop is already on it might put it into standby. The effect of pushing this switch depends on context. The pushbutton has no state, it just generate an event when it’s being pushed. No persistent state here. And context might get lost — after a power failure, for instance.
Context bites.
In itself this is not a bad thing, but the devil is in the details. The implementation of the circuit part can make a geek’s life miserable. I had an external harddisk enclosure sporting one of those soft power switches. Connect power, push the button, it’s on. Power cut? Off it goes (of course). Power restored — it’s still off. But I wanted it to be on whenever there is power. Duct-taping the button in the ‘pushed’ position (hey, ’state’!) didn’t do much good as, in my limited understanding, this particular switch appeared to work by ‘pulling down’ a signal from the high state to the low state. The circuit will never be armed! With the button pushed down when power comes back on, the high state will never be reached as any potential will just leak away through the switch, dammit.
I spent some time trying to solder around this problem, trying to delay the bridging of the contact with all kinds of contraptions made of stuff I had lying around (positive feedback circuit from LEDs and an LDR, fun) to no other avail than eventually frying the controller circuit. I’m such a n00b with electronics, I can’t even build a proper delay circuit.
Laptop relay
But that didn’t stop me from soldering up a solution to the following problem. My old netbook is performing home server duties now, which is nice as it’s energy-efficient, quiet, and has builtin UPS. This is the plan: If the mains go down it can keep running. After a while it sets an RTC wakeup alarm and suspends itself. The RTC alarm will be set ~12 hrs into the future, it will power up and switch on the UPS which powers the ADSL modem, border router, and switch. That way it can collect some mail. And then it shuts down the UPS, sets another RTC wakeup alarm…. wash, rinse, repeat, until battery power runs out.
Problems
- So what if battery power has run out, and mains power gets restored?
Then I’d have to physically push the button to power the thing back on. I don’t want that. But there’s no ‘restore power state after power loss” BIOS option on this laptop. Lucky for me, with this particular piece of equipment I can just permanently short the same pins that the power switch shorts — no timing trouble as with the harddisk enclosure here.
- So what if mains power comes back on after the server has just decided to go to sleep for another XX hours?
Well that would be a silly situation. I need the laptop to resume from standby in that case.
This posed problems as the pin shorting trick mentioned before doesn’t help in this case. Potential just keeps on flowing through the ‘pressed’ switch, regardless of the state of the mains power. So restoring mains power doesn’t generate an event.
Solution
…was to use a relay. The coil is wired to the internal 20V leads coming from the power connector (right picture). klz helped soldering this rather tricky one. The relais switch leads are soldered onto the switch wires (left picture, you can see the little switch connector). In the middle (middle) you can see the relais. It’s huge and makes the bottom plate of the laptop bulge a bit.
But it works! It generates the equivalent of a button push whenever mains power comes on.
Tags: English, hardhack, relay, soldering —

Posted by
Wicher
Topic:
Howto
October 31st
2010
A while ago I received a question from a reader. He was having some trouble with a pretty interesting setup. As I was rather intrigued I spent some time to figure it out — it’s solved, but I’m not 100% sure on the theory. So if you are, please comment.
the question
from Yves <theYinYeti@yeti.selfip.net>
to wicher@gavagai.eu
date Wed, Sep 15, 2010 at 16:06
subject Socat
Dear mister Minnaard,
I hope you don’t mind me asking you for help. By chance, I saw your blog post:
http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/
It happens I have a problem with socat on my Sheevaplug server, and it seems you have good knowledge of this software.
Following http://zarb.org/~gc/html/udp-in-ssh-tunneling.html, I use socat to tunnel DNS through SSH, so as to have transparent DNS resolution, to be used with a transparent proxy (NAT rules + tinyproxy, backed by cntlmd, backed by corporate proxy).
My problem is that the number of socat PIDs on both the client and the server keeps growing. I’ve had floods of “accept: too many open files” messages, and I suspect socat is the culprit (at this point, a ps -ef shows almost a thousand of socat PIDs!); especially since the transparent DNS stops working after this flood of messages.
More specifically, on the client, I run:
dnsmasq with “server=127.0.0.1#1053” as a back-end
socat -ly udp4-listen:1053,reuseaddr,fork tcp:127.0.0.1:1054
ssh -L 1053:208.67.222.222:53 -L 1054:127.0.0.1:1054 \
-Tfx remoteuser@remoteserver \
'socat -lf /dev/null tcp4-listen:1054,reuseaddr,fork \
UDP:208.67.222.222:53'
(208.67.222.222 is OpenDNS)
Besides, on localhost (local client), ssh is configured this way:
Host remoteserver
Port 443
ServerAliveInterval 60
ProxyCommand /usr/bin/proxytunnel -v -p corporateproxy:8080 -P login:passwd -d %h:%p
Do you have an idea on how I could make socat behave better?
I thank you beforehand for any advice you could provide.
Sincerely,
Yves.
the reply
from Wicher Minnaard <wicher@gavagai.eu>
to Yves <theYinYeti@yeti.selfip.net>
date Thu, Sep 16, 2010 at 02:19
subject Re: Socat
Dear mister Yves,
On Wed, Sep 15, 2010 at 16:06, Yves <yves@yeti.selfip.net> wrote:
Dear mister Minnaard,
I hope you don’t mind me asking you for help. By chance, I saw your blog post:
http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/
It happens I have a problem with socat on my Sheevaplug server, and it seems
you have good knowledge of this software.
Thank you for your compliment. Actually, I do not know socat all too well, but I know a thing or two about networking.
Following http://zarb.org/~gc/html/udp-in-ssh-tunneling.html, I use socat to tunnel DNS through SSH, so as to have transparent DNS resolution, to be used with a transparent proxy (NAT rules + tinyproxy, backed by cntlmd, backed by
corporate proxy).
Sounds like just another day at the office ;-)
Interesting setup.
My problem is that the number of socat PIDs on both the client and the server keeps growing. I’ve had floods of “accept: too many open files” messages, and I suspect socat is the culprit (at this point, a ps -ef shows almost a thousand of socat PIDs!); especially since the transparent DNS stops working after this flood of messages.
dnsmasq with “server=127.0.0.1#1053” as a back-end
socat -ly udp4-listen:1053,reuseaddr,fork tcp:127.0.0.1:1054
ssh -L 1053:208.67.222.222:53 -L 1054:127.0.0.1:1054 \
-Tfx remoteuser@remoteserver \
'socat -lf /dev/null tcp4-listen:1054,reuseaddr,fork \
UDP:208.67.222.222:53'
(208.67.222.222 is OpenDNS)
Sidenote: Google provides a solid DNS service nowadays. I don’t know what they do with the data, though the same could be said about OpenDNS.
See http://code.google.com/speed/public-dns/
Host remoteserver
Port 443
ServerAliveInterval 60
ProxyCommand /usr/bin/proxytunnel -v -p corporateproxy:8080 -P login:passwd -d %h:%p
So it’s DNS over UDP over TCP over SSH over proxied HTTPS, or something. I’m losing track of all the layers here.
Do you have an idea on how I could make socat behave better?
I thank you beforehand for any advice you could provide.
Well you got me curious now.
I managed to reproduce your problem in a simplified setup:
- on host w00tage, setup the nameserver façade:
socat udp4-listen:1053,reuseaddr tcp:localhost:1054
- from host w00tage, tunnel the TCP connection over SSH to the host hosting the to-be-proxied nameserver:
ssh -L 1054:localhost:1054 priv
- now, on this host priv, connect the TCP socket to the UDP socket where the nameserver is listening
socat tcp4-listen:1054,fork,reuseaddr UDP:localhost:53
- on w00tage, do queries on localhost:1054:
dig @localhost -p1053 google.fr
Dig is part of the bind-tools package and greatly helps in diagnosing DNS problems.
The first dig will probably return. Subsequent digs may take seconds to return. And they leave stray socat processes behind on both w00tage and priv. Kill the strays on w00tage, and the ones on priv dissolve. So the local ones (on w00tage) tie up the remote ones. The latter are not the problem, just a symptom. You can keep on querying, doing more digs, until your file descriptors run out.
A stab at what is going on here: UDP is stateless, and socat is ignorant of protocol data (as it should be). A DNS query takes one UDP packet. But socat does not know we’re doing DNS. And since UDP is stateless, there’s no ‘bye’ in the conversation. So socat doesn’t know when to stop waiting for more. Dig does, though – it recognizes a DNS answer when it sees one. So dig returns (but why the delays? socat buffers and timeouts maybe?) but socat doesn’t. It’s still waiting for more.
Now since we’re forking socat for every new independent socketpair opened (which is what happens when you do a query), we get a lot of those hanging socats.
I had a look at the URL you sent me, http://zarb.org/~gc/html/udp-in-ssh-tunneling.html, to see how they are getting it to work and it appears there is a ‘UDP-RECVFROM’ address type in socat. From its manpage:
————
UDP-RECVFROM:
Creates a UDP socket on
[UDP service] using UDP/IP version 4
or 6 depending on option pf. It receives one packet from an
unspecified peer and may send one or more answer packets to that peer.
This mode is particularly useful with fork option where each arriving
packet – from arbitrary peers – is handled by its own sub process.
This allows a behaviour similar to typical UDP based servers like
ntpd or named. This address works well with socat UDP-SENDTO address
peers.
————
Hey, explicit mentioning of nameservers there. Receive one packet, don’t wait for more, send it off, collect answer, send that back.
On host w00tage, I run the façade thusly:
socat udp4-recvfrom:1053,fork,reuseaddr tcp:localhost:1054
And that appears to work indeed. But wait, how does socat (on priv) know when to stop trying to collect answer packets?
I think it’s socat timeouts, but I’m not 100% sure. What leads me to think that it’s timeouts is that when I stress-test this setup from w00tage:
while true; do dig @localhost -p1053 sheeva; done
(for ’sheeva’ the nameserver never has to do recursion)
I have about 30 forked-off query-listeners when I do a pgrep socat | wc -l on w00tage. But I have about 400 on priv. That number goes up and down and the pids change, so they do not *accumulate* but rather seem to hang about until they decide their job is done. But to be sure I’d have to study the source. Or ask the socat mailing list.
Anyway, problem solved!
I was happy to help you and I got some exercise out of it. Any reason why you didn’t post this question as a comment on my blog? Maybe it would be a bit off-topic, I can see that.
Would you mind if I published this?
Regards and good luck, Wicher Minnaard.
it works
from Yves <theYinYeti@yeti.selfip.net>
to wicher@gavagai.eu
date Thu, Sep 16, 2010 at 10:01
subject Re: Socat
Dear mister Minnaard,
All in all, I was just careless in adapting the “alternative solution with socat” to my setup.
I thank you for your analysis and tests; I learnt a few things following them. And I certainly wouldn’t mind at all if you were to publish this.
However, if you do so, please change the e-mail to “theYinYeti@yeti.selfip.net” (the Sheevaplug :-) ), and my name to simply “Yves”; thank you. As for the web site (I see there’s a field for it), it is “http://yeti.selfip.net/gablin.php” (a bit lame, but I may find time someday to update it…)
As you said, I had the feeling this was a bit off-topic. But now, I see how this can shed some light on advanced socat usage.
–
Regards,
Yves.
Tags: English, socat, tunneling —
September 15th
2010
Nice hardware, but no channel 13
Last week I got myself a TP-Link TL-WR1043ND and pressed it into service as a smart 5-port gigabit switch (with VLAN capabilities) and wireless-N bridge. It is a fine piece of hardware and unbelievably cheap. Best of all, you can install the OpenWRT linux distribution on it. OpenWRT doesn’t target the inexperienced, but it is very customizable, offers a fair amount of documentation, and the build system seems very worthy of giving it a spin.
However. I noticed I couldn’t select a wireless channel >11. That’s odd, because over here in Europe we’re allowed to use the frequencies of channel 12 and 13, too.
Here’s what dmesg told me:
cfg80211: Calling CRDA to update world regulatory domain
cfg80211: World regulatory domain updated:
(start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
(2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
(2457000 KHz - 2482000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)
(2474000 KHz - 2494000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)
(5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
(5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
ath: EEPROM regdomain: 0x0
ath: EEPROM indicates default country code should be used
ath: doing EEPROM country->regdmn map search
ath: country maps to regdmn code: 0x3a
ath: Country alpha2 being used: US
ath: Regpair used: 0x3a
phy0: Selected rate control algorithm 'ath9k_rate_control'
phy0: Atheros AR9100 MAC/BB Rev:0 AR2133 RF Rev:a2 mem=0xb80c0000, irq=2
cfg80211: Calling CRDA for country: US
cfg80211: Regulatory domain changed to country: US
(start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
(2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2700 mBm)
(5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 1700 mBm)
(5250000 KHz - 5330000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
(5490000 KHz - 5600000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
(5650000 KHz - 5710000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
(5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 3000 mBm)
So why does the system think I am in the US? And how can I tell it that I’m actually in the Netherlands? At first glance, it should be as easy as iw reg set NL. Result:
cfg80211: Calling CRDA for country: NL
cfg80211: Regulatory domain changed to country: NL
(start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
(2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
(5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
(5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
(5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
Looks about right, doesn’t it? Because 2482000 > 2472000. 2472 KHz is the upper bound of the 22 KHz wide channel 11. Likewise, 2482 KHz is the upper bound of channel 13. So am I all good for channel 13 now? Turns out I am not. iwlist wlan0 channel says:
wlan0 11 channels in total; available frequencies :
Channel 01 : 2.412 GHz
Channel 02 : 2.417 GHz
Channel 03 : 2.422 GHz
Channel 04 : 2.427 GHz
Channel 05 : 2.432 GHz
Channel 06 : 2.437 GHz
Channel 07 : 2.442 GHz
Channel 08 : 2.447 GHz
Channel 09 : 2.452 GHz
Channel 10 : 2.457 GHz
Channel 11 : 2.462 GHz
Current Frequency=2.452 GHz (Channel 9)
The wherefore and the why. And the how.
No channel 13. Wut? I found this informative mailing list post detailing what’s happening here. Basically, the kernel first loads the rules for the ‘world’ domain. On planet Earth, no countries exist that allow you to go beyond these frequencies (for domestic wifi). But many countries have additional restrictions. In Japan, you can use channel 14, in Europe you can’t. You can use 12 and 13 though, which in the US you can’t.
The kernel (well, the “ath” driver) then proceeds to look for any hints from the hardware. In my case the value burned into the hardware EEPROM is 0×0. That’s a value, which, per the Atheros specs, should map to the US. The kernel calls a userspace program crda to get the list of allowed frequencies and signal strenghts for the US.
After this has been done, you can do iw reg set FOO until you drop but you wont be able to go beyond the limits set previously.
The weird thing with radio hardware is that not only the users are subject to regulation, the sellers are too. You can’t sell non-compliant radio hardware. Now, the wifi chip manufacturers do not physically limit a chip intended for sale in the US to channels 1-11. Rather, they mass produce chips and then the equipment manufacturers are supposed to configure the chip in compliance with its target market. (As if you’d never take your equipment to Japan).
As I understand it, TP-Link messed up here.
Compare the above setup with what happens on my laptop’s Atheros chip:
ath: EEPROM regdomain: 0x65
ath: EEPROM indicates we should expect a direct regpair map
ath: Country alpha2 being used: 00
ath: Regpair used: 0x65
Looking at the Linux 2.6.35 sourcecode in drivers/net/wireless/ath/regd_common.h you’ll see that 0×65 maps to WOR5_ETSIC, which holds rules for compliance with EU regulation. Which I suppose is right since I’ve bought that laptop in the EU. But my TP-link comes out as FCC3_FCCA — that’s FCC-land, the US.
Fixing it
So either patch the kernel’s mappings, or patch the userspace ‘crda‘ program to supply a modified list of allowed frequencies for the ‘US’. I chose the latter. Reading a bit more about this program and its database, it didn’t appear I could fix it easily — the database is digitally signed and the public key needs to be known to the crda program at compile-time. No problem on a Gentoo system, but the target system was OpenWRT. On a MIPS machine. That meant cross-compiling, so I proceeded by downloading the OpenWRT dev environment for the Backfire release, and had a look around. And whaddayaknow, package/crda/patches/101-make_crypto_use_optional.patch made me hope that signature checking wasn’t even compiled in! On my OpenWRT box:
root@t33t:~# ldd /sbin/crda
libnl-tiny.so => /usr/lib/libnl-tiny.so (0x2aabe000)
libm.so.0 => /lib/libm.so.0 (0x2aad6000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x2aaff000)
libc.so.0 => /lib/libc.so.0 (0x2ab1f000)
ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0x2aaa8000)
(Don’t you just love it). Now I don’t know a lot about embedded environments and uclibc, but I don’t see any libcrypto.so or libgcrypt.so in there. It looks like I can just supply any old unsigned database. More reading of documentation, some compiling of tools.
regdbdump /usr/lib64/crda/regulatory.bin gives me an editable version of the database. I didn’t feel like limiting myself, so I replaced this:
country 00:
(2402.000 - 2472.000 @ 40.000), (3.00, 20.00)
(2457.000 - 2482.000 @ 20.000), (3.00, 20.00), PASSIVE-SCAN, NO-IBSS
(2474.000 - 2494.000 @ 20.000), (3.00, 20.00), NO-OFDM, PASSIVE-SCAN, NO-IBSS
(5170.000 - 5250.000 @ 40.000), (3.00, 20.00), PASSIVE-SCAN, NO-IBSS
(5735.000 - 5835.000 @ 40.000), (3.00, 20.00), PASSIVE-SCAN, NO-IBSS
country US:
(2402.000 - 2472.000 @ 40.000), (3.00, 27.00)
(5170.000 - 5250.000 @ 40.000), (3.00, 17.00)
(5250.000 - 5330.000 @ 40.000), (3.00, 20.00), DFS
(5490.000 - 5600.000 @ 40.000), (3.00, 20.00), DFS
(5650.000 - 5710.000 @ 40.000), (3.00, 20.00), DFS
(5735.000 - 5835.000 @ 40.000), (3.00, 30.00)
with this:
country 00:
(2402.000 - 2494.000 @ 40.000), (N/A, 30.00)
(4910.000 - 5835.000 @ 40.000), (N/A, 30.00)
country US:
(2402.000 - 2494.000 @ 40.000), (N/A, 30.00)
(4910.000 - 5835.000 @ 40.000), (N/A, 30.00)
and then I converted the database to its binary form with db2bin.py which one can find in the wireless-regdb source code releases. Copied the new database over to my OpenWRT machine, restarted it, dmesg, presto:
cfg80211: Calling CRDA to update world regulatory domain
cfg80211: World regulatory domain updated:
(start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
(2402000 KHz - 2494000 KHz @ 40000 KHz), (N/A, 3000 mBm)
(4910000 KHz - 5835000 KHz @ 40000 KHz), (N/A, 3000 mBm)
ath: EEPROM regdomain: 0x0
ath: EEPROM indicates default country code should be used
ath: doing EEPROM country->regdmn map search
ath: country maps to regdmn code: 0x3a
ath: Country alpha2 being used: US
ath: Regpair used: 0x3a
phy0: Selected rate control algorithm 'ath9k_rate_control'
phy0: Atheros AR9100 MAC/BB Rev:0 AR2133 RF Rev:a2 mem=0xb80c0000, irq=2
cfg80211: Calling CRDA for country: US
cfg80211: Regulatory domain changed to country: US
(start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
(2402000 KHz - 2494000 KHz @ 40000 KHz), (N/A, 3000 mBm)
(4910000 KHz - 5835000 KHz @ 40000 KHz), (N/A, 3000 mBm)
root@t33t:~# iwlist wlan0 channel
wlan0 14 channels in total; available frequencies :
Channel 01 : 2.412 GHz
Channel 02 : 2.417 GHz
Channel 03 : 2.422 GHz
Channel 04 : 2.427 GHz
Channel 05 : 2.432 GHz
Channel 06 : 2.437 GHz
Channel 07 : 2.442 GHz
Channel 08 : 2.447 GHz
Channel 09 : 2.452 GHz
Channel 10 : 2.457 GHz
Channel 11 : 2.462 GHz
Channel 12 : 2.467 GHz
Channel 13 : 2.472 GHz
Channel 14 : 2.484 GHz
Current Frequency=2.452 GHz (Channel 9)
Legality
This is a rather libertarian approach. Not legal on Earth, it doesn’t even comply with the ‘World’ regulatory domain. Yes, among other things, this gives you access to the ‘Japanese’ channel 14. The rules don’t prevent you from going totally EMP on your neighbourhood. (Your hardware does, though).
It’s now YOUR responsibility to comply with local regulations. If you look out the window and see an airliner perform a barrel roll, you know it’s time to iw reg set $YOURCOUNTRYCODE.
And remember, interference works both ways. The fact that your neighbours cannot use some frequencies doesn’t necessarily mean you’ll get good performance using those (frequencies I mean). It may well be that you’ll have lousy reception because other (possibly more powerful) equipment is legally being used on those channels. Just stick with your country’s regulations OK?
I kept the previous ‘US’ entry as ‘UX’ so I can still comply with US regulations if necessary. I hear they’re big on regulations in the US. Especially if it involves foreigners.
Tags: atheros, English, hack, linux, openwrt, wifi —

Posted by
Wicher
Topic:
Tech
September 1st
2010

Last night my Sheevaplug’s power supply unit blew up. Slept clean through it but I think the fumes were responsible for the unorthodox dreaming I did that night. Incidentally, the dream was about electronics. I dreamt I was part of some development project, bringing electronics to the Indian countryside and I had just invented this incredibly tiny airplane which could be made from readily available components such as tins and shrubs. It could fly a route autonomously (!) and had a cargo net which could be used for transporting lumber, cattle, children – anything a farmer family would need transported. The size of a large model airplane, one was supposed to hold on to its landing gear (with one’s legs taking over landing gear functions – better bring some bandages) and enjoy the flight. So I was flying around the hot Indian plains, soldering iron tucked under my belt, spreading the tale of this quantum leap in mobility in the land of Vishnu and Sheeva.
Back to my Sheevaplug. It was no more. A capacitator in the power supply unit (that converts 230VAC to 5VDC) had made a complete mess of itself. And the rest of the PSU. It looks like burnt Marshmallows but smells worse.
Also, the button cell that keeps the system clock running when power is lost, had swollen to unhealthy dimensions and had barfed its contents over part of the mainboard:

Well, at least I’m not alone in getting the marshmallow treatment. It appears to be a heat issue on the first version of the development kit.
Amazingly, the PSU still supplied about 2V, enough to make the blue LED blink to signal me that my marshmallows were ready.
I keep this box with old AC/DC adapters around. I’ve adapted my Sheevaplug to contain a socket to plug in a jack from an external 5VDC 2A supply, replaced the CMOS battery, and after some initial problems with the unit losing track of time after being powered down, all is working fine again. But better not take this Sheevaplug to the scorching heat of the land of Sheeva.
Tags: English, kaboom, sheevaplug —
May 8th
2010
Intro – running a cluster
So I had assembled this computing cluster thing based on execnet for a CS project. The slave nodes were dozens of student lab computers. Those are running firewalls blocking all ingress traffic, not just from the big bad internet but also from the lab networks — which contain only trusted machines and are already protected by a firewall restricting flow to and from larger networks!
Thing is, I need access to the SSH daemons running on those nodes. And I do not (and should not) have root on those machines…
Solving this problem on OSI Layer-8 proved to be an inefficient approach. The best way to fight bureaucracy is to not take it seriously. A bureaucracy (almost by definition) is not creative and cannot keep up with workarounds. To admit existence of creative, necessary workarounds to achieve legitimate ends is to admit defeat and that is why workarounds usually get ignored. It’s ignore and counter-ignore which is a bit sad since there is (or should be) a common goal: education, in this case. But I digress. The solution proved to be to ignore layer 8 and solve the problem on OSI layer 4. That certainly saved a lot of keystrokes!
To punch through the firewall, I have each slave node initiate a reverse tunnel (ssh -R) to a central node, using pubkey authentication. This way the otherwise unreachable SSH daemons on the slave nodes are proxied on the master node and are reachable via a socket on the master nodes’ loopback interface. The slave nodes specify the reverse tunnel endpoint port based on their IP. They connect automatically, and with some clever but too-kludgy-to-show scripting I can dynamically update my running clusterfied program with newly joining slave nodes to export compute jobs to.
Obviously, the slave nodes need to login to the central node via SSH and do so using a single account on the central node. But I don’t fully trust the slave nodes. I want them to be able to initiate reverse tunnels, and not do much else. Here’s how I approached this problem, with the server running OpenSSH 5.3p1.
Chroot
Chrooting comes to mind. With a chroot, you can limit the filesystem locations the user can access to some remote uninteresting branch of the tree.
From man 5 sshd_config:
ChrootDirectory
Specifies a path to chroot(2) to after authentication. This path, and
all its components, must be root-owned directories that are not writable
by any other user or group. After the chroot, sshd(8) changes the
working directory to the user's home directory.
The path may contain the following tokens that are expanded at runtime
once the connecting user has been authenticated: %% is replaced by a
literal '%', %h is replaced by the home directory of the user being
authenticated, and %u is replaced by the username of that user.
I set up a group for that and use the following ‘Match’ stanza in the central node’s sshd_config:
Match Group chrootage
ChrootDirectory %h
%h is the matched user’s homedir.
I set up a user named ‘chroot’ with a not-so-regular home directory. More on that home directory later on. This user needs a valid shell, even if it will never run that shell (I do not want them to run one). Without a valid shell the user will not be able to log in.
ForceCommand
From man 5 sshd_config:
ForceCommand
Forces the execution of the command specified by ForceCommand, ignoring
any command supplied by the client and ~/.ssh/rc if present. The command
is invoked by using the user's login shell with the -c option. This
applies to shell, command, or subsystem execution. It is most useful
inside a Match block.
The command originally supplied by the client is available in the
SSH_ORIGINAL_COMMAND environment variable. Specifying a command of
``internal-sftp'' will force the use of an in-process sftp server that
requires no support files when used with ChrootDirectory.
Well that ought to suit my purposes. Ideally, this command would be a rather boring and useless one that just sits there waiting for input. I could copy cat and the libs it links to over to the chroot directory. I might as well make it the login shell for the chroot user. But the user would then still be able to use sftp. And if I would want to disable sftp, I would have to do so server-wide! Me and the other users use sftp extensively, so I’d then need to run a second SSH daemon with no sftp subsystems especially for this purpose. Hmmm.
Another approach is to limit the user to the internal sftp subsystem. Added benefit is that I do not need to maintain any binaries and libs in the chroot. With this, the user logging in with SSH is presented by a boring interpreter in which he cannot launch any programs — it’s the sftp subsystem. Portforwarding, which is needed for the reverse tunnel, will work. SFTP will work, obviously. Let’s do it.
sshd_config:
Subsystem sftp internal-sftp
Match Group chrootage
ChrootDirectory %h
ForceCommand internal-sftp
Make SFTP boring — chrooted home directory contents
So what do we need inside the chroot directory? No binaries or device nodes, that is one of the advantages of using the internal-sftp subsystem.
We do need an authorized_keys file readable by the chrooted user since this user won’t be able to log in using pubkey auth otherwise. We can’t use AuthorizedKeysFile inside a Match stanza, so the authorized_keys will have to reside in the default location — the .ssh subdirectory of the user’s homedir. I will use authorized_keys to limit the set of hosts this user can login from to the domain of the slave nodes, so obviously the user should not be able to add keys or lift limitations — authorized_keys will need to be read-only. This is what I ended up with.
#ls -Rla /path/to/chrooted_homedir
/path/to/chrooted_homedir/:
total 16
drwxr-xr-x 3 root root 178 May 8 16:36 .
drwxrwxrwt 9 root sys 4887 May 8 16:36 ..
dr-xr-x--- 2 root chrootage 189 May 8 16:22 .ssh
/path/to/chrooted_homedir/.ssh:
total 12
dr-xr-x--- 2 root chrootage 189 May 8 16:22 .
drwxr-xr-x 3 root root 178 May 8 16:36 ..
-rw-r----- 1 root chrootage 1221 May 8 14:25 authorized_keys
This is all the user can view over sftp. It is minimal and extremely boring. Note that there is no place the user can write to. Even if he would have some way of launching programs (he hasn’t, due to ForceCommand) there is no place to upload them to and launch them from in the first place.
On to authorized_keys:
no-pty,from="*.science.uu.nl" ssh-rsa AAAAB3and-the-rest-of-the-pubkey
no-pty serves to prevent the allocation of a terminal. I don’t think it offers any security beyond the measures already in place, but I like to think this saves resources.
Tunneling risks
I need TCP forwarding to get the reverse tunnels to work. That means that using the account, users can set up arbitrary TCP tunnels, using the master node as a proxy… Less nice. But wait – can’t I use PermitOpen inside Match to limit the creation of arbitrary tunnels? Yes. But the reverse tunnel is subjected to the same restrictions. Therefore, I would need a PermitOpen for every slave node and I’d need to know the source port of the incoming client connection in advance. There’s no wildcarding in PermitOpen, so this is infeasable.
‘Match’ directive precedence
The order appears to be significant. That’s also what the manpage seems to hint at where it says
Match:
Introduces a conditional block. If all of the criteria on the Match
line are satisfied, the keywords on the following lines override those
set in the global section of the config file, until either another Match
line or the end of the file.
It’s not as clear as could be what happens to keywords in the next Match directive. What if the next Match directive matches? Nothing! The next Match directives don’t matter! In my tests, incoming connections appeared to be only matched once. The match blocks have no relation to eachother, only to the main config.
That implies that you need to put the most specific match directives — the ones with the most patterns — on top, and that each Match block needs to hold any and all keywords you want to specify beyond the main config. It’s not CSS ;-)
Tags: chroot, English, openssh, sftp, tunnel —
April 16th
2010
It’s not too late for posts about April Fool’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’s no authentication & 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!
Due to this fact I just need to know the URLs of your pictures. From the URL I can determine whether it’s a profile picture, profile picture thumbnail, photo, photo thumbnail, etc.
Wouldn’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’re browsing your friend’s albums, you not only see his pictures but pictures from other peoples’ albums too, and vice versa?
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’s beyond Facebook. It’s crowdbrowsing. It’s Megafacebook.
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.
“Why is everyone staring at me like that?” you naively wonder. (They’ve seen those pictures).
“Does she know that I know about those pictures of her and her friends? But wait… what might she know about me?”, your paranoid mind ponders.
It’s all about what you think of others and what others think of you. Total absorption. Now that’s what I call social networking. All hail Facebook Social!

Give the wifi crowd at your local coffeeshop the pleasure of learning a little bit more about eachothers lives and friends.
Get to work
You need:
- one network vulnerable to ARP poison routing (that’s most of them) or one network which you already control anyway. Make everyone route their traffic through your machine.
- one installment of the Nginx web server, configured with
--with-http_random_index_module. I use the 0.8.3x series.
- one installment of the Squid http proxy server. I use the 3.1 series.
- Perl and LWP::Simple.
Set up Nginx
Create some directories to hold the images:
mkdir /var/www/facemix/{albums,photos,photosthumb,smoelen,smoelenthumb}
Tell Nginx to respond to requests for those directories by randomly serving one of the files in them:
location ~ ^/facemix/([^/]+)(/?.*)$ {
alias /var/www/facemix/$1/$2;
random_index on;
expires -1;
}
You need the ‘expires -1′ to avoid caching. If proxies or user agents were to cache the results, they wouldn’t be very random anymore now would they.
Stick some files in there and test your installation.
Set up Squid
Set up squid in interception mode. If you’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.
Set up networking
This is for iptables.
- You’re NATting the pwned hosts. Run something along the lines of
iptables -t nat -A PREROUTING -i $INTERFACE -p tcp --dport 80 -j REDIRECT --to-port 8080
to redirect all traffic incoming on $INTERFACE and destined for port 80 to port 8080, which is where you need squid to listen on.
- You’re doing 2-way ARP poisoning (cheers!). Run something along the lines of
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j DNAT --to-destination $YOURIP
Squid needs to run on port 80 on interface with IP $YOURIP.
Check Squid’s logs to verify that requests are intercepted successfully.
Run the redirection script
I don’t touch Perl very often, and cobbling together this script made me remember why that is. It’s very usable as a means of frightening little kids.
In a nutshell, what my redirector script does is
- determine whether the URL fed to it by Squid is a facebook picture url;
- if so, and if we don’t have that picture yet, fork off to download it;
- point Squid to a random picture of the same type (served by Nginx).
I like the forking. I dislike the iffed regexes which could probably be condensed into one but then it wouldn’t be ‘cobbling together’ anymore.
Adjust the variables for your setup and tell Squid about the script (eg url_rewrite_program /usr/local/lib/facemix-squidredir.pl).
The Facebook logo will change to reflect the fact that the users are now browsing facebook in Social! mode.
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,
« inhale »
… 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 any picture of any user regardless of who you are? Who are you kidding?
If you’re still reading, here’s the script:
#!/usr/bin/perl -w
use LWP::Simple;
$WEBROOT = 'http://localhost/facemix/';
$WEBDIR = '/var/www/facemix/';
$CHANCE = 5; #One in X requests gets mixed
$SIG{CHLD} = 'IGNORE';
$|=1;
while (<>) {
local @reqfrags = split(/ /, $_);
local $url = @reqfrags[0];
if ($url =~ /(^http:\/\/.*.fbcdn.net\/rsrc.php\/z7VU4\/hash\/66ad7upf.png$)/) {
print "http://smormedia.gavagai.nl/2010/04/FacebookSocial2.png\n";
}
elsif (($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_n.jpg$)/) || ($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/n.*.jpg$)/)) {
&mixurl('photos/',$url);
}
elsif (($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_s.jpg$)/) || ($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/s.*.jpg$)/)) {
&mixurl('photosthumb/',$url);
}
elsif (($url =~ /(^http:\/\/profile.*.fbcdn.net\/.*\/.*_n.jpg$)/) || ($url =~ /(^http:\/\/profile.*.fbcdn.net\/.*\/n.*.jpg$)/)) {
&mixurl('smoelen/',$url);
}
elsif (($url =~ /(^http:\/\/profile.*.fbcdn.net\/.*\/.*_q.jpg$)/) || ($url =~ /(^http:\/\/profile.*.fbcdn.net\/.*\/q.*.jpg$)/)) {
&mixurl('smoelenthumb/',$url);
}
elsif (($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_a.jpg$)/) || ($url =~ /(^http:\/\/photos-.*.fbcdn.net\/.*\/a.*.jpg$)/)) {
&mixurl('albums/',$url);
}
else
{
print $url."\n";
}
}
sub mixurl {
#args: subdir, url
local $vork = fork();
if ($vork == 0) {&getit($_[0], $_[1]);}
if (int(rand($CHANCE)) == 0) {
print $WEBROOT.$_[0]."\n";
}
else {
print $_[1]."\n";
}
}
sub getit {
#args: subdir, url
local $storedir = $WEBDIR.$_[0];
local @urlfrags = split(/\//, $_[1]);
local $fname = pop(@urlfrags);
if (!stat($storedir.$fname)) {
getstore($_[1],$storedir.'._tmp-'.$fname);
rename($storedir.'._tmp-'.$fname, $storedir.$fname);
}
exit;
}
Tags: aprilfools, English, facebook, security, squid, upsidedownternet, url_rewrite_program —