FreeRDP
wf_defaults.c
1 
19 #include <windows.h>
20 #include <wincred.h>
21 #include <stdio.h>
22 #include <malloc.h>
23 #include <freerdp/settings.h>
24 
25 #include "wf_defaults.h"
26 
27 static PCWSTR ValidateString(const BYTE* pb, ULONG cb)
28 {
29  if (!pb || !cb)
30  return 0;
31 
32  if (cb % sizeof(WCHAR) != 0)
33  return 0;
34 
35  return (PCWSTR)pb;
36 }
37 
38 static void AddDefaultSettings_I(rdpSettings* settings, size_t idHostname, size_t idUsername,
39  size_t idDomain, size_t idPassword)
40 {
41  static const PSTR TERMSRV = "TERMSRV/%s";
42 
43  PSTR TargetName = NULL;
44  PSTR UserName = NULL;
45  PWSTR TargetNameW = NULL;
46  PWSTR ServerHostNameW = NULL;
47  PWSTR ParsedUserNameW = NULL;
48  PWSTR ParsedDomainW = NULL;
49  PWSTR PasswordNullTerminatedW = NULL;
50  PCREDENTIALW Credential = { 0 };
51 
52  PCSTR ServerHostname = freerdp_settings_get_string(settings, idHostname);
53 
54  if (!ServerHostname)
55  return;
56 
57  BOOL bExistUserName = freerdp_settings_get_string(settings, idUsername) != 0;
58  BOOL bExistPassword = freerdp_settings_get_string(settings, idPassword) != 0;
59 
60  if (bExistUserName && bExistPassword)
61  return;
62 
63  int len = _snprintf(TargetName, 0, TERMSRV, ServerHostname);
64  len++;
65  TargetName = (PSTR)malloc(len);
66 
67  if (!TargetName)
68  goto fail;
69 
70  _snprintf(TargetName, len, TERMSRV, ServerHostname);
71 
72  TargetName[len - 1] = 0;
73 
74  TargetNameW = ConvertUtf8ToWCharAlloc(TargetName, NULL);
75  if (!TargetNameW)
76  goto fail;
77 
78  if (!CredReadW(TargetNameW, CRED_TYPE_GENERIC, 0, &Credential))
79  goto fail;
80 
81  if (!bExistPassword)
82  {
83  const WCHAR* PasswordW =
84  ValidateString(Credential->CredentialBlob, Credential->CredentialBlobSize);
85 
86  PasswordNullTerminatedW = (PWSTR)calloc(Credential->CredentialBlobSize + 1, sizeof(WCHAR));
87 
88  if (!PasswordNullTerminatedW)
89  goto fail;
90 
91  memcpy(PasswordNullTerminatedW, PasswordW, Credential->CredentialBlobSize * sizeof(WCHAR));
92 
93  if (PasswordNullTerminatedW)
94  {
95  if (!freerdp_settings_set_string_from_utf16(settings, idPassword,
96  PasswordNullTerminatedW))
97  {
98  goto fail;
99  }
100  }
101  }
102 
103  if (!bExistUserName)
104  {
105  const WCHAR* UserNameW = Credential->UserName;
106 
107  if (UserNameW)
108  {
109  ParsedUserNameW = calloc(CREDUI_MAX_USERNAME_LENGTH + 1, sizeof(WCHAR));
110  if (!ParsedUserNameW)
111  goto fail;
112 
113  ParsedDomainW = calloc(CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1, sizeof(WCHAR));
114  if (!ParsedDomainW)
115  goto fail;
116 
117  DWORD ParseResult =
118  CredUIParseUserNameW(UserNameW, ParsedUserNameW, CREDUI_MAX_USERNAME_LENGTH + 1,
119  ParsedDomainW, CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1);
120 
121  if (ParseResult == NO_ERROR)
122  {
123  if (!freerdp_settings_set_string_from_utf16(settings, idUsername, ParsedUserNameW))
124  goto fail;
125 
126  if (*ParsedDomainW != 0)
127  {
128  if (!freerdp_settings_set_string_from_utf16(settings, idDomain, ParsedDomainW))
129  goto fail;
130  }
131  }
132  else if (ParseResult == ERROR_INVALID_ACCOUNT_NAME)
133  {
134  if (!freerdp_settings_set_string_from_utf16(settings, idUsername, UserNameW))
135  goto fail;
136  }
137  }
138  }
139 
140 fail:
141  if (Credential)
142  {
143  CredFree(Credential);
144  }
145  free(TargetName);
146  free(UserName);
147  free(TargetNameW);
148  free(ServerHostNameW);
149  free(ParsedUserNameW);
150  free(ParsedDomainW);
151  free(PasswordNullTerminatedW);
152  return;
153 }
154 
155 void WINAPI AddDefaultSettings(rdpSettings* settings)
156 {
157  AddDefaultSettings_I(settings, FreeRDP_ServerHostname, FreeRDP_Username, FreeRDP_Domain,
158  FreeRDP_Password);
159  AddDefaultSettings_I(settings, FreeRDP_GatewayHostname, FreeRDP_GatewayUsername,
160  FreeRDP_GatewayDomain, FreeRDP_GatewayPassword);
161 }
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_string_from_utf16(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const WCHAR *param)
Sets a string settings value. The param is converted to UTF-8 and the copy stored.