summaryrefslogtreecommitdiff
path: root/libproxy/modules/config_kde.cpp
blob: 3068a592f3ec885593790b888050221d8ee22868 (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
/*******************************************************************************
 * libproxy - A library for proxy configuration
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 * Copyright (C) 2016 Fabian Vogt <fvogt@suse.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#include <sys/stat.h>

#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <sstream>

#ifdef WIN32
#define popen _popen
#define pclose _pclose
#else
#include <unistd.h>
#endif

#include "../extension_config.hpp"
using namespace libproxy;

class kde_config_extension : public config_extension {
public:
    kde_config_extension()
        : cache_time(0)
    {
        try {
            // Try the KF5 one first
            command = "kreadconfig5";
            command_output("kreadconfig5 --key nonexistant");

            try {
                parse_dir_list(command_output("kf5-config --path config"));
            }
            catch(...) {}

            return; // Worked
        }
        catch(...) {}

        try {
            // The KDE4 one next
            command = "kreadconfig";
            command_output(command);

            try {
                parse_dir_list(command_output("kde4-config --path config"));
            }
            catch(...) {}

            return; // Worked
        }
        catch(...) {}

        // Neither worked, so throw in get_config
        command = "";
    }

    vector<url> get_config(const url &dst) {
        // See constructor
        if(command.empty())
            throw runtime_error("Unable to read configuration");

        vector<url> response;

        string tmp, proxyType = kde_config_val("ProxyType", "-1");

        // Just switch on the first byte, either a digit, '-' ("-1") or '\0'
        switch(proxyType.c_str()[0])
        {
        case '1':
            tmp = kde_config_val(dst.get_scheme() + "Proxy", "");
            if(tmp.empty()) {
                tmp = kde_config_val("httpProxy", "");
                if(tmp.empty()) {
                    tmp = kde_config_val("socksProxy", "");
                    if(tmp.empty())
                        tmp = "direct://";
                }
            }

            // KDE uses "http://127.0.0.1 8080" instead of "http://127.0.0.1:8080"
            replace(tmp.begin(), tmp.end(), ' ', ':');

            response.push_back(url(tmp));
            break;

        case '2':
            tmp = "pac+" + kde_config_val("Proxy Config Script", "");
            if (url::is_valid(tmp))
            {
                response.push_back(url(tmp));
                break;
            }
            // else fallthrough

        case '3':
            response.push_back(url(string("wpad://")));
            break;

        case '4':
            throw runtime_error("User config_envvar"); // We'll bypass this config plugin and let the envvar plugin wor

        case '0':
        default: // Not set or unknown/illegal
            response.push_back(url("direct://"));
            break;
        }

        return response;
	}

	string get_ignore(const url&) {
        // See constructor
        if(command.empty())
            return "";

        string proxyType = kde_config_val("ProxyType", "-1");
        if(proxyType.c_str()[0] != '1')
            return ""; // Not manual config

        string prefix = kde_config_val("ReversedException", "false") != "false" ? "-" : "";
        return prefix + kde_config_val("NoProxyFor", ""); // Already in the right format
	}

private:
    string command_output(const string &cmdline) {
        // Capture stderr as well
        const string command = "(" + cmdline + ")2>&1";
        FILE *pipe = popen(command.c_str(), "r");
        if (!pipe)
            throw runtime_error("Unable to run command");

        char buffer[128];
        string result = "";
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != NULL)
                result += buffer; // TODO: If this throws bad_alloc, pipe is leaked
        }

        if(pclose(pipe) != 0)
            throw runtime_error("Command failed");

        // Trim newlines and whitespace at end
        result.erase(result.begin() + (result.find_last_not_of(" \n\t")+1), result.end());

        return result;
    }

    // Neither key nor def must contain '
    const string &kde_config_val(const string &key, const string &def) {
        if (cache_needs_refresh())
            cache.clear();
        else {
            // Already in cache?
            map<string, string>::iterator it = cache.find(key);
            if(it != cache.end())
                return it->second;
        }

        // Although all values are specified internally and/or validated,
        // checking is better than trusting.
        if(key.find('\'') != string::npos || def.find('\'') != string::npos)
            return def;

        // Add result to cache and return it
        return cache[key] = command_output(
                command + " --file kioslaverc --group 'Proxy Settings' --key '" + key + "' --default '" + def + "'");
    }

    // Used for cache invalidation
    struct configfile {
        string path;
        time_t mtime; // 0 means either not refreshed or doesn't exist
    };

    // Parses output of qtpaths/kde4-config to fill config_locs
    void parse_dir_list(const string &dirs) {
        string config_path;
        stringstream config_paths_stream(dirs);

        // Try each of the listed folders, seperated by ':'
        while (getline(config_paths_stream, config_path, ':')) {
            configfile config_loc;
            config_loc.path = config_path + "/kioslaverc";
            config_loc.mtime = 0;
            config_locs.push_back(config_loc);
        }
    }

    // If any of the locations in config_locs changed (different mtime),
    // update config_locs and return true.
    bool cache_needs_refresh() {
        // Play safe here, if we can't determine the location,
        // don't cache at all.
        bool needs_refresh = config_locs.empty();
        struct stat config_info;

        for (unsigned int i = 0; i < config_locs.size(); ++i) {
            configfile &config = config_locs[i];
            time_t current_mtime = stat(config.path.c_str(), &config_info) == 0 ? config_info.st_mtime : 0;
            if (config.mtime != current_mtime) {
                config.mtime = current_mtime;
                needs_refresh = true;
            }
        }

        return needs_refresh;
    }

    // Whether to use kreadconfig or kreadconfig5
    string command;
    // When the cache was flushed last
    time_t cache_time;
    // Cache for config values
    map<string, string> cache;
    // State of the config files at the time of the last cache flush
    vector<configfile> config_locs;
};

MM_MODULE_INIT_EZ(kde_config_extension, getenv("KDE_FULL_SESSION"), NULL, NULL);