9672c66db07a6b45d6cc89d4a873ef80564ca0a9
[strongswan.git] / src / starter / args.c
1 /* automatic handling of confread struct arguments
2 * Copyright (C) 2006 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil, Switzerland
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include <library.h>
21 #include <utils/debug.h>
22
23 #include "keywords.h"
24 #include "confread.h"
25 #include "args.h"
26
27 /* argument types */
28
29 typedef enum {
30 ARG_NONE,
31 ARG_ENUM,
32 ARG_UINT,
33 ARG_TIME,
34 ARG_ULNG,
35 ARG_ULLI,
36 ARG_UBIN,
37 ARG_PCNT,
38 ARG_STR,
39 ARG_LST,
40 ARG_MISC
41 } arg_t;
42
43 /* various keyword lists */
44
45 static const char *LST_bool[] = {
46 "no",
47 "yes",
48 NULL
49 };
50
51 static const char *LST_sendcert[] = {
52 "always",
53 "ifasked",
54 "never",
55 "yes",
56 "no",
57 NULL
58 };
59
60 static const char *LST_unique[] = {
61 "no",
62 "yes",
63 "replace",
64 "keep",
65 "never",
66 NULL
67 };
68
69 static const char *LST_strict[] = {
70 "no",
71 "yes",
72 "ifuri",
73 NULL
74 };
75 static const char *LST_dpd_action[] = {
76 "none",
77 "clear",
78 "hold",
79 "restart",
80 NULL
81 };
82
83 static const char *LST_startup[] = {
84 "ignore",
85 "add",
86 "route",
87 "start",
88 NULL
89 };
90
91 static const char *LST_keyexchange[] = {
92 "ike",
93 "ikev1",
94 "ikev2",
95 NULL
96 };
97
98 static const char *LST_authby[] = {
99 "psk",
100 "secret",
101 "pubkey",
102 "rsa",
103 "rsasig",
104 "ecdsa",
105 "ecdsasig",
106 "xauthpsk",
107 "xauthrsasig",
108 "never",
109 NULL
110 };
111
112 static const char *LST_fragmentation[] = {
113 "no",
114 "yes",
115 "force",
116 NULL
117 };
118
119 typedef struct {
120 arg_t type;
121 size_t offset;
122 const char **list;
123 } token_info_t;
124
125 static const token_info_t token_info[] =
126 {
127 /* config setup keywords */
128 { ARG_STR, offsetof(starter_config_t, setup.charondebug), NULL },
129 { ARG_ENUM, offsetof(starter_config_t, setup.uniqueids), LST_unique },
130 { ARG_ENUM, offsetof(starter_config_t, setup.cachecrls), LST_bool },
131 { ARG_ENUM, offsetof(starter_config_t, setup.strictcrlpolicy), LST_strict },
132 { ARG_MISC, 0, NULL /* KW_PKCS11_DEPRECATED */ },
133 { ARG_MISC, 0, NULL /* KW_SETUP_DEPRECATED */ },
134
135 /* conn section keywords */
136 { ARG_STR, offsetof(starter_conn_t, name), NULL },
137 { ARG_ENUM, offsetof(starter_conn_t, startup), LST_startup },
138 { ARG_ENUM, offsetof(starter_conn_t, keyexchange), LST_keyexchange },
139 { ARG_MISC, 0, NULL /* KW_TYPE */ },
140 { ARG_MISC, 0, NULL /* KW_COMPRESS */ },
141 { ARG_ENUM, offsetof(starter_conn_t, install_policy), LST_bool },
142 { ARG_ENUM, offsetof(starter_conn_t, aggressive), LST_bool },
143 { ARG_MISC, 0, NULL /* KW_AUTH */ },
144 { ARG_STR, offsetof(starter_conn_t, authby), LST_authby },
145 { ARG_STR, offsetof(starter_conn_t, eap_identity), NULL },
146 { ARG_STR, offsetof(starter_conn_t, aaa_identity), NULL },
147 { ARG_MISC, 0, NULL /* KW_MOBIKE */ },
148 { ARG_MISC, 0, NULL /* KW_FORCEENCAPS */ },
149 { ARG_ENUM, offsetof(starter_conn_t, fragmentation), LST_fragmentation },
150 { ARG_UBIN, offsetof(starter_conn_t, ikedscp), NULL },
151 { ARG_TIME, offsetof(starter_conn_t, sa_ike_life_seconds), NULL },
152 { ARG_TIME, offsetof(starter_conn_t, sa_ipsec_life_seconds), NULL },
153 { ARG_TIME, offsetof(starter_conn_t, sa_rekey_margin), NULL },
154 { ARG_ULLI, offsetof(starter_conn_t, sa_ipsec_life_bytes), NULL },
155 { ARG_ULLI, offsetof(starter_conn_t, sa_ipsec_margin_bytes), NULL },
156 { ARG_ULLI, offsetof(starter_conn_t, sa_ipsec_life_packets), NULL },
157 { ARG_ULLI, offsetof(starter_conn_t, sa_ipsec_margin_packets), NULL },
158 { ARG_MISC, 0, NULL /* KW_KEYINGTRIES */ },
159 { ARG_PCNT, offsetof(starter_conn_t, sa_rekey_fuzz), NULL },
160 { ARG_MISC, 0, NULL /* KW_REKEY */ },
161 { ARG_MISC, 0, NULL /* KW_REAUTH */ },
162 { ARG_STR, offsetof(starter_conn_t, ike), NULL },
163 { ARG_STR, offsetof(starter_conn_t, esp), NULL },
164 { ARG_STR, offsetof(starter_conn_t, ah), NULL },
165 { ARG_TIME, offsetof(starter_conn_t, dpd_delay), NULL },
166 { ARG_TIME, offsetof(starter_conn_t, dpd_timeout), NULL },
167 { ARG_ENUM, offsetof(starter_conn_t, dpd_action), LST_dpd_action },
168 { ARG_ENUM, offsetof(starter_conn_t, close_action), LST_dpd_action },
169 { ARG_TIME, offsetof(starter_conn_t, inactivity), NULL },
170 { ARG_MISC, 0, NULL /* KW_MODECONFIG */ },
171 { ARG_MISC, 0, NULL /* KW_XAUTH */ },
172 { ARG_STR, offsetof(starter_conn_t, xauth_identity), NULL },
173 { ARG_ENUM, offsetof(starter_conn_t, me_mediation), LST_bool },
174 { ARG_STR, offsetof(starter_conn_t, me_mediated_by), NULL },
175 { ARG_STR, offsetof(starter_conn_t, me_peerid), NULL },
176 { ARG_UINT, offsetof(starter_conn_t, reqid), NULL },
177 { ARG_MISC, 0, NULL /* KW_MARK */ },
178 { ARG_MISC, 0, NULL /* KW_MARK_IN */ },
179 { ARG_MISC, 0, NULL /* KW_MARK_OUT */ },
180 { ARG_MISC, 0, NULL /* KW_TFC */ },
181 { ARG_MISC, 0, NULL /* KW_PFS_DEPRECATED */ },
182 { ARG_MISC, 0, NULL /* KW_CONN_DEPRECATED */ },
183
184 /* ca section keywords */
185 { ARG_STR, offsetof(starter_ca_t, name), NULL },
186 { ARG_ENUM, offsetof(starter_ca_t, startup), LST_startup },
187 { ARG_STR, offsetof(starter_ca_t, cacert), NULL },
188 { ARG_STR, offsetof(starter_ca_t, crluri), NULL },
189 { ARG_STR, offsetof(starter_ca_t, crluri2), NULL },
190 { ARG_STR, offsetof(starter_ca_t, ocspuri), NULL },
191 { ARG_STR, offsetof(starter_ca_t, ocspuri2), NULL },
192 { ARG_STR, offsetof(starter_ca_t, certuribase), NULL },
193 { ARG_MISC, 0, NULL /* KW_CA_DEPRECATED */ },
194
195 /* end keywords */
196 { ARG_STR, offsetof(starter_end_t, host), NULL },
197 { ARG_UINT, offsetof(starter_end_t, ikeport), NULL },
198 { ARG_STR, offsetof(starter_end_t, subnet), NULL },
199 { ARG_MISC, 0, NULL /* KW_PROTOPORT */ },
200 { ARG_STR, offsetof(starter_end_t, sourceip), NULL },
201 { ARG_STR, offsetof(starter_end_t, dns), NULL },
202 { ARG_ENUM, offsetof(starter_end_t, firewall), LST_bool },
203 { ARG_ENUM, offsetof(starter_end_t, hostaccess), LST_bool },
204 { ARG_ENUM, offsetof(starter_end_t, allow_any), LST_bool },
205 { ARG_STR, offsetof(starter_end_t, updown), NULL },
206 { ARG_STR, offsetof(starter_end_t, auth), NULL },
207 { ARG_STR, offsetof(starter_end_t, auth2), NULL },
208 { ARG_STR, offsetof(starter_end_t, id), NULL },
209 { ARG_STR, offsetof(starter_end_t, id2), NULL },
210 { ARG_STR, offsetof(starter_end_t, rsakey), NULL },
211 { ARG_STR, offsetof(starter_end_t, cert), NULL },
212 { ARG_STR, offsetof(starter_end_t, cert2), NULL },
213 { ARG_STR, offsetof(starter_end_t, cert_policy), NULL },
214 { ARG_ENUM, offsetof(starter_end_t, sendcert), LST_sendcert },
215 { ARG_STR, offsetof(starter_end_t, ca), NULL },
216 { ARG_STR, offsetof(starter_end_t, ca2), NULL },
217 { ARG_STR, offsetof(starter_end_t, groups), NULL },
218 { ARG_STR, offsetof(starter_end_t, groups2), NULL },
219 { ARG_MISC, 0, NULL /* KW_END_DEPRECATED */ },
220 };
221
222 static void free_list(char **list)
223 {
224 char **s;
225
226 for (s = list; *s; s++)
227 {
228 free(*s);
229 }
230 free(list);
231 }
232
233 char** new_list(char *value)
234 {
235 char *val, *b, *e, *end, **ret;
236 int count;
237
238 val = strdupnull(value);
239 if (!val)
240 {
241 return NULL;
242 }
243 end = val + strlen(val);
244 for (b = val, count = 0; b < end;)
245 {
246 for (e = b; ((*e != ' ') && (*e != '\0')); e++);
247 *e = '\0';
248 if (e != b)
249 {
250 count++;
251 }
252 b = e + 1;
253 }
254 if (count == 0)
255 {
256 free(val);
257 return NULL;
258 }
259 ret = (char **)malloc((count+1) * sizeof(char *));
260
261 for (b = val, count = 0; b < end; )
262 {
263 for (e = b; (*e != '\0'); e++);
264 if (e != b)
265 {
266 ret[count++] = strdupnull(b);
267 }
268 b = e + 1;
269 }
270 ret[count] = NULL;
271 free(val);
272 return ret;
273 }
274
275
276 /*
277 * assigns an argument value to a struct field
278 */
279 bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
280 bool *assigned)
281 {
282 char *p = base + token_info[token].offset;
283 const char **list = token_info[token].list;
284
285 int index = -1; /* used for enumeration arguments */
286
287 seen_t *seen = (seen_t*)base; /* seen flags are at the top of the struct */
288
289 *assigned = FALSE;
290
291 DBG3(DBG_APP, " %s=%s", kw->entry->name, kw->value);
292
293 if (*seen & SEEN_KW(token, first))
294 {
295 DBG1(DBG_APP, "# duplicate '%s' option", kw->entry->name);
296 return FALSE;
297 }
298
299 /* set flag that this argument has been seen */
300 *seen |= SEEN_KW(token, first);
301
302 /* is there a keyword list? */
303 if (list != NULL && token_info[token].type != ARG_LST)
304 {
305 bool match = FALSE;
306
307 while (*list != NULL && !match)
308 {
309 index++;
310 match = streq(kw->value, *list++);
311 }
312 if (!match)
313 {
314 DBG1(DBG_APP, "# bad value: %s=%s", kw->entry->name, kw->value);
315 return FALSE;
316 }
317 }
318
319 switch (token_info[token].type)
320 {
321 case ARG_NONE:
322 DBG1(DBG_APP, "# option '%s' not supported yet", kw->entry->name);
323 return FALSE;
324 case ARG_ENUM:
325 {
326 if (index < 0)
327 {
328 DBG1(DBG_APP, "# bad enumeration value: %s=%s (%d)",
329 kw->entry->name, kw->value, index);
330 return FALSE;
331 }
332
333 if (token_info[token].list == LST_bool)
334 {
335 bool *b = (bool *)p;
336 *b = (index > 0);
337 }
338 else
339 {
340 int *i = (int *)p;
341 *i = index;
342 }
343 }
344 break;
345
346 case ARG_UINT:
347 {
348 char *endptr;
349 u_int *u = (u_int *)p;
350
351 *u = strtoul(kw->value, &endptr, 10);
352
353 if (*endptr != '\0')
354 {
355 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
356 kw->value);
357 return FALSE;
358 }
359 }
360 break;
361 case ARG_ULNG:
362 case ARG_PCNT:
363 {
364 char *endptr;
365 unsigned long *l = (unsigned long *)p;
366
367 *l = strtoul(kw->value, &endptr, 10);
368
369 if (token_info[token].type == ARG_ULNG)
370 {
371 if (*endptr != '\0')
372 {
373 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
374 kw->value);
375 return FALSE;
376 }
377 }
378 else
379 {
380 if ((*endptr != '%') || (endptr[1] != '\0') || endptr == kw->value)
381 {
382 DBG1(DBG_APP, "# bad percent value: %s=%s", kw->entry->name,
383 kw->value);
384 return FALSE;
385 }
386 }
387
388 }
389 break;
390 case ARG_ULLI:
391 {
392 char *endptr;
393 unsigned long long *ll = (unsigned long long *)p;
394
395 *ll = strtoull(kw->value, &endptr, 10);
396
397 if (*endptr != '\0')
398 {
399 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
400 kw->value);
401 return FALSE;
402 }
403 }
404 break;
405 case ARG_UBIN:
406 {
407 char *endptr;
408 u_int *u = (u_int *)p;
409
410 *u = strtoul(kw->value, &endptr, 2);
411
412 if (*endptr != '\0')
413 {
414 DBG1(DBG_APP, "# bad binary value: %s=%s", kw->entry->name,
415 kw->value);
416 return FALSE;
417 }
418 }
419 break;
420 case ARG_TIME:
421 {
422 char *endptr;
423 time_t *t = (time_t *)p;
424
425 *t = strtoul(kw->value, &endptr, 10);
426
427 /* time in seconds? */
428 if (*endptr == '\0' || (*endptr == 's' && endptr[1] == '\0'))
429 {
430 break;
431 }
432 if (endptr[1] == '\0')
433 {
434 if (*endptr == 'm') /* time in minutes? */
435 {
436 *t *= 60;
437 break;
438 }
439 if (*endptr == 'h') /* time in hours? */
440 {
441 *t *= 3600;
442 break;
443 }
444 if (*endptr == 'd') /* time in days? */
445 {
446 *t *= 3600*24;
447 break;
448 }
449 }
450 DBG1(DBG_APP, "# bad duration value: %s=%s", kw->entry->name,
451 kw->value);
452 return FALSE;
453 }
454 case ARG_STR:
455 {
456 char **cp = (char **)p;
457
458 /* free any existing string */
459 free(*cp);
460
461 /* assign the new string */
462 *cp = strdupnull(kw->value);
463 }
464 break;
465 case ARG_LST:
466 {
467 char ***listp = (char ***)p;
468
469 /* free any existing list */
470 if (*listp != NULL)
471 {
472 free_list(*listp);
473 }
474 /* create a new list and assign values */
475 *listp = new_list(kw->value);
476
477 /* is there a keyword list? */
478 if (list != NULL)
479 {
480 char ** lst;
481
482 for (lst = *listp; lst && *lst; lst++)
483 {
484 bool match = FALSE;
485
486 list = token_info[token].list;
487
488 while (*list != NULL && !match)
489 {
490 match = streq(*lst, *list++);
491 }
492 if (!match)
493 {
494 DBG1(DBG_APP, "# bad value: %s=%s",
495 kw->entry->name, *lst);
496 return FALSE;
497 }
498 }
499 }
500 }
501 /* fall through */
502 default:
503 return TRUE;
504 }
505
506 *assigned = TRUE;
507 return TRUE;
508 }
509
510 /*
511 * frees all dynamically allocated arguments in a struct
512 */
513 void free_args(kw_token_t first, kw_token_t last, char *base)
514 {
515 kw_token_t token;
516
517 for (token = first; token <= last; token++)
518 {
519 char *p = base + token_info[token].offset;
520
521 switch (token_info[token].type)
522 {
523 case ARG_STR:
524 {
525 char **cp = (char **)p;
526
527 free(*cp);
528 *cp = NULL;
529 }
530 break;
531 case ARG_LST:
532 {
533 char ***listp = (char ***)p;
534
535 if (*listp != NULL)
536 {
537 free_list(*listp);
538 *listp = NULL;
539 }
540 }
541 break;
542 default:
543 break;
544 }
545 }
546 }
547
548 /*
549 * clone all dynamically allocated arguments in a struct
550 */
551 void clone_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
552 {
553 kw_token_t token;
554
555 for (token = first; token <= last; token++)
556 {
557 if (token_info[token].type == ARG_STR)
558 {
559 char **cp1 = (char **)(base1 + token_info[token].offset);
560 char **cp2 = (char **)(base2 + token_info[token].offset);
561
562 *cp1 = strdupnull(*cp2);
563 }
564 }
565 }
566
567 static bool cmp_list(char **list1, char **list2)
568 {
569 if ((list1 == NULL) && (list2 == NULL))
570 {
571 return TRUE;
572 }
573 if ((list1 == NULL) || (list2 == NULL))
574 {
575 return FALSE;
576 }
577
578 for ( ; *list1 && *list2; list1++, list2++)
579 {
580 if (strcmp(*list1,*list2) != 0)
581 {
582 return FALSE;
583 }
584 }
585
586 if ((*list1 != NULL) || (*list2 != NULL))
587 {
588 return FALSE;
589 }
590
591 return TRUE;
592 }
593
594 /*
595 * compare all arguments in a struct
596 */
597 bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
598 {
599 kw_token_t token;
600
601 for (token = first; token <= last; token++)
602 {
603 char *p1 = base1 + token_info[token].offset;
604 char *p2 = base2 + token_info[token].offset;
605
606 switch (token_info[token].type)
607 {
608 case ARG_ENUM:
609 if (token_info[token].list == LST_bool)
610 {
611 bool *b1 = (bool *)p1;
612 bool *b2 = (bool *)p2;
613
614 if (*b1 != *b2)
615 {
616 return FALSE;
617 }
618 }
619 else
620 {
621 int *i1 = (int *)p1;
622 int *i2 = (int *)p2;
623
624 if (*i1 != *i2)
625 {
626 return FALSE;
627 }
628 }
629 break;
630 case ARG_UINT:
631 {
632 u_int *u1 = (u_int *)p1;
633 u_int *u2 = (u_int *)p2;
634
635 if (*u1 != *u2)
636 {
637 return FALSE;
638 }
639 }
640 break;
641 case ARG_ULNG:
642 case ARG_PCNT:
643 {
644 unsigned long *l1 = (unsigned long *)p1;
645 unsigned long *l2 = (unsigned long *)p2;
646
647 if (*l1 != *l2)
648 {
649 return FALSE;
650 }
651 }
652 break;
653 case ARG_ULLI:
654 {
655 unsigned long long *ll1 = (unsigned long long *)p1;
656 unsigned long long *ll2 = (unsigned long long *)p2;
657
658 if (*ll1 != *ll2)
659 {
660 return FALSE;
661 }
662 }
663 break;
664 case ARG_TIME:
665 {
666 time_t *t1 = (time_t *)p1;
667 time_t *t2 = (time_t *)p2;
668
669 if (*t1 != *t2)
670 {
671 return FALSE;
672 }
673 }
674 break;
675 case ARG_STR:
676 {
677 char **cp1 = (char **)p1;
678 char **cp2 = (char **)p2;
679
680 if (*cp1 == NULL && *cp2 == NULL)
681 {
682 break;
683 }
684 if (*cp1 == NULL || *cp2 == NULL || strcmp(*cp1, *cp2) != 0)
685 {
686 return FALSE;
687 }
688 }
689 break;
690 case ARG_LST:
691 {
692 char ***listp1 = (char ***)p1;
693 char ***listp2 = (char ***)p2;
694
695 if (!cmp_list(*listp1, *listp2))
696 {
697 return FALSE;
698 }
699 }
700 break;
701 default:
702 break;
703 }
704 }
705 return TRUE;
706 }