FreeRDP
freerdp.c
1 
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 #define TAG FREERDP_TAG("core")
64 
65 static void sig_abort_connect(int signum, const char* signame, void* ctx)
66 {
67  rdpContext* context = (rdpContext*)ctx;
68 
69  WLog_INFO(TAG, "Signal %s [%d], terminating session %p", signame, signum, context);
70  if (context)
71  freerdp_abort_connect_context(context);
72 }
73 
85 static int freerdp_connect_begin(freerdp* instance)
86 {
87  BOOL rc = 0;
88  rdpRdp* rdp = NULL;
89  BOOL status = TRUE;
90  rdpSettings* settings = NULL;
91  UINT32 KeyboardLayout = 0;
92 
93  if (!instance)
94  return -1;
95 
96  WINPR_ASSERT(instance->context);
97 
98  /* We always set the return code to 0 before we start the connect sequence*/
99  instance->ConnectionCallbackState = CLIENT_STATE_INITIAL;
100  freerdp_set_last_error_log(instance->context, FREERDP_ERROR_SUCCESS);
101  clearChannelError(instance->context);
102  if (!utils_reset_abort(instance->context->rdp))
103  return -1;
104 
105  rdp = instance->context->rdp;
106  WINPR_ASSERT(rdp);
107 
108  settings = instance->context->settings;
109  WINPR_ASSERT(settings);
110 
111  freerdp_channels_register_instance(instance->context->channels, instance);
112 
113  if (!freerdp_settings_set_default_order_support(settings))
114  return -1;
115 
116  freerdp_add_signal_cleanup_handler(instance->context, sig_abort_connect);
117 
118  IFCALLRET(instance->PreConnect, status, instance);
119  instance->ConnectionCallbackState = CLIENT_STATE_PRECONNECT_PASSED;
120 
121  freerdp_settings_print_warnings(settings);
122  if (status)
123  status = freerdp_settings_enforce_monitor_exists(settings);
124 
125  if (status)
126  status = freerdp_settings_check_client_after_preconnect(settings);
127 
128  if (status)
129  status = rdp_set_backup_settings(rdp);
130  if (status)
131  status = utils_reload_channels(instance->context);
132 
133  const UINT32 cp = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardCodePage);
134  KeyboardLayout = freerdp_get_keyboard_default_layout_for_locale(cp);
135  if (KeyboardLayout == 0)
136  KeyboardLayout = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardLayout);
137 
138  switch (KeyboardLayout)
139  {
140  case KBD_JAPANESE:
141  case KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002:
142  {
143  if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardType,
144  WINPR_KBD_TYPE_JAPANESE))
145  return -1;
146  if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardSubType, 2))
147  return -1;
148  if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardFunctionKey, 12))
149  return -1;
150  }
151  break;
152  default:
153  break;
154  }
155 
156  if (!status)
157  {
158  rdpContext* context = instance->context;
159  WINPR_ASSERT(context);
160  freerdp_set_last_error_if_not(context, FREERDP_ERROR_PRE_CONNECT_FAILED);
161 
162  WLog_Print(context->log, WLOG_ERROR, "freerdp_pre_connect failed");
163  return 0;
164  }
165 
166  rc = rdp_client_connect(rdp);
167 
168  /* --authonly tests the connection without a UI */
169  if (freerdp_settings_get_bool(rdp->settings, FreeRDP_AuthenticationOnly))
170  {
171  rdpContext* context = rdp->context;
172  WINPR_ASSERT(context);
173  WLog_Print(context->log, WLOG_ERROR, "Authentication only, exit status %" PRId32 "", rc);
174  return 0;
175  }
176 
177  return rc ? 1 : 0;
178 }
179 
180 BOOL freerdp_connect(freerdp* instance)
181 {
182  BOOL status = FALSE;
183  ConnectionResultEventArgs e = { 0 };
184  const int rc = freerdp_connect_begin(instance);
185  rdpRdp* rdp = NULL;
186  UINT status2 = ERROR_INTERNAL_ERROR;
187 
188  WINPR_ASSERT(instance);
189  WINPR_ASSERT(instance->context);
190 
191  rdp = instance->context->rdp;
192  WINPR_ASSERT(rdp);
193  WINPR_ASSERT(rdp->settings);
194 
195  if (rc < 0)
196  return FALSE;
197 
198  if (rc == 0)
199  goto freerdp_connect_finally;
200 
201  /* Pointers might have changed inbetween */
202  {
203  rdp_update_internal* up = update_cast(rdp->update);
204 
205  if (freerdp_settings_get_bool(rdp->settings, FreeRDP_DumpRemoteFx))
206  {
207  up->pcap_rfx = pcap_open(
208  freerdp_settings_get_string(rdp->settings, FreeRDP_DumpRemoteFxFile), TRUE);
209 
210  if (up->pcap_rfx)
211  up->dump_rfx = TRUE;
212  }
213  }
214 
215  if (rc > 0)
216  {
217  pointer_cache_register_callbacks(instance->context->update);
218  status = IFCALLRESULT(TRUE, instance->PostConnect, instance);
219  instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED;
220 
221  if (status)
222  status2 = freerdp_channels_post_connect(instance->context->channels, instance);
223  }
224  else
225  {
226  status2 = CHANNEL_RC_OK;
227  if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_CONNECT_TRANSPORT_FAILED)
228  status = freerdp_reconnect(instance);
229  else
230  goto freerdp_connect_finally;
231  }
232 
233  if (!status || (status2 != CHANNEL_RC_OK) || !update_post_connect(instance->context->update))
234  {
235  rdpContext* context = instance->context;
236  WINPR_ASSERT(context);
237  WLog_Print(context->log, WLOG_ERROR, "freerdp_post_connect failed");
238 
239  freerdp_set_last_error_if_not(context, FREERDP_ERROR_POST_CONNECT_FAILED);
240 
241  status = FALSE;
242  goto freerdp_connect_finally;
243  }
244 
245  if (rdp->settings->PlayRemoteFx)
246  {
247  wStream* s = NULL;
248  rdp_update_internal* update = update_cast(instance->context->update);
249  pcap_record record = { 0 };
250 
251  WINPR_ASSERT(update);
252  update->pcap_rfx = pcap_open(rdp->settings->PlayRemoteFxFile, FALSE);
253  status = FALSE;
254 
255  if (!update->pcap_rfx)
256  goto freerdp_connect_finally;
257  else
258  update->play_rfx = TRUE;
259 
260  status = TRUE;
261 
262  while (pcap_has_next_record(update->pcap_rfx) && status)
263  {
264  pcap_get_next_record_header(update->pcap_rfx, &record);
265 
266  s = transport_take_from_pool(rdp->transport, record.length);
267  if (!s)
268  break;
269 
270  record.data = Stream_Buffer(s);
271  pcap_get_next_record_content(update->pcap_rfx, &record);
272  Stream_SetLength(s, record.length);
273  Stream_SetPosition(s, 0);
274 
275  if (!update_begin_paint(&update->common))
276  status = FALSE;
277  else
278  {
279  if (update_recv_surfcmds(&update->common, s) < 0)
280  status = FALSE;
281 
282  if (!update_end_paint(&update->common))
283  status = FALSE;
284  }
285 
286  Stream_Release(s);
287  }
288 
289  pcap_close(update->pcap_rfx);
290  update->pcap_rfx = NULL;
291  goto freerdp_connect_finally;
292  }
293 
294  if (rdp->errorInfo == ERRINFO_SERVER_INSUFFICIENT_PRIVILEGES)
295  freerdp_set_last_error_log(instance->context, FREERDP_ERROR_INSUFFICIENT_PRIVILEGES);
296 
297  transport_set_connected_event(rdp->transport);
298 
299 freerdp_connect_finally:
300  EventArgsInit(&e, "freerdp");
301  e.result = status ? 0 : -1;
302  PubSub_OnConnectionResult(rdp->pubSub, instance->context, &e);
303 
304  if (!status)
305  freerdp_disconnect(instance);
306 
307  return status;
308 }
309 
310 BOOL freerdp_abort_connect(freerdp* instance)
311 {
312  if (!instance)
313  return FALSE;
314 
315  return freerdp_abort_connect_context(instance->context);
316 }
317 
318 BOOL freerdp_abort_connect_context(rdpContext* context)
319 {
320  if (!context)
321  return FALSE;
322 
323  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
324 
325  return utils_abort_connect(context->rdp);
326 }
327 
328 #if defined(WITH_FREERDP_DEPRECATED)
329 BOOL freerdp_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
330 {
331  rdpRdp* rdp = NULL;
332 
333  WINPR_ASSERT(instance);
334  WINPR_ASSERT(instance->context);
335 
336  rdp = instance->context->rdp;
337  WINPR_ASSERT(rdp);
338 
339  transport_get_fds(rdp->transport, rfds, rcount);
340  return TRUE;
341 }
342 #endif
343 
344 BOOL freerdp_check_fds(freerdp* instance)
345 {
346  int status = 0;
347  rdpRdp* rdp = NULL;
348 
349  if (!instance)
350  return FALSE;
351 
352  if (!instance->context)
353  return FALSE;
354 
355  if (!instance->context->rdp)
356  return FALSE;
357 
358  rdp = instance->context->rdp;
359  status = rdp_check_fds(rdp);
360 
361  if (status < 0)
362  {
363  TerminateEventArgs e;
364  rdpContext* context = instance->context;
365  WINPR_ASSERT(context);
366 
367  WLog_Print(context->log, WLOG_DEBUG, "rdp_check_fds() - %i", status);
368  EventArgsInit(&e, "freerdp");
369  e.code = 0;
370  PubSub_OnTerminate(rdp->pubSub, context, &e);
371  return FALSE;
372  }
373 
374  return TRUE;
375 }
376 
377 DWORD freerdp_get_event_handles(rdpContext* context, HANDLE* events, DWORD count)
378 {
379  DWORD nCount = 0;
380 
381  WINPR_ASSERT(context);
382  WINPR_ASSERT(context->rdp);
383  WINPR_ASSERT(events || (count == 0));
384 
385  nCount += transport_get_event_handles(context->rdp->transport, events, count);
386 
387  if (nCount == 0)
388  return 0;
389 
390  if (events && (nCount < count + 2))
391  {
392  events[nCount++] = freerdp_channels_get_event_handle(context->instance);
393  events[nCount++] = getChannelErrorEventHandle(context);
394  events[nCount++] = utils_get_abort_event(context->rdp);
395  }
396  else
397  return 0;
398 
399  const SSIZE_T rc = freerdp_client_channel_get_registered_event_handles(
400  context->channels, &events[nCount], count - nCount);
401  if (rc < 0)
402  return 0;
403  return nCount + (DWORD)rc;
404 }
405 
406 /* Resend mouse cursor position to prevent session lock in prevent-session-lock mode */
407 static BOOL freerdp_prevent_session_lock(rdpContext* context)
408 {
409  WINPR_ASSERT(context);
410  WINPR_ASSERT(context->input);
411 
412  rdp_input_internal* in = input_cast(context->input);
413 
414  UINT32 FakeMouseMotionInterval =
415  freerdp_settings_get_uint32(context->settings, FreeRDP_FakeMouseMotionInterval);
416  if (FakeMouseMotionInterval && in->lastInputTimestamp)
417  {
418  const time_t now = time(NULL);
419  if (now - in->lastInputTimestamp > FakeMouseMotionInterval)
420  {
421  WLog_Print(context->log, WLOG_DEBUG,
422  "fake mouse move: x=%d y=%d lastInputTimestamp=%" PRIu64 " "
423  "FakeMouseMotionInterval=%" PRIu32,
424  in->lastX, in->lastY, in->lastInputTimestamp, FakeMouseMotionInterval);
425 
426  BOOL status = freerdp_input_send_mouse_event(context->input, PTR_FLAGS_MOVE, in->lastX,
427  in->lastY);
428  if (!status)
429  {
430  if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
431  WLog_Print(context->log, WLOG_ERROR,
432  "freerdp_prevent_session_lock() failed - %" PRIi32 "", status);
433 
434  return FALSE;
435  }
436 
437  return status;
438  }
439  }
440 
441  return TRUE;
442 }
443 
444 BOOL freerdp_check_event_handles(rdpContext* context)
445 {
446  WINPR_ASSERT(context);
447 
448  BOOL status = freerdp_check_fds(context->instance);
449 
450  if (!status)
451  {
452  if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
453  WLog_Print(context->log, WLOG_ERROR, "freerdp_check_fds() failed - %" PRIi32 "",
454  status);
455 
456  return FALSE;
457  }
458 
459  status = freerdp_channels_check_fds(context->channels, context->instance);
460 
461  if (!status)
462  {
463  if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
464  WLog_Print(context->log, WLOG_ERROR,
465  "freerdp_channels_check_fds() failed - %" PRIi32 "", status);
466 
467  return FALSE;
468  }
469 
470  status = checkChannelErrorEvent(context);
471 
472  if (!status)
473  {
474  if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
475  WLog_Print(context->log, WLOG_ERROR, "checkChannelErrorEvent() failed - %" PRIi32 "",
476  status);
477 
478  return FALSE;
479  }
480 
481  status = freerdp_prevent_session_lock(context);
482 
483  return status;
484 }
485 
486 wMessageQueue* freerdp_get_message_queue(freerdp* instance, DWORD id)
487 {
488  wMessageQueue* queue = NULL;
489 
490  WINPR_ASSERT(instance);
491 
492  rdpContext* context = instance->context;
493  WINPR_ASSERT(context);
494 
495  switch (id)
496  {
497  case FREERDP_UPDATE_MESSAGE_QUEUE:
498  {
499  rdp_update_internal* update = update_cast(context->update);
500  queue = update->queue;
501  }
502  break;
503 
504  case FREERDP_INPUT_MESSAGE_QUEUE:
505  {
506  rdp_input_internal* input = input_cast(context->input);
507  queue = input->queue;
508  }
509  break;
510  default:
511  break;
512  }
513 
514  return queue;
515 }
516 
517 HANDLE freerdp_get_message_queue_event_handle(freerdp* instance, DWORD id)
518 {
519  HANDLE event = NULL;
520  wMessageQueue* queue = freerdp_get_message_queue(instance, id);
521 
522  if (queue)
523  event = MessageQueue_Event(queue);
524 
525  return event;
526 }
527 
528 int freerdp_message_queue_process_message(freerdp* instance, DWORD id, wMessage* message)
529 {
530  int status = -1;
531  rdpContext* context = NULL;
532 
533  WINPR_ASSERT(instance);
534 
535  context = instance->context;
536  WINPR_ASSERT(context);
537 
538  switch (id)
539  {
540  case FREERDP_UPDATE_MESSAGE_QUEUE:
541  status = update_message_queue_process_message(context->update, message);
542  break;
543 
544  case FREERDP_INPUT_MESSAGE_QUEUE:
545  status = input_message_queue_process_message(context->input, message);
546  break;
547  default:
548  break;
549  }
550 
551  return status;
552 }
553 
554 int freerdp_message_queue_process_pending_messages(freerdp* instance, DWORD id)
555 {
556  int status = -1;
557  rdpContext* context = NULL;
558 
559  WINPR_ASSERT(instance);
560 
561  context = instance->context;
562  WINPR_ASSERT(context);
563 
564  switch (id)
565  {
566  case FREERDP_UPDATE_MESSAGE_QUEUE:
567  status = update_message_queue_process_pending_messages(context->update);
568  break;
569 
570  case FREERDP_INPUT_MESSAGE_QUEUE:
571  status = input_message_queue_process_pending_messages(context->input);
572  break;
573  default:
574  break;
575  }
576 
577  return status;
578 }
579 
580 static BOOL freerdp_send_channel_data(freerdp* instance, UINT16 channelId, const BYTE* data,
581  size_t size)
582 {
583  WINPR_ASSERT(instance);
584  WINPR_ASSERT(instance->context);
585  WINPR_ASSERT(instance->context->rdp);
586  return rdp_send_channel_data(instance->context->rdp, channelId, data, size);
587 }
588 
589 static BOOL freerdp_send_channel_packet(freerdp* instance, UINT16 channelId, size_t totalSize,
590  UINT32 flags, const BYTE* data, size_t chunkSize)
591 {
592  WINPR_ASSERT(instance);
593  WINPR_ASSERT(instance->context);
594  WINPR_ASSERT(instance->context->rdp);
595  return rdp_channel_send_packet(instance->context->rdp, channelId, totalSize, flags, data,
596  chunkSize);
597 }
598 
599 BOOL freerdp_disconnect(freerdp* instance)
600 {
601  BOOL rc = TRUE;
602  rdpRdp* rdp = NULL;
603  rdp_update_internal* up = NULL;
604 
605  if (!instance || !instance->context)
606  return FALSE;
607 
608  rdp = instance->context->rdp;
609  utils_abort_connect(rdp);
610 
611  if (!rdp_client_disconnect(rdp))
612  rc = FALSE;
613 
614  up = update_cast(rdp->update);
615 
616  update_post_disconnect(rdp->update);
617 
618  IFCALL(instance->PostDisconnect, instance);
619 
620  if (up->pcap_rfx)
621  {
622  up->dump_rfx = FALSE;
623  pcap_close(up->pcap_rfx);
624  up->pcap_rfx = NULL;
625  }
626 
627  freerdp_channels_close(instance->context->channels, instance);
628 
629  IFCALL(instance->PostFinalDisconnect, instance);
630 
631  freerdp_del_signal_cleanup_handler(instance->context, sig_abort_connect);
632  return rc;
633 }
634 
635 BOOL freerdp_disconnect_before_reconnect(freerdp* instance)
636 {
637  WINPR_ASSERT(instance);
638  return freerdp_disconnect_before_reconnect_context(instance->context);
639 }
640 
641 BOOL freerdp_disconnect_before_reconnect_context(rdpContext* context)
642 {
643  rdpRdp* rdp = NULL;
644 
645  WINPR_ASSERT(context);
646 
647  rdp = context->rdp;
648  return rdp_client_disconnect_and_clear(rdp);
649 }
650 
651 BOOL freerdp_reconnect(freerdp* instance)
652 {
653  rdpRdp* rdp = NULL;
654 
655  WINPR_ASSERT(instance);
656  WINPR_ASSERT(instance->context);
657 
658  if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_CONNECT_CANCELLED)
659  return FALSE;
660 
661  rdp = instance->context->rdp;
662 
663  if (!utils_reset_abort(instance->context->rdp))
664  return FALSE;
665  return rdp_client_reconnect(rdp);
666 }
667 
668 BOOL freerdp_shall_disconnect(freerdp* instance)
669 {
670  if (!instance)
671  return FALSE;
672 
673  return freerdp_shall_disconnect_context(instance->context);
674 }
675 
676 BOOL freerdp_shall_disconnect_context(const rdpContext* context)
677 {
678  if (!context)
679  return FALSE;
680 
681  return utils_abort_event_is_set(context->rdp);
682 }
683 
684 BOOL freerdp_focus_required(freerdp* instance)
685 {
686  rdpRdp* rdp = NULL;
687  BOOL bRetCode = FALSE;
688 
689  WINPR_ASSERT(instance);
690  WINPR_ASSERT(instance->context);
691 
692  rdp = instance->context->rdp;
693  WINPR_ASSERT(rdp);
694 
695  if (rdp->resendFocus)
696  {
697  bRetCode = TRUE;
698  rdp->resendFocus = FALSE;
699  }
700 
701  return bRetCode;
702 }
703 
704 void freerdp_set_focus(freerdp* instance)
705 {
706  rdpRdp* rdp = NULL;
707 
708  WINPR_ASSERT(instance);
709  WINPR_ASSERT(instance->context);
710 
711  rdp = instance->context->rdp;
712  WINPR_ASSERT(rdp);
713 
714  rdp->resendFocus = TRUE;
715 }
716 
717 void freerdp_get_version(int* major, int* minor, int* revision)
718 {
719  if (major != NULL)
720  *major = FREERDP_VERSION_MAJOR;
721 
722  if (minor != NULL)
723  *minor = FREERDP_VERSION_MINOR;
724 
725  if (revision != NULL)
726  *revision = FREERDP_VERSION_REVISION;
727 }
728 
729 const char* freerdp_get_version_string(void)
730 {
731  return FREERDP_VERSION_FULL;
732 }
733 
734 const char* freerdp_get_build_config(void)
735 {
736  WINPR_PRAGMA_DIAG_PUSH
737  WINPR_PRAGMA_DIAG_IGNORED_OVERLENGTH_STRINGS
738  static const char build_config[] =
739  "Build configuration: " FREERDP_BUILD_CONFIG "\n"
740  "Build type: " FREERDP_BUILD_TYPE "\n"
741  "CFLAGS: " FREERDP_CFLAGS "\n"
742  "Compiler: " FREERDP_COMPILER_ID ", " FREERDP_COMPILER_VERSION "\n"
743  "Target architecture: " FREERDP_TARGET_ARCH "\n";
744  WINPR_PRAGMA_DIAG_POP
745  return build_config;
746 }
747 
748 const char* freerdp_get_build_revision(void)
749 {
750  return FREERDP_GIT_REVISION;
751 }
752 
753 static wEventType FreeRDP_Events[] = {
754  DEFINE_EVENT_ENTRY(WindowStateChange), DEFINE_EVENT_ENTRY(ResizeWindow),
755  DEFINE_EVENT_ENTRY(LocalResizeWindow), DEFINE_EVENT_ENTRY(EmbedWindow),
756  DEFINE_EVENT_ENTRY(PanningChange), DEFINE_EVENT_ENTRY(ZoomingChange),
757  DEFINE_EVENT_ENTRY(ErrorInfo), DEFINE_EVENT_ENTRY(Terminate),
758  DEFINE_EVENT_ENTRY(ConnectionResult), DEFINE_EVENT_ENTRY(ChannelConnected),
759  DEFINE_EVENT_ENTRY(ChannelDisconnected), DEFINE_EVENT_ENTRY(MouseEvent),
760  DEFINE_EVENT_ENTRY(Activated), DEFINE_EVENT_ENTRY(Timer),
761  DEFINE_EVENT_ENTRY(GraphicsReset)
762 };
763 
774 BOOL freerdp_context_new(freerdp* instance)
775 {
776  return freerdp_context_new_ex(instance, NULL);
777 }
778 
779 BOOL freerdp_context_new_ex(freerdp* instance, rdpSettings* settings)
780 {
781  rdpRdp* rdp = NULL;
782  rdpContext* context = NULL;
783  BOOL ret = TRUE;
784 
785  WINPR_ASSERT(instance);
786 
787  instance->context = context = (rdpContext*)calloc(1, instance->ContextSize);
788 
789  if (!context)
790  return FALSE;
791 
792  context->log = WLog_Get(TAG);
793  if (!context->log)
794  goto fail;
795 
796  /* Set to external settings, prevents rdp_new from creating its own instance */
797  context->settings = settings;
798  context->instance = instance;
799  context->ServerMode = FALSE;
800  context->disconnectUltimatum = 0;
801 
802  context->metrics = metrics_new(context);
803 
804  if (!context->metrics)
805  goto fail;
806 
807  rdp = rdp_new(context);
808 
809  if (!rdp)
810  goto fail;
811 
812  rdp_log_build_warnings(rdp);
813  context->rdp = rdp;
814  context->pubSub = rdp->pubSub;
815 
816  if (!context->pubSub)
817  goto fail;
818 
819  PubSub_AddEventTypes(rdp->pubSub, FreeRDP_Events, ARRAYSIZE(FreeRDP_Events));
820 
821 #if defined(WITH_FREERDP_DEPRECATED)
822  instance->input = rdp->input;
823  instance->update = rdp->update;
824  instance->settings = rdp->settings;
825  instance->autodetect = rdp->autodetect;
826 #endif
827 
828  instance->heartbeat = rdp->heartbeat;
829  context->graphics = graphics_new(context);
830 
831  if (!context->graphics)
832  goto fail;
833 
834  context->input = rdp->input;
835  context->update = rdp->update;
836  context->settings = rdp->settings;
837  context->autodetect = rdp->autodetect;
838 
839  if (!(context->errorDescription = calloc(1, 500)))
840  {
841  WLog_Print(context->log, WLOG_ERROR, "calloc failed!");
842  goto fail;
843  }
844 
845  if (!(context->channelErrorEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
846  {
847  WLog_Print(context->log, WLOG_ERROR, "CreateEvent failed!");
848  goto fail;
849  }
850 
851  update_register_client_callbacks(rdp->update);
852 
853  if (!(context->channels = freerdp_channels_new(instance)))
854  goto fail;
855 
856  context->dump = stream_dump_new();
857  if (!context->dump)
858  goto fail;
859 
860  IFCALLRET(instance->ContextNew, ret, instance, context);
861 
862  if (ret)
863  return TRUE;
864 
865 fail:
866  freerdp_context_free(instance);
867  return FALSE;
868 }
869 
870 BOOL freerdp_context_reset(freerdp* instance)
871 {
872  if (!instance)
873  return FALSE;
874 
875  WINPR_ASSERT(instance->context);
876  rdpRdp* rdp = instance->context->rdp;
877 
878  return rdp_reset_runtime_settings(rdp);
879 }
880 
889 void freerdp_context_free(freerdp* instance)
890 {
891  rdpContext* ctx = NULL;
892 
893  if (!instance)
894  return;
895 
896  if (!instance->context)
897  return;
898 
899  ctx = instance->context;
900 
901  IFCALL(instance->ContextFree, instance, ctx);
902  rdp_free(ctx->rdp);
903  ctx->rdp = NULL;
904  ctx->settings = NULL; /* owned by rdpRdp */
905 
906  graphics_free(ctx->graphics);
907  ctx->graphics = NULL;
908 
909  metrics_free(ctx->metrics);
910  ctx->metrics = NULL;
911 
912  if (ctx->channelErrorEvent)
913  (void)CloseHandle(ctx->channelErrorEvent);
914  ctx->channelErrorEvent = NULL;
915 
916  free(ctx->errorDescription);
917  ctx->errorDescription = NULL;
918 
919  freerdp_channels_free(ctx->channels);
920  ctx->channels = NULL;
921 
922  freerdp_client_codecs_free(ctx->codecs);
923  ctx->codecs = NULL;
924 
925  stream_dump_free(ctx->dump);
926  ctx->dump = NULL;
927 
928  ctx->input = NULL; /* owned by rdpRdp */
929  ctx->update = NULL; /* owned by rdpRdp */
930  ctx->settings = NULL; /* owned by rdpRdp */
931  ctx->autodetect = NULL; /* owned by rdpRdp */
932 
933  free(ctx);
934  instance->context = NULL;
935 #if defined(WITH_FREERDP_DEPRECATED)
936  instance->input = NULL; /* owned by rdpRdp */
937  instance->update = NULL; /* owned by rdpRdp */
938  instance->settings = NULL; /* owned by rdpRdp */
939  instance->autodetect = NULL; /* owned by rdpRdp */
940 #endif
941  instance->heartbeat = NULL; /* owned by rdpRdp */
942 }
943 
944 int freerdp_get_disconnect_ultimatum(rdpContext* context)
945 {
946  WINPR_ASSERT(context);
947  return context->disconnectUltimatum;
948 }
949 
950 UINT32 freerdp_error_info(freerdp* instance)
951 {
952  WINPR_ASSERT(instance);
953  WINPR_ASSERT(instance->context);
954  WINPR_ASSERT(instance->context->rdp);
955  return instance->context->rdp->errorInfo;
956 }
957 
958 void freerdp_set_error_info(rdpRdp* rdp, UINT32 error)
959 {
960  if (!rdp)
961  return;
962 
963  rdp_set_error_info(rdp, error);
964 }
965 
966 BOOL freerdp_send_error_info(rdpRdp* rdp)
967 {
968  if (!rdp)
969  return FALSE;
970 
971  return rdp_send_error_info(rdp);
972 }
973 
974 UINT32 freerdp_get_last_error(rdpContext* context)
975 {
976  WINPR_ASSERT(context);
977  return context->LastError;
978 }
979 
980 const char* freerdp_get_last_error_name(UINT32 code)
981 {
982  const char* name = NULL;
983  const UINT32 cls = GET_FREERDP_ERROR_CLASS(code);
984  const UINT32 type = GET_FREERDP_ERROR_TYPE(code);
985 
986  switch (cls)
987  {
988  case FREERDP_ERROR_ERRBASE_CLASS:
989  name = freerdp_get_error_base_name(type);
990  break;
991 
992  case FREERDP_ERROR_ERRINFO_CLASS:
993  name = freerdp_get_error_info_name(type);
994  break;
995 
996  case FREERDP_ERROR_CONNECT_CLASS:
997  name = freerdp_get_error_connect_name(type);
998  break;
999 
1000  default:
1001  name = rpc_error_to_string(code);
1002  break;
1003  }
1004 
1005  return name;
1006 }
1007 
1008 const char* freerdp_get_last_error_string(UINT32 code)
1009 {
1010  const char* string = NULL;
1011  const UINT32 cls = GET_FREERDP_ERROR_CLASS(code);
1012  const UINT32 type = GET_FREERDP_ERROR_TYPE(code);
1013 
1014  switch (cls)
1015  {
1016  case FREERDP_ERROR_ERRBASE_CLASS:
1017  string = freerdp_get_error_base_string(type);
1018  break;
1019 
1020  case FREERDP_ERROR_ERRINFO_CLASS:
1021  string = freerdp_get_error_info_string(type);
1022  break;
1023 
1024  case FREERDP_ERROR_CONNECT_CLASS:
1025  string = freerdp_get_error_connect_string(type);
1026  break;
1027 
1028  default:
1029  string = rpc_error_to_string(code);
1030  break;
1031  }
1032 
1033  return string;
1034 }
1035 
1036 const char* freerdp_get_last_error_category(UINT32 code)
1037 {
1038  const char* string = NULL;
1039  const UINT32 cls = GET_FREERDP_ERROR_CLASS(code);
1040  const UINT32 type = GET_FREERDP_ERROR_TYPE(code);
1041 
1042  switch (cls)
1043  {
1044  case FREERDP_ERROR_ERRBASE_CLASS:
1045  string = freerdp_get_error_base_category(type);
1046  break;
1047 
1048  case FREERDP_ERROR_ERRINFO_CLASS:
1049  string = freerdp_get_error_info_category(type);
1050  break;
1051 
1052  case FREERDP_ERROR_CONNECT_CLASS:
1053  string = freerdp_get_error_connect_category(type);
1054  break;
1055 
1056  default:
1057  string = rpc_error_to_category(code);
1058  break;
1059  }
1060 
1061  return string;
1062 }
1063 
1064 void freerdp_set_last_error_ex(rdpContext* context, UINT32 lastError, const char* fkt,
1065  const char* file, int line)
1066 {
1067  WINPR_ASSERT(context);
1068  WINPR_ASSERT(line >= 0);
1069 
1070  if (lastError)
1071  {
1072  if (WLog_IsLevelActive(context->log, WLOG_ERROR))
1073  {
1074  WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_ERROR, (size_t)line, file, fkt,
1075  "%s [0x%08" PRIX32 "]", freerdp_get_last_error_name(lastError),
1076  lastError);
1077  }
1078  }
1079 
1080  if (lastError == FREERDP_ERROR_SUCCESS)
1081  {
1082  if (WLog_IsLevelActive(context->log, WLOG_DEBUG))
1083  WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_DEBUG, (size_t)line, file, fkt,
1084  "resetting error state");
1085  }
1086  else if (context->LastError != FREERDP_ERROR_SUCCESS)
1087  {
1088  if (WLog_IsLevelActive(context->log, WLOG_ERROR))
1089  {
1090  WLog_PrintMessage(context->log, WLOG_MESSAGE_TEXT, WLOG_ERROR, (size_t)line, file, fkt,
1091  "TODO: Trying to set error code %s, but %s already set!",
1092  freerdp_get_last_error_name(lastError),
1093  freerdp_get_last_error_name(context->LastError));
1094  }
1095  }
1096  context->LastError = lastError;
1097 }
1098 
1099 const char* freerdp_get_logon_error_info_type_ex(UINT32 type, char* buffer, size_t size)
1100 {
1101  const char* str = freerdp_get_logon_error_info_type(type);
1102  (void)_snprintf(buffer, size, "%s(0x%04" PRIx32 ")", str, type);
1103  return buffer;
1104 }
1105 
1106 const char* freerdp_get_logon_error_info_type(UINT32 type)
1107 {
1108  switch (type)
1109  {
1110  case LOGON_MSG_DISCONNECT_REFUSED:
1111  return "LOGON_MSG_DISCONNECT_REFUSED";
1112 
1113  case LOGON_MSG_NO_PERMISSION:
1114  return "LOGON_MSG_NO_PERMISSION";
1115 
1116  case LOGON_MSG_BUMP_OPTIONS:
1117  return "LOGON_MSG_BUMP_OPTIONS";
1118 
1119  case LOGON_MSG_RECONNECT_OPTIONS:
1120  return "LOGON_MSG_RECONNECT_OPTIONS";
1121 
1122  case LOGON_MSG_SESSION_TERMINATE:
1123  return "LOGON_MSG_SESSION_TERMINATE";
1124 
1125  case LOGON_MSG_SESSION_CONTINUE:
1126  return "LOGON_MSG_SESSION_CONTINUE";
1127 
1128  default:
1129  return "UNKNOWN";
1130  }
1131 }
1132 
1133 const char* freerdp_get_logon_error_info_data(UINT32 data)
1134 {
1135  switch (data)
1136  {
1137  case LOGON_FAILED_BAD_PASSWORD:
1138  return "LOGON_FAILED_BAD_PASSWORD";
1139 
1140  case LOGON_FAILED_UPDATE_PASSWORD:
1141  return "LOGON_FAILED_UPDATE_PASSWORD";
1142 
1143  case LOGON_FAILED_OTHER:
1144  return "LOGON_FAILED_OTHER";
1145 
1146  case LOGON_WARNING:
1147  return "LOGON_WARNING";
1148 
1149  default:
1150  return "SESSION_ID";
1151  }
1152 }
1153 
1154 const char* freerdp_get_logon_error_info_data_ex(UINT32 data, char* buffer, size_t size)
1155 {
1156  const char* str = freerdp_get_logon_error_info_data(data);
1157  (void)_snprintf(buffer, size, "%s(0x%04" PRIx32 ")", str, data);
1158  return buffer;
1159 }
1160 
1164 freerdp* freerdp_new(void)
1165 {
1166  freerdp* instance = NULL;
1167  instance = (freerdp*)calloc(1, sizeof(freerdp));
1168 
1169  if (!instance)
1170  return NULL;
1171 
1172  instance->ContextSize = sizeof(rdpContext);
1173  instance->SendChannelData = freerdp_send_channel_data;
1174  instance->SendChannelPacket = freerdp_send_channel_packet;
1175  instance->ReceiveChannelData = freerdp_channels_data;
1176  return instance;
1177 }
1178 
1183 void freerdp_free(freerdp* instance)
1184 {
1185  free(instance);
1186 }
1187 
1188 ULONG freerdp_get_transport_sent(rdpContext* context, BOOL resetCount)
1189 {
1190  WINPR_ASSERT(context);
1191  WINPR_ASSERT(context->rdp);
1192  return transport_get_bytes_sent(context->rdp->transport, resetCount);
1193 }
1194 
1195 BOOL freerdp_nla_impersonate(rdpContext* context)
1196 {
1197  rdpNla* nla = NULL;
1198 
1199  if (!context)
1200  return FALSE;
1201 
1202  if (!context->rdp)
1203  return FALSE;
1204 
1205  if (!context->rdp->transport)
1206  return FALSE;
1207 
1208  nla = transport_get_nla(context->rdp->transport);
1209  return nla_impersonate(nla);
1210 }
1211 
1212 BOOL freerdp_nla_revert_to_self(rdpContext* context)
1213 {
1214  rdpNla* nla = NULL;
1215 
1216  if (!context)
1217  return FALSE;
1218 
1219  if (!context->rdp)
1220  return FALSE;
1221 
1222  if (!context->rdp->transport)
1223  return FALSE;
1224 
1225  nla = transport_get_nla(context->rdp->transport);
1226  return nla_revert_to_self(nla);
1227 }
1228 
1229 UINT32 freerdp_get_nla_sspi_error(rdpContext* context)
1230 {
1231  rdpNla* nla = NULL;
1232 
1233  WINPR_ASSERT(context);
1234  WINPR_ASSERT(context->rdp);
1235  WINPR_ASSERT(context->rdp->transport);
1236 
1237  nla = transport_get_nla(context->rdp->transport);
1238 
1239  return nla_get_sspi_error(nla);
1240 }
1241 
1242 BOOL freerdp_nla_encrypt(rdpContext* context, const SecBuffer* inBuffer, SecBuffer* outBuffer)
1243 {
1244  WINPR_ASSERT(context);
1245  WINPR_ASSERT(context->rdp);
1246 
1247  rdpNla* nla = context->rdp->nla;
1248  return nla_encrypt(nla, inBuffer, outBuffer);
1249 }
1250 
1251 BOOL freerdp_nla_decrypt(rdpContext* context, const SecBuffer* inBuffer, SecBuffer* outBuffer)
1252 {
1253  WINPR_ASSERT(context);
1254  WINPR_ASSERT(context->rdp);
1255 
1256  rdpNla* nla = context->rdp->nla;
1257  return nla_decrypt(nla, inBuffer, outBuffer);
1258 }
1259 
1260 SECURITY_STATUS freerdp_nla_QueryContextAttributes(rdpContext* context, DWORD ulAttr, PVOID pBuffer)
1261 {
1262  WINPR_ASSERT(context);
1263  WINPR_ASSERT(context->rdp);
1264 
1265  return nla_QueryContextAttributes(context->rdp->nla, ulAttr, pBuffer);
1266 }
1267 
1268 HANDLE getChannelErrorEventHandle(rdpContext* context)
1269 {
1270  WINPR_ASSERT(context);
1271  return context->channelErrorEvent;
1272 }
1273 
1274 BOOL checkChannelErrorEvent(rdpContext* context)
1275 {
1276  WINPR_ASSERT(context);
1277 
1278  if (WaitForSingleObject(context->channelErrorEvent, 0) == WAIT_OBJECT_0)
1279  {
1280  WLog_Print(context->log, WLOG_ERROR, "%s. Error was %" PRIu32 "", context->errorDescription,
1281  context->channelErrorNum);
1282  return FALSE;
1283  }
1284 
1285  return TRUE;
1286 }
1287 
1293 UINT getChannelError(rdpContext* context)
1294 {
1295  WINPR_ASSERT(context);
1296  return context->channelErrorNum;
1297 }
1298 
1299 const char* getChannelErrorDescription(rdpContext* context)
1300 {
1301  WINPR_ASSERT(context);
1302  return context->errorDescription;
1303 }
1304 
1305 void clearChannelError(rdpContext* context)
1306 {
1307  WINPR_ASSERT(context);
1308  context->channelErrorNum = 0;
1309  memset(context->errorDescription, 0, 500);
1310  (void)ResetEvent(context->channelErrorEvent);
1311 }
1312 
1313 WINPR_ATTR_FORMAT_ARG(3, 4)
1314 void setChannelError(rdpContext* context, UINT errorNum, WINPR_FORMAT_ARG const char* format, ...)
1315 {
1316  va_list ap = { 0 };
1317  va_start(ap, format);
1318 
1319  WINPR_ASSERT(context);
1320 
1321  context->channelErrorNum = errorNum;
1322  (void)vsnprintf(context->errorDescription, 499, format, ap);
1323  va_end(ap);
1324  (void)SetEvent(context->channelErrorEvent);
1325 }
1326 
1327 const char* freerdp_nego_get_routing_token(rdpContext* context, DWORD* length)
1328 {
1329  if (!context || !context->rdp)
1330  return NULL;
1331 
1332  return (const char*)nego_get_routing_token(context->rdp->nego, length);
1333 }
1334 
1335 BOOL freerdp_io_callback_set_event(rdpContext* context, BOOL set)
1336 {
1337  WINPR_ASSERT(context);
1338  return rdp_io_callback_set_event(context->rdp, set);
1339 }
1340 
1341 const rdpTransportIo* freerdp_get_io_callbacks(rdpContext* context)
1342 {
1343  WINPR_ASSERT(context);
1344  return rdp_get_io_callbacks(context->rdp);
1345 }
1346 
1347 BOOL freerdp_set_io_callbacks(rdpContext* context, const rdpTransportIo* io_callbacks)
1348 {
1349  WINPR_ASSERT(context);
1350  return rdp_set_io_callbacks(context->rdp, io_callbacks);
1351 }
1352 
1353 BOOL freerdp_set_io_callback_context(rdpContext* context, void* usercontext)
1354 {
1355  WINPR_ASSERT(context);
1356  return rdp_set_io_callback_context(context->rdp, usercontext);
1357 }
1358 
1359 void* freerdp_get_io_callback_context(rdpContext* context)
1360 {
1361  WINPR_ASSERT(context);
1362  return rdp_get_io_callback_context(context->rdp);
1363 }
1364 
1365 CONNECTION_STATE freerdp_get_state(const rdpContext* context)
1366 {
1367  WINPR_ASSERT(context);
1368  return rdp_get_state(context->rdp);
1369 }
1370 
1371 const char* freerdp_state_string(CONNECTION_STATE state)
1372 {
1373  return rdp_state_string(state);
1374 }
1375 
1376 BOOL freerdp_is_active_state(const rdpContext* context)
1377 {
1378  WINPR_ASSERT(context);
1379  return rdp_is_active_state(context->rdp);
1380 }
1381 
1382 BOOL freerdp_channels_from_mcs(rdpSettings* settings, const rdpContext* context)
1383 {
1384  WINPR_ASSERT(context);
1385  return rdp_channels_from_mcs(settings, context->rdp);
1386 }
1387 
1388 HANDLE freerdp_abort_event(rdpContext* context)
1389 {
1390  WINPR_ASSERT(context);
1391  return utils_get_abort_event(context->rdp);
1392 }
1393 
1394 static void test_mcs_free(rdpMcs* mcs)
1395 {
1396  if (!mcs)
1397  return;
1398 
1399  rdpTransport* transport = mcs->transport;
1400  rdpContext* context = transport_get_context(transport);
1401  if (context)
1402  {
1403  rdpSettings* settings = context->settings;
1404  freerdp_settings_free(settings);
1405  }
1406  free(context);
1407  transport_free(transport);
1408 
1409  mcs_free(mcs);
1410 }
1411 
1412 static rdpMcs* test_mcs_new(void)
1413 {
1414  rdpTransport* transport = NULL;
1415  rdpSettings* settings = freerdp_settings_new(0);
1416  rdpContext* context = calloc(1, sizeof(rdpContext));
1417 
1418  if (!settings)
1419  goto fail;
1420  if (!freerdp_settings_set_bool(settings, FreeRDP_TransportDumpReplay, TRUE))
1421  goto fail;
1422 
1423  if (!context)
1424  goto fail;
1425  context->settings = settings;
1426  transport = transport_new(context);
1427  if (!transport)
1428  goto fail;
1429  return mcs_new(transport);
1430 
1431 fail:
1432  transport_free(transport);
1433  free(context);
1434  freerdp_settings_free(settings);
1435 
1436  return NULL;
1437 }
1438 
1439 BOOL freerdp_is_valid_mcs_create_request(const BYTE* data, size_t size)
1440 {
1441 
1442  wStream sbuffer = { 0 };
1443  wStream* s = Stream_StaticConstInit(&sbuffer, data, size);
1444 
1445  WINPR_ASSERT(data || (size == 0));
1446  WINPR_ASSERT(s);
1447 
1448  rdpMcs* mcs = test_mcs_new();
1449  WINPR_ASSERT(mcs);
1450 
1451  BOOL result = mcs_recv_connect_initial(mcs, s);
1452  test_mcs_free(mcs);
1453  return result;
1454 }
1455 
1456 BOOL freerdp_is_valid_mcs_create_response(const BYTE* data, size_t size)
1457 {
1458 
1459  wStream sbuffer = { 0 };
1460  wStream* s = Stream_StaticConstInit(&sbuffer, data, size);
1461 
1462  WINPR_ASSERT(data || (size == 0));
1463  WINPR_ASSERT(s);
1464 
1465  rdpMcs* mcs = test_mcs_new();
1466  WINPR_ASSERT(mcs);
1467 
1468  BOOL result = mcs_recv_connect_response(mcs, s);
1469  test_mcs_free(mcs);
1470  return result;
1471 }
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.