1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4from __future__ import absolute_import, division, unicode_literals
5
6import gpg
7import os.path
8import requests
9
10# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
11#
12# This program is free software; you can redistribute it and/or modify it under
13# the terms of the GNU General Public License as published by the Free Software
14# Foundation; either version 2 of the License, or (at your option) any later
15# version.
16#
17# This program is free software; you can redistribute it and/or modify it under
18# the terms of the GNU Lesser General Public License as published by the Free
19# Software Foundation; either version 2.1 of the License, or (at your option)
20# any later version.
21#
22# This program is distributed in the hope that it will be useful, but WITHOUT
23# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
25# Lesser General Public License for more details.
26#
27# You should have received a copy of the GNU General Public License and the GNU
28# Lesser General Public along with this program; if not, see
29# <https://www.gnu.org/licenses/>.
30
31print("""
32This script imports one or more public keys from the SKS keyservers.
33""")
34
35c = gpg.Context()
36url = "https://sks-keyservers.net/pks/lookup"
37pattern = input("Enter the pattern to search for key or user IDs: ")
38payload = {"op": "get", "search": pattern}
39
40r = requests.get(url, verify=True, params=payload)
41result = c.key_import(r.content)
42
43if result is not None and hasattr(result, "considered") is False:
44    print(result)
45elif result is not None and hasattr(result, "considered") is True:
46    num_keys = len(result.imports)
47    new_revs = result.new_revocations
48    new_sigs = result.new_signatures
49    new_subs = result.new_sub_keys
50    new_uids = result.new_user_ids
51    new_scrt = result.secret_imported
52    nochange = result.unchanged
53    print("""
54The total number of keys considered for import was:  {0}
55
56   Number of keys revoked:  {1}
57 Number of new signatures:  {2}
58    Number of new subkeys:  {3}
59   Number of new user IDs:  {4}
60Number of new secret keys:  {5}
61 Number of unchanged keys:  {6}
62
63The key IDs for all considered keys were:
64""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt,
65           nochange))
66    for i in range(num_keys):
67        print(result.imports[i].fpr)
68    print("")
69else:
70    pass
71