+++ /dev/null
-#!/usr/bin/python3
-###############################################################################
-# #
-# Pakfire - The IPFire package management system #
-# Copyright (C) 2011 Pakfire development team #
-# #
-# 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, either version 3 of the License, or #
-# (at your option) any later version. #
-# #
-# 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, see <http://www.gnu.org/licenses/>. #
-# #
-###############################################################################
-
-import argparse
-import datetime
-import logging
-import os
-import shutil
-import signal
-import sys
-
-from . import _pakfire
-from . import client
-from . import daemon
-
-from .constants import *
-from .i18n import _
-
-class Cli(object):
- default_path = "/"
-
-
-class CliKey(Cli):
- def parse_cli(self):
- parser = argparse.ArgumentParser(
- description = _("Pakfire key command line interface"),
- )
- subparsers = parser.add_subparsers()
-
- # Add common arguments
- self._add_common_arguments(parser)
-
- # delete
- delete = subparsers.add_parser("delete", help=_("Delete a key from the local keyring"))
- delete.add_argument("fingerprint", nargs="+", help=_("The fingerprint of the key to delete"))
- delete.set_defaults(func=self.handle_delete)
-
- # export
- export = subparsers.add_parser("export", help=_("Export a key to a file"))
- export.add_argument("fingerprint", nargs=1,
- help=_("The fingerprint of the key to export"))
- export.add_argument("--filename", nargs="*", help=_("Write the key to this file"))
- export.add_argument("--secret", action="store_true",
- help=_("Export the secret key"))
- export.set_defaults(func=self.handle_export)
-
- # import
- _import = subparsers.add_parser("import", help=_("Import a key from file"))
- _import.add_argument("filename", nargs="+", help=_("Filename of that key to import"))
- _import.set_defaults(func=self.handle_import)
-
- # generate
- generate = subparsers.add_parser("generate", help=_("Import a key from file"))
- generate.add_argument("--realname", nargs=1,
- help=_("The real name of the owner of this key"))
- generate.add_argument("--email", nargs=1,
- help=_("The email address of the owner of this key"))
- generate.set_defaults(func=self.handle_generate)
-
- # list
- list = subparsers.add_parser("list", help=_("List all imported keys"))
- list.set_defaults(func=self.handle_list)
-
- return parser.parse_args()
-
- def parse_command_sign(self):
- # Implement the "sign" command.
- sub_sign = self.sub_commands.add_parser("sign",
- help=_("Sign one or more packages."))
- sub_sign.add_argument("--key", "-k", nargs=1,
- help=_("Key that is used sign the package(s)."))
- sub_sign.add_argument("package", nargs="+",
- help=_("Package(s) to sign."))
- sub_sign.add_argument("action", action="store_const", const="sign")
-
- def parse_command_verify(self):
- # Implement the "verify" command.
- sub_verify = self.sub_commands.add_parser("verify",
- help=_("Verify one or more packages."))
- #sub_verify.add_argument("--key", "-k", nargs=1,
- # help=_("Key that is used verify the package(s)."))
- sub_verify.add_argument("package", nargs="+",
- help=_("Package(s) to verify."))
- sub_verify.add_argument("action", action="store_const", const="verify")
-
- def handle_generate(self, ns):
- p = self.pakfire(ns)
-
- print(_("Generating the key may take a moment..."))
- print()
-
- key = p.generate_key("%s <%s>" % (ns.realname.pop(), ns.email.pop()))
-
- def handle_list(self, ns):
- p = self.pakfire(ns)
-
- for key in p.keys:
- print(key)
-
- def handle_export(self, ns):
- p = self.pakfire(ns)
-
- for fingerprint in ns.fingerprint:
- key = p.get_key(fingerprint)
- if not key:
- print(_("Could not find key with fingerprint %s") % fingerprint)
- continue
-
- data = key.export(ns.secret)
-
- for file in ns.filename:
- with open(file, "w") as f:
- f.write(data)
- else:
- print(data)
-
- def handle_import(self, ns):
- p = self.pakfire(ns)
-
- for filename in ns.filename:
- with open(filename) as f:
- data = f.read()
-
- keys = p.import_key(data)
- for key in keys:
- print(key)
-
- def handle_delete(self, ns):
- p = self.pakfire(ns)
-
- for fingerprint in ns.fingerprint:
- key = p.get_key(fingerprint)
- if key:
- key.delete()
-
- def handle_sign(self):
- # Get the files from the command line options
- files = []
-
- for file in self.args.package:
- # Check, if we got a regular file
- if os.path.exists(file):
- file = os.path.abspath(file)
- files.append(file)
-
- else:
- raise FileNotFoundError(file)
-
- key = self.args.key[0]
-
- # Create pakfire instance.
- p = self.create_pakfire()
-
- for file in files:
- # Open the package.
- pkg = packages.open(p, None, file)
-
- print(_("Signing %s...") % pkg.friendly_name)
- pkg.sign(key)
-
- def handle_verify(self):
- # TODO needs to use new key code from libpakfire
-
- # Get the files from the command line options
- files = []
-
- for file in self.args.package:
- # Check, if we got a regular file
- if os.path.exists(file) and not os.path.isdir(file):
- file = os.path.abspath(file)
- files.append(file)
-
- # Create pakfire instance.
- p = self.create_pakfire()
-
- for file in files:
- # Open the package.
- pkg = packages.open(p, None, file)
-
- print(_("Verifying %s...") % pkg.friendly_name)
- sigs = pkg.verify()
-
- for sig in sigs:
- key = p.keyring.get_key(sig.fpr)
- if key:
- subkey = key.subkeys[0]
-
- print(" %s %s" % (subkey.fpr[-16:], key.uids[0].uid))
- if sig.validity:
- print(" %s" % _("This signature is valid."))
-
- else:
- print(" %s <%s>" % (sig.fpr, _("Unknown key")))
- print(" %s" % _("Could not check if this signature is valid."))
-
- created = datetime.datetime.fromtimestamp(sig.timestamp)
- print(" %s" % _("Created: %s") % created)
-
- if sig.exp_timestamp:
- expires = datetime.datetime.fromtimestamp(sig.exp_timestamp)
- print(" %s" % _("Expires: %s") % expires)
-
- print() # Empty line