#!/usr/bin/env python

# VERSION 08-23-2017

# Copyright (C) 2017:
#     Clemenceau Thomas, thomasclem33@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

import os
from os.path import exists
import sys
import base64
import paramiko
import subprocess
import schecks

VERSION = "0.1"

class LocalExec(object):
    def __init__(self):
        pass

    def close(self):
        pass

    def exec_command(self, command):
        cmd = command.encode('utf8', 'ignore')
        try:
            process = subprocess.Popen(
                cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                close_fds=True, shell=True, preexec_fn=os.setsid)
            stdout, stderr = process.communicate()
        except OSError, exp:
            stderr = exp.__str__()
            stdout = stdin = ''
        return '',stdout,stderr

if __name__ == '__main__':

    done = False
    client = LocalExec()
    stdin, stdout, stderr = client.exec_command("cat /var/log/btmp")
    # stderr = stderr.read()
    if "denied" in stderr:
        print "You're not root, try again with the good permissions."
        sys.exit(84)
    while done == False:
        user = raw_input("Please enter your supervision user [shinken]: ")
        if not user:
            user = "shinken"
        command = "groups %s" % user
        stdin, stdout, stderr = client.exec_command(command)
        while "no such user" in stderr:
            user = raw_input("There is no " + user + " user. Please enter another user: ")
            if not user:
                continue
            command = "groups %s" % user
            stdin, stdout, stderr = client.exec_command(command)
        group = raw_input ("Now please enter your supervision group [shinken]: ")
        if not group:
            group = "shinken"
        stdin, stdout, stderr = client.exec_command("cat /etc/group | awk -F: '{print $1}'")
        while not group or not group in stdout:
            group = raw_input("There is no " + group + " group. Please enter another group: ")
        stdin, stdout, stderr = client.exec_command(command)
        while not group in stdout:
            answer = raw_input(user + " is not in the " + group + " group. Do you want to add him to this group [yes]? ")
            if not answer:
                answer = "yes"
            while answer.lower() != "no" and answer.lower() != "yes":
                answer = raw_input("You can only answer \"Yes\" or \"No\": ")
            if answer.lower() == "no":
                group = raw_input("Please enter another group: ")
            else:
                new_command = "usermod -a -G %s %s" % (group, user)
                client.exec_command(new_command)
                stdin, stdout, sderr = client.exec_command(command)
        answer = raw_input("Your user is : " + user + " , your group is : " + group + ". Is it correct [yes] ? ")
        if not answer:
            answer = "yes"
        while answer.lower() != "no" and answer.lower() != "yes":
            answer = raw_input("You can only answer \"Yes\" or \"No\": ")
        if answer.lower() == "no":
            continue
        done = True
        print "Now we modify the logrotate.conf file and the actual log file."
        command = "sed -i -e \"s/create 0600 root utmp/create 0640 root %s/g\" /etc/logrotate.conf ; chmod 640 /var/log/btmp ; chown root:%s /var/log/btmp" % (group, group)
        client.exec_command(command)
        print "Done ! You can now use the check with Shinken."
    client.close()
    sys.exit(0)
