libzypp: zypp-curl/curlconfig.cc Source File

libzypp 17.38.11

curlconfig.cc
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3
4#include <zypp-core/base/Logger.h>
5#include <zypp-core/base/IOStream.h>
6#include <zypp-core/Pathname.h>
7#include <zypp-core/fs/PathInfo.h>
8
9#include <zypp-curl/CurlConfig>
10
11using std::endl;
12
13namespace zypp
14{
15 namespace media
16 {
17
18
20 //
21 // METHOD NAME : CurlConfig::parseConfig
22 // METHOD TYPE : int
23 //
24 int CurlConfig::parseConfig(CurlConfig & config, const std::string & filename)
25 {
26 Pathname curlrcFile;
27
28 if(filename.empty())
29 {
30 // attempts to load .curlrc from the homedir
31 char *home = getenv("HOME");
32 if(home)
33 curlrcFile = std::string( home ) + std::string( "/.curlrc" );
34 }
35 else
36 curlrcFile = filename;
37
38 PathInfo h_info(curlrcFile.dirname(), PathInfo::LSTAT);
39 PathInfo c_info(curlrcFile, PathInfo::LSTAT);
40
41 if( h_info.isDir() && h_info.userMayRX() &&
42 c_info.isFile() && c_info.userMayR() )
43 {
44 MIL << "Going to parse " << curlrcFile << endl;
45 }
46 else
47 {
48 char buf[32] = "";
49 WAR << "Not allowed to parse '" << curlrcFile
50 << "': dir/file owner: " << h_info.owner() << "/" << c_info.owner()
51 << ", process uid: " << getuid()
52 << " (" << (!getlogin_r(buf, 31) ? buf : "") << ")" << std::endl;
53
54 return 1;
55 }
56
57 std::ifstream inp(curlrcFile.c_str());
58 for(iostr::EachLine in( inp ); in; in.next())
59 {
60 std::string line = str::trim(*in);
61
62 // skip empty lines and comments
63 if (line.empty())
64 continue;
65 switch (line[0])
66 {
67 case '#':
68 case '/':
69 case '\r':
70 case '\n':
71 case '*':
72 case '\0':
73 continue;
74 }
75
76 // DBG << "line " << in.lineNo() << ": " << line << endl; // can't log passwords
77
78 const char * beg = line.c_str();
79 const char * cur = beg;
80
81// space, '=' and ':' are all valid separators in curlrc
82#define ISSEP(x) (((x)=='=') || ((x) == ':') || isspace(x))
83
84 // skip leading dashes (they are optional)
85 while (*cur && *cur == '-')
86 cur++;
87 beg = cur;
88
89 // skip non-separator characters
90 while (*cur && !ISSEP(*cur))
91 cur++;
92
93 std::string option(beg, cur - beg);
94
95 // skip separator characters
96 while (*cur && ISSEP(*cur))
97 cur++;
98
99 // rewind to the end of the line
100 beg = cur;
101 while (*cur)
102 cur++;
103
104 std::string value(beg, cur - beg);
105
106 DBG << "GOT: " << option << endl;
107
108 if (!value.empty())
109 {
110 // quoted parameter
111 if (value[0] == '\"')
112 {
113 // remove the quotes
114 std::string::size_type pos = value.rfind('\"');
115 bool cut_last =
116 pos == value.size() - 1 && pos > 1 && value[pos-1] != '\\';
117 value = value.substr(1,
118 cut_last ? value.size() - 2 : value.size() - 1);
119
120 // replace special characters:
121 pos = 0;
122 while ((pos = value.find('\\', pos)) != std::string::npos)
123 {
124 // just erase the backslash if it is found at the end
125 if (pos == value.size() - 1)
126 {
127 value = value.erase(pos, 1);
128 break;
129 }
130
131 switch(value[pos+1])
132 {
133 case 't':
134 value = value.replace(pos, 2, "\t");
135 break;
136 case 'n':
137 value = value.replace(pos, 2, "\n");
138 break;
139 case 'r':
140 value = value.replace(pos, 2, "\r");
141 break;
142 case 'v':
143 value = value.replace(pos, 2, "\v");
144 break;
145 case '\\':
146 value = value.erase(pos++, 1);
147 break;
148 default:;
149 value = value.erase(pos, 1);
150 }
151 }
152 }
153
154 // DBG << "PARAM: " << value << endl; // can't log passwords
155 }
156
157 CurlConfig::setParameter(config, option, value);
158 } // for EachLine in curlrc
159
160 return 0;
161 }
162
163
165 //
166 // METHOD NAME : CurlConfig::setParameter
167 // METHOD TYPE : int
168 //
169 int CurlConfig::setParameter(CurlConfig & config,
170 const std::string & option,
171 const std::string & value)
172 {
173 if (option == "proxy-user")
174 config.proxyuserpwd = value;
175 // add more curl config data here as they become needed
176 // else if (option == "foo")
177 else
178 DBG << "Ignoring option " << option << endl;
179
180 return 0;
181 }
182
183
184 } // namespace media
185} // namespace zypp
IOStream.h
Logger.h
DBG
#define DBG
Definition Logger.h:102
MIL
#define MIL
Definition Logger.h:103
WAR
#define WAR
Definition Logger.h:104
Pathname.h
zypp::PathInfo::LSTAT
@ LSTAT
Definition PathInfo.h:231
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::userMayR
bool userMayR() const
Definition PathInfo.h:353
zypp::filesystem::PathInfo::userMayRX
bool userMayRX() const
Definition PathInfo.h:358
zypp::filesystem::PathInfo::isDir
bool isDir() const
Definition PathInfo.h:296
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition PathInfo.h:295
zypp::filesystem::PathInfo::owner
uid_t owner() const
Definition PathInfo.h:342
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::dirname
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition IOStream.h:113
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition IOStream.cc:72
ISSEP
#define ISSEP(x)
zypp::media
Definition MediaNetworkRequestExecutor.h:22
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::media::CurlConfig
Structure holding values of curlrc options.
Definition curlconfig.h:27
zypp::media::CurlConfig::proxyuserpwd
std::string proxyuserpwd
Definition curlconfig.h:49
zypp::media::CurlConfig::setParameter
static int setParameter(CurlConfig &config, const std::string &option, const std::string &value)
Stores the value of the option in the config structure or logs an unknown option.
Definition curlconfig.cc:169
zypp::media::CurlConfig::parseConfig
static int parseConfig(CurlConfig &config, const std::string &filename="")
Parse a curlrc file and store the result in the config structure.
Definition curlconfig.cc:24
PathInfo.h