#!/usr/bin/env python
# -*- coding: utf-8 -*-
# +--------------------------------------------------------------------------+
# | Endian Firewall                                                          |
# +--------------------------------------------------------------------------+
# | Copyright (c) 2004-2017 Endian S.p.A. <info@endian.com>                  |
# |         Endian S.p.A.                                                    |
# |         via Pillhof 47                                                   |
# |         39057 Appiano (BZ)                                               |
# |         Italy                                                            |
# |                                                                          |
# | This program is free software; you can redistribute it and/or modify     |
# | it under the terms of the GNU General Public License as published by     |
# | the Free Software Foundation; either version 2 of the License, or        |
# | (at your option) any later version.                                      |
# |                                                                          |
# | This program is distributed in the hope that it will be useful,          |
# | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
# | GNU General Public License for more details.                             |
# |                                                                          |
# | You should have received a copy of the GNU General Public License along  |
# | with this program; if not, write to the Free Software Foundation, Inc.,  |
# | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              |
# +--------------------------------------------------------------------------+

#
# /etc/inittab example:
#
# id:3:initdefault:
#
# l0:0:wait:/etc/rc.d/rc.halt halt
# l6:6:wait:/etc/rc.d/rc.halt reboot
#
# si::sysinit:/etc/rc.d/rc.sysinit
#
# # Trap CTRL-ALT-DELETE
# ca::ctrlaltdel:/sbin/shutdown -r now
#
# # Run efw-console instead of mingetty in standard runlevels
# 1:2345:respawn: /usr/sbin/efw-console tty1
# 2:2345:respawn: /usr/sbin/efw-console tty2
# 3:2345:respawn: /usr/sbin/efw-console tty3
# 4:2345:respawn: /usr/sbin/efw-console tty4
# 5:2345:respawn: /usr/sbin/efw-console tty5
# 6:2345:respawn: /usr/sbin/efw-console tty6
# s0:2345:respawn: /usr/sbin/efw-console ttyS0 115200 vt100
#

import math
import subprocess
import termios
import sys
import os
import glob
import getpass
import signal
import fcntl
import time
import crypt
import struct
import cStringIO as StringIO
from endian.data import DataSource
from endian.core import ansi
from endian.core import utmp
from endian.core.procutils import set_process_name
from endian.validators.boolean import to_bool

# Defaults for line-editing etc. characters

DEF_ERASE  = 127         # default erase character (del)
DEF_INTR   = 3           # default interrupt character (ctrl+c)
DEF_QUIT   = 28          # default quit char (ctrl+\)
DEF_KILL   = 21          # default kill char (ctrl+u)
DEF_EOF    = 4           # default EOF char (ctrl+d)
DEF_EOL    = 0
DEF_SWITCH = 0           # default switch char
DEF_NEXT   = 22

ANSI = dict([ (x, getattr(ansi, x)) for x in [
    'BBLACK', 'BBLUE', 'BCYAN', 'BGREEN', 'BLACK', 'BLUE', 'BLINK',
    'BMAGENTA', 'BOLD', 'BRED', 'BWHITE', 'BYELLOW', 'CLEAR',
    'CLEAR_LINE', 'CURSOR_TOP', 'CYAN', 'GREEN', 'INVERSE', 'MAGENTA',
    'RED', 'RESET', 'RESTORE_POSITION', 'SAVE_POSITION',
    'UNDERLINE', 'WHITE', 'YELLOW']])

class EFWConsole(object):

    tty = None
    baud = None
    term = None
    test = None

    def __init__(self, tty=None, baud=None, term="linux", test=False):
        self.tty = tty
        self.baud = baud
        self.term = term
        self.test = test
        self.running = True
        # Open the tty
        if tty:
            self.open_tty(tty)
        # Set tty attributes
        self.termio_init(baud)
        os.putenv("TERM", term)
        # Create a new session
        try:
            os.setsid()
        except:
            pass
        # Register signal handlers
        self.set_signals()
        self.header = self.read_file_content("/etc/endian/console")
        os.chdir("/")
        # Update utmp
        self.update_utmp_console()
        # Set the process title
        try:
            set_process_name("efw-console %s " % os.path.basename(os.ttyname(0)))
        except:
            pass

    def is_enabled(self):
        try:
            ds = DataSource()
            return to_bool(ds.console.settings.enabled)
        except:
            return True

    def update_utmp_console(self):
        """ Update our utmp entry - Set user logged off """
        try:
            utmp.update_utmp(type=utmp.LOGIN_PROCESS, user="LOGIN")
        except:
            pass

    def get_actions(self):
        ds = DataSource()
        actions = sorted(ds.console.menu, key=lambda x: x['order'])
        disabled_keys = (ds.console.settings.disabled_keys or '').split(',')
        for i, action in enumerate(actions):
            if i < 10:
                action['key'] = str(i) # use a number as key
            else:
                action['key'] = chr(i+97-5) # user a letter as key
            action['enabled'] = action['key'] not in disabled_keys
        actions = [x for x in actions if x['enabled']]
        return actions

    def run(self):
        while self.running:
            actions = self.get_actions()
            self.display_menu(actions)
            action = self.prompt_action(actions)
            self.run_action(action)

    def display_menu(self, actions):
        output = StringIO.StringIO()
        width, height = get_terminal_size()
        try:
            hostname = self.get_hostname()
            product = self.get_product()
            release = self.get_release()
            green = self.get_green()
            uplinks = self.get_uplinks()

            if self.header:
                output.write('%s\n' % self.header)
            else:
                output.write('\n')
            if release:
                output.write('Release: {release}\n'.format(release=release, **ANSI))
            if product:
                output.write('Product: {product}\n'.format(product=product, **ANSI))
            if hostname:
                output.write('Hostname: {hostname}\n'.format(hostname=hostname, **ANSI))
            output.write('\n')
            output.write('{BOLD}{GREEN}GREEN Zone {dhcp}{RESET}\n'.format(dhcp=green.get('GREEN_DHCP_SERVER', False) and '[DHCP SERVER ENABLED]' or '', **ANSI))
            if green.get('URL'):
                output.write('Management URL: {url}\n'.format(url=green.get('URL', ''), **ANSI))
            output.write('IPs: {ips}\n'.format(ips=green.get('GREEN_IPS', ''), **ANSI))
            output.write('Devices: {interfaces}\n'.format(interfaces=' '.join(green.get('INTERFACES', [])), **ANSI))
            output.write('\n')
            for uplink in uplinks[0:2]: # Show only 2 uplinks
                output.write('{BOLD}{RED}Uplink - {name} {active}{RESET}\n'.format(name=uplink.get('NAME', ''),active=(uplink.get('ACTIVE', '') and '[ACTIVE]' or ''), **ANSI))
                output.write('IPs: {ips} [{red_type}]\n'.format(ips=uplink.get('IPS', ''), red_type=uplink.get('RED_TYPE',''), **ANSI))
                output.write('Device: {interface}\n'.format(interface=self.format_interface(uplink.get('INTERFACE', '')), **ANSI))
                output.write('\n')
            if os.path.isfile('/tmp/reboot_required') or os.path.isfile('/var/tmp/oldkernel'):
                output.write("   +     System reboot required\n")
                output.write("  / \    ======================\n")
                output.write(" / ! \   Reboot is required to complete\n")
                output.write("+-----+  the installation of software updates.\n")
            output.write('\n')
        except Exception as ex:
            print(ex)

        # empty lines
        lines = height - len(output.getvalue().split('\n')) - 4
        for i in range (0, min(lines,20)):
            output.write('\n')

        # menu
        try:
            rows = int(math.ceil(len(actions) / 2.0))
            for i in range(0, rows):
                left = "{BOLD}{GREEN}{n}{RESET} {GREEN}{description:38s}{RESET}".format(n=actions[i].key, description=actions[i].label, **ANSI)
                if i + rows < len(actions):
                    right = "{BOLD}{GREEN}{n}{RESET} {GREEN}{description}{RESET}".format(n=actions[i+rows].key, description=actions[i+rows].label, **ANSI)
                else:
                    right = ""
                output.write(left + right + '\n')
        except Exception as ex:
            output.write('%s\n' % ex)
        menu = output.getvalue()
        output.close()
        sys.stdout.write("\033[H\033[J" + menu)
        return menu

    def prompt_action(self, actions):
        if self.test == 'quit':
            self.running = False
            return None
        try:
            value = self.getkey("\nChoice: ", echo=True, newline=True)
            if value == '\n':
                return None
            if self.test and value == 'q':
                self.running = False
                return None
            for action in actions:
                if action.key == value:
                    return action
            return None
        except Exception as ex:
            print(ex)
            return None

    def run_action(self, action):
        try:
            if action is None:
                return
            if not self.is_enabled():
                sys.stdout.write("Console access disabled...\n")
                time.sleep(3)
                return
            if to_bool(action['password_required']):
                if not self.check_root_password():
                    return
            subprocess.call(action['command'])
            self.wait_enter("\nPress ENTER")
        except Exception as ex:
            print(ex)
        finally:
            if action is not None and to_bool(action['reset']):
                self.set_signals()
                self.termio_init(self.baud)

    def set_signals(self):
        # When a console receives the SIGUSR1 signal, it redisplays the menu
        try:
            signal.signal(signal.SIGINT, signal.SIG_IGN) # ctrl-c
            signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ctrl-z
            signal.signal(signal.SIGTERM, signal.SIG_IGN)
            signal.signal(signal.SIGTTIN, signal.SIG_IGN)
            signal.signal(signal.SIGTTOU, signal.SIG_IGN)
            signal.signal(signal.SIGQUIT, signal.SIG_IGN)
            signal.signal(signal.SIGUSR1, self.signal_received)
        except:
            pass

    def reset_signals(self):
        try:
            signal.signal(signal.SIGINT, signal.SIG_DFL)
            signal.signal(signal.SIGTSTP, signal.SIG_DFL)
            signal.signal(signal.SIGTERM, signal.SIG_DFL)
            signal.signal(signal.SIGTTIN, signal.SIG_DFL)
            signal.signal(signal.SIGTTOU, signal.SIG_DFL)
            signal.signal(signal.SIGQUIT, signal.SIG_DFL)
        except:
            pass

    def signal_received(self, signum, stack):
        pass

    def wait_enter(self, prompt):
        sys.stdout.write(prompt)
        sys.stdout.flush()
        while self.getkey() != '\n':
            pass

    def getkey(self, prompt=None, echo=False, newline=False):
        if prompt:
            sys.stdout.write(prompt)
            sys.stdout.flush()
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
        new[6][termios.VMIN] = 1
        new[6][termios.VTIME] = 0
        termios.tcsetattr(fd, termios.TCSANOW, new)
        c = None
        try:
            try:
                c = os.read(fd, 1)
            except:
                c = None
        finally:
            termios.tcsetattr(fd, termios.TCSAFLUSH, old)
        if echo:
            if c != None:
                sys.stdout.write(c)
            sys.stdout.flush()
        if newline:
            sys.stdout.write("\n")
        return c

    def check_root_password(self):
        " Ask and check the root password "
        rootpwd = getpass.getpass("Enter Root Password: ")
        with open("/etc/shadow") as f:
            for line in f.xreadlines():
                items = line.split(":")
                if items[0] == "root":
                    f.close()
                    res = crypt.crypt(rootpwd, items[1]) == items[1]
                    if res:
                        return True
                    else:
                        sys.stdout.write("Wrong password\n")
                        self.wait_enter("\nPress ENTER")
                        return False
        return False

    def get_uplinks(self):
        " Get uplinks informations, return a list of dict "
        uplinks = DataSource("uplinks")
        result = []
        for f in glob.glob("/var/efw/uplinks/*/data"):
            try:
                name = os.path.basename(os.path.dirname(f))
                uplink = uplinks[name].data
                t = {
                    'NAME': name,
                    'IPS': uplink.ips,
                    'INTERFACE': uplink.interface,
                    'RED_TYPE': uplink.red_type,
                    'ACTIVE': os.path.exists(os.path.join(os.path.dirname(f), "active"))
                }
                result.append(t)
            except:
                pass
        return result

    def get_green(self):
        result = {}
        try:
            settings = DataSource("ethernet").settings
            green_dev = result['GREEN_DEV'] = settings.get("GREEN_DEV")
            result['GREEN_IPS'] = settings.get("GREEN_IPS")

            # get the interfaces
            result['INTERFACES'] = sorted([self.format_interface(os.path.basename(x)) for x in glob.glob('/sys/class/net/%s/brif/eth*' % green_dev)])

            # try to get the current green ip from "ip addr"
            green_ip = settings.get("GREEN_ADDRESS")
            output = subprocess.Popen(["/sbin/ip", "addr", "show", green_dev], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
            ip = None
            for line in output.split("\n"):
                if line.strip().startswith("inet "): # use "inet " avoid matching with "inet6"
                    if ip:
                        # ip already found, this is a second ip, return the one from settings
                        ip = green_ip
                        break
                    else:
                        ip = line.strip().split()[1]
            green_ip = ip or green_ip
            if green_ip:
                result['URL'] = "https://%s:10443" % (green_ip.split("/")[0])

            result['GREEN_DHCP_SERVER'] = DataSource("dhcp").settings.ENABLE_GREEN == 'on'
            return result
        except:
            return result

    def format_interface(self, interface):
        " If the interface is UP, return 'interface [UP]' else return 'interface "
        if not interface:
            return ''
        else:
            active = self.get_interface_status(interface)
            return '%s %s' % (interface, active and '[UP]' or '')

    def get_interface_status(self, interface):
        " Return True if an interface (e.g. eth0) is UP "
        data = self.read_file_content('/sys/class/net/%s' % interface)
        return data != '0'

    def get_release(self):
        " Return the release "
        return self.read_file_content("/etc/issue").strip()

    def get_product(self):
        " Return the product description "
        try:
            settings = DataSource("product").settings
            return settings.get('PRODUCT_NAME', '')
        except:
            return ""

    def get_hostname(self):
        " Return the hostname "
        try:
            settings = DataSource("host").settings
            return settings.get('HOSTNAME', '')
        except:
            return ""

    def read_file_content(self, filename):
        " Read the content of a file, return '' in case of errors "
        try:
            with open(filename, 'r') as f:
                return f.read()
        except Exception:
            return ""

    def open_tty(self, tty):
        " Replacement for mingetty/agetty "

        try:
            os.chown(tty, 0, 0)
        except Exception as ex:
            print(ex)

        try:
            os.chmod(tty, 0600)
        except Exception as ex:
            print(ex)

        try:
            signal.signal(signal.SIGHUP, signal.SIG_IGN)
        except:
            pass

        try:
            fd = os.open(tty, os.O_RDWR)
        except OSError as ex:
            print("%s: cannot open tty: %s" % (tty, str(ex)))
            sys.exit(1)

        try:
            fcntl.ioctl(fd, termios.TIOCSCTTY, 1)
        except OSError as ex:
            print("%s: no controlling tty: %s" % (tty, str(ex)))
        except IOError as ex:
            print("%s: no controlling tty: %s" % (tty, str(ex)))

        if not os.isatty(fd):
            print("%s: not a tty" % tty)
            sys.exit(1)

        # Set up stdin/stdout/stderr
        try:
            os.dup2(fd, 0)
            os.dup2(fd, 1)
            os.dup2(fd, 2)
            if fd > 2:
                os.close(fd)
        except Exception as ex:
            print("%s: dup2(): %s" % (tty, str(ex)))
            sys.exit(0)

        # Clear the terminal
        sys.stdout.write('\033[H\033[J')
        sys.stdout.flush()

    def termio_init(self, baud):
        " Set tty attributes "
        def get_termios_attributes(attrs):
            result = 0
            for attr in attrs:
                result |= getattr(termios, attr, 0)
            return result

        try:
            fd = sys.stdin
            (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(fd)
            # iflag
            iflag = get_termios_attributes(['IXON','IXOFF','ICRNL'])
            # oflag
            oflag = get_termios_attributes(['OPOST','ONLCR'])
            # cflag
            cflag = get_termios_attributes(['CS8','HUPCL','CREAD','CLOCAL'])
            # baud
            if baud:
                ispeed = baud
                ospeed = baud
            # lflag
            lflag = get_termios_attributes(['ICANON','ISIG','ECHO','ECHOE','ECHOK','ECHOKE'])
            # cc (list of the tty special character)
            cc[termios.VMIN] = 1
            cc[termios.VTIME] = 0
            cc[termios.VINTR] = DEF_INTR   # default interrupt
            cc[termios.VQUIT] = DEF_QUIT   # default quit
            cc[termios.VEOF] = DEF_EOF     # default EOF character
            cc[termios.VEOL] = DEF_EOL     # default EOL character
            cc[termios.VERASE] = DEF_ERASE # set erase character
            cc[termios.VKILL] = DEF_KILL   # set kill character
            cc[termios.VLNEXT] = DEF_NEXT
            if hasattr(termios, 'VSWTC'):
                cc[termios.VSWTC] = DEF_SWITCH # default switch character
            # make the new settings effective
            termios.tcsetattr(fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
        except Exception as ex:
            print("error setting tty attributes: %s" % str(ex))

def get_terminal_size():
    env = os.environ
    def ioctl_GWINSZ(fd):
        try:
            cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
        except:
            return
        return cr
    cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            cr = ioctl_GWINSZ(fd)
            os.close(fd)
        except:
            pass
    if not cr or cr[0] == 0:
        cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
    return int(cr[1]), int(cr[0])

def main():
    # Parse args
    tty = None
    baud = None
    term = "linux"
    argv = sys.argv[1:]
    test = False
    for arg in argv:
        if arg in ['-h', '-help', '--help', '?', '-?']:
            # Help
            sys.stderr.write("Usage: efw-console [TTY] [BAUD] [TERM]\n")
            sys.exit(2)
        elif arg == "-t": # Test mode - exit pressing 'q'
            test = True
        elif arg == "-T": # Test mode - quit after start
            test = 'quit'
        elif not arg.startswith("-"): # Ignore options
            if not baud:
                # Convert speed string to speed code
                baud = getattr(termios, 'B%s' % arg, None)
                if baud:
                    continue
            if not tty:
                tty = arg
                if not tty.startswith('/'):
                    tty = "/dev/%s" % tty
            else:
                term = arg
    # Start the console
    console = EFWConsole(tty=tty, baud=baud, term=term, test=test)
    console.run()

if __name__ == '__main__':
    main()
