captiveportal 5.39 KB
Newer Older
1 2
#!/bin/sh

Ad Schellevis's avatar
Ad Schellevis committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# Copyright (C) 2014 Deciso B.V.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

27 28
# load standard rc
. /etc/rc.subr
29

30
name=captiveportal
31
rcvar=captiveportal_enable
32 33 34
start_precmd=captiveportal_prestart
start_cmd="${name}_start"
stop_cmd="${name}_stop"
35

36 37
[ -z "$captiveportal_enable" ]       && captiveportal_enable="NO"

38 39 40 41
captiveportal_load_rc_config()
{
    CPWORKDIR="/var/captiveportal"
    CPDEFAULTTEMPLATE="/usr/local/opnsense/scripts/OPNsense/CaptivePortal/htdocs_default"
42

43 44 45
    # extract all zones from captive portal configuration
    CPZONES=`cat /usr/local/etc/captiveportal.conf | grep "\[zone_" | sed 's/\[zone_//' | sed 's/\]//'`
}
46

47 48 49 50 51
captiveportal_prestart()
{
  # initialize captiveportal work directory
  mkdir -p $CPWORKDIR
}
52

53 54
captiveportal_start()
{
55
    # if the API dispatcher is already running, we will assume all parts are running
56 57 58
    if [ ! -f /var/run/lighttpd-api-dispatcher.pid ]; then
        echo "Starting API dispatcher"
        /usr/local/sbin/lighttpd -f /var/etc/lighttpd-api-dispatcher.conf
59

60 61 62
        # generate ssl certificates
        /usr/local/opnsense/scripts/OPNsense/CaptivePortal/generate_certs.php

63
        # startup / bootstrap zones
64 65
        for zoneid in $CPZONES
        do
66 67 68
            # bootstrap captiveportal jail
            zonedirname="zone$zoneid"
            echo "Install : zone $zoneid"
69 70 71
            if [ ! -d $CPWORKDIR/$zonedirname ]; then
                mkdir $CPWORKDIR/$zonedirname
            fi
72 73 74
            if [ -d $CPWORKDIR/$zonedirname/tmp ]; then
                # remove temp (flush)
                rm -rf $CPWORKDIR/$zonedirname/tmp
75
            fi
76 77 78 79 80
            mkdir $CPWORKDIR/$zonedirname/tmp
            chmod 770 $CPWORKDIR/$zonedirname/tmp
            chown www:www $CPWORKDIR/$zonedirname/tmp

            # sync default template
81
            /usr/local/bin/rsync --delete -ar $CPDEFAULTTEMPLATE/ $CPWORKDIR/$zonedirname/htdocs/
82

83 84
            # overlay custom user layout if available.
            /usr/local/opnsense/scripts/OPNsense/CaptivePortal/overlay_template.py $zoneid
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

            # start new instance
            echo "Start : zone $zoneid"
            /usr/local/sbin/lighttpd -f /var/etc/lighttpd-cp-zone-$zoneid.conf
        done


        # cleanup removed zones
        for installed_zoneid in `ls $CPWORKDIR |  sed 's/zone//g'`
        do
            if [ -d $CPWORKDIR/zone$installed_zoneid ]; then
                for zoneid in $CPZONES
                do
                    is_installed=0
                    if [ "$zoneid" -eq "$installed_zoneid" ]; then
                        is_installed=1
                    fi
                    if [ "$is_installed" -eq 0 ]; then
                        echo "Uninstall : zone $installed_zoneid"
104
                        rm -rf "$CPWORKDIR/zone$installed_zoneid"
105 106
                    fi
                done
107 108
            fi
        done
109
        echo "start captiveportal background process"
110
        /usr/local/opnsense/scripts/OPNsense/CaptivePortal/cp-background-process.py start
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    else
        echo "already running"
    fi
}

# stop captive portal (sub) processes
captiveportal_stop()
{
    # startup API dispatcher, forwards captive portal api request to shared OPNsense API
    if [ -f /var/run/lighttpd-api-dispatcher.pid ]; then
        echo "Stopping API dispatcher"
        /bin/pkill -TERM -F /var/run/lighttpd-api-dispatcher.pid
        if [ -f /var/run/lighttpd-api-dispatcher.pid ]; then
            # in case pkill didn't do anything, always remove pid file
            rm /var/run/lighttpd-api-dispatcher.pid
        fi
127
    fi
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    # stopping zone http servers
    for zoneid in $CPZONES
    do
        # stop running instance
        zonepid="/var/run/lighttpd-cp-zone-$zoneid.pid"
        if [ -f $zonepid ]; then
            echo "Stop : zone $zoneid"
            /bin/pkill -TERM -F $zonepid
            rm $zonepid
        fi
    done
    # stopping unconfigured zones (not in $CPZONES list)
    for zonepid in `ls /var/run/lighttpd-cp-zone-*.pid 2>/dev/null`
    do
        /bin/pkill -TERM -F $zonepid
        rm $zonepid
    done

    if [ -f /var/run/captiveportal.db.pid ]; then
      echo "stop captiveportal background process"
      /bin/pkill -TERM -F /var/run/captiveportal.db.pid
    fi
}

captiveportal_load_rc_config
load_rc_config $name
run_rc_command $1