Category Archives: Hacking

Hacking Code :)

Recursively checking out ports tree

I’ve come across a problem posted on #bsdports channel few days ago, how to download a port (FreeBSD) and its dependencies without checking out complete ports tree. So, I hacked a solution for this in Haskell to celebrate my recently attained bliss moment:

λ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load download-portstree.hs
[1 of 1] Compiling Main             ( download-portstree.hs, interpreted )
Ok, modules loaded: Main.
*Main> downloadPortsTree "editors/emacs-nox11"
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.2 ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package unix-2.6.0.1 ... linking ... done.
Loading package filepath-1.3.0.1 ... linking ... done.
Loading package directory-1.2.0.1 ... linking ... done.
Loading package process-1.1.0.2 ... linking ... done.
PortsList {portsSeen = fromList ["converters/libiconv","devel/gettext","devel/gmake","devel/libffi","devel/ncurses","devel/pkgconf","editors/emacs-nox11","lang/perl5.16","math/gmp","print/indexinfo","security/ca_root_nss","security/gnutls","security/libgpg-error","security/libtasn1","security/nettle","security/openssl","security/p11-kit","textproc/libxml2"], portsNew = fromList []}
Checking out port editors/emacs-nox11
Checking out port devel/gmake
Checking out port converters/libiconv
Checking out port devel/gettext
Checking out port devel/ncurses
Checking out port devel/pkgconf
Checking out port print/indexinfo
Checking out port security/gnutls
Checking out port lang/perl5.16
Checking out port security/libgpg-error
Checking out port security/libtasn1
Checking out port security/nettle
Checking out port math/gmp
Checking out port security/openssl
Checking out port security/p11-kit
Checking out port devel/libffi
Checking out port security/ca_root_nss
Checking out port textproc/libxml2

Happy hacking lambdas… 😉

Sagittarius Scheme — FreeBSD port

Created a FreeBSD port for Sagittarius Scheme for fun1, and have this:

SHA256 (sagittarius-scheme-0.5.5.tar.xz) = 1ddf2841af2d79ddf2a54346e97fe7c0e45b9c486df69b866d60e2e6c89f7e03

The only issue pending is, fixing building on 10-CURRENT, and 11-CURRENT (i386) platforms, which I need some time to look.

[UPDATE: There you go]

HTH


1In memory of a sagittarian, I once knew.

Listing FreeBSD packages in order of dependency

So, I upgraded GHC (via pkg) few minutes ago, and didn’t upgrade any of hs- packages, which means now their registration information is gone from GHC’s cache. So as a workaround I just decided to invoke their corresponding register.sh which would result in their package being registered:

λ pkg info |awk '/^hs-/ { print $1; }' |xargs -n1 pkg info -l |fgrep register.sh |xargs -n1 sudo /bin/sh

This was ideal command except it didn’t work because package list (pkg info) was not topologically sorted. And RTFM-ing pkg info didn’t result in any option which could output topologically sorted listing. But thanks to pkg info -d which generates the list of dependencies, I hacked this, which worked:

λ pkg info |awk '/^hs-/ { print $1; }' |xargs -n1 pkg info -d |awk '/^[[:space:]]+hs-/ { print $1; }'  |sort |uniq -c |sort -k1 -nr |awk '{ print $2; }' |sed -re 's/-[[:digit:]._,]+$//g' |xargs -n1 pkg info -l |fgrep register.sh |xargs -n1 sudo /bin/sh

🙂

Checkout specific patchset of FreeBSD sources

Following is a script to checkout FreeBSD sources (from its subversion repository) to specific patch-level as asked by a friend on IRC:

#!/bin/sh

if [ -z "$1" -o -z "$2" ]; then
        echo Usage: $0 release patchlevel
        echo e.g. To get 8.0-p5, $0 8.0 5

        exit 0
fi

RVER=$1
PVER=$2

SVNBASE=svn://svn.freebsd.org/base/releng
NEWVERS=sys/conf/newvers.sh
LIMIT=40
RETVAL=0

SVNLOG=$(mktemp -t $(basename $0))

if svn log -l $LIMIT $SVNBASE/$RVER/$NEWVERS >$SVNLOG; then
        BASEREV=$(awk 'BEGIN { sec=1; i=0; } /^r[[:digit:]]+/ { sec=0; revs[i++]=rev=$1; } /^(Security|Errata):/ { sec=1; } /^-+$/ { if(sec == 0) { print revs[i-'$PVER'-1]; exit 0; } }' <$SVNLOG)
        echo svn co -$BASEREV $SVNBASE/$RVER
else
        echo Error executing svn log
        RETVAL=$1
fi

rm -f $SVNLOG
exit $RETVAL

Obligatory screenshot:

chateau.d.if!abbe [~/bin] % co-freebsd-sources 7.0 4
svn co -r182740 svn://svn.freebsd.org/base/releng/7.0
chateau.d.if!abbe [~/bin] % svn cat -r 182740 svn://svn.freebsd.org/base/releng/7.0/sys/conf/newvers.sh|grep RELEASE-p
BRANCH="RELEASE-p4"
Download link: http://www.lostca.se/~abbe/scripts/co-freebsd-sources
SHA256 sum: c9958c4fd7cae5a5e9ff3fa84ba3af6adf38d9c8494b7913ad5b3a2f265a3f48

IRC proxy-ing

Sometime ago, I’ve received SSH access to a host which was quite restricted. Lots of executables are denied execution, as it was supposed to be only for learning, so no network access curl/wget/nc/socat/ssh -(L|D|R), or compilers, but then it’s got bash. Here is a tiny hack to connect to IRC from that host:

1. Create a file irc.sh on $host:

#!bash
exec 3<>/dev/tcp/irc.freenode.net/7070
cat <&3 &
cat >&3

2. Add following line to inetd/xinetd (or netcat):

sua     stream  tcp     nowait          $localuser    /usr/bin/ssh    ssh -i $privatekey -l $user $host bash irc.sh

3. Now connect your IRC client to localhost:sua (localhost:14001).

This is only a fun hack, and not something used to regularly circumvent access. 😛

ldd.sh

On numerous occasions, I need to find the direct shared library dependencies (for ELF ofcourse), and not what ldd(1) outputs. And at all those occasions, I’d to RTFM objdump(1) everytime, and now since I plan to ditch laziness, here is a script which I wrote:

#!/bin/sh

if [ -n "$1" ]; then
	objdump --all-headers $1|awk '/^[[:space:]]*NEEDED/ { print "'$1' "$2; }'
else
	echo Usage: $0 file.so
fi

Obligatory screenshot:

chateau.d.if!abbe [~] % ldd.sh /bin/ls
/bin/ls libutil.so.9
/bin/ls libncurses.so.8
/bin/ls libc.so.7

HTH