summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/Bugzilla.cpp
blob: 5dc4298a7d02803414229a3c0b4b2a54ca6c3dea (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#include "abrtlib.h"
#include "abrt_xmlrpc.h"
#include "Bugzilla.h"
#include "CrashTypes.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#define XML_RPC_SUFFIX      "/xmlrpc.cgi"
#define NEW_BUG             -1
#define MISSING_MEMBER      -2

/*
 *  TODO: npajkovs: better deallocation of xmlrpc value
 */

/*
 * Static namespace for xmlrpc stuff.
 * Used mainly to ensure we always destroy xmlrpc client and server_info.
 */

namespace {

struct ctx: public abrt_xmlrpc_conn {
    ctx(const char* url, bool no_ssl_verify): abrt_xmlrpc_conn(url, no_ssl_verify) {}

    void login(const char* login, const char* passwd);
    void logout();
    int32_t check_uuid_in_bugzilla(const char* component, const char* UUID);
    bool check_cc_and_reporter(uint32_t bug_id, const char* login);
    void add_plus_one_cc(uint32_t bug_id, const char* login);
    uint32_t new_bug(const map_crash_data_t& pCrashData);
    void add_attachments(const char* bug_id_str, const map_crash_data_t& pCrashData);
};

void ctx::login(const char* login, const char* passwd)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value* param = xmlrpc_build_value(&env, "({s:s,s:s})", "login", login, "password", passwd);
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result = NULL;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "User.login", param, &result);
    xmlrpc_DECREF(param);
    if (result)
        xmlrpc_DECREF(result);

    if (env.fault_occurred)
    {
        std::string errmsg = ssprintf("Can't login. Check Edit->Plugins->Bugzilla and /etc/abrt/plugins/Bugzilla.conf. Server said: %s", env.fault_string);
        xmlrpc_env_clean(&env);
        error_msg("%s", errmsg.c_str()); // show error in daemon log
        throw CABRTException(EXCEP_PLUGIN, errmsg.c_str());
    }
}

void ctx::logout()
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value* param = xmlrpc_build_value(&env, "(s)", "");
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result = NULL;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "User.logout", param, &result);
    xmlrpc_DECREF(param);
    if (result)
        xmlrpc_DECREF(result);
    if (env.fault_occurred)
        throw_xml_fault(&env);
}

bool ctx::check_cc_and_reporter(uint32_t bug_id, const char* login)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    // fails only when you write query. when it's done it never fails.
    xmlrpc_value* param = xmlrpc_build_value(&env, "(s)", to_string(bug_id).c_str());
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result = NULL;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "bugzilla.getBug", param, &result);
    // we don't need anymore xml structure for calling xmlrpc query(calls only once)
    xmlrpc_DECREF(param);
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* reporter_member = NULL;
    xmlrpc_struct_find_value(&env, result, "reporter", &reporter_member);
    if (env.fault_occurred)
    {
        xmlrpc_DECREF(result);
        throw_xml_fault(&env);
    }

    if (reporter_member)
    {
        const char* reporter = NULL;
        xmlrpc_read_string(&env, reporter_member, &reporter);
        if (env.fault_occurred)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(reporter_member);
            throw_xml_fault(&env);
        }

        bool eq = (strcmp(reporter, login) == 0);
        free((void*)reporter);
        xmlrpc_DECREF(reporter_member);
        if (eq)
        {
            xmlrpc_DECREF(result);
            return true;
        }
    }

    xmlrpc_value* cc_member = NULL;
    xmlrpc_struct_find_value(&env, result, "cc", &cc_member);
    if (env.fault_occurred)
    {
        xmlrpc_DECREF(result);
        throw_xml_fault(&env);
    }

    if (cc_member)
    {
        uint32_t array_size = xmlrpc_array_size(&env, cc_member);

        for (uint32_t i = 0; i < array_size; i++)
        {
            xmlrpc_value* item = NULL;
            xmlrpc_array_read_item(&env, cc_member, i, &item); // Correct
            if (env.fault_occurred)
            {
                xmlrpc_DECREF(result);
                xmlrpc_DECREF(cc_member);
                throw_xml_fault(&env);
            }

            const char* cc = NULL;
            xmlrpc_read_string(&env, item, &cc);
            if (env.fault_occurred)
            {
                xmlrpc_DECREF(result);
                xmlrpc_DECREF(cc_member);
                xmlrpc_DECREF(item);
                throw_xml_fault(&env);
            }

            bool eq = (strcmp(cc, login) == 0);
            free((void*)cc);
            xmlrpc_DECREF(item);
            if (eq)
            {
                xmlrpc_DECREF(cc_member);
                xmlrpc_DECREF(result);
                return true;
            }
        }
        xmlrpc_DECREF(cc_member);
    }

    xmlrpc_DECREF(result);
    return false;
}

void ctx::add_plus_one_cc(uint32_t bug_id, const char* login)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    // fails only when you write query. when it's done it never fails.
    xmlrpc_value* param = xmlrpc_build_value(&env, "({s:i,s:{s:(s)}})", "ids", bug_id, "updates", "add_cc", login); 
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result = NULL;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "Bug.update", param, &result);
    // we don't need anymore xml structure for calling xmlrpc query(calls only once)
    xmlrpc_DECREF(param);
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_DECREF(result);
}

int32_t ctx::check_uuid_in_bugzilla(const char* component, const char* UUID)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    std::string query = ssprintf("ALL component:\"%s\" statuswhiteboard:\"%s\"", component, UUID);

    // fails only when you write query. when it's done it never fails.
    xmlrpc_value* param = xmlrpc_build_value(&env, "({s:s})", "quicksearch", query.c_str());
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result = NULL;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "Bug.search", param, &result);
    // we don't need anymore xml structure for calling xmlrpc query(calls only once)
    xmlrpc_DECREF(param);
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* bugs_member = NULL;
    xmlrpc_struct_find_value(&env, result, "bugs", &bugs_member);
    if (env.fault_occurred)
    {
        xmlrpc_DECREF(result);
        throw_xml_fault(&env);
    }

    if (bugs_member)
    {
        // when array size is equal 0 that means no bug reported
        uint32_t array_size = xmlrpc_array_size(&env, bugs_member);
        if (env.fault_occurred)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(bugs_member);
            throw_xml_fault(&env);
        }
        else if (array_size == 0)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(bugs_member);
            return NEW_BUG;
        }

        xmlrpc_value* item = NULL;
        xmlrpc_array_read_item(&env, bugs_member, 0, &item); // Correct
        if (env.fault_occurred)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(bugs_member);
            throw_xml_fault(&env);
        }

        xmlrpc_value* bug = NULL;
        xmlrpc_struct_find_value(&env, item, "bug_id", &bug);
        if (env.fault_occurred)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(bugs_member);
            xmlrpc_DECREF(item);
            throw_xml_fault(&env);
        }

        if (bug)
        {
            xmlrpc_int bug_id;
            xmlrpc_read_int(&env, bug, &bug_id);
            log("Bug is already reported: %i", (int)bug_id);
            update_client(_("Bug is already reported: %i"), (int)bug_id);

            xmlrpc_DECREF(bug);
            xmlrpc_DECREF(item);
            xmlrpc_DECREF(bugs_member);
            xmlrpc_DECREF(result);
            return bug_id;
        }
        else
        {
            VERB3 log("Missing member 'bug_id'");
        }
        xmlrpc_DECREF(item);
        xmlrpc_DECREF(bugs_member);
    }
    else
    {
        VERB3 log("Missing member 'bugs'");
    }

    xmlrpc_DECREF(result);
    return MISSING_MEMBER;
}

uint32_t ctx::new_bug(const map_crash_data_t& pCrashData)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    const std::string& package   = get_crash_data_item_content(pCrashData, FILENAME_PACKAGE);
    const std::string& component = get_crash_data_item_content(pCrashData, FILENAME_COMPONENT);
    const std::string& release   = get_crash_data_item_content(pCrashData, FILENAME_RELEASE);
    const std::string& arch      = get_crash_data_item_content(pCrashData, FILENAME_ARCHITECTURE);
    const std::string& uuid      = get_crash_data_item_content(pCrashData, CD_UUID);

    std::string summary = "[abrt] crash in " + package;
    std::string status_whiteboard = "abrt_hash:" + uuid;

    std::string description = "abrt "VERSION" detected a crash.\n\n";
    description += make_description_bz(pCrashData);

    std::string product;
    std::string version;
    parse_release(release.c_str(), product, version);

    // fails only when you write query. when it's done it never fails.
    xmlrpc_value* param = xmlrpc_build_value(&env, "({s:s,s:s,s:s,s:s,s:s,s:s,s:s})",
                                        "product", product.c_str(),
                                        "component", component.c_str(),
                                        "version", version.c_str(),
                                        "summary", summary.c_str(),
                                        "description", description.c_str(),
                                        "status_whiteboard", status_whiteboard.c_str(),
                                        "platform", arch.c_str()
                              );
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* result;
    xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "Bug.create", param, &result);
    // we don't need anymore xml structure for calling xmlrpc query(calls only once)
    xmlrpc_DECREF(param);
    if (env.fault_occurred)
        throw_xml_fault(&env);

    xmlrpc_value* id;
    xmlrpc_struct_find_value(&env, result, "id", &id);
    if (env.fault_occurred)
    {
        xmlrpc_DECREF(result);
        throw_xml_fault(&env);
    }

    xmlrpc_int bug_id = -1;
    if (id)
    {
        xmlrpc_read_int(&env, id, &bug_id);
        if (env.fault_occurred)
        {
            xmlrpc_DECREF(result);
            xmlrpc_DECREF(id);
            throw_xml_fault(&env);
        }
        log("New bug id: %i", bug_id);
        update_client(_("New bug id: %i"), bug_id);
    }

    xmlrpc_DECREF(result);
    xmlrpc_DECREF(id);
    return bug_id;
}

void ctx::add_attachments(const char* bug_id_str, const map_crash_data_t& pCrashData)
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value* result = NULL;

    map_crash_data_t::const_iterator it = pCrashData.begin();
    for (; it != pCrashData.end(); it++)
    {
        const std::string &filename = it->first;
        const std::string &type = it->second[CD_TYPE];
        const std::string &content = it->second[CD_CONTENT];

        if (type == CD_TXT && content.length() > CD_TEXT_ATT_SIZE)
        {
            char *encoded64 = encode_base64(content.c_str(), content.length());
            // fails only when you write query. when it's done it never fails.
            xmlrpc_value* param = xmlrpc_build_value(&env, "(s{s:s,s:s,s:s,s:s})",
                                              bug_id_str,
                                              "description", ("File: " + filename).c_str(),
                                              "filename", filename.c_str(),
                                              "contenttype", "text/plain",
                                              "data", encoded64
                                      );
            free(encoded64);
            if (env.fault_occurred)
                throw_xml_fault(&env);

            xmlrpc_client_call2(&env, m_pClient, m_pServer_info, "bugzilla.addAttachment", param, &result);
            // we don't need anymore xml structure for calling xmlrpc query(calls only once)
            xmlrpc_DECREF(param);
            if (env.fault_occurred)
            {
                xmlrpc_DECREF(result);
                throw_xml_fault(&env);
            }
            xmlrpc_DECREF(result);
        }
    }
}

} /* namespace */


/*
 * CReporterBugzilla
 */

CReporterBugzilla::CReporterBugzilla() :
    m_bNoSSLVerify(false),
    m_sBugzillaURL("https://bugzilla.redhat.com"),
    m_sBugzillaXMLRPC("https://bugzilla.redhat.com"XML_RPC_SUFFIX)
{}

CReporterBugzilla::~CReporterBugzilla()
{}

std::string CReporterBugzilla::Report(const map_crash_data_t& pCrashData,
                                      const map_plugin_settings_t& pSettings,
                                      const char *pArgs)
{
    int32_t bug_id = -1;
    std::string Login;
    std::string Password;
    std::string BugzillaXMLRPC;
    std::string BugzillaURL;
    bool NoSSLVerify;
    map_plugin_settings_t settings = parse_settings(pSettings);
    /* if parse_settings fails it returns an empty map so we need to use defaults */
    if (!settings.empty())
    {
        Login = settings["Login"];
        Password = settings["Password"];
        BugzillaXMLRPC = settings["BugzillaXMLRPC"];
        BugzillaURL = settings["BugzillaURL"];
        NoSSLVerify = string_to_bool(settings["NoSSLVerify"].c_str());
    }
    else
    {
        Login = m_sLogin;
        Password = m_sPassword;
        BugzillaXMLRPC = m_sBugzillaXMLRPC;
        BugzillaURL = m_sBugzillaURL;
        NoSSLVerify = m_bNoSSLVerify;
    }

    const std::string& component = get_crash_data_item_content(pCrashData, FILENAME_COMPONENT);
    const std::string& uuid      = get_crash_data_item_content(pCrashData, CD_UUID);
    try
    {
        ctx bz_server(BugzillaXMLRPC.c_str(), NoSSLVerify);

        update_client(_("Checking for duplicates..."));
        bug_id = bz_server.check_uuid_in_bugzilla(component.c_str(), uuid.c_str());

        if ((Login == "") && (Password == ""))
        {
            VERB3 log("Empty login and password");
            throw CABRTException(EXCEP_PLUGIN, _("Empty login and password. Please check Bugzilla.conf"));
        }

        update_client(_("Logging into bugzilla..."));
        bz_server.login(Login.c_str(), Password.c_str());

        if (bug_id > 0)
        {
            update_client(_("Checking CC..."));
            if (!bz_server.check_cc_and_reporter(bug_id, Login.c_str()))
            {
                bz_server.add_plus_one_cc(bug_id, Login.c_str());
            }
            bz_server.logout();
            BugzillaURL += "/show_bug.cgi?id=";
            BugzillaURL += to_string(bug_id);
            return BugzillaURL;
        }

        if (bug_id == NEW_BUG)
        {
            update_client(_("Creating new bug..."));
            bug_id = bz_server.new_bug(pCrashData);
            bz_server.add_attachments(to_string(bug_id).c_str(), pCrashData);
        }

        update_client(_("Logging out..."));
        bz_server.logout();
    }
    catch (CABRTException& e)
    {
        throw CABRTException(EXCEP_PLUGIN, e.what());
    }

    if (bug_id > 0)
    {
        BugzillaURL += "/show_bug.cgi?id=";
        BugzillaURL += to_string(bug_id);
        return BugzillaURL;
    }

    BugzillaURL += "/show_bug.cgi?id=";
    return BugzillaURL;
}

//todo: make static
map_plugin_settings_t CReporterBugzilla::parse_settings(const map_plugin_settings_t& pSettings)
{
    map_plugin_settings_t plugin_settings;

    map_plugin_settings_t::const_iterator end = pSettings.end();
    map_plugin_settings_t::const_iterator it;
    it = pSettings.find("BugzillaURL");
    if (it != end)
    {
        std::string BugzillaURL = it->second;
        //remove the /xmlrpc.cgi part from old settings
        //FIXME: can be removed after users are informed about new config format
        std::string::size_type pos = BugzillaURL.find(XML_RPC_SUFFIX);
        if (pos != std::string::npos)
        {
            BugzillaURL.erase(pos);
        }
        //remove the trailing '/'
        while (BugzillaURL[BugzillaURL.length() - 1] == '/')
        {
            BugzillaURL.erase(BugzillaURL.length() - 1);
        }
        plugin_settings["BugzillaXMLRPC"] = BugzillaURL + XML_RPC_SUFFIX;
        plugin_settings["BugzillaURL"] = BugzillaURL;
    }

    it = pSettings.find("Login");
    if (it == end)
    {
        /* if any of the option is not set we use the defaults for everything */
        plugin_settings.clear();
        return plugin_settings;
    }
    plugin_settings["Login"] = it->second;

    it = pSettings.find("Password");
    if (it == end)
    {
        plugin_settings.clear();
        return plugin_settings;
    }
    plugin_settings["Password"] = it->second;

    it = pSettings.find("NoSSLVerify");
    if (it == end)
    {
        plugin_settings.clear();
        return plugin_settings;
    }
    plugin_settings["NoSSLVerify"] = it->second;

    VERB1 log("User settings ok, using them instead of defaults");
    return plugin_settings;
}

void CReporterBugzilla::SetSettings(const map_plugin_settings_t& pSettings)
{
    m_pSettings = pSettings;

//BUG! This gets called when user's keyring contains login data,
//then it takes precedence over /etc/abrt/plugins/Bugzilla.conf.
//I got a case when keyring had a STALE password, and there was no way
//for me to know that it is being used. Moreover, when I discovered it
//(by hacking abrt source!), I don't know how to purge it from the keyring.
//At the very least, log("SOMETHING") here.

    map_plugin_settings_t::const_iterator end = pSettings.end();
    map_plugin_settings_t::const_iterator it;
    it = pSettings.find("BugzillaURL");
    if (it != end)
    {
        m_sBugzillaURL = it->second;
        //remove the /xmlrpc.cgi part from old settings
        //FIXME: can be removed after users are informed about new config format
        std::string::size_type pos = m_sBugzillaURL.find(XML_RPC_SUFFIX);
        if (pos != std::string::npos)
        {
            m_sBugzillaURL.erase(pos);
        }
        //remove the trailing '/'
        while (m_sBugzillaURL[m_sBugzillaURL.length() - 1] == '/')
        {
            m_sBugzillaURL.erase(m_sBugzillaURL.length() - 1);
        }
        /*
        if (*(--m_sBugzillaURL.end()) == '/')
        {
            m_sBugzillaURL.erase(--m_sBugzillaURL.end());
        }
        */
        m_sBugzillaXMLRPC = m_sBugzillaURL + XML_RPC_SUFFIX;
    }
    it = pSettings.find("Login");
    if (it != end)
    {
        m_sLogin = it->second;
    }
    it = pSettings.find("Password");
    if (it != end)
    {
        m_sPassword = it->second;
    }
    it = pSettings.find("NoSSLVerify");
    if (it != end)
    {
        m_bNoSSLVerify = string_to_bool(it->second.c_str());
    }
}

/* Should not be deleted (why?) */
const map_plugin_settings_t& CReporterBugzilla::GetSettings()
{
    m_pSettings["BugzillaURL"] = m_sBugzillaURL;
    m_pSettings["Login"] = m_sLogin;
    m_pSettings["Password"] = m_sPassword;
    m_pSettings["NoSSLVerify"] = m_bNoSSLVerify ? "yes" : "no";

    return m_pSettings;
}

PLUGIN_INFO(REPORTER,
            CReporterBugzilla,
            "Bugzilla",
            "0.0.4",
            "Reports bugs to bugzilla",
            "npajkovs@redhat.com",
            "https://fedorahosted.org/abrt/wiki",
            PLUGINS_LIB_DIR"/Bugzilla.GTKBuilder");