ad73a163526f73a399fdfe00dce2b97a0a6931ac
1 /* automatic handling of confread struct arguments
2 * Copyright (C) 2006 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil, Switzerland
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>.
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
21 #include <utils/debug.h>
42 /* various keyword lists */
44 static const char *LST_bool
[] = {
50 static const char *LST_sendcert
[] = {
59 static const char *LST_unique
[] = {
68 static const char *LST_strict
[] = {
74 static const char *LST_dpd_action
[] = {
82 static const char *LST_startup
[] = {
90 static const char *LST_keyexchange
[] = {
97 static const char *LST_authby
[] = {
117 static const token_info_t token_info
[] =
119 /* config setup keywords */
120 { ARG_STR
, offsetof(starter_config_t
, setup
.charondebug
), NULL
},
121 { ARG_ENUM
, offsetof(starter_config_t
, setup
.uniqueids
), LST_unique
},
122 { ARG_ENUM
, offsetof(starter_config_t
, setup
.cachecrls
), LST_bool
},
123 { ARG_ENUM
, offsetof(starter_config_t
, setup
.strictcrlpolicy
), LST_strict
},
124 { ARG_MISC
, 0, NULL
/* KW_PKCS11_DEPRECATED */ },
125 { ARG_MISC
, 0, NULL
/* KW_SETUP_DEPRECATED */ },
127 /* conn section keywords */
128 { ARG_STR
, offsetof(starter_conn_t
, name
), NULL
},
129 { ARG_ENUM
, offsetof(starter_conn_t
, startup
), LST_startup
},
130 { ARG_ENUM
, offsetof(starter_conn_t
, keyexchange
), LST_keyexchange
},
131 { ARG_MISC
, 0, NULL
/* KW_TYPE */ },
132 { ARG_MISC
, 0, NULL
/* KW_COMPRESS */ },
133 { ARG_ENUM
, offsetof(starter_conn_t
, install_policy
), LST_bool
},
134 { ARG_ENUM
, offsetof(starter_conn_t
, aggressive
), LST_bool
},
135 { ARG_MISC
, 0, NULL
/* KW_AUTH */ },
136 { ARG_STR
, offsetof(starter_conn_t
, authby
), LST_authby
},
137 { ARG_STR
, offsetof(starter_conn_t
, eap_identity
), NULL
},
138 { ARG_STR
, offsetof(starter_conn_t
, aaa_identity
), NULL
},
139 { ARG_MISC
, 0, NULL
/* KW_MOBIKE */ },
140 { ARG_MISC
, 0, NULL
/* KW_FORCEENCAPS */ },
141 { ARG_MISC
, 0, NULL
/* KW_FRAGMENTATION */ },
142 { ARG_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
143 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
144 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
145 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_bytes
), NULL
},
146 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_bytes
), NULL
},
147 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_packets
), NULL
},
148 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_packets
), NULL
},
149 { ARG_MISC
, 0, NULL
/* KW_KEYINGTRIES */ },
150 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
151 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
152 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
153 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
154 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
155 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
156 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
157 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
158 { ARG_ENUM
, offsetof(starter_conn_t
, close_action
), LST_dpd_action
},
159 { ARG_TIME
, offsetof(starter_conn_t
, inactivity
), NULL
},
160 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
161 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
162 { ARG_STR
, offsetof(starter_conn_t
, xauth_identity
), NULL
},
163 { ARG_ENUM
, offsetof(starter_conn_t
, me_mediation
), LST_bool
},
164 { ARG_STR
, offsetof(starter_conn_t
, me_mediated_by
), NULL
},
165 { ARG_STR
, offsetof(starter_conn_t
, me_peerid
), NULL
},
166 { ARG_UINT
, offsetof(starter_conn_t
, reqid
), NULL
},
167 { ARG_MISC
, 0, NULL
/* KW_MARK */ },
168 { ARG_MISC
, 0, NULL
/* KW_MARK_IN */ },
169 { ARG_MISC
, 0, NULL
/* KW_MARK_OUT */ },
170 { ARG_MISC
, 0, NULL
/* KW_TFC */ },
171 { ARG_MISC
, 0, NULL
/* KW_PFS_DEPRECATED */ },
172 { ARG_MISC
, 0, NULL
/* KW_CONN_DEPRECATED */ },
174 /* ca section keywords */
175 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
176 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
177 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
178 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
179 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
180 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
181 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
182 { ARG_STR
, offsetof(starter_ca_t
, certuribase
), NULL
},
183 { ARG_MISC
, 0, NULL
/* KW_CA_DEPRECATED */ },
186 { ARG_STR
, offsetof(starter_end_t
, host
), NULL
},
187 { ARG_UINT
, offsetof(starter_end_t
, ikeport
), NULL
},
188 { ARG_STR
, offsetof(starter_end_t
, subnet
), NULL
},
189 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
190 { ARG_STR
, offsetof(starter_end_t
, sourceip
), NULL
},
191 { ARG_STR
, offsetof(starter_end_t
, dns
), NULL
},
192 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
193 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
194 { ARG_ENUM
, offsetof(starter_end_t
, allow_any
), LST_bool
},
195 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
196 { ARG_STR
, offsetof(starter_end_t
, auth
), NULL
},
197 { ARG_STR
, offsetof(starter_end_t
, auth2
), NULL
},
198 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
199 { ARG_STR
, offsetof(starter_end_t
, id2
), NULL
},
200 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
201 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
202 { ARG_STR
, offsetof(starter_end_t
, cert2
), NULL
},
203 { ARG_STR
, offsetof(starter_end_t
, cert_policy
), NULL
},
204 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
205 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
206 { ARG_STR
, offsetof(starter_end_t
, ca2
), NULL
},
207 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
208 { ARG_STR
, offsetof(starter_end_t
, groups2
), NULL
},
209 { ARG_MISC
, 0, NULL
/* KW_END_DEPRECATED */ },
212 static void free_list(char **list
)
216 for (s
= list
; *s
; s
++)
223 char** new_list(char *value
)
225 char *val
, *b
, *e
, *end
, **ret
;
228 val
= strdupnull(value
);
233 end
= val
+ strlen(val
);
234 for (b
= val
, count
= 0; b
< end
;)
236 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
249 ret
= (char **)malloc((count
+1) * sizeof(char *));
251 for (b
= val
, count
= 0; b
< end
; )
253 for (e
= b
; (*e
!= '\0'); e
++);
256 ret
[count
++] = strdupnull(b
);
267 * assigns an argument value to a struct field
269 bool assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
,
272 char *p
= base
+ token_info
[token
].offset
;
273 const char **list
= token_info
[token
].list
;
275 int index
= -1; /* used for enumeration arguments */
277 seen_t
*seen
= (seen_t
*)base
; /* seen flags are at the top of the struct */
281 DBG3(DBG_APP
, " %s=%s", kw
->entry
->name
, kw
->value
);
283 if (*seen
& SEEN_KW(token
, first
))
285 DBG1(DBG_APP
, "# duplicate '%s' option", kw
->entry
->name
);
289 /* set flag that this argument has been seen */
290 *seen
|= SEEN_KW(token
, first
);
292 /* is there a keyword list? */
293 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
297 while (*list
!= NULL
&& !match
)
300 match
= streq(kw
->value
, *list
++);
304 DBG1(DBG_APP
, "# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
309 switch (token_info
[token
].type
)
312 DBG1(DBG_APP
, "# option '%s' not supported yet", kw
->entry
->name
);
318 DBG1(DBG_APP
, "# bad enumeration value: %s=%s (%d)",
319 kw
->entry
->name
, kw
->value
, index
);
323 if (token_info
[token
].list
== LST_bool
)
339 u_int
*u
= (u_int
*)p
;
341 *u
= strtoul(kw
->value
, &endptr
, 10);
345 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
355 unsigned long *l
= (unsigned long *)p
;
357 *l
= strtoul(kw
->value
, &endptr
, 10);
359 if (token_info
[token
].type
== ARG_ULNG
)
363 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
370 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
372 DBG1(DBG_APP
, "# bad percent value: %s=%s", kw
->entry
->name
,
383 unsigned long long *ll
= (unsigned long long *)p
;
385 *ll
= strtoull(kw
->value
, &endptr
, 10);
389 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
398 time_t *t
= (time_t *)p
;
400 *t
= strtoul(kw
->value
, &endptr
, 10);
402 /* time in seconds? */
403 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
407 if (endptr
[1] == '\0')
409 if (*endptr
== 'm') /* time in minutes? */
414 if (*endptr
== 'h') /* time in hours? */
419 if (*endptr
== 'd') /* time in days? */
425 DBG1(DBG_APP
, "# bad duration value: %s=%s", kw
->entry
->name
,
431 char **cp
= (char **)p
;
433 /* free any existing string */
436 /* assign the new string */
437 *cp
= strdupnull(kw
->value
);
442 char ***listp
= (char ***)p
;
444 /* free any existing list */
449 /* create a new list and assign values */
450 *listp
= new_list(kw
->value
);
452 /* is there a keyword list? */
457 for (lst
= *listp
; lst
&& *lst
; lst
++)
461 list
= token_info
[token
].list
;
463 while (*list
!= NULL
&& !match
)
465 match
= streq(*lst
, *list
++);
469 DBG1(DBG_APP
, "# bad value: %s=%s",
470 kw
->entry
->name
, *lst
);
486 * frees all dynamically allocated arguments in a struct
488 void free_args(kw_token_t first
, kw_token_t last
, char *base
)
492 for (token
= first
; token
<= last
; token
++)
494 char *p
= base
+ token_info
[token
].offset
;
496 switch (token_info
[token
].type
)
500 char **cp
= (char **)p
;
508 char ***listp
= (char ***)p
;
524 * clone all dynamically allocated arguments in a struct
526 void clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
530 for (token
= first
; token
<= last
; token
++)
532 if (token_info
[token
].type
== ARG_STR
)
534 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
535 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
537 *cp1
= strdupnull(*cp2
);
542 static bool cmp_list(char **list1
, char **list2
)
544 if ((list1
== NULL
) && (list2
== NULL
))
548 if ((list1
== NULL
) || (list2
== NULL
))
553 for ( ; *list1
&& *list2
; list1
++, list2
++)
555 if (strcmp(*list1
,*list2
) != 0)
561 if ((*list1
!= NULL
) || (*list2
!= NULL
))
570 * compare all arguments in a struct
572 bool cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
576 for (token
= first
; token
<= last
; token
++)
578 char *p1
= base1
+ token_info
[token
].offset
;
579 char *p2
= base2
+ token_info
[token
].offset
;
581 switch (token_info
[token
].type
)
584 if (token_info
[token
].list
== LST_bool
)
586 bool *b1
= (bool *)p1
;
587 bool *b2
= (bool *)p2
;
607 u_int
*u1
= (u_int
*)p1
;
608 u_int
*u2
= (u_int
*)p2
;
619 unsigned long *l1
= (unsigned long *)p1
;
620 unsigned long *l2
= (unsigned long *)p2
;
630 unsigned long long *ll1
= (unsigned long long *)p1
;
631 unsigned long long *ll2
= (unsigned long long *)p2
;
641 time_t *t1
= (time_t *)p1
;
642 time_t *t2
= (time_t *)p2
;
652 char **cp1
= (char **)p1
;
653 char **cp2
= (char **)p2
;
655 if (*cp1
== NULL
&& *cp2
== NULL
)
659 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
667 char ***listp1
= (char ***)p1
;
668 char ***listp2
= (char ***)p2
;
670 if (!cmp_list(*listp1
, *listp2
))