@@ -79,8 +79,8 @@ EnablePrivilege(const char *privName, int enabled)
79
79
80
80
81
81
void
82
- LoadProfile (HANDLE token , char * user , char * domain ) {
83
- PROFILEINFOA profileInfo ;
82
+ LoadProfile (struct agent_connection * con , wchar_t * user , wchar_t * domain ) {
83
+ PROFILEINFOW profileInfo ;
84
84
profileInfo .dwFlags = PI_NOUI ;
85
85
profileInfo .lpProfilePath = NULL ;
86
86
profileInfo .lpUserName = user ;
@@ -91,11 +91,12 @@ LoadProfile(HANDLE token, char* user, char* domain) {
91
91
profileInfo .dwSize = sizeof (profileInfo );
92
92
EnablePrivilege ("SeBackupPrivilege" , 1 );
93
93
EnablePrivilege ("SeRestorePrivilege" , 1 );
94
- if (LoadUserProfileA (token , & profileInfo ) == FALSE)
95
- debug ("Loading user profile failed ERROR: %d" , GetLastError ());
94
+ if (LoadUserProfileW (con -> auth_token , & profileInfo ) == FALSE)
95
+ debug ("Loading user (%ls,%ls) profile failed ERROR: %d" , user , domain , GetLastError ());
96
+ else
97
+ con -> hProfile = profileInfo .hProfile ;
96
98
EnablePrivilege ("SeBackupPrivilege" , 0 );
97
99
EnablePrivilege ("SeRestorePrivilege" , 0 );
98
-
99
100
}
100
101
101
102
#define MAX_USER_LEN 256
@@ -200,7 +201,7 @@ generate_user_token(wchar_t* user) {
200
201
& token ,
201
202
& quotas ,
202
203
& subStatus ) != STATUS_SUCCESS ) {
203
- debug ("LsaRegisterLogonProcess failed" );
204
+ debug ("LsaLogonUser failed" );
204
205
goto done ;
205
206
}
206
207
@@ -218,10 +219,12 @@ generate_user_token(wchar_t* user) {
218
219
#define PUBKEY_AUTH_REQUEST "pubkey"
219
220
#define PASSWD_AUTH_REQUEST "password"
220
221
#define MAX_USER_NAME_LEN 256
222
+ #define MAX_PW_LEN 128
221
223
222
224
int process_passwordauth_request (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
223
- char * user = NULL , * pwd = NULL , * dom = NULL , * tmp ;
224
- //wchar_t *userW = NULL, *domW = NULL, *pwdW = NULL;
225
+ char * user = NULL , * pwd = NULL ;
226
+ wchar_t userW_buf [MAX_USER_NAME_LEN ], pwdW_buf [MAX_PW_LEN ];
227
+ wchar_t * userW = userW_buf , * domW = NULL , * pwdW = pwdW_buf , * tmp ;
225
228
size_t user_len = 0 , pwd_len = 0 , dom_len = 0 ;
226
229
int r = -1 ;
227
230
HANDLE token = 0 , dup_token , client_proc = 0 ;
@@ -235,20 +238,25 @@ int process_passwordauth_request(struct sshbuf* request, struct sshbuf* response
235
238
goto done ;
236
239
}
237
240
238
- /*TODO - support Unicode*/
241
+ userW [0 ] = L'\0' ;
242
+ if (MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , userW , MAX_USER_NAME_LEN ) == 0 ||
243
+ MultiByteToWideChar (CP_UTF8 , 0 , pwd , pwd_len + 1 , pwdW , MAX_PW_LEN ) == 0 ) {
244
+ debug ("unable to convert user (%s) or password to UTF-16" , user );
245
+ goto done ;
246
+ }
239
247
240
- if ((tmp = strchr ( user , '\\' )) != NULL ) {
241
- dom = user ;
242
- user = tmp + 1 ;
243
- * tmp = '\0' ;
248
+ if ((tmp = wcschr ( userW , L '\\' )) != NULL ) {
249
+ domW = userW ;
250
+ userW = tmp + 1 ;
251
+ * tmp = L '\0' ;
244
252
245
253
}
246
- else if ((tmp = strchr ( user , '@' )) != NULL ) {
247
- dom = tmp + 1 ;
248
- * tmp = '\0' ;
254
+ else if ((tmp = wcschr ( userW , L '@' )) != NULL ) {
255
+ domW = tmp + 1 ;
256
+ * tmp = L '\0' ;
249
257
}
250
258
251
- if (LogonUser ( user , dom , pwd , LOGON32_LOGON_NETWORK , LOGON32_PROVIDER_DEFAULT , & token ) == FALSE ||
259
+ if (LogonUserW ( userW , domW , pwdW , LOGON32_LOGON_NETWORK , LOGON32_PROVIDER_DEFAULT , & token ) == FALSE ||
252
260
(FALSE == GetNamedPipeClientProcessId (con -> connection , & client_pid )) ||
253
261
((client_proc = OpenProcess (PROCESS_DUP_HANDLE , FALSE, client_pid )) == NULL ) ||
254
262
(FALSE == DuplicateHandle (GetCurrentProcess (), token , client_proc , & dup_token , TOKEN_QUERY | TOKEN_IMPERSONATE , FALSE, DUPLICATE_SAME_ACCESS )) ||
@@ -257,7 +265,8 @@ int process_passwordauth_request(struct sshbuf* request, struct sshbuf* response
257
265
goto done ;
258
266
}
259
267
260
- LoadProfile (token , user , dom );
268
+ con -> auth_token = token ;
269
+ LoadProfile (con , userW , domW );
261
270
r = 0 ;
262
271
done :
263
272
/* TODO Fix this hacky protocol*/
@@ -268,8 +277,6 @@ int process_passwordauth_request(struct sshbuf* request, struct sshbuf* response
268
277
free (user );
269
278
if (pwd )
270
279
free (pwd );
271
- if (token )
272
- CloseHandle (token );
273
280
if (client_proc )
274
281
CloseHandle (client_proc );
275
282
@@ -296,12 +303,15 @@ int process_pubkeyauth_request(struct sshbuf* request, struct sshbuf* response,
296
303
goto done ;
297
304
}
298
305
306
+ wuser [0 ] = L'\0' ;
299
307
if (MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , wuser , MAX_USER_NAME_LEN ) == 0 ||
300
308
(token = generate_user_token (wuser )) == 0 ) {
301
309
debug ("unable to generate token for user %ls" , wuser );
302
310
goto done ;
303
311
}
304
312
313
+ con -> auth_token = token ;
314
+
305
315
if (SHGetKnownFolderPath (& FOLDERID_Profile , 0 , token , & wuser_home ) != S_OK ||
306
316
pubkey_allowed (key , wuser , wuser_home ) != 1 ) {
307
317
debug ("given public key is not mapped to user %ls (profile:%ls)" , wuser , wuser_home );
@@ -320,30 +330,33 @@ int process_pubkeyauth_request(struct sshbuf* request, struct sshbuf* response,
320
330
debug ("failed to authorize user" );
321
331
goto done ;
322
332
}
323
- {
324
- /*TODO - support Unicode*/
325
- char * tmp , * u = user , * d = NULL ;
326
- if ((tmp = strchr (user , '\\' )) != NULL ) {
327
- d = user ;
328
- u = tmp + 1 ;
329
- * tmp = '\0' ;
330
-
331
- }
332
- else if ((tmp = strchr (user , '@' )) != NULL ) {
333
- d = tmp + 1 ;
334
- * tmp = '\0' ;
335
- }
336
- LoadProfile (token , u , d );
333
+
334
+ {
335
+ wchar_t * tmp , * userW , * domW ;
336
+ userW = wuser ;
337
+ if ((tmp = wcschr (userW , L'\\' )) != NULL ) {
338
+ domW = userW ;
339
+ userW = tmp + 1 ;
340
+ * tmp = L'\0' ;
341
+
342
+ }
343
+ else if ((tmp = wcschr (userW , L'@' )) != NULL ) {
344
+ domW = tmp + 1 ;
345
+ * tmp = L'\0' ;
346
+ }
347
+ LoadProfile (con , userW , domW );
337
348
}
338
349
339
350
r = 0 ;
340
351
done :
352
+ /* TODO Fix this hacky protocol*/
353
+ if ((r == -1 ) && (sshbuf_put_u8 (response , SSH_AGENT_FAILURE ) == 0 ))
354
+ r = 0 ;
355
+
341
356
if (user )
342
357
free (user );
343
358
if (key )
344
359
sshkey_free (key );
345
- if (token )
346
- CloseHandle (token );
347
360
if (wuser_home )
348
361
CoTaskMemFree (wuser_home );
349
362
if (client_proc )
0 commit comments