|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* libproxy sysconfig module |
| 3 |
* Copyright (C) 2010 Novell Inc. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
******************************************************************************/ |
| 19 |
|
| 20 |
#include <sys/stat.h> |
| 21 |
#include <cstdlib> |
| 22 |
#include <map> |
| 23 |
#include <fstream> |
| 24 |
|
| 25 |
#include "../extension_config.hpp" |
| 26 |
using namespace libproxy; |
| 27 |
using std::map; |
| 28 |
|
| 29 |
enum Trim { |
| 30 |
NO_TRIM = 0x00, |
| 31 |
L_TRIM = 0x01, |
| 32 |
R_TRIM = 0x02, |
| 33 |
TRIM = (L_TRIM|R_TRIM) |
| 34 |
}; |
| 35 |
|
| 36 |
static std::string trim( const std::string & s, const Trim trim_r = TRIM ) { |
| 37 |
|
| 38 |
if (s.empty() || trim_r == NO_TRIM) |
| 39 |
return s; |
| 40 |
|
| 41 |
std::string ret(s); |
| 42 |
|
| 43 |
if (trim_r & L_TRIM) { |
| 44 |
std::string::size_type p = ret.find_first_not_of(" \t\n"); |
| 45 |
if (p == std::string::npos) |
| 46 |
return std::string(); |
| 47 |
|
| 48 |
ret = ret.substr(p); |
| 49 |
} |
| 50 |
|
| 51 |
if (trim_r & R_TRIM) { |
| 52 |
std::string::size_type p = ret.find_last_not_of(" \t\n"); |
| 53 |
if (p == std::string::npos) |
| 54 |
return std::string(); |
| 55 |
|
| 56 |
ret = ret.substr(0, p+1); |
| 57 |
} |
| 58 |
|
| 59 |
return ret; |
| 60 |
} |
| 61 |
|
| 62 |
static map<string,string> sysconfig_read(const string &_path) { |
| 63 |
|
| 64 |
map<string,string> ret; |
| 65 |
string line; |
| 66 |
|
| 67 |
ifstream in(_path.c_str()); |
| 68 |
if (in.fail()) { |
| 69 |
return ret; |
| 70 |
} |
| 71 |
|
| 72 |
while(getline( in, line)) { |
| 73 |
|
| 74 |
if (*line.begin() != '#') { |
| 75 |
|
| 76 |
string::size_type pos = line.find('=', 0); |
| 77 |
|
| 78 |
if (pos != string::npos) { |
| 79 |
|
| 80 |
string key = trim(line.substr(0, pos)); |
| 81 |
string value = trim(line.substr(pos + 1, line.length() - pos - 1)); |
| 82 |
|
| 83 |
if (value.length() >= 2 |
| 84 |
&& *(value.begin()) == '"' |
| 85 |
&& *(value.rbegin()) == '"') { |
| 86 |
value = value.substr( 1, value.length() - 2 ); |
| 87 |
} |
| 88 |
|
| 89 |
if (value.length() >= 2 |
| 90 |
&& *(value.begin()) == '\'' |
| 91 |
&& *(value.rbegin()) == '\'' ) { |
| 92 |
value = value.substr( 1, value.length() - 2 ); |
| 93 |
} |
| 94 |
ret[key] = value; |
| 95 |
} // '=' found |
| 96 |
|
| 97 |
} // not comment |
| 98 |
|
| 99 |
} // while getline |
| 100 |
return ret; |
| 101 |
} |
| 102 |
|
| 103 |
static bool should_use_sysconfig() |
| 104 |
{ |
| 105 |
struct stat st; |
| 106 |
if (stat("/etc/sysconfig", &st) == 0) |
| 107 |
return (getuid() == 0); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
class sysconfig_config_extension : public config_extension { |
| 112 |
|
| 113 |
map<string,string> _data; |
| 114 |
|
| 115 |
public: |
| 116 |
sysconfig_config_extension() |
| 117 |
: _data(sysconfig_read("/etc/sysconfig/proxy")) { |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
~sysconfig_config_extension() { |
| 122 |
} |
| 123 |
|
| 124 |
url get_config(url url) throw (runtime_error) { |
| 125 |
map<string,string>::const_iterator it = _data.find("PROXY_ENABLED"); |
| 126 |
if (it != _data.end() && it->second == "no") |
| 127 |
return libproxy::url("direct://"); |
| 128 |
|
| 129 |
string key; |
| 130 |
string proxy; |
| 131 |
|
| 132 |
// If the URL is an ftp url, try to read the ftp proxy |
| 133 |
if (url.get_scheme() == "ftp") |
| 134 |
key = "FTP_PROXY"; |
| 135 |
else if (url.get_scheme() == "http") |
| 136 |
key = "HTTP_PROXY"; |
| 137 |
else if (url.get_scheme() == "https") |
| 138 |
key = "HTTPS_PROXY"; |
| 139 |
|
| 140 |
it = _data.find(key); |
| 141 |
if (it != _data.end()) |
| 142 |
proxy = it->second; |
| 143 |
|
| 144 |
if (proxy.empty()) |
| 145 |
throw runtime_error("Unable to read configuration"); |
| 146 |
|
| 147 |
return libproxy::url(proxy); |
| 148 |
} |
| 149 |
|
| 150 |
string get_ignore(url) { |
| 151 |
map<string,string>::const_iterator it = _data.find("NO_PROXY"); |
| 152 |
if (it != _data.end()) |
| 153 |
return it->second; |
| 154 |
return ""; |
| 155 |
} |
| 156 |
|
| 157 |
// if we are running as root, and the module is used, then |
| 158 |
// make sure this is the first method tried |
| 159 |
virtual bool operator<(const base_extension&) const { |
| 160 |
return true; |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
MM_MODULE_INIT_EZ(sysconfig_config_extension, should_use_sysconfig(), NULL, NULL); |
| 165 |
|