Debian Codegraph @

src/net123_winhttp.c

raw
1 #include "config.h"
2 #include "version.h"
3 #include "net123.h"
4 #include "compat/compat.h"
5 #include "common/debug.h"
6 #include <ws2tcpip.h>
7 #include <winhttp.h>
8
9 // The network implementation defines the struct for private use.
10 // The purpose is just to keep enough context to be able to
11 // call net123_read() and net123_close() afterwards.
12 #define URL_COMPONENTS_LENGTH 255
13 typedef struct {
14 HINTERNET session;
15 HINTERNET connect;
16 HINTERNET request;
17 URL_COMPONENTS comps;
18 wchar_t lpszHostName[URL_COMPONENTS_LENGTH];
19 wchar_t lpszUserName[URL_COMPONENTS_LENGTH];
20 wchar_t lpszPassword[URL_COMPONENTS_LENGTH];
21 wchar_t lpszUrlPath[URL_COMPONENTS_LENGTH];
22 wchar_t lpszExtraInfo[URL_COMPONENTS_LENGTH];
23 DWORD supportedAuth, firstAuth, authTarget, authTried;
24 char *headers;
25 size_t headers_pos, headers_len;
26 DWORD internetStatus, internetStatusLength;
27 LPVOID additionalInfo;
28 } winhttp_handle;
29
30 #define MPG123CONCAT_(x,y) x ## y
31 #define MPG123CONCAT(x,y) MPG123CONCAT_(x,y)
32 #define MPG123STRINGIFY_(x) #x
33 #define MPG123STRINGIFY(x) MPG123STRINGIFY_(x)
34 #define MPG123WSTR(x) MPG123CONCAT(L,MPG123STRINGIFY(x))
35
36 static DWORD wrap_auth(winhttp_handle *nh){
37 DWORD mode;
38 DWORD ret;
39
40 if(nh->comps.dwUserNameLength) {
41 if(!nh->authTried) {
42 ret = WinHttpQueryAuthSchemes(nh->request, &nh->supportedAuth, &nh->firstAuth, &nh->authTarget);
43 if(!ret) return GetLastError();
44 nh->authTried = 1;
45 }
46
47 mode = nh->supportedAuth & WINHTTP_AUTH_SCHEME_DIGEST ? WINHTTP_AUTH_SCHEME_DIGEST :
48 nh->supportedAuth & WINHTTP_AUTH_SCHEME_BASIC ? WINHTTP_AUTH_SCHEME_BASIC : 0;
49
50 /* no supported mode? */
51 if(!mode)
52 return ERROR_WINHTTP_INTERNAL_ERROR;
53
54 ret = WinHttpSetCredentials(nh->request, WINHTTP_AUTH_TARGET_SERVER, mode, nh->comps.lpszUserName, nh->comps.lpszPassword, NULL);
55 return GetLastError();
56 }
57 return TRUE;
58 }
59
60 #if DEBUG
61 static void debug_crack(URL_COMPONENTS *comps){
62 wprintf(L"dwStructSize: %lu\n", comps->dwStructSize);
63 //wprintf(L"lpszScheme: %s\n", comps->lpszScheme ? comps->lpszScheme : L"null");
64 //wprintf(L"dwSchemeLength: %lu\n", comps->dwSchemeLength);
65 wprintf(L"nScheme: %u %s\n", comps->nScheme, comps->nScheme == 1 ? L"INTERNET_SCHEME_HTTP": comps->nScheme == 2 ? L"INTERNET_SCHEME_HTTPS" : L"UNKNOWN");
66 wprintf(L"lpszHostName: %s\n", comps->lpszHostName ? comps->lpszHostName : L"null");
67 wprintf(L"dwHostNameLength: %u\n", comps->dwHostNameLength);
68 wprintf(L"nPort: %u\n", comps->nPort);
69 wprintf(L"lpszUserName: %s\n", comps->lpszUserName ? comps->lpszUserName : L"null");
70 wprintf(L"dwUserNameLength: %lu\n", comps->dwUserNameLength);
71 wprintf(L"lpszPassword: %s\n", comps->lpszPassword ? comps->lpszPassword : L"null");
72 wprintf(L"dwPasswordLength: %lu\n", comps->dwPasswordLength);
73 wprintf(L"lpszUrlPath: %s\n", comps->lpszUrlPath ? comps->lpszUrlPath : L"null");
74 wprintf(L"dwUrlPathLength: %lu\n", comps->dwUrlPathLength);
75 wprintf(L"lpszExtraInfo: %s\n", comps->lpszExtraInfo? comps->lpszExtraInfo : L"null");
76 wprintf(L"dwExtraInfoLength: %lu\n", comps->dwExtraInfoLength);
77 }
78 #else
79 static void debug_crack(URL_COMPONENTS *comps){}
80 #endif
81
82 static
83 void WINAPI net123_ssl_errors(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength){
84 winhttp_handle *nh = (winhttp_handle *)dwContext;
85 nh->internetStatus = dwInternetStatus;
86 nh->additionalInfo = lpvStatusInformation;
87 nh->internetStatusLength = dwStatusInformationLength;
88 }
89
90 static size_t net123_read(net123_handle *nh, void *buf, size_t bufsize);
91 static void net123_close(net123_handle *nh);
92
93 net123_handle *net123_open_winhttp(const char *url, const char * const *client_head){
94 LPWSTR urlW = NULL, headers = NULL;
95 size_t ii;
96 WINBOOL res;
97 DWORD headerlen;
98 const LPCWSTR useragent = MPG123WSTR(PACKAGE_NAME) L"/" MPG123WSTR(MPG123_VERSION);
99 WINHTTP_STATUS_CALLBACK cb;
100 net123_handle *handle = NULL;
101
102 if(!WinHttpCheckPlatform())
103 return NULL;
104
105 INT123_win32_utf8_wide(url, &urlW, NULL);
106 if(urlW == NULL) goto cleanup;
107
108 winhttp_handle *ret = calloc(1, sizeof(winhttp_handle));
109 if (!ret) goto cleanup;
110
111 handle = calloc(1, sizeof(net123_handle));
112 if (!handle) {
113 free(ret);
114 goto cleanup;
115 }
116
117 handle->parts = ret;
118 handle->read = net123_read;
119 handle->close = net123_close;
120
121 ret->comps.dwStructSize = sizeof(ret->comps);
122 ret->comps.dwSchemeLength = 0;
123 ret->comps.dwUserNameLength = URL_COMPONENTS_LENGTH - 1;
124 ret->comps.dwPasswordLength = URL_COMPONENTS_LENGTH - 1;
125 ret->comps.dwHostNameLength = URL_COMPONENTS_LENGTH - 1;
126 ret->comps.dwUrlPathLength = URL_COMPONENTS_LENGTH - 1;
127 ret->comps.dwExtraInfoLength = URL_COMPONENTS_LENGTH - 1;
128 ret->comps.lpszHostName = ret->lpszHostName;
129 ret->comps.lpszUserName = ret->lpszUserName;
130 ret->comps.lpszPassword = ret->lpszPassword;
131 ret->comps.lpszUrlPath = ret->lpszUrlPath;
132 ret->comps.lpszExtraInfo = ret->lpszExtraInfo;
133
134 debug1("net123_open start crack %S", urlW);
135
136 if(!(res = WinHttpCrackUrl(urlW, 0, 0, &ret->comps))) {
137 debug1("net123_open crack fail %lu", GetLastError());
138 goto cleanup;
139 }
140
141 debug("net123_open crack OK");
142 debug_crack(&ret->comps);
143
144 ret->session = WinHttpOpen(useragent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
145 free(urlW);
146 urlW = NULL;
147 debug("net123_open WinHttpOpen OK");
148 if(!ret->session) goto cleanup;
149
150 debug2("net123_open WinHttpConnect %S %u", ret->comps.lpszHostName, ret->comps.nPort);
151 ret->connect = WinHttpConnect(ret->session, ret->comps.lpszHostName, ret->comps.nPort, 0);
152 if(!ret->connect) goto cleanup;
153 debug("net123_open WinHttpConnect OK");
154
155 debug1("WinHttpOpenRequest GET %S", ret->comps.lpszUrlPath);
156 ret->request = WinHttpOpenRequest(ret->connect, L"GET", ret->comps.lpszUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, ret->comps.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
157 if(!ret->request) goto cleanup;
158 debug("WinHttpOpenRequest GET OK");
159
160 cb = WinHttpSetStatusCallback(ret->request, (WINHTTP_STATUS_CALLBACK)net123_ssl_errors, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE, 0);
161 if(cb == WINHTTP_INVALID_STATUS_CALLBACK){
162 error1("WinHttpSetStatusCallback failed to install callback, errors might not be reported properly! (%lu)", GetLastError());
163 }
164
165 wrap_auth(ret);
166
167 for(ii = 0; client_head[ii]; ii++){
168 INT123_win32_utf8_wide(client_head[ii], &headers, NULL);
169 if(!headers)
170 goto cleanup;
171 debug1("WinHttpAddRequestHeaders add %S", headers);
172 res = WinHttpAddRequestHeaders(ret->request, headers, (DWORD) -1, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
173 debug2("WinHttpAddRequestHeaders returns %u %lu", res, res ? 0 : GetLastError());
174 free(headers);
175 headers = NULL;
176 }
177
178 debug("net123_open ADD HEADERS OK");
179
180 res = WinHttpSendRequest(ret->request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, (DWORD_PTR)ret);
181
182 if (!res) {
183 res = GetLastError();
184 error1("WinHttpSendRequest failed with %d", res);
185 if(res == ERROR_WINHTTP_SECURE_FAILURE){
186 res = *(DWORD *)ret->additionalInfo;
187 error("Additionally, the ERROR_WINHTTP_SECURE_FAILURE failed with:");
188 if (res & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED) error(" WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED");
189 if (res & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT) error(" WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT");
190 if (res & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED) error(" WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED");
191 if (res & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA) error(" WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA");
192 if (res & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID) error(" WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID");
193 if (res & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID) error(" WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID");
194 if (res & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR) error(" WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR");
195 }
196 goto cleanup;
197 }
198
199 res = WinHttpReceiveResponse(ret->request, NULL);
200
201 if (!res) {
202 error1("WinHttpReceiveResponse failed with %lu", GetLastError());
203 goto cleanup;
204 }
205
206 res = WinHttpQueryHeaders(ret->request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, WINHTTP_NO_OUTPUT_BUFFER, &headerlen, WINHTTP_NO_HEADER_INDEX);
207
208 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER && headerlen > 0) {
209 headers = calloc(1, headerlen);
210 if (!headers) goto cleanup;
211 WinHttpQueryHeaders(ret->request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, headers, &headerlen, WINHTTP_NO_HEADER_INDEX);
212 INT123_win32_wide_utf7(headers, &ret->headers, &ret->headers_len);
213 /* bytes written, skip the terminating null, we want to stop at the \r\n\r\n */
214 ret->headers_len --;
215 free(headers);
216 headers = NULL;
217 } else {
218 error("WinHttpQueryHeaders did not execute as expected");
219 goto cleanup;
220 }
221 debug("net123_open OK");
222
223 return handle;
224 cleanup:
225 debug("net123_open error");
226 if (urlW) free(urlW);
227 if (handle) {
228 net123_close(handle);
229 handle = NULL;
230 }
231 return handle;
232 }
233
234 // Read data into buffer, return bytes read.
235 // This handles interrupts (EAGAIN, EINTR, ..) internally and only returns
236 // a short byte count on EOF or error. End of file or error is not distinguished:
237 // For the user, it only matters if there will be more bytes or not.
238 // Feel free to communicate errors via error() / merror() functions inside.
239 size_t net123_read(net123_handle *nh, void *buf, size_t bufsize){
240 winhttp_handle *h = nh->parts;
241 size_t ret;
242 size_t to_copy = h->headers_len - h->headers_pos;
243 DWORD bytesread = 0;
244
245 if(to_copy){
246 ret = to_copy <= bufsize ? to_copy : bufsize;
247 memcpy(buf, h->headers + h->headers_pos, ret);
248 h->headers_pos += ret;
249 return ret;
250 }
251
252 /* is this needed? */
253 to_copy = bufsize > ULONG_MAX ? ULONG_MAX : bufsize;
254 if(!WinHttpReadData(h->request, buf, to_copy, &bytesread)){
255 return EOF;
256 }
257 return bytesread;
258 }
259
260 // Call that to free up resources, end processes.
261 void net123_close(net123_handle *nh){
262 if (!nh) return;
263 winhttp_handle *h = nh->parts;
264 if(h) {
265 if(h->headers) {
266 free(h->headers);
267 h->headers = NULL;
268 }
269 if(h->request) {
270 WinHttpCloseHandle(h->request);
271 h->request = NULL;
272 }
273 if(h->connect) {
274 WinHttpCloseHandle(h->connect);
275 h->connect = NULL;
276 }
277 if(h->session) {
278 WinHttpCloseHandle(h->session);
279 h->session = NULL;
280 }
281 free(h);
282 }
283 free(nh);
284 }