summaryrefslogtreecommitdiffstats
path: root/lib/MiddleWare/MiddleWare.cpp
blob: d2588146e3017e6be0a9b63b15e37f030cd3fa7e (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
/*
    MiddleWare.cpp

    Copyright (C) 2009  Zdenek Prikryl (zprikryl@redhat.com)
    Copyright (C) 2009  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 "MiddleWare.h"
#include "DebugDump.h"
#include "Settings.h"

CMiddleWare::CMiddleWare(const std::string& pPlugisConfDir,
                         const std::string& pPlugisLibDir,
                         const std::string& pMiddleWareConfFile) :
    m_pPluginManager(NULL),
    m_bOpenGPGCheck(true)
{
    m_pPluginManager = new CPluginManager(pPlugisConfDir, pPlugisLibDir);
    if (m_pPluginManager == NULL)
    {
        throw std::string("Not enought memory.");
    }
    m_pPluginManager->LoadPlugins();
    LoadSettings(pMiddleWareConfFile);

    set_enabled_plugins_t::iterator it_p;
    for (it_p = m_setEnabledPlugins.begin(); it_p != m_setEnabledPlugins.end(); it_p++)
    {
        m_pPluginManager->RegisterPlugin(*it_p);
    }
}

CMiddleWare::~CMiddleWare()
{
    m_pPluginManager->UnLoadPlugins();
    delete m_pPluginManager;
}

void CMiddleWare::LoadSettings(const std::string& pPath)
{
    map_settings_t settings;
    load_settings(pPath, settings);
    if (settings.find("BlackList") != settings.end())
    {
        parse_settings(settings["BlackList"], m_setBlackList);
    }
    if (settings.find("EnabledPlugins") != settings.end())
    {
        parse_settings(settings["EnabledPlugins"], m_setEnabledPlugins);
    }
    if (settings.find("OpenGPGPublicKeys") != settings.end())
    {
        parse_settings(settings["OpenGPGPublicKeys"], m_setOpenGPGKeys);
        set_opengpg_keys_t::iterator it_k;
        for (it_k = m_setOpenGPGKeys.begin(); it_k != m_setOpenGPGKeys.end(); it_k++)
        {
            m_RPMInfo.LoadOpenGPGPublicKey(*it_k);
        }
    }
    if (settings.find("EnableOpenGPG") != settings.end())
    {
        m_bOpenGPGCheck = settings["EnableOpenGPG"] == "yes";
    }
    if (settings.find("Database") != settings.end())
    {
        m_sDatabase = settings["Database"];
        if (m_setEnabledPlugins.find(m_sDatabase) == m_setEnabledPlugins.end())
        {
            throw std::string("Database plugin '"+m_sDatabase+"' isn't enabled.");
        }
    }
    else
    {
        throw std::string("No database plugin is selected.");
    }
    set_enabled_plugins_t::iterator it_p;
    for (it_p = m_setEnabledPlugins.begin(); it_p != m_setEnabledPlugins.end(); it_p++)
    {
        if (settings.find(*it_p) != settings.end())
        {
            set_reporters_t reporters;
            parse_settings(settings[*it_p], reporters);
            m_mapPlugin2Reporters[*it_p] = reporters;
        }
    }
}

void CMiddleWare::DebugDump2Report(const std::string& pDebugDumpDir, crash_report_t& pCrashReport)
{
    CDebugDump dd;
    dd.Open(pDebugDumpDir);
    dd.LoadText(FILENAME_UUID, pCrashReport.m_sUUID);
    dd.LoadText(FILENAME_ARCHITECTURE, pCrashReport.m_sArchitecture);
    dd.LoadText(FILENAME_KERNEL, pCrashReport.m_sKernel);
    dd.LoadText(FILENAME_PACKAGE, pCrashReport.m_sPackage);
    dd.LoadText(FILENAME_EXECUTABLE, pCrashReport.m_sExecutable);
    dd.LoadText(FILENAME_CMDLINE, pCrashReport.m_sCmdLine);
    dd.LoadText(FILENAME_RELEASE, pCrashReport.m_sRelease);

    if (dd.Exist(FILENAME_TEXTDATA1))
    {
        dd.LoadText(FILENAME_TEXTDATA1, pCrashReport.m_sTextData1);
    }
    if (dd.Exist(FILENAME_TEXTDATA2))
    {
        dd.LoadText(FILENAME_TEXTDATA2, pCrashReport.m_sTextData2);
    }
    if (dd.Exist(FILENAME_BINARYDATA1))
    {
        pCrashReport.m_sBinaryData1 = pDebugDumpDir + "/" + FILENAME_BINARYDATA1;
    }
    if (dd.Exist(FILENAME_BINARYDATA2))
    {
        pCrashReport.m_sBinaryData2 = pDebugDumpDir + "/" + FILENAME_BINARYDATA2;
    }
    dd.Close();
}

void CMiddleWare::RegisterPlugin(const std::string& pName)
{
    m_pPluginManager->RegisterPlugin(pName);
}

void CMiddleWare::UnRegisterPlugin(const std::string& pName)
{
    m_pPluginManager->UnRegisterPlugin(pName);
}


std::string CMiddleWare::GetLocalUUIDLanguage(const std::string& pLanguage,
                                              const std::string& pDebugDumpDir)
{
    CLanguage* language = m_pPluginManager->GetLanguage(pLanguage);
    return language->GetLocalUUID(pDebugDumpDir);
}

std::string CMiddleWare::GetGlobalUUIDLanguage(const std::string& pLanguage,
                                               const std::string& pDebugDumpDir)
{
    CLanguage* language = m_pPluginManager->GetLanguage(pLanguage);
    return language->GetGlobalUUID(pDebugDumpDir);
}

void CMiddleWare::CreateReportLanguage(const std::string& pLanguage,
                                       const std::string& pDebugDumpDir)
{
    CLanguage* language = m_pPluginManager->GetLanguage(pLanguage);
    return language->CreateReport(pDebugDumpDir);
}

std::string CMiddleWare::GetLocalUUIDApplication(const std::string& pApplication,
                                                 const std::string& pDebugDumpDir)
{
    CApplication* application = m_pPluginManager->GetApplication(pApplication);
    return application->GetLocalUUID(pDebugDumpDir);
}

std::string CMiddleWare::GetGlobalUUIDApplication(const std::string& pApplication,
                                                  const std::string& pDebugDumpDir)
{
    CApplication* application = m_pPluginManager->GetApplication(pApplication);
    return application->GetGlobalUUID(pDebugDumpDir);
}

void CMiddleWare::CreateReportApplication(const std::string& pApplication,
                                          const std::string& pDebugDumpDir)
{
    CApplication* application = m_pPluginManager->GetApplication(pApplication);
    return application->CreateReport(pDebugDumpDir);
}

void CMiddleWare::CreateReport(const std::string& pUUID,
                               const std::string& pUID,
                               crash_report_t& pCrashReport)
{
    CDatabase* database = m_pPluginManager->GetDatabase(m_sDatabase);
    database_row_t row;
    database->Connect();
    row = database->GetUUIDData(pUUID, pUID);
    database->DisConnect();

    if (pUUID == "" || row.m_sUUID != pUUID)
    {
        throw std::string("CMiddleWare::CreateReport(): UUID '"+pUUID+"' is not in database.");
    }

    std::string appLan;
    std::string UUID;
    CDebugDump dd;
    dd.Open(row.m_sDebugDumpDir);
    if (dd.Exist(FILENAME_APPLICATION))
    {
        dd.LoadText(FILENAME_APPLICATION, appLan);
        CreateReportApplication(appLan, row.m_sDebugDumpDir);
        UUID = GetGlobalUUIDApplication(appLan, row.m_sDebugDumpDir);
    }
    if (dd.Exist(FILENAME_LANGUAGE))
    {
        dd.LoadText(FILENAME_LANGUAGE, appLan);
        CreateReportLanguage(appLan, row.m_sDebugDumpDir);
        UUID = GetGlobalUUIDLanguage(appLan, row.m_sDebugDumpDir);
    }
    dd.SaveText(FILENAME_UUID, UUID);
    dd.Close();

    DebugDump2Report(row.m_sDebugDumpDir, pCrashReport);

    pCrashReport.m_sMWID =  appLan + ";" + pUID + ";" + pUUID  ;
}
#include <iostream>
void CMiddleWare::Report(const crash_report_t& pCrashReport)
{
    std::string::size_type pos1 = 0;
    std::string::size_type pos2 = pCrashReport.m_sMWID.find(";", pos1);
    std::string lanAppPlugin = pCrashReport.m_sMWID.substr(pos1, pos2);
    pos1 = pos2 + 1;
    pos2 = pCrashReport.m_sMWID.find(";", pos1);
    std::string UID = pCrashReport.m_sMWID.substr(pos1, pos2 - pos1);
    pos1 = pos2 + 1;
    std::string UUID = pCrashReport.m_sMWID.substr(pos1);;

    CDatabase* database = m_pPluginManager->GetDatabase(m_sDatabase);
    database->Connect();
    database->SetReported(UUID, UID);
    database->DisConnect();

    if (m_mapPlugin2Reporters.find(lanAppPlugin) != m_mapPlugin2Reporters.end())
    {
        set_reporters_t::iterator it_r;
        for (it_r = m_mapPlugin2Reporters[lanAppPlugin].begin();
             it_r != m_mapPlugin2Reporters[lanAppPlugin].end();
             it_r++)
        {
            CReporter* reporter = m_pPluginManager->GetReporter(*it_r);
            reporter->Report(pCrashReport);
        }
    }
}

void CMiddleWare::DeleteDebugDump(const std::string& pUUID,
                                  const std::string& pUID)
{
    database_row_t row;
    CDatabase* database = m_pPluginManager->GetDatabase(m_sDatabase);
    database->Connect();
    row = database->GetUUIDData(pUUID, pUID);
    database->Delete(pUUID, pUID);
    database->DisConnect();
    CDebugDump dd;
    dd.Open(row.m_sDebugDumpDir);
    dd.Delete();
    dd.Close();
}

int CMiddleWare::SaveDebugDump(const std::string& pDebugDumpDir, crash_info_t& pCrashInfo)
{
    CDatabase* database = m_pPluginManager->GetDatabase(m_sDatabase);

    std::string UUID;
    std::string UID;
    std::string package;
    std::string description;
    std::string executable;
    std::string time;

    CDebugDump dd;
    dd.Open(pDebugDumpDir);

    dd.LoadText(FILENAME_EXECUTABLE, executable);
    package = m_RPMInfo.GetPackage(executable, description);
    std::string packageName = package.substr(0, package.rfind("-", package.rfind("-") - 1));
    if (packageName == "" ||
       (m_setBlackList.find(packageName) != m_setBlackList.end()))
    {
        dd.Delete();
        dd.Close();
        return 0;
    }
    if (m_bOpenGPGCheck)
    {
        if (!m_RPMInfo.CheckFingerprint(packageName) ||
            !m_RPMInfo.CheckHash(packageName, executable))
        {
            dd.Delete();
            dd.Close();
            return 0;
        }
    }
    dd.SaveText(FILENAME_PACKAGE, package);
    dd.SaveText(FILENAME_DESCRIPTION, description);

    if (dd.Exist(FILENAME_APPLICATION))
    {
        std::string application;
        dd.LoadText(FILENAME_APPLICATION, application);
        UUID = GetLocalUUIDApplication(application, pDebugDumpDir);
    }
    if (dd.Exist(FILENAME_LANGUAGE))
    {
        std::string language;
        dd.LoadText(FILENAME_LANGUAGE, language);
        UUID = GetLocalUUIDLanguage(language, pDebugDumpDir);
    }
    if (UUID == "")
    {
        throw std::string("CMiddleWare::SaveDebugDumpToDataBase(): Wrong UUID.");
    }

    dd.LoadText(FILENAME_TIME, time);
    dd.LoadText(FILENAME_UID, UID);

    database_row_t row;
    database->Connect();
    database->Insert(UUID, UID, pDebugDumpDir, time);
    row = database->GetUUIDData(UUID, UID);
    database->DisConnect();

    if (row.m_sReported == "1")
    {
        dd.Delete();
        dd.Close();
        return 0;
    }
    if (row.m_sCount != "1")
    {
        dd.Delete();
    }
    dd.Close();

    pCrashInfo.m_sUUID = UUID;
    pCrashInfo.m_sUID = UID;
    pCrashInfo.m_sCount = row.m_sCount;
    pCrashInfo.m_sExecutable = executable;
    pCrashInfo.m_sPackage = package;
    pCrashInfo.m_sTime = row.m_sTime;
    pCrashInfo.m_sReported = "0";

    return 1;
}

vector_crash_infos_t CMiddleWare::GetCrashInfos(const std::string& pUID)
{
    CDatabase* database = m_pPluginManager->GetDatabase(m_sDatabase);
    vector_database_rows_t rows;
    database->Connect();
    rows = database->GetUIDData(pUID);
    database->DisConnect();

    vector_crash_infos_t infos;
    std::string data;
    int ii;
    for (ii = 0; ii < rows.size(); ii++)
    {
        crash_info_t info;
        CDebugDump dd;
        info.m_sUUID = rows[ii].m_sUUID;
        info.m_sUID = rows[ii].m_sUID;
        info.m_sCount = rows[ii].m_sCount;
        info.m_sTime = rows[ii].m_sTime;
        info.m_sReported = rows[ii].m_sReported;

        dd.Open(rows[ii].m_sDebugDumpDir);
        dd.LoadText(FILENAME_EXECUTABLE, data);
        info.m_sExecutable = data;
        dd.LoadText(FILENAME_PACKAGE, data);
        info.m_sPackage = data;
        dd.LoadText(FILENAME_DESCRIPTION, data);
        info.m_sDescription = data;
        dd.Close();

        infos.push_back(info);
    }

    return infos;
}