Skip to content

Commit 87b27d8

Browse files
committed
agent updates
1 parent 367476c commit 87b27d8

File tree

8 files changed

+89
-87
lines changed

8 files changed

+89
-87
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ d2utmpa*
278278
configure
279279
contrib/win32/openssh/Win32-OpenSSH.VC.opendb
280280
*.opendb
281+
*.db

authfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ssh_get_authentication_socket(int *fdp)
107107
}
108108

109109
h = CreateFile(
110-
"\\\\.\\pipe\\ssh-keyagent", // pipe name
110+
"\\\\.\\pipe\\ssh-agent", // pipe name
111111
GENERIC_READ | // read and write access
112112
GENERIC_WRITE,
113113
0, // no sharing

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,20 @@ int main(int argc, char **argv) {
9898
if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
9999
/*todo - support debug mode*/
100100
/*
101-
if (debugmode) {
101+
if (debugMode) {
102102
SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
103103
log_init("ssh-agent", 7, 1, 1);
104-
agent_start(TRUE, FALSE, 0, 0);
104+
agent_start(TRUE, FALSE, 0);
105105
return 0;
106106
}
107107
*/
108-
if (argc == 3) {
108+
if (argc == 2) {
109109
/*agent process is likely a spawned child*/
110110
char* h = 0;
111111
h += atoi(*(argv + 1));
112112
if (h != 0) {
113113
log_init("ssh-agent", config_log_level(), 1, 0);
114-
agent_start(FALSE, TRUE, h, atoi(*(argv + 2)));
114+
agent_start(FALSE, TRUE, h);
115115
return 0;
116116
}
117117
}
@@ -148,7 +148,7 @@ int scm_start_servie(DWORD num, LPWSTR* args) {
148148
ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 300);
149149
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
150150
log_init("ssh-agent", config_log_level(), 1, 0);
151-
agent_start(FALSE, FALSE, 0, 0);
151+
agent_start(FALSE, FALSE, 0);
152152
return 0;
153153
}
154154

contrib/win32/win32compat/ssh-agent/agent-request.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ typedef unsigned __int64 u_int64_t;
1010
#include "digest.h"
1111

1212

13-
int process_keyagent_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);
14-
int process_pubkeyagent_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);
15-
int process_authagent_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);
13+
/* key management */
14+
int process_add_identity(struct sshbuf*, struct sshbuf*, struct agent_connection*);
15+
int process_request_identities(struct sshbuf*, struct sshbuf*, struct agent_connection*);
16+
int process_sign_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);
17+
int process_remove_key(struct sshbuf*, struct sshbuf*, struct agent_connection*);
18+
int process_remove_all(struct sshbuf*, struct sshbuf*, struct agent_connection*);
19+
int process_authagent_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);
20+
21+
/* auth */

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

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,24 @@
3535
static HANDLE ioc_port = NULL;
3636
static BOOL debug_mode = FALSE;
3737

38-
#define NUM_LISTENERS 3
39-
#define KEY_AGENT_PIPE_ID L"\\\\.\\pipe\\ssh-keyagent"
40-
#define PUBKEY_AGENT_PIPE_ID L"\\\\.\\pipe\\ssh-pubkeyagent"
41-
#define AUTH_AGENT_PIPE_ID L"\\\\.\\pipe\\ssh-authagent"
38+
#define AGENT_PIPE_ID L"\\\\.\\pipe\\ssh-agent"
4239

43-
static wchar_t *pipe_ids[NUM_LISTENERS] = { KEY_AGENT_PIPE_ID, PUBKEY_AGENT_PIPE_ID, AUTH_AGENT_PIPE_ID };
44-
static enum agent_type pipe_types[NUM_LISTENERS] = { KEY_AGENT, PUBKEY_AGENT, PUBKEY_AUTH_AGENT};
45-
static wchar_t *pipe_sddls[NUM_LISTENERS] = { L"D:P(A;; GA;;; AU)", L"D:P(A;; GA;;; AU)", L"D:P(A;; GA;;; AU)" };
46-
HANDLE event_stop_agent;
47-
48-
struct listener {
49-
OVERLAPPED ol;
50-
HANDLE pipe;
51-
wchar_t *pipe_id;
52-
enum agent_type type;
53-
SECURITY_ATTRIBUTES sa;
54-
} listeners[NUM_LISTENERS];
40+
static HANDLE event_stop_agent;
41+
static OVERLAPPED ol;
42+
static HANDLE pipe;
43+
static SECURITY_ATTRIBUTES sa;
5544

5645
static int
57-
init_listeners() {
58-
int i;
59-
memset(listeners, 0, sizeof(listeners));
60-
for (i = 0; i < NUM_LISTENERS; i++) {
61-
if ((listeners[i].ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
46+
init_listener() {
47+
{
48+
if ((ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
6249
debug("cannot create event ERROR:%d", GetLastError());
6350
return GetLastError();
6451
}
65-
listeners[i].pipe_id = pipe_ids[i];
66-
listeners[i].type = pipe_types[i];
67-
listeners[i].pipe = INVALID_HANDLE_VALUE;
68-
listeners[i].sa.bInheritHandle = FALSE;
69-
if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(pipe_sddls[i], SDDL_REVISION_1,
70-
&listeners[i].sa.lpSecurityDescriptor, &listeners[i].sa.nLength)) {
52+
pipe = INVALID_HANDLE_VALUE;
53+
sa.bInheritHandle = FALSE;
54+
if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(L"D:P(A;; GA;;; AU)", SDDL_REVISION_1,
55+
&sa.lpSecurityDescriptor, &sa.nLength)) {
7156
debug("cannot convert sddl ERROR:%d", GetLastError());
7257
return GetLastError();
7358
}
@@ -78,12 +63,11 @@ init_listeners() {
7863

7964
static void
8065
agent_cleanup() {
81-
int i;
82-
for (i = 0; i < NUM_LISTENERS; i++) {
83-
if (listeners[i].ol.hEvent != NULL)
84-
CloseHandle(listeners[i].ol.hEvent);
85-
if (listeners[i].pipe != INVALID_HANDLE_VALUE)
86-
CloseHandle(listeners[i].pipe);
66+
{
67+
if (ol.hEvent != NULL)
68+
CloseHandle(ol.hEvent);
69+
if (pipe != INVALID_HANDLE_VALUE)
70+
CloseHandle(pipe);
8771
}
8872
if (ioc_port)
8973
CloseHandle(ioc_port);
@@ -112,15 +96,14 @@ iocp_work(LPVOID lpParam) {
11296
}
11397

11498
static void
115-
process_connection(HANDLE pipe, int type) {
99+
process_connection(HANDLE pipe) {
116100
struct agent_connection* con;
117101

118102
if ((con = malloc(sizeof(struct agent_connection))) == NULL)
119103
fatal("failed to alloc");
120104

121105
memset(con, 0, sizeof(struct agent_connection));
122106
con->connection = pipe;
123-
con->type = type;
124107
if (CreateIoCompletionPort(pipe, ioc_port, (ULONG_PTR)con, 0) != ioc_port)
125108
fatal("failed to assign pipe to ioc_port");
126109

@@ -130,18 +113,18 @@ process_connection(HANDLE pipe, int type) {
130113

131114
static void
132115
agent_listen_loop() {
133-
DWORD i, r;
134-
HANDLE wait_events[NUM_LISTENERS + 1];
116+
DWORD r;
117+
HANDLE wait_events[2];
135118

136119
wait_events[0] = event_stop_agent;
137-
for (i = 0; i < NUM_LISTENERS; i++)
138-
wait_events[i + 1] = listeners[i].ol.hEvent;
120+
121+
wait_events[1] = ol.hEvent;
139122

140123
while (1) {
141-
for (i = 0; i < NUM_LISTENERS; i++) {
142-
if (listeners[i].pipe == INVALID_HANDLE_VALUE) {
143-
listeners[i].pipe = CreateNamedPipeW(
144-
listeners[i].pipe_id, // pipe name
124+
{
125+
{
126+
pipe = CreateNamedPipeW(
127+
AGENT_PIPE_ID, // pipe name
145128
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access
146129
PIPE_TYPE_BYTE | // message type pipe
147130
PIPE_READMODE_BYTE | // message-read mode
@@ -150,20 +133,20 @@ agent_listen_loop() {
150133
BUFSIZE, // output buffer size
151134
BUFSIZE, // input buffer size
152135
0, // client time-out
153-
&listeners[i].sa);
136+
&sa);
154137

155-
if (listeners[i].pipe == INVALID_HANDLE_VALUE) {
138+
if (pipe == INVALID_HANDLE_VALUE) {
156139
verbose("cannot create listener pipe ERROR:%d", GetLastError());
157140
SetEvent(event_stop_agent);
158141
}
159-
else if (ConnectNamedPipe(listeners[i].pipe, &listeners[i].ol) != FALSE) {
142+
else if (ConnectNamedPipe(pipe, &ol) != FALSE) {
160143
verbose("ConnectNamedPipe returned TRUE unexpectedly ");
161144
SetEvent(event_stop_agent);
162145
}
163146

164147
if (GetLastError() == ERROR_PIPE_CONNECTED) {
165-
debug("Client has already connection to %d", i);
166-
SetEvent(listeners[i].ol.hEvent);
148+
debug("Client has already connected");
149+
SetEvent(ol.hEvent);
167150
}
168151

169152
if (GetLastError() != ERROR_IO_PENDING) {
@@ -174,22 +157,22 @@ agent_listen_loop() {
174157
}
175158
}
176159

177-
r = WaitForMultipleObjects(NUM_LISTENERS + 1, wait_events, FALSE, INFINITE);
160+
r = WaitForMultipleObjects(2, wait_events, FALSE, INFINITE);
178161
if (r == WAIT_OBJECT_0) {
179162
//received signal to shutdown
180163
debug("shutting down");
181164
agent_cleanup();
182165
return;
183166
}
184-
else if ((r > WAIT_OBJECT_0) && (r <= (WAIT_OBJECT_0 + NUM_LISTENERS))) {
167+
else if ((r > WAIT_OBJECT_0) && (r <= (WAIT_OBJECT_0 + 1))) {
185168
/* process incoming connection */
186-
HANDLE con = listeners[r - 1].pipe;
169+
HANDLE con = pipe;
187170
DWORD client_pid = 0;
188-
listeners[r - 1].pipe = INVALID_HANDLE_VALUE;
171+
pipe = INVALID_HANDLE_VALUE;
189172
GetNamedPipeClientProcessId(con, &client_pid);
190-
verbose("client pid %d connected on %ls", client_pid, pipe_ids[r-1]);
173+
verbose("client pid %d connected", client_pid);
191174
if (debug_mode) {
192-
process_connection(con, listeners[r - 1].type);
175+
process_connection(con);
193176
agent_cleanup();
194177
return;
195178
}
@@ -203,14 +186,14 @@ agent_listen_loop() {
203186
memset(&si, 0, sizeof(STARTUPINFOW));
204187
GetModuleFileNameW(NULL, module_path, MAX_PATH);
205188
SetHandleInformation(con, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
206-
if ((swprintf_s(path, MAX_PATH, L"%s %d %d", module_path, con, listeners[r - 1].type) == -1 ) ||
189+
if ((swprintf_s(path, MAX_PATH, L"%s %d", module_path, con) == -1 ) ||
207190
(CreateProcessW(NULL, path, NULL, NULL, TRUE,
208191
DETACHED_PROCESS, NULL, NULL,
209192
&si, &pi) == FALSE)) {
210193
verbose("Failed to create child process %ls ERROR:%d", module_path, GetLastError());
211194
}
212195
else {
213-
debug("spawned child %d to process %d", pi.dwProcessId, i);
196+
debug("spawned child %d ", pi.dwProcessId);
214197
CloseHandle(pi.hProcess);
215198
CloseHandle(pi.hThread);
216199
}
@@ -242,7 +225,7 @@ void agent_shutdown() {
242225
#define REG_AGENT_SDDL L"D:P(A;; GR;;; AU)(A;; GA;;; SY)(A;; GA;;; BA)"
243226

244227
void
245-
agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe, enum agent_type type) {
228+
agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe) {
246229
int r;
247230
HKEY agent_root = NULL;
248231
DWORD process_id = GetCurrentProcessId();
@@ -266,12 +249,12 @@ agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe, enum agent_type type) {
266249
fatal("cannot publish agent master process id ERROR:%d", r);
267250
if ((event_stop_agent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
268251
fatal("cannot create global stop event ERROR:%d", GetLastError());
269-
if ((r = init_listeners()) != 0)
252+
if ((r = init_listener()) != 0)
270253
fatal("failed to create server pipes ERROR:%d", r);
271254
agent_listen_loop();
272255
}
273256
else { /* this is a child process that processes one connection */
274-
process_connection(pipe, type);
257+
process_connection(pipe);
275258
}
276259

277260
}

contrib/win32/win32compat/ssh-agent/agent.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99

1010
#define HEADER_SIZE 4
1111

12-
enum agent_type {
13-
KEY_AGENT,
14-
PUBKEY_AGENT,
15-
PUBKEY_AUTH_AGENT
16-
};
17-
1812
struct agent_connection {
1913
OVERLAPPED ol;
2014
HANDLE connection;
@@ -38,14 +32,13 @@ struct agent_connection {
3832
SSHD,
3933
NETWORK_SERVICE
4034
} client_type;
41-
enum agent_type type;
4235
};
4336

4437
void agent_connection_on_io(struct agent_connection*, DWORD, OVERLAPPED*);
4538
void agent_connection_on_error(struct agent_connection* , DWORD );
4639
void agent_connection_disconnect(struct agent_connection*);
4740

48-
void agent_start(BOOL, BOOL, HANDLE, enum agent_type);
41+
void agent_start(BOOL, BOOL, HANDLE);
4942
void agent_shutdown();
5043
void agent_cleanup_connection(struct agent_connection*);
5144

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,31 @@ process_request(struct agent_connection* con) {
183183
if ((request == NULL) || (response == NULL))
184184
goto done;
185185

186-
if (con->type == KEY_AGENT)
187-
r = process_keyagent_request(request, response, con);
188-
else if (con->type == PUBKEY_AGENT)
189-
r = process_pubkeyagent_request(request, response, con);
190-
else if (con->type == PUBKEY_AUTH_AGENT)
191-
r = process_authagent_request(request, response, con);
186+
{
187+
u_char type;
188+
189+
if (sshbuf_get_u8(request, &type) != 0)
190+
return -1;
191+
debug2("process key agent request type %d", type);
192+
193+
switch (type) {
194+
case SSH2_AGENTC_ADD_IDENTITY:
195+
return process_add_identity(request, response, con);
196+
case SSH2_AGENTC_REQUEST_IDENTITIES:
197+
return process_request_identities(request, response, con);
198+
case SSH2_AGENTC_SIGN_REQUEST:
199+
return process_sign_request(request, response, con);
200+
case SSH2_AGENTC_REMOVE_IDENTITY:
201+
return process_remove_key(request, response, con);
202+
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);
206+
default:
207+
debug("unknown agent request %d", type);
208+
return -1;
209+
}
210+
}
192211

193212
done:
194213
if (request)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ convert_blob(struct agent_connection* con, const char *blob, DWORD blen, char **
9898

9999
#define REG_KEY_SDDL L"D:P(A;; GA;;; SY)(A;; GA;;; BA)"
100100

101-
static int
101+
int
102102
process_add_identity(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
103103
struct sshkey* key = NULL;
104104
int r = 0, blob_len, eblob_len, request_invalid = 0, success = 0;
@@ -224,7 +224,7 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
224224
return success ? 0 : -1;
225225
}
226226

227-
static int
227+
int
228228
process_sign_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
229229
u_char *blob, *data, *signature = NULL;
230230
size_t blen, dlen, slen = 0;
@@ -270,7 +270,7 @@ process_sign_request(struct sshbuf* request, struct sshbuf* response, struct age
270270
return r;
271271
}
272272

273-
static int
273+
int
274274
process_remove_key(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
275275
HKEY user_root = 0, root = 0;
276276
char *blob, *thumbprint = NULL;
@@ -308,7 +308,7 @@ process_remove_key(struct sshbuf* request, struct sshbuf* response, struct agent
308308
free(thumbprint);
309309
return r;
310310
}
311-
static int
311+
int
312312
process_remove_all(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
313313
HKEY user_root = 0, root = 0;
314314
int r = 0;
@@ -332,7 +332,7 @@ process_remove_all(struct sshbuf* request, struct sshbuf* response, struct agent
332332
return r;
333333
}
334334

335-
static int
335+
int
336336
process_request_identities(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
337337
int count = 0, index = 0, success = 0, r = 0;
338338
HKEY root = NULL, sub = NULL, user_root = 0;

0 commit comments

Comments
 (0)