summaryrefslogtreecommitdiff
path: root/usr/libexec/commission/commission
blob: 869198453a3a94fa1b6c284d436a0bfde0b08218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lighttpd
DAEMONNAME="lighttpd"
NAME=commission
DESC="MT lighttpd Commissioning Server"
OPTS="-f /etc/lighttpd-commission.conf"
PIDFILE=/var/run/lighttpd-commission.pid 
CONF_DIR=/var/config
ENABLED=yes
STINV="/sys/class/leds/led-status/invert"

INIT=$(ps --no-header -o comm -p 1)
if [[ $INIT == systemd ]] ; then
    OPTS="-D ${OPTS}"
fi

PASSWORDS=$(passwd -Sa | egrep '^[^[:space:]]+[[:space:]]P[[:space:]]' | wc -l)

if (($PASSWORDS == 0)) ; then
    # Block wwan0 from commissioning
    /usr/libexec/commission/nfon.sh
    # No password, so indicate commissioning mode
    # php-fpm-commision will be turned on as well
    rm -f "/run/mt-commission"
    echo 'ENABLED="yes"' >"/run/mt-commission"
    # Invert the blink to show commissioning mode
    echo "Invert the blink, I am $(id)"
    t1=(1 1 1 1 1 1 1 1 1 1 1 1 4 16 64)
    i=0
    while ! echo "1" >"$STINV" ; do
        if ((i < 15)) ; then
            t0=${t1[$i]}
        else
            t0=60
        fi
        echo "LED is not ready, so sleep for $t0"
        sleep $t0
        ((t0=(t0+1)*(t0+1)))
        if((t0 > 64)) ; then
            t0=64
        fi
    done 2>/dev/null
else
    ENABLED="no"
    echo 'ENABLED="no"' >"/run/mt-commission"
    echo "Clear the blink"
    echo "0" >"$STINV"
fi

[ -f "/etc/default/no-${NAME}" ] && . "/etc/default/no-${NAME}"

if [ ! -f $CONF_DIR/server.pem ]; then
  echo "Generating server cert"
  openssl req \
    -x509 -nodes -days 3650 \
    -subj '/C=US/ST=Minnesota/L=Minneapolis/CN=mlinux.example.com' \
    -newkey rsa:2048 -keyout $CONF_DIR/server.pem -out $CONF_DIR/server.pem
fi

if [ "$ENABLED" != "yes" ]; then
  echo "Some user has a password"
  if [ "$VERBOSE" = very ] ; then
            passwd -Sa | egrep '^[^[:space:]]+[[:space:]]P[[:space:]]' | logger -s -p daemon.notice
            echo "user count with passwords: $PASSWORDS"
  fi
	  exit
fi
echo -n "Starting $DESC: "
echo -n "Starting $DESC: " >/dev/console
exec "$DAEMON" $OPTS