aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py
diff options
context:
space:
mode:
authorshmel1k <shmel1k@ydb.tech>2023-11-26 18:16:14 +0300
committershmel1k <shmel1k@ydb.tech>2023-11-26 18:43:30 +0300
commitb8cf9e88f4c5c64d9406af533d8948deb050d695 (patch)
tree218eb61fb3c3b96ec08b4d8cdfef383104a87d63 /contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py
parent523f645a83a0ec97a0332dbc3863bb354c92a328 (diff)
downloadydb-b8cf9e88f4c5c64d9406af533d8948deb050d695.tar.gz
add kikimr_configure
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py b/contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py
new file mode 100644
index 0000000000..20051fc89f
--- /dev/null
+++ b/contrib/python/Twisted/py3/twisted/conch/openssh_compat/factory.py
@@ -0,0 +1,74 @@
+# -*- test-case-name: twisted.conch.test.test_openssh_compat -*-
+# Copyright (c) Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+"""
+Factory for reading openssh configuration files: public keys, private keys, and
+moduli file.
+"""
+
+import errno
+import os
+from typing import Dict, List, Optional, Tuple
+
+from twisted.conch.openssh_compat import primes
+from twisted.conch.ssh import common, factory, keys
+from twisted.python.util import runAsEffectiveUser
+
+
+class OpenSSHFactory(factory.SSHFactory):
+ dataRoot = "/usr/local/etc"
+ # For openbsd which puts moduli in a different directory from keys.
+ moduliRoot = "/usr/local/etc"
+
+ def getPublicKeys(self):
+ """
+ Return the server public keys.
+ """
+ ks = {}
+ for filename in os.listdir(self.dataRoot):
+ if filename[:9] == "ssh_host_" and filename[-8:] == "_key.pub":
+ try:
+ k = keys.Key.fromFile(os.path.join(self.dataRoot, filename))
+ t = common.getNS(k.blob())[0]
+ ks[t] = k
+ except Exception as e:
+ self._log.error(
+ "bad public key file {filename}: {error}",
+ filename=filename,
+ error=e,
+ )
+ return ks
+
+ def getPrivateKeys(self):
+ """
+ Return the server private keys.
+ """
+ privateKeys = {}
+ for filename in os.listdir(self.dataRoot):
+ if filename[:9] == "ssh_host_" and filename[-4:] == "_key":
+ fullPath = os.path.join(self.dataRoot, filename)
+ try:
+ key = keys.Key.fromFile(fullPath)
+ except OSError as e:
+ if e.errno == errno.EACCES:
+ # Not allowed, let's switch to root
+ key = runAsEffectiveUser(0, 0, keys.Key.fromFile, fullPath)
+ privateKeys[key.sshType()] = key
+ else:
+ raise
+ except Exception as e:
+ self._log.error(
+ "bad public key file {filename}: {error}",
+ filename=filename,
+ error=e,
+ )
+ else:
+ privateKeys[key.sshType()] = key
+ return privateKeys
+
+ def getPrimes(self) -> Optional[Dict[int, List[Tuple[int, int]]]]:
+ try:
+ return primes.parseModuliFile(self.moduliRoot + "/moduli")
+ except OSError:
+ return None