Wednesday, September 12, 2012

Block facebook on a proxied internet with iptables

Those who access internet by setting a proxy(insti proxy!!) in their browsers and want to block facebook on their computers can issue the following command as root on their linux box

iptables -I OUTPUT -m string --algo bm --string "facebook.com" -j DROP

I can't control myself from opening Facebook every now and then, when working in lab, just to see what's going on. Facebook is my biggest distraction. Hope this move will help me to concentrate on work.
By the way, iptables doesn't persist the firewall rules across restarts. So I have appended the above command to my /etc/profile file so I don't have to manually type the above command when I fresh boot my computer.
By the way the above command will block all packets that match the string facebook.com, so if you try to search for facebook.com in Google, you won't be able to see the results page, as the search query will never reach Google.

And yeah, if you want to unblock facebook, check the rule number by issuing
iptables -L OUTPUT
And see the line number. If the rule you created is on 1st line then line number is 1. Now issue the following command:
iptables -D OUTPUT 1



Sunday, July 29, 2012

Migration to systemd on Arch

Today I migrated to systemd on my Arch system. All the mortals unaware of the word, please allow me the privilege to introduce systemd - the latest replacement PID 1 in your linux box. As you might be aware that after the kernel loads up, it launches init in the inter-stellar space with the process ID of 1 which is responsible for forking other payloads into the user space. The traditional SysV Init boot method was conceived decades ago and has been used almost unaltered in most of the linux distros. It was excogitated in the age when processors were babies with single cores which could compute ONLY a few million instructions a second, the computers had only crossed the stone age and had just stepped into the semiconductor age(the second part of this statement is technically wrong, take it just as a humorous piece). SysV Init starts the userspace processes one by one. But today in the age of multicore monsters with each of the 4(or 6) cores capable of simultaneously computing at a staggering speed of  more than a couple of billion instructions per second coupled with architectural innovations like simultaneous multithreading(SMT i.e. hyper threading in intel terms), advance tournament branch predictioning, added with speculative execution and deep pipelining that makes sure an application cleverly programmed can run almost as fast as the demarcation set by Amdahl's law; Sys V init is very inefficient since it's coders hadn't dreamt of these features in their weirdest of day(night)dreams.
Then some bored lazy guy named Lennard Pottering at Fedora fed up of waiting for the age old system boot process to load his OS had a mesmerizing realization of an Archimedes like enlightening idea - to boot a system fast, start less; and start more parallelly. And he came up with systemd along with some helping hands in the community. Although Ubuntu gave up on SysV Init long ago and took the upstart way, but systemd is any day any moment better than upstart.
systemd organises services and other things(beyond the scope of this post) into units. An assortment of one or more units can be grouped into a target, which is similar to the concept of runlevels but is not exactly run level. A target can be a requirement for another target(i.e. latter inherits from former). And systemd starts the units as parallelly as possible by making sure independent units can start together and a unit dependent on another is held on, until its dependency is started.
Enough of background story and theory! Lets see how easy it is to set it up on Arch. Archwiki has an awesome page for it, but n00bs are n00bs and are born to make mistakes and do things that they are not supposed to. Even non-n00bs can make silly mistakes :P
Install the packages systemd, systemd-sysvcompat, systemd-arch-units after removing the packages sysvinit and initscripts. But before removing initscripts, you may want to take a backup of /etc/rc.conf. Afterwards you should make a few files which are expected by systemd. They are /etc/hostname, /etc/vconsole.conf, /etc/locale.conf, /etc/timezone
# cp /etc/rc.conf /etc/rc.conf.bak
# pacman -Rns sysvinit initscripts
# pacman -S systemd systemd-sysvcompat systemd-arch-units
# echo `hostname` > /etc/hostname
In the file /etc/vconsole.conf write
KEYMAP=us
FONT=lat9w-16
FONT_MAP=8859-1_to_uni

and in the file /etc/locale.conf write

LANG=en_US.UTF-8
LC_COLLATE=C

and lastly in the file /etc/timezone enter your timezone, for example:

Asia/Calcutta

If you dual boot with Windows then to keep your linux system showing localtime write the following in /etc/adjtime

0.0 0.0 0.0
0
LOCAL
You should also uncomment the en_US-UTF8 UTF8 and en_US-ISO8859-1 lines from /etc/locale-gen and then run the locale-gen command on the terminal as root user to avoid locale warnings while next time an mkinitcpio is done or something else that requires it.
If you load some modules at boot time by adding them in /etc/rc.conf then to do the same with systemd, you can create a file named /etc/modules-load.d/modules.conf and add all your modules names each in a single line.
For example:

vboxdrv
vboxnetflt

If you run some services from /etc/rc.conf at boot time, then you can use:

# systemctl enable sshd.service

to keep starting them at boot. These service configs are in /usr/lib/systemd/system/ directory and are the units as introduced somewhere above in this post. All the files with extension .service are daemon units that you can run at boot or for certain targets, or later by the systemctl command.

In case of some services, for example networkmanager you will see an  error if you try to do a systemctl enable over it:
Failed to issue method call: No such file or directory

Don't panic. Just create a symlink in /etc/systemd/system for that file in multi-user.target wants.

Example:
# ln -s /usr/lib/systemd/system/networkmanager.service /etc/systemd/system/multi-user.target.wants/networkmanager.service

This may not exactly work in some cases if the daemon you want to start requires other daemons to be enabled too. In that case you can enable the required daemon first, or the required target first.

Hope, this should get you up and running with systemd on your arch box.
One more thing, systemd provides a nifty tool to analyse your boot time. Try these commands, and you will know what I mean:

$ systemd-analyze
$ systemd-analyze blame | less
$ systemd-analyze plot > ~/bootplot.svg
By the way my Fujitsu Siemens Amilo Si3655 with Core2Duo P8400 2.26GHz, 320GB hard disk, 4 gigs of RAM and pae kernel boots in less than 13 seconds. Here is my output from systemd-analyze.
Startup finished in 656ms (kernel) + 2521ms (initramfs) + 9648ms (userspace) = 12826ms
And here is my plot svg file.

References:
https://wiki.archlinux.org/index.php/Systemd
http://0pointer.de/blog/projects/systemd.html
http://0pointer.de/blog/projects/why.html
http://freedesktop.org/wiki/Software/systemd
http://distrowatch.com/table.php?distribution=arch

Notes:
1. Commands prefixed with # are to be run as root user, and ones prefixed with $ are to be run as normal user.
2. If you are not an arch user, please don't try to install systemd by following this article word by word. Fedora and OpenSuse come with systemd by default. Other distro users should check corresponding wiki and forums.

Monday, July 2, 2012

Fix disfunctional shutdown/restart button in Arch Linux

Arch Linux is one awesomely awesome operating system! You get a chance to build your own Linux system from scratch, right from installing the kernel, then installing core-utils, followed by editing config files and then setting up X and a Desktop Environment. I will post sometime later an step by step verbose article on building a system with Arch + XFCE + General utilities. For now I'll focus on the title of this post.

Those of you who run Arch and start your GUI with:

exec ck-launch-session dbus-launch startxfce4

in xinitrc and use SLIM might have noticed that you can't shutdown with the buttons available in your Desktop Env.

The reason is that the updated version of SLIM already launches a console kit session(the thing responsible to execute commands that require root access). So there are two console kit sessions and the conflict.
You can check the number of console kit sessions yourself by:
ck-list-sessions
To solve it, remove the portion ck-launch-session dbus-launch from the exec line.
Here is my .xinitrc, that you may use.

#!/bin/sh
# ~/.xinitrc
# Executed by startx (run your window manager from here)
if [ -d /etc/X11/xinit/xinitrc.d ]; then
  for f in /etc/X11/xinit/xinitrc.d/*; do
    [ -x "$f" ] && . "$f"
  done
  unset f
fi

DEFAULT_SESSION=startxfce4
case $1 in
  lxde)
  exec startlxde
  ;;
  xfce4)
  exec startxfce4
  ;;
  *)
  exec $DEFAULT_SESSION
  ;;
esac


/usr/bin/xscreensaver -no-splash &

Friday, October 28, 2011

Setup transparent net access over insti proxies

If you are a student at an institute that forces you to cross a damn proxy server for accessing internet, you must be fed up of entering your username and password time and again in browsers, as well as configuring each new application you install, to use the proxy. And if an application doesn't have any proxy configuration option, then you can't use that application! For example you can't install the Android Development plugin for eclipse. This is very irritating. And, some campuses add to this torture further by using crappy NTLM authentication.
Here in IIT Madras exactly this condition prevails. But here I present a step by step walk through to get rid of this situation. Welcome to transparent proxying!

What is transparent proxy?
A transparent proxy(correctly called intercepting proxy) intercepts your requests and forwards to the destination without letting your applications know that there is a proxy between you and internet. This is done with some intervention from iptables in Linux. So what are the advantages? You need not configure each and every application with the proxy address, port, username and passwords. You can keep the setting "Direct connection to internet" in every application and every darn-so-ever app will work flawlessly!!!! Cool?? Eh?? Lets start.

Configure a transparent proxy on your notebook
I assume you are using a recent version of Ubuntu. Even older ones will do :) The steps should work for any Linux distro provided you use appropriate distro specific commands.

Before installing software you should have a working internet connection. You may setup proxy in /etc/apt/apt.conf for the time being to install required software for now. You need to put the proxy details in apt.conf as follows for https, https and ftp:

Acquire::http::Proxy "http://<username>:<password>@<proxy-address>:<port>


An example:

Acquire::http::Proxy "http://cs11m039:bleeeep@proxy.iitm.ac.in:3128";
Acquire::https::Proxy "https://cs11m039:bleeeep@proxy.iitm.ac.in:3128";
Acquire::ftp::Proxy "ftp://cs11m039:bleeeep@proxy.iitm.ac.in:3128";


If you are not from computer science background then I should tell you that, to edit /etc/apt/apt.conf you should open terminal then type sudo gedit /etc/apt/apt.conf

Step 1: 
If your campus doesn't require NTLM authentication then you can simply install and configure squid to setup transparent proxying. The details for that are beyond the scope of this post and I may cover that sometime later or you may search the net for other writers who might have posted the same.
IIT Madras campus network has NTLM. So I need to install cntlm. It works as a proxy and performs NTLM authentication on your behalf so you don't need to enter username and password everytime ypu lauch the browser. Type the following command and press enter:
sudo apt-get install cntlm 
Now edit /etc/cntlm.conf file and edit these lines to reflect your settings:

Username cs11m039
Domain iitm
Password password
If you have different proxies in institute and hostels then you can specify them all in these lines



Proxy 10.93.0.38:3128
Proxy 10.93.0.34:3128
Edit socks proxy details too

SOCKS5Proxy 3128
SOCKS5User cs11m039:password

Leave all other settings as default. Instead of cs11m039, put your username and instead of password put your password. Instead of domain as iitm put your institutes NTLM domain. For SOCKS5Proxy put the port number of proxy.

Now restart cntlm by typing:

sudo service cntlm restart

Now open any browser and set the http proxy as 127.0.0.1 and port 3128. Try opening google.com. If it works, means you got cntlm installed perfectly. Proceed to next step. Else, double check your configuration. Make sure cntlm got installed. Also make sure if your computer is even turned on!!!!! If all else fails, write a comment here and I may reply if I feel like.(:P)

Step 2:
Install tinyproxy.
sudo apt-get install tinyproxy

Open its configuration file

sudo gedit /etc/tinyproxy.conf 

Edit these lines:
Port 8888
Upstream 127.0.0.1:3128
Comment the line that says Allow 127.0.0.1 by adding a leading # symbol before Allow.
Don't touch any other line.

Step 3:
Open /etc/default/tinyproxy and put this inside and close the file:


case "$1" in
  start)
    iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 8888
    ;;
  stop)
    iptables -t nat -F OUTPUT
    ;;
  restart)
    start-stop-daemon --stop --quiet -t --exec $DAEMON > /dev/null || exit 0

    iptables -t nat -F OUTPUT
    iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 8888
    ;;
esac


Now restart tinyproxy by typing sudo service tinyproxy restart 
Disable proxy settings in the browser and try opening a website. If you followed all the above steps correctly then the website should open. Also, you may now safely truncate the /etc/apt/apt.conf file and it will work. All your apps like installing software in Eclipse will work too without any problems.

This is how your internet is working now:

Your application requests for internet access which is received by Linux kernel and due to the iptables rule setup above the request is redirected to port 8888 where tinyproxy is running. Tinyproxy intercepts the non-proxyified request and makes it http-proxy request compliant then forwards it to upstream proxy which is cntlm running on port 3128. Cntlm then performs NTLM authentication and contacts the insti proxy server to serve your request.

User application<==>Kernel(iptables)<==>tinyproxy intercepting proxy<==>cntlm authentication proxy<==>insti squid proxy<==>The internet

Happy browsing!

Update
On 3rd November, IIT Madras ditched the NTLM authentication and implemented rather insecure HTTP Basic authentication with OpenLDAP. Now you don't require cntlm if you want to setup transparent proxy.

In step 1 above don't install cntlm. Just install polipo by:
sudo apt-get install polipo

Then open its configuration file by:
sudo gedit /etc/polipo/config

And set these values:
parentProxy = "hproxy.iitm.ac.in:3128"
parentAuthCredentials = "username:password"

where username and password should be your username and your password for OpenLDAP.Also make sure to comment the lines that stand for SOCKS proxy.
Also set its listening port to 3128, so that tinyproxy doesn't need to be reconfigured. And don't forget to uninstall or disable cntlm(if it is installed) from startup because both polipo and cntlm will try to capture port 3128 for listening and you may face issues.