Example #1
0
    def _do_command(self):
        """
        Executes the command.
        """
        f = open(self.options.sourcefile, 'r')
        json_buf = f.read()
        imported_config = config.ConfigBuilder().build_config(json_buf)
        c = config.ConfigBuilder().dump_config(imported_config)

        crypto.write_file(self.options.config, c, self.passphrase)
Example #2
0
 def _do_command(self):
     if self.options.name:
         if self.config.has_profile(self.options.name):
             self.config.remove_profile(self.options.name)
             c = config.ConfigBuilder().dump_config(self.config)
             crypto.write_file(self.options.config, c, self.passphrase)
             print(_("Profile %s removed" % self.options.name))
         else:
             print(_("ERROR: No such profile: %s") % self.options.name)
             sys.exit(1)
     elif self.options.all:
         self.config.clear_profiles()
         c = config.ConfigBuilder().dump_config(self.config)
         crypto.write_file(self.options.config, c, self.passphrase)
         print(_("All network profiles removed"))
Example #3
0
    def _do_command(self):
        a = self.config.get_auth(self.options.name)

        if not a:
            print(_("Auth %s does not exist.") % self.options.name)
            sys.exit(1)

        if self.options.username:
            a.username = self.options.username

        if self.options.password:
            a.password = get_password(a.username, RHO_AUTH_PASSWORD)

        if self.options.filename:

            sshkey = _read_key_file(self.options.filename)

            if a.type == config.SSH_TYPE:
                cred = config.SshKeyAuth({
                    "name": a.name,
                    "key": sshkey,
                    "username": a.username,
                    "password": a.password,
                    "type": "ssh_key"
                })
                # remove the old ssh, and new key type
                self.config.remove_auth(self.options.name)
                self.config.add_auth(cred)

            elif a.type == config.SSH_KEY_TYPE:
                a.key = sshkey

        c = config.ConfigBuilder().dump_config(self.config)
        crypto.write_file(self.options.config, c, self.passphrase)
        print(_("Auth %s updated" % self.options.name))
Example #4
0
    def _do_command(self):
        if self.options.name:
            self.config.remove_auth(self.options.name)
        elif self.options.all:
            self.config.clear_auths()

        c = config.ConfigBuilder().dump_config(self.config)
        crypto.write_file(self.options.config, c, self.passphrase)
Example #5
0
    def _read_config(self, filename, password):
        """
        Read config file and decrypt with the given password.

        Note that password here is the password provided by the user, not the
        actual salted AES key.
        """
        if os.path.exists(filename):
            try:
                confstr = crypto.read_file(filename, password)
            except crypto.DecryptionException:
                print self.parser.error(
                    _("Error decrypting configuration file"))

            try:
                return config.ConfigBuilder().build_config(confstr)
            except config.BadJsonException:
                print self.parser.error(
                    _("Cannot parse configuration, check encryption password"))

        else:
            print _("Creating new config file: %s" % filename)
            return config.Config()
Example #6
0
class ProfileAddCommand(CliCommand):
    def __init__(self):
        usage = _("usage: %prog profile add [options]")
        shortdesc = _("add a network profile")
        desc = _("add a network profile")

        CliCommand.__init__(self, "profile add", usage, shortdesc, desc)

        self.parser.add_option("--name",
                               dest="name",
                               metavar="NAME",
                               help=_("NAME of the profile - REQUIRED"))
        self.parser.add_option(
            "--range",
            dest="ranges",
            action="append",
            metavar="RANGE",
            default=[],
            help=_("IP range to scan. See 'man rho' for supported formats."))

        self.parser.add_option(
            "--ports",
            dest="ports",
            metavar="PORTS",
            help=_("list of ssh ports to try i.e. '22, 2222, 5402'")),
        self.parser.add_option("--auth",
                               dest="auth",
                               metavar="AUTH",
                               action="append",
                               default=[],
                               help=_("auth class to associate with profile"))

        self.parser.set_defaults(ports="22")

    def _validate_options(self):
        CliCommand._validate_options(self)

        if self.options.ranges:
            self._validate_ranges(self.options.ranges)

        if not self.options.name:
            self.parser.print_help()
            sys.exit(1)

    def _do_command(self):
        ports = []
        if self.options.ports:
            ports = self.options.ports.strip().split(",")
            self._validate_ports(ports)

        auth_names = []
        for auth in self.options.auth:
            for a in auth.strip().split(","):
                auth_names.append(a)

        # unfortunately can't valid these in _validate_options
        # as we don't have a config at that point
        for auth in self.options.auth:
            for a in auth.strip().split(","):
                try:
                    self.config.get_auth(a)
                except config.NoSuchAuthError, e:
                    print _("ERROR: No such auth: %s") % e.authname
                    sys.exit(1)

        g = config.Profile(name=self.options.name,
                           ranges=self.options.ranges,
                           auth_names=auth_names,
                           ports=ports)
        self.config.add_profile(g)
        c = config.ConfigBuilder().dump_config(self.config)
        crypto.write_file(self.options.config, c, self.passphrase)
Example #7
0
 def _save_cred(self, cred):
     self.config.add_auth(cred)
     c = config.ConfigBuilder().dump_config(self.config)
     crypto.write_file(self.options.config, c, self.passphrase)