2009-04-22 13:35:27 -05:00
|
|
|
# Authors: Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2009-04-22 13:35:27 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-22 13:35:27 -05:00
|
|
|
#
|
|
|
|
|
2015-08-13 01:32:54 -05:00
|
|
|
import collections
|
2009-04-22 13:35:27 -05:00
|
|
|
import xml.dom.minidom
|
2012-08-23 11:38:45 -05:00
|
|
|
|
2010-02-02 21:52:11 -06:00
|
|
|
import nss.nss as nss
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
2016-08-24 06:37:30 -05:00
|
|
|
# pylint: disable=import-error
|
2015-09-14 05:52:29 -05:00
|
|
|
from six.moves.urllib.parse import urlencode
|
2016-08-24 06:37:30 -05:00
|
|
|
# pylint: enable=import-error
|
2012-07-04 07:52:47 -05:00
|
|
|
|
|
|
|
from ipalib import api, errors
|
2014-03-18 10:23:30 -05:00
|
|
|
from ipalib.errors import NetworkError
|
2012-07-04 07:52:47 -05:00
|
|
|
from ipalib.text import _
|
2012-08-23 11:38:45 -05:00
|
|
|
from ipapython import nsslib, ipautil
|
2015-12-16 12:04:20 -06:00
|
|
|
from ipapython.ipa_log_manager import root_logger
|
2012-08-23 11:38:45 -05:00
|
|
|
|
2015-09-14 07:03:58 -05:00
|
|
|
# Python 3 rename. The package is available in "six.moves.http_client", but
|
|
|
|
# pylint cannot handle classes from that alias
|
|
|
|
try:
|
|
|
|
import httplib
|
|
|
|
except ImportError:
|
2016-08-24 06:37:30 -05:00
|
|
|
# pylint: disable=import-error
|
2015-09-14 07:03:58 -05:00
|
|
|
import http.client as httplib
|
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2015-08-13 01:32:54 -05:00
|
|
|
Profile = collections.namedtuple('Profile', ['profile_id', 'description', 'store_issued'])
|
|
|
|
|
2015-05-11 20:17:48 -05:00
|
|
|
INCLUDED_PROFILES = {
|
2015-08-13 01:32:54 -05:00
|
|
|
Profile(u'caIPAserviceCert', u'Standard profile for network services', True),
|
|
|
|
Profile(u'IECUserRoles', u'User profile that includes IECUserRoles extension from request', True),
|
2016-07-26 10:19:01 -05:00
|
|
|
Profile(u'KDCs_PKINIT_Certs',
|
|
|
|
u'Profile for PKINIT support by KDCs',
|
|
|
|
False),
|
2015-05-11 20:17:48 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 01:23:24 -05:00
|
|
|
DEFAULT_PROFILE = u'caIPAserviceCert'
|
2016-07-26 10:19:01 -05:00
|
|
|
KDC_PROFILE = u'KDCs_PKINIT_Certs'
|
2015-05-08 01:23:24 -05:00
|
|
|
|
2009-04-22 13:35:27 -05:00
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
def error_from_xml(doc, message_template):
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("Error")
|
|
|
|
reason = item_node[0].childNodes[0].data
|
|
|
|
return errors.RemoteRetrieveError(reason=reason)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2012-09-25 08:57:03 -05:00
|
|
|
return errors.RemoteRetrieveError(reason=message_template % e)
|
|
|
|
|
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
def get_ca_certchain(ca_host=None):
|
2009-04-22 13:35:27 -05:00
|
|
|
"""
|
|
|
|
Retrieve the CA Certificate chain from the configured Dogtag server.
|
|
|
|
"""
|
2009-07-10 15:18:16 -05:00
|
|
|
if ca_host is None:
|
|
|
|
ca_host = api.env.ca_host
|
2009-04-22 13:35:27 -05:00
|
|
|
chain = None
|
2014-03-18 10:23:30 -05:00
|
|
|
conn = httplib.HTTPConnection(
|
|
|
|
ca_host,
|
2015-11-09 11:28:47 -06:00
|
|
|
api.env.ca_install_port or 8080)
|
2009-04-22 13:35:27 -05:00
|
|
|
conn.request("GET", "/ca/ee/ca/getCertChain")
|
|
|
|
res = conn.getresponse()
|
2010-11-22 09:27:34 -06:00
|
|
|
doc = None
|
2009-04-22 13:35:27 -05:00
|
|
|
if res.status == 200:
|
|
|
|
data = res.read()
|
|
|
|
conn.close()
|
2009-05-21 16:34:00 -05:00
|
|
|
try:
|
|
|
|
doc = xml.dom.minidom.parseString(data)
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("ChainBase64")
|
|
|
|
chain = item_node[0].childNodes[0].data
|
|
|
|
except IndexError:
|
2012-09-25 08:57:03 -05:00
|
|
|
raise error_from_xml(
|
|
|
|
doc, _("Retrieving CA cert chain failed: %s"))
|
2009-05-21 16:34:00 -05:00
|
|
|
finally:
|
2010-11-22 09:27:34 -06:00
|
|
|
if doc:
|
|
|
|
doc.unlink()
|
|
|
|
else:
|
2012-07-04 07:52:47 -05:00
|
|
|
raise errors.RemoteRetrieveError(
|
|
|
|
reason=_("request failed with HTTP status %d") % res.status)
|
2009-04-22 13:35:27 -05:00
|
|
|
|
|
|
|
return chain
|
2010-02-02 21:52:11 -06:00
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
|
2014-11-18 12:49:15 -06:00
|
|
|
def _parse_ca_status(body):
|
|
|
|
doc = xml.dom.minidom.parseString(body)
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("XMLResponse")[0]
|
|
|
|
item_node = item_node.getElementsByTagName("Status")[0]
|
|
|
|
return item_node.childNodes[0].data
|
|
|
|
except IndexError:
|
|
|
|
raise error_from_xml(doc, _("Retrieving CA status failed: %s"))
|
|
|
|
|
|
|
|
|
2016-01-20 01:35:15 -06:00
|
|
|
def ca_status(ca_host=None):
|
2012-09-25 08:57:03 -05:00
|
|
|
"""Return the status of the CA, and the httpd proxy in front of it
|
|
|
|
|
|
|
|
The returned status can be:
|
|
|
|
- running
|
|
|
|
- starting
|
|
|
|
- Service Temporarily Unavailable
|
|
|
|
"""
|
|
|
|
if ca_host is None:
|
|
|
|
ca_host = api.env.ca_host
|
2016-09-26 07:08:17 -05:00
|
|
|
status, _headers, body = http_request(
|
2016-01-20 01:35:15 -06:00
|
|
|
ca_host, 8080, '/ca/admin/ca/getStatus')
|
2012-09-25 08:57:03 -05:00
|
|
|
if status == 503:
|
|
|
|
# Service temporarily unavailable
|
2016-01-05 21:50:42 -06:00
|
|
|
return status
|
2012-09-25 08:57:03 -05:00
|
|
|
elif status != 200:
|
|
|
|
raise errors.RemoteRetrieveError(
|
2016-01-05 21:50:42 -06:00
|
|
|
reason=_("Retrieving CA status failed with status %d") % status)
|
2014-11-18 12:49:15 -06:00
|
|
|
return _parse_ca_status(body)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
|
2015-04-30 03:55:29 -05:00
|
|
|
def https_request(host, port, url, secdir, password, nickname,
|
|
|
|
method='POST', headers=None, body=None, **kw):
|
2010-02-02 21:52:11 -06:00
|
|
|
"""
|
2015-04-30 03:55:29 -05:00
|
|
|
:param method: HTTP request method (defalut: 'POST')
|
2012-09-25 08:57:03 -05:00
|
|
|
:param url: The path (not complete URL!) to post to.
|
2015-04-30 03:55:29 -05:00
|
|
|
:param body: The request body (encodes kw if None)
|
2010-02-02 21:52:11 -06:00
|
|
|
:param kw: Keyword arguments to encode into POST body.
|
2016-01-05 21:50:42 -06:00
|
|
|
:return: (http_status, http_headers, http_body)
|
|
|
|
as (integer, dict, str)
|
2010-02-02 21:52:11 -06:00
|
|
|
|
|
|
|
Perform a client authenticated HTTPS request
|
|
|
|
"""
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
def connection_factory(host, port):
|
2015-11-22 19:09:32 -06:00
|
|
|
no_init = secdir == nsslib.current_dbdir
|
|
|
|
conn = nsslib.NSSConnection(host, port, dbdir=secdir, no_init=no_init,
|
2014-10-30 10:52:14 -05:00
|
|
|
tls_version_min=api.env.tls_version_min,
|
|
|
|
tls_version_max=api.env.tls_version_max)
|
2011-06-29 14:01:18 -05:00
|
|
|
conn.set_debuglevel(0)
|
|
|
|
conn.connect()
|
2012-09-25 08:57:03 -05:00
|
|
|
conn.sock.set_client_auth_data_callback(
|
|
|
|
nsslib.client_auth_data_callback,
|
|
|
|
nickname, password, nss.get_default_certdb())
|
|
|
|
return conn
|
|
|
|
|
2015-04-30 03:55:29 -05:00
|
|
|
if body is None:
|
|
|
|
body = urlencode(kw)
|
2012-09-25 08:57:03 -05:00
|
|
|
return _httplib_request(
|
2015-04-30 03:55:29 -05:00
|
|
|
'https', host, port, url, connection_factory, body,
|
|
|
|
method=method, headers=headers)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
def http_request(host, port, url, **kw):
|
|
|
|
"""
|
|
|
|
:param url: The path (not complete URL!) to post to.
|
|
|
|
:param kw: Keyword arguments to encode into POST body.
|
2016-01-05 21:50:42 -06:00
|
|
|
:return: (http_status, http_headers, http_body)
|
|
|
|
as (integer, dict, str)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
Perform an HTTP request.
|
|
|
|
"""
|
|
|
|
body = urlencode(kw)
|
|
|
|
return _httplib_request(
|
|
|
|
'http', host, port, url, httplib.HTTPConnection, body)
|
|
|
|
|
|
|
|
|
|
|
|
def _httplib_request(
|
2015-04-30 03:55:29 -05:00
|
|
|
protocol, host, port, path, connection_factory, request_body,
|
|
|
|
method='POST', headers=None):
|
2012-09-25 08:57:03 -05:00
|
|
|
"""
|
|
|
|
:param request_body: Request body
|
|
|
|
:param connection_factory: Connection class to use. Will be called
|
|
|
|
with the host and port arguments.
|
2015-04-30 03:55:29 -05:00
|
|
|
:param method: HTTP request method (default: 'POST')
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
Perform a HTTP(s) request.
|
|
|
|
"""
|
2017-01-10 11:21:13 -06:00
|
|
|
uri = u'%s://%s%s' % (protocol, ipautil.format_netloc(host, port), path)
|
2015-11-19 22:39:00 -06:00
|
|
|
root_logger.debug('request %s %s', method, uri)
|
2012-09-25 08:57:03 -05:00
|
|
|
root_logger.debug('request body %r', request_body)
|
2015-04-30 03:55:29 -05:00
|
|
|
|
|
|
|
headers = headers or {}
|
|
|
|
if (
|
|
|
|
method == 'POST'
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
and 'content-type' not in (str(k).lower() for k in headers)
|
2015-04-30 03:55:29 -05:00
|
|
|
):
|
|
|
|
headers['content-type'] = 'application/x-www-form-urlencoded'
|
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
try:
|
|
|
|
conn = connection_factory(host, port)
|
2015-04-30 03:55:29 -05:00
|
|
|
conn.request(method, uri, body=request_body, headers=headers)
|
2010-02-02 21:52:11 -06:00
|
|
|
res = conn.getresponse()
|
|
|
|
|
|
|
|
http_status = res.status
|
2017-01-10 11:24:16 -06:00
|
|
|
http_headers = res.msg
|
2010-02-02 21:52:11 -06:00
|
|
|
http_body = res.read()
|
|
|
|
conn.close()
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2017-01-12 09:20:43 -06:00
|
|
|
root_logger.exception("httplib request failed:")
|
2010-02-02 21:52:11 -06:00
|
|
|
raise NetworkError(uri=uri, error=str(e))
|
|
|
|
|
2016-01-05 21:50:42 -06:00
|
|
|
root_logger.debug('response status %d', http_status)
|
2015-11-19 22:39:00 -06:00
|
|
|
root_logger.debug('response headers %s', http_headers)
|
|
|
|
root_logger.debug('response body %r', http_body)
|
2010-02-02 21:52:11 -06:00
|
|
|
|
2016-01-05 21:50:42 -06:00
|
|
|
return http_status, http_headers, http_body
|