Developers Guide

Introduction

LibreServer consists almost entirely of bash scripts, with some html, css and php for the web interface. There are many bash scripts, but they're not very complicated. If you're familiar with the GNU/Linux commandline and can hack a bash script then you can probably add a new app or fix a bug in the system. There are no trendy development frameworks to learn or to get in your way. You might also want to consult the Code of Conduct for guidance upon the overall esprit de corps, and there is a Matrix room at #libreserver:conduit.libreserver.org and an XMPP multi-user chat at libreserver@chat.libreserver.org

Source code

Creating the Raspbian image

Download the Raspberry Pi Debian image, then copy it to a USB drive:

  unxz *_raspi_4_bookworm.img.xz
  sudo dd bs=512 if=*_raspi_4_bookworm.img of=/dev/sdX conv=fdatasync,sync,noerror status=progress

Mount the boot and rootfs partitions on the USB drive, then enable ssh login.

  sudo su
  cd /media/yourusername/RASPIFIRM
  sed -i 's|#root_pw=.*|root_pw=libreserver|g' sysconf.txt
  touch ssh

Now we add some scripts to the rootfs.

  cd /media/yourusername/RASPIROOT
  nano etc/profile.d/libreserver.sh
Add the following:
#!/bin/bash

install_libreserver_message()
{
    LIBREROOT=/root/libreserver
    if [ -f /root/libreserver-completed.txt ]; then
        if grep -q "install_final" /root/libreserver-completed.txt; then
            return
        fi
    fi
    clear
    if [[ "$USER" == "pi" ]]; then
        echo "Welcome, traveller"
        echo "Type \"sudo su\" to proceed with installation of LibreServer"
        return
    fi
}

install_libreserver_message

Save and exit. Then add another script:

  sed -i 's|#PermitRootLogin.*|PermitRootLogin yes|g' etc/ssh/sshd_config
  nano root/.bashrc

Add the following:

install_libreserver()
{
    LIBREROOT=/root/libreserver
    if [ -f /root/libreserver-completed.txt ]; then
        if grep -q "install_final" /root/libreserver-completed.txt; then
            return
        fi
    fi
    clear
    echo -e "\nBeginning LibreServer install\n"
    echo -e "\nIf at any point it asks to choose a version of a file, choose the Maintainer's version.\n\n"
    echo -e "\nWhen the install is complete use a browser to navigate to: http://[local ip address]/admin\n\n"
    if [ -d ${LIBREROOT} ]; then
        echo -e "\nRemoving existing LibreServer files."
        rm -rf ${LIBREROOT}
    fi
    echo -e "\nInstalling packages needed for LibreServer."
    apt update
    echo "apt install -y git build-essential dialog"
    apt install -y git build-essential dialog
    echo -e "\nCloning LibreServer repo."
    git clone https://gitlab.com/bashrc2/libreserver ${LIBREROOT}
    if [ ! -d ${LIBREROOT} ]; then
        echo -e "\nFailed to clone LibreServer repo"
        return
    fi
    if [ ! -d /home/admin ]; then
        echo ""
        echo "Creating an admin account. You can use whatever password you want here, since it will be replaced during the installation."
        echo ""
        /usr/sbin/adduser admin
    fi
    cd ${LIBREROOT} || return
    git checkout bookworm
    make install
    echo ""
    while true; do
        read -p "Install the onion (Tor only) version? Y/N" yn
        case $yn in
            [Yy]* ) libreserver menuconfig-onion; break;;
            [Nn]* ) libreserver menuconfig; break;;
            ,* ) echo "Please answer yes or no.";;
        esac
    done
}

install_libreserver

Save and exit. Then sync the USB drive.

  sync
  exit

You can now unmount the USB drive and use it as the basis for an img file. See the manpage for libreserver-distro for details of how to create an image which only includes the needed partitions and not the full drive size.

Creating images

To build images you will need the libreserver-maker tool. It can be installed with:

sudo apt install git btrfs-progs debootstrap kpartx \
  parted qemu-user-static qemu-utils sshpass
git clone https://gitlab.com/bashrc2/libreserver-maker
cd libreserver-maker

Then to build an image:

sudo python3 -m freedommaker amd64

The resulting image will appear as an xz file in the build subdirectory.

To copy it to a drive (eg. microSD or USB):

xzcat build/libreserver-bookworm-*-amd64.img.xz | \
  sudo dd bs=4096 of=/dev/sdg conv=fsync status=progress

Adding extra apps

Suppose you have some internet application which you want to add to the system. To do this you need to create an app script which tells the system how to install/remove and also backup/restore. The script should be designed to work with the current stable version of Debian.

Avoid apps written in Rust unless you can be sure that cargo will never try to run arbitrary applications from the /tmp directory, which is forbidden for security reasons and attempts to do that will be blocked.

There's a command which you can use to generate scripts for new apps. Some examples are as follows:

To create a script for a generic PHP plus MySql/MariaDB web app with a couple of extra packages:

libreserver-template --app [name] -e [email] -r [repo url] \
  --packages "cowsay libssl-dev" \
  -c [commit] --php yes -d mariadb > \
  src/libreserver-app-myappname

For a Nodejs app with MySql/MariaDB database:

libreserver-template --app [name] -e [email] -r [repo url] \
  -c [commit] --node yes -d mariadb \
  --dir /etc/myappname --daemon yes > \
  src/libreserver-app-myappname

For a Python app with Postgresql database:

libreserver-template --app [name] -e [email] -r [repo url] \
  -c [commit] -d postgresql \
  --dir /etc/myappname --daemon yes > \
  src/libreserver-app-myappname

For a Python app without any database, communicating between the daemon and the web server on port 1234:

libreserver-template --app [name] -e [email] -r [repo url] \
  -c [commit] --dir /etc/myappname \
  --daemon yes --portinternal 1234 > \
  src/libreserver-app-myappname

For an app without any database which communicates directly on a particular port through the firewall:

libreserver-template --app [name] -e [email] -r [repo url] \
  -c [commit] --dir /etc/myappname \
  --daemon yes --port 5000 > \
  src/libreserver-app-myappname

A generic PHP plus MySql/MariaDB web app which is only available on an onion address:

libreserver-template --app [name] -e [email] -r [repo url] \
  -c [commit] --php yes -d mariadb \
  --onion yes > \
  src/libreserver-app-myappname

For more details see the manpage:

man libreserver-template

The template command won't give you a fully working app, but it will give you a big head start and avoid a lot of potential mistakes. It's highly likely that you'll still need to add extra configuration for your particular app, especially within the install_app function.

When your new script is ready for testing you can install it with:

make install

Then run the administrator control panel and you should see the new app within Add/Remove apps.

Submit your working app via one of the methods described below.

Submitting patches or pull requests

If you've made changes and want to submit them back to the LibreServer project then there are a couple of ways to do this. This project aims not to have prolonged formal debates over patch correctness and they will typically be merged as soon as possible, with any necessary correcting commits being made subsequently.

Make a patch for a single small change

Commit your changes to your local repo.

git commit -a -m"My patch description"

Format it as a patch:

git format-patch -1 HEAD --stdout

HEAD can be replaced by the specific commit hash if needed. Copy and paste the result into an email addressed to bob@libreserver.org or a Matrix message to @bob:conduit.libreserver.org, and you're done.

Submitting multiple changes over time

If you plan to make many changes over an extended period of time then create an account on gitlab and make pull requests to https://gitlab.com/bashrc/libreserver.

Architecture

Apps

Apps are scripts beginning with src/libreserver-app-. New scripts can be created with the template command described above. Each script contains a number of function interfaces which enable the app to be installed, removed, backed up and for logging to be turned on or off. The interfaces are:

install_appname
install_interactive_appname
configure_interactive_appname
reconfigure_appname
remove_appname
upgrade_appname
backup_local_appname
restore_local_appname
logging_on_appname
logging_off_appname
add_user_appname
remove_user_appname
change_password_appname

install_interactive and configure_interactive are only used by the dialog menu interface accessible via ssh.

change_password is optional because with some apps there isn't any way to do that from the commandline.

reconfigure is used during factory reset to remove and regenerate keys or set the app back to its initial defaults.

Web interface

The web interface consists of a set of html files within webadmin/EN. These use forms to reference some php files within webadmin and those generate files which are then picked up by a systemd daemon running in the background (webadmin.service) which is implemented by src/libreserver-installer.

Typically the daemon detects the presence of a file, extracts the variables from it, maybe does some additional validation and then does whatever it needs to do with those.

The separate daemon also avoids having php code making any direct changes to the underlying system, which could create security risks. The files created by the php scripts are very small and can typically be fully validated.

The user interface is regenerated by the function install_web_admin within src/libreserver-utils-webadmin.

When installed, the web interface is in /var/www/libreserver.local/htdocs/admin. The directory above that is where a json API exists for use with the FreedomBox Android app.

Although it's tempting and easy, avoid adding javascript into the html files. The policy is to keep the web interface free from javascript, so that if you're using noscript or have javascript turned off then the interface can still be used without any loss of functionality.

It is assumed that only the administrator will be accessing the web interface, so dealing with collisions between multiple users making contradicting changes to the same things at the same time isn't necessary.

The web interface operates in a strictly sequential/synchronous mode. So if you try to install multiple apps it puts them into a queue and does the installs one after another. The same applies for app removals.

How translations happen

Translations are stored within webadmin/translations. When you select a language html files are copied from webadmin/EN and then the English strings are searched and replaced. Strings to be translated are indicated within the html files with the parameter translate="yes".

Doing it this way avoids having duplicated versions of the many html files for different languages. See the change_language function within src/libreserver-installer for details of how this process works.

Doing translations

These can be added or edited within the web UI. Navigate to http://libreserver/admin or its onion address, select settings then language. Select the language that you want to translate to then the translate button. In the right hand column you can then manually edit the translated strings. When you are done select the update button.

The format of the translation files is very simple. It's:

English string|Translated string

If you prefer to edit the raw files within your own editor of choice they can be found within the webadmin/translations directory of the repo.

Icons

If you want to change the icons of the web user interface they can be found within the webadmin/images directory of the repo.

Icons used for the mesh desktop can be found within img and img/avatars directories and their filenames begin with icon_.

Ideas for improvements