Skip to content

Commit 5f7c10b

Browse files
authored
Handle raw emails as UID value in keys (#151)
UIDs usually follow the form of `Name <[email protected]>`, but they can be just raw emails (`[email protected]`), so support that. Fixes #111 Signed-off-by: Phil Dibowitz <[email protected]>
1 parent a91b15e commit 5f7c10b

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

libpius/signer.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,22 @@ def get_uids(self, key):
298298
# For the normal case (have email), we'll be storing each email
299299
# twice but that's OK since it means that email is *always* a valid
300300
# email or None and id is *always* a valid identifier
301-
match = re.search(".* <(.*)>", uid)
301+
match = re.search("^.* <(.*)>$", uid)
302302
if match:
303303
email = match.group(1)
304304
PiusUtil.debug("got email %s" % email)
305305
filename = re.sub("@", "_at_", email)
306306
filename = "%s__%s" % (key, filename)
307307
uid = email
308+
# It is possible for the UID to be JUST a raw email address, so
309+
# if that appears to be the case, use that
310+
elif re.match(
311+
"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", uid
312+
):
313+
email = uid
314+
PiusUtil.debug("got email %s" % email)
315+
filename = re.sub("@", "_at_", email)
316+
filename = "%s__%s" % (key, filename)
308317
else:
309318
# but if it doesn't have an email, do the right thing
310319
email = None
@@ -442,7 +451,7 @@ def encrypt_signed_uid(self, key, filename):
442451

443452
def _run_and_check_status(self, cmd, shell=False):
444453
"""Helper function for running a gpg call that requires no input
445-
but that we want to make sure succeeded."""
454+
but that we want to make sure succeeded."""
446455
PiusUtil.logcmd(cmd)
447456
gpg = subprocess.Popen(
448457
cmd,
@@ -495,7 +504,7 @@ def clean_clean_key(self, key):
495504

496505
def import_clean_key(self, key):
497506
"""Import the clean key we exported in export_clean_key() to our temp
498-
keyring."""
507+
keyring."""
499508
# Import the export of our public key and the given public key
500509
for x in [self.signer, key]:
501510
PiusUtil.debug("importing %s" % x)
@@ -690,7 +699,7 @@ def sign_all_uids(self, key, level):
690699
uids = self.get_uids(key)
691700
print(
692701
" There %s %s UID%s on this key to sign"
693-
% (["is", "are"][len(uids) != 1], len(uids), "s"[len(uids) == 1:])
702+
% (["is", "are"][len(uids) != 1], len(uids), "s"[len(uids) == 1 :])
694703
)
695704

696705
# From the user key ring make a clean copy
@@ -812,7 +821,7 @@ def import_unsigned_keys(self):
812821
def encrypt_and_sign_file(self, infile, outfile, keyid):
813822
"""Encrypt and sign a file.
814823
815-
Used for PGP/Mime email generation."""
824+
Used for PGP/Mime email generation."""
816825
cmd = (
817826
[self.gpg]
818827
+ GPG_BASE_OPTS

libpius/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def debug(line):
2727
print("DEBUG:", line)
2828

2929
def logcmd(cmd):
30-
PiusUtil.debug("Running: %s" % " ".join(cmd))
30+
outcmd = " ".join(cmd) if type(cmd) == list else cmd
31+
PiusUtil.debug("Running: %s" % outcmd)
3132

3233
def clean_files(flist):
3334
"""Delete a list of files."""

0 commit comments

Comments
 (0)