1 |
werner |
36 |
/* wptProxySettingsDlg.cpp - Dialog for the proxy settings |
2 |
twoaday |
225 |
* Copyright (C) 2002-2006 Timo Schulz |
3 |
werner |
36 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
*/ |
16 |
|
|
#ifdef HAVE_CONFIG_H |
17 |
|
|
#include <config.h> |
18 |
|
|
#endif |
19 |
|
|
|
20 |
|
|
#include <windows.h> |
21 |
werner |
47 |
#include <ctype.h> |
22 |
twoaday |
270 |
#include <stdio.h> |
23 |
werner |
36 |
|
24 |
werner |
47 |
#include "resource.h" |
25 |
werner |
36 |
#include "wptTypes.h" |
26 |
twoaday |
271 |
#include "wptGPG.h" |
27 |
werner |
36 |
#include "wptKeyserver.h" |
28 |
|
|
#include "wptW32API.h" |
29 |
|
|
#include "wptErrors.h" |
30 |
|
|
#include "wptNLS.h" |
31 |
twoaday |
180 |
#include "wptRegistry.h" |
32 |
werner |
36 |
|
33 |
twoaday |
328 |
// TODO: there are segfault problems in the password mode |
34 |
|
|
|
35 |
twoaday |
185 |
#define combo_get_sel(dlg, id) \ |
36 |
|
|
SendDlgItemMessage ((dlg), (id), CB_GETCURSEL, 0, 0) |
37 |
werner |
36 |
|
38 |
twoaday |
225 |
|
39 |
twoaday |
270 |
static int |
40 |
|
|
set_reg_entry_keyserver_int (const char *key, int val) |
41 |
|
|
{ |
42 |
|
|
char buf[32]; |
43 |
|
|
|
44 |
|
|
_snprintf (buf, DIM (buf)-1, "%d", val); |
45 |
|
|
return set_reg_entry_keyserver (key, buf); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/* Save the proxy settings in the registry. */ |
50 |
|
|
int |
51 |
|
|
set_reg_proxy_prefs (keyserver_proxy_t prox) |
52 |
|
|
{ |
53 |
|
|
int rc; |
54 |
|
|
|
55 |
|
|
rc = set_reg_entry_keyserver_int ("Proto", prox->proto); |
56 |
|
|
if (!rc) |
57 |
|
|
rc = set_reg_entry_keyserver ("Host", prox->host? prox->host : ""); |
58 |
|
|
if (!rc) |
59 |
|
|
rc = set_reg_entry_keyserver_int ("Port", prox->port); |
60 |
|
|
if (!rc) |
61 |
|
|
rc = set_reg_entry_keyserver ("User", prox->user? prox->user : ""); |
62 |
|
|
if (!rc) |
63 |
|
|
rc = set_reg_entry_keyserver ("Pass", prox->pass? prox->pass : ""); |
64 |
|
|
return rc; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
twoaday |
225 |
/* Check if the edit control (@id) contains a valid number. */ |
69 |
werner |
36 |
static int |
70 |
|
|
check_number (HWND dlg, int id) |
71 |
|
|
{ |
72 |
|
|
char buf[32]; |
73 |
|
|
size_t i; |
74 |
|
|
|
75 |
twoaday |
270 |
i = GetDlgItemText (dlg, id, buf, DIM (buf)-1); |
76 |
twoaday |
225 |
if (!i) |
77 |
|
|
return -1; |
78 |
twoaday |
270 |
for (i = 0; i < strlen (buf); i++) { |
79 |
twoaday |
225 |
if (!isdigit (buf[i])) |
80 |
werner |
36 |
return -1; |
81 |
|
|
} |
82 |
|
|
return 0; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
static int |
87 |
twoaday |
180 |
read_proxy (HWND dlg, keyserver_proxy_t ctx) |
88 |
werner |
36 |
{ |
89 |
|
|
char t[512]; |
90 |
twoaday |
273 |
int ncount; |
91 |
|
|
|
92 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_PWD, t, DIM (t)-1); |
93 |
|
|
if (ncount > 0) |
94 |
twoaday |
225 |
ctx->pass = m_strdup (t); |
95 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_USER, t, DIM (t)-1); |
96 |
|
|
if (ncount > 0) |
97 |
twoaday |
225 |
ctx->user = m_strdup (t); |
98 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_HOST, t, DIM (t)-1); |
99 |
|
|
if (ncount > 0) { |
100 |
twoaday |
273 |
int pos = 0; |
101 |
|
|
|
102 |
|
|
if (check_inet_address (t)) { |
103 |
|
|
msg_box (dlg, _("Invalid host or IP address."), _("Proxy Settings"), MB_ERR); |
104 |
werner |
36 |
return -1; |
105 |
|
|
} |
106 |
twoaday |
328 |
/* TODO: check prefix */ |
107 |
|
|
if (!strnicmp (t, "http://", 7)) |
108 |
werner |
36 |
pos = 7; |
109 |
twoaday |
225 |
ctx->host = m_strdup (t + pos); |
110 |
werner |
36 |
} |
111 |
twoaday |
185 |
else { |
112 |
|
|
msg_box (dlg, _("Please enter the proxy hostname."), |
113 |
|
|
_("Proxy Settings"), MB_ERR); |
114 |
|
|
return -1; |
115 |
|
|
} |
116 |
|
|
|
117 |
werner |
36 |
if (check_number (dlg, IDC_PROXY_PORT)) { |
118 |
|
|
msg_box (dlg, _("Invalid port number."), _("Proxy Settings"), MB_ERR); |
119 |
|
|
return -1; |
120 |
|
|
} |
121 |
|
|
|
122 |
twoaday |
180 |
ctx->port = GetDlgItemInt (dlg, IDC_PROXY_PORT, NULL, FALSE); |
123 |
|
|
if (ctx->port < 0 || ctx->port > 65535) { |
124 |
werner |
36 |
msg_box (dlg, _("Please select a value from 0-65535 for the port"), |
125 |
|
|
_("Proxy Settings"), MB_INFO); |
126 |
|
|
return -1; |
127 |
|
|
} |
128 |
twoaday |
328 |
if ((ctx->user && !ctx->pass) || (!ctx->user && ctx->pass)) { |
129 |
werner |
36 |
msg_box (dlg, _("When you want to use authentication, " |
130 |
|
|
"please fill out both fields."), _("Proxy Settings"), MB_ERR); |
131 |
|
|
return -1; |
132 |
|
|
} |
133 |
twoaday |
328 |
if ((ctx->host && ctx->port==0) || (!ctx->host && ctx->port != 0)) { |
134 |
werner |
36 |
msg_box (dlg, _("Please enter a host name and a port."), |
135 |
|
|
_("Proxy Settings"), MB_INFO); |
136 |
|
|
return -1; |
137 |
|
|
} |
138 |
twoaday |
185 |
ctx->proto = combo_get_sel (dlg, IDC_PROXY_PROTO); |
139 |
werner |
36 |
return 0; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
twoaday |
225 |
/* Enable or disable the authentication elements based on |
144 |
|
|
the value @val. */ |
145 |
werner |
36 |
static void |
146 |
|
|
enable_proxy_auth (HWND dlg, int val) |
147 |
|
|
{ |
148 |
|
|
int mode = val? TRUE : FALSE; |
149 |
twoaday |
273 |
|
150 |
werner |
36 |
EnableWindow (GetDlgItem (dlg, IDC_PROXY_PWD), mode); |
151 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PROXY_USER), mode); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
|
155 |
twoaday |
225 |
/* Fill in all valid proxy protocols. */ |
156 |
twoaday |
180 |
static void |
157 |
|
|
init_proxy_protocols (HWND dlg) |
158 |
|
|
{ |
159 |
|
|
HWND cb = GetDlgItem (dlg, IDC_PROXY_PROTO); |
160 |
twoaday |
273 |
|
161 |
twoaday |
185 |
combox_add_string (cb, (char *)"NONE"); |
162 |
twoaday |
180 |
combox_add_string (cb, (char *)"HTTP"); |
163 |
twoaday |
328 |
// TODO: combox_add_string (cb, (char *)"SOCKS5"); |
164 |
twoaday |
180 |
SendMessage (cb, CB_SETCURSEL, (WPARAM)0, 0); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
werner |
36 |
/* Dialog box procedure for proxy authentication. */ |
169 |
|
|
BOOL CALLBACK |
170 |
|
|
keyserver_proxy_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
171 |
twoaday |
180 |
{ |
172 |
|
|
int auth = 0; |
173 |
|
|
|
174 |
werner |
36 |
switch (msg) { |
175 |
|
|
case WM_INITDIALOG: |
176 |
twoaday |
180 |
if (proxy.host != NULL) { |
177 |
|
|
SetDlgItemText (dlg, IDC_PROXY_HOST, proxy.host); |
178 |
|
|
SetDlgItemInt( dlg, IDC_PROXY_PORT, proxy.port, FALSE ); |
179 |
|
|
if (proxy.user != NULL) { |
180 |
|
|
SetDlgItemText (dlg, IDC_PROXY_USER, proxy.user); |
181 |
werner |
36 |
CheckDlgButton (dlg, IDC_PROXY_AUTH, BST_CHECKED); |
182 |
|
|
auth++; |
183 |
|
|
} |
184 |
twoaday |
180 |
if (proxy.pass != NULL) { |
185 |
|
|
SetDlgItemText (dlg, IDC_PROXY_PWD, proxy.pass); |
186 |
werner |
36 |
auth++; |
187 |
|
|
} |
188 |
|
|
} |
189 |
twoaday |
180 |
enable_proxy_auth (dlg, auth); |
190 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_PROXY_HOSTINF, _("Proxy host name or IP address")); |
191 |
werner |
36 |
SetDlgItemText (dlg, IDC_PROXY_AUTH, _("Server requires &authentication")); |
192 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_PROXY_USERINF, _("User name")); |
193 |
|
|
SetDlgItemText (dlg, IDC_PROXY_PWDINF, _("Password")); |
194 |
twoaday |
180 |
SetDlgItemText (dlg, IDC_PROXY_PROTOINF, _("Proxy type")); |
195 |
|
|
SetDlgItemText (dlg, IDC_PROXY_AUTHINF, _("Authentication")); |
196 |
werner |
36 |
SetWindowText (dlg, _("Proxy Settings")); |
197 |
twoaday |
180 |
init_proxy_protocols (dlg); |
198 |
twoaday |
185 |
SendDlgItemMessage (dlg, IDC_PROXY_PROTO, CB_SETCURSEL, |
199 |
|
|
(WPARAM)proxy.proto, 0); |
200 |
twoaday |
78 |
center_window (dlg, NULL); |
201 |
werner |
36 |
SetForegroundWindow (dlg); |
202 |
|
|
break; |
203 |
|
|
|
204 |
|
|
case WM_COMMAND: |
205 |
|
|
switch (HIWORD (wparam)) { |
206 |
twoaday |
185 |
case CBN_SELCHANGE: |
207 |
|
|
if (combo_get_sel (dlg, (int)LOWORD (wparam))) { |
208 |
|
|
} |
209 |
|
|
else { |
210 |
|
|
} |
211 |
|
|
break; |
212 |
|
|
|
213 |
werner |
36 |
case BN_CLICKED: |
214 |
twoaday |
180 |
switch ((int)LOWORD (wparam)) { |
215 |
werner |
36 |
case IDC_PROXY_AUTH: |
216 |
|
|
if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH)) |
217 |
|
|
enable_proxy_auth (dlg, 1); |
218 |
|
|
else |
219 |
|
|
enable_proxy_auth (dlg, 0); |
220 |
|
|
break; |
221 |
|
|
} |
222 |
|
|
break; |
223 |
|
|
} |
224 |
|
|
|
225 |
twoaday |
180 |
switch (LOWORD (wparam)) { |
226 |
werner |
36 |
case IDOK: |
227 |
|
|
if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH) && |
228 |
|
|
item_get_text_length (dlg, IDC_PROXY_USER) == 0 && |
229 |
|
|
item_get_text_length (dlg, IDC_PROXY_PWD) == 0) { |
230 |
twoaday |
328 |
msg_box (dlg, _("Please fill out all required fields to enable proxy authentication."), |
231 |
werner |
36 |
_("Proxy Settings"), MB_WARN); |
232 |
|
|
return TRUE; |
233 |
|
|
} |
234 |
twoaday |
180 |
kserver_proxy_release (&proxy); |
235 |
twoaday |
185 |
if (combo_get_sel (dlg, IDC_PROXY_PROTO) != PROXY_PROTO_NONE) { |
236 |
|
|
if (read_proxy (dlg, &proxy)) |
237 |
|
|
return FALSE; |
238 |
|
|
} |
239 |
twoaday |
180 |
set_reg_proxy_prefs (&proxy); |
240 |
werner |
36 |
EndDialog (dlg, TRUE); |
241 |
|
|
return TRUE; |
242 |
|
|
|
243 |
|
|
case IDCANCEL: |
244 |
|
|
EndDialog (dlg, FALSE); |
245 |
|
|
return FALSE; |
246 |
|
|
} |
247 |
|
|
break; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
return FALSE; |
251 |
|
|
} |