doc: Fix Python example code.

* lang/python/doc/src/gpgme-python-howto.org: Fix chmod race.
* lang/python/examples/howto/export-secret-key.py: Likewise.
* lang/python/examples/howto/export-secret-keys.py: Likewise.
* lang/python/examples/howto/temp-homedir-config.py: Likewise.

--

It's not for Python 2.7.

GnuPG-bug-id: 6466
Reported-by: Hanno Böck
Co-authored-by: Ingo Klöcker
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2023-04-26 08:46:45 +09:00
parent d99156ff08
commit d44a473e27
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
4 changed files with 24 additions and 15 deletions

View File

@ -1612,6 +1612,7 @@ of the entire public keybox.
#+BEGIN_SRC python -i #+BEGIN_SRC python -i
import gpg import gpg
import os
import os.path import os.path
import sys import sys
@ -1619,6 +1620,9 @@ print("""
This script exports one or more public keys in minimised form. This script exports one or more public keys in minimised form.
""") """)
def open_0o600(path, flags):
return os.open(path, flags, mode=0o600)
c = gpg.Context(armor=True) c = gpg.Context(armor=True)
if len(sys.argv) >= 4: if len(sys.argv) >= 4:
@ -1654,7 +1658,7 @@ except:
result = c.key_export_minimal(pattern=None) result = c.key_export_minimal(pattern=None)
if result is not None: if result is not None:
with open(keyfile, "wb") as f: with open(keyfile, "wb", opener=open_0o600) as f:
f.write(result) f.write(result)
else: else:
pass pass
@ -1686,6 +1690,9 @@ This script exports one or more secret keys.
The gpg-agent and pinentry are invoked to authorise the export. 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) c = gpg.Context(armor=True)
if len(sys.argv) >= 4: if len(sys.argv) >= 4:
@ -1735,9 +1742,8 @@ except:
result = c.key_export_secret(pattern=None) result = c.key_export_secret(pattern=None)
if result is not None: if result is not None:
with open(keyfile, "wb") as f: with open(keyfile, "wb", opener=open_0o600)) as f:
f.write(result) f.write(result)
os.chmod(keyfile, 0o600)
else: else:
pass pass
#+END_SRC #+END_SRC

View File

@ -35,6 +35,9 @@ This script exports one or more secret keys.
The gpg-agent and pinentry are invoked to authorise the export. 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) c = gpg.Context(armor=True)
if len(sys.argv) >= 4: if len(sys.argv) >= 4:
@ -84,8 +87,7 @@ except:
result = c.key_export_secret(pattern=None) result = c.key_export_secret(pattern=None)
if result is not None: if result is not None:
with open(keyfile, "wb") as f: with open(keyfile, "wb", opener=open_0o600) as f:
f.write(result) f.write(result)
os.chmod(keyfile, 0o600)
else: else:
pass pass

View File

@ -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. 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": if sys.platform == "win32":
gpgconfcmd = "gpgconf.exe --list-dirs homedir" gpgconfcmd = "gpgconf.exe --list-dirs homedir"
else: else:
@ -119,15 +122,13 @@ except:
b_result = b.key_export_secret(pattern=None) b_result = b.key_export_secret(pattern=None)
if a_result is not 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) f.write(a_result)
os.chmod(ascfile, 0o600)
else: else:
pass pass
if b_result is not None: 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) f.write(b_result)
os.chmod(gpgfile, 0o600)
else: else:
pass pass

View File

@ -112,18 +112,18 @@ else:
nh = "{0}/.{1}".format(userdir, new_homedir) 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: if os.path.exists(nh) is True:
print("The {0} directory already exists.".format(nh)) print("The {0} directory already exists.".format(nh))
else: else:
print("Creating the {0} directory.".format(nh)) print("Creating the {0} directory.".format(nh))
os.mkdir(nh) os.mkdir(nh, 0o700)
os.chmod(nh, 0o700) with open("{0}/{1}".format(nh, "gpg.conf"), "w", opener=open_0o600) as f1:
with open("{0}/{1}".format(nh, "gpg.conf"), "w") as f1:
f1.write(gpgconf) f1.write(gpgconf)
os.chmod("{0}/{1}".format(nh, "gpg.conf"), 0o600) with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w", opener=open_0o600) as f2:
with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w") as f2:
f2.write(gpgconf) 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: print("""You may now use the {0} directory as an alternative GPG homedir:
gpg --homedir {0} gpg --homedir {0}