Merge branch 'starter-kernel-flush'
[strongswan.git] / src / starter / confread.c
1 /*
2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <netdb.h>
26
27 #include <library.h>
28 #include <utils/debug.h>
29
30 #include "keywords.h"
31 #include "confread.h"
32 #include "args.h"
33 #include "files.h"
34 #include "parser/conf_parser.h"
35
36 #define IKE_LIFETIME_DEFAULT 10800 /* 3 hours */
37 #define IPSEC_LIFETIME_DEFAULT 3600 /* 1 hour */
38 #define SA_REPLACEMENT_MARGIN_DEFAULT 540 /* 9 minutes */
39 #define SA_REPLACEMENT_FUZZ_DEFAULT 100 /* 100% of margin */
40 #define SA_REPLACEMENT_RETRIES_DEFAULT 3
41 #define SA_REPLAY_WINDOW_DEFAULT -1 /* use charon.replay_window */
42
43 static const char ike_defaults[] = "aes128-sha1-modp2048,3des-sha1-modp1536";
44 static const char esp_defaults[] = "aes128-sha1,3des-sha1";
45
46 static const char firewall_defaults[] = IPSEC_SCRIPT " _updown iptables";
47
48 /**
49 * Provided by GPERF
50 */
51 extern kw_entry_t *in_word_set (char *str, unsigned int len);
52
53 /**
54 * Process deprecated keywords
55 */
56 static bool is_deprecated(kw_token_t token, char *name, char *conn)
57 {
58 switch (token)
59 {
60 case KW_SETUP_DEPRECATED:
61 case KW_PKCS11_DEPRECATED:
62 DBG1(DBG_APP, "# deprecated keyword '%s' in config setup", name);
63 break;
64 case KW_CONN_DEPRECATED:
65 case KW_END_DEPRECATED:
66 case KW_PFS_DEPRECATED:
67 DBG1(DBG_APP, "# deprecated keyword '%s' in conn '%s'", name, conn);
68 break;
69 case KW_CA_DEPRECATED:
70 DBG1(DBG_APP, "# deprecated keyword '%s' in ca '%s'", name, conn);
71 break;
72 default:
73 return FALSE;
74 }
75 /* additional messages for some */
76 switch (token)
77 {
78 case KW_PKCS11_DEPRECATED:
79 DBG1(DBG_APP, " use the 'pkcs11' plugin instead");
80 break;
81 case KW_PFS_DEPRECATED:
82 DBG1(DBG_APP, " PFS is enabled by specifying a DH group in the "
83 "'esp' cipher suite");
84 break;
85 default:
86 break;
87 }
88 return TRUE;
89 }
90
91 /*
92 * parse config setup section
93 */
94 static void load_setup(starter_config_t *cfg, conf_parser_t *parser)
95 {
96 enumerator_t *enumerator;
97 dictionary_t *dict;
98 kw_entry_t *entry;
99 char *key, *value;
100
101 DBG2(DBG_APP, "Loading config setup");
102 dict = parser->get_section(parser, CONF_PARSER_CONFIG_SETUP, NULL);
103 if (!dict)
104 {
105 return;
106 }
107 enumerator = dict->create_enumerator(dict);
108 while (enumerator->enumerate(enumerator, &key, &value))
109 {
110 bool assigned = FALSE;
111
112 entry = in_word_set(key, strlen(key));
113 if (!entry)
114 {
115 DBG1(DBG_APP, "# unknown keyword '%s'", key);
116 cfg->non_fatal_err++;
117 continue;
118 }
119 if ((int)entry->token < KW_SETUP_FIRST || entry->token > KW_SETUP_LAST)
120 {
121 DBG1(DBG_APP, "# unsupported keyword '%s' in config setup", key);
122 cfg->err++;
123 continue;
124 }
125 if (is_deprecated(entry->token, key, ""))
126 {
127 cfg->non_fatal_err++;
128 continue;
129 }
130 if (!assign_arg(entry->token, KW_SETUP_FIRST, key, value, cfg,
131 &assigned))
132 {
133 DBG1(DBG_APP, " bad argument value in config setup");
134 cfg->err++;
135 }
136 }
137 enumerator->destroy(enumerator);
138 dict->destroy(dict);
139 }
140
141 /*
142 * parse a ca section
143 */
144 static void load_ca(starter_ca_t *ca, starter_config_t *cfg,
145 conf_parser_t *parser)
146 {
147 enumerator_t *enumerator;
148 dictionary_t *dict;
149 kw_entry_t *entry;
150 kw_token_t token;
151 char *key, *value;
152
153 DBG2(DBG_APP, "Loading ca '%s'", ca->name);
154 dict = parser->get_section(parser, CONF_PARSER_CA, ca->name);
155 if (!dict)
156 {
157 return;
158 }
159 enumerator = dict->create_enumerator(dict);
160 while (enumerator->enumerate(enumerator, &key, &value))
161 {
162 bool assigned = FALSE;
163
164 entry = in_word_set(key, strlen(key));
165 if (!entry)
166 {
167 DBG1(DBG_APP, "# unknown keyword '%s'", key);
168 cfg->non_fatal_err++;
169 continue;
170 }
171 token = entry->token;
172 if (token == KW_AUTO)
173 {
174 token = KW_CA_SETUP;
175 }
176 if (token < KW_CA_FIRST || token > KW_CA_LAST)
177 {
178 DBG1(DBG_APP, "# unsupported keyword '%s' in ca '%s'",
179 key, ca->name);
180 cfg->err++;
181 continue;
182 }
183 if (is_deprecated(token, key, ca->name))
184 {
185 cfg->non_fatal_err++;
186 continue;
187 }
188 if (!assign_arg(token, KW_CA_FIRST, key, value, ca, &assigned))
189 {
190 DBG1(DBG_APP, " bad argument value in ca '%s'", ca->name);
191 cfg->err++;
192 }
193 }
194 enumerator->destroy(enumerator);
195 dict->destroy(dict);
196
197 /* treat 'route' and 'start' as 'add' */
198 if (ca->startup != STARTUP_NO)
199 {
200 ca->startup = STARTUP_ADD;
201 }
202 }
203
204 /*
205 * set some default values
206 */
207 static void conn_defaults(starter_conn_t *conn)
208 {
209 conn->startup = STARTUP_NO;
210 conn->state = STATE_IGNORE;
211 conn->mode = MODE_TUNNEL;
212 conn->options = SA_OPTION_MOBIKE;
213
214 conn->ike = strdupnull(ike_defaults);
215 /* esp defaults are set after parsing the conn section */
216 conn->sa_ike_life_seconds = IKE_LIFETIME_DEFAULT;
217 conn->sa_ipsec_life_seconds = IPSEC_LIFETIME_DEFAULT;
218 conn->sa_rekey_margin = SA_REPLACEMENT_MARGIN_DEFAULT;
219 conn->sa_rekey_fuzz = SA_REPLACEMENT_FUZZ_DEFAULT;
220 conn->sa_keying_tries = SA_REPLACEMENT_RETRIES_DEFAULT;
221 conn->install_policy = TRUE;
222 conn->dpd_delay = 30; /* seconds */
223 conn->dpd_timeout = 150; /* seconds */
224 conn->replay_window = SA_REPLAY_WINDOW_DEFAULT;
225
226 conn->left.sendcert = CERT_SEND_IF_ASKED;
227 conn->right.sendcert = CERT_SEND_IF_ASKED;
228
229 conn->left.ikeport = 500;
230 conn->right.ikeport = 500;
231
232 conn->left.to_port = 0xffff;
233 conn->right.to_port = 0xffff;
234 }
235
236 /*
237 * parse left|right specific options
238 */
239 static void kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token,
240 char *key, char *value, starter_config_t *cfg)
241 {
242 bool assigned = FALSE;
243
244 if (is_deprecated(token, key, conn->name))
245 {
246 cfg->non_fatal_err++;
247 return;
248 }
249
250 if (!assign_arg(token, KW_END_FIRST, key, value, end, &assigned))
251 {
252 goto err;
253 }
254
255 /* post processing of some keywords that were assigned automatically */
256 switch (token)
257 {
258 case KW_HOST:
259 if (value && strlen(value) > 0 && value[0] == '%')
260 {
261 if (streq(value, "%defaultroute"))
262 {
263 value = "%any";
264 }
265 if (!streq(value, "%any") && !streq(value, "%any4") &&
266 !streq(value, "%any6"))
267 { /* allow_any prefix */
268 end->allow_any = TRUE;
269 value++;
270 }
271 }
272 free(end->host);
273 end->host = strdupnull(value);
274 break;
275 case KW_SOURCEIP:
276 conn->mode = MODE_TUNNEL;
277 conn->proxy_mode = FALSE;
278 break;
279 case KW_SENDCERT:
280 if (end->sendcert == CERT_YES_SEND)
281 {
282 end->sendcert = CERT_ALWAYS_SEND;
283 }
284 else if (end->sendcert == CERT_NO_SEND)
285 {
286 end->sendcert = CERT_NEVER_SEND;
287 }
288 break;
289 default:
290 break;
291 }
292
293 if (assigned)
294 {
295 return;
296 }
297
298 /* individual processing of keywords that were not assigned automatically */
299 switch (token)
300 {
301 case KW_PROTOPORT:
302 {
303 struct protoent *proto;
304 struct servent *svc;
305 char *sep, *port = "", *endptr;
306 long int p;
307
308 sep = strchr(value, '/');
309 if (sep)
310 { /* protocol/port */
311 *sep = '\0';
312 port = sep + 1;
313 }
314
315 if (streq(value, "%any"))
316 {
317 end->protocol = 0;
318 }
319 else
320 {
321 proto = getprotobyname(value);
322 if (proto)
323 {
324 end->protocol = proto->p_proto;
325 }
326 else
327 {
328 p = strtol(value, &endptr, 0);
329 if ((*value && *endptr) || p < 0 || p > 0xff)
330 {
331 DBG1(DBG_APP, "# bad protocol: %s=%s", key, value);
332 goto err;
333 }
334 end->protocol = (u_int8_t)p;
335 }
336 }
337 if (streq(port, "%any"))
338 {
339 end->from_port = 0;
340 end->to_port = 0xffff;
341 }
342 else if (streq(port, "%opaque"))
343 {
344 end->from_port = 0xffff;
345 end->to_port = 0;
346 }
347 else if (*port)
348 {
349 svc = getservbyname(port, NULL);
350 if (svc)
351 {
352 end->from_port = end->to_port = ntohs(svc->s_port);
353 }
354 else
355 {
356 p = strtol(port, &endptr, 0);
357 if (p < 0 || p > 0xffff)
358 {
359 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
360 goto err;
361 }
362 end->from_port = p;
363 if (*endptr == '-')
364 {
365 port = endptr + 1;
366 p = strtol(port, &endptr, 0);
367 if (p < 0 || p > 0xffff)
368 {
369 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
370 goto err;
371 }
372 }
373 end->to_port = p;
374 if (*endptr)
375 {
376 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
377 goto err;
378 }
379 }
380 }
381 if (sep)
382 { /* restore the original text in case also= is used */
383 *sep = '/';
384 }
385 break;
386 }
387 default:
388 break;
389 }
390 return;
391
392 err:
393 DBG1(DBG_APP, " bad argument value in conn '%s'", conn->name);
394 cfg->err++;
395 }
396
397 /*
398 * macro to handle simple flags
399 */
400 #define KW_SA_OPTION_FLAG(sy, sn, fl) \
401 if (streq(value, sy)) { conn->options |= fl; } \
402 else if (streq(value, sn)) { conn->options &= ~fl; } \
403 else { DBG1(DBG_APP, "# bad option value: %s=%s", key, value); cfg->err++; }
404
405 /*
406 * parse settings not handled by the simple argument parser
407 */
408 static void handle_keyword(kw_token_t token, starter_conn_t *conn, char *key,
409 char *value, starter_config_t *cfg)
410 {
411 if ((token == KW_ESP && conn->ah) || (token == KW_AH && conn->esp))
412 {
413 DBG1(DBG_APP, "# can't have both 'ah' and 'esp' options");
414 cfg->err++;
415 return;
416 }
417 switch (token)
418 {
419 case KW_TYPE:
420 {
421 conn->mode = MODE_TRANSPORT;
422 conn->proxy_mode = FALSE;
423 if (streq(value, "tunnel"))
424 {
425 conn->mode = MODE_TUNNEL;
426 }
427 else if (streq(value, "beet"))
428 {
429 conn->mode = MODE_BEET;
430 }
431 else if (streq(value, "transport_proxy"))
432 {
433 conn->mode = MODE_TRANSPORT;
434 conn->proxy_mode = TRUE;
435 }
436 else if (streq(value, "passthrough") || streq(value, "pass"))
437 {
438 conn->mode = MODE_PASS;
439 }
440 else if (streq(value, "drop") || streq(value, "reject"))
441 {
442 conn->mode = MODE_DROP;
443 }
444 else if (!streq(value, "transport"))
445 {
446 DBG1(DBG_APP, "# bad policy value: %s=%s", key, value);
447 cfg->err++;
448 }
449 break;
450 }
451 case KW_COMPRESS:
452 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_COMPRESS)
453 break;
454 case KW_MARK:
455 if (!mark_from_string(value, &conn->mark_in))
456 {
457 cfg->err++;
458 break;
459 }
460 conn->mark_out = conn->mark_in;
461 break;
462 case KW_MARK_IN:
463 if (!mark_from_string(value, &conn->mark_in))
464 {
465 cfg->err++;
466 }
467 break;
468 case KW_MARK_OUT:
469 if (!mark_from_string(value, &conn->mark_out))
470 {
471 cfg->err++;
472 }
473 break;
474 case KW_TFC:
475 if (streq(value, "%mtu"))
476 {
477 conn->tfc = -1;
478 }
479 else
480 {
481 char *endptr;
482
483 conn->tfc = strtoul(value, &endptr, 10);
484 if (*endptr != '\0')
485 {
486 DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
487 cfg->err++;
488 }
489 }
490 break;
491 case KW_KEYINGTRIES:
492 if (streq(value, "%forever"))
493 {
494 conn->sa_keying_tries = 0;
495 }
496 else
497 {
498 char *endptr;
499
500 conn->sa_keying_tries = strtoul(value, &endptr, 10);
501 if (*endptr != '\0')
502 {
503 DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
504 cfg->err++;
505 }
506 }
507 break;
508 case KW_REKEY:
509 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REKEY)
510 break;
511 case KW_REAUTH:
512 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REAUTH)
513 break;
514 case KW_MOBIKE:
515 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_MOBIKE)
516 break;
517 case KW_FORCEENCAPS:
518 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_FORCE_ENCAP)
519 break;
520 case KW_MODECONFIG:
521 KW_SA_OPTION_FLAG("push", "pull", SA_OPTION_MODECFG_PUSH)
522 break;
523 case KW_XAUTH:
524 KW_SA_OPTION_FLAG("server", "client", SA_OPTION_XAUTH_SERVER)
525 break;
526 default:
527 break;
528 }
529 }
530
531 /*
532 * handles left|rightfirewall and left|rightupdown parameters
533 */
534 static void handle_firewall(const char *label, starter_end_t *end,
535 starter_config_t *cfg)
536 {
537 if (end->firewall)
538 {
539 if (end->updown != NULL)
540 {
541 DBG1(DBG_APP, "# cannot have both %sfirewall and %supdown", label,
542 label);
543 cfg->err++;
544 }
545 else
546 {
547 end->updown = strdupnull(firewall_defaults);
548 end->firewall = FALSE;
549 }
550 }
551 }
552
553 /*
554 * parse a conn section
555 */
556 static void load_conn(starter_conn_t *conn, starter_config_t *cfg,
557 conf_parser_t *parser)
558 {
559 enumerator_t *enumerator;
560 dictionary_t *dict;
561 kw_entry_t *entry;
562 kw_token_t token;
563 char *key, *value;
564
565 DBG2(DBG_APP, "Loading conn '%s'", conn->name);
566 dict = parser->get_section(parser, CONF_PARSER_CONN, conn->name);
567 if (!dict)
568 {
569 return;
570 }
571 enumerator = dict->create_enumerator(dict);
572 while (enumerator->enumerate(enumerator, &key, &value))
573 {
574 bool assigned = FALSE;
575
576 entry = in_word_set(key, strlen(key));
577 if (!entry)
578 {
579 DBG1(DBG_APP, "# unknown keyword '%s'", key);
580 cfg->non_fatal_err++;
581 continue;
582 }
583 token = entry->token;
584 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
585 {
586 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST,
587 key, value, cfg);
588 continue;
589 }
590 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
591 {
592 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST,
593 key, value, cfg);
594 continue;
595 }
596 if (token == KW_AUTO)
597 {
598 token = KW_CONN_SETUP;
599 }
600 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
601 {
602 DBG1(DBG_APP, "# unsupported keyword '%s' in conn '%s'",
603 key, conn->name);
604 cfg->err++;
605 continue;
606 }
607 if (is_deprecated(token, key, conn->name))
608 {
609 cfg->non_fatal_err++;
610 continue;
611 }
612 if (!assign_arg(token, KW_CONN_FIRST, key, value, conn,
613 &assigned))
614 {
615 DBG1(DBG_APP, " bad argument value in conn '%s'", conn->name);
616 cfg->err++;
617 continue;
618 }
619 if (!assigned)
620 {
621 handle_keyword(token, conn, key, value, cfg);
622 }
623 }
624 enumerator->destroy(enumerator);
625 dict->destroy(dict);
626
627 handle_firewall("left", &conn->left, cfg);
628 handle_firewall("right", &conn->right, cfg);
629
630 if (!conn->esp && !conn->ah)
631 {
632 conn->esp = strdupnull(esp_defaults);
633 }
634 }
635
636 /*
637 * free the memory used by a starter_ca_t object
638 */
639 static void confread_free_ca(starter_ca_t *ca)
640 {
641 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
642 free(ca);
643 }
644
645 /*
646 * free the memory used by a starter_conn_t object
647 */
648 static void confread_free_conn(starter_conn_t *conn)
649 {
650 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
651 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
652 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
653 free(conn);
654 }
655
656 /*
657 * free the memory used by a starter_config_t object
658 */
659 void confread_free(starter_config_t *cfg)
660 {
661 starter_conn_t *conn = cfg->conn_first;
662 starter_ca_t *ca = cfg->ca_first;
663
664 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
665
666 while (conn != NULL)
667 {
668 starter_conn_t *conn_aux = conn;
669
670 conn = conn->next;
671 confread_free_conn(conn_aux);
672 }
673
674 while (ca != NULL)
675 {
676 starter_ca_t *ca_aux = ca;
677
678 ca = ca->next;
679 confread_free_ca(ca_aux);
680 }
681
682 free(cfg);
683 }
684
685 /*
686 * load and parse an IPsec configuration file
687 */
688 starter_config_t* confread_load(const char *file)
689 {
690 conf_parser_t *parser;
691 starter_config_t *cfg = NULL;
692 enumerator_t *enumerator;
693 u_int total_err;
694 char *name;
695
696 parser = conf_parser_create(file);
697 if (!parser->parse(parser))
698 {
699 parser->destroy(parser);
700 return NULL;
701 }
702
703 INIT(cfg,
704 .setup = {
705 .uniqueids = TRUE,
706 }
707 );
708
709 /* load config setup section */
710 load_setup(cfg, parser);
711
712 /* load ca sections */
713 enumerator = parser->get_sections(parser, CONF_PARSER_CA);
714 while (enumerator->enumerate(enumerator, &name))
715 {
716 u_int previous_err = cfg->err;
717 starter_ca_t *ca;
718
719 INIT(ca,
720 .name = strdup(name),
721 );
722 load_ca(ca, cfg, parser);
723
724 if (cfg->err > previous_err)
725 {
726 total_err = cfg->err - previous_err;
727 DBG1(DBG_APP, "# ignored ca '%s' due to %d parsing error%s", name,
728 total_err, (total_err > 1) ? "s" : "");
729 confread_free_ca(ca);
730 cfg->non_fatal_err += cfg->err - previous_err;
731 cfg->err = previous_err;
732 }
733 else
734 {
735 if (cfg->ca_last)
736 {
737 cfg->ca_last->next = ca;
738 }
739 cfg->ca_last = ca;
740 if (!cfg->ca_first)
741 {
742 cfg->ca_first = ca;
743 }
744 if (ca->startup != STARTUP_NO)
745 {
746 ca->state = STATE_TO_ADD;
747 }
748 }
749 }
750 enumerator->destroy(enumerator);
751
752 /* load conn sections */
753 enumerator = parser->get_sections(parser, CONF_PARSER_CONN);
754 while (enumerator->enumerate(enumerator, &name))
755 {
756 u_int previous_err = cfg->err;
757 starter_conn_t *conn;
758
759 INIT(conn,
760 .name = strdup(name),
761 );
762 conn_defaults(conn);
763 load_conn(conn, cfg, parser);
764
765 if (cfg->err > previous_err)
766 {
767 total_err = cfg->err - previous_err;
768 DBG1(DBG_APP, "# ignored conn '%s' due to %d parsing error%s", name,
769 total_err, (total_err > 1) ? "s" : "");
770 confread_free_conn(conn);
771 cfg->non_fatal_err += cfg->err - previous_err;
772 cfg->err = previous_err;
773 }
774 else
775 {
776 if (cfg->conn_last)
777 {
778 cfg->conn_last->next = conn;
779 }
780 cfg->conn_last = conn;
781 if (!cfg->conn_first)
782 {
783 cfg->conn_first = conn;
784 }
785 if (conn->startup != STARTUP_NO)
786 {
787 conn->state = STATE_TO_ADD;
788 }
789 }
790 }
791 enumerator->destroy(enumerator);
792
793 parser->destroy(parser);
794
795 total_err = cfg->err + cfg->non_fatal_err;
796 if (total_err > 0)
797 {
798 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
799 total_err, (total_err > 1)?"s":"", cfg->err);
800 }
801 return cfg;
802 }