summaryrefslogtreecommitdiffstats
path: root/src/daemon/Settings.cpp
blob: 90efd1999f985a0228c39c7e5b1c8f0c9ba18f11 (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
/*
    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"

bool          g_settings_bOpenGPGCheck = false;
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;


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;
}

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 void ParseCommon(map_string_h *settings, const char *conf_filename)
{
    char *value;

    value = g_hash_table_lookup(settings, "OpenGPGCheck");
    if (value)
    {
        g_settings_bOpenGPGCheck = string_to_bool(value);
        g_hash_table_remove(settings, "OpenGPGCheck");
    }

    value = g_hash_table_lookup(settings, "BlackList");
    if (value)
    {
        g_settings_setBlackListedPkgs = parse_list(value);
        g_hash_table_remove(settings, "BlackList");
    }

    value = g_hash_table_lookup(settings, "BlackListedPaths");
    if (value)
    {
        g_settings_setBlackListedPaths = parse_list(value);
        g_hash_table_remove(settings, "BlackListedPaths");
    }

    value = g_hash_table_lookup(settings, "WatchCrashdumpArchiveDir");
    if (value)
    {
        g_settings_sWatchCrashdumpArchiveDir = xstrdup(value);
        g_hash_table_remove(settings, "WatchCrashdumpArchiveDir");
    }

    value = g_hash_table_lookup(settings, "MaxCrashReportsSize");
    if (value)
    {
//fixme: dont die
        g_settings_nMaxCrashReportsSize = xatoi_positive(value);
        g_hash_table_remove(settings, "MaxCrashReportsSize");
    }

    value = g_hash_table_lookup(settings, "ProcessUnpackaged");
    if (value)
    {
        g_settings_bProcessUnpackaged = string_to_bool(value);
        g_hash_table_remove(settings, "ProcessUnpackaged");
    }

    GHashTableIter iter;
    char *name;
    /*char *value; - already declared */
    g_hash_table_iter_init(&iter, settings);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        error_msg("Unrecognized variable '%s' in '%s'", name, conf_filename);
    }
}

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 beginning of a path, so let's handle it as a key
                g_settings_setOpenGPGPublicKeys = g_list_append(g_settings_setOpenGPGPublicKeys, line);
            else
                free(line);
        }
        fclose(fp);
    }
}

int load_settings()
{
    free_settings();

    map_string_h *settings = new_map_string();
    if (!load_conf_file(CONF_DIR"/abrt.conf", settings, /*skip key w/o values:*/ false))
        error_msg("Can't open '%s'", CONF_DIR"/abrt.conf");

    ParseCommon(settings, CONF_DIR"/abrt.conf");
    free_map_string(settings);

    LoadGPGKeys();

    return 0;
}