e78f2a70b288c1e19be12ef51fcba4756f3912ed
[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-sha256-curve25519";
44 static const char esp_defaults[] = "aes128-sha256";
45
46 static const char firewall_defaults[] = IPSEC_SCRIPT " _updown iptables";
47
48 /**
49 * Provided by GPERF
50 */
51 extern const kw_entry_t *in_word_set(register const char*, register GPERF_LEN_TYPE);
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 const 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 const 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 conn->fragmentation = FRAGMENTATION_YES;
226
227 conn->left.sendcert = CERT_SEND_IF_ASKED;
228 conn->right.sendcert = CERT_SEND_IF_ASKED;
229
230 conn->left.ikeport = 500;
231 conn->right.ikeport = 500;
232
233 conn->left.to_port = 0xffff;
234 conn->right.to_port = 0xffff;
235 }
236
237 /*
238 * parse left|right specific options
239 */
240 static void kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token,
241 char *key, char *value, starter_config_t *cfg)
242 {
243 bool assigned = FALSE;
244
245 if (is_deprecated(token, key, conn->name))
246 {
247 cfg->non_fatal_err++;
248 return;
249 }
250
251 if (!assign_arg(token, KW_END_FIRST, key, value, end, &assigned))
252 {
253 goto err;
254 }
255
256 /* post processing of some keywords that were assigned automatically */
257 switch (token)
258 {
259 case KW_HOST:
260 if (value && strlen(value) > 0 && value[0] == '%')
261 {
262 if (streq(value, "%defaultroute"))
263 {
264 value = "%any";
265 }
266 if (!streq(value, "%any") && !streq(value, "%any4") &&
267 !streq(value, "%any6"))
268 { /* allow_any prefix */
269 end->allow_any = TRUE;
270 value++;
271 }
272 }
273 free(end->host);
274 end->host = strdupnull(value);
275 break;
276 case KW_SOURCEIP:
277 conn->mode = MODE_TUNNEL;
278 conn->proxy_mode = FALSE;
279 break;
280 case KW_SENDCERT:
281 if (end->sendcert == CERT_YES_SEND)
282 {
283 end->sendcert = CERT_ALWAYS_SEND;
284 }
285 else if (end->sendcert == CERT_NO_SEND)
286 {
287 end->sendcert = CERT_NEVER_SEND;
288 }
289 break;
290 default:
291 break;
292 }
293
294 if (assigned)
295 {
296 return;
297 }
298
299 /* individual processing of keywords that were not assigned automatically */
300 switch (token)
301 {
302 case KW_PROTOPORT:
303 {
304 struct protoent *proto;
305 struct servent *svc;
306 char *sep, *port = "", *endptr;
307 long int p;
308
309 sep = strchr(value, '/');
310 if (sep)
311 { /* protocol/port */
312 *sep = '\0';
313 port = sep + 1;
314 }
315
316 if (streq(value, "%any"))
317 {
318 end->protocol = 0;
319 }
320 else
321 {
322 proto = getprotobyname(value);
323 if (proto)
324 {
325 end->protocol = proto->p_proto;
326 }
327 else
328 {
329 p = strtol(value, &endptr, 0);
330 if ((*value && *endptr) || p < 0 || p > 0xff)
331 {
332 DBG1(DBG_APP, "# bad protocol: %s=%s", key, value);
333 goto err;
334 }
335 end->protocol = (uint8_t)p;
336 }
337 }
338 if (streq(port, "%any"))
339 {
340 end->from_port = 0;
341 end->to_port = 0xffff;
342 }
343 else if (streq(port, "%opaque"))
344 {
345 end->from_port = 0xffff;
346 end->to_port = 0;
347 }
348 else if (*port)
349 {
350 svc = getservbyname(port, NULL);
351 if (svc)
352 {
353 end->from_port = end->to_port = ntohs(svc->s_port);
354 }
355 else
356 {
357 p = strtol(port, &endptr, 0);
358 if (p < 0 || p > 0xffff)
359 {
360 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
361 goto err;
362 }
363 end->from_port = p;
364 if (*endptr == '-')
365 {
366 port = endptr + 1;
367 p = strtol(port, &endptr, 0);
368 if (p < 0 || p > 0xffff)
369 {
370 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
371 goto err;
372 }
373 }
374 end->to_port = p;
375 if (*endptr)
376 {
377 DBG1(DBG_APP, "# bad port: %s=%s", key, port);
378 goto err;
379 }
380 }
381 }
382 if (sep)
383 { /* restore the original text in case also= is used */
384 *sep = '/';
385 }
386 break;
387 }
388 default:
389 break;
390 }
391 return;
392
393 err:
394 DBG1(DBG_APP, " bad argument value in conn '%s'", conn->name);
395 cfg->err++;
396 }
397
398 /*
399 * macro to handle simple flags
400 */
401 #define KW_SA_OPTION_FLAG(sy, sn, fl) \
402 if (streq(value, sy)) { conn->options |= fl; } \
403 else if (streq(value, sn)) { conn->options &= ~fl; } \
404 else { DBG1(DBG_APP, "# bad option value: %s=%s", key, value); cfg->err++; }
405
406 /*
407 * parse settings not handled by the simple argument parser
408 */
409 static void handle_keyword(kw_token_t token, starter_conn_t *conn, char *key,
410 char *value, starter_config_t *cfg)
411 {
412 if ((token == KW_ESP && conn->ah) || (token == KW_AH && conn->esp))
413 {
414 DBG1(DBG_APP, "# can't have both 'ah' and 'esp' options");
415 cfg->err++;
416 return;
417 }
418 switch (token)
419 {
420 case KW_TYPE:
421 {
422 conn->mode = MODE_TRANSPORT;
423 conn->proxy_mode = FALSE;
424 if (streq(value, "tunnel"))
425 {
426 conn->mode = MODE_TUNNEL;
427 }
428 else if (streq(value, "beet"))
429 {
430 conn->mode = MODE_BEET;
431 }
432 else if (streq(value, "transport_proxy"))
433 {
434 conn->mode = MODE_TRANSPORT;
435 conn->proxy_mode = TRUE;
436 }
437 else if (streq(value, "passthrough") || streq(value, "pass"))
438 {
439 conn->mode = MODE_PASS;
440 }
441 else if (streq(value, "drop") || streq(value, "reject"))
442 {
443 conn->mode = MODE_DROP;
444 }
445 else if (!streq(value, "transport"))
446 {
447 DBG1(DBG_APP, "# bad policy value: %s=%s", key, value);
448 cfg->err++;
449 }
450 break;
451 }
452 case KW_COMPRESS:
453 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_COMPRESS)
454 break;
455 case KW_MARK:
456 if (!mark_from_string(value, &conn->mark_in))
457 {
458 cfg->err++;
459 break;
460 }
461 conn->mark_out = conn->mark_in;
462 break;
463 case KW_MARK_IN:
464 if (!mark_from_string(value, &conn->mark_in))
465 {
466 cfg->err++;
467 }
468 break;
469 case KW_MARK_OUT:
470 if (!mark_from_string(value, &conn->mark_out))
471 {
472 cfg->err++;
473 }
474 break;
475 case KW_TFC:
476 if (streq(value, "%mtu"))
477 {
478 conn->tfc = -1;
479 }
480 else
481 {
482 char *endptr;
483
484 conn->tfc = strtoul(value, &endptr, 10);
485 if (*endptr != '\0')
486 {
487 DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
488 cfg->err++;
489 }
490 }
491 break;
492 case KW_KEYINGTRIES:
493 if (streq(value, "%forever"))
494 {
495 conn->sa_keying_tries = 0;
496 }
497 else
498 {
499 char *endptr;
500
501 conn->sa_keying_tries = strtoul(value, &endptr, 10);
502 if (*endptr != '\0')
503 {
504 DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
505 cfg->err++;
506 }
507 }
508 break;
509 case KW_REKEY:
510 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REKEY)
511 break;
512 case KW_REAUTH:
513 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REAUTH)
514 break;
515 case KW_MOBIKE:
516 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_MOBIKE)
517 break;
518 case KW_FORCEENCAPS:
519 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_FORCE_ENCAP)
520 break;
521 case KW_MODECONFIG:
522 KW_SA_OPTION_FLAG("push", "pull", SA_OPTION_MODECFG_PUSH)
523 break;
524 case KW_XAUTH:
525 KW_SA_OPTION_FLAG("server", "client", SA_OPTION_XAUTH_SERVER)
526 break;
527 default:
528 break;
529 }
530 }
531
532 /*
533 * handles left|rightfirewall and left|rightupdown parameters
534 */
535 static void handle_firewall(const char *label, starter_end_t *end,
536 starter_config_t *cfg)
537 {
538 if (end->firewall)
539 {
540 if (end->updown != NULL)
541 {
542 DBG1(DBG_APP, "# cannot have both %sfirewall and %supdown", label,
543 label);
544 cfg->err++;
545 }
546 else
547 {
548 end->updown = strdupnull(firewall_defaults);
549 end->firewall = FALSE;
550 }
551 }
552 }
553
554 /*
555 * parse a conn section
556 */
557 static void load_conn(starter_conn_t *conn, starter_config_t *cfg,
558 conf_parser_t *parser)
559 {
560 enumerator_t *enumerator;
561 dictionary_t *dict;
562 const kw_entry_t *entry;
563 kw_token_t token;
564 char *key, *value;
565
566 DBG2(DBG_APP, "Loading conn '%s'", conn->name);
567 dict = parser->get_section(parser, CONF_PARSER_CONN, conn->name);
568 if (!dict)
569 {
570 return;
571 }
572 enumerator = dict->create_enumerator(dict);
573 while (enumerator->enumerate(enumerator, &key, &value))
574 {
575 bool assigned = FALSE;
576
577 entry = in_word_set(key, strlen(key));
578 if (!entry)
579 {
580 DBG1(DBG_APP, "# unknown keyword '%s'", key);
581 cfg->non_fatal_err++;
582 continue;
583 }
584 token = entry->token;
585 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
586 {
587 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST,
588 key, value, cfg);
589 continue;
590 }
591 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
592 {
593 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST,
594 key, value, cfg);
595 continue;
596 }
597 if (token == KW_AUTO)
598 {
599 token = KW_CONN_SETUP;
600 }
601 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
602 {
603 DBG1(DBG_APP, "# unsupported keyword '%s' in conn '%s'",
604 key, conn->name);
605 cfg->err++;
606 continue;
607 }
608 if (is_deprecated(token, key, conn->name))
609 {
610 cfg->non_fatal_err++;
611 continue;
612 }
613 if (!assign_arg(token, KW_CONN_FIRST, key, value, conn,
614 &assigned))
615 {
616 DBG1(DBG_APP, " bad argument value in conn '%s'", conn->name);
617 cfg->err++;
618 continue;
619 }
620 if (!assigned)
621 {
622 handle_keyword(token, conn, key, value, cfg);
623 }
624 }
625 enumerator->destroy(enumerator);
626 dict->destroy(dict);
627
628 handle_firewall("left", &conn->left, cfg);
629 handle_firewall("right", &conn->right, cfg);
630
631 if (!conn->esp && !conn->ah)
632 {
633 conn->esp = strdupnull(esp_defaults);
634 }
635 }
636
637 /*
638 * free the memory used by a starter_ca_t object
639 */
640 static void confread_free_ca(starter_ca_t *ca)
641 {
642 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
643 free(ca);
644 }
645
646 /*
647 * free the memory used by a starter_conn_t object
648 */
649 static void confread_free_conn(starter_conn_t *conn)
650 {
651 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
652 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
653 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
654 free(conn);
655 }
656
657 /*
658 * free the memory used by a starter_config_t object
659 */
660 void confread_free(starter_config_t *cfg)
661 {
662 starter_conn_t *conn = cfg->conn_first;
663 starter_ca_t *ca = cfg->ca_first;
664
665 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
666
667 while (conn != NULL)
668 {
669 starter_conn_t *conn_aux = conn;
670
671 conn = conn->next;
672 confread_free_conn(conn_aux);
673 }
674
675 while (ca != NULL)
676 {
677 starter_ca_t *ca_aux = ca;
678
679 ca = ca->next;
680 confread_free_ca(ca_aux);
681 }
682
683 free(cfg);
684 }
685
686 /*
687 * load and parse an IPsec configuration file
688 */
689 starter_config_t* confread_load(const char *file)
690 {
691 conf_parser_t *parser;
692 starter_config_t *cfg = NULL;
693 enumerator_t *enumerator;
694 u_int total_err;
695 char *name;
696
697 parser = conf_parser_create(file);
698 if (!parser->parse(parser))
699 {
700 parser->destroy(parser);
701 return NULL;
702 }
703
704 INIT(cfg,
705 .setup = {
706 .uniqueids = TRUE,
707 }
708 );
709
710 /* load config setup section */
711 load_setup(cfg, parser);
712
713 /* load ca sections */
714 enumerator = parser->get_sections(parser, CONF_PARSER_CA);
715 while (enumerator->enumerate(enumerator, &name))
716 {
717 u_int previous_err = cfg->err;
718 starter_ca_t *ca;
719
720 INIT(ca,
721 .name = strdup(name),
722 );
723 load_ca(ca, cfg, parser);
724
725 if (cfg->err > previous_err)
726 {
727 total_err = cfg->err - previous_err;
728 DBG1(DBG_APP, "# ignored ca '%s' due to %d parsing error%s", name,
729 total_err, (total_err > 1) ? "s" : "");
730 confread_free_ca(ca);
731 cfg->non_fatal_err += cfg->err - previous_err;
732 cfg->err = previous_err;
733 }
734 else
735 {
736 if (cfg->ca_last)
737 {
738 cfg->ca_last->next = ca;
739 }
740 cfg->ca_last = ca;
741 if (!cfg->ca_first)
742 {
743 cfg->ca_first = ca;
744 }
745 if (ca->startup != STARTUP_NO)
746 {
747 ca->state = STATE_TO_ADD;
748 }
749 }
750 }
751 enumerator->destroy(enumerator);
752
753 /* load conn sections */
754 enumerator = parser->get_sections(parser, CONF_PARSER_CONN);
755 while (enumerator->enumerate(enumerator, &name))
756 {
757 u_int previous_err = cfg->err;
758 starter_conn_t *conn;
759
760 INIT(conn,
761 .name = strdup(name),
762 );
763 conn_defaults(conn);
764 load_conn(conn, cfg, parser);
765
766 if (cfg->err > previous_err)
767 {
768 total_err = cfg->err - previous_err;
769 DBG1(DBG_APP, "# ignored conn '%s' due to %d parsing error%s", name,
770 total_err, (total_err > 1) ? "s" : "");
771 confread_free_conn(conn);
772 cfg->non_fatal_err += cfg->err - previous_err;
773 cfg->err = previous_err;
774 }
775 else
776 {
777 if (cfg->conn_last)
778 {
779 cfg->conn_last->next = conn;
780 }
781 cfg->conn_last = conn;
782 if (!cfg->conn_first)
783 {
784 cfg->conn_first = conn;
785 }
786 if (conn->startup != STARTUP_NO)
787 {
788 conn->state = STATE_TO_ADD;
789 }
790 }
791 }
792 enumerator->destroy(enumerator);
793
794 parser->destroy(parser);
795
796 total_err = cfg->err + cfg->non_fatal_err;
797 if (total_err > 0)
798 {
799 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
800 total_err, (total_err > 1)?"s":"", cfg->err);
801 }
802 return cfg;
803 }