Saturday, June 28, 2008

Customizing Asus AM200g - IV. jabberd configuration

The following is a sequence of trial and errors, one by one, covering the path from the default config file to a working one. It is intended to be a search engine trap for people trying to resolve configuration problems.




$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to r (192.168.1.1).
Escape character is '^]'.
BCM96348 ADSL Router
Login: admin
Password:
> sh
BusyBox v1.00 (2005.04.12-18:11+0000) Built-in shell (msh)
Enter 'help' for a list of built-in commands.
# /var/usb/usb_1/bin/jabberd
Not writing pidfile /var/usb/usb_1/var/run/jabberd/jabber.pid: Read-only file system

In my setup, the jabberd ecosystem resides on a readonly filesystem, so the very first thing to do is to copy the etc/jabber.xml file to the vfat partition and make sure the files that need to be dynamically created/modified are there as well.

# mkdir /var/usb/usb_2/jabberd
# cp /var/usb/usb_1/etc/jabber.xml /var/usb/usb_2/jabberd
# /var/usb/usb_1/bin/vim /var/usb/usb_2/jabberd/jabber.xml


If you're not familiar with vi, here's quick help:

To quit without saving type ':q' (colon q Enter)

To save changes and quit type ':wq' (colon w q Enter)

To move around use the arrow keys

To start writing at cursor press 'i'

To get out of the writing mode (so that you can move somewhere else) press 'Esc'

To delete character at cursor press 'x'




The changes I did at first were:


1.

To prevent the message


Not writing pidfile /var/usb/usb_1/var/run/jabberd/jabber.pid: Read-only file system

I changed /var/usb/usb_1/var/run/jabberd/jabber.pid to /var/usb/usb_2/jabberd/jabber.pid.

2.


Next I launched jabberd in debug mode (-D) and pointed it to the alternate config file (-c path/to/file):


# /var/usb/usb_1/bin/jabberd -D -c /var/usb/usb_2/jabberd/jabber.xml
Mon Jan  3 20:31:13 2000  mio_tls.cc:921 MIO TLS init (GNU TLS)
Mon Jan  3 20:31:14 2000  log.cc:221 <log xmlns='jabber:server' type='warn' from='-internal'>
Cannot open /var/usb/usb_1/etc/dhparams.pem for reading dhparams: No such file or directory</log>
...

The comments in the file say:
<!-- With the <dhparams/> element right inside the <tls/> element,  -->
<!-- you can configure a file containing parameters for Diffie      -->
<!-- Hellmann key exchanges. If this configuration setting is not   -->
<!-- present, jabberd14 will generated these parameters             -->
<!-- automatically on each startup. This takes some time, therefore -->
<!-- you get a faster startup, if this setting is present.          -->

So I commented out <dhparams type='pem'>/var/usb/usb_1/etc/dhparams.pem</dhparams> as I don't know how to make it properly and don't mind waiting a few seconds more at startup. To start a comment, you write "<!--", you close it with "-->".

3.

# /var/usb/usb_1/bin/jabberd -D -c /var/usb/usb_2/jabberd/jabber.xml
...
Mon Jan  3 20:47:02 2000  xdb_sql.cc:911 using the following query on SQL connection establishment: SET NAMES utf8
Mon Jan  3 20:47:02 2000  log.cc:221 <log xmlns='jabber:server' type='alert' from='xdbsql.localhost'>Your xdb_sql is compiled without support
for the selected database driver 'mysql'.</log>
Mon Jan  3 20:47:02 2000  xdb_sql.cc:813 processing handler definition: <handler xmlns='jabber:config:xdb_sql' ns='jabber:iq:last'>
...
Mon Jan  3 20:47:03 2000  deliver.cc:837 delivering to instance 'elogger.localhost'
Mon Jan  3 20:47:03 2000  deliver.cc:614 DELIVER 3:xdbsql.localhost <log xmlns='jabber:server' type='alert' from='xdbsql.localhost'>Your
xdb_sql is compiled without support for the selected database driver 'mysql'.</log>


Yes, this is true, the daemon was compiled without any sql support. I've followed the steps in README.filespool to setup jabber to store data in files - I definitely don't want to run sql server on the machine. Of course I replaced the <xdb_file>$PREFIX/lib/libjabberdxdbfile.so</xdb_file> with <xdb_file>/var/usb/usb_1/lib/libjabberdxdbfile.so</xdb_file>, and <spool><jabberd:cmdline flag='s'>$PREFIX/var/spool/jabberd</jabberd:cmdline></spool> with <spool><jabberd:cmdline flag='s'>/var/usb/usb_2/jabberd/jabberdspool</jabberd:cmdline></spool> - then made sure the directory exists:
# mkdir /var/usb/usb_2/jabberd/jabberdspool



4.


# /var/usb/usb_1/bin/jabberd -D -c /var/usb/usb_2/jabberd/jabber.xml
...
Mon Jan  3 21:17:58 2000  log.cc:221 <log xmlns='jabber:server' type='alert' from='inject.localhost'>
could not open directory /var/usb/usb_1/var/spool/jabberd/inject.localhost for reading</log>
...

inject.localhost is a directory, where some other process can put messages. jabberd then automatically sends them. To make it working I changed
<in>/var/usb/usb_1/var/spool/jabberd/inject.localhost</in> to <in>/var/usb/usb_2/jabberd/inject.localhost</in> and created the directory with
# mkdir /var/usb/usb_2/jabberd/inject

5.
# /var/usb/usb_1/bin/jabberd -D -c /var/usb/usb_2/jabberd/jabber.xml
...
Mon Jan  3 21:33:15 2000  jabberd.cc:336 main load check of 1.00 with 4 total threads
Mon Jan  3 21:33:16 2000  users.cc:209 0        total users
Mon Jan  3 21:33:17 2000  dialback.cc:1207 dialback idle check

hmm, seems to be working ..., time to launch kopete and try registering a user hajma@192.168.1.1. It failed, of course. The message I got from Kopete was definitely not helpful: "There was an error in the protocol stream: There was a conflict in the information received." and "Unable to create account on the server. The Jabber ID is probably already in use.". What a nonsense ... a brief look into the konsole shows the infamous "bouncing a routed packet" message:
Mon Jan  3 21:35:40 2000  deliver.cc:614 DELIVER 4:192.168.1.1 <route xmlns='jabber:server' type='auth'
to='hajma@192.168.1.1' from='14@c2s/100E0278'><iq type='set' to='192.168.1.1' id='aab0a'>
<query xmlns='jabber:iq:register'>
<username>hajma</username>
<password>qwertyuiop</password>
</query>
</iq></route>
Mon Jan  3 21:35:40 2000  deliver.cc:837 delivering to instance 'dnsrv.localhost'
Mon Jan  3 21:35:40 2000  deliver.cc:766 delivery failed (Internal Delivery Error)
Mon Jan  3 21:35:40 2000  log.cc:221 <log xmlns='jabber:server' type='notice' from='192.168.1.1'>bouncing a routed
packet to hajma@192.168.1.1 from 14@c2s/100E0278: Internal Delivery Error</log>

In short, to make jabberd working correctly, you have to replace all occurences of 'localhost' by the domain or IP jabberd is listening at. Now I replaced localhost with 192.168.1.1.

6.

# /var/usb/usb_1/bin/jabberd -D -c /var/usb/usb_2/jabberd/jabber.xml
...

Registering with Kopete again ... This time I only got one error: "Unable to create account on the server. The Jabber ID is probably already in use.". And again it was completely unrelated - in the konsole I found this:
Mon Jan  3 21:50:02 2000  mod_register.cc:278 we got a reply for: username
Mon Jan  3 21:50:02 2000  mod_register.cc:278 we got a reply for: password
Mon Jan  3 21:50:03 2000  mod_register.cc:298 returned err msg: Missing data field: name
...
<error code='400' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Missing data field: name</text></error></iq></route>

The stupid thing Kopete completely ignores XEP-0077, does not ask the server what is needed for registration and just sends username and password. To support Kopete, I had to delete
<name/> and <email/> from the <register xmlns="jabber:iq:register" notify="yes"> section.


After this change I am able to register a new user and send messages.
Just to be perfect I also changed /var/usb/usb_1/var/log/jabberd/error.log to /var/usb/usb_2/jabberd/error.log.
Off to the next level!

7.


To have it available from outside world, I replaced 192.168.1.1 with my_domain.cz.
To open a port in the AM-200g router, issue the following command:

iptables -I INPUT -p tcp -i ppp_8_48_1 --dport 5222 -m state --state NEW -j ACCEPT



8.

The main reason for running own jabber server is more privacy and that is impossible without encryption - let's do it.
To be able to use TLS I had to generate a self-signed certificate. I've used the procedure described at http://www.vanemery.com/Linux/Jabber/jabberd.html:

$ openssl req -new -x509 -newkey rsa:1024 -days 3650 -keyout privkey.pem -out key.pem

Generating a 1024 bit RSA private key

...........++++++

.........++++++

writing new private key to 'privkey.pem'

Enter PEM pass phrase:

Verifying - Enter PEM pass phrase:

Tuesday, June 17, 2008

Customizing Asus AM200g - III. jabberd and vim


It took me some time to actually make the jabber server working, few people seem to really care about cross-compiling their stuff, but I succeeded and here's the howto:




0. Errata


First of all, during the testing I found out I have to modify the environment settings - the static build of jabber did not work at all. I also added the location of newly compiled libs and headers so that they can be used by programs that depend on them. Some programs were in C++, so the CXX variable had to be set as well. Here's the final environment (P.S. in the end it seems some additions like CPPFLAGS would be needed for perfection, but I'm not in the mood of retrying):


$ cat environment
TOOLCHAINS_HOME=/opt/toolchains/uclibc-crosstools
export LIBS="-L${TOOLCHAINS_HOME}/lib/gcc/mips-linux/3.4.2 \
-L/var/usb/usb_1/lib \
"
export INCLUDES="-I${TOOLCHAINS_HOME}/include/c++/3.4.2 \
-I${TOOLCHAINS_HOME}/mips-linux/sys-include \
-I/var/usb/usb_1/include \
-I/home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/kernel/linux/include \
-I/var/usb/usb_1/include/ncurses \
"
#export CFLAGS="${INCLUDES} ${LIBS} -static"
#export LDFLAGS="-static"
export CFLAGS="${INCLUDES} ${LIBS}"
export LDFLAGS=""
export CC="${TOOLCHAINS_HOME}/bin/mipsel-uclibc-gcc"
export CXX="${TOOLCHAINS_HOME}/bin/mipsel-uclibc-g++"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${TOOLCHAINS_HOME}/lib
export PATH="${TOOLCHAINS_HOME}/bin:${PATH}"

Let's use it:

. environment



Also the symlinking of the uclibc toolchain needed to be further enhanced for some package to get built:

# cd /opt/toolchains/uclibc-crosstools/bin
# for A in mips-linux-uclibc-* ; do
> ln -s ${A} mipsel-linux-${A#mips-linux-uclibc-}
> done

1a. jabberd

$ cd GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource
$ wget http://download.jabberd.org/jabberd14/jabberd14-1.6.1.1.tar.gz
$ tar xvf jabberd14-1.6.1.1.tar.gz
$ cd jabberd14-1.6.1.1
$./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1

This ends badly:
...
checking for libpopt... configure: error: no


Apparently the library for parsing command line parameters is needed. This was reported a year ago ...


2. popt

$ cd GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource
$ wget http://freshmeat.net/redir/popt/72854/url_tgz/popt-1.14.tar.gz
$ tar xvf popt-1.14.tar.gz
$ cd popt-1.14
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
...
checking for va_copy() function... configure: error: cannot run test program while cross compiling
See `config.log' for more details.

WTF! There's bug 25 about it that the developers closed, saying "va_copy is no longer needed or used in popt-1.14.". Hell, when it's not needed, why did you keep the check ... :-(

I added a comment to the bug and sucessfully run:
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 ac_cv_va_copy=no
$ make
$ make install

1b. jabberd

Retry:
$ cd ../jabberd14-1.6.1.1
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
...
checking for libpopt... configure: error: no

The previous step did not help much ... The configure script does not search for the popt.h in the provided directories - again something reported a year ago. It forced me to rise bug 93.

Retry:

$ CPPFLAGS="-I/var/usb/usb_1/include" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
...
checking for GNUTLS... no
No package 'gnutls' found
configure: error: Couldn't find required GnuTLS installation

Fine, let's get GnuTLS working first. Furthermore, ./configure --help reveals that some more libraries are needed by jabberd:
--with-libidn=DIR       Where to find libidn (required)
--with-libpth=DIR       Where to find libpth (required)
--with-expat=DIR        Where to find libexpat (required)


In addition GnuTLS requires some other libraries, libtasn1 and libgcrypt, so let's get them all first.


3. GNU Portable Threads
$ cd ..
$ wget ftp://ftp.gnu.org/gnu/pth/pth-2.0.7.tar.gz
$ tar xvf pth-2.0.7.tar.gz
$ cd pth-2.0.7
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

4. Expat XML Parser
$ cd ..
$ wget http://downloads.sourceforge.net/expat/expat-2.0.1.tar.gz?modtime=1181083143&big_mirror=0
$ tar xvf expat-2.0.1.tar.gz
$ cd expat-2.0.1
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

5. GNU IDN Library - Libidn
$ cd ..
$ wget ftp://alpha.gnu.org/pub/gnu/libidn/libidn-1.8.tar.gz
$ tar xvf libidn-1.8.tar.gz
$ cd libidn-1.8
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

6. Tiny ASN.1 library - Libtasn1
$ cd ..
$ wget http://josefsson.org/gnutls/releases/libtasn1/libtasn1-1.4.tar.gz
$ tar xvf libtasn1-1.4.tar.gz
$ cd libtasn1-1.4
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

To save me from
checking for gpg-error-config... no
checking for GPG Error - version >= 1.4... no
configure: error: libgpg-error is needed.
See ftp://ftp.gnupg.org/gcrypt/libgpg-error/ .


when configuring libgcrypt, libgpg-error has to be done in advance.


7. libgpg-error
$ cd ..
$ wget ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-1.6.tar.bz2
$ tar xvf libgpg-error-1.6.tar.bz2
$ cd libgpg-error-1.6
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

8. libgcrypt
$ cd ..
$ wget ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.4.1.tar.gz
$ tar xvf libgcrypt-1.4.1.tar.gz
$ cd libgcrypt-1.4.1

The path to libggpg-error has to be specified explicitly:
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-gpg-error-prefix=/var/usb/usb_1 --with-pth-prefix=/var/usb/usb_1

But it still does not help!:
$ make
...
/opt/toolchains/uclibc-crosstools/bin/mipsel-uclibc-gcc -I/var/usb/usb_1/include -I/opt/toolchains/uclibc-crosstools/include/c++/3.4.2 -I/opt/toolchains/uclibc-crosstools/mips-linux/sys-include -I/var/usb/usb_1/include -I/home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/kernel/linux/include -I/var/usb/usb_1/include/ncurses -Wall -Wpointer-arith -o .libs/version version.o  -L/opt/toolchains/uclibc-crosstools/lib/gcc/mips-linux/3.4.2 -L/var/usb/usb_1/lib ../src/.libs/libgcrypt.so -Wl,--rpath -Wl,/var/usb/usb_1/lib
/opt/toolchains/uclibc-crosstools_gcc-3.4.2_uclibc-20050502/bin/../lib/gcc/mips-linux-uclibc/3.4.2/../../../../mips-linux-uclibc/bin/ld: warning: libgpg-error.so.0, needed by ../src/.libs/libgcrypt.so, not found (try using -rpath or -rpath-link)
../src/.libs/libgcrypt.so: undefined reference to `gpg_err_code_from_syserror'
../src/.libs/libgcrypt.so: undefined reference to `gpg_err_code_from_errno'
../src/.libs/libgcrypt.so: undefined reference to `gpg_strerror'
../src/.libs/libgcrypt.so: undefined reference to `gpg_strsource'
collect2: ld returned 1 exit status
make[2]: *** [version] Error 1
make[2]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/libgcrypt-1.4.1/tests'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/libgcrypt-1.4.1'
make: *** [all] Error 2
$

I found out the guys at OpenWRT had the same problem and have a patch available.
I wanted to file a bug to the GnuPG bug tracker, but did not receive the confirmation mail when registering...
Actually this was told to the author two years ago and he does not care at all.

So here goes the needed modification:
$ diff -u tests/Makefile.in.old tests/Makefile.in
--- tests/Makefile.in.old       2008-06-12 00:01:05.000000000 +0200
+++ tests/Makefile.in   2008-06-12 00:01:11.000000000 +0200
@@ -313,7 +313,7 @@
# a built header.
AM_CPPFLAGS = -I../src -I$(top_srcdir)/src
AM_CFLAGS = $(GPG_ERROR_CFLAGS)
-LDADD = ../src/libgcrypt.la
+LDADD = ../src/libgcrypt.la @LTLIBOBJS@ @GPG_ERROR_LIBS@
# pkbench uses mmap for no good reason.  Needs to be fixed.  Code for
# this can be found in libksba/tests.

and try again:
$ make clean
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-gpg-error-prefix=/var/usb/usb_1 --with-pth-prefix=/var/usb/usb_1
$ make
$ make install

9. The GNU Transport Layer Security Library
$ cd ..
$ wget ftp://ftp.gnutls.org/pub/gnutls/gnutls-2.2.5.tar.bz2
$ tar xvf gnutls-2.2.5.tar.bz2
$ cd gnutls-2.2.5

To prevent a configure error
checking for libgcrypt-config... no
checking for LIBGCRYPT - version >= 1.2.4... no
configure: error:
***
*** libgcrypt was not found. You may want to get it from
*** ftp://ftp.gnupg.org/gcrypt/libgcrypt/
***

the prefix to libgcrypt has to be specified (and libtasn1 too, to avoid using the built-in one):
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libgcrypt-prefix=/var/usb/usb_1 --with-libtasn1-prefix=/var/usb/usb_1

but it still does not work:
$ make
...
/opt/toolchains/uclibc-crosstools/bin/mipsel-uclibc-gcc -std=gnu99 -I/opt/toolchains/uclibc-crosstools/include/c++/3.4.2 -I/opt/toolchains/uclibc-crosstools/mips-linux/sys-include -I/var/usb/usb_1/include -I/home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/kernel/linux/include -I/var/usb/usb_1/include/ncurses -D_REENTRANT -D_THREAD_SAFE -pipe -I/var/usb/usb_1/include -I/opt/toolchains/uclibc-crosstools/include/c++/3.4.2 -I/opt/toolchains/uclibc-crosstools/mips-linux/sys-include -I/var/usb/usb_1/include -I/home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/kernel/linux/include -I/var/usb/usb_1/include/ncurses -D_REENTRANT -D_THREAD_SAFE -o ex-cert-select ex-cert-select.o  -L/opt/toolchains/uclibc-crosstools/lib/gcc/mips-linux/3.4.2 -L/var/usb/usb_1/lib ./.libs/libexamples.a ../../gl/.libs/libgnu.a /var/usb/usb_1/lib/libgcrypt.so /var/usb/usb_1/lib/libgpg-error.so ../../lib/.libs/libgnutls.so ../../libextra/.libs/libgnutls-extra.so  -Wl,--rpath -Wl,/var/usb/usb_1/lib -Wl,--rpath -Wl,/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5/lib/.libs -Wl,--rpath -Wl,/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5/libextra/.libs -Wl,--rpath -Wl,/var/usb/usb_1/lib
/opt/toolchains/uclibc-crosstools_gcc-3.4.2_uclibc-20050502/bin/../lib/gcc/mips-linux-uclibc/3.4.2/../../../../mips-linux-uclibc/bin/ld: warning: libtasn1.so.3, needed by ../../lib/.libs/libgnutls.so, not found (try using -rpath or -rpath-link)
../../lib/.libs/libgnutls.so: undefined reference to `asn1_write_value@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_delete_structure@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_get_length_der@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_get_tag_der@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_der_decoding_startEnd@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_der_decoding@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_create_element@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_copy_node@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_read_value@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_number_of_elements@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_array2tree@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_check_version@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_find_node@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_find_structure_from_oid@LIBTASN1_0_3'
../../lib/.libs/libgnutls.so: undefined reference to `asn1_der_coding@LIBTASN1_0_3'
collect2: ld returned 1 exit status
make[4]: *** [ex-cert-select] Error 1
make[4]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5/doc/examples'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5/doc'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5/doc'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/gnutls-2.2.5'
make: *** [all] Error 2

After some experimentation (I told you I am no programmer) I found the following to work:
$ LIBS="-ltasn1 $LIBS" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libgcrypt-prefix=/var/usb/usb_1 --with-libtasn1-prefix=/var/usb/usb_1
$ make
$ make install


Also I found that the two official mirrors have both different directory structure, so I was missing the devel branch of GnuTLS. The above works with http://www.gnu.org/software/gnutls/releases/gnutls-2.3.14.tar.bz2 (and breaks with 2.2.5).


1c. jabberd

Let's not try to find more bugs and specify the needed components to the configure script now:
$ cd ../jabberd14-1.6.1.1
$ CPPFLAGS="-I/var/usb/usb_1/include" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libidn=/var/usb/usb_1 --with-libpth=/var/usb/usb_1 --with-expat=/var/usb/usb_1

but still bad luck:
checking for GnuTLS... checking for mipsel-linux-pkg-config... no
checking for pkg-config... /usr/bin/pkg-config
configure: WARNING: In the future, Autoconf will not detect cross-tools
whose name does not start with the host triplet.  If you think this
configuration is useful to you, please write to autoconf@gnu.org.
checking pkg-config is at least version 0.9.0... yes
checking for GNUTLS... no
No package 'gnutls' found
configure: error: Couldn't find required GnuTLS installation

And again, reported a year ago.
We can fix that, but the show is still not at the end:
$ PKG_CONFIG_PATH=/var/usb/usb_1/lib/pkgconfig CPPFLAGS="-I/var/usb/usb_1/include" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libidn=/var/usb/usb_1 --with-libpth=/var/usb/usb_1 --with-expat=/var/usb/usb_1
...
checking for GnuTLS... checking for mipsel-linux-pkg-config... no
checking for pkg-config... /usr/bin/pkg-config
configure: WARNING: In the future, Autoconf will not detect cross-tools
whose name does not start with the host triplet.  If you think this
configuration is useful to you, please write to autoconf@gnu.org.
checking pkg-config is at least version 0.9.0... yes
checking for GNUTLS... yes
checking for TLS 1.2 support in GnuTLS... yes
checking for GNUTLSEXTRA... yes
checking idna.h usability... yes
checking idna.h presence... yes
checking for idna.h... yes
checking for stringprep_check_version in -lidn... no
configure: error: Couldn't find required libidn installation

Quick look into config.log reveals something familiar:
/opt/toolchains/uclibc-crosstools_gcc-3.4.2_uclibc-20050502/bin/../lib/gcc/mips-linux-uclibc/3.4.2/../../../../mips-linux-uclibc/bin/ld: warning: libtasn1.so.3, needed by /var/usb/usb_1/lib/libgnutls-extra.so, not found (try using -rpath or -rpath-link)
/var/usb/usb_1/lib/libgnutls.so: undefined reference to `asn1_write_value@LIBTASN1_0_3'
/var/usb/usb_1/lib/libgnutls.so: undefined reference to `gcry_sexp_release@GCRYPT_1.2'
...

Let's see if the GnuTLS fix works here as well:
LIBS="-ltasn1 $LIBS" PKG_CONFIG_PATH=/var/usb/usb_1/lib/pkgconfig CPPFLAGS="-I/var/usb/usb_1/include" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libidn=/var/usb/usb_1 --with-libpth=/var/usb/usb_1 --with-expat=/var/usb/usb_1
...
checking for stringprep_check_version in -lidn... no

No, it doesn't :-(, but config.log reveals we got one step further:
...
/opt/toolchains/uclibc-crosstools/bin/mipsel-uclibc-g++ -o conftest -g -O2 -I/va
r/usb/usb_1/include -I/var/usb/usb_1/include -I/var/usb/usb_1/include -I/var/usb
/usb_1/include conftest.cpp  -L/var/usb/usb_1/lib /var/usb/usb_1/lib/libgnutls-e
xtra.so /var/usb/usb_1/lib/libgnutls.so /var/usb/usb_1/lib/libidn.so -lresolv /v
ar/usb/usb_1/lib/libtasn1.so -L/opt/toolchains/uclibc-crosstools/lib/gcc/mips-li
nux/3.4.2 -ldl -Wl,--rpath -Wl,/var/usb/usb_1/lib -Wl,--rpath -Wl,/var/usb/usb_1
/lib
/opt/toolchains/uclibc-crosstools_gcc-3.4.2_uclibc-20050502/bin/../lib/gcc/mips-
linux-uclibc/3.4.2/../../../../mips-linux-uclibc/bin/ld: warning: libgcrypt.so.1
1, needed by /var/usb/usb_1/lib/libgnutls-extra.so, not found (try using -rpath
or -rpath-link)
...

Finally, this works:
LIBS="-ltasn1 -lgpg-error -lgcrypt $LIBS" PKG_CONFIG_PATH=/var/usb/usb_1/lib/pkgconfig CPPFLAGS="-I/var/usb/usb_1/include" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-libidn=/var/usb/usb_1 --with-libpth=/var/usb/usb_1 --with-expat=/var/usb/usb_1

but make fails:
$ make
...
/var/usb/usb_1/include/gnutls/openpgp.h: In function `void mio_tls_process_credentials(xmlnode_t*, const std::list<std::string, std::allocator<std::string> >&, const std::list<std::string, std::allocator<std::string> >&, gnutls_dh_params_int*, const std::list<std::string, std::allocator<std::string> >&, const std::list<std::string, std::allocator<std::string> >&)':
/var/usb/usb_1/include/gnutls/openpgp.h:247: error: too few arguments to function `int gnutls_certificate_set_openpgp_key_file(gnutls_certificate_credentials_st*, const char*, const char*, gnutls_openpgp_crt_fmt_t)'
mio_tls.cc:615: error: at this point in file
/var/usb/usb_1/include/gnutls/openpgp.h:271: error: too few arguments to function `int gnutls_certificate_set_openpgp_keyring_file(gnutls_certificate_credentials_st*, const char*, gnutls_openpgp_crt_fmt_t)'
mio_tls.cc:634: error: at this point in file
mio_tls.cc:653: error: `gnutls_certificate_set_openpgp_trustdb' undeclared (first use this function)
mio_tls.cc:653: error: (Each undeclared identifier is reported only once for each function it appears in.)
make[3]: *** [mio_tls.lo] Error 1
make[3]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/jabberd14-1.6.1.1/jabberd'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/jabberd14-1.6.1.1/jabberd'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/jabberd14-1.6.1.1'
make: *** [all] Error 2

This turned out to be easy, the Gentoo folks found and patched it couple of months ago (and it looks it is in the jabberd cvs now) and after applying it, the journey ends:
$ make
$ make install


Wow, congratulations!!


Let's get an editor so that we can modify the config files in place:


10a. vim
$ cd ..
$ wget ftp://ftp.vim.org/pub/vim/unix/vim-7.1.tar.bz2
$ tar xvf vim-7.1.tar.bz2
$ cd vim71
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-features=tiny
...
checking for strip... strip
configure: error: cannot compile a simple program, check CC and CFLAGS
(cross compiling doesn't work)

Would you expect an opensource app as big and widespread as Vim to be so broken? I would now. Fortunately the OpenWRT and FreeWRT people come to help again.
$ wget --no-check-certificate https://dev.openwrt.org/browser/packages/utils/vim/patches/001-cross_compile.patch?format=txt
$ patch -p1 <001-cross_compile.patch\?format\=txt
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-features=tiny
...
checking --with-tlib argument... empty: automatic terminal library selection
checking for tgetent in -lncurses... no
checking for tgetent in -ltermlib... no
checking for tgetent in -ltermcap... no
checking for tgetent in -lcurses... no
no terminal library found
checking for tgetent()... configure: error: NOT FOUND!
You need to install a terminal library; for example ncurses.
Or specify the name of the library with --with-tlib.

Seems some terminal library is needed ...
11. The Ncurses (new curses) library
$ cd ..
$ wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz
$ tar xvf ncurses-5.6.tar.gz
$ cd ncurses-5.6
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1
$ make
$ make install

10b. vim
$ cd ../vim71
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-features=tiny
...
checking for tgetent in -lncurses... yes
ncurses library is not usable
checking for tgetent in -ltermlib... no
checking for tgetent in -ltermcap... no
checking for tgetent in -lcurses... no
no terminal library found
checking for tgetent()... configure: error: NOT FOUND!
You need to install a terminal library; for example ncurses.
Or specify the name of the library with --with-tlib.

seems we have to force it:
$ ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-features=tiny --with-tlib=ncurses
$ make
...
Starting make in the src directory.
If there are problems, cd to the src directory and run make there
cd src && make first
make[1]: Entering directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/vim71/src'
CC="/opt/toolchains/uclibc-crosstools/bin/mipsel-uclibc-gcc -Iproto -DHAVE_CONFIG_H   -I/var/usb/usb_1        " srcdir=. sh ./osdef.sh
osdef0.c:10:44: termcap.h: No such file or directory
...
In file included from term.c:33:
/var/usb/usb_1/include/ncurses/termcap.h:64: error: conflicting types for 'tgoto'
...

hmm, I guess I really need to use the CPPFLAGS, shouldn't I modify the environment file? Perhaps, but now it's too late, it's finished:
$ make distclean
$ CPPFLAGS="-I/var/usb/usb_1/include/ncurses" ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1 --with-features=tiny --with-tlib=ncurses --disable-gui --disable-gtktest --disable-xim --without-x --disable-multibyte --disable-netbeans --disable-cscope --disable-gpm
$ make
$ make install
...
strip /var/usb/usb_1/bin/vim
strip: Unable to recognise the format of the input file `/var/usb/usb_1/bin/vim'
make[1]: *** [installvimbin] Error 1
make[1]: Leaving directory `/home/hajma/Dokumenty/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/vim71/src'
make: *** [install] Error 2

No, it's not finished... Let's just hack it:
$ diff -u src/Makefile.old src/Makefile
--- src/Makefile.old    2008-06-17 02:16:32.000000000 +0200
+++ src/Makefile        2008-06-17 02:16:47.000000000 +0200
@@ -999,7 +999,7 @@
INSTALL_DATA_R = cp -r
### Program to run on installed binary
-#STRIP = strip
+STRIP = echo
### Permissions for binaries  {{{1
BINMOD = 755
$ make install


I was quite surprised with the size of some files. Performing

/opt/toolchains/uclibc-crosstools/bin/mipsel-linux-sstrip
on bin/ and lib/ helped a bit, but 1MB of vim suggests there's still place for optimizations.



Final creation and writing of the squashfs image:


# /home/hajma/Dokumenty/asus/squashfs2.0r2/squashfs-tools/mksquashfs /var/usb/usb_1 /tmp/new.sqsh -be -noI -noD -noF
# dd if=/tmp/new.sqsh of=/dev/sda1

In the next part I'll try to correctly configure jabberd and make it start at router boot.



Customizing Asus AM200g - V. firmware modification

Customizing Asus AM200g - IV. jabberd configuration

Customizing Asus AM200g - II. Preparing the environment

Customizing Asus AM200g - I. PREPARATION

Monday, June 2, 2008

Customizing Asus AM200g - II. Preparing the environment

I know I promised you a jabber server, but in the end it was not so easy and the entry would be perhaps too long, so let's start with simple stuff :-)


Note I am not a programmer, have no C skills, so there can be a glitch here and there, but in the end all steps below led me to something that worked. If you find something really nasty, please let me know.

I took most of the inspiration at Daniel Fišer's page on Asus WL-600g hacking and just tweaked it to suit my setup.

1. On a fresh Mandriva 2008.1 installation some of the necessary tools are missing. Install them as root:

# urpmi make gcc autoconf automake


2. Since Asus still did not release the sources for AM200g, I used the GPL setup provided for AM604g, which uses the same chipset and seems to be almost identical (it even comes in the same box, except for missing USB port and second antenna). Follow these steps to install it:

$ wget http://dlsvr02.asus.com/pub/ASUS/Broadband/xDSL/GPL_AM604G_ForRussia.zip
$ unzip GPL_AM604G_ForRussia.zip
$ cd GPL_AM604G_ForRussia
$ tar xvf GENERIC_6348_WLAN_A34_3-06-02-01_GPL.tar.gz
$ su
# ./consumer_install

3. Not surprisingly, the AM604g source is just as broken as the AM600g one, so the same steps as described by Daniel need to be done:

# cd /opt/toolchains/uclibc-crosstools/bin
# for A in mips-linux-uclibc-* ; do
> ln -s ${A} mipsel-uclibc-${A#mips-linux-uclibc-}
> done
# cd /home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01
# wget http://daniel.fiser.cz/IT/asus_WL-600g/cur_menu.patch
# patch -p0 <cur_menu.patch


4. Now you can try to build the firmware, just for fun:

# make PROFILE=GENERIC_6348_WLAN_A34

The firmware is created in the images directory. Since I have no replacement for the router, I'm not going to try it, although it might actually work.

5. To prepare the environment, create a file containing:

$ cat environment
TOOLCHAINS_HOME=/opt/toolchains/uclibc-crosstools
export LIBS="-L${TOOLCHAINS_HOME}/lib/gcc/mips-linux/3.4.2"
export INCLUDES="-I${TOOLCHAINS_HOME}/include/c++/3.4.2 \
-I${TOOLCHAINS_HOME}/mips-linux/sys-include \
-I/home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/kernel/linux/include"
export CFLAGS="${INCLUDES} ${LIBS} -static"
export LDFLAGS="-static"
export CC="${TOOLCHAINS_HOME}/bin/mipsel-uclibc-gcc"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${TOOLCHAINS_HOME}/lib
export PATH="${PATH}:${TOOLCHAINS_HOME}/bin"

and set the environment before doing anything else:
# . ./environment

(yes, this is "dot space dot ...")

6. Let's proceed to the helloworld example. Create it's own folder:

mkdir /home/hajma/Documents/GPL_AM604G_ForRussia/GENERIC_6348_WLAN_A34_3-06-02-01/userapps/opensource/helloworld

and follow the steps described in the automake manual:

Create the following files in an empty directory.

  • src/main.c is the source file for the hello program. We
    store it in the src/ subdirectory, because later, when the package
    evolves, it will ease the addition of a man/ directory for man
    pages, a data/ directory for data files, etc.
    ~/amhello % cat src/main.c
    #include <config.h>
    #include <stdio.h>
    int
    main (void)
    {
    puts ("Hello World!");
    puts ("This is " PACKAGE_STRING ".");
    return 0;
    }
    

  • README contains some very limited documentation for our little
    package.
    ~/amhello % cat README
    This is a demonstration package for GNU Automake.
    Type `info Automake' to read the Automake manual.
    

  • Makefile.am and src/Makefile.am contain Automake
    instructions for these two directories.
    ~/amhello % cat src/Makefile.am
    bin_PROGRAMS = hello
    hello_SOURCES = main.c
    ~/amhello % cat Makefile.am
    SUBDIRS = src
    dist_doc_DATA = README
    

  • Finally, configure.ac contains Autoconf instructions to
    create the configure script.
    ~/amhello % cat configure.ac
    AC_INIT([amhello], [1.0], [bug-automake@gnu.org])
    AM_INIT_AUTOMAKE([-Wall -Werror foreign])
    AC_PROG_CC
    AC_CONFIG_HEADERS([config.h])
    AC_CONFIG_FILES([
    Makefile
    src/Makefile
    ])
    AC_OUTPUT
    

Once you have these five files, it is time to run the Autotools to instantiate the build system. Do this using the autoreconf command as follows:
~/amhello % autoreconf --install
configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
src/Makefile.am: installing `./depcomp'
At this point the build system is complete.
In addition to the three scripts mentioned in its output, you can see that autoreconf created four other files: configure, config.h.in, Makefile.in, and src/Makefile.in. The latter three files are templates that will be adapted to the system by configure under the names config.h, Makefile, and src/Makefile. Let's do this:
In addition to the manual's instructions, the details of host architecture must be provided when compiling for another platform. Also I set the prefix as the flash drive gets mounted as /var/usb/usb_1:
# ./configure --build=i686-pc-linux-gnu --host=mipsel-linux --target=mipsel-linux --prefix=/var/usb/usb_1/hello
# make
# make install
7. Optionally you can strip (remove all unnecessary bits) the binary:
# cd /var/usb/usb_1/hello/bin
# /opt/toolchains/uclibc-crosstools/bin/mipsel-uclibc-strip -o hellostripped hello
8. Copy the files to the flash:
# /home/hajma/Documents/squashfs2.0r2/squashfs-tools/mksquashfs /var/usb/usb_1 /tmp/dir.sqsh -be -noI -noD -noF
# dd if=/tmp/dir.sqsh of=/dev/sdb1
9. Plug the flash drive to the router and verify all runs fine:
$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1 (192.168.1.1).
Escape character is '^]'.
BCM96348 ADSL Router
Login: admin
Password:
> sh
BusyBox v1.00 (2005.04.12-18:11+0000) Built-in shell (msh)
Enter 'help' for a list of built-in commands.
# cd /var/usb/usb_1/hello/bin
# ./hello
Hello World!
This is amhello 1.0.
# ./hellostripped
Hello World!
This is amhello 1.0.
Final note: if you get "hello: applet not found" instead, it means you somehow did not compile the program for correct platform and BusyBox was not able to execute it.
Customizing Asus AM200g - V. firmware modification Customizing Asus AM200g - IV. jabberd configuration Customizing Asus AM200g - III. jabberd and vim Customizing Asus AM200g - I. PREPARATION