]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/starter/args.c
390062a99eb63106ee9123fc25bf35ac893aa30b
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
[] = {
111 static const char *LST_fragmentation
[] = {
124 static const token_info_t token_info
[] =
126 /* config setup keywords */
127 { ARG_STR
, offsetof(starter_config_t
, setup
.charondebug
), NULL
},
128 { ARG_ENUM
, offsetof(starter_config_t
, setup
.uniqueids
), LST_unique
},
129 { ARG_ENUM
, offsetof(starter_config_t
, setup
.cachecrls
), LST_bool
},
130 { ARG_ENUM
, offsetof(starter_config_t
, setup
.strictcrlpolicy
), LST_strict
},
131 { ARG_MISC
, 0, NULL
/* KW_PKCS11_DEPRECATED */ },
132 { ARG_MISC
, 0, NULL
/* KW_SETUP_DEPRECATED */ },
134 /* conn section keywords */
135 { ARG_STR
, offsetof(starter_conn_t
, name
), NULL
},
136 { ARG_ENUM
, offsetof(starter_conn_t
, startup
), LST_startup
},
137 { ARG_ENUM
, offsetof(starter_conn_t
, keyexchange
), LST_keyexchange
},
138 { ARG_MISC
, 0, NULL
/* KW_TYPE */ },
139 { ARG_MISC
, 0, NULL
/* KW_COMPRESS */ },
140 { ARG_ENUM
, offsetof(starter_conn_t
, install_policy
), LST_bool
},
141 { ARG_ENUM
, offsetof(starter_conn_t
, aggressive
), LST_bool
},
142 { ARG_MISC
, 0, NULL
/* KW_AUTH */ },
143 { ARG_STR
, offsetof(starter_conn_t
, authby
), LST_authby
},
144 { ARG_STR
, offsetof(starter_conn_t
, eap_identity
), NULL
},
145 { ARG_STR
, offsetof(starter_conn_t
, aaa_identity
), NULL
},
146 { ARG_MISC
, 0, NULL
/* KW_MOBIKE */ },
147 { ARG_MISC
, 0, NULL
/* KW_FORCEENCAPS */ },
148 { ARG_ENUM
, offsetof(starter_conn_t
, fragmentation
), LST_fragmentation
},
149 { ARG_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
150 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
151 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
152 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_bytes
), NULL
},
153 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_bytes
), NULL
},
154 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_packets
), NULL
},
155 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_packets
), NULL
},
156 { ARG_MISC
, 0, NULL
/* KW_KEYINGTRIES */ },
157 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
158 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
159 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
160 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
161 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
162 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
163 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
164 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
165 { ARG_ENUM
, offsetof(starter_conn_t
, close_action
), LST_dpd_action
},
166 { ARG_TIME
, offsetof(starter_conn_t
, inactivity
), NULL
},
167 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
168 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
169 { ARG_STR
, offsetof(starter_conn_t
, xauth_identity
), NULL
},
170 { ARG_ENUM
, offsetof(starter_conn_t
, me_mediation
), LST_bool
},
171 { ARG_STR
, offsetof(starter_conn_t
, me_mediated_by
), NULL
},
172 { ARG_STR
, offsetof(starter_conn_t
, me_peerid
), NULL
},
173 { ARG_UINT
, offsetof(starter_conn_t
, reqid
), NULL
},
174 { ARG_MISC
, 0, NULL
/* KW_MARK */ },
175 { ARG_MISC
, 0, NULL
/* KW_MARK_IN */ },
176 { ARG_MISC
, 0, NULL
/* KW_MARK_OUT */ },
177 { ARG_MISC
, 0, NULL
/* KW_TFC */ },
178 { ARG_MISC
, 0, NULL
/* KW_PFS_DEPRECATED */ },
179 { ARG_MISC
, 0, NULL
/* KW_CONN_DEPRECATED */ },
181 /* ca section keywords */
182 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
183 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
184 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
185 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
186 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
187 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
188 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
189 { ARG_STR
, offsetof(starter_ca_t
, certuribase
), NULL
},
190 { ARG_MISC
, 0, NULL
/* KW_CA_DEPRECATED */ },
193 { ARG_STR
, offsetof(starter_end_t
, host
), NULL
},
194 { ARG_UINT
, offsetof(starter_end_t
, ikeport
), NULL
},
195 { ARG_STR
, offsetof(starter_end_t
, subnet
), NULL
},
196 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
197 { ARG_STR
, offsetof(starter_end_t
, sourceip
), NULL
},
198 { ARG_STR
, offsetof(starter_end_t
, dns
), NULL
},
199 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
200 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
201 { ARG_ENUM
, offsetof(starter_end_t
, allow_any
), LST_bool
},
202 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
203 { ARG_STR
, offsetof(starter_end_t
, auth
), NULL
},
204 { ARG_STR
, offsetof(starter_end_t
, auth2
), NULL
},
205 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
206 { ARG_STR
, offsetof(starter_end_t
, id2
), NULL
},
207 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
208 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
209 { ARG_STR
, offsetof(starter_end_t
, cert2
), NULL
},
210 { ARG_STR
, offsetof(starter_end_t
, cert_policy
), NULL
},
211 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
212 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
213 { ARG_STR
, offsetof(starter_end_t
, ca2
), NULL
},
214 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
215 { ARG_STR
, offsetof(starter_end_t
, groups2
), NULL
},
216 { ARG_MISC
, 0, NULL
/* KW_END_DEPRECATED */ },
219 static void free_list(char **list
)
223 for (s
= list
; *s
; s
++)
230 char** new_list(char *value
)
232 char *val
, *b
, *e
, *end
, **ret
;
235 val
= strdupnull(value
);
240 end
= val
+ strlen(val
);
241 for (b
= val
, count
= 0; b
< end
;)
243 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
256 ret
= (char **)malloc((count
+1) * sizeof(char *));
258 for (b
= val
, count
= 0; b
< end
; )
260 for (e
= b
; (*e
!= '\0'); e
++);
263 ret
[count
++] = strdupnull(b
);
274 * assigns an argument value to a struct field
276 bool assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
,
279 char *p
= base
+ token_info
[token
].offset
;
280 const char **list
= token_info
[token
].list
;
282 int index
= -1; /* used for enumeration arguments */
284 seen_t
*seen
= (seen_t
*)base
; /* seen flags are at the top of the struct */
288 DBG3(DBG_APP
, " %s=%s", kw
->entry
->name
, kw
->value
);
290 if (*seen
& SEEN_KW(token
, first
))
292 DBG1(DBG_APP
, "# duplicate '%s' option", kw
->entry
->name
);
296 /* set flag that this argument has been seen */
297 *seen
|= SEEN_KW(token
, first
);
299 /* is there a keyword list? */
300 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
304 while (*list
!= NULL
&& !match
)
307 match
= streq(kw
->value
, *list
++);
311 DBG1(DBG_APP
, "# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
316 switch (token_info
[token
].type
)
319 DBG1(DBG_APP
, "# option '%s' not supported yet", kw
->entry
->name
);
325 DBG1(DBG_APP
, "# bad enumeration value: %s=%s (%d)",
326 kw
->entry
->name
, kw
->value
, index
);
330 if (token_info
[token
].list
== LST_bool
)
346 u_int
*u
= (u_int
*)p
;
348 *u
= strtoul(kw
->value
, &endptr
, 10);
352 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
362 unsigned long *l
= (unsigned long *)p
;
364 *l
= strtoul(kw
->value
, &endptr
, 10);
366 if (token_info
[token
].type
== ARG_ULNG
)
370 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
377 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
379 DBG1(DBG_APP
, "# bad percent value: %s=%s", kw
->entry
->name
,
390 unsigned long long *ll
= (unsigned long long *)p
;
392 *ll
= strtoull(kw
->value
, &endptr
, 10);
396 DBG1(DBG_APP
, "# bad integer value: %s=%s", kw
->entry
->name
,
405 time_t *t
= (time_t *)p
;
407 *t
= strtoul(kw
->value
, &endptr
, 10);
409 /* time in seconds? */
410 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
414 if (endptr
[1] == '\0')
416 if (*endptr
== 'm') /* time in minutes? */
421 if (*endptr
== 'h') /* time in hours? */
426 if (*endptr
== 'd') /* time in days? */
432 DBG1(DBG_APP
, "# bad duration value: %s=%s", kw
->entry
->name
,
438 char **cp
= (char **)p
;
440 /* free any existing string */
443 /* assign the new string */
444 *cp
= strdupnull(kw
->value
);
449 char ***listp
= (char ***)p
;
451 /* free any existing list */
456 /* create a new list and assign values */
457 *listp
= new_list(kw
->value
);
459 /* is there a keyword list? */
464 for (lst
= *listp
; lst
&& *lst
; lst
++)
468 list
= token_info
[token
].list
;
470 while (*list
!= NULL
&& !match
)
472 match
= streq(*lst
, *list
++);
476 DBG1(DBG_APP
, "# bad value: %s=%s",
477 kw
->entry
->name
, *lst
);
493 * frees all dynamically allocated arguments in a struct
495 void free_args(kw_token_t first
, kw_token_t last
, char *base
)
499 for (token
= first
; token
<= last
; token
++)
501 char *p
= base
+ token_info
[token
].offset
;
503 switch (token_info
[token
].type
)
507 char **cp
= (char **)p
;
515 char ***listp
= (char ***)p
;
531 * clone all dynamically allocated arguments in a struct
533 void clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
537 for (token
= first
; token
<= last
; token
++)
539 if (token_info
[token
].type
== ARG_STR
)
541 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
542 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
544 *cp1
= strdupnull(*cp2
);
549 static bool cmp_list(char **list1
, char **list2
)
551 if ((list1
== NULL
) && (list2
== NULL
))
555 if ((list1
== NULL
) || (list2
== NULL
))
560 for ( ; *list1
&& *list2
; list1
++, list2
++)
562 if (strcmp(*list1
,*list2
) != 0)
568 if ((*list1
!= NULL
) || (*list2
!= NULL
))
577 * compare all arguments in a struct
579 bool cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
583 for (token
= first
; token
<= last
; token
++)
585 char *p1
= base1
+ token_info
[token
].offset
;
586 char *p2
= base2
+ token_info
[token
].offset
;
588 switch (token_info
[token
].type
)
591 if (token_info
[token
].list
== LST_bool
)
593 bool *b1
= (bool *)p1
;
594 bool *b2
= (bool *)p2
;
614 u_int
*u1
= (u_int
*)p1
;
615 u_int
*u2
= (u_int
*)p2
;
626 unsigned long *l1
= (unsigned long *)p1
;
627 unsigned long *l2
= (unsigned long *)p2
;
637 unsigned long long *ll1
= (unsigned long long *)p1
;
638 unsigned long long *ll2
= (unsigned long long *)p2
;
648 time_t *t1
= (time_t *)p1
;
649 time_t *t2
= (time_t *)p2
;
659 char **cp1
= (char **)p1
;
660 char **cp2
= (char **)p2
;
662 if (*cp1
== NULL
&& *cp2
== NULL
)
666 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
674 char ***listp1
= (char ***)p1
;
675 char ***listp2
= (char ***)p2
;
677 if (!cmp_list(*listp1
, *listp2
))