maandag 23 december 2013

#19 | Include images in Latex

Let's say you have an image that you would like to incorperate into another image. If you convert both images to PDF, you can use latex for this. Code below will include small.pdf into a3.pdf (using a3paper). Adjust hspace, vspace and width to move your image around.

\documentclass{article}
\usepackage{pdfpages}
\usepackage[a3paper]{geometry}
\begin{document}
\pagestyle{empty}
\includepdf[ pagecommand={ \begin{figure} \hspace*{+0.8in} \vspace*{-1.1in} \includegraphics[width=500pt]{small.pdf} \end{figure} } ]{a3.pdf}
\end{document}

dinsdag 9 april 2013

woensdag 14 december 2011

#17 | Printing via my TP-Link router with DD-WRT

  • enable JFFS support via the web interface (like this)
  • enable USB support via the web interface (everything but usb storage)
  • Download and install the server
wget http://downloads.openwrt.org/snapshots/trunk/atheros/packages/p910nd_0.95-2_atheros.ipk
ipkg install p910nd_0.95-2_atheros.ipk
  • put this watch script in /jffs/
wget http://wl520gu.googlepages.com/watchprinter
chmod +x watchprinter
  • Add these commands to the startup script via the web interface (administration - commands - save startup)
/bin/mkdir -m 755 -p /dev/usb
/bin/mknod -m 660 /dev/usb/lp0 c 180 0
  • Add the watch script to crontab via the web interface (administration - cron)

zondag 14 augustus 2011

#16 | Compiling glibc on Ubuntu 10.10

Is a pain in the ass. For sure :)

#Somehow mawk gives us trouble?
sudo apt-get install gawk
sudo ln -s /usr/bin/gawk /usr/bin/mawk

#Checkout the latest glibc source via GIT (currently 2.14)
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout --track -b glibc-2_14-branch origin/release/2.14/master

#apply patch (manually): http://sourceware.org/ml/libc-alpha/2008-05/txt00002.txt
#apply patch (manually): http://www.linuxfromscratch.org/patches/lfs/6.8/glibc-2.13-gcc_fix-1.patch

#To avoid the use of SYS ENTER system calls:
#apply patch (manually): http://stackoverflow.com/questions/2131094/how-to-force-gcc-use-int-for-system-calls-not-sysenter


#Somehow Ubuntu doesn't like SSP?
export CFLAGS="-O2 -g -fno-stack-protector -U_FORTIFY_SOURCE"

cd ..
mkdir build
mkdir install
mkdir install/etc/
touch ./install/etc/ld.so.conf
cd build
../glibc/configure --prefix=/home/vvdveen/mylibc/install
make
make install


After this, I use static linking to use the new library:
gcc file.c -L/home/vvdveen/mylibc/install/lib -lc -static

donderdag 21 juli 2011

#15 | More networking

Now that I turned my desktop into a wireless access point, it's time to play a bit with it. I wanted to do something like the Upside-Down-Ternet and gathered most relevant information to do so from g0tmi1k.

First the DHCP part. My AP is still unsecured, which means that we can play around with freeloaders. I want to make sure that my own laptop connection is steady, however. To accomplish that, I'll create two subnets, both running on the same interface. One of them will be the 'secure' network, the other one will be the funny one. The /etc/dhcp3/dhcpd.conf listed below does exactly this (10.0.0.0/24 being the 'secure' subnet, providing only one fixed IP-address (10.0.0.20) for one known host, 192.168.0.0 being the 'funny' one, providing IP-addresses in the range of 192.168.0.10-192.168.0.100).
default-lease-time 3600;
max-lease-time 86400;

log-facility local7;

shared-network local {
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.10 10.0.0.100;
option routers 10.0.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers x.x.x.x, x.x.x.x;
deny unknown-clients;

host D430 {
hardware ethernet xx:xx:xx:xx:xx:xx;
fixed-address 10.0.0.20;
}
}
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.100;
option routers 192.168.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers x.x.x.x, x.x.x.x;
allow unknown-clients;
}
}
For each subnet, DHCP will now search for an interface which has an IP-address on that subnet. Since we're using a single access point, build on wlan0, we'll need to assign two IP-address to this interface:
sudo ifconfig wlan0 10.0.0.1 netmask 255.255.255.0
sudo ifconfig wlan0:0 192.168.0.1 netmask 255.255.255.0

Note that the log-facility local7; line in the above dhcpd.conf forces syslog to use local7 as output log. If we edit /etc/rsyslog.conf and add the line local7.debug /var/log/dhcpd.log to it, then DHCP logs will be written to /var/log/dhcpd.log instead of the general syslog file. If you do this, make sure to restart the syslog daemon after updating its configuration: sudo service rsyslog restart.
We can now (re)start the DHCP server with sudo /etc/init.d/dhcp3 restart (or sudo /etc/init.d/isc-dhcp-server restart on newer Ubuntu releases)

After all above, clients should still be able to connect. Trusted clients, however, should be placed on the 'trusted' network, while unknown clients will be on the 'funny' subnet :)

Now that we can distinguish the two networks, it is time to play a bit. You could, for example, redirect all web traffic towards a single IP-address (xkcd.com) using IP-tables:
sudo iptables -t nat -A PREROUTING -s 192.168.0.0/255.255.255.0 -p tcp --dport 80 -j DNAT --to-destination 72.26.203.99


We'll be having more fun when we redirect everything through a squid proxy. Download and extract these scripts into (e.g.) /scripts and chmod them 755. Note that you probably have to update the $ourIP variable in those scripts (into 192.168.0.1 in this situation).
Then remove the just created iptables rule and install squid3, apache2 (yeah, we also need apache) and some more tools:
sudo iptables -t nat -D PREROUTING 1
sudo apt-get install squid3 apache2 imagemagick ghostscript jp2a

As shown on g0tmi1k, the things we need to change in /etc/squid3/squid.conf are the following:
  • change "http_port 3128" into "http_port 3128 transparent"
  • Add "acl localnet src 10.0.0.0/24" after the "#acl localnet src ..." lines
  • Add "http_access allow localnet" near the "http_access" statements
  • (at EOF) add "url_rewrite_program /scripts/blurImages.pl" (or any other script)
We are almost there:
sudo mkdir /var/www/tmp
sudo chown nobody:nogroup /var/www/tmp
sudo chmod 777 /var/www/tmp
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/squid3 restart
sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 80 -j REDIRECT --to-port 3128

woensdag 13 juli 2011

#14 | Linux (Ubuntu) as Wireless Access Point

This used to be a lot harder, but thanks the work on Linux wireless drivers, turning your desktop PC into an Wireless Access Point is only a few clicks away. Check this out to learn more about the Linux wireless 'community' (?)
Setting up an access point involves some different steps:
  1. Setup the access point
  2. Enable a DHCP server
  3. Enable IP forwarding and NAT on the host
I'm assuming an Ubuntu installation with a direct Internet connection via eth0 and a wireless device doing nothing on wlan0.

(pre)

We first need to be sure that the device actually supports being an AP at all. This was discussed before, but comes down to
  1. Do a lspci -nn | grep -i wireless to find the ID (%4x:%4x) pair of your device.
  2. Search cateee.net/lkddb/ for this particular ID. In my case the Google search query was "168c 0029" site:cateee.net/lkddb/. If there is a hit, this will allow you to learn which driver to use for your device. If there is no, the device is most probably unsupported.
  3. Now, look up your driver on this list and see if it supports AP mode.
Now that we know that our device is supported, we can install the required packages: sudo apt-get install hostapd dhcp3-server

(Setting up the access point)

For this, we'll be using hostapd. A very simple first configuration is shown below.
interface=wlan0
driver=nl80211
ssid=test
hw_mode=g
channel=1
This should be enough to get things up and running. Save this configuration to /etc/hostapd/hostapd.conf and give it a try afterwards: sudo hostapd /etc/hostapd/hostapd.conf. You should be able to see your freshly created network now on other computers when searching for wireless networks in range. Since we have no DHCP server running yet, it will be a bit harder to connect to it.

(Enable the DHCP server)

Let's keep hostapd running in your terminal and setup the DHCP server. Something like the following should be sufficient for your /etc/dhcp3/dhcpd.conf:
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.111.255;
option routers 192.168.111.151;
option domain-name-servers {YOUR DNS1 HERE},{YOUR DNS2 HERE};

subnet 192.168.111.0 netmask 255.255.255.0 {
range 192.168.111.1 192.168.111.100;
}

Make sure to replace {YOUR DNSx HERE} with the appropriate IP addresses of your DNS servers.
Before starting the DHCP server, we need to set the wlan0 device to the correct IP address. I already assumed that this would be 192.168.111.151. So a simple sudo ifconfig wlan0 192.168.111.151 will do. We can now start the DHCP server: sudo /etc/init.d/dhcp3-server start.
It should now be possible to connect to the new AP and ping the host (192.168.111.151). Browsing the web won't work: the host is still dropping IP packets which have a different destination than its own IP address.

(Setup the routing programme)

To let clients browse the web, the host needs to forward IP packets that are destined to one of the clients instead of just dropping them (default reaction). For this, we first enable IP forwarding in the kernel: sudo sysctl net.ipv4.ip_forward=1, which is saying something to the kernel like "Forward all IP packets towards iptables (which we'll set next)".
I'm using three (copied) iptables rules to enable NAT.
iptables -A FORWARD -i $RECEIVE -o $BROADCAST -s 192.168.111.151/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE

As far as I can understand, the first rule says "Forward packets from the 192.168.111.151/24 subnet that are trying to setup a new connection" (wireless clients are allowed to setup connections). The second rule is short for "Forward packets that are part of an existing connection" (once a connection is setup, both wireless clients and server they are communicating with are allowed to send data). The last rule enables IP masquerading so that packets are actually routed the way the should be.

That's all for now, more fun about this later!

woensdag 6 juli 2011

#13 | MINIX 3 voting machine implementation

I was assigned an Individual Programming Assignment concerning the implementation of a voting machine in MINIX 3. We never managed to get it working, but it was quite fun to find out how to implement CD-Write support.
I've uploaded the final document.