@@ -138,10 +138,10 @@ generate_user_token(wchar_t* user) {
138
138
}
139
139
140
140
#define AUTH_REQUEST "keyauthenticate"
141
- #define MAX_USER_NAME_LEN 255 + 255
141
+ #define MAX_USER_NAME_LEN 256
142
142
143
143
int process_authagent_request (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
144
- int r = 0 ;
144
+ int r = -1 ;
145
145
char * opn , * key_blob , * user , * sig , * blob ;
146
146
size_t opn_len , key_blob_len , user_len , sig_len , blob_len ;
147
147
struct sshkey * key = NULL ;
@@ -151,36 +151,44 @@ int process_authagent_request(struct sshbuf* request, struct sshbuf* response, s
151
151
ULONG client_pid ;
152
152
153
153
user = NULL ;
154
- if ((r = sshbuf_get_string_direct (request , & opn , & opn_len )) != 0 ||
155
- (r = sshbuf_get_string_direct (request , & key_blob , & key_blob_len )) != 0 ||
156
- (r = sshbuf_get_cstring (request , & user , & user_len )) != 0 ||
157
- (r = sshbuf_get_string_direct (request , & sig , & sig_len )) != 0 ||
158
- (r = sshbuf_get_string_direct (request , & blob , & blob_len )) != 0 ||
159
- (r = sshkey_from_blob (key_blob , key_blob_len , & key )) != 0 )
154
+ if (sshbuf_get_string_direct (request , & opn , & opn_len ) != 0 ||
155
+ sshbuf_get_string_direct (request , & key_blob , & key_blob_len ) != 0 ||
156
+ sshbuf_get_cstring (request , & user , & user_len ) != 0 ||
157
+ sshbuf_get_string_direct (request , & sig , & sig_len ) != 0 ||
158
+ sshbuf_get_string_direct (request , & blob , & blob_len ) != 0 ||
159
+ sshkey_from_blob (key_blob , key_blob_len , & key ) != 0 ||
160
+ opn_len != strlen (AUTH_REQUEST ) ||
161
+ memcmp (opn , AUTH_REQUEST , opn_len ) != 0 ) {
162
+ debug ("auth agent invalid request" );
160
163
goto done ;
164
+ }
161
165
162
- if ((opn_len != strlen (AUTH_REQUEST )) || (memcmp (opn , AUTH_REQUEST , opn_len ) != 0 )) {
163
- r = EINVAL ;
166
+ if (MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , wuser , MAX_USER_NAME_LEN ) == 0 ||
167
+ (token = generate_user_token (wuser )) == 0 ) {
168
+ debug ("unable to generate user token" );
164
169
goto done ;
165
170
}
166
171
167
- if (0 == MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , wuser , MAX_USER_NAME_LEN )) {
168
- r = GetLastError ();
172
+ if (SHGetKnownFolderPath (& FOLDERID_Profile , 0 , token , & wuser_home ) != S_OK ||
173
+ pubkey_allowed (key , wuser , wuser_home ) != 1 ) {
174
+ debug ("given public key is not mapped to user %ls" , wuser );
169
175
goto done ;
170
176
}
171
177
172
- if (key_verify (key , sig , sig_len , blob , blob_len ) != 1 ||
173
- (token = generate_user_token (wuser )) == 0 ||
174
- SHGetKnownFolderPath (& FOLDERID_Profile , 0 , token , & wuser_home ) != S_OK ||
175
- pubkey_allowed (key , wuser , wuser_home ) != 1 ||
176
- (FALSE == GetNamedPipeClientProcessId (con -> connection , & client_pid )) ||
178
+ if (key_verify (key , sig , sig_len , blob , blob_len ) != 1 ) {
179
+ debug ("signature verification failed" );
180
+ goto done ;
181
+ }
182
+
183
+ if ((FALSE == GetNamedPipeClientProcessId (con -> connection , & client_pid )) ||
177
184
( (client_proc = OpenProcess (PROCESS_DUP_HANDLE , FALSE, client_pid )) == NULL ) ||
178
185
(FALSE == DuplicateHandle (GetCurrentProcess (), token , client_proc , & dup_token , TOKEN_QUERY | TOKEN_IMPERSONATE , FALSE, DUPLICATE_SAME_ACCESS )) ||
179
186
(sshbuf_put_u32 (response , dup_token ) != 0 ) ) {
180
- r = EINVAL ;
187
+ debug ( "failed to authorize user" ) ;
181
188
goto done ;
182
189
}
183
190
191
+ r = 0 ;
184
192
done :
185
193
if (user )
186
194
free (user );
0 commit comments