Debian Codegraph @

src/tools/davix_config_parser.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN
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
21 #include "davix_config_parser.hpp"
22 #include "davix_tool_util.hpp"
23 #include <iostream>
24 #include <sstream>
25 #include <fstream>
26
27 #define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str()
28 static const std::string delimiters = " \t\n\"\'";
29
30 // find the end of a token - special handling for quotes
31 static size_t extract_end(const std::string &contents, const size_t start) {
32 if(contents[start] == '\"' || contents[start] == '\'') {
33 size_t end = contents.find(contents[start], start+1);
34 if(end == std::string::npos) return end;
35 end++;
36
37 // hit into an escaped char?
38 if(contents[end-2] == '\\') {
39 return extract_end(contents, end-1);
40 }
41 return end;
42 }
43 else {
44 size_t end = contents.find_first_of(" \t\n", start);
45 if(end == std::string::npos) {
46 end = contents.size()+1;
47 }
48 return end;
49 }
50 }
51
52 static void escape_token(std::string &s) {
53 // no need to escape
54 if(s[0] != '\'' && s[0] != '\"') {
55 return;
56 }
57
58 std::string replacement(1, s[0]);
59 std::string target = "\\" + replacement;
60
61 size_t index = 0;
62 while( (index = s.find(target, index)) != std::string::npos) {
63 s.replace(index, 2, replacement);
64 }
65 s = s.substr(1, s.size()-2);
66 }
67
68 static size_t start_token(const std::string &contents, const size_t start=0) {
69 return contents.find_first_not_of(" \t\n", start);
70 }
71
72 static bool next_token(const std::string &contents, std::string &err, size_t start, size_t &end, size_t &next) {
73 if(start == std::string::npos) {
74 return false;
75 }
76 // tokenize by splitting on delimiters
77 end = extract_end(contents, start);
78 if(end == std::string::npos) {
79 err = SSTR("Tokenization error (mismatched quote?) near position " << start << ":" << contents.substr(start));
80 return false;
81 }
82
83 next = contents.find_first_not_of(" \t\n", end);
84 return true;
85 }
86
87 static size_t consume_macdef(const size_t start, const std::string &contents) {
88 return start_token(contents, contents.find("\n\n", start));
89 }
90
91 static void store_if_empty(std::string &source, const std::string &destination) {
92 if(source == "")
93 source = destination;
94 }
95
96 static bool string_starts_with(const std::string &target, const std::string &prefix) {
97 if(target.size() < prefix.size()) return false;
98 return std::equal(prefix.begin(), prefix.end(), target.begin());
99 }
100
101 static void store_option(const std::string &first, const std::string &second, Davix::Tool::OptParams &params) {
102 if(first == "login") {
103 store_if_empty(params.userlogpasswd.first, second);
104 }
105 else if(first == "password") {
106 store_if_empty(params.userlogpasswd.second, second);
107 }
108 else if(first == "cert") {
109 store_if_empty(params.cred_path, Davix::Tool::SanitiseTildedPath(second.c_str()));
110 }
111 else if(first == "key") {
112 store_if_empty(params.priv_key, Davix::Tool::SanitiseTildedPath(second.c_str()));
113 }
114 else if(first == "capath") {
115 params.params.addCertificateAuthorityPath(Davix::Tool::SanitiseTildedPath(second.c_str()));
116 }
117 else if(first == "s3accesskey") {
118 store_if_empty(params.aws_auth.second, second);
119 }
120 else if(first == "s3secretkey") {
121 store_if_empty(params.aws_auth.first, second);
122 }
123 else if(first == "s3region") {
124 store_if_empty(params.aws_region, second);
125 }
126 else if(first == "s3alternate") {
127 if(second == "true") params.aws_alternate = true;
128 }
129 else if(first == "s3token") {
130 store_if_empty(params.aws_token, second);
131 }
132 else if(first == "azurekey") {
133 store_if_empty(params.azure_key, second);
134 }
135 else if(first == "ostoken") {
136 store_if_empty(params.os_token, second);
137 }
138 else if(first == "osprojectid") {
139 store_if_empty(params.os_project_id, second);
140 }
141 }
142
143 namespace Davix {
144
145 std::vector<std::string> davix_config_tokenize(const std::string &contents, std::string &err) {
146 std::vector<std::string> tokens;
147 size_t start = start_token(contents), end, next;
148 while(next_token(contents, err, start, end, next)) {
149 std::string token = contents.substr(start, end-start);
150 escape_token(token);
151 tokens.push_back(token);
152
153 start = next;
154 }
155 return tokens;
156 }
157
158 // returns true if there was any match for host
159 bool davix_config_apply(const std::string &filename, const std::string &contents, const Uri &uri, Tool::OptParams &params) {
160 std::string err;
161
162 std::string prevtoken;
163 std::string hostname;
164 std::string path;
165
166 bool active = false;
167 bool in_default = false;
168
169 size_t start = start_token(contents), end, next;
170 while(next_token(contents, err, start, end, next)) {
171 std::string token = contents.substr(start, end-start);
172
173 // first token in the pair
174 if(prevtoken == "") {
175 // special case: ignore macro definitions
176 if(token == "macdef") {
177 start = consume_macdef(start, contents);
178 continue;
179 }
180 if(token == "default") {
181 if(active) return true;
182
183 if(in_default) {
184 std::cerr << "davix: Warning: Malformed config file: " << filename << ". No entries should follow after 'default'." << std::endl;
185 return false;
186 }
187 active = true;
188 in_default = true;
189 path = "";
190 std::cerr << "davix: using " << filename << " to load additional configuration. (match: default)" << std::endl;
191 }
192 else {
193 prevtoken = token;
194 }
195 }
196 // second token in the pair
197 else {
198 escape_token(token);
199
200 if(prevtoken == "machine") {
201 if(active) return true;
202 path = "";
203
204 if(token == uri.getHost()) {
205 std::cerr << "davix: using " << filename << " to load additional configuration. (match: " << uri.getHost() << ")" << std::endl;
206 hostname = token;
207 active = true;
208 }
209 }
210 else if(prevtoken == "path") {
211 // only match a single explicit path!
212 if(active && path != "" && string_starts_with(uri.getPath(), path)) return true;
213 path = token;
214 }
215 else if(active && string_starts_with(uri.getPath(), path)) {
216 store_option(prevtoken, token, params);
217 }
218 prevtoken = "";
219 }
220 start = next;
221 }
222 return active;
223 }
224
225 bool davix_config_apply(const std::string &filename, Tool::OptParams &params, const std::string &url) {
226 Uri uri(url);
227 std::string sanitized = Tool::SanitiseTildedPath(filename.c_str());
228 std::ifstream t(sanitized.c_str());
229 if(uri.getHost() != "" && t.good()) {
230 std::string contents((std::istreambuf_iterator<char>(t)),
231 std::istreambuf_iterator<char>());
232
233 return davix_config_apply(filename, contents, uri, params);
234 }
235 return false;
236 }
237
238 }