35
35
static HANDLE ioc_port = NULL ;
36
36
static BOOL debug_mode = FALSE;
37
37
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"
42
39
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 ;
55
44
56
45
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 ) {
62
49
debug ("cannot create event ERROR:%d" , GetLastError ());
63
50
return GetLastError ();
64
51
}
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 )) {
71
56
debug ("cannot convert sddl ERROR:%d" , GetLastError ());
72
57
return GetLastError ();
73
58
}
@@ -78,12 +63,11 @@ init_listeners() {
78
63
79
64
static void
80
65
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 );
87
71
}
88
72
if (ioc_port )
89
73
CloseHandle (ioc_port );
@@ -112,15 +96,14 @@ iocp_work(LPVOID lpParam) {
112
96
}
113
97
114
98
static void
115
- process_connection (HANDLE pipe , int type ) {
99
+ process_connection (HANDLE pipe ) {
116
100
struct agent_connection * con ;
117
101
118
102
if ((con = malloc (sizeof (struct agent_connection ))) == NULL )
119
103
fatal ("failed to alloc" );
120
104
121
105
memset (con , 0 , sizeof (struct agent_connection ));
122
106
con -> connection = pipe ;
123
- con -> type = type ;
124
107
if (CreateIoCompletionPort (pipe , ioc_port , (ULONG_PTR )con , 0 ) != ioc_port )
125
108
fatal ("failed to assign pipe to ioc_port" );
126
109
@@ -130,18 +113,18 @@ process_connection(HANDLE pipe, int type) {
130
113
131
114
static void
132
115
agent_listen_loop () {
133
- DWORD i , r ;
134
- HANDLE wait_events [NUM_LISTENERS + 1 ];
116
+ DWORD r ;
117
+ HANDLE wait_events [2 ];
135
118
136
119
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 ;
139
122
140
123
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
145
128
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED , // read/write access
146
129
PIPE_TYPE_BYTE | // message type pipe
147
130
PIPE_READMODE_BYTE | // message-read mode
@@ -150,20 +133,20 @@ agent_listen_loop() {
150
133
BUFSIZE , // output buffer size
151
134
BUFSIZE , // input buffer size
152
135
0 , // client time-out
153
- & listeners [ i ]. sa );
136
+ & sa );
154
137
155
- if (listeners [ i ]. pipe == INVALID_HANDLE_VALUE ) {
138
+ if (pipe == INVALID_HANDLE_VALUE ) {
156
139
verbose ("cannot create listener pipe ERROR:%d" , GetLastError ());
157
140
SetEvent (event_stop_agent );
158
141
}
159
- else if (ConnectNamedPipe (listeners [ i ]. pipe , & listeners [ i ]. ol ) != FALSE) {
142
+ else if (ConnectNamedPipe (pipe , & ol ) != FALSE) {
160
143
verbose ("ConnectNamedPipe returned TRUE unexpectedly " );
161
144
SetEvent (event_stop_agent );
162
145
}
163
146
164
147
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 );
167
150
}
168
151
169
152
if (GetLastError () != ERROR_IO_PENDING ) {
@@ -174,22 +157,22 @@ agent_listen_loop() {
174
157
}
175
158
}
176
159
177
- r = WaitForMultipleObjects (NUM_LISTENERS + 1 , wait_events , FALSE, INFINITE );
160
+ r = WaitForMultipleObjects (2 , wait_events , FALSE, INFINITE );
178
161
if (r == WAIT_OBJECT_0 ) {
179
162
//received signal to shutdown
180
163
debug ("shutting down" );
181
164
agent_cleanup ();
182
165
return ;
183
166
}
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 ))) {
185
168
/* process incoming connection */
186
- HANDLE con = listeners [ r - 1 ]. pipe ;
169
+ HANDLE con = pipe ;
187
170
DWORD client_pid = 0 ;
188
- listeners [ r - 1 ]. pipe = INVALID_HANDLE_VALUE ;
171
+ pipe = INVALID_HANDLE_VALUE ;
189
172
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 );
191
174
if (debug_mode ) {
192
- process_connection (con , listeners [ r - 1 ]. type );
175
+ process_connection (con );
193
176
agent_cleanup ();
194
177
return ;
195
178
}
@@ -203,14 +186,14 @@ agent_listen_loop() {
203
186
memset (& si , 0 , sizeof (STARTUPINFOW ));
204
187
GetModuleFileNameW (NULL , module_path , MAX_PATH );
205
188
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 ) ||
207
190
(CreateProcessW (NULL , path , NULL , NULL , TRUE,
208
191
DETACHED_PROCESS , NULL , NULL ,
209
192
& si , & pi ) == FALSE)) {
210
193
verbose ("Failed to create child process %ls ERROR:%d" , module_path , GetLastError ());
211
194
}
212
195
else {
213
- debug ("spawned child %d to process %d " , pi .dwProcessId , i );
196
+ debug ("spawned child %d " , pi .dwProcessId );
214
197
CloseHandle (pi .hProcess );
215
198
CloseHandle (pi .hThread );
216
199
}
@@ -242,7 +225,7 @@ void agent_shutdown() {
242
225
#define REG_AGENT_SDDL L"D:P(A;; GR;;; AU)(A;; GA;;; SY)(A;; GA;;; BA)"
243
226
244
227
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 ) {
246
229
int r ;
247
230
HKEY agent_root = NULL ;
248
231
DWORD process_id = GetCurrentProcessId ();
@@ -266,12 +249,12 @@ agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe, enum agent_type type) {
266
249
fatal ("cannot publish agent master process id ERROR:%d" , r );
267
250
if ((event_stop_agent = CreateEvent (NULL , TRUE, FALSE, NULL )) == NULL )
268
251
fatal ("cannot create global stop event ERROR:%d" , GetLastError ());
269
- if ((r = init_listeners ()) != 0 )
252
+ if ((r = init_listener ()) != 0 )
270
253
fatal ("failed to create server pipes ERROR:%d" , r );
271
254
agent_listen_loop ();
272
255
}
273
256
else { /* this is a child process that processes one connection */
274
- process_connection (pipe , type );
257
+ process_connection (pipe );
275
258
}
276
259
277
260
}
0 commit comments