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