#!/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.              |
# +--------------------------------------------------------------------------+

import os
import signal
import subprocess
from endian.core import utmp

SHELL_PATH = "/usr/local/bin/efw-shell-restricted"

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

def update_utmp_shell(pid=None):
    """ Update our utmp entry - Set user logged in """
    try:
        utmp.update_utmp(type=utmp.USER_PROCESS, user="no login", pid=pid)
    except:
        pass

def main():
    try:
        signal.signal(signal.SIGUSR1, signal.SIG_IGN)
        signal.signal(signal.SIGINT, signal.SIG_IGN) # ctrl-c
        signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ctrl-z
        env = dict(os.environ)
        for var in ["PREVLEVEL", "RUNLEVEL", "INIT_VERSION", "CONSOLE", "PWD", "OLDPWD"]:
            if var in env:
                del env[var]
        popen = subprocess.Popen([SHELL_PATH], env=env, cwd="/", shell=False, close_fds=True)
        update_utmp_shell(popen.pid)
        popen.wait()
    except:
        pass
    finally:
        update_utmp_console()

if __name__ == '__main__':
    main()
