summaryrefslogtreecommitdiffstats
path: root/src/plugins/abrt_rh_support.c
blob: 3ffa54bedcb6d65b47553ce7cc5c0d61eeea48a2 (plain)
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
458
459
460
/*
    Copyright (C) 2010  ABRT team
    Copyright (C) 2010  RedHat Inc

    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 2 of the License, or
    (at your option) any later version.

    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 along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <curl/curl.h>
#include "abrtlib.h"
#include "abrt_curl.h"
#include "abrt_rh_support.h"

struct reportfile {
    xmlTextWriterPtr writer;
    xmlBufferPtr     buf;
};

static void __attribute__((__noreturn__))
die_xml_oom(void)
{
    error_msg_and_die("can't create XML attribute (out of memory?)");
}

static xmlBufferPtr
xxmlBufferCreate(void)
{
    xmlBufferPtr r = xmlBufferCreate();
    if (!r)
        die_xml_oom();
    return r;
}

static xmlTextWriterPtr
xxmlNewTextWriterMemory(xmlBufferPtr buf /*, int compression*/)
{
    xmlTextWriterPtr r = xmlNewTextWriterMemory(buf, /*compression:*/ 0);
    if (!r)
        die_xml_oom();
    return r;
}

static void
xxmlTextWriterStartDocument(xmlTextWriterPtr writer,
    const char * version,
    const char * encoding,
    const char * standalone)
{
    if (xmlTextWriterStartDocument(writer, version, encoding, standalone) < 0)
        die_xml_oom();
}

static void
xxmlTextWriterEndDocument(xmlTextWriterPtr writer)
{
    if (xmlTextWriterEndDocument(writer) < 0)
        die_xml_oom();
}

static void
xxmlTextWriterStartElement(xmlTextWriterPtr writer, const char *name)
{
    // these bright guys REDEFINED CHAR (!) to unsigned char...
    if (xmlTextWriterStartElement(writer, (unsigned char*)name) < 0)
        die_xml_oom();
}

static void
xxmlTextWriterEndElement(xmlTextWriterPtr writer)
{
    if (xmlTextWriterEndElement(writer) < 0)
        die_xml_oom();
}

static void
xxmlTextWriterWriteElement(xmlTextWriterPtr writer, const char *name, const char *content)
{
    if (xmlTextWriterWriteElement(writer, (unsigned char*)name, (unsigned char*)content) < 0)
        die_xml_oom();
}

static void
xxmlTextWriterWriteAttribute(xmlTextWriterPtr writer, const char *name, const char *content)
{
    if (xmlTextWriterWriteAttribute(writer, (unsigned char*)name, (unsigned char*)content) < 0)
        die_xml_oom();
}

#if 0 //unused
static void
xxmlTextWriterWriteString(xmlTextWriterPtr writer, const char *content)
{
    if (xmlTextWriterWriteString(writer, (unsigned char*)content) < 0)
        die_xml_oom();
}
#endif

//
// End the reportfile, and prepare it for delivery.
// No more bindings can be added after this.
//
static void
close_writer(reportfile_t* file)
{
    if (!file->writer)
        return;

    // close off the end of the xml file
    xxmlTextWriterEndDocument(file->writer);
    xmlFreeTextWriter(file->writer);
    file->writer = NULL;
}

//
// This allocates a reportfile_t structure and initializes it.
//
reportfile_t*
new_reportfile(void)
{
    // create a new reportfile_t
    reportfile_t* file = (reportfile_t*)xmalloc(sizeof(*file));

    // set up a libxml 'buffer' and 'writer' to that buffer
    file->buf = xxmlBufferCreate();
    file->writer = xxmlNewTextWriterMemory(file->buf);

    // start a new xml document:
    // <report xmlns="http://www.redhat.com/gss/strata">...
    xxmlTextWriterStartDocument(file->writer, /*version:*/ NULL, /*encoding:*/ NULL, /*standalone:*/ NULL);
    xxmlTextWriterStartElement(file->writer, "report");
    xxmlTextWriterWriteAttribute(file->writer, "xmlns", "http://www.redhat.com/gss/strata");

    return file;
}

static void
internal_reportfile_start_binding(reportfile_t* file, const char* name, int isbinary, const char* filename)
{
    // <binding name=NAME [fileName=FILENAME] type=text/binary...
    xxmlTextWriterStartElement(file->writer, "binding");
    xxmlTextWriterWriteAttribute(file->writer, "name", name);
    if (filename)
        xxmlTextWriterWriteAttribute(file->writer, "fileName", filename);
    if (isbinary)
        xxmlTextWriterWriteAttribute(file->writer, "type", "binary");
    else
        xxmlTextWriterWriteAttribute(file->writer, "type", "text");
}

//
// Add a new text binding
//
void
reportfile_add_binding_from_string(reportfile_t* file, const char* name, const char* value)
{
    // <binding name=NAME type=text value=VALUE>
    internal_reportfile_start_binding(file, name, /*isbinary:*/ 0, /*filename:*/ NULL);
    xxmlTextWriterWriteAttribute(file->writer, "value", value);
    xxmlTextWriterEndElement(file->writer);
}

//
// Add a new binding to a report whose value is represented as a file.
//
void
reportfile_add_binding_from_namedfile(reportfile_t* file,
                const char* on_disk_filename, /* unused so far */
                const char* binding_name,
                const char* recorded_filename,
                int isbinary)
{
    // <binding name=NAME fileName=FILENAME type=text/binary...
    internal_reportfile_start_binding(file, binding_name, isbinary, recorded_filename);
    // ... href=content/NAME>
    char *href_name = concat_path_file("content", binding_name);
    xxmlTextWriterWriteAttribute(file->writer, "href", href_name);
    free(href_name);
}

//
// Return the contents of the reportfile as a string.
//
const char*
reportfile_as_string(reportfile_t* file)
{
    close_writer(file);
    // unsigned char -> char
    return (char*)file->buf->content;
}

void
reportfile_free(reportfile_t* file)
{
    if (!file)
        return;
    close_writer(file);
    xmlBufferFree(file->buf);
    free(file);
}


//
// send_report_to_new_case()
//

static char*
make_case_data(const char* summary, const char* description,
               const char* product, const char* version,
               const char* component)
{
    char* retval;
    xmlTextWriterPtr writer;
    xmlBufferPtr buf;

    buf = xxmlBufferCreate();
    writer = xxmlNewTextWriterMemory(buf);

    xxmlTextWriterStartDocument(writer, NULL, "UTF-8", "yes");
    xxmlTextWriterStartElement(writer, "case");
    xxmlTextWriterWriteAttribute(writer, "xmlns",
                                   "http://www.redhat.com/gss/strata");

    xxmlTextWriterWriteElement(writer, "summary", summary);
    xxmlTextWriterWriteElement(writer, "description", description);
    if (product) {
        xxmlTextWriterWriteElement(writer, "product", product);
    }
    if (version) {
        xxmlTextWriterWriteElement(writer, "version", version);
    }
    if (component) {
        xxmlTextWriterWriteElement(writer, "component", component);
    }

    xxmlTextWriterEndDocument(writer);
    retval = xstrdup((const char*)buf->content);
    xmlFreeTextWriter(writer);
    xmlBufferFree(buf);
    return retval;
}

#if 0 //unused
static char*
make_response(const char* title, const char* body,
              const char* actualURL, const char* displayURL)
{
    char* retval;
    xmlTextWriterPtr writer;
    xmlBufferPtr buf;

    buf = xxmlBufferCreate();
    writer = xxmlNewTextWriterMemory(buf);

    xxmlTextWriterStartDocument(writer, NULL, "UTF-8", "yes");
    xxmlTextWriterStartElement(writer, "response");
    if (title) {
        xxmlTextWriterWriteElement(writer, "title", title);
    }
    if (body) {
        xxmlTextWriterWriteElement(writer, "body", body);
    }
    if (actualURL || displayURL) {
        xxmlTextWriterStartElement(writer, "URL");
        if (actualURL) {
            xxmlTextWriterWriteAttribute(writer, "href", actualURL);
        }
        if (displayURL) {
            xxmlTextWriterWriteString(writer, displayURL);
        }
    }

    xxmlTextWriterEndDocument(writer);
    retval = xstrdup((const char*)buf->content);
    xmlFreeTextWriter(writer);
    xmlBufferFree(buf);
    return retval;
}
//Example:
//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
//<response><title>Case Created and Report Attached</title><body></body><URL href="http://support-services-devel.gss.redhat.com:8080/Strata/cases/00005129/attachments/ccbf3e65-b941-3db7-a016-6a3831691a32">New Case URL</URL></response>
#endif

char*
send_report_to_new_case(const char* baseURL,
                const char* username,
                const char* password,
                bool ssl_verify,
                const char* summary,
                const char* description,
                const char* component,
                const char* report_file_name)
{
    char *case_url = concat_path_file(baseURL, "/cases");

    char *case_data = make_case_data(summary, description,
                                         "Red Hat Enterprise Linux", "6.0",
                                         component);

    int redirect_count = 0;
    char *errmsg;
    char *allocated = NULL;
    char* retval = NULL;
    abrt_post_state_t *case_state;

 redirect_case:
    case_state = new_abrt_post_state(0
            + ABRT_POST_WANT_HEADERS
            + ABRT_POST_WANT_BODY
            + ABRT_POST_WANT_ERROR_MSG
            + (ssl_verify ? ABRT_POST_WANT_SSL_VERIFY : 0)
    );
    case_state->username = username;
    case_state->password = password;

    static const char *headers[] = {
        "Accept: text/plain",
        NULL
    };

    abrt_post_string(case_state, case_url, "application/xml", headers, case_data);

    char *case_location = find_header_in_abrt_post_state(case_state, "Location:");
    switch (case_state->http_resp_code)
    {
    case 404:
        /* Not strictly necessary (default branch would deal with it too),
         * but makes this typical error less cryptic:
         * instead of returning html-encoded body, we show short concise message,
         * and show offending URL (typos in which is a typical cause) */
        retval = xasprintf("error in case creation, "
                        "HTTP code: 404 (Not found), URL:'%s'", case_url);
        break;

    case 301: /* "301 Moved Permanently" (for example, used to move http:// to https://) */
    case 302: /* "302 Found" (just in case) */
    case 305: /* "305 Use Proxy" */
        if (++redirect_count < 10 && case_location)
        {
            free(case_url);
            case_url = xstrdup(case_location);
            free_abrt_post_state(case_state);
            goto redirect_case;
        }
        /* fall through */

    default:
        errmsg = case_state->curl_error_msg;
        if (errmsg && errmsg[0])
            retval = xasprintf("error in case creation: %s", errmsg);
        else
        {
            errmsg = case_state->body;
            if (errmsg && errmsg[0])
                retval = xasprintf("error in case creation, HTTP code: %d, server says: '%s'",
                        case_state->http_resp_code, errmsg);
            else
                retval = xasprintf("error in case creation, HTTP code: %d",
                        case_state->http_resp_code);
        }
        break;

    case 200:
    case 201: {
        if (!case_location) {
            /* Case Creation returned valid code, but no location */
            retval = xasprintf("error in case creation: no Location URL, HTTP code: %d",
                    case_state->http_resp_code);
            break;
        }

        char *atch_url = concat_path_file(case_location, "/attachments");
        abrt_post_state_t *atch_state;
 redirect_attach:
        atch_state = new_abrt_post_state(0
                + ABRT_POST_WANT_HEADERS
                + ABRT_POST_WANT_BODY
                + ABRT_POST_WANT_ERROR_MSG
                + (ssl_verify ? ABRT_POST_WANT_SSL_VERIFY : 0)
        );
        atch_state->username = username;
        atch_state->password = password;

        abrt_post_file_as_form(atch_state, atch_url, "application/binary", headers,
                               report_file_name);

        char *atch_location = find_header_in_abrt_post_state(atch_state, "Location:");
        switch (atch_state->http_resp_code)
        {
        case 305: /* "305 Use Proxy" */
            if (++redirect_count < 10 && atch_location)
            {
                free(atch_url);
                atch_url = xstrdup(atch_location);
                free_abrt_post_state(atch_state);
                goto redirect_attach;
            }
            /* fall through */

        default:
            /* Case Creation Succeeded, attachement FAILED */
            errmsg = atch_state->curl_error_msg;
            if (atch_state->body && atch_state->body[0])
            {
                if (errmsg && errmsg[0]
                 && strcmp(errmsg, atch_state->body) != 0
                ) /* both strata/curl error and body are present (and aren't the same) */
                    allocated = errmsg = xasprintf("%s. %s",
                            atch_state->body,
                            errmsg);
                else /* only body exists */
                    errmsg = atch_state->body;
            }
            /* Note: to prevent URL misparsing, make sure to delimit
             * case_location only using spaces */
            retval = xasprintf("Case created: %s but report attachment failed (HTTP code %d)%s%s",
                    case_location,
                    atch_state->http_resp_code,
                    errmsg ? ": " : "",
                    errmsg ? errmsg : ""
            );
            break;

        case 200:
        case 201:
            // unused
            //char *body = atch_state->body;
            //if (case_state->body && case_state->body[0])
            //{
            //    body = case_state->body;
            //    if (atch_state->body && atch_state->body[0])
            //        allocated = body = xasprintf("%s\n%s",
            //                case_state->body,
            //                atch_state->body);
            //}
            retval = xasprintf("Case created: %s", /*body,*/ case_location);
        } /* switch (attach HTTP code) */

        free_abrt_post_state(atch_state);
        free(atch_url);
    } /* case 200/201 */

    } /* switch (case HTTP code) */

    free_abrt_post_state(case_state);
    free(allocated);
    free(case_url);
    return retval;
}