handle zero size Base64 conversions
[strongswan.git] / src / stroke / stroke.c
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stddef.h>
28 #include <string.h>
29
30 #include <library.h>
31
32 #include "stroke_msg.h"
33 #include "stroke_keywords.h"
34
35 struct stroke_token {
36 char *name;
37 stroke_keyword_t kw;
38 };
39
40 static char* push_string(stroke_msg_t *msg, char *string)
41 {
42 unsigned long string_start = msg->length;
43
44 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
45 {
46 return NULL;
47 }
48 else
49 {
50 msg->length += strlen(string) + 1;
51 strcpy((char*)msg + string_start, string);
52 return (char*)string_start;
53 }
54 }
55
56 static int send_stroke_msg (stroke_msg_t *msg)
57 {
58 struct sockaddr_un ctl_addr;
59 int sock, byte_count;
60 char buffer[512], *pass;
61
62 ctl_addr.sun_family = AF_UNIX;
63 strcpy(ctl_addr.sun_path, STROKE_SOCKET);
64
65 msg->output_verbosity = 1; /* CONTROL */
66
67 sock = socket(AF_UNIX, SOCK_STREAM, 0);
68 if (sock < 0)
69 {
70 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
71 return -1;
72 }
73 if (connect(sock, (struct sockaddr *)&ctl_addr,
74 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
75 {
76 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
77 close(sock);
78 return -1;
79 }
80
81 /* send message */
82 if (write(sock, msg, msg->length) != msg->length)
83 {
84 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
85 close(sock);
86 return -1;
87 }
88
89 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
90 {
91 buffer[byte_count] = '\0';
92
93 /* we prompt if we receive the "Passphrase:"/"PIN:" magic keyword */
94 if ((byte_count >= 12 &&
95 strcmp(buffer + byte_count - 12, "Passphrase:\n") == 0) ||
96 (byte_count >= 5 &&
97 strcmp(buffer + byte_count - 5, "PIN:\n") == 0))
98 {
99 /* remove trailing newline */
100 pass = strrchr(buffer, '\n');
101 if (pass)
102 {
103 *pass = ' ';
104 }
105 pass = getpass(buffer);
106 if (pass)
107 {
108 ignore_result(write(sock, pass, strlen(pass)));
109 ignore_result(write(sock, "\n", 1));
110 }
111 }
112 else
113 {
114 printf("%s", buffer);
115 }
116 }
117 if (byte_count < 0)
118 {
119 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
120 }
121
122 close(sock);
123 return 0;
124 }
125
126 static int add_connection(char *name,
127 char *my_id, char *other_id,
128 char *my_addr, char *other_addr,
129 char *my_nets, char *other_nets)
130 {
131 stroke_msg_t msg;
132
133 memset(&msg, 0, sizeof(msg));
134 msg.length = offsetof(stroke_msg_t, buffer);
135 msg.type = STR_ADD_CONN;
136
137 msg.add_conn.name = push_string(&msg, name);
138 msg.add_conn.ikev2 = 1;
139 msg.add_conn.auth_method = 2;
140 msg.add_conn.mode = 1;
141 msg.add_conn.mobike = 1;
142 msg.add_conn.dpd.action = 1;
143
144 msg.add_conn.me.id = push_string(&msg, my_id);
145 msg.add_conn.me.address = push_string(&msg, my_addr);
146 msg.add_conn.me.ikeport = 500;
147 msg.add_conn.me.subnets = push_string(&msg, my_nets);
148 msg.add_conn.me.sendcert = 1;
149
150 msg.add_conn.other.id = push_string(&msg, other_id);
151 msg.add_conn.other.address = push_string(&msg, other_addr);
152 msg.add_conn.other.ikeport = 500;
153 msg.add_conn.other.subnets = push_string(&msg, other_nets);
154 msg.add_conn.other.sendcert = 1;
155
156 return send_stroke_msg(&msg);
157 }
158
159 static int del_connection(char *name)
160 {
161 stroke_msg_t msg;
162
163 msg.length = offsetof(stroke_msg_t, buffer);
164 msg.type = STR_DEL_CONN;
165 msg.initiate.name = push_string(&msg, name);
166 return send_stroke_msg(&msg);
167 }
168
169 static int initiate_connection(char *name)
170 {
171 stroke_msg_t msg;
172
173 msg.length = offsetof(stroke_msg_t, buffer);
174 msg.type = STR_INITIATE;
175 msg.initiate.name = push_string(&msg, name);
176 return send_stroke_msg(&msg);
177 }
178
179 static int terminate_connection(char *name)
180 {
181 stroke_msg_t msg;
182
183 msg.type = STR_TERMINATE;
184 msg.length = offsetof(stroke_msg_t, buffer);
185 msg.initiate.name = push_string(&msg, name);
186 return send_stroke_msg(&msg);
187 }
188
189 static int terminate_connection_srcip(char *start, char *end)
190 {
191 stroke_msg_t msg;
192
193 msg.type = STR_TERMINATE_SRCIP;
194 msg.length = offsetof(stroke_msg_t, buffer);
195 msg.terminate_srcip.start = push_string(&msg, start);
196 msg.terminate_srcip.end = push_string(&msg, end);
197 return send_stroke_msg(&msg);
198 }
199
200 static int rekey_connection(char *name)
201 {
202 stroke_msg_t msg;
203
204 msg.type = STR_REKEY;
205 msg.length = offsetof(stroke_msg_t, buffer);
206 msg.rekey.name = push_string(&msg, name);
207 return send_stroke_msg(&msg);
208 }
209
210 static int route_connection(char *name)
211 {
212 stroke_msg_t msg;
213
214 msg.type = STR_ROUTE;
215 msg.length = offsetof(stroke_msg_t, buffer);
216 msg.route.name = push_string(&msg, name);
217 return send_stroke_msg(&msg);
218 }
219
220 static int unroute_connection(char *name)
221 {
222 stroke_msg_t msg;
223
224 msg.type = STR_UNROUTE;
225 msg.length = offsetof(stroke_msg_t, buffer);
226 msg.unroute.name = push_string(&msg, name);
227 return send_stroke_msg(&msg);
228 }
229
230 static int show_status(stroke_keyword_t kw, char *connection)
231 {
232 stroke_msg_t msg;
233
234 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
235 msg.length = offsetof(stroke_msg_t, buffer);
236 msg.status.name = push_string(&msg, connection);
237 return send_stroke_msg(&msg);
238 }
239
240 static int list_flags[] = {
241 LIST_PUBKEYS,
242 LIST_CERTS,
243 LIST_CACERTS,
244 LIST_OCSPCERTS,
245 LIST_AACERTS,
246 LIST_ACERTS,
247 LIST_GROUPS,
248 LIST_CAINFOS,
249 LIST_CRLS,
250 LIST_OCSP,
251 LIST_ALGS,
252 LIST_ALL
253 };
254
255 static int list(stroke_keyword_t kw, int utc)
256 {
257 stroke_msg_t msg;
258
259 msg.type = STR_LIST;
260 msg.length = offsetof(stroke_msg_t, buffer);
261 msg.list.utc = utc;
262 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
263 return send_stroke_msg(&msg);
264 }
265
266 static int reread_flags[] = {
267 REREAD_SECRETS,
268 REREAD_CACERTS,
269 REREAD_OCSPCERTS,
270 REREAD_AACERTS,
271 REREAD_ACERTS,
272 REREAD_CRLS,
273 REREAD_ALL
274 };
275
276 static int reread(stroke_keyword_t kw)
277 {
278 stroke_msg_t msg;
279
280 msg.type = STR_REREAD;
281 msg.length = offsetof(stroke_msg_t, buffer);
282 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
283 return send_stroke_msg(&msg);
284 }
285
286 static int purge_flags[] = {
287 PURGE_OCSP,
288 PURGE_IKE,
289 PURGE_CRLS,
290 PURGE_CERTS,
291 };
292
293 static int purge(stroke_keyword_t kw)
294 {
295 stroke_msg_t msg;
296
297 msg.type = STR_PURGE;
298 msg.length = offsetof(stroke_msg_t, buffer);
299 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
300 return send_stroke_msg(&msg);
301 }
302
303 static int export_flags[] = {
304 EXPORT_X509,
305 };
306
307 static int export(stroke_keyword_t kw, char *selector)
308 {
309 stroke_msg_t msg;
310
311 msg.type = STR_EXPORT;
312 msg.length = offsetof(stroke_msg_t, buffer);
313 msg.export.selector = push_string(&msg, selector);
314 msg.export.flags = export_flags[kw - STROKE_EXPORT_FIRST];
315 return send_stroke_msg(&msg);
316 }
317
318 static int leases(stroke_keyword_t kw, char *pool, char *address)
319 {
320 stroke_msg_t msg;
321
322 msg.type = STR_LEASES;
323 msg.length = offsetof(stroke_msg_t, buffer);
324 msg.leases.pool = push_string(&msg, pool);
325 msg.leases.address = push_string(&msg, address);
326 return send_stroke_msg(&msg);
327 }
328
329 static int set_loglevel(char *type, u_int level)
330 {
331 stroke_msg_t msg;
332
333 msg.type = STR_LOGLEVEL;
334 msg.length = offsetof(stroke_msg_t, buffer);
335 msg.loglevel.type = push_string(&msg, type);
336 msg.loglevel.level = level;
337 return send_stroke_msg(&msg);
338 }
339
340 static void exit_error(char *error)
341 {
342 if (error)
343 {
344 fprintf(stderr, "%s\n", error);
345 }
346 exit(-1);
347 }
348
349 static void exit_usage(char *error)
350 {
351 printf("Usage:\n");
352 printf(" Add a connection:\n");
353 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
354 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
355 printf(" where: ID is any IKEv2 ID \n");
356 printf(" ADDR is a IPv4 address\n");
357 printf(" NET is a IPv4 subnet in CIDR notation\n");
358 printf(" Delete a connection:\n");
359 printf(" stroke delete NAME\n");
360 printf(" where: NAME is a connection name added with \"stroke add\"\n");
361 printf(" Initiate a connection:\n");
362 printf(" stroke up NAME\n");
363 printf(" where: NAME is a connection name added with \"stroke add\"\n");
364 printf(" Terminate a connection:\n");
365 printf(" stroke down NAME\n");
366 printf(" where: NAME is a connection name added with \"stroke add\"\n");
367 printf(" Terminate a connection by remote srcip:\n");
368 printf(" stroke down-srcip START [END]\n");
369 printf(" where: START and optional END define the clients source IP\n");
370 printf(" Set loglevel for a logging type:\n");
371 printf(" stroke loglevel TYPE LEVEL\n");
372 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
373 printf(" LEVEL is -1|0|1|2|3|4\n");
374 printf(" Show connection status:\n");
375 printf(" stroke status\n");
376 printf(" Show list of authority and attribute certificates:\n");
377 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
378 printf(" Show list of end entity certificates, ca info records and crls:\n");
379 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
380 printf(" Show list of supported algorithms:\n");
381 printf(" stroke listalgs\n");
382 printf(" Reload authority and attribute certificates:\n");
383 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
384 printf(" Reload secrets and crls:\n");
385 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
386 printf(" Purge ocsp cache entries:\n");
387 printf(" stroke purgeocsp\n");
388 printf(" Purge CRL cache entries:\n");
389 printf(" stroke purgecrls\n");
390 printf(" Purge X509 cache entries:\n");
391 printf(" stroke purgecerts\n");
392 printf(" Purge IKE_SAs without a CHILD_SA:\n");
393 printf(" stroke purgeike\n");
394 printf(" Export credentials to the console:\n");
395 printf(" stroke exportx509 DN\n");
396 printf(" Show leases of a pool:\n");
397 printf(" stroke leases [POOL [ADDRESS]]\n");
398 exit_error(error);
399 }
400
401 int main(int argc, char *argv[])
402 {
403 const stroke_token_t *token;
404 int res = 0;
405
406 library_init(NULL);
407 atexit(library_deinit);
408
409 if (argc < 2)
410 {
411 exit_usage(NULL);
412 }
413
414 token = in_word_set(argv[1], strlen(argv[1]));
415
416 if (token == NULL)
417 {
418 exit_usage("unknown keyword");
419 }
420
421 switch (token->kw)
422 {
423 case STROKE_ADD:
424 if (argc < 11)
425 {
426 exit_usage("\"add\" needs more parameters...");
427 }
428 res = add_connection(argv[2],
429 argv[3], argv[4],
430 argv[5], argv[6],
431 argv[7], argv[8]);
432 break;
433 case STROKE_DELETE:
434 case STROKE_DEL:
435 if (argc < 3)
436 {
437 exit_usage("\"delete\" needs a connection name");
438 }
439 res = del_connection(argv[2]);
440 break;
441 case STROKE_UP:
442 if (argc < 3)
443 {
444 exit_usage("\"up\" needs a connection name");
445 }
446 res = initiate_connection(argv[2]);
447 break;
448 case STROKE_DOWN:
449 if (argc < 3)
450 {
451 exit_usage("\"down\" needs a connection name");
452 }
453 res = terminate_connection(argv[2]);
454 break;
455 case STROKE_DOWN_SRCIP:
456 if (argc < 3)
457 {
458 exit_usage("\"down-srcip\" needs start and optional end address");
459 }
460 res = terminate_connection_srcip(argv[2], argc > 3 ? argv[3] : NULL);
461 break;
462 case STROKE_REKEY:
463 if (argc < 3)
464 {
465 exit_usage("\"rekey\" needs a connection name");
466 }
467 res = rekey_connection(argv[2]);
468 break;
469 case STROKE_ROUTE:
470 if (argc < 3)
471 {
472 exit_usage("\"route\" needs a connection name");
473 }
474 res = route_connection(argv[2]);
475 break;
476 case STROKE_UNROUTE:
477 if (argc < 3)
478 {
479 exit_usage("\"unroute\" needs a connection name");
480 }
481 res = unroute_connection(argv[2]);
482 break;
483 case STROKE_LOGLEVEL:
484 if (argc < 4)
485 {
486 exit_usage("\"logtype\" needs more parameters...");
487 }
488 res = set_loglevel(argv[2], atoi(argv[3]));
489 break;
490 case STROKE_STATUS:
491 case STROKE_STATUSALL:
492 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
493 break;
494 case STROKE_LIST_PUBKEYS:
495 case STROKE_LIST_CERTS:
496 case STROKE_LIST_CACERTS:
497 case STROKE_LIST_OCSPCERTS:
498 case STROKE_LIST_AACERTS:
499 case STROKE_LIST_ACERTS:
500 case STROKE_LIST_CAINFOS:
501 case STROKE_LIST_CRLS:
502 case STROKE_LIST_OCSP:
503 case STROKE_LIST_ALGS:
504 case STROKE_LIST_ALL:
505 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
506 break;
507 case STROKE_REREAD_SECRETS:
508 case STROKE_REREAD_CACERTS:
509 case STROKE_REREAD_OCSPCERTS:
510 case STROKE_REREAD_AACERTS:
511 case STROKE_REREAD_ACERTS:
512 case STROKE_REREAD_CRLS:
513 case STROKE_REREAD_ALL:
514 res = reread(token->kw);
515 break;
516 case STROKE_PURGE_OCSP:
517 case STROKE_PURGE_CRLS:
518 case STROKE_PURGE_CERTS:
519 case STROKE_PURGE_IKE:
520 res = purge(token->kw);
521 break;
522 case STROKE_EXPORT_X509:
523 if (argc != 3)
524 {
525 exit_usage("\"exportx509\" needs a distinguished name");
526 }
527 res = export(token->kw, argv[2]);
528 break;
529 case STROKE_LEASES:
530 res = leases(token->kw, argc > 2 ? argv[2] : NULL,
531 argc > 3 ? argv[3] : NULL);
532 break;
533 default:
534 exit_usage(NULL);
535 }
536 return res;
537 }