#!/usr/libexec/platform-python

# Copyright (C) 2011 Oracle. All rights reserved.
#
# 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, version 2.  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., 59 Temple Place - Suite 330, Boston, MA
# 021110-1307, USA.

### BEGIN PLUGIN INFO
# name: authentication
# configure: 90
# cleanup: 10
# description: Script to configure template authentication.
### END PLUGIN INFO

import json

from templateconfig.cli import main
from templateconfig.common import run_cmd, set_password


def do_enumerate(target):
    param = []
    if target == 'configure':
        param += [{'key': 'com.oracle.linux.root-password',
                   'description': 'System root password.',
                   'password': True,
                   'required': True}]
    return json.dumps(param)


def do_configure(param):
    param = json.loads(param)
    root_password = param.get('com.oracle.linux.root-password')
    if root_password is not None:
        set_password('root', root_password)
    return json.dumps(param)


def do_cleanup(param):
    param = json.loads(param)
    run_cmd(['passwd', '-d', 'root'])
    return json.dumps(param)


if __name__ == '__main__':
    main(do_enumerate, {'configure': do_configure, 'cleanup': do_cleanup})
