miniupnpd.inc 7.53 KB
Newer Older
1
<?php
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
/*
    Copyright (C) 2016 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.
*/

29
function miniupnpd_enabled()
30 31
{
    global $config;
32

33 34 35 36 37 38 39
    return isset($config['installedpackages']['miniupnpd']['config'][0]['enable']);
}

function miniupnpd_firewall($fw)
{
    if (!miniupnpd_enabled()) {
        return;
40
    }
41

42 43
    $fw->registerAnchor('miniupnpd', 'rdr');
    $fw->registerAnchor('miniupnpd', 'fw');
44
}
45 46 47 48 49

function miniupnpd_services()
{
    $services = array();

50 51
    if (!miniupnpd_enabled()) {
        return $services;
52 53
    }

54 55 56 57 58 59 60 61 62
    $pconfig = array();
    $pconfig['name'] = 'miniupnpd';
    $pconfig['description'] = gettext('Univeral Plug and Play');
    $pconfig['php']['restart'] = array('miniupnpd_stop', 'miniupnpd_start');
    $pconfig['php']['start'] = array('miniupnpd_start');
    $pconfig['php']['stop'] = array('miniupnpd_stop');
    $pconfig['pidfile'] = '/var/run/miniupnpd.pid';
    $services[] = $pconfig;

63 64
    return $services;
}
65 66 67

function miniupnpd_start()
{
68 69 70 71 72 73
    if (!miniupnpd_enabled()) {
        return;
    }

    if (isvalidpid('/var/run/miniupnpd.pid')) {
        return;
74
    }
75 76

    mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P /var/run/miniupnpd.pid');
77 78 79 80 81 82 83 84 85 86
}

function miniupnpd_stop()
{
    killbypid('/var/run/miniupnpd.pid', 'TERM', true);
    mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
    mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
}

function miniupnpd_configure()
87 88 89 90
{
    return array('miniupnpd_configure_do');
}

91 92 93 94 95 96 97 98
function miniupnpd_uuid()
{
    /* md5 hash of wan mac */
    $uuid = md5(get_interface_mac(get_real_interface("wan")));
    /* put uuid in correct format 8-4-4-4-12 */
    return substr($uuid,0,8).'-'.substr($uuid,9,4).'-'.substr($uuid,13,4).'-'.substr($uuid,17,4).'-'.substr($uuid,21,12);
}

99
function miniupnpd_configure_do($verbose = false)
100 101
{
    global $config;
102

103 104
    miniupnpd_stop();

105 106 107 108 109 110 111 112
    if (!miniupnpd_enabled()) {
        return;
    }

    if ($verbose) {
        echo 'Starting UPnP service...';
        flush();
    }
113 114 115 116 117

    $upnp_config = $config['installedpackages']['miniupnpd']['config'][0];

    $ext_ifname = get_real_interface($upnp_config['ext_iface']);
    if ($ext_ifname == $upnp_config['ext_iface']) {
118 119 120
        if ($verbose) {
            echo "failed.\n";
        }
121 122 123 124 125 126 127 128 129
        return;
    }

    $config_text = "ext_ifname={$ext_ifname}\n";
    $config_text .= "port=2189\n";

    $ifaces_active = '';

    /* since config is written before this file invoked we don't need to read post data */
130
    if (!empty($upnp_config['iface_array'])) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        foreach(explode(',', $upnp_config['iface_array']) as $iface) {
            /* Setting the same internal and external interface is not allowed. */
            if ($iface == $upnp_config['ext_iface']) {
                continue;
            }
            $if = get_real_interface($iface);
            /* above function returns iface if fail */
            if ($if!=$iface) {
                $addr = find_interface_ip($if);
                $bits = find_interface_subnet($if);
                /* check that the interface has an ip address before adding parameters */
                if (is_ipaddr($addr)) {
                  $config_text .= "listening_ip={$if}\n";
                  if (!$ifaces_active) {
                      $webgui_ip = $addr;
                      $ifaces_active = $iface;
                  } else {
                      $ifaces_active .= ", {$iface}";
                  }
                } else {
                    log_error("miniupnpd: Interface {$iface} has no ip address, ignoring");
                }
            } else {
                log_error("miniupnpd: Could not resolve real interface for {$iface}");
            }
        }

        if (!empty($ifaces_active)) {
            /* override wan ip address, common for carp, etc */
            if (!empty($upnp_config['overridewanip'])) {
                $config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
            }
            /* set upload and download bitrates */
            if (!empty($upnp_config['download']) && !empty($upnp_config['upload'])) {
                $download = $upnp_config['download']*1000;
                $upload = $upnp_config['upload']*1000;
                $config_text .= "bitrate_down={$download}\n";
                $config_text .= "bitrate_up={$upload}\n";
            }

            $config_text .= "secure_mode=yes\n";

            /* enable logging of packets handled by miniupnpd rules */
            if (!empty($upnp_config['logpackets'])) {
                $config_text .= "packet_log=yes\n";
            }

            /* enable system uptime instead of miniupnpd uptime */
            if (!empty($upnp_config['sysuptime'])) {
                $config_text .= "system_uptime=yes\n";
            }

            /* set webgui url */
            if (!empty($config['system']['webgui']['protocol'])) {
                $config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
                if (!empty($config['system']['webgui']['port'])) {
                    $config_text .= ":{$config['system']['webgui']['port']}";
                }
                $config_text .= "/\n";
            }

            /* set uuid and serial */
            $config_text .= "uuid=".miniupnpd_uuid()."\n";
            $config_text .= "serial=".strtoupper(substr(miniupnpd_uuid(),0,8))."\n";

            /* set model number */
            $config_text .= "model_number=".file_get_contents("/usr/local/opnsense/version/opnsense")."\n";

            /* upnp access restrictions */
            for ($i=1; $i<=4; $i++) {
                if ($upnp_config["permuser{$i}"]) {
                    $config_text .= "{$upnp_config["permuser{$i}"]}\n";
                }
            }

            if (!empty($upnp_config['permdefault'])) {
                $config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
            }

            /* Allow UPnP or NAT-PMP as requested */
            $config_text .= "enable_upnp="   . ( $upnp_config['enable_upnp']   ? "yes\n" : "no\n" );
            $config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" );

            /* write out the configuration */
215
            file_put_contents('/var/etc/miniupnpd.conf', $config_text);
216

217 218
            log_error("miniupnpd: Starting service on interface: {$ifaces_active}");
            miniupnpd_start();
219 220
        }
    }
221 222 223 224

    if ($verbose) {
        echo "done.\n";
    }
225
}