/src/FreeRDP/libfreerdp/core/freerdp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * FreeRDP Core |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include "settings.h" |
25 | | |
26 | | #include <string.h> |
27 | | #include <stdarg.h> |
28 | | #include <time.h> |
29 | | |
30 | | #include "rdp.h" |
31 | | #include "input.h" |
32 | | #include "update.h" |
33 | | #include "surface.h" |
34 | | #include "transport.h" |
35 | | #include "connection.h" |
36 | | #include "message.h" |
37 | | #include <freerdp/buildflags.h> |
38 | | #include "gateway/rpc_fault.h" |
39 | | |
40 | | #include <winpr/assert.h> |
41 | | |
42 | | #include <winpr/crt.h> |
43 | | #include <winpr/string.h> |
44 | | #include <winpr/stream.h> |
45 | | #include <winpr/wtsapi.h> |
46 | | #include <winpr/ssl.h> |
47 | | #include <winpr/debug.h> |
48 | | |
49 | | #include <freerdp/freerdp.h> |
50 | | #include <freerdp/streamdump.h> |
51 | | #include <freerdp/error.h> |
52 | | #include <freerdp/event.h> |
53 | | #include <freerdp/locale/keyboard.h> |
54 | | #include <freerdp/locale/locale.h> |
55 | | #include <freerdp/channels/channels.h> |
56 | | #include <freerdp/version.h> |
57 | | #include <freerdp/log.h> |
58 | | #include <freerdp/utils/signal.h> |
59 | | |
60 | | #include "../cache/pointer.h" |
61 | | #include "utils.h" |
62 | | |
63 | 0 | #define TAG FREERDP_TAG("core") |
64 | | |
65 | | static void sig_abort_connect(int signum, const char* signame, void* ctx) |
66 | 0 | { |
67 | 0 | rdpContext* context = (rdpContext*)ctx; |
68 | |
|
69 | 0 | WLog_INFO(TAG, "Signal %s [%d], terminating session %p", signame, signum, context); |
70 | 0 | if (context) |
71 | 0 | freerdp_abort_connect_context(context); |
72 | 0 | } |
73 | | |
74 | | /** Creates a new connection based on the settings found in the "instance" parameter |
75 | | * It will use the callbacks registered on the structure to process the pre/post connect operations |
76 | | * that the caller requires. |
77 | | * @see struct rdp_freerdp in freerdp.h |
78 | | * |
79 | | * @param instance - pointer to a rdp_freerdp structure that contains base information to establish |
80 | | * the connection. On return, this function will be initialized with the new connection's settings. |
81 | | * |
82 | | * @return TRUE if successful. FALSE otherwise. |
83 | | * |
84 | | */ |
85 | | static int freerdp_connect_begin(freerdp* instance) |
86 | 0 | { |
87 | 0 | BOOL rc = 0; |
88 | 0 | rdpRdp* rdp = NULL; |
89 | 0 | BOOL status = TRUE; |
90 | 0 | rdpSettings* settings = NULL; |
91 | |
|
92 | 0 | if (!instance) |
93 | 0 | return -1; |
94 | | |
95 | 0 | WINPR_ASSERT(instance->context); |
96 | | |
97 | | /* We always set the return code to 0 before we start the connect sequence*/ |
98 | 0 | instance->ConnectionCallbackState = CLIENT_STATE_INITIAL; |
99 | 0 | freerdp_set_last_error_log(instance->context, FREERDP_ERROR_SUCCESS); |
100 | 0 | clearChannelError(instance->context); |
101 | 0 | if (!utils_reset_abort(instance->context->rdp)) |
102 | 0 | return -1; |
103 | | |
104 | 0 | rdp = instance->context->rdp; |
105 | 0 | WINPR_ASSERT(rdp); |
106 | | |
107 | 0 | settings = instance->context->settings; |
108 | 0 | WINPR_ASSERT(settings); |
109 | | |
110 | 0 | freerdp_channels_register_instance(instance->context->channels, instance); |
111 | |
|
112 | 0 | if (!freerdp_settings_set_default_order_support(settings)) |
113 | 0 | return -1; |
114 | | |
115 | 0 | freerdp_add_signal_cleanup_handler(instance->context, sig_abort_connect); |
116 | |
|
117 | 0 | IFCALLRET(instance->PreConnect, status, instance); |
118 | 0 | instance->ConnectionCallbackState = CLIENT_STATE_PRECONNECT_PASSED; |
119 | |
|
120 | 0 | freerdp_settings_print_warnings(settings); |
121 | 0 | if (status) |
122 | 0 | status = freerdp_settings_enforce_monitor_exists(settings); |
123 | |
|
124 | 0 | if (status) |
125 | 0 | status = freerdp_settings_enforce_consistency(settings); |
126 | |
|
127 | 0 | if (status) |
128 | 0 | status = freerdp_settings_check_client_after_preconnect(settings); |
129 | |
|
130 | 0 | if (status) |
131 | 0 | status = rdp_set_backup_settings(rdp); |
132 | 0 | if (status) |
133 | 0 | status = utils_reload_channels(instance->context); |
134 | |
|
135 | 0 | const UINT32 cp = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardCodePage); |
136 | 0 | int64_t KeyboardLayout = freerdp_get_keyboard_default_layout_for_locale(cp); |
137 | 0 | if (KeyboardLayout == 0) |
138 | 0 | KeyboardLayout = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardLayout); |
139 | |
|
140 | 0 | switch (KeyboardLayout) |
141 | 0 | { |
142 | 0 | case KBD_JAPANESE: |
143 | 0 | case KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002: |
144 | 0 | { |
145 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardType, |
146 | 0 | WINPR_KBD_TYPE_JAPANESE)) |
147 | 0 | return -1; |
148 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardSubType, 2)) |
149 | 0 | return -1; |
150 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardFunctionKey, 12)) |
151 | 0 | return -1; |
152 | 0 | } |
153 | 0 | break; |
154 | 0 | default: |
155 | 0 | break; |
156 | 0 | } |
157 | | |
158 | 0 | if (!status) |
159 | 0 | { |
160 | 0 | rdpContext* context = instance->context; |
161 | 0 | WINPR_ASSERT(context); |
162 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_PRE_CONNECT_FAILED); |
163 | |
|
164 | 0 | WLog_Print(context->log, WLOG_ERROR, "freerdp_pre_connect failed: %s", |
165 | 0 | rdp_client_connection_state_string(instance->ConnectionCallbackState)); |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | 0 | rc = rdp_client_connect(rdp); |
170 | | |
171 | | /* --authonly tests the connection without a UI */ |
172 | 0 | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_AuthenticationOnly)) |
173 | 0 | { |
174 | 0 | rdpContext* context = rdp->context; |
175 | 0 | WINPR_ASSERT(context); |
176 | 0 | WLog_Print(context->log, WLOG_ERROR, "Authentication only, exit status %" PRId32 "", rc); |
177 | 0 | return 0; |
178 | 0 | } |
179 | | |
180 | 0 | return rc ? 1 : 0; |
181 | 0 | } |
182 | | |
183 | | BOOL freerdp_connect(freerdp* instance) |
184 | 0 | { |
185 | 0 | BOOL status = FALSE; |
186 | 0 | ConnectionResultEventArgs e = { 0 }; |
187 | 0 | const int rc = freerdp_connect_begin(instance); |
188 | 0 | rdpRdp* rdp = NULL; |
189 | 0 | UINT status2 = ERROR_INTERNAL_ERROR; |
190 | |
|
191 | 0 | WINPR_ASSERT(instance); |
192 | 0 | WINPR_ASSERT(instance->context); |
193 | | |
194 | 0 | rdp = instance->context->rdp; |
195 | 0 | WINPR_ASSERT(rdp); |
196 | 0 | WINPR_ASSERT(rdp->settings); |
197 | | |
198 | 0 | if (rc > 0) |
199 | | /* Pointers might have changed in between */ |
200 | 0 | { |
201 | 0 | rdp_update_internal* up = update_cast(rdp->update); |
202 | |
|
203 | 0 | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_DumpRemoteFx)) |
204 | 0 | { |
205 | 0 | up->pcap_rfx = pcap_open( |
206 | 0 | freerdp_settings_get_string(rdp->settings, FreeRDP_DumpRemoteFxFile), TRUE); |
207 | |
|
208 | 0 | if (up->pcap_rfx) |
209 | 0 | up->dump_rfx = TRUE; |
210 | 0 | } |
211 | |
|
212 | 0 | pointer_cache_register_callbacks(instance->context->update); |
213 | 0 | status = IFCALLRESULT(TRUE, instance->PostConnect, instance); |
214 | 0 | instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED; |
215 | |
|
216 | 0 | if (status) |
217 | 0 | status2 = freerdp_channels_post_connect(instance->context->channels, instance); |
218 | 0 | } |
219 | 0 | else |
220 | 0 | { |
221 | 0 | status2 = CHANNEL_RC_OK; |
222 | 0 | if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_CONNECT_TRANSPORT_FAILED) |
223 | 0 | status = freerdp_reconnect(instance); |
224 | 0 | else |
225 | 0 | goto freerdp_connect_finally; |
226 | 0 | } |
227 | | |
228 | 0 | if (!status || (status2 != CHANNEL_RC_OK) || !update_post_connect(instance->context->update)) |
229 | 0 | { |
230 | 0 | rdpContext* context = instance->context; |
231 | 0 | WINPR_ASSERT(context); |
232 | 0 | WLog_Print(context->log, WLOG_ERROR, "freerdp_post_connect failed"); |
233 | |
|
234 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_POST_CONNECT_FAILED); |
235 | |
|
236 | 0 | status = FALSE; |
237 | 0 | goto freerdp_connect_finally; |
238 | 0 | } |
239 | | |
240 | 0 | if (rdp->settings->PlayRemoteFx) |
241 | 0 | { |
242 | 0 | wStream* s = NULL; |
243 | 0 | rdp_update_internal* update = update_cast(instance->context->update); |
244 | 0 | pcap_record record = { 0 }; |
245 | |
|
246 | 0 | WINPR_ASSERT(update); |
247 | 0 | update->pcap_rfx = pcap_open(rdp->settings->PlayRemoteFxFile, FALSE); |
248 | 0 | status = FALSE; |
249 | |
|
250 | 0 | if (!update->pcap_rfx) |
251 | 0 | goto freerdp_connect_finally; |
252 | 0 | else |
253 | 0 | update->play_rfx = TRUE; |
254 | | |
255 | 0 | status = TRUE; |
256 | |
|
257 | 0 | while (pcap_has_next_record(update->pcap_rfx) && status) |
258 | 0 | { |
259 | 0 | pcap_get_next_record_header(update->pcap_rfx, &record); |
260 | |
|
261 | 0 | s = transport_take_from_pool(rdp->transport, record.length); |
262 | 0 | if (!s) |
263 | 0 | break; |
264 | | |
265 | 0 | record.data = Stream_Buffer(s); |
266 | 0 | pcap_get_next_record_content(update->pcap_rfx, &record); |
267 | 0 | Stream_SetLength(s, record.length); |
268 | 0 | Stream_SetPosition(s, 0); |
269 | |
|
270 | 0 | if (!update_begin_paint(&update->common)) |
271 | 0 | status = FALSE; |
272 | 0 | else |
273 | 0 | { |
274 | 0 | if (update_recv_surfcmds(&update->common, s) < 0) |
275 | 0 | status = FALSE; |
276 | |
|
277 | 0 | if (!update_end_paint(&update->common)) |
278 | 0 | status = FALSE; |
279 | 0 | } |
280 | |
|
281 | 0 | Stream_Release(s); |
282 | 0 | } |
283 | |
|
284 | 0 | pcap_close(update->pcap_rfx); |
285 | 0 | update->pcap_rfx = NULL; |
286 | 0 | goto freerdp_connect_finally; |
287 | 0 | } |
288 | | |
289 | 0 | if (rdp->errorInfo == ERRINFO_SERVER_INSUFFICIENT_PRIVILEGES) |
290 | 0 | freerdp_set_last_error_log(instance->context, FREERDP_ERROR_INSUFFICIENT_PRIVILEGES); |
291 | |
|
292 | 0 | transport_set_connected_event(rdp->transport); |
293 | |
|
294 | 0 | freerdp_connect_finally: |
295 | 0 | EventArgsInit(&e, "freerdp"); |
296 | 0 | e.result = status ? 0 : -1; |
297 | 0 | PubSub_OnConnectionResult(rdp->pubSub, instance->context, &e); |
298 | |
|
299 | 0 | if (!status) |
300 | 0 | freerdp_disconnect(instance); |
301 | |
|
302 | 0 | return status; |
303 | 0 | } |
304 | | |
305 | | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
306 | | BOOL freerdp_abort_connect(freerdp* instance) |
307 | 0 | { |
308 | 0 | if (!instance) |
309 | 0 | return FALSE; |
310 | | |
311 | 0 | return freerdp_abort_connect_context(instance->context); |
312 | 0 | } |
313 | | #endif |
314 | | |
315 | | BOOL freerdp_abort_connect_context(rdpContext* context) |
316 | 0 | { |
317 | 0 | if (!context) |
318 | 0 | return FALSE; |
319 | | |
320 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED); |
321 | | |
322 | | /* Try to send a [MS-RDPBCGR] 1.3.1.4.1 User-Initiated on Client PDU, we don't care about |
323 | | * success */ |
324 | 0 | if (context->rdp && context->rdp->mcs) |
325 | 0 | { |
326 | 0 | if (!context->ServerMode) |
327 | 0 | { |
328 | 0 | (void)mcs_send_disconnect_provider_ultimatum(context->rdp->mcs, |
329 | 0 | Disconnect_Ultimatum_user_requested); |
330 | 0 | } |
331 | 0 | } |
332 | 0 | return utils_abort_connect(context->rdp); |
333 | 0 | } |
334 | | |
335 | | #if defined(WITH_FREERDP_DEPRECATED) |
336 | | BOOL freerdp_get_fds(freerdp* instance, void** rfds, int* rcount, WINPR_ATTR_UNUSED void** wfds, |
337 | | WINPR_ATTR_UNUSED int* wcount) |
338 | | { |
339 | | rdpRdp* rdp = NULL; |
340 | | |
341 | | WINPR_ASSERT(instance); |
342 | | WINPR_ASSERT(instance->context); |
343 | | |
344 | | rdp = instance->context->rdp; |
345 | | WINPR_ASSERT(rdp); |
346 | | |
347 | | transport_get_fds(rdp->transport, rfds, rcount); |
348 | | return TRUE; |
349 | | } |
350 | | #endif |
351 | | |
352 | | BOOL freerdp_check_fds(freerdp* instance) |
353 | 0 | { |
354 | 0 | int status = 0; |
355 | 0 | rdpRdp* rdp = NULL; |
356 | |
|
357 | 0 | if (!instance) |
358 | 0 | return FALSE; |
359 | | |
360 | 0 | if (!instance->context) |
361 | 0 | return FALSE; |
362 | | |
363 | 0 | if (!instance->context->rdp) |
364 | 0 | return FALSE; |
365 | | |
366 | 0 | rdp = instance->context->rdp; |
367 | 0 | status = rdp_check_fds(rdp); |
368 | |
|
369 | 0 | if (status < 0) |
370 | 0 | { |
371 | 0 | TerminateEventArgs e; |
372 | 0 | rdpContext* context = instance->context; |
373 | 0 | WINPR_ASSERT(context); |
374 | | |
375 | 0 | WLog_Print(context->log, WLOG_DEBUG, "rdp_check_fds() - %i", status); |
376 | 0 | EventArgsInit(&e, "freerdp"); |
377 | 0 | e.code = 0; |
378 | 0 | PubSub_OnTerminate(rdp->pubSub, context, &e); |
379 | 0 | return FALSE; |
380 | 0 | } |
381 | | |
382 | 0 | return TRUE; |
383 | 0 | } |
384 | | |
385 | | DWORD freerdp_get_event_handles(rdpContext* context, HANDLE* events, DWORD count) |
386 | 0 | { |
387 | 0 | DWORD nCount = 0; |
388 | |
|
389 | 0 | WINPR_ASSERT(context); |
390 | 0 | WINPR_ASSERT(context->rdp); |
391 | 0 | WINPR_ASSERT(events || (count == 0)); |
392 | | |
393 | 0 | const size_t rrc = rdp_get_event_handles(context->rdp, &events[nCount], count - nCount); |
394 | 0 | if (rrc == 0) |
395 | 0 | return 0; |
396 | | |
397 | 0 | nCount += WINPR_ASSERTING_INT_CAST(uint32_t, rrc); |
398 | | |
399 | 0 | if (events && (nCount < count + 2)) |
400 | 0 | { |
401 | 0 | events[nCount++] = freerdp_channels_get_event_handle(context->instance); |
402 | 0 | events[nCount++] = getChannelErrorEventHandle(context); |
403 | 0 | } |
404 | 0 | else |
405 | 0 | return 0; |
406 | | |
407 | 0 | const SSIZE_T rc = freerdp_client_channel_get_registered_event_handles( |
408 | 0 | context->channels, &events[nCount], count - nCount); |
409 | 0 | if (rc < 0) |
410 | 0 | return 0; |
411 | 0 | return nCount + (DWORD)rc; |
412 | 0 | } |
413 | | |
414 | | /* Resend mouse cursor position to prevent session lock in prevent-session-lock mode */ |
415 | | static BOOL freerdp_prevent_session_lock(rdpContext* context) |
416 | 0 | { |
417 | 0 | WINPR_ASSERT(context); |
418 | 0 | WINPR_ASSERT(context->input); |
419 | | |
420 | 0 | rdp_input_internal* in = input_cast(context->input); |
421 | |
|
422 | 0 | UINT32 FakeMouseMotionInterval = |
423 | 0 | freerdp_settings_get_uint32(context->settings, FreeRDP_FakeMouseMotionInterval); |
424 | 0 | if (FakeMouseMotionInterval && in->lastInputTimestamp) |
425 | 0 | { |
426 | 0 | const time_t now = time(NULL); |
427 | 0 | if (WINPR_ASSERTING_INT_CAST(size_t, now) - in->lastInputTimestamp > |
428 | 0 | FakeMouseMotionInterval) |
429 | 0 | { |
430 | 0 | WLog_Print(context->log, WLOG_DEBUG, |
431 | 0 | "fake mouse move: x=%d y=%d lastInputTimestamp=%" PRIu64 " " |
432 | 0 | "FakeMouseMotionInterval=%" PRIu32, |
433 | 0 | in->lastX, in->lastY, in->lastInputTimestamp, FakeMouseMotionInterval); |
434 | |
|
435 | 0 | BOOL status = freerdp_input_send_mouse_event(context->input, PTR_FLAGS_MOVE, in->lastX, |
436 | 0 | in->lastY); |
437 | 0 | if (!status) |
438 | 0 | { |
439 | 0 | if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS) |
440 | 0 | WLog_Print(context->log, WLOG_ERROR, |
441 | 0 | "freerdp_prevent_session_lock() failed - %" PRIi32 "", status); |
442 | |
|
443 | 0 | return FALSE; |
444 | 0 | } |
445 | | |
446 | 0 | return status; |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | 0 | return TRUE; |
451 | 0 | } |
452 | | |
453 | | BOOL freerdp_check_event_handles(rdpContext* context) |
454 | 0 | { |
455 | 0 | WINPR_ASSERT(context); |
456 | | |
457 | 0 | BOOL status = freerdp_check_fds(context->instance); |
458 | |
|
459 | 0 | if (!status) |
460 | 0 | { |
461 | 0 | if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS) |
462 | 0 | WLog_Print(context->log, WLOG_ERROR, "freerdp_check_fds() failed - %" PRIi32 "", |
463 | 0 | status); |
464 | |
|
465 | 0 | return FALSE; |
466 | 0 | } |
467 | | |
468 | 0 | status = freerdp_channels_check_fds(context->channels, context->instance); |
469 | |
|
470 | 0 | if (!status) |
471 | 0 | { |
472 | 0 | if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS) |
473 | 0 | WLog_Print(context->log, WLOG_ERROR, |
474 | 0 | "freerdp_channels_check_fds() failed - %" PRIi32 "", status); |
475 | |
|
476 | 0 | return FALSE; |
477 | 0 | } |
478 | | |
479 | 0 | status = checkChannelErrorEvent(context); |
480 | |
|
481 | 0 | if (!status) |
482 | 0 | { |
483 | 0 | if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS) |
484 | 0 | WLog_Print(context->log, WLOG_ERROR, "checkChannelErrorEvent() failed - %" PRIi32 "", |
485 | 0 | status); |
486 | |
|
487 | 0 | return FALSE; |
488 | 0 | } |
489 | | |
490 | 0 | status = freerdp_prevent_session_lock(context); |
491 | |
|
492 | 0 | return status; |
493 | 0 | } |
494 | | |
495 | | wMessageQueue* freerdp_get_message_queue(freerdp* instance, DWORD id) |
496 | 0 | { |
497 | 0 | wMessageQueue* queue = NULL; |
498 | |
|
499 | 0 | WINPR_ASSERT(instance); |
500 | | |
501 | 0 | rdpContext* context = instance->context; |
502 | 0 | WINPR_ASSERT(context); |
503 | | |
504 | 0 | switch (id) |
505 | 0 | { |
506 | 0 | case FREERDP_UPDATE_MESSAGE_QUEUE: |
507 | 0 | { |
508 | 0 | rdp_update_internal* update = update_cast(context->update); |
509 | 0 | queue = update->queue; |
510 | 0 | } |
511 | 0 | break; |
512 | | |
513 | 0 | case FREERDP_INPUT_MESSAGE_QUEUE: |
514 | 0 | { |
515 | 0 | rdp_input_internal* input = input_cast(context->input); |
516 | 0 | queue = input->queue; |
517 | 0 | } |
518 | 0 | break; |
519 | 0 | default: |
520 | 0 | break; |
521 | 0 | } |
522 | | |
523 | 0 | return queue; |
524 | 0 | } |
525 | | |
526 | | HANDLE freerdp_get_message_queue_event_handle(freerdp* instance, DWORD id) |
527 | 0 | { |
528 | 0 | HANDLE event = NULL; |
529 | 0 | wMessageQueue* queue = freerdp_get_message_queue(instance, id); |
530 | |
|
531 | 0 | if (queue) |
532 | 0 | event = MessageQueue_Event(queue); |
533 | |
|
534 | 0 | return event; |
535 | 0 | } |
536 | | |
537 | | int freerdp_message_queue_process_message(freerdp* instance, DWORD id, wMessage* message) |
538 | 0 | { |
539 | 0 | int status = -1; |
540 | 0 | rdpContext* context = NULL; |
541 | |
|
542 | 0 | WINPR_ASSERT(instance); |
543 | | |
544 | 0 | context = instance->context; |
545 | 0 | WINPR_ASSERT(context); |
546 | | |
547 | 0 | switch (id) |
548 | 0 | { |
549 | 0 | case FREERDP_UPDATE_MESSAGE_QUEUE: |
550 | 0 | status = update_message_queue_process_message(context->update, message); |
551 | 0 | break; |
552 | | |
553 | 0 | case FREERDP_INPUT_MESSAGE_QUEUE: |
554 | 0 | status = input_message_queue_process_message(context->input, message); |
555 | 0 | break; |
556 | 0 | default: |
557 | 0 | break; |
558 | 0 | } |
559 | | |
560 | 0 | return status; |
561 | 0 | } |
562 | | |
563 | | int freerdp_message_queue_process_pending_messages(freerdp* instance, DWORD id) |
564 | 0 | { |
565 | 0 | int status = -1; |
566 | 0 | rdpContext* context = NULL; |
567 | |
|
568 | 0 | WINPR_ASSERT(instance); |
569 | | |
570 | 0 | context = instance->context; |
571 | 0 | WINPR_ASSERT(context); |
572 | | |
573 | 0 | switch (id) |
574 | 0 | { |
575 | 0 | case FREERDP_UPDATE_MESSAGE_QUEUE: |
576 | 0 | status = update_message_queue_process_pending_messages(context->update); |
577 | 0 | break; |
578 | | |
579 | 0 | case FREERDP_INPUT_MESSAGE_QUEUE: |
580 | 0 | status = input_message_queue_process_pending_messages(context->input); |
581 | 0 | break; |
582 | 0 | default: |
583 | 0 | break; |
584 | 0 | } |
585 | | |
586 | 0 | return status; |
587 | 0 | } |
588 | | |
589 | | static BOOL freerdp_send_channel_data(freerdp* instance, UINT16 channelId, const BYTE* data, |
590 | | size_t size) |
591 | 0 | { |
592 | 0 | WINPR_ASSERT(instance); |
593 | 0 | WINPR_ASSERT(instance->context); |
594 | 0 | WINPR_ASSERT(instance->context->rdp); |
595 | 0 | return rdp_send_channel_data(instance->context->rdp, channelId, data, size); |
596 | 0 | } |
597 | | |
598 | | static BOOL freerdp_send_channel_packet(freerdp* instance, UINT16 channelId, size_t totalSize, |
599 | | UINT32 flags, const BYTE* data, size_t chunkSize) |
600 | 0 | { |
601 | 0 | WINPR_ASSERT(instance); |
602 | 0 | WINPR_ASSERT(instance->context); |
603 | 0 | WINPR_ASSERT(instance->context->rdp); |
604 | 0 | return rdp_channel_send_packet(instance->context->rdp, channelId, totalSize, flags, data, |
605 | 0 | chunkSize); |
606 | 0 | } |
607 | | |
608 | | BOOL freerdp_disconnect(freerdp* instance) |
609 | 0 | { |
610 | 0 | BOOL rc = TRUE; |
611 | 0 | rdpRdp* rdp = NULL; |
612 | 0 | rdp_update_internal* up = NULL; |
613 | |
|
614 | 0 | if (!instance || !instance->context) |
615 | 0 | return FALSE; |
616 | | |
617 | 0 | rdp = instance->context->rdp; |
618 | 0 | utils_abort_connect(rdp); |
619 | |
|
620 | 0 | if (!rdp_client_disconnect(rdp)) |
621 | 0 | rc = FALSE; |
622 | |
|
623 | 0 | up = update_cast(rdp->update); |
624 | |
|
625 | 0 | update_post_disconnect(rdp->update); |
626 | |
|
627 | 0 | IFCALL(instance->PostDisconnect, instance); |
628 | |
|
629 | 0 | if (up->pcap_rfx) |
630 | 0 | { |
631 | 0 | up->dump_rfx = FALSE; |
632 | 0 | pcap_close(up->pcap_rfx); |
633 | 0 | up->pcap_rfx = NULL; |
634 | 0 | } |
635 | |
|
636 | 0 | freerdp_channels_close(instance->context->channels, instance); |
637 | |
|
638 | 0 | IFCALL(instance->PostFinalDisconnect, instance); |
639 | |
|
640 | 0 | freerdp_del_signal_cleanup_handler(instance->context, sig_abort_connect); |
641 | 0 | return rc; |
642 | 0 | } |
643 | | |
644 | | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
645 | | BOOL freerdp_disconnect_before_reconnect(freerdp* instance) |
646 | 0 | { |
647 | 0 | WINPR_ASSERT(instance); |
648 | 0 | return freerdp_disconnect_before_reconnect_context(instance->context); |
649 | 0 | } |
650 | | #endif |
651 | | |
652 | | BOOL freerdp_disconnect_before_reconnect_context(rdpContext* context) |
653 | 0 | { |
654 | 0 | rdpRdp* rdp = NULL; |
655 | |
|
656 | 0 | WINPR_ASSERT(context); |
657 | | |
658 | 0 | rdp = context->rdp; |
659 | 0 | return rdp_client_disconnect_and_clear(rdp); |
660 | 0 | } |
661 | | |
662 | | BOOL freerdp_reconnect(freerdp* instance) |
663 | 0 | { |
664 | 0 | rdpRdp* rdp = NULL; |
665 | |
|
666 | 0 | WINPR_ASSERT(instance); |
667 | 0 | WINPR_ASSERT(instance->context); |
668 | | |
669 | 0 | if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_CONNECT_CANCELLED) |
670 | 0 | return FALSE; |
671 | | |
672 | 0 | rdp = instance->context->rdp; |
673 | |
|
674 | 0 | if (!utils_reset_abort(instance->context->rdp)) |
675 | 0 | return FALSE; |
676 | 0 | return rdp_client_reconnect(rdp); |
677 | 0 | } |
678 | | |
679 | | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
680 | | BOOL freerdp_shall_disconnect(freerdp* instance) |
681 | 0 | { |
682 | 0 | if (!instance) |
683 | 0 | return FALSE; |
684 | | |
685 | 0 | return freerdp_shall_disconnect_context(instance->context); |
686 | 0 | } |
687 | | #endif |
688 | | |
689 | | BOOL freerdp_shall_disconnect_context(const rdpContext* context) |
690 | 0 | { |
691 | 0 | if (!context) |
692 | 0 | return FALSE; |
693 | | |
694 | 0 | return utils_abort_event_is_set(context->rdp); |
695 | 0 | } |
696 | | |
697 | | BOOL freerdp_focus_required(freerdp* instance) |
698 | 0 | { |
699 | 0 | rdpRdp* rdp = NULL; |
700 | 0 | BOOL bRetCode = FALSE; |
701 | |
|
702 | 0 | WINPR_ASSERT(instance); |
703 | 0 | WINPR_ASSERT(instance->context); |
704 | | |
705 | 0 | rdp = instance->context->rdp; |
706 | 0 | WINPR_ASSERT(rdp); |
707 | | |
708 | 0 | if (rdp->resendFocus) |
709 | 0 | { |
710 | 0 | bRetCode = TRUE; |
711 | 0 | rdp->resendFocus = FALSE; |
712 | 0 | } |
713 | |
|
714 | 0 | return bRetCode; |
715 | 0 | } |
716 | | |
717 | | void freerdp_set_focus(freerdp* instance) |
718 | 0 | { |
719 | 0 | rdpRdp* rdp = NULL; |
720 | |
|
721 | 0 | WINPR_ASSERT(instance); |
722 | 0 | WINPR_ASSERT(instance->context); |
723 | | |
724 | 0 | rdp = instance->context->rdp; |
725 | 0 | WINPR_ASSERT(rdp); |
726 | | |
727 | 0 | rdp->resendFocus = TRUE; |
728 | 0 | } |
729 | | |
730 | | void freerdp_get_version(int* major, int* minor, int* revision) |
731 | 0 | { |
732 | 0 | if (major != NULL) |
733 | 0 | *major = FREERDP_VERSION_MAJOR; |
734 | |
|
735 | 0 | if (minor != NULL) |
736 | 0 | *minor = FREERDP_VERSION_MINOR; |
737 | |
|
738 | 0 | if (revision != NULL) |
739 | 0 | *revision = FREERDP_VERSION_REVISION; |
740 | 0 | } |
741 | | |
742 | | const char* freerdp_get_version_string(void) |
743 | 0 | { |
744 | 0 | return FREERDP_VERSION_FULL; |
745 | 0 | } |
746 | | |
747 | | const char* freerdp_get_build_config(void) |
748 | 0 | { |
749 | 0 | WINPR_PRAGMA_DIAG_PUSH |
750 | 0 | WINPR_PRAGMA_DIAG_IGNORED_OVERLENGTH_STRINGS |
751 | 0 | static const char build_config[] = |
752 | 0 | "Build configuration: " FREERDP_BUILD_CONFIG "\n" |
753 | 0 | "Build type: " FREERDP_BUILD_TYPE "\n" |
754 | 0 | "CFLAGS: " FREERDP_CFLAGS "\n" |
755 | 0 | "Compiler: " FREERDP_COMPILER_ID ", " FREERDP_COMPILER_VERSION "\n" |
756 | 0 | "Target architecture: " FREERDP_TARGET_ARCH "\n"; |
757 | 0 | WINPR_PRAGMA_DIAG_POP |
758 | 0 | return build_config; |
759 | 0 | } |
760 | | |
761 | | const char* freerdp_get_build_revision(void) |
762 | 0 | { |
763 | 0 | return FREERDP_GIT_REVISION; |
764 | 0 | } |
765 | | |
766 | | static wEventType FreeRDP_Events[] = { |
767 | | DEFINE_EVENT_ENTRY(WindowStateChange), DEFINE_EVENT_ENTRY(ResizeWindow), |
768 | | DEFINE_EVENT_ENTRY(LocalResizeWindow), DEFINE_EVENT_ENTRY(EmbedWindow), |
769 | | DEFINE_EVENT_ENTRY(PanningChange), DEFINE_EVENT_ENTRY(ZoomingChange), |
770 | | DEFINE_EVENT_ENTRY(ErrorInfo), DEFINE_EVENT_ENTRY(Terminate), |
771 | | DEFINE_EVENT_ENTRY(ConnectionResult), DEFINE_EVENT_ENTRY(ChannelConnected), |
772 | | DEFINE_EVENT_ENTRY(ChannelDisconnected), DEFINE_EVENT_ENTRY(MouseEvent), |
773 | | DEFINE_EVENT_ENTRY(Activated), DEFINE_EVENT_ENTRY(Timer), |
774 | | DEFINE_EVENT_ENTRY(GraphicsReset) |
775 | | }; |
776 | | |
777 | | /** Allocator function for a rdp context. |
778 | | * The function will allocate a rdpRdp structure using rdp_new(), then copy |
779 | | * its contents to the appropriate fields in the rdp_freerdp structure given in parameters. |
780 | | * It will also initialize the 'context' field in the rdp_freerdp structure as needed. |
781 | | * If the caller has set the ContextNew callback in the 'instance' parameter, it will be called at |
782 | | * the end of the function. |
783 | | * |
784 | | * @param instance - Pointer to the rdp_freerdp structure that will be initialized with the new |
785 | | * context. |
786 | | */ |
787 | | BOOL freerdp_context_new(freerdp* instance) |
788 | 0 | { |
789 | 0 | return freerdp_context_new_ex(instance, NULL); |
790 | 0 | } |
791 | | |
792 | | static BOOL freerdp_common_context(rdpContext* context, AccessTokenType tokenType, char** token, |
793 | | size_t count, ...) |
794 | 0 | { |
795 | 0 | BOOL rc = FALSE; |
796 | |
|
797 | 0 | WINPR_ASSERT(context); |
798 | 0 | if (!context->instance || !context->instance->GetAccessToken) |
799 | 0 | return TRUE; |
800 | | |
801 | 0 | va_list ap; |
802 | 0 | va_start(ap, count); |
803 | 0 | switch (tokenType) |
804 | 0 | { |
805 | 0 | case ACCESS_TOKEN_TYPE_AAD: |
806 | 0 | if (count != 2) |
807 | 0 | { |
808 | 0 | WLog_ERR(TAG, |
809 | 0 | "ACCESS_TOKEN_TYPE_AAD expected 2 additional arguments, but got %" PRIuz |
810 | 0 | ", aborting", |
811 | 0 | count); |
812 | 0 | } |
813 | 0 | else |
814 | 0 | { |
815 | 0 | const char* scope = va_arg(ap, const char*); |
816 | 0 | const char* req_cnf = va_arg(ap, const char*); |
817 | 0 | rc = context->instance->GetAccessToken(context->instance, tokenType, token, count, |
818 | 0 | scope, req_cnf); |
819 | 0 | } |
820 | 0 | break; |
821 | 0 | case ACCESS_TOKEN_TYPE_AVD: |
822 | 0 | if (count != 0) |
823 | 0 | { |
824 | 0 | WLog_WARN(TAG, |
825 | 0 | "ACCESS_TOKEN_TYPE_AVD expected 0 additional arguments, but got %" PRIuz |
826 | 0 | ", ignoring", |
827 | 0 | count); |
828 | 0 | } |
829 | 0 | else |
830 | 0 | { |
831 | 0 | rc = context->instance->GetAccessToken(context->instance, tokenType, token, count); |
832 | 0 | } |
833 | 0 | break; |
834 | 0 | default: |
835 | 0 | break; |
836 | 0 | } |
837 | 0 | va_end(ap); |
838 | |
|
839 | 0 | return rc; |
840 | 0 | } |
841 | | |
842 | | BOOL freerdp_context_new_ex(freerdp* instance, rdpSettings* settings) |
843 | 0 | { |
844 | 0 | rdpRdp* rdp = NULL; |
845 | 0 | rdpContext* context = NULL; |
846 | 0 | BOOL ret = TRUE; |
847 | |
|
848 | 0 | WINPR_ASSERT(instance); |
849 | | |
850 | 0 | instance->context = context = (rdpContext*)calloc(1, instance->ContextSize); |
851 | |
|
852 | 0 | if (!context) |
853 | 0 | return FALSE; |
854 | | |
855 | 0 | context->log = WLog_Get(TAG); |
856 | 0 | if (!context->log) |
857 | 0 | goto fail; |
858 | | |
859 | | /* Set to external settings, prevents rdp_new from creating its own instance */ |
860 | 0 | context->settings = settings; |
861 | 0 | context->instance = instance; |
862 | 0 | context->ServerMode = FALSE; |
863 | 0 | context->disconnectUltimatum = 0; |
864 | |
|
865 | 0 | context->metrics = metrics_new(context); |
866 | |
|
867 | 0 | if (!context->metrics) |
868 | 0 | goto fail; |
869 | | |
870 | 0 | rdp = rdp_new(context); |
871 | |
|
872 | 0 | if (!rdp) |
873 | 0 | goto fail; |
874 | | |
875 | 0 | context->rdp = rdp; |
876 | 0 | context->pubSub = rdp->pubSub; |
877 | |
|
878 | 0 | if (!context->pubSub) |
879 | 0 | goto fail; |
880 | | |
881 | 0 | PubSub_AddEventTypes(rdp->pubSub, FreeRDP_Events, ARRAYSIZE(FreeRDP_Events)); |
882 | |
|
883 | | #if defined(WITH_FREERDP_DEPRECATED) |
884 | | instance->input = rdp->input; |
885 | | instance->update = rdp->update; |
886 | | instance->settings = rdp->settings; |
887 | | instance->autodetect = rdp->autodetect; |
888 | | #endif |
889 | |
|
890 | 0 | instance->heartbeat = rdp->heartbeat; |
891 | 0 | context->graphics = graphics_new(context); |
892 | |
|
893 | 0 | if (!context->graphics) |
894 | 0 | goto fail; |
895 | | |
896 | 0 | context->input = rdp->input; |
897 | 0 | context->update = rdp->update; |
898 | 0 | context->settings = rdp->settings; |
899 | 0 | context->autodetect = rdp->autodetect; |
900 | |
|
901 | 0 | if (!(context->errorDescription = calloc(1, 500))) |
902 | 0 | { |
903 | 0 | WLog_Print(context->log, WLOG_ERROR, "calloc failed!"); |
904 | 0 | goto fail; |
905 | 0 | } |
906 | | |
907 | 0 | if (!(context->channelErrorEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) |
908 | 0 | { |
909 | 0 | WLog_Print(context->log, WLOG_ERROR, "CreateEvent failed!"); |
910 | 0 | goto fail; |
911 | 0 | } |
912 | | |
913 | 0 | update_register_client_callbacks(rdp->update); |
914 | |
|
915 | 0 | if (!(context->channels = freerdp_channels_new(instance))) |
916 | 0 | goto fail; |
917 | | |
918 | 0 | context->dump = stream_dump_new(); |
919 | 0 | if (!context->dump) |
920 | 0 | goto fail; |
921 | | |
922 | | /* Fallback: |
923 | | * Client common library might set a function pointer to handle this, but here we provide a |
924 | | * default implementation that simply calls instance->GetAccessToken. |
925 | | */ |
926 | 0 | if (!freerdp_set_common_access_token(context, freerdp_common_context)) |
927 | 0 | goto fail; |
928 | | |
929 | 0 | IFCALLRET(instance->ContextNew, ret, instance, context); |
930 | |
|
931 | 0 | if (!ret) |
932 | 0 | goto fail; |
933 | | |
934 | 0 | return TRUE; |
935 | | |
936 | 0 | fail: |
937 | 0 | freerdp_context_free(instance); |
938 | 0 | return FALSE; |
939 | 0 | } |
940 | | |
941 | | BOOL freerdp_context_reset(freerdp* instance) |
942 | 0 | { |
943 | 0 | if (!instance) |
944 | 0 | return FALSE; |
945 | | |
946 | 0 | WINPR_ASSERT(instance->context); |
947 | 0 | rdpRdp* rdp = instance->context->rdp; |
948 | |
|
949 | 0 | return rdp_reset_runtime_settings(rdp); |
950 | 0 | } |
951 | | |
952 | | /** Deallocator function for a rdp context. |
953 | | * The function will deallocate the resources from the 'instance' parameter that were allocated |
954 | | * from a call to freerdp_context_new(). If the ContextFree callback is set in the 'instance' |
955 | | * parameter, it will be called before deallocation occurs. |
956 | | * |
957 | | * @param instance - Pointer to the rdp_freerdp structure that was initialized by a call to |
958 | | * freerdp_context_new(). On return, the fields associated to the context are invalid. |
959 | | */ |
960 | | void freerdp_context_free(freerdp* instance) |
961 | 0 | { |
962 | 0 | rdpContext* ctx = NULL; |
963 | |
|
964 | 0 | if (!instance) |
965 | 0 | return; |
966 | | |
967 | 0 | if (!instance->context) |
968 | 0 | return; |
969 | | |
970 | 0 | ctx = instance->context; |
971 | |
|
972 | 0 | IFCALL(instance->ContextFree, instance, ctx); |
973 | 0 | rdp_free(ctx->rdp); |
974 | 0 | ctx->rdp = NULL; |
975 | 0 | ctx->settings = NULL; /* owned by rdpRdp */ |
976 | |
|
977 | 0 | graphics_free(ctx->graphics); |
978 | 0 | ctx->graphics = NULL; |
979 | |
|
980 | 0 | metrics_free(ctx->metrics); |
981 | 0 | ctx->metrics = NULL; |
982 | |
|
983 | 0 | if (ctx->channelErrorEvent) |
984 | 0 | (void)CloseHandle(ctx->channelErrorEvent); |
985 | 0 | ctx->channelErrorEvent = NULL; |
986 | |
|
987 | 0 | free(ctx->errorDescription); |
988 | 0 | ctx->errorDescription = NULL; |
989 | |
|
990 | 0 | freerdp_channels_free(ctx->channels); |
991 | 0 | ctx->channels = NULL; |
992 | |
|
993 | 0 | freerdp_client_codecs_free(ctx->codecs); |
994 | 0 | ctx->codecs = NULL; |
995 | |
|
996 | 0 | stream_dump_free(ctx->dump); |
997 | 0 | ctx->dump = NULL; |
998 | |
|
999 | 0 | ctx->input = NULL; /* owned by rdpRdp */ |
1000 | 0 | ctx->update = NULL; /* owned by rdpRdp */ |
1001 | 0 | ctx->settings = NULL; /* owned by rdpRdp */ |
1002 | 0 | ctx->autodetect = NULL; /* owned by rdpRdp */ |
1003 | |
|
1004 | 0 | free(ctx); |
1005 | 0 | instance->context = NULL; |
1006 | | #if defined(WITH_FREERDP_DEPRECATED) |
1007 | | instance->input = NULL; /* owned by rdpRdp */ |
1008 | | instance->update = NULL; /* owned by rdpRdp */ |
1009 | | instance->settings = NULL; /* owned by rdpRdp */ |
1010 | | instance->autodetect = NULL; /* owned by rdpRdp */ |
1011 | | #endif |
1012 | 0 | instance->heartbeat = NULL; /* owned by rdpRdp */ |
1013 | 0 | } |
1014 | | |
1015 | | int freerdp_get_disconnect_ultimatum(rdpContext* context) |
1016 | 0 | { |
1017 | 0 | WINPR_ASSERT(context); |
1018 | 0 | return context->disconnectUltimatum; |
1019 | 0 | } |
1020 | | |
1021 | | UINT32 freerdp_error_info(freerdp* instance) |
1022 | 0 | { |
1023 | 0 | WINPR_ASSERT(instance); |
1024 | 0 | WINPR_ASSERT(instance->context); |
1025 | 0 | WINPR_ASSERT(instance->context->rdp); |
1026 | 0 | return instance->context->rdp->errorInfo; |
1027 | 0 | } |
1028 | | |
1029 | | void freerdp_set_error_info(rdpRdp* rdp, UINT32 error) |
1030 | 0 | { |
1031 | 0 | if (!rdp) |
1032 | 0 | return; |
1033 | | |
1034 | 0 | rdp_set_error_info(rdp, error); |
1035 | 0 | } |
1036 | | |
1037 | | BOOL freerdp_send_error_info(rdpRdp* rdp) |
1038 | 0 | { |
1039 | 0 | if (!rdp) |
1040 | 0 | return FALSE; |
1041 | | |
1042 | 0 | return rdp_send_error_info(rdp); |
1043 | 0 | } |
1044 | | |
1045 | | UINT32 freerdp_get_last_error(rdpContext* context) |
1046 | 0 | { |
1047 | 0 | WINPR_ASSERT(context); |
1048 | 0 | return context->LastError; |
1049 | 0 | } |
1050 | | |
1051 | | const char* freerdp_get_last_error_name(UINT32 code) |
1052 | 0 | { |
1053 | 0 | const char* name = NULL; |
1054 | 0 | const UINT32 cls = GET_FREERDP_ERROR_CLASS(code); |
1055 | 0 | const UINT32 type = GET_FREERDP_ERROR_TYPE(code); |
1056 | |
|
1057 | 0 | switch (cls) |
1058 | 0 | { |
1059 | 0 | case FREERDP_ERROR_ERRBASE_CLASS: |
1060 | 0 | name = freerdp_get_error_base_name(type); |
1061 | 0 | break; |
1062 | | |
1063 | 0 | case FREERDP_ERROR_ERRINFO_CLASS: |
1064 | 0 | name = freerdp_get_error_info_name(type); |
1065 | 0 | break; |
1066 | | |
1067 | 0 | case FREERDP_ERROR_CONNECT_CLASS: |
1068 | 0 | name = freerdp_get_error_connect_name(type); |
1069 | 0 | break; |
1070 | | |
1071 | 0 | default: |
1072 | 0 | name = rpc_error_to_string(code); |
1073 | 0 | break; |
1074 | 0 | } |
1075 | | |
1076 | 0 | return name; |
1077 | 0 | } |
1078 | | |
1079 | | const char* freerdp_get_last_error_string(UINT32 code) |
1080 | 0 | { |
1081 | 0 | const char* string = NULL; |
1082 | 0 | const UINT32 cls = GET_FREERDP_ERROR_CLASS(code); |
1083 | 0 | const UINT32 type = GET_FREERDP_ERROR_TYPE(code); |
1084 | |
|
1085 | 0 | switch (cls) |
1086 | 0 | { |
1087 | 0 | case FREERDP_ERROR_ERRBASE_CLASS: |
1088 | 0 | string = freerdp_get_error_base_string(type); |
1089 | 0 | break; |
1090 | | |
1091 | 0 | case FREERDP_ERROR_ERRINFO_CLASS: |
1092 | 0 | string = freerdp_get_error_info_string(type); |
1093 | 0 | break; |
1094 | | |
1095 | 0 | case FREERDP_ERROR_CONNECT_CLASS: |
1096 | 0 | string = freerdp_get_error_connect_string(type); |
1097 | 0 | break; |
1098 | | |
1099 | 0 | default: |
1100 | 0 | string = rpc_error_to_string(code); |
1101 | 0 | break; |
1102 | 0 | } |
1103 | | |
1104 | 0 | return string; |
1105 | 0 | } |
1106 | | |
1107 | | const char* freerdp_get_last_error_category(UINT32 code) |
1108 | 0 | { |
1109 | 0 | const char* string = NULL; |
1110 | 0 | const UINT32 cls = GET_FREERDP_ERROR_CLASS(code); |
1111 | 0 | const UINT32 type = GET_FREERDP_ERROR_TYPE(code); |
1112 | |
|
1113 | 0 | switch (cls) |
1114 | 0 | { |
1115 | 0 | case FREERDP_ERROR_ERRBASE_CLASS: |
1116 | 0 | string = freerdp_get_error_base_category(type); |
1117 | 0 | break; |
1118 | | |
1119 | 0 | case FREERDP_ERROR_ERRINFO_CLASS: |
1120 | 0 | string = freerdp_get_error_info_category(type); |
1121 | 0 | break; |
1122 | | |
1123 | 0 | case FREERDP_ERROR_CONNECT_CLASS: |
1124 | 0 | string = freerdp_get_error_connect_category(type); |
1125 | 0 | break; |
1126 | | |
1127 | 0 | default: |
1128 | 0 | string = rpc_error_to_category(code); |
1129 | 0 | break; |
1130 | 0 | } |
1131 | | |
1132 | 0 | return string; |
1133 | 0 | } |
1134 | | |
1135 | | void freerdp_set_last_error_ex(rdpContext* context, UINT32 lastError, const char* fkt, |
1136 | | const char* file, int line) |
1137 | 0 | { |
1138 | 0 | WINPR_ASSERT(context); |
1139 | 0 | WINPR_ASSERT(line >= 0); |
1140 | | |
1141 | 0 | if (lastError) |
1142 | 0 | { |
1143 | 0 | if (WLog_IsLevelActive(context->log, WLOG_ERROR)) |
1144 | 0 | { |
1145 | 0 | WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_ERROR, (size_t)line, file, fkt, |
1146 | 0 | "%s [0x%08" PRIX32 "]", freerdp_get_last_error_name(lastError), |
1147 | 0 | lastError); |
1148 | 0 | } |
1149 | 0 | } |
1150 | |
|
1151 | 0 | if (lastError == FREERDP_ERROR_SUCCESS) |
1152 | 0 | { |
1153 | 0 | if (WLog_IsLevelActive(context->log, WLOG_DEBUG)) |
1154 | 0 | WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_DEBUG, (size_t)line, file, fkt, |
1155 | 0 | "resetting error state"); |
1156 | 0 | } |
1157 | 0 | else if (context->LastError != FREERDP_ERROR_SUCCESS) |
1158 | 0 | { |
1159 | 0 | if (WLog_IsLevelActive(context->log, WLOG_ERROR)) |
1160 | 0 | { |
1161 | 0 | WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_ERROR, (size_t)line, file, fkt, |
1162 | 0 | "TODO: Trying to set error code %s, but %s already set!", |
1163 | 0 | freerdp_get_last_error_name(lastError), |
1164 | 0 | freerdp_get_last_error_name(context->LastError)); |
1165 | 0 | } |
1166 | 0 | } |
1167 | 0 | context->LastError = lastError; |
1168 | 0 | } |
1169 | | |
1170 | | const char* freerdp_get_logon_error_info_type_ex(UINT32 type, char* buffer, size_t size) |
1171 | 0 | { |
1172 | 0 | const char* str = freerdp_get_logon_error_info_type(type); |
1173 | 0 | (void)_snprintf(buffer, size, "%s(0x%04" PRIx32 ")", str, type); |
1174 | 0 | return buffer; |
1175 | 0 | } |
1176 | | |
1177 | | const char* freerdp_get_logon_error_info_type(UINT32 type) |
1178 | 0 | { |
1179 | 0 | switch (type) |
1180 | 0 | { |
1181 | 0 | case LOGON_MSG_DISCONNECT_REFUSED: |
1182 | 0 | return "LOGON_MSG_DISCONNECT_REFUSED"; |
1183 | | |
1184 | 0 | case LOGON_MSG_NO_PERMISSION: |
1185 | 0 | return "LOGON_MSG_NO_PERMISSION"; |
1186 | | |
1187 | 0 | case LOGON_MSG_BUMP_OPTIONS: |
1188 | 0 | return "LOGON_MSG_BUMP_OPTIONS"; |
1189 | | |
1190 | 0 | case LOGON_MSG_RECONNECT_OPTIONS: |
1191 | 0 | return "LOGON_MSG_RECONNECT_OPTIONS"; |
1192 | | |
1193 | 0 | case LOGON_MSG_SESSION_TERMINATE: |
1194 | 0 | return "LOGON_MSG_SESSION_TERMINATE"; |
1195 | | |
1196 | 0 | case LOGON_MSG_SESSION_CONTINUE: |
1197 | 0 | return "LOGON_MSG_SESSION_CONTINUE"; |
1198 | | |
1199 | 0 | default: |
1200 | 0 | return "UNKNOWN"; |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | | const char* freerdp_get_logon_error_info_data(UINT32 data) |
1205 | 0 | { |
1206 | 0 | switch (data) |
1207 | 0 | { |
1208 | 0 | case LOGON_FAILED_BAD_PASSWORD: |
1209 | 0 | return "LOGON_FAILED_BAD_PASSWORD"; |
1210 | | |
1211 | 0 | case LOGON_FAILED_UPDATE_PASSWORD: |
1212 | 0 | return "LOGON_FAILED_UPDATE_PASSWORD"; |
1213 | | |
1214 | 0 | case LOGON_FAILED_OTHER: |
1215 | 0 | return "LOGON_FAILED_OTHER"; |
1216 | | |
1217 | 0 | case LOGON_WARNING: |
1218 | 0 | return "LOGON_WARNING"; |
1219 | | |
1220 | 0 | default: |
1221 | 0 | return "SESSION_ID"; |
1222 | 0 | } |
1223 | 0 | } |
1224 | | |
1225 | | const char* freerdp_get_logon_error_info_data_ex(UINT32 data, char* buffer, size_t size) |
1226 | 0 | { |
1227 | 0 | const char* str = freerdp_get_logon_error_info_data(data); |
1228 | 0 | (void)_snprintf(buffer, size, "%s(0x%04" PRIx32 ")", str, data); |
1229 | 0 | return buffer; |
1230 | 0 | } |
1231 | | |
1232 | | /** Allocator function for the rdp_freerdp structure. |
1233 | | * @return an allocated structure filled with 0s. Need to be deallocated using freerdp_free() |
1234 | | */ |
1235 | | freerdp* freerdp_new(void) |
1236 | 0 | { |
1237 | 0 | freerdp* instance = NULL; |
1238 | 0 | instance = (freerdp*)calloc(1, sizeof(freerdp)); |
1239 | |
|
1240 | 0 | if (!instance) |
1241 | 0 | return NULL; |
1242 | | |
1243 | 0 | instance->ContextSize = sizeof(rdpContext); |
1244 | 0 | instance->SendChannelData = freerdp_send_channel_data; |
1245 | 0 | instance->SendChannelPacket = freerdp_send_channel_packet; |
1246 | 0 | instance->ReceiveChannelData = freerdp_channels_data; |
1247 | 0 | return instance; |
1248 | 0 | } |
1249 | | |
1250 | | /** Deallocator function for the rdp_freerdp structure. |
1251 | | * @param instance - pointer to the rdp_freerdp structure to deallocate. |
1252 | | * On return, this pointer is not valid anymore. |
1253 | | */ |
1254 | | void freerdp_free(freerdp* instance) |
1255 | 0 | { |
1256 | 0 | free(instance); |
1257 | 0 | } |
1258 | | |
1259 | | ULONG freerdp_get_transport_sent(rdpContext* context, BOOL resetCount) |
1260 | 0 | { |
1261 | 0 | WINPR_ASSERT(context); |
1262 | 0 | WINPR_ASSERT(context->rdp); |
1263 | 0 | UINT64 rc = transport_get_bytes_sent(context->rdp->transport, resetCount); |
1264 | 0 | return WINPR_CXX_COMPAT_CAST(ULONG, MIN(rc, UINT32_MAX)); |
1265 | 0 | } |
1266 | | |
1267 | | BOOL freerdp_nla_impersonate(rdpContext* context) |
1268 | 0 | { |
1269 | 0 | rdpNla* nla = NULL; |
1270 | |
|
1271 | 0 | if (!context) |
1272 | 0 | return FALSE; |
1273 | | |
1274 | 0 | if (!context->rdp) |
1275 | 0 | return FALSE; |
1276 | | |
1277 | 0 | if (!context->rdp->transport) |
1278 | 0 | return FALSE; |
1279 | | |
1280 | 0 | nla = transport_get_nla(context->rdp->transport); |
1281 | 0 | return nla_impersonate(nla); |
1282 | 0 | } |
1283 | | |
1284 | | BOOL freerdp_nla_revert_to_self(rdpContext* context) |
1285 | 0 | { |
1286 | 0 | rdpNla* nla = NULL; |
1287 | |
|
1288 | 0 | if (!context) |
1289 | 0 | return FALSE; |
1290 | | |
1291 | 0 | if (!context->rdp) |
1292 | 0 | return FALSE; |
1293 | | |
1294 | 0 | if (!context->rdp->transport) |
1295 | 0 | return FALSE; |
1296 | | |
1297 | 0 | nla = transport_get_nla(context->rdp->transport); |
1298 | 0 | return nla_revert_to_self(nla); |
1299 | 0 | } |
1300 | | |
1301 | | UINT32 freerdp_get_nla_sspi_error(rdpContext* context) |
1302 | 0 | { |
1303 | 0 | WINPR_ASSERT(context); |
1304 | 0 | WINPR_ASSERT(context->rdp); |
1305 | 0 | WINPR_ASSERT(context->rdp->transport); |
1306 | | |
1307 | 0 | rdpNla* nla = transport_get_nla(context->rdp->transport); |
1308 | |
|
1309 | 0 | return (UINT32)nla_get_sspi_error(nla); |
1310 | 0 | } |
1311 | | |
1312 | | BOOL freerdp_nla_encrypt(rdpContext* context, const SecBuffer* inBuffer, SecBuffer* outBuffer) |
1313 | 0 | { |
1314 | 0 | WINPR_ASSERT(context); |
1315 | 0 | WINPR_ASSERT(context->rdp); |
1316 | | |
1317 | 0 | rdpNla* nla = context->rdp->nla; |
1318 | 0 | return nla_encrypt(nla, inBuffer, outBuffer); |
1319 | 0 | } |
1320 | | |
1321 | | BOOL freerdp_nla_decrypt(rdpContext* context, const SecBuffer* inBuffer, SecBuffer* outBuffer) |
1322 | 0 | { |
1323 | 0 | WINPR_ASSERT(context); |
1324 | 0 | WINPR_ASSERT(context->rdp); |
1325 | | |
1326 | 0 | rdpNla* nla = context->rdp->nla; |
1327 | 0 | return nla_decrypt(nla, inBuffer, outBuffer); |
1328 | 0 | } |
1329 | | |
1330 | | SECURITY_STATUS freerdp_nla_QueryContextAttributes(rdpContext* context, DWORD ulAttr, PVOID pBuffer) |
1331 | 0 | { |
1332 | 0 | WINPR_ASSERT(context); |
1333 | 0 | WINPR_ASSERT(context->rdp); |
1334 | | |
1335 | 0 | return nla_QueryContextAttributes(context->rdp->nla, ulAttr, pBuffer); |
1336 | 0 | } |
1337 | | |
1338 | | HANDLE getChannelErrorEventHandle(rdpContext* context) |
1339 | 0 | { |
1340 | 0 | WINPR_ASSERT(context); |
1341 | 0 | return context->channelErrorEvent; |
1342 | 0 | } |
1343 | | |
1344 | | BOOL checkChannelErrorEvent(rdpContext* context) |
1345 | 0 | { |
1346 | 0 | WINPR_ASSERT(context); |
1347 | | |
1348 | 0 | if (WaitForSingleObject(context->channelErrorEvent, 0) == WAIT_OBJECT_0) |
1349 | 0 | { |
1350 | 0 | WLog_Print(context->log, WLOG_ERROR, "%s. Error was %" PRIu32 "", context->errorDescription, |
1351 | 0 | context->channelErrorNum); |
1352 | 0 | return FALSE; |
1353 | 0 | } |
1354 | | |
1355 | 0 | return TRUE; |
1356 | 0 | } |
1357 | | |
1358 | | /** |
1359 | | * Function description |
1360 | | * |
1361 | | * @return 0 on success, otherwise a Win32 error code |
1362 | | */ |
1363 | | UINT getChannelError(rdpContext* context) |
1364 | 0 | { |
1365 | 0 | WINPR_ASSERT(context); |
1366 | 0 | return context->channelErrorNum; |
1367 | 0 | } |
1368 | | |
1369 | | const char* getChannelErrorDescription(rdpContext* context) |
1370 | 0 | { |
1371 | 0 | WINPR_ASSERT(context); |
1372 | 0 | return context->errorDescription; |
1373 | 0 | } |
1374 | | |
1375 | | void clearChannelError(rdpContext* context) |
1376 | 0 | { |
1377 | 0 | WINPR_ASSERT(context); |
1378 | 0 | context->channelErrorNum = 0; |
1379 | 0 | memset(context->errorDescription, 0, 500); |
1380 | 0 | (void)ResetEvent(context->channelErrorEvent); |
1381 | 0 | } |
1382 | | |
1383 | | WINPR_ATTR_FORMAT_ARG(3, 4) |
1384 | | void setChannelError(rdpContext* context, UINT errorNum, WINPR_FORMAT_ARG const char* format, ...) |
1385 | 0 | { |
1386 | 0 | va_list ap = { 0 }; |
1387 | 0 | va_start(ap, format); |
1388 | |
|
1389 | 0 | WINPR_ASSERT(context); |
1390 | | |
1391 | 0 | context->channelErrorNum = errorNum; |
1392 | 0 | (void)vsnprintf(context->errorDescription, 499, format, ap); |
1393 | 0 | va_end(ap); |
1394 | 0 | (void)SetEvent(context->channelErrorEvent); |
1395 | 0 | } |
1396 | | |
1397 | | const char* freerdp_nego_get_routing_token(rdpContext* context, DWORD* length) |
1398 | 0 | { |
1399 | 0 | if (!context || !context->rdp) |
1400 | 0 | return NULL; |
1401 | | |
1402 | 0 | return (const char*)nego_get_routing_token(context->rdp->nego, length); |
1403 | 0 | } |
1404 | | |
1405 | | BOOL freerdp_io_callback_set_event(rdpContext* context, BOOL set) |
1406 | 0 | { |
1407 | 0 | WINPR_ASSERT(context); |
1408 | 0 | return rdp_io_callback_set_event(context->rdp, set); |
1409 | 0 | } |
1410 | | |
1411 | | const rdpTransportIo* freerdp_get_io_callbacks(rdpContext* context) |
1412 | 0 | { |
1413 | 0 | WINPR_ASSERT(context); |
1414 | 0 | return rdp_get_io_callbacks(context->rdp); |
1415 | 0 | } |
1416 | | |
1417 | | BOOL freerdp_set_io_callbacks(rdpContext* context, const rdpTransportIo* io_callbacks) |
1418 | 0 | { |
1419 | 0 | WINPR_ASSERT(context); |
1420 | 0 | return rdp_set_io_callbacks(context->rdp, io_callbacks); |
1421 | 0 | } |
1422 | | |
1423 | | BOOL freerdp_set_io_callback_context(rdpContext* context, void* usercontext) |
1424 | 0 | { |
1425 | 0 | WINPR_ASSERT(context); |
1426 | 0 | return rdp_set_io_callback_context(context->rdp, usercontext); |
1427 | 0 | } |
1428 | | |
1429 | | void* freerdp_get_io_callback_context(rdpContext* context) |
1430 | 0 | { |
1431 | 0 | WINPR_ASSERT(context); |
1432 | 0 | return rdp_get_io_callback_context(context->rdp); |
1433 | 0 | } |
1434 | | |
1435 | | CONNECTION_STATE freerdp_get_state(const rdpContext* context) |
1436 | 0 | { |
1437 | 0 | WINPR_ASSERT(context); |
1438 | 0 | return rdp_get_state(context->rdp); |
1439 | 0 | } |
1440 | | |
1441 | | const char* freerdp_state_string(CONNECTION_STATE state) |
1442 | 0 | { |
1443 | 0 | return rdp_state_string(state); |
1444 | 0 | } |
1445 | | |
1446 | | BOOL freerdp_is_active_state(const rdpContext* context) |
1447 | 0 | { |
1448 | 0 | WINPR_ASSERT(context); |
1449 | 0 | return rdp_is_active_state(context->rdp); |
1450 | 0 | } |
1451 | | |
1452 | | BOOL freerdp_channels_from_mcs(rdpSettings* settings, const rdpContext* context) |
1453 | 0 | { |
1454 | 0 | WINPR_ASSERT(context); |
1455 | 0 | return rdp_channels_from_mcs(settings, context->rdp); |
1456 | 0 | } |
1457 | | |
1458 | | HANDLE freerdp_abort_event(rdpContext* context) |
1459 | 0 | { |
1460 | 0 | WINPR_ASSERT(context); |
1461 | 0 | return utils_get_abort_event(context->rdp); |
1462 | 0 | } |
1463 | | |
1464 | | static void test_mcs_free(rdpMcs* mcs) |
1465 | 0 | { |
1466 | 0 | if (!mcs) |
1467 | 0 | return; |
1468 | | |
1469 | 0 | rdpTransport* transport = mcs->transport; |
1470 | 0 | rdpContext* context = transport_get_context(transport); |
1471 | 0 | if (context) |
1472 | 0 | { |
1473 | 0 | rdpSettings* settings = context->settings; |
1474 | 0 | freerdp_settings_free(settings); |
1475 | 0 | } |
1476 | 0 | free(context); |
1477 | 0 | transport_free(transport); |
1478 | |
|
1479 | 0 | mcs_free(mcs); |
1480 | 0 | } |
1481 | | |
1482 | | static rdpMcs* test_mcs_new(void) |
1483 | 0 | { |
1484 | 0 | rdpTransport* transport = NULL; |
1485 | 0 | rdpSettings* settings = freerdp_settings_new(0); |
1486 | 0 | rdpContext* context = calloc(1, sizeof(rdpContext)); |
1487 | |
|
1488 | 0 | if (!settings) |
1489 | 0 | goto fail; |
1490 | 0 | if (!freerdp_settings_set_bool(settings, FreeRDP_TransportDumpReplay, TRUE)) |
1491 | 0 | goto fail; |
1492 | | |
1493 | 0 | if (!context) |
1494 | 0 | goto fail; |
1495 | 0 | context->settings = settings; |
1496 | 0 | transport = transport_new(context); |
1497 | 0 | if (!transport) |
1498 | 0 | goto fail; |
1499 | 0 | return mcs_new(transport); |
1500 | | |
1501 | 0 | fail: |
1502 | 0 | transport_free(transport); |
1503 | 0 | free(context); |
1504 | 0 | freerdp_settings_free(settings); |
1505 | |
|
1506 | 0 | return NULL; |
1507 | 0 | } |
1508 | | |
1509 | | BOOL freerdp_is_valid_mcs_create_request(const BYTE* data, size_t size) |
1510 | 0 | { |
1511 | |
|
1512 | 0 | wStream sbuffer = { 0 }; |
1513 | 0 | wStream* s = Stream_StaticConstInit(&sbuffer, data, size); |
1514 | |
|
1515 | 0 | WINPR_ASSERT(data || (size == 0)); |
1516 | 0 | WINPR_ASSERT(s); |
1517 | | |
1518 | 0 | rdpMcs* mcs = test_mcs_new(); |
1519 | 0 | WINPR_ASSERT(mcs); |
1520 | | |
1521 | 0 | BOOL result = mcs_recv_connect_initial(mcs, s); |
1522 | 0 | test_mcs_free(mcs); |
1523 | 0 | return result; |
1524 | 0 | } |
1525 | | |
1526 | | BOOL freerdp_is_valid_mcs_create_response(const BYTE* data, size_t size) |
1527 | 0 | { |
1528 | |
|
1529 | 0 | wStream sbuffer = { 0 }; |
1530 | 0 | wStream* s = Stream_StaticConstInit(&sbuffer, data, size); |
1531 | |
|
1532 | 0 | WINPR_ASSERT(data || (size == 0)); |
1533 | 0 | WINPR_ASSERT(s); |
1534 | | |
1535 | 0 | rdpMcs* mcs = test_mcs_new(); |
1536 | 0 | WINPR_ASSERT(mcs); |
1537 | | |
1538 | 0 | BOOL result = mcs_recv_connect_response(mcs, s); |
1539 | 0 | test_mcs_free(mcs); |
1540 | 0 | return result; |
1541 | 0 | } |
1542 | | |
1543 | | BOOL freerdp_persist_credentials(rdpContext* context) |
1544 | 0 | { |
1545 | 0 | if (!context) |
1546 | 0 | return FALSE; |
1547 | 0 | WINPR_ASSERT(context->rdp); |
1548 | 0 | return utils_persist_credentials(context->rdp->originalSettings, context->rdp->settings); |
1549 | 0 | } |
1550 | | |
1551 | | const char* freerdp_disconnect_reason_string(int reason) |
1552 | 0 | { |
1553 | 0 | switch (reason) |
1554 | 0 | { |
1555 | 0 | case Disconnect_Ultimatum_domain_disconnected: |
1556 | 0 | return "rn-domain-disconnected"; |
1557 | 0 | case Disconnect_Ultimatum_provider_initiated: |
1558 | 0 | return "rn-provider-initiated"; |
1559 | 0 | case Disconnect_Ultimatum_token_purged: |
1560 | 0 | return "rn-token-purged"; |
1561 | 0 | case Disconnect_Ultimatum_user_requested: |
1562 | 0 | return "rn-user-requested"; |
1563 | 0 | case Disconnect_Ultimatum_channel_purged: |
1564 | 0 | return "rn-channel-purged"; |
1565 | 0 | default: |
1566 | 0 | return "rn-unknown"; |
1567 | 0 | } |
1568 | 0 | } |
1569 | | |
1570 | | BOOL freerdp_set_common_access_token(rdpContext* context, |
1571 | | pGetCommonAccessToken GetCommonAccessToken) |
1572 | 0 | { |
1573 | 0 | WINPR_ASSERT(context); |
1574 | 0 | WINPR_ASSERT(context->rdp); |
1575 | 0 | context->rdp->GetCommonAccessToken = GetCommonAccessToken; |
1576 | 0 | return TRUE; |
1577 | 0 | } |
1578 | | |
1579 | | pGetCommonAccessToken freerdp_get_common_access_token(rdpContext* context) |
1580 | 0 | { |
1581 | 0 | WINPR_ASSERT(context); |
1582 | 0 | WINPR_ASSERT(context->rdp); |
1583 | 0 | return context->rdp->GetCommonAccessToken; |
1584 | 0 | } |