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
|
/* $OpenBSD: ui.c,v 1.22 2001/10/05 08:18:37 ho Exp $ */
/* $EOM: ui.c,v 1.43 2000/10/05 09:25:12 niklas Exp $ */
/*
* Copyright (c) 1998, 1999, 2000 Niklas Hallqvist. All rights reserved.
* Copyright (c) 1999, 2000, 2001 Håkan Olsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Ericsson Radio Systems.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "sysdep.h"
#include "conf.h"
#include "connection.h"
#include "doi.h"
#include "exchange.h"
#include "isakmp.h"
#include "log.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
#include "ui.h"
#include "util.h"
#define BUF_SZ 256
/* from isakmpd.c */
void daemon_shutdown_now (int);
char *ui_fifo = FIFO;
int ui_socket;
/* Create and open the FIFO used for user control. */
void
ui_init (void)
{
struct stat st;
/* -f- means control messages comes in via stdin. */
if (strcmp (ui_fifo, "-") == 0)
ui_socket = 0;
else
{
/* Don't overwrite a file, i.e '-f /etc/isakmpd/isakmpd.conf'. */
if (lstat (ui_fifo, &st) == 0)
if ((st.st_mode & S_IFMT) == S_IFREG)
{
errno = EEXIST;
log_fatal ("ui_init: could not create FIFO \"%s\"", ui_fifo);
}
/* No need to know about errors. */
unlink (ui_fifo);
if (mkfifo (ui_fifo, 0600) == -1)
log_fatal ("ui_init: mkfifo (\"%s\", 0600) failed", ui_fifo);
/* XXX Is O_RDWR needed on some OSes? Photurisd seems to imply that. */
ui_socket = open (ui_fifo, O_RDONLY | O_NONBLOCK, 0);
if (ui_socket == -1)
log_fatal ("ui_init: open (\"%s\", O_RDONLY | O_NONBLOCK, 0) failed",
ui_fifo);
}
}
/*
* Setup a phase 2 connection.
* XXX Maybe phase 1 works too, but teardown won't work then, fix?
*/
static void
ui_connect (char *cmd)
{
char name[81];
if (sscanf (cmd, "c %80s", name) != 1)
{
log_print ("ui_connect: command \"%s\" malformed", cmd);
return;
}
log_print ("ui_connect: setup connection \"%s\"", name);
connection_setup (name);
}
/* Tear down a phase 2 connection. */
static void
ui_teardown (char *cmd)
{
char name[81];
struct sa *sa;
if (sscanf (cmd, "t %80s", name) != 1)
{
log_print ("ui_teardown: command \"%s\" malformed", cmd);
return;
}
log_print ("ui_teardown: teardown connection \"%s\"", name);
connection_teardown (name);
while ((sa = sa_lookup_by_name (name, 2)) != 0)
sa_delete (sa, 1);
}
/*
* Call the configuration API.
* XXX Error handling! How to do multi-line transactions? Too short arbitrary
* limit on the parameters?
*/
static void
ui_config (char *cmd)
{
char subcmd[81], section[81], tag[81], value[81];
int override, trans = 0;
if (sscanf (cmd, "C %80s", subcmd) != 1)
goto fail;
trans = conf_begin ();
if (strcasecmp (subcmd, "set") == 0)
{
if (sscanf (cmd, "C %*s [%80[^]]]:%80[^=]=%80s %d", section, tag, value,
&override) != 4)
goto fail;
conf_set (trans, section, tag, value, override, 0);
}
else if (strcasecmp (subcmd, "rm") == 0)
{
if (sscanf (cmd, "C %*s [%80[^]]]:%80s", section, tag) != 2)
goto fail;
conf_remove (trans, section, tag);
}
else if (strcasecmp (subcmd, "rms") == 0)
{
if (sscanf (cmd, "C %*s [%80[^]]]", section) != 1)
goto fail;
conf_remove_section (trans, section);
}
else
goto fail;
log_print ("ui_config: \"%s\"", cmd);
conf_end (trans, 1);
return;
fail:
if (trans)
conf_end (trans, 0);
log_print ("ui_config: command \"%s\" malformed", cmd);
}
static void
ui_delete (char *cmd)
{
char cookies_str[ISAKMP_HDR_COOKIES_LEN * 2 + 1];
char message_id_str[ISAKMP_HDR_MESSAGE_ID_LEN * 2 + 1];
u_int8_t cookies[ISAKMP_HDR_COOKIES_LEN];
u_int8_t message_id_buf[ISAKMP_HDR_MESSAGE_ID_LEN];
u_int8_t *message_id = message_id_buf;
struct sa *sa;
if (sscanf (cmd, "d %32s %8s", cookies_str, message_id_str) != 2)
{
log_print ("ui_delete: command \"%s\" malformed", cmd);
return;
}
if (strcmp (message_id_str, "-") == 0)
message_id = 0;
if (hex2raw (cookies_str, cookies, ISAKMP_HDR_COOKIES_LEN) == -1
|| (message_id && hex2raw (message_id_str, message_id_buf,
ISAKMP_HDR_MESSAGE_ID_LEN) == -1))
{
log_print ("ui_delete: command \"%s\" has bad arguments", cmd);
return;
}
sa = sa_lookup (cookies, message_id);
if (!sa)
{
log_print ("ui_delete: command \"%s\" found no SA", cmd);
return;
}
log_print ("ui_delete: deleting SA for cookie \"%s\" msgid \"%s\"",
cookies_str, message_id_str);
sa_delete (sa, 1);
}
#ifdef USE_DEBUG
/* Parse the debug command found in CMD. */
static void
ui_debug (char *cmd)
{
int cls, level;
char subcmd[3];
if (sscanf (cmd, "D %d %d", &cls, &level) == 2)
{
log_debug_cmd (cls, level);
return;
}
else if (sscanf (cmd, "D %2s %d", subcmd, &level) == 2)
{
switch (subcmd[0])
{
case 'A':
for (cls = 0; cls < LOG_ENDCLASS; cls++)
log_debug_cmd (cls, level);
return;
}
}
else if (sscanf (cmd, "D %2s", subcmd) == 1)
{
switch (subcmd[0])
{
case 'T':
log_debug_toggle ();
return;
}
}
log_print ("ui_debug: command \"%s\" malformed", cmd);
return;
}
static void
ui_packetlog (char *cmd)
{
char subcmd[81];
if (sscanf (cmd, "p %80s", subcmd) != 1)
goto fail;
if (strncasecmp (subcmd, "on=", 3) == 0)
{
/* Start capture to a new file. */
if (subcmd[strlen (subcmd) - 1] == '\n')
subcmd[strlen (subcmd) - 1] = 0;
log_packet_restart (subcmd + 3);
}
else if (strcasecmp (subcmd, "on") == 0)
log_packet_restart (NULL);
else if (strcasecmp (subcmd, "off") == 0)
log_packet_stop ();
return;
fail:
log_print ("ui_packetlog: command \"%s\" malformed", cmd);
}
#endif /* USE_DEBUG */
static void
ui_shutdown_daemon (char *cmd)
{
if (strlen (cmd) == 1)
{
log_print ("ui_shutdown_daemon: received shutdown command");
daemon_shutdown_now (0);
}
else
log_print ("ui_shutdown_daemon: command \"%s\" malformed", cmd);
}
/* Report SAs and ongoing exchanges. */
void
ui_report (char *cmd)
{
/* XXX Skip 'cmd' as arg? */
sa_report ();
exchange_report ();
transport_report ();
connection_report ();
timer_report ();
conf_report ();
}
/*
* Call the relevant command handler based on the first character of the
* line (the command).
*/
static void
ui_handle_command (char *line)
{
/* Find out what one-letter command was sent. */
switch (line[0])
{
case 'c':
ui_connect (line);
break;
case 'C':
ui_config (line);
break;
case 'd':
ui_delete (line);
break;
#ifdef USE_DEBUG
case 'D':
ui_debug (line);
break;
#endif
#ifdef USE_DEBUG
case 'p':
ui_packetlog (line);
break;
#endif
case 'Q':
ui_shutdown_daemon (line);
break;
case 'r':
ui_report (line);
break;
case 't':
ui_teardown (line);
break;
default:
log_print ("ui_handle_messages: unrecognized command: '%c'", line[0]);
}
}
/*
* A half-complex implementation of reading from a file descriptor
* line by line without resorting to stdio which apparently have
* troubles with non-blocking fifos.
*/
void
ui_handler (void)
{
static char *buf = 0;
static char *p;
static size_t sz;
static size_t resid;
size_t n;
char *new_buf;
/* If no buffer, set it up. */
if (!buf)
{
sz = BUF_SZ;
buf = malloc (sz);
if (!buf)
{
log_print ("ui_handler: malloc (%d) failed", sz);
return;
}
p = buf;
resid = sz;
}
/* If no place left in the buffer reallocate twice as large. */
if (!resid)
{
new_buf = realloc (buf, sz * 2);
if (!new_buf)
{
log_print ("ui_handler: realloc (%p, %d) failed", buf, sz * 2);
free (buf);
buf = 0;
return;
}
buf = new_buf;
p = buf + sz;
resid = sz;
sz *= 2;
}
n = read (ui_socket, p, resid);
if (n == -1)
{
log_error ("ui_handler: read (%d, %p, %d)", ui_socket, p, resid);
return;
}
if (!n)
return;
resid -= n;
while (n--)
{
/*
* When we find a newline, cut off the line and feed it to the
* command processor. Then move the rest up-front.
*/
if (*p == '\n')
{
*p = '\0';
ui_handle_command (buf);
memcpy (buf, p + 1, n);
p = buf;
resid = sz - n;
continue;
}
p++;
}
}
|