|
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 <cstdlib> |
| 21 |
#include <map> |
| 22 |
#include <fstream> |
| 23 |
|
| 24 |
#include "../extension_config.hpp" |
| 25 |
using namespace libproxy; |
| 26 |
using std::map; |
| 27 |
|
| 28 |
enum Trim { |
| 29 |
NO_TRIM = 0x00, |
| 30 |
L_TRIM = 0x01, |
| 31 |
R_TRIM = 0x02, |
| 32 |
TRIM = (L_TRIM|R_TRIM) |
| 33 |
}; |
| 34 |
|
| 35 |
static std::string trim( const std::string & s, const Trim trim_r = TRIM ) |
| 36 |
{ |
| 37 |
if ( s.empty() || trim_r == NO_TRIM ) |
| 38 |
return s; |
| 39 |
|
| 40 |
std::string ret( s ); |
| 41 |
|
| 42 |
if ( trim_r & L_TRIM ) |
| 43 |
{ |
| 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 |
{ |
| 53 |
std::string::size_type p = ret.find_last_not_of( " \t\n" ); |
| 54 |
if ( p == std::string::npos ) |
| 55 |
return std::string(); |
| 56 |
|
| 57 |
ret = ret.substr( 0, p+1 ); |
| 58 |
} |
| 59 |
|
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
static map<string,string> sysconfig_read( const string &_path ) |
| 64 |
{ |
| 65 |
map<string,string> ret; |
| 66 |
|
| 67 |
string line; |
| 68 |
ifstream in( _path.c_str() ); |
| 69 |
if ( in.fail() ) { |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
while( getline( in, line ) ) { |
| 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 |
{ |
| 87 |
value = value.substr( 1, value.length() - 2 ); |
| 88 |
} |
| 89 |
if ( value.length() >= 2 |
| 90 |
&& *(value.begin()) == '\'' |
| 91 |
&& *(value.rbegin()) == '\'' ) |
| 92 |
{ |
| 93 |
value = value.substr( 1, value.length() - 2 ); |
| 94 |
} |
| 95 |
ret[key] = value; |
| 96 |
|
| 97 |
} // '=' found |
| 98 |
|
| 99 |
} // not comment |
| 100 |
|
| 101 |
} // while getline |
| 102 |
return ret; |
| 103 |
} |
| 104 |
|
| 105 |
class sysconfig_config_extension : public config_extension { |
| 106 |
|
| 107 |
map<string,string> _data; |
| 108 |
|
| 109 |
public: |
| 110 |
sysconfig_config_extension() |
| 111 |
: _data(sysconfig_read("/etc/sysconfig/proxy")) |
| 112 |
{ |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
~sysconfig_config_extension() { |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
url get_config(url url) throw (runtime_error) { |
| 121 |
map<string,string>::const_iterator it = _data.find("PROXY_ENABLED"); |
| 122 |
if (it != _data.end() && it->second == "no") |
| 123 |
return libproxy::url("direct://"); |
| 124 |
|
| 125 |
string key; |
| 126 |
string proxy; |
| 127 |
|
| 128 |
// If the URL is an ftp url, try to read the ftp proxy |
| 129 |
if (url.get_scheme() == "ftp") |
| 130 |
key = "FTP_PROXY"; |
| 131 |
else if (url.get_scheme() == "http") |
| 132 |
key = "HTTP_PROXY"; |
| 133 |
else if (url.get_scheme() == "https") |
| 134 |
key = "HTTPS_PROXY"; |
| 135 |
|
| 136 |
it = _data.find(key); |
| 137 |
if (it != _data.end()) |
| 138 |
proxy = it->second; |
| 139 |
|
| 140 |
if (proxy.empty()) |
| 141 |
throw runtime_error("Unable to read configuration"); |
| 142 |
|
| 143 |
return libproxy::url(proxy); |
| 144 |
} |
| 145 |
|
| 146 |
string get_ignore(url) { |
| 147 |
map<string,string>::const_iterator it = _data.find("NO_PROXY"); |
| 148 |
if (it != _data.end()) |
| 149 |
return it->second; |
| 150 |
return ""; |
| 151 |
} |
| 152 |
|
| 153 |
// Make sure that envvar is pushed to the back behind all other config extensions |
| 154 |
// if we are running as root, then make sure this is the |
| 155 |
// first thing tried |
| 156 |
virtual bool operator<(const base_extension&) const { |
| 157 |
if (getuid == 0) |
| 158 |
return true; |
| 159 |
return false; |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
MM_MODULE_INIT_EZ(sysconfig_config_extension, true, NULL, NULL); |