forked from rosspeoples/python3-pywbem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcim_http.py
432 lines (357 loc) · 15.3 KB
/
cim_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#
# (C) Copyright 2003-2005 Hewlett-Packard Development Company, L.P.
# (C) Copyright 2006-2007 Novell, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; version 2 of the License.
#
# 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Tim Potter <[email protected]>
# Author: Martin Pool <[email protected]>
# Author: Bart Whiteley <[email protected]>
# Author: Ross Peoples <[email protected]>
'''
Send HTTP/HTTPS requests to a WBEM server.
This module does not know anything about the fact that the data being
transferred in the HTTP request and response is CIM-XML. It is up to the
caller to provide CIM-XML formatted input data and interpret the result data
as CIM-XML.
Requires Python 2.7.9+ or Python 3.3+
'''
import base64
import getpass
import os
import platform
import re
import socket
from stat import S_ISSOCK
import six
from six.moves import http_client as httplib
from six.moves.urllib import parse as urllib
from . import cim_obj
if six.PY2:
from M2Crypto import SSL
else:
import ssl as SSL
class Error(Exception):
"""This exception is raised when a transport error occurs."""
pass
class AuthError(Error):
"""This exception is raised when an authentication error (401) occurs."""
pass
def parse_url(url):
"""Return a tuple of (host, port, ssl) from the URL parameter.
The returned port defaults to 5988 if not specified. SSL supports
defaults to False if not specified."""
host = url # Defaults
port = 5988
ssl = False
if re.match("https", url): # Set SSL if specified
ssl = True
port = 5989
m = re.search("^https?://", url) # Eat protocol name
if m:
host = url[len(m.group(0)):]
# IPv6 with/without port
m = re.match("^\[?([0-9A-Fa-f:]*)\]?(:([0-9]*))?$", host)
if m:
host = m.group(1)
port_tmp = m.group(3)
if port_tmp:
port = int(port_tmp)
return host, port, ssl
s = host.split(":") # Set port number
if len(s) != 1:
host = s[0]
port = int(s[1])
return host, port, ssl
def get_default_ca_certs():
"""
Try to find out system path with ca certificates. This path is cached and
returned. If no path is found out, None is returned.
"""
if not hasattr(get_default_ca_certs, '_path'):
for path in (
'/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt',
'/etc/ssl/certs',
'/etc/ssl/certificates'):
if os.path.exists(path):
get_default_ca_certs._path = path
break
else:
get_default_ca_certs._path = None
return get_default_ca_certs._path
def wbem_request(url, data, creds, headers=[], debug=0, x509=None,
verify_callback=None, ca_certs=None,
no_verification=False):
"""
Send an HTTP or HTTPS request to a WBEM server and return the response.
This function uses Python's built-in `httplib` module.
:Parameters:
url : `unicode` or UTF-8 encoded `str`
URL of the WBEM server (e.g. ``"https://10.11.12.13:6988"``).
For details, see the ``url`` parameter of
`WBEMConnection.__init__`.
data : `unicode` or UTF-8 encoded `str`
The CIM-XML formatted data to be sent as a request to the WBEM server.
creds
Credentials for authenticating with the WBEM server.
For details, see the ``creds`` parameter of
`WBEMConnection.__init__`.
headers : list of `unicode` or UTF-8 encoded `str`
List of HTTP header fields to be added to the request, in addition to
the standard header fields such as ``Content-type``,
``Content-length``, and ``Authorization``.
debug : ``bool``
Boolean indicating whether to create debug information.
Not currently used.
x509
Used for HTTPS with certificates.
For details, see the ``x509`` parameter of
`WBEMConnection.__init__`.
verify_callback
Used for HTTPS with certificates.
For details, see the ``verify_callback`` parameter of
`WBEMConnection.__init__`.
ca_certs
Used for HTTPS with certificates.
For details, see the ``ca_certs`` parameter of
`WBEMConnection.__init__`.
no_verification
Used for HTTPS with certificates.
For details, see the ``no_verification`` parameter of
`WBEMConnection.__init__`.
:Returns:
The CIM-XML formatted response data from the WBEM server, as a `unicode`
object.
:Raises:
:raise Error:
:raise AuthError:
"""
class HTTPBaseConnection:
def send(self, s):
""" Same as httplib.HTTPConnection.send(), except we don't
check for sigpipe and close the connection. If the connection
gets closed, getresponse() fails.
"""
if self.sock is None:
if self.auto_open:
self.connect()
else:
raise httplib.NotConnected()
if self.debuglevel > 0:
print("send:", repr(s))
if not isinstance(s, six.binary_type):
s = s.encode('utf-8')
self.sock.sendall(s)
class HTTPConnection(HTTPBaseConnection, httplib.HTTPConnection):
def __init__(self, host, port=None, strict=None):
httplib.HTTPConnection.__init__(self, host, port, strict)
class HTTPSConnection(HTTPBaseConnection, httplib.HTTPSConnection):
def __init__(self, host, port=None, key_file=None, cert_file=None,
strict=None, ca_certs=None, verify_callback=None):
httplib.HTTPSConnection.__init__(self, host, port, key_file,
cert_file, strict)
self.ca_certs = ca_certs
self.verify_callback = verify_callback
def connect(self):
"Connect to a host on a given (SSL) port."
new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_socket.settimeout(self.timeout)
self.sock = SSL.wrap_socket(new_socket)
return self.sock.connect_ex((self.host, self.port))
class FileHTTPConnection(HTTPBaseConnection, httplib.HTTPConnection):
def __init__(self, uds_path):
httplib.HTTPConnection.__init__(self, 'localhost')
self.uds_path = uds_path
def connect(self):
try:
socket_af = socket.AF_UNIX
except AttributeError:
raise Error('file URL not supported on %s platform due ' \
'to missing AF_UNIX support' % platform.system())
self.sock = socket.socket(socket_af, socket.SOCK_STREAM)
self.sock.connect(self.uds_path)
host, port, use_ssl = parse_url(url)
key_file = None
cert_file = None
if use_ssl and x509 is not None:
cert_file = x509.get('cert_file')
key_file = x509.get('key_file')
numTries = 0
localAuthHeader = None
tryLimit = 5
# Make sure the data argument is converted to a UTF-8 encoded str object.
# This is important because according to RFC2616, the Content-Length HTTP
# header must be measured in Bytes (and the Content-Type header will
# indicate UTF-8).
if six.PY2 and isinstance(data, six.text_type):
data = data.encode('utf-8')
data = '<?xml version="1.0" encoding="utf-8" ?>\n' + data
if not no_verification and ca_certs is None:
ca_certs = get_default_ca_certs()
elif no_verification:
ca_certs = None
local = False
if use_ssl:
h = HTTPSConnection(host,
port=port,
key_file=key_file,
cert_file=cert_file,
ca_certs=ca_certs,
verify_callback=verify_callback)
else:
if url.startswith('http'):
h = HTTPConnection(host, port=port)
else:
if url.startswith('file:'):
url = url[5:]
try:
s = os.stat(url)
if S_ISSOCK(s.st_mode):
h = FileHTTPConnection(url)
local = True
else:
raise Error('Invalid URL')
except OSError:
raise Error('Invalid URL')
locallogin = None
if host in ('localhost', 'localhost6', '127.0.0.1', '::1'):
local = True
if local:
try:
locallogin = getpass.getuser()
except (KeyError, ImportError):
locallogin = None
while numTries < tryLimit:
numTries += 1
h.putrequest('POST', '/cimom')
h.putheader('Content-type', 'application/xml; charset="utf-8"')
h.putheader('Content-length', str(len(data)))
if localAuthHeader is not None:
h.putheader(*localAuthHeader)
elif creds is not None:
auth = '%s:%s' % (creds[0], creds[1])
auth64 = base64.b64encode(auth.encode('utf-8')).decode('utf-8').replace('\n', '')
h.putheader('Authorization', 'Basic %s' % auth64)
elif locallogin is not None:
h.putheader('PegasusAuthorization', 'Local "%s"' % locallogin)
for hdr in headers:
if six.PY2 and isinstance(hdr, six.text_type):
hdr = hdr.encode('utf-8')
s = [x.strip() for x in hdr.split(':', 1)]
h.putheader(urllib.quote(s[0]), urllib.quote(s[1]))
try:
# See RFC 2616 section 8.2.2
# An http server is allowed to send back an error (presumably
# a 401), and close the connection without reading the entire
# request. A server may do this to protect itself from a DoS
# attack.
#
# If the server closes the connection during our h.send(), we
# will either get a socket exception 104 (TCP RESET), or a
# socket exception 32 (broken pipe). In either case, thanks
# to our fixed HTTPConnection classes, we'll still be able to
# retrieve the response so that we can read and respond to the
# authentication challenge.
h.endheaders()
try:
h.send(data)
except socket.error as arg:
if arg[0] != 104 and arg[0] != 32:
raise
response = h.getresponse()
body = response.read()
if response.status != 200:
if response.status == 401:
if numTries >= tryLimit:
raise AuthError(response.reason)
if not local:
raise AuthError(response.reason)
authChal = response.getheader('WWW-Authenticate', '')
if 'openwbem' in response.getheader('Server', ''):
if 'OWLocal' not in authChal:
try:
uid = os.getuid()
except AttributeError:
raise Error("OWLocal authorization for " \
"openwbem server not supported on %s " \
"platform due to missing os.getuid()" % \
platform.system())
localAuthHeader = ('Authorization',
'OWLocal uid="%d"' % uid)
continue
else:
try:
nonceIdx = authChal.index('nonce=')
nonceBegin = authChal.index('"', nonceIdx)
nonceEnd = authChal.index('"', nonceBegin + 1)
nonce = authChal[nonceBegin + 1:nonceEnd]
cookieIdx = authChal.index('cookiefile=')
cookieBegin = authChal.index('"', cookieIdx)
cookieEnd = authChal.index('"', cookieBegin + 1)
cookieFile = authChal[cookieBegin + 1:cookieEnd]
f = open(cookieFile, 'r')
cookie = f.read().strip()
f.close()
localAuthHeader = (
'Authorization',
'OWLocal nonce="%s", cookie="%s"' % \
(nonce, cookie))
continue
except:
localAuthHeader = None
continue
elif 'Local' in authChal:
try:
beg = authChal.index('"') + 1
end = authChal.rindex('"')
if end > beg:
file = authChal[beg:end]
fo = open(file, 'r')
cookie = fo.read().strip()
fo.close()
localAuthHeader = (
'PegasusAuthorization',
'Local "%s:%s:%s"' % \
(locallogin, file, cookie))
continue
except ValueError:
pass
raise AuthError(response.reason)
if response.getheader('CIMError', None) is not None and \
response.getheader('PGErrorDetail', None) is not None:
raise Error(
'CIMError: %s: %s' %
(response.getheader('CIMError'),
urllib.unquote(response.getheader('PGErrorDetail'))))
raise Error('HTTP error: %s' % response.reason)
except httplib.BadStatusLine as arg:
raise Error("The web server returned a bad status line: '%s'" % arg)
except socket.error as arg:
raise Error("Socket error: %s" % (arg,))
break
return body
def get_object_header(obj):
"""Return the HTTP header required to make a CIM operation request
using the given object. Return None if the object does not need
to have a header."""
# Local namespacepath
if isinstance(obj, six.string_types):
return 'CIMObject: %s' % obj
# CIMLocalClassPath
if isinstance(obj, cim_obj.CIMClassName):
return 'CIMObject: %s:%s' % (obj.namespace, obj.classname)
# CIMInstanceName with namespace
if isinstance(obj, cim_obj.CIMInstanceName) and obj.namespace is not None:
return 'CIMObject: %s' % obj
raise TypeError('Don\'t know how to generate HTTP headers for %s' % obj)