@@ -207,93 +207,74 @@ userauth_pubkey(Authctxt *authctxt)
207207 * On pure win32 try to logon using lsa first.
208208 */
209209
210- #ifdef WIN32_FIXME
210+ #ifdef WIN32_FIXME
211+ {
212+ #define SSH_AGENT_ROOT "SOFTWARE\\SSH\\Agent"
213+ HKEY agent_root = 0 ;
214+ DWORD agent_pid = 0 , tmp_size = 4 , pipe_server_pid = 0xff ;
215+ int sock = -1 , r ;
216+ u_char * blob = NULL ;
217+ size_t blen = 0 ;
218+ DWORD token = 0 ;
219+ HANDLE h = INVALID_HANDLE_VALUE ;
220+ struct sshbuf * msg = NULL ;
221+
222+ while (1 ) {
223+ RegOpenKeyEx (HKEY_LOCAL_MACHINE , SSH_AGENT_ROOT , 0 , KEY_QUERY_VALUE , & agent_root );
224+ if (agent_root )
225+ RegQueryValueEx (agent_root , "ProcessId" , 0 , NULL , & agent_pid , & tmp_size );
226+
227+
228+ h = CreateFile (
229+ "\\\\.\\pipe\\ssh-authagent" , // pipe name
230+ GENERIC_READ | // read and write access
231+ GENERIC_WRITE ,
232+ 0 , // no sharing
233+ NULL , // default security attributes
234+ OPEN_EXISTING , // opens existing pipe
235+ FILE_FLAG_OVERLAPPED , // attributes
236+ NULL ); // no template file
237+ if (h == INVALID_HANDLE_VALUE )
238+ break ;
211239
212- authctxt -> methoddata = NULL ;
213-
214- /*
215- * Retrieve name of current login user (i.e. sshd process owner).
216- */
217-
218- GetUserName (currentUser , & currentUserSize );
240+ if (!GetNamedPipeServerProcessId (h , & pipe_server_pid ) || (agent_pid != pipe_server_pid ))
241+ break ;
219242
220- /*
221- * Try to get token from lsa, but only if targetUser != currentUser.
222- * Owerthise we already have targetUser's token in current thread, so
223- * we only need key verify from original OpenSSH code.
224- */
243+ sock = w32_allocate_fd_for_handle (h , FALSE);
244+ msg = sshbuf_new ();
245+ if (!msg )
246+ break ;
247+ if ((r = sshbuf_put_cstring (msg , "keyauthenticate" )) != 0 ||
248+ (r = sshkey_to_blob (key , & blob , & blen )) != 0 ||
249+ (r = sshbuf_put_string (msg , blob , blen )) != 0 ||
250+ (r = sshbuf_put_cstring (msg , authctxt -> pw -> pw_name )) != 0 ||
251+ (r = sshbuf_put_string (msg , sig , slen )) != 0 ||
252+ (r = sshbuf_put_string (msg , buffer_ptr (& b ), buffer_len (& b ))) != 0 ||
253+ (r = ssh_request_reply (sock , msg , msg )) != 0 ||
254+ (r = sshbuf_get_u32 (msg , & token )) != 0 )
255+ break ;
256+
257+ break ;
258+
259+ }
260+ if (agent_root )
261+ RegCloseKey (agent_root );
262+ if (blob )
263+ free (blob );
264+ if (sock != -1 )
265+ close (sock );
266+ if (msg )
267+ sshbuf_free (msg );
268+
269+ if (token ) {
270+ authenticated = 1 ;
271+ authctxt -> methoddata = token ;
272+ }
273+
274+ }
225275
226- targetIsCurrent = (strcmp (currentUser , authctxt -> user ) == 0 );
227-
228- if (targetIsCurrent )
229- {
230- doOpenSSHVerify = 1 ;
231- }
232- else
233- {
234- loginStat = LsaLogon (& authctxt -> methoddata , HomeDirLsaW ,
235- authctxt -> user , pkblob , blen , sig , slen ,
236- buffer_ptr (& b ), buffer_len (& b ), datafellows );
237-
238- /*
239- * If lsa logon process success.
240- */
241-
242- if (loginStat == 0 )
243- {
244- /*
245- * And user authorized OK.
246- */
247-
248- if (authctxt -> methoddata )
249- {
250- doOpenSSHVerify = 0 ;
251-
252- /*
253- * This is part of openssh authorization needed for parsing
254- * 'options' block in key.
255- */
256-
257- authctxt -> pw -> pw_dir = GetHomeDir (authctxt -> user );
258-
259- if (PRIVSEP (user_key_allowed (authctxt -> pw , key , 1 ))) // PRAGMA:TODO
260- {
261- authenticated = 1 ;
262- }
263- else
264- {
265- authenticated = 0 ;
266- }
267-
268- buffer_free (& b );
269-
270- free (sig );
271- }
272- }
273- }
274-
275- if (doOpenSSHVerify )
276- {
277- /*
278- * If lsa fails, test for correct signature using openssh code.
279- */
280-
281- authctxt -> pw -> pw_dir = GetHomeDir (authctxt -> user );
282-
283- if (PRIVSEP (user_key_allowed (authctxt -> pw , key , 0 )) //PRAGMA:TODO
284- &&
285- PRIVSEP (key_verify (key , sig , slen , buffer_ptr (& b ), buffer_len (& b ))) == 1 )
286-
287- {
288- authenticated = 1 ;
289- }
290- }
291-
292- /*
293- * Original code.
294- */
295276
296- #else /* #ifdef WIN32_FIXME */
277+ #else /* #ifdef WIN32_FIXME */
297278
298279 if (PRIVSEP (user_key_allowed (authctxt -> pw , key , 1 )) &&
299280 PRIVSEP (key_verify (key , sig , slen , buffer_ptr (& b ),
0 commit comments