summaryrefslogtreecommitdiff
path: root/libproxy/modules/config_sysconfig.cpp
blob: 616585a1153d4cf294de458d4507731dbebe3b38 (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
/*******************************************************************************
 * libproxy sysconfig module
 * Copyright (C) 2010 Novell Inc.
 *
 * 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 <cstdlib>
#include <map>
#include <fstream>

#include "../extension_config.hpp"
using namespace libproxy;
using std::map;

enum Trim {
    NO_TRIM = 0x00,
    L_TRIM  = 0x01,
    R_TRIM  = 0x02,
    TRIM    = (L_TRIM|R_TRIM)
};

static std::string trim( const std::string & s, const Trim trim_r = TRIM ) {

	if (s.empty() || trim_r == NO_TRIM)
		return s;

	std::string ret(s);
    
	if (trim_r & L_TRIM) {
 		std::string::size_type p = ret.find_first_not_of(" \t\n");
		if (p == std::string::npos)
			return std::string();
        
		ret = ret.substr(p);
	}
    
	if (trim_r & R_TRIM) {
		std::string::size_type p = ret.find_last_not_of(" \t\n");
		if (p == std::string::npos)
			return std::string();
        
		ret = ret.substr(0, p+1);
	}
    
	return ret;
}

static map<string,string> sysconfig_read(const string &_path)	{
    
	map<string,string> ret;  
	string line;

	ifstream in(_path.c_str());
	if (in.fail()) {
		return ret;
	}

	while(getline( in, line)) {
		
		if (*line.begin() != '#') {
			
			string::size_type pos = line.find('=', 0);
            
			if (pos != string::npos) {

 				string key = trim(line.substr(0, pos));
 				string value = trim(line.substr(pos + 1, line.length() - pos - 1));

				if (value.length() >= 2
				    && *(value.begin()) == '"'
				    && *(value.rbegin()) == '"') {
					value = value.substr( 1, value.length() - 2 );
				}
                
				if (value.length() >= 2
				    && *(value.begin()) == '\''
 				    && *(value.rbegin()) == '\'' ) {
					value = value.substr( 1, value.length() - 2 );
				}
				ret[key] = value;
			} // '=' found

		} // not comment

	} // while getline
	return ret;
}

static bool should_use_sysconfig()
{
	struct stat st;
	if (stat("/etc/sysconfig", &st) == 0)
		return (getuid() == 0);
	return false;
}

class sysconfig_config_extension : public config_extension {

	map<string,string> _data;
    
public:
	sysconfig_config_extension()
		: _data(sysconfig_read("/etc/sysconfig/proxy")) {
            
	}

	~sysconfig_config_extension() {
	}
   
	url get_config(url url) throw (runtime_error) {
		map<string,string>::const_iterator it = _data.find("PROXY_ENABLED");
		if (it != _data.end() && it->second == "no")
			return libproxy::url("direct://");
            
		string key;
		string proxy;
            
		// If the URL is an ftp url, try to read the ftp proxy
		if (url.get_scheme() == "ftp")
			key = "FTP_PROXY";
		else if (url.get_scheme() == "http")
			key = "HTTP_PROXY";
		else if (url.get_scheme() == "https")
			key = "HTTPS_PROXY";
            
		it = _data.find(key);
		if (it != _data.end())
			proxy = it->second;

		if (proxy.empty())
			throw runtime_error("Unable to read configuration");

		return libproxy::url(proxy);
	}

	string get_ignore(url) {
		map<string,string>::const_iterator it = _data.find("NO_PROXY");
		if (it != _data.end())
			return it->second;
		return "";
        }
    
	// if we are running as root, and the module is used, then 
	// make sure this is the first method tried
	virtual bool operator<(const base_extension&) const {
		return true;
	}
};

MM_MODULE_INIT_EZ(sysconfig_config_extension, should_use_sysconfig(), NULL, NULL);