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
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_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
142 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
143 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
144 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_bytes
), NULL
},
145 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_bytes
), NULL
},
146 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_packets
), NULL
},
147 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_packets
), NULL
},
148 { ARG_MISC
, 0, NULL
/* KW_KEYINGTRIES */ },
149 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
150 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
151 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
152 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
153 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
154 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
155 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
156 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
157 { ARG_ENUM
, offsetof(starter_conn_t
, close_action
), LST_dpd_action
},
158 { ARG_TIME
, offsetof(starter_conn_t
, inactivity
), NULL
},
159 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
160 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
161 { ARG_STR
, offsetof(starter_conn_t
, xauth_identity
), NULL
},
162 { ARG_ENUM
, offsetof(starter_conn_t
, me_mediation
), LST_bool
},
163 { ARG_STR
, offsetof(starter_conn_t
, me_mediated_by
), NULL
},
164 { ARG_STR
, offsetof(starter_conn_t
, me_peerid
), NULL
},
165 { ARG_UINT
, offsetof(starter_conn_t
, reqid
), NULL
},
166 { ARG_MISC
, 0, NULL
/* KW_MARK */ },
167 { ARG_MISC
, 0, NULL
/* KW_MARK_IN */ },
168 { ARG_MISC
, 0, NULL
/* KW_MARK_OUT */ },
169 { ARG_MISC
, 0, NULL
/* KW_TFC */ },
170 { ARG_MISC
, 0, NULL
/* KW_PFS_DEPRECATED */ },
171 { ARG_MISC
, 0, NULL
/* KW_CONN_DEPRECATED */ },
173 /* ca section keywords */
174 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
175 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
176 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
177 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
178 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
179 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
180 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
181 { ARG_STR
, offsetof(starter_ca_t
, certuribase
), NULL
},
182 { ARG_MISC
, 0, NULL
/* KW_CA_DEPRECATED */ },
185 { ARG_STR
, offsetof(starter_end_t
, host
), NULL
},
186 { ARG_UINT
, offsetof(starter_end_t
, ikeport
), NULL
},
187 { ARG_STR
, offsetof(starter_end_t
, subnet
), NULL
},
188 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
189 { ARG_STR
, offsetof(starter_end_t
, sourceip
), NULL
},
190 { ARG_STR
, offsetof(starter_end_t
, dns
), NULL
},
191 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
192 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
193 { ARG_ENUM
, offsetof(starter_end_t
, allow_any
), LST_bool
},
194 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
195 { ARG_STR
, offsetof(starter_end_t
, auth
), NULL
},
196 { ARG_STR
, offsetof(starter_end_t
, auth2
), NULL
},
197 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
198 { ARG_STR
, offsetof(starter_end_t
, id2
), NULL
},
199 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
200 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
201 { ARG_STR
, offsetof(starter_end_t
, cert2
), NULL
},
202 { ARG_STR
, offsetof(starter_end_t
, cert_policy
), NULL
},
203 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
204 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
205 { ARG_STR
, offsetof(starter_end_t
, ca2
), NULL
},
206 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
207 { ARG_STR
, offsetof(starter_end_t
, groups2
), NULL
},
208 { ARG_MISC
, 0, NULL
/* KW_END_DEPRECATED */ },
211 static void free_list(char **list
)
215 for (s
= list
; *s
; s
++)
222 char** new_list(char *value
)
224 char *val
, *b
, *e
, *end
, **ret
;
227 val
= strdupnull(value
);
232 end
= val
+ strlen(val
);
233 for (b
= val
, count
= 0; b
< end
;)
235 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
248 ret
= (char **)malloc((count
+1) * sizeof(char *));
250 for (b
= val
, count
= 0; b
< end
; )
252 for (e
= b
; (*e
!= '\0'); e
++);
255 ret
[count
++] = strdupnull(b
);
266 * assigns an argument value to a struct field
268 bool assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
,
271 char *p
= base
+ token_info
[token
].offset
;
272 const char **list
= token_info
[token
].list
;
274 int index
= -1; /* used for enumeration arguments */
276 seen_t
*seen
= (seen_t
*)base
; /* seen flags are at the top of the struct */
280 DBG3(DBG_APP
, " %s=%s", kw
->entry
->name
, kw
->value
);
282 if (*seen
& SEEN_KW(token
, first
))
284 DBG1(DBG_APP
, "# duplicate '%s' option", kw
->entry
->name
);
288 /* set flag that this argument has been seen */
289 *seen
|= SEEN_KW(token
, first
);
291 /* is there a keyword list? */
292 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
296 while (*list
!= NULL
&& !match
)
299 match
= streq(kw
->value
, *list
++);
303 DBG1(DBG_APP
, "# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
308 switch (token_info
[token
].type
)
311 DBG1(DBG_APP
, "# option '%s' not supported yet", kw
->entry
->name
);
317 DBG1(DBG_APP
, "# bad enumeration value: %s=%s (%d)",
318 kw
->entry
->name
, kw
->value
, index
);
322 if (token_info
[token
].list
== LST_bool
)
338 u_int
*u
= (u_int
*)p
;
340 *u
= strtoul(kw
->value
, &endptr
, 10);
344 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
354 unsigned long *l
= (unsigned long *)p
;
356 *l
= strtoul(kw
->value
, &endptr
, 10);
358 if (token_info
[token
].type
== ARG_ULNG
)
362 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
369 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
371 DBG1(DBG_APP
, "# bad percent value: %s=%s", kw
->entry
->name
,
382 unsigned long long *ll
= (unsigned long long *)p
;
384 *ll
= strtoull(kw
->value
, &endptr
, 10);
388 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
397 time_t *t
= (time_t *)p
;
399 *t
= strtoul(kw
->value
, &endptr
, 10);
401 /* time in seconds? */
402 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
406 if (endptr
[1] == '\0')
408 if (*endptr
== 'm') /* time in minutes? */
413 if (*endptr
== 'h') /* time in hours? */
418 if (*endptr
== 'd') /* time in days? */
424 DBG1(DBG_APP
, "# bad duration value: %s=%s", kw
->entry
->name
,
430 char **cp
= (char **)p
;
432 /* free any existing string */
435 /* assign the new string */
436 *cp
= strdupnull(kw
->value
);
441 char ***listp
= (char ***)p
;
443 /* free any existing list */
448 /* create a new list and assign values */
449 *listp
= new_list(kw
->value
);
451 /* is there a keyword list? */
456 for (lst
= *listp
; lst
&& *lst
; lst
++)
460 list
= token_info
[token
].list
;
462 while (*list
!= NULL
&& !match
)
464 match
= streq(*lst
, *list
++);
468 DBG1(DBG_APP
, "# bad value: %s=%s",
469 kw
->entry
->name
, *lst
);
485 * frees all dynamically allocated arguments in a struct
487 void free_args(kw_token_t first
, kw_token_t last
, char *base
)
491 for (token
= first
; token
<= last
; token
++)
493 char *p
= base
+ token_info
[token
].offset
;
495 switch (token_info
[token
].type
)
499 char **cp
= (char **)p
;
507 char ***listp
= (char ***)p
;
523 * clone all dynamically allocated arguments in a struct
525 void clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
529 for (token
= first
; token
<= last
; token
++)
531 if (token_info
[token
].type
== ARG_STR
)
533 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
534 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
536 *cp1
= strdupnull(*cp2
);
541 static bool cmp_list(char **list1
, char **list2
)
543 if ((list1
== NULL
) && (list2
== NULL
))
547 if ((list1
== NULL
) || (list2
== NULL
))
552 for ( ; *list1
&& *list2
; list1
++, list2
++)
554 if (strcmp(*list1
,*list2
) != 0)
560 if ((*list1
!= NULL
) || (*list2
!= NULL
))
569 * compare all arguments in a struct
571 bool cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
575 for (token
= first
; token
<= last
; token
++)
577 char *p1
= base1
+ token_info
[token
].offset
;
578 char *p2
= base2
+ token_info
[token
].offset
;
580 switch (token_info
[token
].type
)
583 if (token_info
[token
].list
== LST_bool
)
585 bool *b1
= (bool *)p1
;
586 bool *b2
= (bool *)p2
;
606 u_int
*u1
= (u_int
*)p1
;
607 u_int
*u2
= (u_int
*)p2
;
618 unsigned long *l1
= (unsigned long *)p1
;
619 unsigned long *l2
= (unsigned long *)p2
;
629 unsigned long long *ll1
= (unsigned long long *)p1
;
630 unsigned long long *ll2
= (unsigned long long *)p2
;
640 time_t *t1
= (time_t *)p1
;
641 time_t *t2
= (time_t *)p2
;
651 char **cp1
= (char **)p1
;
652 char **cp2
= (char **)p2
;
654 if (*cp1
== NULL
&& *cp2
== NULL
)
658 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
666 char ***listp1
= (char ***)p1
;
667 char ***listp2
= (char ***)p2
;
669 if (!cmp_list(*listp1
, *listp2
))