Exemplo n.º 1
0
    def run(self):
        '''Parse the various arguments and output passphrases for each label

        Each positional argument is a label. For each label, it will try to
        find (via `FileHandler.labletup`) if it is in the labelfile so other
        settings for the label can be applied. If it is not found, the default
        settings object defined as `dispass.dispass.settings` will be used. The
        parameters can be overridden through the various optargs.
        '''
        if self.parentFlags['file']:
            lf = Filehandler(settings, file_location=self.parentFlags['file'])
        else:
            lf = Filehandler(settings)

        if not lf.file_found:
            if not lf.promptForCreation(silent=self.flags['silent']):
                return 1

        if not self.args or self.flags['help']:
            print(self.usage)
            return 1

        algo = None
        length = None
        seqno = None

        if self.flags['algo']:
            if self.flags['algo'] in algorithms:
                algo = self.flags['algo']
        if self.flags['length']:
            try:
                length = int(self.flags['length'])
            except ValueError:
                print('Error: length argument must be a number')
                return 1
        if self.flags['seqno']:
            seqno = self.flags['seqno']

        console = CLI(lf)
        console.verifyPassword = self.flags['verify']

        if self.flags['password']:
            password = self.flags['password']
        else:
            password = console.passwordPrompt()

        for arg in self.args:
            labeltup = lf.labeltup(arg)
            if labeltup:
                console.generate(password, (arg, length or labeltup[1],
                                            algo or labeltup[2],
                                            seqno or labeltup[3],
                                            False))
            else:
                console.generate(password, (
                    arg, length or settings.passphrase_length,
                    algo or settings.algorithm,
                    seqno or settings.sequence_number,
                    False))
        del password

        if self.flags['stdout']:
            console.useCurses = False
            console.scriptableIO = True

        if not console.output():
            print('Error: could not generate keys')
            return 1
        else:
            return 0
Exemplo n.º 2
0
    def run(self):
        '''Parse labels and add them using `FileHandler.add`.

        When the -i flag is passed, add them using `InteractiveEditor.add`
        '''

        newlabels = []
        '''A list of labelnames that have been added.'''

        if self.parentFlags['file']:
            lf = Filehandler(settings, file_location=self.parentFlags['file'])
        else:
            lf = Filehandler(settings)

        if not lf.file_found:
            if not lf.promptForCreation(silent=self.flags['silent']):
                return 1

        if self.flags['interactive']:
            intedit = InteractiveEditor(settings, lf, interactive=False)
            newlabels.append(intedit.add())
        else:
            if not self.args or self.flags['help']:
                print(self.usage)
                return

            for arg in set(self.args):
                labelspec = arg.split(':')
                params = len(labelspec)

                length = 0
                try:
                    length = params >= 2 and int(labelspec[1])
                except ValueError:
                    pass

                if not length:
                    length = settings.passphrase_length

                algo = params >= 3 and labelspec[2] or settings.algorithm
                if not algo in algos.algorithms:
                    algo = settings.algorithm

                seqno = 0
                if algo != 'dispass1':
                    try:
                        seqno = params >= 4 and int(labelspec[3])
                    except ValueError:
                        pass

                if not seqno:
                    seqno = settings.sequence_number

                if lf.add(labelname=labelspec[0], length=length, algo=algo,
                          seqno=seqno):
                    newlabels.append(labelspec[0])
                    if not self.flags['silent']:
                        print("Label '{name}' saved".format(name=labelspec[0]))
                else:
                    if not self.flags['silent']:
                        print("Label '{name}' already exists in labelfile"
                              .format(name=labelspec[0]))

            if not self.flags['dry-run']:
                lf.save()

        if self.flags['generate'] and newlabels:
            console = CLI(lf)
            console.verifyPassword = True
            password = console.passwordPrompt()

            for label in newlabels:
                console.generate(password, lf.labeltup(label))
            del password

            if not console.output():
                if not self.flags['silent']:
                    print('Error: could not generate passphrase')
                return 1
            else:
                return 0