#coding:utf-8

import os
import sys
import stat
import time
import base64
import sunhpc
from sunhpc.core.utils import SafeError
class Command(sunhpc.commands.create.security.command):
    """
    Generate the root rsa and sshd rsa, dsa, ecdsa ...

    <params type='Bool' name='force'>
    Force overwrite old files.
    </params>

    <params type='Bool' name='quiet'>
    quiet mode.
    </params>

    <example cmd='create security sshd'>
    Generate the crypted.
    </example>
    """
    def run(self, parms, args):

        (force, quiet) = self.fillParams([('force', 'no'), ('quiet', 'yes')])

        force = self.str2bool(force)
        quiet = self.str2bool(quiet)

        safedirs = '/etc/safe.d'
        src_dirs = '/etc/safe-security'
        filelist = ['ssh_host_rsa_key', 'ssh_host_ecdsa_key', 'ssh_host_ed25519_key']

        valid_list = []
        for i in filelist:
            filename = os.path.join(src_dirs, i)
            if force and os.path.exists(filename):
                os.remove(filename)
                os.system("ssh-keygen -q -t rsa -f %s -C '' -N ''" % filename)

            if not os.path.exists(filename):
                os.system("ssh-keygen -q -t rsa -f %s -C '' -N ''" % filename)

            os.system("chmod 0640 %s" % filename)
            os.system("chmod 0644 %s.pub" % filename)
            os.system("chown root:ssh_keys %s" % filename)


            key = (filename, "/etc/ssh/%s" % i)
            pub = ("%s.pub" % filename, "/etc/ssh/%s.pub" % i)
            valid_list.extend([key, pub])
            

        for s, d in valid_list:
            self.makeEncrypt(s, d, safedirs, quiet)

RollName = "base"
