-
Notifications
You must be signed in to change notification settings - Fork 405
/
manage.c
513 lines (461 loc) · 14.8 KB
/
manage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2010 Heiko Hund <heikoh@users.sf.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <windows.h>
#include <winsock2.h>
#include <malloc.h>
#include "options.h"
#include "manage.h"
#include "main.h"
#include "misc.h"
extern options_t o;
static mgmt_msg_func rtmsg_handler[mgmt_rtmsg_type_max];
/*
* Number of seconds to try connecting to management interface
*/
static const time_t max_connect_time = 15;
/*
* Initialize the real-time notification handlers
*/
void
InitManagement(const mgmt_rtmsg_handler *handler)
{
int i;
for (i = 0; handler[i].handler; ++i)
{
rtmsg_handler[handler[i].type] = handler[i].handler;
}
}
/*
* Connect to the OpenVPN management interface and register
* asynchronous socket event notification for it
*/
BOOL
OpenManagement(connection_t *c)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return FALSE;
}
c->manage.connected = 0;
c->manage.sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (c->manage.sk == INVALID_SOCKET)
{
WSACleanup();
return FALSE;
}
if (WSAAsyncSelect(c->manage.sk, c->hwndStatus, WM_MANAGEMENT,
FD_CONNECT|FD_READ|FD_WRITE|FD_CLOSE) != 0)
{
return FALSE;
}
connect(c->manage.sk, (SOCKADDR *)&c->manage.skaddr, sizeof(c->manage.skaddr));
c->manage.timeout = time(NULL) + max_connect_time;
return TRUE;
}
/*
* Try to send a queued management command to OpenVPN
*/
static void
SendCommand(connection_t *c)
{
int res;
mgmt_cmd_t *cmd = c->manage.cmd_queue;
if (cmd == NULL || cmd->size == 0)
{
return;
}
res = send(c->manage.sk, cmd->command, cmd->size, 0);
if (res < 1)
{
return;
}
if (res != cmd->size)
{
memmove(cmd->command, cmd->command + res, cmd->size - res);
}
cmd->size -= res;
}
/*
* Send a command to the OpenVPN management interface
*/
BOOL
ManagementCommand(connection_t *c, char *command, mgmt_msg_func handler, mgmt_cmd_type type)
{
mgmt_cmd_t *cmd = calloc(1, sizeof(*cmd));
if (cmd == NULL)
{
return FALSE;
}
cmd->size = strlen(command) + 1;
cmd->command = malloc(cmd->size);
if (cmd->command == NULL)
{
free(cmd);
return FALSE;
}
memcpy(cmd->command, command, cmd->size);
*(cmd->command + cmd->size - 1) = '\n';
cmd->handler = handler;
cmd->type = type;
if (c->manage.cmd_queue)
{
cmd->next = c->manage.cmd_queue;
cmd->prev = c->manage.cmd_queue->prev;
cmd->next->prev = cmd->prev->next = cmd;
}
else
{
cmd->next = cmd->prev = cmd;
c->manage.cmd_queue = cmd;
}
if (c->manage.cmd_queue == cmd)
{
SendCommand(c);
}
return TRUE;
}
/*
* Remove a command from a connection's command queue
*/
static BOOL
UnqueueCommand(connection_t *c)
{
mgmt_cmd_t *cmd = c->manage.cmd_queue;
if (!cmd)
{
return FALSE;
}
/* Wipe command as it may contain passwords */
memset(cmd->command, 'x', cmd->size);
if (cmd->type == combined)
{
cmd->type = regular;
return TRUE;
}
if (cmd->next == cmd)
{
c->manage.cmd_queue = NULL;
}
else
{
cmd->prev->next = cmd->next;
cmd->next->prev = cmd->prev;
c->manage.cmd_queue = cmd->next;
SendCommand(c);
}
free(cmd->command);
free(cmd);
return TRUE;
}
/*
* Handle management socket events asynchronously
*/
void
OnManagement(SOCKET sk, LPARAM lParam)
{
int res;
char *data;
ULONG data_size, offset;
connection_t *c = GetConnByManagement(sk);
if (c == NULL)
{
return;
}
switch (WSAGETSELECTEVENT(lParam))
{
case FD_CONNECT:
if (WSAGETSELECTERROR(lParam))
{
/* keep trying for connections with persistent daemons */
if (c->flags & FLAG_DAEMON_PERSISTENT
|| time(NULL) < c->manage.timeout)
{
/* show a message on status window */
if (rtmsg_handler[log_] && (c->flags & FLAG_DAEMON_PERSISTENT))
{
char buf[256];
_snprintf_0(buf, "%lld,W,Waiting for the management interface to come up",
(long long)time(NULL))
rtmsg_handler[log_](c, buf);
}
connect(c->manage.sk, (SOCKADDR *)&c->manage.skaddr, sizeof(c->manage.skaddr));
}
else
{
/* Connection to MI timed out. */
CloseManagement(c);
if (c->state != disconnected)
{
rtmsg_handler[timeout_](c, "");
}
}
}
else
{
c->manage.connected = 1;
}
break;
case FD_READ:
if (ioctlsocket(c->manage.sk, FIONREAD, &data_size) != 0
|| data_size == 0)
{
return;
}
data = malloc(c->manage.saved_size + data_size);
if (data == NULL)
{
return;
}
res = recv(c->manage.sk, data + c->manage.saved_size, data_size, 0);
if (res != (int) data_size)
{
free(data);
return;
}
/* Copy previously saved management data */
if (c->manage.saved_size)
{
memcpy(data, c->manage.saved_data, c->manage.saved_size);
data_size += c->manage.saved_size;
free(c->manage.saved_data);
c->manage.saved_data = NULL;
c->manage.saved_size = 0;
}
offset = 0;
while (offset < data_size)
{
char *pos;
char *line = data + offset;
size_t line_size = data_size - offset;
BOOL passwd_request = false;
const char *passwd_prompt = "ENTER PASSWORD:";
if (line_size >= strlen(passwd_prompt)
&& memcmp(line, passwd_prompt, strlen(passwd_prompt)) == 0)
{
pos = memchr(line, ':', line_size);
passwd_request = true;
}
else
{
pos = memchr(line, '\n', line_size);
}
if (pos == NULL)
{
c->manage.saved_data = malloc(line_size);
if (c->manage.saved_data)
{
c->manage.saved_size = line_size;
memcpy(c->manage.saved_data, line, c->manage.saved_size);
}
break;
}
offset += (pos - line) + 1;
/* Reply to a management password request */
if (*c->manage.password && passwd_request)
{
ManagementCommand(c, c->manage.password, NULL, regular);
SecureZeroMemory(c->manage.password, sizeof(c->manage.password));
continue;
}
if (!*c->manage.password && passwd_request)
{
/* either we don't have a password or we used it and didn't match */
MsgToEventLog(EVENTLOG_WARNING_TYPE, L"%ls: management password mismatch",
c->config_name);
c->state = disconnecting;
CloseManagement(c);
rtmsg_handler[stop_](c, "");
continue;
}
/* Handle regular management interface output */
line[pos - line - 1] = '\0';
if (line[0] == '>')
{
/* Real time notifications */
pos = line + 1;
if (strncmp(pos, "LOG:", 4) == 0)
{
if (rtmsg_handler[log_])
{
rtmsg_handler[log_](c, pos + 4);
}
}
else if (strncmp(pos, "STATE:", 6) == 0)
{
if (rtmsg_handler[state_])
{
rtmsg_handler[state_](c, pos + 6);
}
}
else if (strncmp(pos, "HOLD:", 5) == 0)
{
if (rtmsg_handler[hold_])
{
rtmsg_handler[hold_](c, pos + 5);
}
}
else if (strncmp(pos, "PASSWORD:", 9) == 0)
{
if (rtmsg_handler[password_])
{
rtmsg_handler[password_](c, pos + 9);
}
}
else if (strncmp(pos, "PROXY:", 6) == 0)
{
if (rtmsg_handler[proxy_])
{
rtmsg_handler[proxy_](c, pos + 6);
}
}
else if (strncmp(pos, "INFO:", 5) == 0)
{
/* delay until management interface accepts input */
/* use real sleep here, since WM_MANAGEMENT might arrive before management is ready */
Sleep(100);
c->manage.connected = 2;
if (rtmsg_handler[ready_])
{
rtmsg_handler[ready_](c, pos + 5);
}
}
else if (strncmp(pos, "NEED-OK:", 8) == 0)
{
if (rtmsg_handler[needok_])
{
rtmsg_handler[needok_](c, pos + 8);
}
}
else if (strncmp(pos, "NEED-STR:", 9) == 0)
{
if (rtmsg_handler[needstr_])
{
rtmsg_handler[needstr_](c, pos + 9);
}
}
else if (strncmp(pos, "ECHO:", 5) == 0)
{
if (rtmsg_handler[echo_])
{
rtmsg_handler[echo_](c, pos + 5);
}
}
else if (strncmp(pos, "BYTECOUNT:", 10) == 0)
{
if (rtmsg_handler[bytecount_])
{
rtmsg_handler[bytecount_](c, pos + 10);
}
}
else if (strncmp(pos, "INFOMSG:", 8) == 0)
{
if (rtmsg_handler[infomsg_])
{
rtmsg_handler[infomsg_](c, pos + 8);
}
}
else if (strncmp(pos, "PKCS11ID", 8) == 0
&& c->manage.cmd_queue)
{
/* This is not a real-time message, but unfortunately implemented
* in the core as one. Work around by handling the response here.
*/
mgmt_cmd_t *cmd = c->manage.cmd_queue;
if (cmd->handler)
{
cmd->handler(c, line);
}
UnqueueCommand(c);
}
}
else if (c->manage.cmd_queue)
{
/* Response to commands */
mgmt_cmd_t *cmd = c->manage.cmd_queue;
if (strncmp(line, "SUCCESS:", 8) == 0)
{
if (cmd->handler)
{
cmd->handler(c, line + 9);
}
UnqueueCommand(c);
}
else if (strncmp(line, "ERROR:", 6) == 0)
{
/* Response sent to management is not processed. Log an error in status window */
char buf[256];
_snprintf_0(buf, "%lld,N,Previous command sent to management failed: %s",
(long long)time(NULL), line)
rtmsg_handler[log_](c, buf);
if (cmd->handler)
{
cmd->handler(c, NULL);
}
UnqueueCommand(c);
}
else if (strcmp(line, "END") == 0)
{
UnqueueCommand(c);
}
else if (cmd->handler)
{
cmd->handler(c, line);
}
}
}
free(data);
break;
case FD_WRITE:
SendCommand(c);
break;
case FD_CLOSE:
CloseManagement(c);
if (rtmsg_handler[stop_])
{
rtmsg_handler[stop_](c, "");
}
break;
}
}
void
CloseManagement(connection_t *c)
{
if (c->manage.sk != INVALID_SOCKET)
{
if (c->manage.saved_size)
{
free(c->manage.saved_data);
c->manage.saved_data = NULL;
c->manage.saved_size = 0;
}
closesocket(c->manage.sk);
c->manage.sk = INVALID_SOCKET;
c->manage.connected = 0;
while (UnqueueCommand(c))
{
}
WSACleanup();
}
}