2018-08-13 01:15:01 +02:00
|
|
|
# Copyright 2006-2018 Eygene A. Ryabinkin & contributors.
|
2014-09-21 09:15:38 +02:00
|
|
|
#
|
|
|
|
# Module that supports distribution-specific functions.
|
|
|
|
|
|
|
|
import platform
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Each dictionary value is either string or some iterable.
|
2016-09-20 22:31:48 +02:00
|
|
|
#
|
2014-09-21 09:15:38 +02:00
|
|
|
# For the former we will just return the value, for an iterable
|
|
|
|
# we will walk through the values and will return the first
|
|
|
|
# one that corresponds to the existing file.
|
|
|
|
__DEF_OS_LOCATIONS = {
|
|
|
|
'freebsd': '/usr/local/share/certs/ca-root-nss.crt',
|
2014-10-08 04:39:47 +02:00
|
|
|
'openbsd': '/etc/ssl/cert.pem',
|
2014-10-08 08:39:07 +02:00
|
|
|
'dragonfly': '/etc/ssl/cert.pem',
|
2014-09-21 09:15:38 +02:00
|
|
|
'darwin': [
|
2020-08-29 20:01:52 +02:00
|
|
|
# MacPorts, port curl-ca-bundle
|
|
|
|
'/opt/local/share/curl/curl-ca-bundle.crt',
|
|
|
|
# homebrew, package openssl
|
|
|
|
'/usr/local/etc/openssl/cert.pem',
|
2014-09-21 09:15:38 +02:00
|
|
|
],
|
|
|
|
'linux-ubuntu': '/etc/ssl/certs/ca-certificates.crt',
|
|
|
|
'linux-debian': '/etc/ssl/certs/ca-certificates.crt',
|
2016-09-20 22:31:48 +02:00
|
|
|
'linux-gentoo': '/etc/ssl/certs/ca-certificates.crt',
|
2014-09-21 09:15:38 +02:00
|
|
|
'linux-fedora': '/etc/pki/tls/certs/ca-bundle.crt',
|
|
|
|
'linux-redhat': '/etc/pki/tls/certs/ca-bundle.crt',
|
|
|
|
'linux-suse': '/etc/ssl/ca-bundle.pem',
|
2016-11-03 16:11:37 +01:00
|
|
|
'linux-opensuse': '/etc/ssl/ca-bundle.pem',
|
2018-08-10 11:08:20 +02:00
|
|
|
'linux-arch': '/etc/ssl/certs/ca-certificates.crt',
|
2014-09-21 09:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_os_name():
|
|
|
|
"""
|
|
|
|
Finds out OS name. For non-Linux system it will be just a plain
|
|
|
|
OS name (like FreeBSD), for Linux it will be "linux-<distro>",
|
|
|
|
where <distro> is the name of the distribution, as returned by
|
|
|
|
the first component of platform.linux_distribution.
|
|
|
|
|
|
|
|
Return value will be all-lowercase to avoid confusion about
|
|
|
|
proper name capitalisation.
|
|
|
|
|
|
|
|
"""
|
|
|
|
OS = platform.system().lower()
|
|
|
|
|
|
|
|
if OS.startswith('linux'):
|
|
|
|
DISTRO = platform.linux_distribution()[0]
|
|
|
|
if DISTRO:
|
2018-08-17 13:05:15 +02:00
|
|
|
OS = OS + "-%s" % DISTRO.split()[0].lower()
|
2018-08-17 12:27:41 +02:00
|
|
|
if os.path.exists('/etc/arch-release'):
|
|
|
|
OS = "linux-arch"
|
2014-09-21 09:15:38 +02:00
|
|
|
|
|
|
|
return OS
|
|
|
|
|
2020-08-29 20:01:52 +02:00
|
|
|
|
2015-01-18 08:45:46 +01:00
|
|
|
def get_os_sslcertfile_searchpath():
|
|
|
|
"""Returns search path for CA bundle for the current OS.
|
2016-09-20 22:31:48 +02:00
|
|
|
|
2015-01-18 08:45:46 +01:00
|
|
|
We will return an iterable even if configuration has just
|
|
|
|
a single value: it is easier for our callers to be sure
|
|
|
|
that they can iterate over result.
|
|
|
|
|
|
|
|
Returned value of None means that there is no search path
|
|
|
|
at all.
|
|
|
|
"""
|
|
|
|
|
|
|
|
OS = get_os_name()
|
|
|
|
|
|
|
|
l = None
|
|
|
|
if OS in __DEF_OS_LOCATIONS:
|
|
|
|
l = __DEF_OS_LOCATIONS[OS]
|
|
|
|
if not hasattr(l, '__iter__'):
|
2020-08-29 20:01:52 +02:00
|
|
|
l = (l,)
|
2015-01-18 08:45:46 +01:00
|
|
|
return l
|
|
|
|
|
2014-09-21 09:15:38 +02:00
|
|
|
|
|
|
|
def get_os_sslcertfile():
|
|
|
|
"""
|
|
|
|
Finds out the location for the distribution-specific
|
|
|
|
CA certificate file bundle.
|
|
|
|
|
|
|
|
Returns the location of the file or None if there is
|
|
|
|
no known CA certificate file or all known locations
|
|
|
|
correspond to non-existing filesystem objects.
|
|
|
|
"""
|
|
|
|
|
2015-01-18 08:45:46 +01:00
|
|
|
l = get_os_sslcertfile_searchpath()
|
2020-08-30 11:18:18 +02:00
|
|
|
if l is None:
|
2015-01-18 08:45:46 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
for f in l:
|
2020-08-29 20:01:52 +02:00
|
|
|
assert (type(f) == type(""))
|
|
|
|
if os.path.exists(f) and (os.path.isfile(f) or os.path.islink(f)):
|
|
|
|
return f
|
2014-09-21 09:15:38 +02:00
|
|
|
|
|
|
|
return None
|