diff options
Diffstat (limited to 'lang/python/examples/howto')
-rwxr-xr-x | lang/python/examples/howto/export-secret-key.py | 6 | ||||
-rwxr-xr-x | lang/python/examples/howto/export-secret-keys.py | 9 | ||||
-rwxr-xr-x | lang/python/examples/howto/temp-homedir-config.py | 12 |
3 files changed, 15 insertions, 12 deletions
diff --git a/lang/python/examples/howto/export-secret-key.py b/lang/python/examples/howto/export-secret-key.py index eeedb84b..caae0874 100755 --- a/lang/python/examples/howto/export-secret-key.py +++ b/lang/python/examples/howto/export-secret-key.py @@ -35,6 +35,9 @@ This script exports one or more secret keys. The gpg-agent and pinentry are invoked to authorise the export. """) +def open_0o600(path, flags): + return os.open(path, flags, mode=0o600) + c = gpg.Context(armor=True) if len(sys.argv) >= 4: @@ -84,8 +87,7 @@ except: result = c.key_export_secret(pattern=None) if result is not None: - with open(keyfile, "wb") as f: + with open(keyfile, "wb", opener=open_0o600) as f: f.write(result) - os.chmod(keyfile, 0o600) else: pass diff --git a/lang/python/examples/howto/export-secret-keys.py b/lang/python/examples/howto/export-secret-keys.py index 8055e4e3..32a1e4ab 100755 --- a/lang/python/examples/howto/export-secret-keys.py +++ b/lang/python/examples/howto/export-secret-keys.py @@ -37,6 +37,9 @@ file formats, saved in files within the user's GPG home directory. The gpg-agent and pinentry are invoked to authorise the export. """) +def open_0o600(path, flags): + return os.open(path, flags, mode=0o600) + if sys.platform == "win32": gpgconfcmd = "gpgconf.exe --list-dirs homedir" else: @@ -119,15 +122,13 @@ except: b_result = b.key_export_secret(pattern=None) if a_result is not None: - with open(ascfile, "wb") as f: + with open(ascfile, "wb", opener=open_0o600) as f: f.write(a_result) - os.chmod(ascfile, 0o600) else: pass if b_result is not None: - with open(gpgfile, "wb") as f: + with open(gpgfile, "wb", opener=open_0o600) as f: f.write(b_result) - os.chmod(gpgfile, 0o600) else: pass diff --git a/lang/python/examples/howto/temp-homedir-config.py b/lang/python/examples/howto/temp-homedir-config.py index 897d2f9a..0a80b6d2 100755 --- a/lang/python/examples/howto/temp-homedir-config.py +++ b/lang/python/examples/howto/temp-homedir-config.py @@ -112,18 +112,18 @@ else: nh = "{0}/.{1}".format(userdir, new_homedir) +def open_0o600(path, flags): + return os.open(path, flags, mode=0o600) + if os.path.exists(nh) is True: print("The {0} directory already exists.".format(nh)) else: print("Creating the {0} directory.".format(nh)) - os.mkdir(nh) - os.chmod(nh, 0o700) - with open("{0}/{1}".format(nh, "gpg.conf"), "w") as f1: + os.mkdir(nh, 0o700) + with open("{0}/{1}".format(nh, "gpg.conf"), "w", opener=open_0o600) as f1: f1.write(gpgconf) - os.chmod("{0}/{1}".format(nh, "gpg.conf"), 0o600) - with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w") as f2: + with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w", opener=open_0o600) as f2: f2.write(gpgconf) - os.chmod("{0}/{1}".format(nh, "gpg-agent.conf"), 0o600) print("""You may now use the {0} directory as an alternative GPG homedir: gpg --homedir {0} |