måndag 14 mars 2011

bind9 dnssec

Example:

To configure bind9 so that you can sign the zone with
rndc sign sics6.se; rndc freeze; rndc unfreeze;


in /etc/bind/named.conf.options:


options {
directory "/var/cache/bind";
key-directory "/etc/bind/dnssec";

...

};




in /etc/bind/named.conf.local:


include "/etc/bind/keys.conf";
include "/etc/bind/rndc.key";



controls {
      inet 127.0.0.1 port 953
              allow { 127.0.0.1; } keys { "rndc-key"; };
};



...



zone "sics6.se." IN {
        type master;
        file "/etc/bind/zones/sics6.se..signed";
        allow-update { key lra.sics.se.; };
        auto-dnssec allow;
};


måndag 7 februari 2011

Python virtualenv

Virtualenv http://pypi.python.org/pypi/virtualenv is very useful.  It creates a new file environment into which you can install python software without screwing up your already installed system python.

To create a virtual environment, if you already have  virtualenv installed, do

  • python -m virtualenv myEnv

otherwise,
  • download virtualenv-1.5.1.tar.gz
  • unpack it: tar xfz virtualenv-*-gz
  • create the environment:  python virtualenv-1.5.1/virtualenv.py myEnv
  • once the environment (myEnv) has been created,  you can remove the virtualenv-1.5.1 directory.
Example:
curl -s http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.1.tar.gz | tar zxvf -
python virtualenv-1.5.1/virtualenv.py myEnv

Now you can use myEnv/bin/python or myEnv/bin/pip to install packages

(pip can download and install python packages very neatly).


tisdag 1 februari 2011

wget and ssl certificates

wget and ssl certificates


To use certificates with wget, first get the root cerificate for the site.

1. Just have one certificate


You can either go to the site with Firefox, click on the little lock in
the status bar, in the Security tab click View certificate, click on Details,
click on the topmost certificate in the “Certificate Hierarchy”, click on
“Export…” and save as “.pem”.

Now you can use ssl with wget like this:

wget --ca-certificate={the_cert_file}  https://www.google.com

2. A directory full of certificates


Or you can have an entire directory full of certificates that wget
can choose from. Useful if you want to use all the certificates
from the KeyChain.app.

If you export all the certificates from KeyChain.app in one go
(you can select multiple and export all at once), then you must
split up the file into individual files for each certificate, and
name the certificates by their hash and a “.0” at the end.

If the certificates from KeyChain.app are saved into the file
Certificates.pem, then this splits these commands splits the
files and renames them.

mkdir certdir
cd certdir

n=0 ; cat ../Certificates.pem | while read x; do if [ "$x" == "-----BEGIN CERTIFICATE-----" ]; then n=$((n+1)); fi; echo >>cert-$n.pem $x ; done

for f in cert-*; do n=$(openssl x509 -hash -in $f -noout); mv $f $n.0; done

cd ..

Now you can use ssl with wget like this:

wget --ca-directory=certdir  https://www.google.com
If you want to, you can put the certdir in your ~/.wgetrc file so you
won't have to specify it all the time.  Just put the line

ca_directory = {full path to certdir}
and you're done.

Actually, you also need some way to handle revocation of certificates.
But that's for another day.

måndag 24 januari 2011

Building WiiMC on a Mac OS X

WiiMC is a homebrew program for Wii.
These are my bash notes on how to downloads a devkitPPC environment, install some libraries needed, and to show where to patch a file to make it compile.

Here are the bash commands...   Use them with care...  :-)



 
export WIIROOT=$(pwd)
 
svn checkout http://wiimc.googlecode.com/svn/trunk/ wiimc-read-only
svn checkout http://libext2fs-wii.googlecode.com/svn/trunk/ libext2fs-wii-read-only
svn checkout http://grrlib.googlecode.com/svn/trunk/GRRLIB/lib/zlib
 
export WIIMCSRC=$WIIROOT/wiimc-read-only
 
 
mkdir -p $WIIROOT/devkitpro
export DEVKITPRO=$WIIROOT/devkitpro
 
echo THIS IS IMPORTANT
echo export DEVKITPPC=$DEVKITPRO/devkitPPC
echo export PATH=$DEVKITPPC/bin:$PATH
 
export DEVKITPPC=$DEVKITPRO/devkitPPC
export PATH=$DEVKITPPC/bin:$PATH
 
 
mkdir -p $WIIROOT/download
cd $WIIROOT/download
 
DEVKITURL=http://sourceforge.net/projects/devkitpro/files
 
(
wget -c ${DEVKITURL}/devkitPPC/devkitPPC_r22-osx.tar.bz2 &
wget -c ${DEVKITURL}/libogc/libogc-1.8.6.tar.bz2 &
wget -c ${DEVKITURL}/examples/wii/wii-examples-20100930.tar.bz2 &
wget -c ${DEVKITURL}/examples/gamecube/gamecube-examples-20100930.tar.bz2 &
wget -c ${DEVKITURL}/libfat/libfat-ogc-1.0.8.tar.bz2 &
 
# wget -c ${DEVKITURL}/portlibs/zlib-1.2.4-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/mxml-2.6-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/libpng-1.4.1-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/jpeg8a-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/freetype-2.4.2-ppc.tar.bz2 &
wait
)
 
diff <(md5 *bz2) <(cat << EOF
MD5 (devkitPPC_r22-osx.tar.bz2) = 385300a31c4c2cc6890adfb8f220674c
MD5 (freetype-2.4.2-ppc.tar.bz2) = 0e91ad37ead10137cc9f8d2f3454a245
MD5 (gamecube-examples-20100930.tar.bz2) = e9ef264a5c7acd6ef6144b676527bacd
MD5 (jpeg8a-ppc.tar.bz2) = ef4c03019ade885dbba05982e777ea2b
MD5 (libfat-ogc-1.0.8.tar.bz2) = 495cb164afa9ca420fd21d4c78c0723f
MD5 (libogc-1.8.6.tar.bz2) = 5be16c7c972e8c92e637220adb123be1
MD5 (libpng-1.4.1-ppc.tar.bz2) = 29a9a6c754d32990f542990d3d38e715
MD5 (mxml-2.6-ppc.tar.bz2) = b196036aa9f0b310efb6bdc4b02614dc
MD5 (wii-examples-20100930.tar.bz2) = 5a37fb6cc1704f43cb3813ddbb964a24
EOF) 
 
#MD5 (zlib-1.2.4-ppc.tar.bz2) = c952918d7fb3e52e8fa66fa09f2edf87
 
 
if [ ! $? ] ; then
    echo The checksums on the downloaded files were wrong.
    exit 1
fi
 
cd $WIIROOT/download
 
tar -C $DEVKITPRO -xjf devkitPPC_r22-osx.tar.bz2
 
mkdir $DEVKITPRO/libogc
tar -C $DEVKITPRO/libogc -xjf libogc-1.8.6.tar.bz2
tar -C $DEVKITPRO/libogc -xjf libfat-ogc-1.0.8.tar.bz2
 
mkdir -p $DEVKITPRO/examples/{wii,gamecube}
tar -C $DEVKITPRO/examples/wii  -jxf wii-examples-20100930.tar.bz2
tar -C $DEVKITPRO/examples/gamecube  -jxf gamecube-examples-20100930.tar.bz2
 
mkdir -p $DEVKITPRO/portlibs/ppc
#tar -C $DEVKITPRO/portlibs/ppc -xjf zlib-1.2.4-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf mxml-2.6-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf libpng-1.4.1-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf jpeg8a-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf freetype-2.4.2-ppc.tar.bz2
 
 
############################################
 
 
cd $WIIMCSRC/libs/libntfs
make
make install
 
# remember: the powerpc-eabi commands must be in the path 
# so that 'configure' can find them)
 
for lib in fribidi libexif pcre libiconv ; do
  echo Building $lib.  Press RETURN to start
  read
  cd $WIIMCSRC/libs/$lib
  ./configure --host=powerpc-eabi --prefix="${DEVKITPRO}/portlibs/ppc" --libdir="${DEVKITPRO}/portlibs/ppc/lib" --disable-shared
  make
  make install
done
 
cd $WIIROOT/libext2fs-wii-read-only
make
make install
 
 
#install zlib 1.2.5 instead of 1.2.4 which is what we got from the devkitpro site
cd $WIIROOT/zlib
make
make install
cp $DEVKITPRO/libogc/include/{zconf,zlib}.h $DEVKITPRO/portlibs/ppc/include
cp $DEVKITPRO/libogc/lib/wii/libz.a $DEVKITPRO/portlibs/ppc/lib/libz.a 
#sed -i -e 's/1\.2\.4/1\.2\.5/' $DEVKITPRO/portlibs/ppc/lib/pkgconfig/zlib.pc
 
 
# fix missing include file in wiimc.cpp
 
===================================================================
--- source/wiimc.cpp    (revision 802)
+++ source/wiimc.cpp    (working copy)
@@ -14,6 +14,7 @@
 #include <dirent.h>
 #include <wiiuse/wpad.h>
 #include <di/di.h>
+#include <sys/iosupport.h>
 
 #include "utils/FreeTypeGX.h"
 #include "utils/gettext.h"
 
 
 
cd $WIIMCSRC
make

fredag 10 december 2010

GDB reverse debugging

GDB reverse debugging


Let's start with a simple C++ program in the file gdb-test.cpp:


#include <iostream>
 
const unsigned int SIZE = 10;
 
int main() {
 
  int b[SIZE];
 
  // intitialize
  for(int i=0; i <= SIZE; i++) {
    b[i]=0;
  }
 
  std::cout << "done!" << std::endl;
  return 0;
}

Let's comple and run it.


$ g++ -g gdb-test.cpp -o gdb-test
$ ./gdb-test

??? It doesn't stop!


Let's load it into gdb.


$ gdb ./gdb-test
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[...]
Reading symbols from /home/larsr/gdb-test...done.
(gdb)

Now run it:


(gdb) r
Starting program: /home/larsr/gdb-test

Still dosn't stop! Stop it with ctrl-c:


^C
Program received signal SIGINT, Interrupt.
0x08048704 in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb)

Let's look at the variable i.


(gdb) print i
$1 = 4
(gdb)

Looks normal enough! But strange that it didn't get further than 4... Well, let's run some more, break, and look at i.


(gdb) continue
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x080486fc in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$2 = 7
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x080486f8 in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$4 = 6
(gdb)

What? First i was 7 and then it was 6! How did that happen? Lets start to record, and run some more!


(gdb) record
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$6 = 2
(gdb)

Something has modified i to become smaller! When did this happen? Let's watch i and go backwards.


(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 2
New value = 1
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {

This makes sense. We are walking backswards, and in that direction we had a value of 2 which turned into 1. Let's keep going backwards.


(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 1
New value = 0
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 0
New value = 10
0x080486eb in main () at gdb-test.cpp:12
12      b[i]=0;
(gdb)

Here we see that as we went backwards, we went from 0 to 10. That means that when we ran forward, i went from 10 to 0, and it happened when we executed


b[i]=0;

Could it be that writing to b[10] overwrites the memory of i? Where are they located in memory?


(gdb) print &i
$7 = (int *) 0xbffff7bc
(gdb) print &b[i]
$8 = (int *) 0xbffff7bc
(gdb)

Yup, they are at the same address. Why? Doh! An array indexing error! b[10] is not within the array bounds! The last element is b[9].


Line 11 should use < instead of <= and read


for(int i=0; i <  SIZE; i++) {

Case closed.


An ugly observation is that programs like this bug out silently and "work" if you change the program to be initialized to 100 (or something bigger than SIZE).

Tic-tac-toe in python

Represent a board with a nine character string with spaces, "X", and "O". The rows of the board are concatenated to make the string, so the board


 X |   |   
---+---+---
   | O |   
---+---+---
   | X | O 


is the string "X   O  XO".

Given a board string, the function tic() returns the next board.  However, if the opponent can win (provided he makes the right moves), then tic() doesn't try to fight back, and may return bad moves.  A smarter program might continue to fight back as long as the game is not actually over.

fredag 26 november 2010

kontrollsiffra personnummer oneliner in python

def pnr(nr,d=2,c=0,i=0): return (i==9) and 10-(c%10) or pnr(str(nr),3-d,c+(d*int(nr[i]))%10+(d*int(nr[i]))/10,i+1)



def pnr(p):
    a,p=[0,2,4,6,8,1,3,5,7,9],[int(i) for i in p]
    return 10-(a[p[0]]+p[1]+a[p[2]]+p[3]+a[p[4]]+p[5]+a[p[6]]+p[7]+a[p[8]])%10



print "sista siffran = %d" % pnr("6408233234"[:-1])