pki: Add pki --signcrl man page
[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_TIME, offsetof(starter_conn_t, dpd_delay), NULL },
165 { ARG_TIME, offsetof(starter_conn_t, dpd_timeout), NULL },
166 { ARG_ENUM, offsetof(starter_conn_t, dpd_action), LST_dpd_action },
167 { ARG_ENUM, offsetof(starter_conn_t, close_action), LST_dpd_action },
168 { ARG_TIME, offsetof(starter_conn_t, inactivity), NULL },
169 { ARG_MISC, 0, NULL /* KW_MODECONFIG */ },
170 { ARG_MISC, 0, NULL /* KW_XAUTH */ },
171 { ARG_STR, offsetof(starter_conn_t, xauth_identity), NULL },
172 { ARG_ENUM, offsetof(starter_conn_t, me_mediation), LST_bool },
173 { ARG_STR, offsetof(starter_conn_t, me_mediated_by), NULL },
174 { ARG_STR, offsetof(starter_conn_t, me_peerid), NULL },
175 { ARG_UINT, offsetof(starter_conn_t, reqid), NULL },
176 { ARG_MISC, 0, NULL /* KW_MARK */ },
177 { ARG_MISC, 0, NULL /* KW_MARK_IN */ },
178 { ARG_MISC, 0, NULL /* KW_MARK_OUT */ },
179 { ARG_MISC, 0, NULL /* KW_TFC */ },
180 { ARG_MISC, 0, NULL /* KW_PFS_DEPRECATED */ },
181 { ARG_MISC, 0, NULL /* KW_CONN_DEPRECATED */ },
182
183 /* ca section keywords */
184 { ARG_STR, offsetof(starter_ca_t, name), NULL },
185 { ARG_ENUM, offsetof(starter_ca_t, startup), LST_startup },
186 { ARG_STR, offsetof(starter_ca_t, cacert), NULL },
187 { ARG_STR, offsetof(starter_ca_t, crluri), NULL },
188 { ARG_STR, offsetof(starter_ca_t, crluri2), NULL },
189 { ARG_STR, offsetof(starter_ca_t, ocspuri), NULL },
190 { ARG_STR, offsetof(starter_ca_t, ocspuri2), NULL },
191 { ARG_STR, offsetof(starter_ca_t, certuribase), NULL },
192 { ARG_MISC, 0, NULL /* KW_CA_DEPRECATED */ },
193
194 /* end keywords */
195 { ARG_STR, offsetof(starter_end_t, host), NULL },
196 { ARG_UINT, offsetof(starter_end_t, ikeport), NULL },
197 { ARG_STR, offsetof(starter_end_t, subnet), NULL },
198 { ARG_MISC, 0, NULL /* KW_PROTOPORT */ },
199 { ARG_STR, offsetof(starter_end_t, sourceip), NULL },
200 { ARG_STR, offsetof(starter_end_t, dns), NULL },
201 { ARG_ENUM, offsetof(starter_end_t, firewall), LST_bool },
202 { ARG_ENUM, offsetof(starter_end_t, hostaccess), LST_bool },
203 { ARG_ENUM, offsetof(starter_end_t, allow_any), LST_bool },
204 { ARG_STR, offsetof(starter_end_t, updown), NULL },
205 { ARG_STR, offsetof(starter_end_t, auth), NULL },
206 { ARG_STR, offsetof(starter_end_t, auth2), NULL },
207 { ARG_STR, offsetof(starter_end_t, id), NULL },
208 { ARG_STR, offsetof(starter_end_t, id2), NULL },
209 { ARG_STR, offsetof(starter_end_t, rsakey), NULL },
210 { ARG_STR, offsetof(starter_end_t, cert), NULL },
211 { ARG_STR, offsetof(starter_end_t, cert2), NULL },
212 { ARG_STR, offsetof(starter_end_t, cert_policy), NULL },
213 { ARG_ENUM, offsetof(starter_end_t, sendcert), LST_sendcert },
214 { ARG_STR, offsetof(starter_end_t, ca), NULL },
215 { ARG_STR, offsetof(starter_end_t, ca2), NULL },
216 { ARG_STR, offsetof(starter_end_t, groups), NULL },
217 { ARG_STR, offsetof(starter_end_t, groups2), NULL },
218 { ARG_MISC, 0, NULL /* KW_END_DEPRECATED */ },
219 };
220
221 static void free_list(char **list)
222 {
223 char **s;
224
225 for (s = list; *s; s++)
226 {
227 free(*s);
228 }
229 free(list);
230 }
231
232 char** new_list(char *value)
233 {
234 char *val, *b, *e, *end, **ret;
235 int count;
236
237 val = strdupnull(value);
238 if (!val)
239 {
240 return NULL;
241 }
242 end = val + strlen(val);
243 for (b = val, count = 0; b < end;)
244 {
245 for (e = b; ((*e != ' ') && (*e != '\0')); e++);
246 *e = '\0';
247 if (e != b)
248 {
249 count++;
250 }
251 b = e + 1;
252 }
253 if (count == 0)
254 {
255 free(val);
256 return NULL;
257 }
258 ret = (char **)malloc((count+1) * sizeof(char *));
259
260 for (b = val, count = 0; b < end; )
261 {
262 for (e = b; (*e != '\0'); e++);
263 if (e != b)
264 {
265 ret[count++] = strdupnull(b);
266 }
267 b = e + 1;
268 }
269 ret[count] = NULL;
270 free(val);
271 return ret;
272 }
273
274
275 /*
276 * assigns an argument value to a struct field
277 */
278 bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
279 bool *assigned)
280 {
281 char *p = base + token_info[token].offset;
282 const char **list = token_info[token].list;
283
284 int index = -1; /* used for enumeration arguments */
285
286 seen_t *seen = (seen_t*)base; /* seen flags are at the top of the struct */
287
288 *assigned = FALSE;
289
290 DBG3(DBG_APP, " %s=%s", kw->entry->name, kw->value);
291
292 if (*seen & SEEN_KW(token, first))
293 {
294 DBG1(DBG_APP, "# duplicate '%s' option", kw->entry->name);
295 return FALSE;
296 }
297
298 /* set flag that this argument has been seen */
299 *seen |= SEEN_KW(token, first);
300
301 /* is there a keyword list? */
302 if (list != NULL && token_info[token].type != ARG_LST)
303 {
304 bool match = FALSE;
305
306 while (*list != NULL && !match)
307 {
308 index++;
309 match = streq(kw->value, *list++);
310 }
311 if (!match)
312 {
313 DBG1(DBG_APP, "# bad value: %s=%s", kw->entry->name, kw->value);
314 return FALSE;
315 }
316 }
317
318 switch (token_info[token].type)
319 {
320 case ARG_NONE:
321 DBG1(DBG_APP, "# option '%s' not supported yet", kw->entry->name);
322 return FALSE;
323 case ARG_ENUM:
324 {
325 if (index < 0)
326 {
327 DBG1(DBG_APP, "# bad enumeration value: %s=%s (%d)",
328 kw->entry->name, kw->value, index);
329 return FALSE;
330 }
331
332 if (token_info[token].list == LST_bool)
333 {
334 bool *b = (bool *)p;
335 *b = (index > 0);
336 }
337 else
338 {
339 int *i = (int *)p;
340 *i = index;
341 }
342 }
343 break;
344
345 case ARG_UINT:
346 {
347 char *endptr;
348 u_int *u = (u_int *)p;
349
350 *u = strtoul(kw->value, &endptr, 10);
351
352 if (*endptr != '\0')
353 {
354 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
355 kw->value);
356 return FALSE;
357 }
358 }
359 break;
360 case ARG_ULNG:
361 case ARG_PCNT:
362 {
363 char *endptr;
364 unsigned long *l = (unsigned long *)p;
365
366 *l = strtoul(kw->value, &endptr, 10);
367
368 if (token_info[token].type == ARG_ULNG)
369 {
370 if (*endptr != '\0')
371 {
372 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
373 kw->value);
374 return FALSE;
375 }
376 }
377 else
378 {
379 if ((*endptr != '%') || (endptr[1] != '\0') || endptr == kw->value)
380 {
381 DBG1(DBG_APP, "# bad percent value: %s=%s", kw->entry->name,
382 kw->value);
383 return FALSE;
384 }
385 }
386
387 }
388 break;
389 case ARG_ULLI:
390 {
391 char *endptr;
392 unsigned long long *ll = (unsigned long long *)p;
393
394 *ll = strtoull(kw->value, &endptr, 10);
395
396 if (*endptr != '\0')
397 {
398 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
399 kw->value);
400 return FALSE;
401 }
402 }
403 break;
404 case ARG_UBIN:
405 {
406 char *endptr;
407 u_int *u = (u_int *)p;
408
409 *u = strtoul(kw->value, &endptr, 2);
410
411 if (*endptr != '\0')
412 {
413 DBG1(DBG_APP, "# bad binary value: %s=%s", kw->entry->name,
414 kw->value);
415 return FALSE;
416 }
417 }
418 break;
419 case ARG_TIME:
420 {
421 char *endptr;
422 time_t *t = (time_t *)p;
423
424 *t = strtoul(kw->value, &endptr, 10);
425
426 /* time in seconds? */
427 if (*endptr == '\0' || (*endptr == 's' && endptr[1] == '\0'))
428 {
429 break;
430 }
431 if (endptr[1] == '\0')
432 {
433 if (*endptr == 'm') /* time in minutes? */
434 {
435 *t *= 60;
436 break;
437 }
438 if (*endptr == 'h') /* time in hours? */
439 {
440 *t *= 3600;
441 break;
442 }
443 if (*endptr == 'd') /* time in days? */
444 {
445 *t *= 3600*24;
446 break;
447 }
448 }
449 DBG1(DBG_APP, "# bad duration value: %s=%s", kw->entry->name,
450 kw->value);
451 return FALSE;
452 }
453 case ARG_STR:
454 {
455 char **cp = (char **)p;
456
457 /* free any existing string */
458 free(*cp);
459
460 /* assign the new string */
461 *cp = strdupnull(kw->value);
462 }
463 break;
464 case ARG_LST:
465 {
466 char ***listp = (char ***)p;
467
468 /* free any existing list */
469 if (*listp != NULL)
470 {
471 free_list(*listp);
472 }
473 /* create a new list and assign values */
474 *listp = new_list(kw->value);
475
476 /* is there a keyword list? */
477 if (list != NULL)
478 {
479 char ** lst;
480
481 for (lst = *listp; lst && *lst; lst++)
482 {
483 bool match = FALSE;
484
485 list = token_info[token].list;
486
487 while (*list != NULL && !match)
488 {
489 match = streq(*lst, *list++);
490 }
491 if (!match)
492 {
493 DBG1(DBG_APP, "# bad value: %s=%s",
494 kw->entry->name, *lst);
495 return FALSE;
496 }
497 }
498 }
499 }
500 /* fall through */
501 default:
502 return TRUE;
503 }
504
505 *assigned = TRUE;
506 return TRUE;
507 }
508
509 /*
510 * frees all dynamically allocated arguments in a struct
511 */
512 void free_args(kw_token_t first, kw_token_t last, char *base)
513 {
514 kw_token_t token;
515
516 for (token = first; token <= last; token++)
517 {
518 char *p = base + token_info[token].offset;
519
520 switch (token_info[token].type)
521 {
522 case ARG_STR:
523 {
524 char **cp = (char **)p;
525
526 free(*cp);
527 *cp = NULL;
528 }
529 break;
530 case ARG_LST:
531 {
532 char ***listp = (char ***)p;
533
534 if (*listp != NULL)
535 {
536 free_list(*listp);
537 *listp = NULL;
538 }
539 }
540 break;
541 default:
542 break;
543 }
544 }
545 }
546
547 /*
548 * clone all dynamically allocated arguments in a struct
549 */
550 void clone_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
551 {
552 kw_token_t token;
553
554 for (token = first; token <= last; token++)
555 {
556 if (token_info[token].type == ARG_STR)
557 {
558 char **cp1 = (char **)(base1 + token_info[token].offset);
559 char **cp2 = (char **)(base2 + token_info[token].offset);
560
561 *cp1 = strdupnull(*cp2);
562 }
563 }
564 }
565
566 static bool cmp_list(char **list1, char **list2)
567 {
568 if ((list1 == NULL) && (list2 == NULL))
569 {
570 return TRUE;
571 }
572 if ((list1 == NULL) || (list2 == NULL))
573 {
574 return FALSE;
575 }
576
577 for ( ; *list1 && *list2; list1++, list2++)
578 {
579 if (strcmp(*list1,*list2) != 0)
580 {
581 return FALSE;
582 }
583 }
584
585 if ((*list1 != NULL) || (*list2 != NULL))
586 {
587 return FALSE;
588 }
589
590 return TRUE;
591 }
592
593 /*
594 * compare all arguments in a struct
595 */
596 bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
597 {
598 kw_token_t token;
599
600 for (token = first; token <= last; token++)
601 {
602 char *p1 = base1 + token_info[token].offset;
603 char *p2 = base2 + token_info[token].offset;
604
605 switch (token_info[token].type)
606 {
607 case ARG_ENUM:
608 if (token_info[token].list == LST_bool)
609 {
610 bool *b1 = (bool *)p1;
611 bool *b2 = (bool *)p2;
612
613 if (*b1 != *b2)
614 {
615 return FALSE;
616 }
617 }
618 else
619 {
620 int *i1 = (int *)p1;
621 int *i2 = (int *)p2;
622
623 if (*i1 != *i2)
624 {
625 return FALSE;
626 }
627 }
628 break;
629 case ARG_UINT:
630 {
631 u_int *u1 = (u_int *)p1;
632 u_int *u2 = (u_int *)p2;
633
634 if (*u1 != *u2)
635 {
636 return FALSE;
637 }
638 }
639 break;
640 case ARG_ULNG:
641 case ARG_PCNT:
642 {
643 unsigned long *l1 = (unsigned long *)p1;
644 unsigned long *l2 = (unsigned long *)p2;
645
646 if (*l1 != *l2)
647 {
648 return FALSE;
649 }
650 }
651 break;
652 case ARG_ULLI:
653 {
654 unsigned long long *ll1 = (unsigned long long *)p1;
655 unsigned long long *ll2 = (unsigned long long *)p2;
656
657 if (*ll1 != *ll2)
658 {
659 return FALSE;
660 }
661 }
662 break;
663 case ARG_TIME:
664 {
665 time_t *t1 = (time_t *)p1;
666 time_t *t2 = (time_t *)p2;
667
668 if (*t1 != *t2)
669 {
670 return FALSE;
671 }
672 }
673 break;
674 case ARG_STR:
675 {
676 char **cp1 = (char **)p1;
677 char **cp2 = (char **)p2;
678
679 if (*cp1 == NULL && *cp2 == NULL)
680 {
681 break;
682 }
683 if (*cp1 == NULL || *cp2 == NULL || strcmp(*cp1, *cp2) != 0)
684 {
685 return FALSE;
686 }
687 }
688 break;
689 case ARG_LST:
690 {
691 char ***listp1 = (char ***)p1;
692 char ***listp2 = (char ***)p2;
693
694 if (!cmp_list(*listp1, *listp2))
695 {
696 return FALSE;
697 }
698 }
699 break;
700 default:
701 break;
702 }
703 }
704 return TRUE;
705 }