summaryrefslogtreecommitdiffstats
path: root/src/daemon/Settings.cpp
blob: b3d1ade5ba8b0abc146d2b1c315a35482db809a8 (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
/*
    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 "abrtlib.h"
#include "Settings.h"

#define SECTION_COMMON       "Common"
#define SECTION_LOG_SCANNERS "LogScanners"

/* Conf file has this format:
 * [ section_name1 ]
 * name1 = value1
 * name2 = value2
 * [ section_name2 ]
 * name = value
 */

/* Static data */
/* Filled by load_settings() */

/* map["name"] = "value" strings from [ Common ] section.
 * If the same name found on more than one line,
 * the values are appended, separated by comma: map["name"] = "value1,value2" */
static map_string_t s_mapSectionCommon;

/* Public data */

/* [ Common ] */
/* one line: "OpenGPGCheck = value" */
bool          g_settings_bOpenGPGCheck = false;
/* one line: "OpenGPGPublicKeys = value1,value2" */
GList *       g_settings_setOpenGPGPublicKeys = NULL;
GList *       g_settings_setBlackListedPkgs = NULL;
GList *       g_settings_setBlackListedPaths = NULL;
char *        g_settings_sWatchCrashdumpArchiveDir = NULL;
unsigned int  g_settings_nMaxCrashReportsSize = 1000;
bool          g_settings_bProcessUnpackaged = false;

/* [ LogScanners ] */
char *        g_settings_sLogScanners = NULL;


/*
 * Loading
 */

static GList *parse_list(const char* list)
{
    struct strbuf *item = strbuf_new();
    GList *l = NULL;

    char *trim_item = NULL;

    for (unsigned ii = 0; list[ii]; ii++)
    {
        if (list[ii] == ',')
        {
            trim_item = strtrim(item->buf);
            l = g_list_append(l, xstrdup(trim_item));
            strbuf_clear(item);
        }
        else
            strbuf_append_char(item, list[ii]);
    }

    if (item->len > 0)
    {
        trim_item = strtrim(item->buf);
        l = g_list_append(l, xstrdup(trim_item));
    }

    strbuf_free(item);
    return l;
}

static int ParseCommon()
{
    map_string_t::const_iterator end = s_mapSectionCommon.end();
    map_string_t::const_iterator it = s_mapSectionCommon.find("OpenGPGCheck");
    if (it != end)
    {
        g_settings_bOpenGPGCheck = string_to_bool(it->second.c_str());
    }
    it = s_mapSectionCommon.find("BlackList");
    if (it != end)
    {
        g_settings_setBlackListedPkgs = parse_list(it->second.c_str());
    }
    it = s_mapSectionCommon.find("BlackListedPaths");
    if (it != end)
    {
        g_settings_setBlackListedPaths = parse_list(it->second.c_str());
    }
    it = s_mapSectionCommon.find("WatchCrashdumpArchiveDir");
    if (it != end)
    {
        g_settings_sWatchCrashdumpArchiveDir = xstrdup(it->second.c_str());
    }
    it = s_mapSectionCommon.find("MaxCrashReportsSize");
    if (it != end)
    {
        g_settings_nMaxCrashReportsSize = xatoi_positive(it->second.c_str());
    }
    it = s_mapSectionCommon.find("ProcessUnpackaged");
    if (it != end)
    {
        g_settings_bProcessUnpackaged = string_to_bool(it->second.c_str());
    }
    return 0; /* no error */
}

static void LoadGPGKeys()
{
    FILE *fp = fopen(CONF_DIR"/gpg_keys", "r");
    if (fp)
    {
        /* every line is one key
         * FIXME: make it more robust, it doesn't handle comments
         */
        char *line;
        while ((line = xmalloc_fgetline(fp)) != NULL)
        {
            if (line[0] == '/') // probably the begining of path, so let's handle it as a key
                g_settings_setOpenGPGPublicKeys = g_list_append(g_settings_setOpenGPGPublicKeys, line);
            else
                free(line);
        }
        fclose(fp);
    }
}

/**
 * Reads configuration from file to s_mapSection* static variables.
 * The file must be opened for reading.
 */
static int ReadConfigurationFromFile(FILE *fp)
{
    char *line;
    std::string section;
    int lineno = 0;
    while ((line = xmalloc_fgetline(fp)) != NULL)
    {
        ++lineno;
        bool is_key = true;
        bool is_section = false;
        bool is_quote = false;
        unsigned ii;
        std::string key, value;
        for (ii = 0; line[ii] != '\0'; ii++)
        {
            if (is_quote && line[ii] == '\\' && line[ii + 1])
            {
                value += line[ii];
                ii++;
                value += line[ii];
                continue;
            }
            if (isspace(line[ii]) && !is_quote && is_key)
            {
                continue;
            }
            if (line[ii] == '#' && !is_quote && key == "")
            {
                break;
            }
            if (line[ii] == '[' && !is_quote)
            {
                is_section = true;
                section = "";
                continue;
            }
            if (line[ii] == '"')
            {
                is_quote = !is_quote;
                value += line[ii];
                continue;
            }
            if (is_section)
            {
                if (line[ii] == ']')
                    break;
                section += line[ii];
                continue;
            }
            if (is_key && line[ii] == '=' && !is_quote)
            {
		while (isspace(line[ii + 1])) ii++;
                is_key = false;
                key = value;
                value = "";
                continue;
            }
            value += line[ii];
        } /* for */

        if (is_quote)
        {
            error_msg("abrt.conf: Invalid syntax on line %d", lineno);
            goto return_error;
        }

        if (is_section)
        {
            if (line[ii] != ']') /* section not closed */
            {
                error_msg("abrt.conf: Section not closed on line %d", lineno);
                goto return_error;
            }
            goto free_line;
        }

        if (is_key)
        {
            if (!value.empty()) /* the key is stored in value */
            {
                error_msg("abrt.conf: Invalid syntax on line %d", lineno);
                goto return_error;
            }
            goto free_line;
        }
        if (key.empty()) /* A line without key: " = something" */
        {
            error_msg("abrt.conf: Invalid syntax on line %d", lineno);
            goto return_error;
        }

        if (section == SECTION_COMMON)
        {
            if (s_mapSectionCommon[key] != "")
                s_mapSectionCommon[key] += ",";
            s_mapSectionCommon[key] += value;
        }
        else if (section == SECTION_LOG_SCANNERS)
        {
            g_settings_sLogScanners = xstrdup(value.c_str());
        }
        else
        {
            error_msg("abrt.conf: Ignoring entry in invalid section [%s]", section.c_str());
 return_error:
            free(line);
            return 1; /* error */
        }
 free_line:
        free(line);
    } /* while */

    return 0; /* success */
}

/* abrt daemon loads .conf file */
int load_settings()
{
    int err = 0;

    FILE *fp = fopen(CONF_DIR"/abrt.conf", "r");
    if (fp)
    {
        err = ReadConfigurationFromFile(fp);
        fclose(fp);
    }
    else
        error_msg("Unable to read configuration file %s", CONF_DIR"/abrt.conf");

    if (err == 0)
        err = ParseCommon();

    if (err == 0)
    {
        /*
         * loading gpg keys will invoke rpm_load_gpgkey() from rpm.cpp
         * pgpReadPkts which makes nss to re-init and thus makes
         * bugzilla plugin work :-/
         */
        //FIXME FIXME FIXME FIXME FIXME FIXME!!!
        //if (g_settings_bOpenGPGCheck)
        LoadGPGKeys();
    }

    return err;
}

/* dbus call to retrieve .conf file data from daemon */
map_abrt_settings_t GetSettings()
{
    map_abrt_settings_t ABRTSettings;

    ABRTSettings[SECTION_COMMON] = s_mapSectionCommon;

    return ABRTSettings;
}

///* dbus call to change some .conf file data */
//void SetSettings(const map_abrt_settings_t& pSettings, const char *dbus_sender)
//{
//    map_abrt_settings_t::const_iterator it = pSettings.find(SECTION_COMMON);
//    map_abrt_settings_t::const_iterator end = pSettings.end();
//    if (it != end)
//    {
//        s_mapSectionCommon = it->second;
//        ParseCommon();
//    }
//}

void free_settings()
{
    list_free_with_free(g_settings_setOpenGPGPublicKeys);
    g_settings_setOpenGPGPublicKeys = NULL;

    list_free_with_free(g_settings_setBlackListedPkgs);
    g_settings_setBlackListedPkgs = NULL;

    list_free_with_free(g_settings_setBlackListedPaths);
    g_settings_setBlackListedPaths = NULL;

    free(g_settings_sWatchCrashdumpArchiveDir);
    g_settings_sWatchCrashdumpArchiveDir = NULL;

    free(g_settings_sLogScanners);
    g_settings_sLogScanners = NULL;
}