-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindCIMXMLExport.c
457 lines (406 loc) · 12.1 KB
/
indCIMXMLExport.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* $Id: indCIMXMLExport.c,v 1.14 2010/01/21 21:50:37 mchasal Exp $
*
* © Copyright IBM Corp. 2005, 2007
*
* THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
* ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
* CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
*
* You can obtain a current copy of the Eclipse Public License from
* http://www.opensource.org/licenses/eclipse-1.0.php
*
* Author: Adrian Schuur <[email protected]>
*
* Description:
*
* CIM XML export support for indications.
*
*/
#include <curl/curl.h>
#include <sfcCommon/utilft.h>
#include "trace.h"
#include <stdlib.h>
#include <string.h>
#include "control.h"
extern UtilStringBuffer *newStringBuffer(int);
extern int getControlChars(char *id, char **val);
// These are the constant headers added to all requests
static const char *headers[] = {
"Content-type: application/xml; charset=\"utf-8\"",
"Connection: Keep-Alive, TE",
"CIMProtocolVersion: 1.0",
"CIMExport: MethodRequest",
"CIMExportMethod: ExportIndication"
};
#define NUM_HEADERS ((sizeof(headers))/(sizeof(headers[0])))
//
// NOTE:
//
// All strings passed to libcurl are not copied so they must have a
// lifetime
// at least as long as the last function call made on the curl handle. The
// only exception to this seems to be strings passed to the
// curl_slist_append
// function which are copied.
//
typedef struct curlData {
// The handle to the curl object
CURL *mHandle;
// The list of headers sent with each request
struct curl_slist *mHeaders;
// The body of the request
UtilStringBuffer *mBody;
// The uri of the request
UtilStringBuffer *mUri;
// The username/password used in authentication
char *mUserPass;
// Used to store the HTTP response
UtilStringBuffer *mResponse;
/*
* bool supportsSSL(); void genRequest(URL &url, char *op, bool
* cls=false, bool keys=false); void addPayload(const string& pl);
* string getResponse();
*
* // Initializes the HTTP headers void initializeHeaders();
*/
} CurlData;
static void
init(CurlData * cd)
{
cd->mHandle = curl_easy_init();
cd->mHeaders = NULL;
cd->mResponse = NULL;
cd->mBody = newStringBuffer(4096);
cd->mUserPass = NULL;
}
static void
uninit(CurlData * cd)
{
if (cd->mHandle)
curl_easy_cleanup(cd->mHandle);
if (cd->mHeaders)
curl_slist_free_all(cd->mHeaders);
cd->mBody->ft->release(cd->mBody);
if (cd->mResponse)
cd->mResponse->ft->release(cd->mResponse);
}
static int
supportsSSL()
{
#if LIBCURL_VERSION_NUM >= 0x071000
static curl_version_info_data *version = NULL;
if (version == NULL) {
version = curl_version_info(CURLVERSION_NOW);
}
if (version && (version->features & CURL_VERSION_SSL))
return 1;
else
return 0;
#else
// Assume we support SSL if we don't have the curl_version_info API
return 1;
#endif
}
static void
initializeHeaders(CurlData * cd)
{
unsigned int i;
// Free any pre-existing headers
if (cd->mHeaders) {
curl_slist_free_all(cd->mHeaders);
cd->mHeaders = NULL;
}
// Add all of the common headers
for (i = 0; i < NUM_HEADERS; i++)
cd->mHeaders = curl_slist_append(cd->mHeaders, headers[i]);
}
static size_t
writeCb(void *ptr, size_t size, size_t nmemb, void *stream)
{
UtilStringBuffer *sb = (UtilStringBuffer *) stream;
unsigned int length = 0;
unsigned long long calcLength = (unsigned long) size * nmemb;
if (calcLength > UINT_MAX) {
mlogf(M_ERROR, M_SHOW,
"--- Cannot allocate for %d members of size $d\n", nmemb, size);
return 0;
}
length = calcLength & UINT_MAX;
char c = ((char *) ptr)[length];
((char *) ptr)[length] = 0;
sb->ft->appendChars(sb, (char *) ptr);
((char *) ptr)[length] = c;
return length;
}
static int
genRequest(CurlData * cd, char *url, char **msg)
{
CURLcode rv;
char *fnc,
*fnk,
*fnt,
*fnl;
*msg = NULL;
if (!cd->mHandle) {
*msg = strdup("Unable to initialize curl interface.");
return 1;
}
if (!supportsSSL() && (strncasecmp(url, "https:", 6) == 0)) {
*msg = strdup("This curl library does not support https urls.");
return 2;
}
/*
* Initialize curl with the url
*/
rv = curl_easy_setopt(cd->mHandle, CURLOPT_URL, url);
/*
* Disable progress output
*/
rv = curl_easy_setopt(cd->mHandle, CURLOPT_NOPROGRESS, 1);
/*
* This will be a HTTP post
*/
rv = curl_easy_setopt(cd->mHandle, CURLOPT_POST, 1);
/*
* Enable endpoint cert verification as required
*/
getControlChars("sslIndicationReceiverCert", &fnl);
for(;;) {
if (strcasecmp(fnl, "ignore") == 0) {
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSL_VERIFYPEER, 0);
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSL_VERIFYHOST, 0);
break;
} else if ((strcasecmp(fnl, "verify") == 0) ||
(strcasecmp(fnl, "verifyhostname") == 0)) {
if (getControlChars("sslClientTrustStore", &fnt) == 0) {
rv = curl_easy_setopt(cd->mHandle, CURLOPT_CAINFO, fnt);
} else {
/* possible? */
*msg=strdup("Cannot determine value of sslClientTrustStore parameter.");
return 3;
}
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSL_VERIFYPEER, 1);
if (strcasecmp(fnl, "verify") == 0) {
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSL_VERIFYHOST, 0);
break;
} else { /* verifyhostname */
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSL_VERIFYHOST, 2);
break;
}
} else {
// Since we don't know user intent in this case, assume the strictest.
mlogf(M_ERROR,M_SHOW,
"--- ERROR: Invalid value for sslIndicationReceiverCert, setting to: verifyhostname.\n");
fnl = "verifyhostname";
}
}
/*
* set up client side cert usage
*/
if ((getControlChars("sslCertificateFilePath", &fnc) == 0) &&
(getControlChars("sslKeyFilePath", &fnk) == 0)) {
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSLKEY, fnk);
rv = curl_easy_setopt(cd->mHandle, CURLOPT_SSLCERT, fnc);
} else {
*msg =
strdup
("Failed to get cert path and/or key file information for client side cert usage.");
return 3;
}
/* Set transfer timeout to config option or 10 sec */
long indicationCurlTimeout=10;
if (getControlNum("indicationCurlTimeout", &indicationCurlTimeout))
indicationCurlTimeout = 10;
rv = curl_easy_setopt(cd->mHandle, CURLOPT_TIMEOUT, indicationCurlTimeout);
/*
* Set username and password * / if (url.user.length() > 0 &&
* url.password.length() > 0) { mUserPass = url.user + ":" +
* url.password; rv = curl_easy_setopt(mHandle, CURLOPT_USERPWD,
* mUserPass.c_str()); }
*/
// Initialize default headers
initializeHeaders(cd);
// Set all of the headers for the request
rv = curl_easy_setopt(cd->mHandle, CURLOPT_HTTPHEADER, cd->mHeaders);
// Set up the callbacks to store the response
rv = curl_easy_setopt(cd->mHandle, CURLOPT_WRITEFUNCTION, writeCb);
// Use CURLOPT_FILE instead of CURLOPT_WRITEDATA - more portable
cd->mResponse = newStringBuffer(4096);
rv = curl_easy_setopt(cd->mHandle, CURLOPT_FILE, cd->mResponse);
// Fail if we receive an error (HTTP response code >= 300)
rv = curl_easy_setopt(cd->mHandle, CURLOPT_FAILONERROR, 1);
rv = curl_easy_setopt(cd->mHandle, CURLOPT_NOSIGNAL, 1);
char *curldebug = getenv("CURLDEBUG");
if (curldebug && strcasecmp(curldebug,"false"))
rv = curl_easy_setopt(cd->mHandle, CURLOPT_VERBOSE, 1);
if (rv) {
*msg = strdup("Some curl opts failed during setup");
return 2;
}
return 0;
}
char *curlMsgs[] = {
"OK",
"Unsupported protocol. This build of curl has no support for this protocol",
"Failed to initialize.",
"URL malformat. The syntax was not correct.",
"URL user malformatted. The user-part of the URL syntax was not correct.",
"Couldn’t resolve proxy. The given proxy host could not be resolved.",
"Couldn’t resolve host. The given remote host was not resolved.",
/*
* 7
*/ "Failed to connect to host.",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
/*
* 22
*/ "HTTP page not retrieved. The requested url was not found or returned another "
"error with the HTTP error code being 400 or above.",
"Write error. Curl couldn’t write data to a local filesystem or similar."
};
static char *
getErrorMessage(CURLcode err)
{
char error[4096];
#if LIBCURL_VERSION_NUM >= 0x071200
// error = curl_easy_strerror(err);
snprintf(error, 4095, "CURL error: %d (%s)", err,
curl_easy_strerror(err));
#else
if ((err > 0 && err < 8) || (err >= 22 && err <= 23))
snprintf(error, 4095, "CURL error: %d (%s)", err, curlMsgs[err]);
else
snprintf(error, 4095, "CURL error: %d", err);
#endif
return strdup(error);
}
static int
getResponse(CurlData * cd, char **msg)
{
CURLcode rv;
int rc = 0;
rv = curl_easy_perform(cd->mHandle);
if (rv) {
long responseCode = -1;
char *error;
// Use CURLINFO_HTTP_CODE instead of CURLINFO_RESPONSE_CODE
// (more portable to older versions of curl)
curl_easy_getinfo(cd->mHandle, CURLINFO_HTTP_CODE, &responseCode);
switch(responseCode) {
case 200:
rc = 0; /* HTTP 200 is OK. set rc to 0 */
break;
case 400:
*msg = strdup("Bad Request");
rc = 400;
break;
case 401:
error = (cd->mUserPass) ? "Invalid username/password." :
"Username/password required.";
*msg = strdup(error);
rc = 401;
break;
case 501:
*msg = strdup("Not Implemented");
rc = 501;
break;
default:
if (responseCode != 0) {
// If we got an unrecognized response code, return it
rc = (int)responseCode;
} else {
// Otherwise there was some other curl error, return that
rc = (int)rv;
}
*msg = getErrorMessage(rv);
break;
}
return rc;
}
if (cd->mResponse->ft->getSize(cd->mResponse) == 0) {
rc = 5;
*msg = strdup("No data received from server.");
}
// if (dumpXml)
// cerr << "From server: " << response << endl;
return 0;
}
static int
addPayload(CurlData * cd, char *pl, char **msg)
{
CURLcode rv;
cd->mBody->ft->appendChars(cd->mBody, pl);
rv = curl_easy_setopt(cd->mHandle, CURLOPT_POSTFIELDS,
cd->mBody->ft->getCharPtr(cd->mBody));
if (rv) {
*msg = getErrorMessage(rv);
return 6;
}
else {
rv = curl_easy_setopt(cd->mHandle, CURLOPT_POSTFIELDSIZE,
cd->mBody->ft->getSize(cd->mBody));
if (rv) {
*msg = getErrorMessage(rv);
return 7;
}
}
return 0;
}
int
exportIndication(char *url, char *payload, char **resp, char **msg)
{
CurlData cd;
int rc = 0;
FILE *out;
*msg = NULL;
*resp = NULL;
_SFCB_ENTER(TRACE_INDPROVIDER, "exportIndication");
if (strncasecmp(url, "file://", 7) == 0) {
out = fopen(url + 7, "a+");
if (out) {
fprintf(out, "%s\n", payload);
fprintf(out, "=========== End of Indication ===========\n");
fclose(out);
} else {
rc = 1;
mlogf(M_ERROR, M_SHOW,
"Unable to open file to process indication: %s\n", url);
_SFCB_TRACE(1, ("--- Unable to open file: %s", url));
}
_SFCB_RETURN(rc);
}
init(&cd);
if ((rc = genRequest(&cd, url, msg)) == 0) {
if ((rc = addPayload(&cd, payload, msg)) == 0) {
if ((rc = getResponse(&cd, msg)) == 0) {
*resp = strdup(cd.mResponse->ft->getCharPtr(cd.mResponse));
}
}
}
_SFCB_TRACE(1, ("--- url: %s rc: %d %s", url, rc, *msg));
if (rc) {
mlogf(M_ERROR, M_SHOW,
"Problem processing indication to %s. sfcb rc: %d %s\n", url, rc,
*msg);
}
uninit(&cd);
_SFCB_RETURN(rc);
}
/* MODELINES */
/* DO NOT EDIT BELOW THIS COMMENT */
/* Modelines are added by 'make pretty' */
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* vi:set ts=2 sts=2 sw=2 expandtab: */