summaryrefslogtreecommitdiff
path: root/src/stroke/stroke.c
blob: 03890b5173a6641a2017ac574967a3c63ca3c802 (plain)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* Stroke for charon is the counterpart to whack from pluto
 * Copyright (C) 2007-2012 Tobias Brunner
 * Copyright (C) 2006 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>

#include <library.h>

#include "stroke_msg.h"
#include "stroke_keywords.h"

struct stroke_token {
    char *name;
    stroke_keyword_t kw;
};

static char* push_string(stroke_msg_t *msg, char *string)
{
	unsigned long string_start = msg->length;

	if (string == NULL ||  msg->length + strlen(string) >= sizeof(stroke_msg_t))
	{
		return NULL;
	}
	else
	{
		msg->length += strlen(string) + 1;
		strcpy((char*)msg + string_start, string);
		return (char*)string_start;
	}
}

static int send_stroke_msg (stroke_msg_t *msg)
{
	struct sockaddr_un ctl_addr;
	int sock, byte_count;
	char buffer[512], *pass;

	ctl_addr.sun_family = AF_UNIX;
	strcpy(ctl_addr.sun_path, STROKE_SOCKET);

	msg->output_verbosity = 1; /* CONTROL */

	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0)
	{
		fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
		return -1;
	}
	if (connect(sock, (struct sockaddr *)&ctl_addr,
				offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
	{
		fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
		close(sock);
		return -1;
	}

	/* send message */
	if (write(sock, msg, msg->length) != msg->length)
	{
		fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
		close(sock);
		return -1;
	}

	while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
	{
		buffer[byte_count] = '\0';

		/* we prompt if we receive a magic keyword */
		if ((byte_count >= 12 &&
			 strcmp(buffer + byte_count - 12, "Passphrase:\n") == 0) ||
			(byte_count >= 10 &&
			 strcmp(buffer + byte_count - 10, "Password:\n") == 0) ||
			(byte_count >= 5 &&
			 strcmp(buffer + byte_count - 5, "PIN:\n") == 0))
		{
			/* remove trailing newline */
			pass = strrchr(buffer, '\n');
			if (pass)
			{
				*pass = ' ';
			}
#ifdef HAVE_GETPASS
			pass = getpass(buffer);
#else
			pass = "";
#endif
			if (pass)
			{
				ignore_result(write(sock, pass, strlen(pass)));
				ignore_result(write(sock, "\n", 1));
			}
		}
		else
		{
			printf("%s", buffer);
		}
	}
	if (byte_count < 0)
	{
		fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
	}

	close(sock);
	return 0;
}

static int add_connection(char *name,
						  char *my_id, char *other_id,
						  char *my_addr, char *other_addr,
						  char *my_nets, char *other_nets)
{
	stroke_msg_t msg;

	memset(&msg, 0, sizeof(msg));
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.type = STR_ADD_CONN;

	msg.add_conn.name = push_string(&msg, name);
	msg.add_conn.version = 2;
	msg.add_conn.mode = 1;
	msg.add_conn.mobike = 1;
	msg.add_conn.dpd.action = 1;

	msg.add_conn.me.id = push_string(&msg, my_id);
	msg.add_conn.me.address = push_string(&msg, my_addr);
	msg.add_conn.me.ikeport = 500;
	msg.add_conn.me.subnets = push_string(&msg, my_nets);
	msg.add_conn.me.sendcert = 1;

	msg.add_conn.other.id = push_string(&msg, other_id);
	msg.add_conn.other.address = push_string(&msg, other_addr);
	msg.add_conn.other.ikeport = 500;
	msg.add_conn.other.subnets = push_string(&msg, other_nets);
	msg.add_conn.other.sendcert = 1;

	return send_stroke_msg(&msg);
}

static int del_connection(char *name)
{
	stroke_msg_t msg;

	msg.length = offsetof(stroke_msg_t, buffer);
	msg.type = STR_DEL_CONN;
	msg.initiate.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int initiate_connection(char *name)
{
	stroke_msg_t msg;

	msg.length = offsetof(stroke_msg_t, buffer);
	msg.type = STR_INITIATE;
	msg.initiate.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int terminate_connection(char *name)
{
	stroke_msg_t msg;

	msg.type = STR_TERMINATE;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.initiate.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int terminate_connection_srcip(char *start, char *end)
{
	stroke_msg_t msg;

	msg.type = STR_TERMINATE_SRCIP;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.terminate_srcip.start = push_string(&msg, start);
	msg.terminate_srcip.end = push_string(&msg, end);
	return send_stroke_msg(&msg);
}

static int rekey_connection(char *name)
{
	stroke_msg_t msg;

	msg.type = STR_REKEY;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.rekey.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int route_connection(char *name)
{
	stroke_msg_t msg;

	msg.type = STR_ROUTE;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.route.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int unroute_connection(char *name)
{
	stroke_msg_t msg;

	msg.type = STR_UNROUTE;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.unroute.name = push_string(&msg, name);
	return send_stroke_msg(&msg);
}

static int show_status(stroke_keyword_t kw, char *connection)
{
	stroke_msg_t msg;

	switch (kw)
	{
		case STROKE_STATUSALL:
			msg.type = STR_STATUS_ALL;
			break;
		case STROKE_STATUSALL_NOBLK:
			msg.type = STR_STATUS_ALL_NOBLK;
			break;
		default:
			msg.type = STR_STATUS;
			break;
	}
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.status.name = push_string(&msg, connection);
	return send_stroke_msg(&msg);
}

static int list_flags[] = {
	LIST_PUBKEYS,
	LIST_CERTS,
	LIST_CACERTS,
	LIST_OCSPCERTS,
	LIST_AACERTS,
	LIST_ACERTS,
	LIST_GROUPS,
	LIST_CAINFOS,
	LIST_CRLS,
	LIST_OCSP,
	LIST_ALGS,
	LIST_PLUGINS,
	LIST_ALL
};

static int list(stroke_keyword_t kw, int utc)
{
	stroke_msg_t msg;

	msg.type = STR_LIST;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.list.utc = utc;
	msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
	return send_stroke_msg(&msg);
}

static int reread_flags[] = {
	REREAD_SECRETS,
	REREAD_CACERTS,
	REREAD_OCSPCERTS,
	REREAD_AACERTS,
	REREAD_ACERTS,
	REREAD_CRLS,
	REREAD_ALL
};

static int reread(stroke_keyword_t kw)
{
	stroke_msg_t msg;

	msg.type = STR_REREAD;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
	return send_stroke_msg(&msg);
}

static int purge_flags[] = {
	PURGE_OCSP,
	PURGE_CRLS,
	PURGE_CERTS,
	PURGE_IKE,
};

static int purge(stroke_keyword_t kw)
{
	stroke_msg_t msg;

	msg.type = STR_PURGE;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
	return send_stroke_msg(&msg);
}

static int export_flags[] = {
	EXPORT_X509,
};

static int export(stroke_keyword_t kw, char *selector)
{
	stroke_msg_t msg;

	msg.type = STR_EXPORT;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.export.selector = push_string(&msg, selector);
	msg.export.flags = export_flags[kw - STROKE_EXPORT_FIRST];
	return send_stroke_msg(&msg);
}

static int leases(stroke_keyword_t kw, char *pool, char *address)
{
	stroke_msg_t msg;

	msg.type = STR_LEASES;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.leases.pool = push_string(&msg, pool);
	msg.leases.address = push_string(&msg, address);
	return send_stroke_msg(&msg);
}

static int memusage()
{
	stroke_msg_t msg;

	msg.type = STR_MEMUSAGE;
	msg.length = offsetof(stroke_msg_t, buffer);
	return send_stroke_msg(&msg);
}

static int user_credentials(char *name, char *user, char *pass)
{
	stroke_msg_t msg;

	msg.type = STR_USER_CREDS;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.user_creds.name = push_string(&msg, name);
	msg.user_creds.username = push_string(&msg, user);
	msg.user_creds.password = push_string(&msg, pass);
	return send_stroke_msg(&msg);
}


static int set_loglevel(char *type, u_int level)
{
	stroke_msg_t msg;

	msg.type = STR_LOGLEVEL;
	msg.length = offsetof(stroke_msg_t, buffer);
	msg.loglevel.type = push_string(&msg, type);
	msg.loglevel.level = level;
	return send_stroke_msg(&msg);
}

static void exit_error(char *error)
{
	if (error)
	{
		fprintf(stderr, "%s\n", error);
	}
	exit(-1);
}

static void exit_usage(char *error)
{
	printf("Usage:\n");
	printf("  Add a connection:\n");
	printf("    stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
	printf("           MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
	printf("    where: ID is any IKEv2 ID \n");
	printf("           ADDR is a IPv4 address\n");
	printf("           NET is a IPv4 subnet in CIDR notation\n");
	printf("  Delete a connection:\n");
	printf("    stroke delete NAME\n");
	printf("    where: NAME is a connection name added with \"stroke add\"\n");
	printf("  Initiate a connection:\n");
	printf("    stroke up NAME\n");
	printf("    where: NAME is a connection name added with \"stroke add\"\n");
	printf("  Terminate a connection:\n");
	printf("    stroke down NAME\n");
	printf("    where: NAME is a connection name added with \"stroke add\"\n");
	printf("  Terminate a connection by remote srcip:\n");
	printf("    stroke down-srcip START [END]\n");
	printf("    where: START and optional END define the clients source IP\n");
	printf("  Set loglevel for a logging type:\n");
	printf("    stroke loglevel TYPE LEVEL\n");
	printf("    where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|asn|enc|tnc|imc|imv|pts|tls|esp|lib\n");
	printf("           LEVEL is -1|0|1|2|3|4\n");
	printf("  Show connection status:\n");
	printf("    stroke status\n");
	printf("  Show extended status information:\n");
	printf("    stroke statusall\n");
	printf("  Show extended status information without blocking:\n");
	printf("    stroke statusallnb\n");
	printf("  Show list of authority and attribute certificates:\n");
	printf("    stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
	printf("  Show list of end entity certificates, ca info records  and crls:\n");
	printf("    stroke listcerts|listcainfos|listcrls|listall\n");
	printf("  Show list of supported algorithms:\n");
	printf("    stroke listalgs\n");
	printf("  Reload authority and attribute certificates:\n");
	printf("    stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
	printf("  Reload secrets and crls:\n");
	printf("    stroke rereadsecrets|rereadcrls|rereadall\n");
	printf("  Purge ocsp cache entries:\n");
	printf("    stroke purgeocsp\n");
	printf("  Purge CRL cache entries:\n");
	printf("    stroke purgecrls\n");
	printf("  Purge X509 cache entries:\n");
	printf("    stroke purgecerts\n");
	printf("  Purge IKE_SAs without a CHILD_SA:\n");
	printf("    stroke purgeike\n");
	printf("  Export credentials to the console:\n");
	printf("    stroke exportx509 DN\n");
	printf("  Show current memory usage:\n");
	printf("    stroke memusage\n");
	printf("  Show leases of a pool:\n");
	printf("    stroke leases [POOL [ADDRESS]]\n");
	printf("  Set username and password for a connection:\n");
	printf("    stroke user-creds NAME USERNAME [PASSWORD]\n");
	printf("    where: NAME is a connection name added with \"stroke add\"\n");
	printf("           USERNAME is the username\n");
	printf("           PASSWORD is the optional password, you'll be asked to enter it if not given\n");
	exit_error(error);
}

int main(int argc, char *argv[])
{
	const stroke_token_t *token;
	int res = 0;

	library_init(NULL);
	atexit(library_deinit);

	if (argc < 2)
	{
		exit_usage(NULL);
	}

	token = in_word_set(argv[1], strlen(argv[1]));

	if (token == NULL)
	{
		exit_usage("unknown keyword");
	}

	switch (token->kw)
	{
		case STROKE_ADD:
			if (argc < 11)
			{
				exit_usage("\"add\" needs more parameters...");
			}
			res = add_connection(argv[2],
								 argv[3], argv[4],
								 argv[5], argv[6],
								 argv[7], argv[8]);
			break;
		case STROKE_DELETE:
		case STROKE_DEL:
			if (argc < 3)
			{
				exit_usage("\"delete\" needs a connection name");
			}
			res = del_connection(argv[2]);
			break;
		case STROKE_UP:
			if (argc < 3)
			{
				exit_usage("\"up\" needs a connection name");
			}
			res = initiate_connection(argv[2]);
			break;
		case STROKE_DOWN:
			if (argc < 3)
			{
				exit_usage("\"down\" needs a connection name");
			}
			res = terminate_connection(argv[2]);
			break;
		case STROKE_DOWN_SRCIP:
			if (argc < 3)
			{
				exit_usage("\"down-srcip\" needs start and optional end address");
			}
			res = terminate_connection_srcip(argv[2], argc > 3 ? argv[3] : NULL);
			break;
		case STROKE_REKEY:
			if (argc < 3)
			{
				exit_usage("\"rekey\" needs a connection name");
			}
			res = rekey_connection(argv[2]);
			break;
		case STROKE_ROUTE:
			if (argc < 3)
			{
				exit_usage("\"route\" needs a connection name");
			}
			res = route_connection(argv[2]);
			break;
		case STROKE_UNROUTE:
			if (argc < 3)
			{
				exit_usage("\"unroute\" needs a connection name");
			}
			res = unroute_connection(argv[2]);
			break;
		case STROKE_LOGLEVEL:
			if (argc < 4)
			{
				exit_usage("\"logtype\" needs more parameters...");
			}
			res = set_loglevel(argv[2], atoi(argv[3]));
			break;
		case STROKE_STATUS:
		case STROKE_STATUSALL:
		case STROKE_STATUSALL_NOBLK:
			res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
			break;
		case STROKE_LIST_PUBKEYS:
		case STROKE_LIST_CERTS:
		case STROKE_LIST_CACERTS:
		case STROKE_LIST_OCSPCERTS:
		case STROKE_LIST_AACERTS:
		case STROKE_LIST_ACERTS:
		case STROKE_LIST_CAINFOS:
		case STROKE_LIST_CRLS:
		case STROKE_LIST_OCSP:
		case STROKE_LIST_ALGS:
		case STROKE_LIST_PLUGINS:
		case STROKE_LIST_ALL:
			res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
			break;
		case STROKE_REREAD_SECRETS:
		case STROKE_REREAD_CACERTS:
		case STROKE_REREAD_OCSPCERTS:
		case STROKE_REREAD_AACERTS:
		case STROKE_REREAD_ACERTS:
		case STROKE_REREAD_CRLS:
		case STROKE_REREAD_ALL:
			res = reread(token->kw);
			break;
		case STROKE_PURGE_OCSP:
		case STROKE_PURGE_CRLS:
		case STROKE_PURGE_CERTS:
		case STROKE_PURGE_IKE:
			res = purge(token->kw);
			break;
		case STROKE_EXPORT_X509:
			if (argc != 3)
			{
				exit_usage("\"exportx509\" needs a distinguished name");
			}
			res = export(token->kw, argv[2]);
			break;
		case STROKE_LEASES:
			res = leases(token->kw, argc > 2 ? argv[2] : NULL,
						 argc > 3 ? argv[3] : NULL);
			break;
		case STROKE_MEMUSAGE:
			res = memusage();
			break;
		case STROKE_USER_CREDS:
			if (argc < 4)
			{
				exit_usage("\"user-creds\" needs a connection name, "
						   "username and optionally a password");
			}
			res = user_credentials(argv[2], argv[3], argc > 4 ? argv[4] : NULL);
			break;
		default:
			exit_usage(NULL);
	}
	return res;
}