Skip to content

Commit 45809a6

Browse files
committed
Merging agent end points to a single one
1 parent 87b27d8 commit 45809a6

File tree

5 files changed

+52
-60
lines changed

5 files changed

+52
-60
lines changed

auth2-pubkey.c

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extern u_int session_id2_len;
8686
#ifdef WIN32_FIXME
8787

8888
extern char HomeDirLsaW[MAX_PATH];
89+
extern int auth_sock;
8990

9091
#endif
9192

@@ -192,52 +193,24 @@ userauth_pubkey(Authctxt *authctxt)
192193
#ifdef WIN32_FIXME
193194
{
194195
#define SSH_AGENT_ROOT "SOFTWARE\\SSH\\Agent"
195-
HKEY agent_root = 0;
196-
DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff;
197-
int sock = -1, r;
196+
int r;
198197
u_char *blob = NULL;
199198
size_t blen = 0;
200199
DWORD token = 0;
201-
HANDLE h = INVALID_HANDLE_VALUE;
202200
struct sshbuf *msg = NULL;
203201

204202
while (1) {
205-
RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, KEY_QUERY_VALUE, &agent_root);
206-
if (agent_root)
207-
RegQueryValueEx(agent_root, "ProcessId", 0, NULL, &agent_pid, &tmp_size);
208-
209-
210-
h = CreateFile(
211-
"\\\\.\\pipe\\ssh-authagent", // pipe name
212-
GENERIC_READ | // read and write access
213-
GENERIC_WRITE,
214-
0, // no sharing
215-
NULL, // default security attributes
216-
OPEN_EXISTING, // opens existing pipe
217-
FILE_FLAG_OVERLAPPED, // attributes
218-
NULL); // no template file
219-
if (h == INVALID_HANDLE_VALUE) {
220-
debug("cannot connect to auth agent");
221-
break;
222-
}
223-
224-
if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) || (agent_pid != pipe_server_pid)) {
225-
debug("auth agent pid mismatch");
226-
break;
227-
}
228-
229-
if ((sock = w32_allocate_fd_for_handle(h, FALSE)) < 0)
230-
break;
231203
msg = sshbuf_new();
232204
if (!msg)
233205
break;
234-
if ((r = sshbuf_put_cstring(msg, "keyauthenticate")) != 0 ||
206+
if ((r = sshbuf_put_u8(msg, 100)) != 0 ||
207+
(r = sshbuf_put_cstring(msg, "pubkey")) != 0 ||
235208
(r = sshkey_to_blob(key, &blob, &blen)) != 0 ||
236209
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
237210
(r = sshbuf_put_cstring(msg, authctxt->pw->pw_name)) != 0 ||
238211
(r = sshbuf_put_string(msg, sig, slen)) != 0 ||
239212
(r = sshbuf_put_string(msg, buffer_ptr(&b), buffer_len(&b))) != 0 ||
240-
(r = ssh_request_reply(sock, msg, msg)) != 0 ||
213+
(r = ssh_request_reply(auth_sock, msg, msg)) != 0 ||
241214
(r = sshbuf_get_u32(msg, &token)) != 0) {
242215
debug("auth agent did not authorize client %s", authctxt->pw->pw_name);
243216
break;
@@ -246,12 +219,8 @@ userauth_pubkey(Authctxt *authctxt)
246219
break;
247220

248221
}
249-
if (agent_root)
250-
RegCloseKey(agent_root);
251222
if (blob)
252223
free(blob);
253-
if (sock != -1)
254-
close(sock);
255224
if (msg)
256225
sshbuf_free(msg);
257226

contrib/win32/win32compat/ssh-agent/agent-main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
#include "agent.h"
3333

3434

35-
int scm_start_servie(DWORD, LPWSTR*);
35+
int scm_start_service(DWORD, LPWSTR*);
3636

3737
SERVICE_TABLE_ENTRYW dispatch_table[] =
3838
{
39-
{ L"ssh-agent", (LPSERVICE_MAIN_FUNCTIONW)scm_start_servie },
39+
{ L"ssh-agent", (LPSERVICE_MAIN_FUNCTIONW)scm_start_service },
4040
{ NULL, NULL }
4141
};
4242
static SERVICE_STATUS_HANDLE service_status_handle;
@@ -141,7 +141,7 @@ int main(int argc, char **argv) {
141141
return 0;
142142
}
143143

144-
int scm_start_servie(DWORD num, LPWSTR* args) {
144+
int scm_start_service(DWORD num, LPWSTR* args) {
145145
service_status_handle = RegisterServiceCtrlHandlerW(L"ssh-agent", service_handler);
146146
ZeroMemory(&service_status, sizeof(service_status));
147147
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

contrib/win32/win32compat/ssh-agent/agentconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Buffer cfg;
5151
ServerOptions options;
5252
struct passwd *privsep_pw = NULL;
5353
static char *config_file_name = _PATH_SERVER_CONFIG_FILE;
54+
int auth_sock = -1;
5455

5556
int auth2_methods_valid(const char * c, int i) {
5657
return 1;

contrib/win32/win32compat/ssh-agent/authagent-request.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,26 @@ generate_user_token(wchar_t* user) {
167167
return token;
168168
}
169169

170-
#define AUTH_REQUEST "keyauthenticate"
170+
#define AUTH_REQUEST "pubkey"
171171
#define MAX_USER_NAME_LEN 256
172172

173-
int process_authagent_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
173+
int process_pubkeyauth_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
174174
int r = -1;
175-
char *opn, *key_blob, *user, *sig, *blob;
176-
size_t opn_len, key_blob_len, user_len, sig_len, blob_len;
175+
char *key_blob, *user, *sig, *blob;
176+
size_t key_blob_len, user_len, sig_len, blob_len;
177177
struct sshkey *key = NULL;
178178
HANDLE token = NULL, dup_token = NULL, client_proc = NULL;
179179
wchar_t wuser[MAX_USER_NAME_LEN];
180180
PWSTR wuser_home = NULL;
181181
ULONG client_pid;
182182

183183
user = NULL;
184-
if (sshbuf_get_string_direct(request, &opn, &opn_len) != 0 ||
185-
sshbuf_get_string_direct(request, &key_blob, &key_blob_len) != 0 ||
184+
if (sshbuf_get_string_direct(request, &key_blob, &key_blob_len) != 0 ||
186185
sshbuf_get_cstring(request, &user, &user_len) != 0 ||
187186
sshbuf_get_string_direct(request, &sig, &sig_len) != 0 ||
188187
sshbuf_get_string_direct(request, &blob, &blob_len) != 0 ||
189-
sshkey_from_blob(key_blob, key_blob_len, &key) != 0 ||
190-
opn_len != strlen(AUTH_REQUEST) ||
191-
memcmp(opn, AUTH_REQUEST, opn_len) != 0) {
192-
debug("auth agent invalid request");
188+
sshkey_from_blob(key_blob, key_blob_len, &key) != 0) {
189+
debug("invalid pubkey auth request");
193190
goto done;
194191
}
195192

@@ -231,4 +228,21 @@ int process_authagent_request(struct sshbuf* request, struct sshbuf* response, s
231228
if (client_proc)
232229
CloseHandle(client_proc);
233230
return r;
231+
}
232+
233+
int process_authagent_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
234+
char *opn;
235+
size_t opn_len;
236+
if (sshbuf_get_string_direct(request, &opn, &opn_len) != 0) {
237+
debug("invalid auth request");
238+
return -1;
239+
}
240+
241+
if (opn_len == strlen(AUTH_REQUEST) && memcmp(opn, AUTH_REQUEST, opn_len) == 0)
242+
return process_pubkeyauth_request(request, response, con);
243+
else {
244+
debug("unknown auth request: %s", opn);
245+
return -1;
246+
}
247+
234248
}

contrib/win32/win32compat/ssh-agent/connection.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void agent_connection_on_error(struct agent_connection* con, DWORD error) {
4646
void agent_connection_on_io(struct agent_connection* con, DWORD bytes, OVERLAPPED* ol) {
4747

4848
/* process error */
49-
debug("connection io %p #bytes:%d state:%d", con, bytes, con->state);
49+
debug3("connection io %p #bytes:%d state:%d", con, bytes, con->state);
5050
if ((bytes == 0) && (GetOverlappedResult(con->connection, ol, &bytes, FALSE) == FALSE))
5151
ABORT_CONNECTION_RETURN(con);
5252

@@ -155,7 +155,7 @@ get_con_client_type(HANDLE pipe) {
155155
else
156156
r = OTHER;
157157

158-
debug("client type: %d", r);
158+
debug2("client type: %d", r);
159159
done:
160160
if (sshd_sid)
161161
free(sshd_sid);
@@ -167,6 +167,7 @@ get_con_client_type(HANDLE pipe) {
167167
return r;
168168
}
169169

170+
#define SSH_AGENT_AUTHENTICATE 100
170171

171172
static int
172173
process_request(struct agent_connection* con) {
@@ -188,24 +189,31 @@ process_request(struct agent_connection* con) {
188189

189190
if (sshbuf_get_u8(request, &type) != 0)
190191
return -1;
191-
debug2("process key agent request type %d", type);
192+
debug("process agent request type %d", type);
192193

193194
switch (type) {
194195
case SSH2_AGENTC_ADD_IDENTITY:
195-
return process_add_identity(request, response, con);
196+
r = process_add_identity(request, response, con);
197+
break;
196198
case SSH2_AGENTC_REQUEST_IDENTITIES:
197-
return process_request_identities(request, response, con);
199+
r = process_request_identities(request, response, con);
200+
break;
198201
case SSH2_AGENTC_SIGN_REQUEST:
199-
return process_sign_request(request, response, con);
202+
r = process_sign_request(request, response, con);
203+
break;
200204
case SSH2_AGENTC_REMOVE_IDENTITY:
201-
return process_remove_key(request, response, con);
205+
r = process_remove_key(request, response, con);
206+
break;
202207
case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
203-
return process_remove_all(request, response, con);
204-
case 100:
205-
return process_authagent_request(request, response, con);
208+
r = process_remove_all(request, response, con);
209+
break;
210+
case SSH_AGENT_AUTHENTICATE:
211+
r = process_authagent_request(request, response, con);
212+
break;
206213
default:
207214
debug("unknown agent request %d", type);
208-
return -1;
215+
r = -1;
216+
break;
209217
}
210218
}
211219

0 commit comments

Comments
 (0)