/
Proxy.ycp
421 lines (357 loc) · 11.7 KB
/
Proxy.ycp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* File: modules/Proxy.ycp
* Package: Network configuration
* Summary: Proxy data
* Authors: Michal Svec <msvec@suse.cz>
*
* $Id$
*
* Configures FTP and HTTP proxies via sysconfig & SuSEconfig
* and /root/.curlrc (for YOU)
*/
{
module "Proxy";
textdomain "network";
import "Summary";
import "Progress";
include "network/routines.ycp";
include "network/runtime.ycp";
global boolean proposal_valid = false;
global boolean write_only = false;
/**
* Data was modified?
*/
global boolean modified = false;
global boolean enabled = false;
global string http = "";
global string https = "";
global string ftp = "";
global string no = "";
global string user = "";
global string pass = "";
/**
* Display popup at the end of the proxy configuration
* @param modified true if proxy settings have been modified
define void ProxyFinishPopup(boolean modified) ``{
string text = _("Applications that support it, such as YaST2 Online Update,
use them immediately. Other applications use them after a new login
(lynx, wget). Some applications do not support these settings at all --
they use their own settings system.
");
FinishPopup(modified, "proxy", text, "", []);
}
*/
/**
* Read settings
* @return true if success
*/
global define boolean Read() {
/* Read /etc/sysconfig/proxy */
http = (string) SCR::Read(.sysconfig.proxy.HTTP_PROXY);
if(http == nil) http = "";
https = (string) SCR::Read(.sysconfig.proxy.HTTPS_PROXY);
if(https == nil) https = "";
ftp = (string) SCR::Read(.sysconfig.proxy.FTP_PROXY);
if(ftp == nil) ftp = "";
no = (string) SCR::Read(.sysconfig.proxy.NO_PROXY);
if(no == nil) no = "";
enabled = (string) SCR::Read(.sysconfig.proxy.PROXY_ENABLED) != "no";
/* Read /root/.curlrc */
if(SCR::Read(.target.size, "/root/.curlrc") > 0)
user = (string) SCR::Read (add (.root.curlrc, "--proxy-user"));
if(user == nil) user = "";
if(issubstring(user, ":")) {
pass = regexpsub(user, "^.*:(.*)$", "\\1");
user = regexpsub(user, "^(.*):.*$", "\\1");
}
if(pass == nil) pass = "";
if(user == nil)
user = "";
else {
if ( regexpmatch(user, "^.*\\\\.*$"))
user = regexpsub(user, "^(.*)\\\\(.*)$", "\\1\\2");
}
/* Read /root/.wgetrc */
/* YOU uses curl(1)
user = SCR::Read(.root.wgetrc.proxy_user);
if(user == nil) user = "";
pass = SCR::Read(.root.wgetrc.proxy_passwd);
if(pass == nil) pass = "";
*/
return true;
}
void WriteSysconfig() {
SCR::Write(.sysconfig.proxy.PROXY_ENABLED, enabled?"yes":"no");
SCR::Write(.sysconfig.proxy.HTTP_PROXY, http);
SCR::Write(.sysconfig.proxy.HTTPS_PROXY, https);
SCR::Write(.sysconfig.proxy.FTP_PROXY, ftp);
SCR::Write(.sysconfig.proxy.NO_PROXY, no);
SCR::Write(.sysconfig.proxy, nil);
}
/*
* Escape backslash characters in .curlrc (bnc#331038)
* see also http://curl.haxx.se/docs/manpage.html#-K for escaping rules
*/
string EscapeForCurlrc(string s) {
return mergestring (splitstring(s, "\\"), "\\\\");
}
void WriteCurlrc() {
string proxyuser = nil;
if (user != "") {
user = EscapeForCurlrc(user); // FIXME this clobbers the master value
proxyuser = user;
if (pass != "") proxyuser = user + ":" + pass;
}
map<string, string> options = $[
"--proxy-user": proxyuser,
// bnc#305163
"--proxy": http
];
// proxy is used, write /root/.curlrc
if (enabled) {
boolean write_comment = true;
foreach(string option, string value, options, {
if (value == "") {
value = nil;
}
SCR::Write (add (.root.curlrc, option), value);
if (value != nil && write_comment) {
SCR::Write (add (add (.root.curlrc, option), "comment"),
ChangedComment("proxy"));
write_comment = false;
}
});
// proxy is not used, remove proxy-related settings
} else {
foreach(string option, string value, options, {
SCR::Write (add (.root.curlrc, option), nil);
});
}
SCR::Write(.root.curlrc, nil);
}
/**
* Write routing settings and apply changes
* @return true if success
*/
global define boolean Write() {
y2milestone("Writing configuration");
if(!modified) {
y2milestone("No changes to proxy configuration -> nothing to write");
return true;
}
list <string> steps = [
_("Update proxy configuration"),
];
string caption = _("Saving Proxy Configuration");
// sleep for longer time, so that progress does not disappear right afterwards
// but only when Progress is visible
integer sl = (Progress::status() == true ? 500 : 0);
Progress::New(caption, " ", size(steps), steps, [], "");
ProgressNextStage(_("Updating proxy configuration..."));
/* Update /etc/sysconfig/proxy */
WriteSysconfig();
WriteCurlrc();
sleep(sl);
Progress::NextStage();
/*if (!Mode::config ())
ProxyFinishPopup(true); */
/* Update /root/.wgetrc */
/* YOU uses curl(1)
SCR::Write(.root.wgetrc.proxy_user, user);
SCR::Write(.root.wgetrc.proxy_passwd, pass);
SCR::Write(.root.wgetrc, nil); */
modified = false;
return true;
}
/**
* Get all settings from a map.
* When called by <name>_auto (preparing autoinstallation data)
* the map may be empty.
* @param settings autoinstallation settings
* @return true if success
*/
global define boolean Import(map settings) {
enabled = settings["enabled"]:false;
http = settings["http_proxy"]:"";
https = settings["https_proxy"]:"";
ftp = settings["ftp_proxy"]:"";
no = settings["no_proxy"]:"localhost";
user = settings["proxy_user"]:"";
pass = settings["proxy_password"]:"";
modified = true;
return true;
}
/**
* Runs tests of the HTTP and FTP proxy
*
* @param string http_proxy such as "http://cache.example.com:3128"
* @param string https_proxy such as "http://cache.example.com:3128"
* @param string ftp_proxy such as "http://cache.example.com:3128"
* @param string proxy_user such as "proxy-username"
* @param string proxy_password such as "proxy-password"
*
* @return map <string, map <string, any> > with results of the test
* @struct return = $[
* "HTTP" : $[
* "exit" : _exit_code,
* "stdout" : _stdout,
* "stderr" : _stderr,
* ],
* "HTTPS" : $[
* "exit" : _exit_code,
* "stdout" : _stdout,
* "stderr" : _stderr,
* ],
* "FTP" : $[
* "exit" : _exit_code,
* "stdout" : _stdout,
* "stderr" : _stderr,
* ],
* ]
*/
global define map <string, map <string, any> > RunTestProxy (string http_proxy, string https_proxy, string ftp_proxy, string proxy_user, string proxy_password) {
// /usr/bin/curl --verbose
// --proxy http://server_name:port_number
// --proxy-user user:password
// --url http://www.novell.com or ftp://ftp.novell.com | suggested for HTTP or FTP test
// --url https://secure-www.novell.com --insecure
map <string, map <string, any> > ret = $[];
boolean test_http = ((http_proxy != "" && http_proxy != "http://") ? true:false);
boolean test_https = ((https_proxy != "" && https_proxy != "http://") ? true:false);
boolean test_ftp = ((ftp_proxy != "" && ftp_proxy != "http://") ? true:false);
http_proxy = mergestring (splitstring(http_proxy, "\""), "\\\"");
https_proxy = mergestring (splitstring(https_proxy, "\""), "\\\"");
ftp_proxy = mergestring (splitstring(ftp_proxy, "\""), "\\\"");
proxy_user = mergestring (splitstring(proxy_user, "\""), "\\\"");
//escape also '\' character - usernames such as domain\user are causing pain to .target.bash_output
//and to curl - #256360
proxy_user = mergestring (splitstring(proxy_user, "\\"), "\\\\");
proxy_password = mergestring (splitstring(proxy_password, "\""), "\\\"");
string user_pass = (proxy_user != "" ?
" --proxy-user '" + proxy_user + (proxy_password != "" ?
":" + proxy_password
:
"") + "'"
:
"");
// timeout for the connection
integer timeout_sec = 90;
// %1 = http or ftp proxy, %2 = user:password if any, %3 = URL
// enclose user:password into quotes, it may contain special characters (#338264)
string command = "curl --verbose --proxy %1 %2 --connect-timeout %3 --url %4";
string http_command = sformat(command, http_proxy, user_pass, timeout_sec, "http://www.novell.com");
// adding option --insecure to accept the certificate without asking
string https_command = sformat(command, https_proxy, user_pass, timeout_sec, "https://secure-www.novell.com --insecure");
string ftp_command = sformat(command, ftp_proxy, user_pass, timeout_sec, "ftp://ftp.novell.com");
y2milestone("Running HTTP_PROXY test...");
if (test_http) {
y2milestone("Testing HTTP proxy %1", http_proxy);
ret["HTTP"] = (map <string, any>) SCR::Execute(.target.bash_output, http_command);
} else {
y2milestone("Skipping HTTP Proxy test, no proxy used.");
ret["HTTP"] = $["exit":0, "stderr":"", "stdout":"", "tested":false];
}
y2milestone("Done.");
y2milestone("Running HTTPS_PROXY test...");
if (test_https) {
y2milestone("Testing HTTPS proxy %1", https_proxy);
ret["HTTPS"] = (map <string, any>) SCR::Execute(.target.bash_output, https_command);
} else {
y2milestone("Skipping HTTPS Proxy test, no proxy used.");
ret["HTTPS"] = $["exit":0, "stderr":"", "stdout":"", "tested":false];
}
y2milestone("Done.");
y2milestone("Running FTP_PROXY test...");
if (test_ftp) {
y2milestone("Testing FTP proxy %1", ftp_proxy);
ret["FTP"] = (map <string, any>) SCR::Execute(.target.bash_output, ftp_command);
} else {
y2milestone("Skipping FTP Proxy test, no proxy used.");
ret["FTP"] = $["exit":0, "stderr":"", "stdout":"", "tested":false];
}
y2milestone("Done.");
return ret;
}
/**
* Dump the Routing settings to a map, for autoinstallation use.
* @return autoinstallation settings
*/
global define map Export() {
map settings = $[
"enabled" : enabled,
"http_proxy" : http,
"https_proxy" : https,
"ftp_proxy" : ftp,
"no_proxy" : no,
"proxy_user" : user,
"proxy_password": pass,
];
return settings;
}
/**
* Create proxy summary
* @return summary text
*/
global define string Summary() {
list<string> ret = [];
/* Summary text */
if(!enabled) ret = [ Summary::Device(_("Proxy is disabled."), "") ];
else {
/* Summary text */
ret = [ Summary::Device(_("Proxy is enabled."), "" +
/* Summary text */
(http != "" ? sformat(_("HTTP Proxy: %1"), http) + "<br>" : "" ) +
/* Summary text */
(https != "" ? sformat(_("HTTPS Proxy: %1"), https) + "<br>" : "" ) +
/* Summary text */
(ftp != "" ? sformat(_("FTP Proxy: %1"), ftp) + "<br>" : "" ) +
"" )];
/* Summary text * /
Summary::Device(sformat(_("No Proxy Domains: %1"), no) + "\n<br>" +
"<p>" + ( user == "" ?
/* Summary text * /
_("Proxy user name is not set.") :
/* Summary text * /
sformat(_("Proxy User Name: %1"), user)) +
"<br>" + ( pass == "" ?
/* Summary text * /
_("Proxy password is not set.") :
/* Summary text * /
_("Proxy password is set.")) ];
*/
}
return Summary::DevicesList(ret);
}
/**
* Function which returns if the settings were modified
* @return boolean settings were modified
*/
global define boolean GetModified () {
return modified;
}
/**
* Function sets internal variable, which indicates, that any
* settings were modified, to "true"
*/
global define void SetModified () {
modified = true;
}
/**
* Function returns an environment usable for curl. The proxy user/password
* are read from /root/.curlrc.
*/
global define map <string, string> GetEnvironment ()
{
if (!enabled)
return $[];
if (!modified)
Read ();
return $[
"http_proxy" : http,
"HTTPS_PROXY" : https,
"FTP_PROXY" : ftp,
"NO_PROXY" : no
];
}
/* EOF */
}