forked from ocdnix/hpisee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpisee.py
266 lines (241 loc) · 10 KB
/
hpisee.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
#!/usr/bin/env python
#
# Simple proof of concept implementation of an HP ISEE (Instant
# Support Enterprise Edition) client. It supports client registration
# and entitlement/warranty lookup.
#
# Usage: $0 serial,prod,[country] [serial,prod,[country] ...]
# Example: $0 CZ10130050,519841-425,ES
#
# Notes:
# * No WSDL available. SOAP protocol forged with lxml+requests.
# * Output is raw XML.
# * Product number is mandatory (unfortunately).
# * Product number must be complete (not only the six first digits),
# but see also examples/warranty-empty_prodno.xml.
# * Country defaults to 'US'. Results return fine, but with a warning
# that the country doesn't match shipping country.
# * The template XML files have been stripped to their bare minimum
# accepted by the server.
#
# OCDnix 2013
# rot13(bpqavk_lnubb.pbz)
# http://ocdnix.wordpress.com/
#
import sys
import json
import requests
from lxml import etree
def reg_timestamp(payload):
"""
Add timestamps to registration payload.
The OSID and CSID timestamps are required for the registration to
succeed.
"""
from time import gmtime, strftime
payload.xpath('/isee:ISEE-Registration/RegistrationSource/' \
'HP_OOSIdentifiers/OSID/Section/' \
'Property[@name="TimestampGenerated"]',
namespaces=reqnsmap)[0].set('value',
strftime("%Y/%m/%d %H:%M:%S %Z", gmtime()))
payload.xpath('/isee:ISEE-Registration/RegistrationSource/' \
'HP_OOSIdentifiers/CSID/Section/' \
'Property[@name="TimestampGenerated"]',
namespaces=reqnsmap)[0].set('value',
strftime("%Y/%m/%d %H:%M:%S %Z", gmtime()))
def reg_addpayload(soapenv, payload):
"""Add registration XML payload to SOAP envelope."""
soapenv.xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/' \
'iseeReg:RegisterClient2/iseeReg:request',
namespaces=reqnsmap)[0].text = etree.tostring(payload)
def reg_getauthdata(soapenv):
"""Get auth gdid and token fram XML payload."""
success = soapenv.xpath('/soap:Envelope/soap:Body/' \
'isee:RegisterClient2Response/' \
'isee:RegisterClient2Result/' \
'isee:IsSuccess',
namespaces=resnsmap)[0].text
if not success.lower() == 'true':
error = soapenv.xpath('/soap:Envelope/soap:Body/' \
'isee:RegisterClient2Response/' \
'isee:RegisterClient2Result/' \
'isee:Error',
namespaces=resnsmap)[0].text
sys.stderr.write(etree.tostring(error, pretty_print=True))
sys.exit(1) # FIXME: Don't exit this deep.
gdid = soapenv.xpath('/soap:Envelope/soap:Body/' \
'isee:RegisterClient2Response/' \
'isee:RegisterClient2Result/' \
'isee:Gdid',
namespaces=resnsmap)[0].text
regtoken = soapenv.xpath('/soap:Envelope/soap:Body/' \
'isee:RegisterClient2Response/' \
'isee:RegisterClient2Result/' \
'isee:RegistrationToken',
namespaces=resnsmap)[0].text
assert len(gdid)
assert len(regtoken)
config['auth']['gdid'] = gdid
config['auth']['regtoken'] = regtoken
authfile = open(config['auth']['file'], 'w')
json.dump({'gdid': gdid, 'regtoken': regtoken}, authfile, indent=4)
authfile.close()
def war_populate(payload):
"""
Add entitlement parameters.
Populate the CountryCode, SerialNumber and ProductNumber fields in
the entitlement info request payload.
"""
for serial, prodno, country in config['entitlements']:
assert len(serial) # Required
#assert len(prodno) # Required
if not country:
# Country mismatch is normally allowed.
country = 'US'
parent = payload.xpath('/isee:ISEE-GetOOSEntitlementInfoRequest',
namespaces=reqnsmap_ent)[0]
entparams = etree.SubElement(parent, 'HP_ISEEEntitlementParameters')
countrycode = etree.SubElement(entparams, 'CountryCode')
serialnumber = etree.SubElement(entparams, 'SerialNumber')
productnumber = etree.SubElement(entparams, 'ProductNumber')
etree.SubElement(entparams, 'EntitlementType') # Required
etree.SubElement(entparams, 'EntitlementId') # Required
etree.SubElement(entparams, 'ObligationId') # Required
countrycode.text = country
serialnumber.text = serial
productnumber.text = prodno
def war_addpayload(soapenv, payload):
"""Add auth and registration XML payload to SOAP envelope."""
soapenv.xpath('/SOAP-ENV:Envelope/SOAP-ENV:Header/'\
'isee:IseeWebServicesHeader/isee:GDID',
namespaces=reqnsmap_war)[0].text = config['auth']['gdid']
soapenv.xpath('/SOAP-ENV:Envelope/SOAP-ENV:Header/'\
'isee:IseeWebServicesHeader/isee:registrationToken',
namespaces=reqnsmap_war)[0].text = config['auth']['regtoken']
soapenv.xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/' \
'isee:GetOOSEntitlementList2/isee:request',
namespaces=reqnsmap_war)[0].text = etree.tostring(payload)
def war_getentdata(soapenv):
"""Handle entitlement XML payload."""
# FIXME: Error handling.
payload = soapenv.xpath('/soap:Envelope/soap:Body/' \
'isee:GetOOSEntitlementList2Response/' \
'isee:GetOOSEntitlementList2Result/' \
'isee:Response',
namespaces=resnsmap)[0].text
print etree.tostring(etree.fromstring(payload), pretty_print=True)
# Keep all config in a large structure.
config = {
'http': {
'host': 'https://services.isee.hp.com',
'user-agent': 'RemoteSupport/A.05.05 - gSOAP/2.7',
'content-type': 'text/xml; charset=utf-8',
},
'ops': {
'register': {
'url': '/ClientRegistration/ClientRegistrationService.asmx',
'soap_action': '"http://www.hp.com/isee/webservices/'\
'RegisterClient2"',
'xml_soapenv': 'templates/register_soapenv.xml',
'xml_payload': 'templates/register_payload.xml',
'hooks_req_payload': [reg_timestamp],
'hooks_req_soapenv': [reg_addpayload],
'hooks_res_soapenv': [reg_getauthdata],
'hooks_res_payload': [],
},
'warranty': {
'url': '/EntitlementCheck/EntitlementCheckService.asmx',
'soap_action': '"http://www.hp.com/isee/webservices/'\
'GetOOSEntitlementList2"',
'xml_soapenv': 'templates/warranty_soapenv.xml',
'xml_payload': 'templates/warranty_payload.xml',
'hooks_req_payload': [war_populate],
'hooks_req_soapenv': [war_addpayload],
'hooks_res_soapenv': [war_getentdata],
'hooks_res_payload': [],
},
},
'auth': {}, # Added in main().
'entitlements': [], # Added in main().
}
# Namespace maps. We use .. four since there are duplicates and
# discrepancies between each type of query, and between the client
# request and server response. (Client might use 'SOAP-ENV' while the
# server uses 'soap'.) This is a bloody mess and should be cleaned up
# properly.
reqnsmap = {
'SOAP-ENV': 'http://schemas.xmlsoap.org/soap/envelope/',
'iseeReg': 'http://www.hp.com/isee/webservices/',
'isee': 'http://www.hp.com/schemas/isee/5.00/event',
}
reqnsmap_ent = {
'SOAP-ENV': 'http://schemas.xmlsoap.org/soap/envelope/',
'isee': 'http://www.hp.com/schemas/isee/5.00/entitlement',
}
reqnsmap_war = {
'SOAP-ENV': 'http://schemas.xmlsoap.org/soap/envelope/',
'isee': 'http://www.hp.com/isee/webservices/',
}
resnsmap = {
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'isee': 'http://www.hp.com/isee/webservices/',
}
def do_request(op):
"""Called with op=register|warranty."""
payload = etree.parse(config['ops'][op]['xml_payload'])
for func in config['ops'][op]['hooks_req_payload']:
func(payload)
soapenv = etree.parse(config['ops'][op]['xml_soapenv'])
for func in config['ops'][op]['hooks_req_soapenv']:
func(soapenv, payload)
# Prepare HTTP transport.
url = config['http']['host'] + config['ops'][op]['url']
headers = {
'User-Agent': config['http']['user-agent'],
'SOAPAction': config['ops'][op]['soap_action'],
'Content-Type': config['http']['content-type'],
}
r = requests.post(url,
data=etree.tostring(soapenv),
headers=headers)
# Handle result.
soapenv = etree.fromstring(r.text.encode('utf-8'))
for func in config['ops'][op]['hooks_res_soapenv']:
func(soapenv)
def main():
import argparse
import os.path
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-a', '--authfile',
help='JSON-formatted auth file',
default=os.path.join(os.path.expanduser('~'), '.hpiseeauth'))
parser.add_argument('entitlements', metavar='ENT', nargs='+',
help='entitlement, format: serial,product,[country]')
args = parser.parse_args()
try:
# Save the file path, so the auth data can be written to it
# later in reg_getauthdata() if needed.
config['auth']['file'] = args.authfile
authfile = open(config['auth']['file'])
authfilep = json.load(authfile)
authfile.close()
assert authfilep.has_key('gdid')
assert authfilep.has_key('regtoken')
assert authfilep['gdid']
assert authfilep['regtoken']
config['auth']['gdid'] = authfilep['gdid']
config['auth']['regtoken'] = authfilep['regtoken']
except:
sys.stderr.write('Registering new client.\n')
do_request('register')
config['entitlements'] = map(lambda ent:
tuple(ent.split(',')), filter(lambda ent:
len(ent.split(',')) == 3, args.entitlements))
assert len(config['entitlements'])
assert config['auth'].has_key('gdid')
assert config['auth'].has_key('regtoken')
sys.stderr.write('Looking up entitlement info.\n')
do_request('warranty')
if __name__ == '__main__':
main()