__init__.py 4.82 KB
Newer Older
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
"""
    Copyright (c) 2015 Ad Schellevis
    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.

"""
import os.path
import stat
29
import xml.etree.ElementTree
30 31
from ConfigParser import ConfigParser

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
class Config(object):
    """ handle to captive portal config (/usr/local/etc/captiveportal.conf)
    """
    _cnf_filename = "/usr/local/etc/captiveportal.conf"

    def __init__(self):
        """ consctruct new config object
        """
        self.last_updated = 0
        self._conf_handle = None
        self._update()

    def _update(self):
        """ check if config is changed and (re)load
        """
        mod_time = os.stat(self._cnf_filename)[stat.ST_MTIME]
        if os.path.exists(self._cnf_filename) and self.last_updated != mod_time:
            self._conf_handle = ConfigParser()
            self._conf_handle.read(self._cnf_filename)
            self.last_updated = mod_time

    def get_zones(self):
        """ return list of configured zones
            :return: dictionary index by zoneid, containing dictionaries with zone properties
        """
        result = dict()
        self._update()
60
        if self._conf_handle is not None:
61 62
            for section in self._conf_handle.sections():
                if section.find('zone_') == 0:
63
                    zoneid = section.split('_')[1]
64
                    result[zoneid] = dict()
65
                    for item in self._conf_handle.items(section):
66 67 68 69 70 71 72 73 74 75 76 77
                        result[zoneid][item[0]] = item[1]
                    # convert allowed(MAC)addresses string to list
                    if 'allowedaddresses' in result[zoneid] and result[zoneid]['allowedaddresses'].strip() != '':
                        result[zoneid]['allowedaddresses'] = \
                            map(lambda x: x.strip(), result[zoneid]['allowedaddresses'].split(','))
                    else:
                        result[zoneid]['allowedaddresses'] = list()
                    if 'allowedmacaddresses' in result[zoneid] and result[zoneid]['allowedmacaddresses'].strip() != '':
                        result[zoneid]['allowedmacaddresses'] = \
                            map(lambda x: x.strip(), result[zoneid]['allowedmacaddresses'].split(','))
                    else:
                        result[zoneid]['allowedmacaddresses'] = list()
78
        return result
79

80 81 82 83 84 85 86 87 88
    def fetch_template_data(self, zoneid):
        """ fetch template content from config
        """
        for section in self._conf_handle.sections():
            if section.find('template_for_zone_') == 0 and section.split('_')[-1] == str(zoneid):
                if self._conf_handle.has_option(section, 'content'):
                    return self._conf_handle.get(section, 'content')
        return None

89

90 91 92 93
class OPNSenseConfig(object):
    """ Read configuration data from config.xml
    """
    def __init__(self):
94
        self.rootNode = None
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        self.load_config()

    def load_config(self):
        """ load config.xml
        """
        tree = xml.etree.ElementTree.parse('/conf/config.xml')
        self.rootNode = tree.getroot()

    def get_template(self, fileid):
        """ fetch template content from config.xml
            :param fileid: internal fileid (field in template node)
            :return: string, bse64 encoded data or None if not found
        """
        templates = self.rootNode.findall("./OPNsense/captiveportal/templates/template")
        if templates is not None:
            for template in templates:
111
                if template.find('fileid') is not None and template.find('content') is not None:
112 113 114 115
                    if template.find('fileid').text == fileid:
                        return template.find('content').text

        return None