Control Centre

From Egbert's Wiki

Embedded Linux Control Centre

Introduction

This is a project from a book (Elektor ISBN 978-1-907920-02-8) by Hans Henrik Skovgaard. The book focusses on the re-use of old(er) hardware and uses Damn Small Linux (DSL) with Fluxbox as GUI. I do use older HW too (Wyse Terminal 9455, VIA C3-550MHz Samuel2, 512 Mb RAM, 80 Gb disk) but I will stick to Ubuntu, eventually with Gnome/gdm or Fluxbox/xdm.

I started with installation of 10.04 LTS server edition to get a clean CLI only system. Why not 10.10 or newer? As of Ubuntu 10.10 support for the VIA C3 has been dropped from the release. The C3 omits just 2 686 instructions...

At first I installed the SSHserver and the Ubuntu LAMP package during the tasksel phase when installing from CD. Mainly to be able to follow the automatic security updates but later I decided that the XAMPP tarball would be much more convenient since all paths mentioned in the book would be correct then. Also the guys behind XAMPP seem to know what they are doing. When needed, they promise to release an upgrade tarball that can be installed without messing up the configuration.

So, to have a clean start I installed a base system from CD again. Not a single packages was selected during the tasksel phase this time. Well... I planned to do just the SSHserver but I hit the <return> key before selecting the package.

Essential packages

Extra installed packages (all using aptitude install <package>):

  • openSSL-server (Secure connect to this server)
  • build-essential (GCC the C compiler).
  • libmysqlclient-dev, MySQL database development files.
  • ssmtp, extremely simple MTA to get mail off the system to a mail hub.

Only needed when USB connected sensors aka the 'thermo snake' are used.

  • libusb-dev, userspace USB programming library development files.
  • libhid-dev, userspace USB HID development files.

Needed when XAMPP is NOT used but Ubuntu LAMP:

  • LAMP virtual package (apache2, MySQL, php5, perl).
  • phpmyadmin, MySQL web administration tool.

Users/passwords

The system user in the book is 'dsl'. I use 'egbert' instead. Choose a good password. This is the user that can sudo to become root.

Other (application) users are:

  • the 'lampp' for the XAMPP pages (security achieved by creating a .htaccess file)
  • the 'root' account in MySQL. Advised is to set a password but beware: there are a few places where the plaintext password is needed to make the website to function correctly.
  • the 'pma' user (for phpmyadmin) needs a password too.
  • 'nobody' ftp user.

Executing '/opt/lampp/lampp security' takes care of all these. Remember that these users must be created by hand when not XAMPP but Ubuntu LAMP is installed!

There is also an user 'dsl' created for ControlCentre in the database. This user is created during 'install_db.php'. Its password is 'dsl9000p3'. Change this as soon as the ControlCentre pages are working.

IMPORTANT: The above mentioned Control Centre user 'dsl' is hard coded in the admin.php module and has more privileges than any other CC user that was added via the admin webpage.

XAMPP software

The XAMPP software can be downloaded from the XAMPP site. Installation is very straitforward. Just download and untar the packages (install also the devel package) to /opt. See chapter 2.2.1 on page 38.

tar zxvf xampp-linux-1.7.4.tar.gz -C /opt
tar zxvf xampp-linux-devel-1.7.4.tar.gz -C /opt

The instructions to update the libraries with 'ldconfig' on page 44-45 can safely be ignored when 'libmysqlclient-dev' is installed from Ubuntu repository (the files will go to /usr/lib and /usr/include/mysql). Existing Ubuntu programs and own programs like 'helloworld.c' and 'my_test.c' will use the libraries in /usr/lib and the header files in /usr/include/mysql. Theoretically it is possible to use the libraries in /opt/lampp/lib but there will be problems with existing Ubuntu programs like 'gdm' and 'gnone'. Those expect newer (non-mysql) libraries that those supplied in /opt/lampp/lib. If you would move the 'libmysql*' files in /opt/lampp/lib to a new subdirectory 'mysql2', place that path in the /etc/ld.so.conf and execute ldconfig, it will work. I did not encounter any problems with the pre-linked programs in XAMPP. I guess the are linked against /opt/lampp/lib.

IMPORTANT: The ownership was not correct! Therre was no user '1001:users' on my system. I had to do a 'chown -R nobody:nogroup /opt/lampp/*' to get the website working since XAMPP Apache2 is running as nobody:nogroup.

Follow the instructions in the book to make XAMPP more secure by executing '/opt/lampp/lampp security'.

Initial tests

Initial functionality was tested with the "hello world" C-program (page 46) and MySQL program (page 47). First the simple 'Hello world' program:

egbert@wyse:~$ gcc helloworld.c -o helloworld
egbert@wyse:~$ ./helloworld
egbert@wyse:~$ Hello world!

In the XAMPP package all header and library files are placed in /opt/lampp/include and /opt/lampp/lib. The book gives another subdirectory level (../mysql) below these but that seems to have changed sometime after the XAMPP 1.6.8a release. This imposes a problem though. Having all the mysql libraries in '/opt/lampp/lib' means that too many libraries are mapped with 'ldconfig' if '/opt/lampp/lib' is added to '/etc/ld.so.conf'. This became painfully clear after a reboot when gdm/gnome couldn't start anymore due to wrong version problems. I solved this at first by moving all *mysql* libraries to a new subdir mysql2 and placing this subdirectory in the ld.so.conf file instead. The compilation succeeds as shown:

egbert@wyse:~$ gcc my_test.c -o my_test -I/opt/lampp/include -L/opt/lampp/lib -lmysqlclient
egbert@wyse:~$ ./mytest
egbert@wyse:~$ MySQL client version: 5.5.8

A better solutions is to leave XAMPP untouched. Do not change '/etc/ld.so.conf at all but install the libmysqlclent-dev package. Compile the test program again with :

egbert@wyse:~$ gcc my_test.c -o my_test -I/usr/include/mysql -L/usr/lib -lmysqlclient
egbert@wyse:~$ ./mytest
egbert@wyse:~$ MySQL client version: 5.5.41

From the output it is clear that there is an older version of the mysqlclient library in XAMPP 1.7.4 than in Ubuntu 10.04.

Control Centre software

The Control Centre software can be downloaded from the Elektor website. It comes as an uncompressed tarball and unpacks to two subdirectories; the website (in htdocs) and the programs (in ctrl_centre). Download to /home/egbert and unpack to /var/www.

root@wyse:~# tar xvf ctrl_centre_20110120.tar -C /opt/lampp

The root password to access the database is hard coded in /var/www/htdocs/install_db.php (see page 55). This must be changed to the root password given when XAMPP/MySQL was secured with a password. The install_db.php script creates the database and the tables. It needs the priv. user for that.

The root password is also hard coded in /var/www/htdocs/public/vars.inc.php. The passwords in these files must be edited before running 'install_db.php'!

Issues with newer versions of XAMPP (PHP5.x)

error_reporting

PHP5 is much stricter than before. The 'error_reporting' gives 'Notice:' messages when constant and variables are not defined properly. It is possible to change this by modifying the 'error_reporting' setting in /opt/lampp/etc/php.ini to:

error_reporting = E_ALL & ~E_NOTICE

...OR... bring your source in line with the standards...

depreciated functions

In /opt/lampp/htdocs/admin/usermgmt.php the functions 'ereg' and 'eregi' are used. These are depreciated and should be replaced by 'preg_match'. The syntax is nerly the same. Just add '/.../' around the first argument (or '#...#' when the first argument contains a lot of '/'). For 'eregi' (case insensitive) use '/.../i' to escape the first argument.

so:
if (!ereg("^[A-Za-z' -]{1,50}$",$value))
becomes:
if (!preg_match("/^[A-Za-z' -]{1,50}$/",$value))

if(eregi("name",$field)
becomes:
if(preg_match("/name/i",$field)

Website vulnerablilties

To keep the software as simple as possible, no input sanity checking is done. This makes it possible to login to the system with trivial SQL injection tricks. Do NOT enable internet access to the Control Centre webpage as long as you have not taken counter messures! A first attempt to prevent illegal logins would be to replace a few lines in login.php.

root@wyse:/opt/lampp/htdocs# diff /home/egbert/tarballs/cc/htdocs/index.php index.php
22c22
< $sql = "SELECT user_name FROM $table_name WHERE user_name='$_POST[login_username]'";
---
> $sql = sprintf("SELECT user_name FROM $table_name WHERE user_name='%s'", mysql_real_escape_string($_POST[login_username]));
27c27
< $sql = "SELECT user_name FROM $table_name WHERE user_name='$_POST[login_username]' AND password=md5('$_POST[login_password]')";
---
> $sql = sprintf("SELECT user_name FROM $table_name WHERE user_name='%s' AND password=md5('%s')",\
         mysql_real_escape_string($_POST[login_username]), mysql_real_escape_string($_POST[login_password]));

Parallel port driver

Compile and test the parallel port driver lptout.c as instructed on page 61. The parallel port on the Wyse Terminal has adres 0x378. Make sure '/etc/sudoers' has a line for the user that runs apache2 (nobody) on this system (see page 56):

nobody wyse=(root) NOPASSWD: /usr/local/bin/lptout

This line is much more restricted than the example on page 56. Here the ONLY command that the user 'nobody' may execute as 'root' on system 'wyse', without giving a password, is '/usr/local/bin/lptout'. Do consult the 'sudoers' man page.

Before the test via the website can be executed, the test page needs a small update to match the hardware interface. Edit '/opt/lampp/htdocs/test/lamp_test.php' as follows:

case "ON":
    {
    echo exec ("sudo lptout $par_port 1; sleep 1; sudo lptout $par_port 0");
    }
    break;
case "OFF":
    {
    echo exec ("sudo lptout $par_port 8; sleep 1; sudo lptout $par_port 0");
    }
    break;

The hardware interface

The remote controlled 230V unit I bought is slightly different from the one used in the book. The remote controller in the book has simple switches that make contact to ground. This can easily be bridged by a transistor or opto-coupler. See page 64, 65. My remote controller has only 6 contacts which form a floating 2x3 matrix connected to 5 pins of the chip in the remote control. I had to design another solution.

The hardware interface I designed mimics a 2x3 matrix, since that is what I found in the remote control. To be able to do the key press without actually pressing the rubberish keys, I used 6 small reed relays of which the contacts are arranged in a 2x3 matrix. On the remote the 5 points of the matrix are brought out via a small connector placed under the battery. This needs some soldering skills. In this way the remote control stays fully operational when used as standalone device. It operates on its own 3V battery.

The remote control must be placed in a 'base station' to be operated from the computer. The 2x3 matrix formed by the relay contacts are then connected in parallel to the 2x3 matrix in the remote control via the 7 pins header in the remote control. The 6 relays coils are connected to the first 6 data bits of the parallel port via an inverting buffer (74LS04). Vcc for the buffers and the relays comes from an USB port via a second cable.

The bits of the parallel port are connected as follows:

pin 2 = bit 0 = 0x00000001 = channel 1 ON 
pin 3 = bit 1 = 0x00000010 = channel 2 ON
pin 4 = bit 2 = 0x00000100 = channel 3 ON
pin 5 = bit 3 = 0x00001000 = channel 1 OFF
pin 6 = bit 4 = 0x00010000 = channel 2 OFF
pin 7 = bit 5 = 0x00100000 = channel 3 OFF
pin 8 = bit 6 = unused
pin 9 = bit 7 = unused

pin 18-25 ground.
Click the photos to enlarge
Img 0012.jpg This is the PCB in the remote controller. The connection from top to bottom is by some black conducting paint. It is not possible to solder here.
Img 0046.jpg The hardware interface with 74LS04 and 6 DIL sized reed swtiches. The black cable is a USB cable for just the 5V. The 7 pin header is visible at the right. Only 5 pins are used; 2 are used in the remote controller to keep the connector in place.
Img 0045.jpg The 'base station' with the remote controller on top. To operate the controller must be placed upright in a hole in the base station.
Img 0054.jpg The schematic of the 'base station'.

Operation

To simulate sending the code for channe 1 ON, a '1'shoud be placed on pin 2 (bit 0). After a short dalay the '1' should be removed again; place a '0' on pin 2. To switch channel 1 OFF, toggle pin 5 (bit 3) in the same way. This is exactly what the lines in the case statement in the lamp_test.php page do.

Switch 1 ON:
root@wyse:~# lptout 378 1
root@wyse:~# sleep 1
root@wyse:~# lptout 378 0

Switch 1 OFF:
root@wyse:~# lptout 378 8
root@wyse:~# sleep 1
root@wyse:~# lptout 378 0

SMS Service

My Nokia 3109c works fine as modem via USB (ttyACM0) but I need this phone tot *receive* the messages. I have several other phones but no data cables.

GPS receiver

A Holtec GPS receiver works fine via USB (ttyUSBx). Set minicom to 4800 Baud for readable messages.

Webcam

Running a webcam on a low-end PC like the Wyse terminal is a challenge... Most webcams should work under 10.04 but some need much more CPU power than others. I have a re-branded Aiptek webcam that works most of the time... Creative cams seem to ease live a bit but I do not own such a model. I installed two extra packages: streamer and xawtv (both via 'aptitude install').