]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/starter/args.c
26f3aaef44f9a7525392856a25098beb027b7bc3
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
22 #include "../pluto/constants.h"
23 #include "../pluto/defs.h"
24 #include "../pluto/log.h"
46 /* various keyword lists */
48 static const char *LST_bool
[] = {
54 static const char *LST_sendcert
[] = {
63 static const char *LST_unique
[] = {
71 static const char *LST_strict
[] = {
77 static const char *LST_dpd_action
[] = {
85 static const char *LST_startup
[] = {
93 static const char *LST_packetdefault
[] = {
100 static const char *LST_keyexchange
[] = {
107 static const char *LST_pfsgroup
[] = {
123 static const char *LST_plutodebug
[] = {
141 static const char *LST_klipsdebug
[] = {
164 static const token_info_t token_info
[] =
166 /* config setup keywords */
167 { ARG_LST
, offsetof(starter_config_t
, setup
.interfaces
), NULL
},
168 { ARG_STR
, offsetof(starter_config_t
, setup
.dumpdir
), NULL
},
169 { ARG_ENUM
, offsetof(starter_config_t
, setup
.charonstart
), LST_bool
},
170 { ARG_ENUM
, offsetof(starter_config_t
, setup
.plutostart
), LST_bool
},
172 /* pluto/charon keywords */
173 { ARG_LST
, offsetof(starter_config_t
, setup
.plutodebug
), LST_plutodebug
},
174 { ARG_STR
, offsetof(starter_config_t
, setup
.charondebug
), NULL
},
175 { ARG_STR
, offsetof(starter_config_t
, setup
.prepluto
), NULL
},
176 { ARG_STR
, offsetof(starter_config_t
, setup
.postpluto
), NULL
},
177 { ARG_STR
, offsetof(starter_config_t
, setup
.plutostderrlog
), NULL
},
178 { ARG_ENUM
, offsetof(starter_config_t
, setup
.uniqueids
), LST_unique
},
179 { ARG_UINT
, offsetof(starter_config_t
, setup
.overridemtu
), NULL
},
180 { ARG_TIME
, offsetof(starter_config_t
, setup
.crlcheckinterval
), NULL
},
181 { ARG_ENUM
, offsetof(starter_config_t
, setup
.cachecrls
), LST_bool
},
182 { ARG_ENUM
, offsetof(starter_config_t
, setup
.strictcrlpolicy
), LST_strict
},
183 { ARG_ENUM
, offsetof(starter_config_t
, setup
.nocrsend
), LST_bool
},
184 { ARG_ENUM
, offsetof(starter_config_t
, setup
.nat_traversal
), LST_bool
},
185 { ARG_TIME
, offsetof(starter_config_t
, setup
.keep_alive
), NULL
},
186 { ARG_ENUM
, offsetof(starter_config_t
, setup
.force_keepalive
), LST_bool
},
187 { ARG_STR
, offsetof(starter_config_t
, setup
.virtual_private
), NULL
},
188 { ARG_STR
, offsetof(starter_config_t
, setup
.pkcs11module
), NULL
},
189 { ARG_STR
, offsetof(starter_config_t
, setup
.pkcs11initargs
), NULL
},
190 { ARG_ENUM
, offsetof(starter_config_t
, setup
.pkcs11keepstate
), LST_bool
},
191 { ARG_ENUM
, offsetof(starter_config_t
, setup
.pkcs11proxy
), LST_bool
},
194 { ARG_LST
, offsetof(starter_config_t
, setup
.klipsdebug
), LST_klipsdebug
},
195 { ARG_ENUM
, offsetof(starter_config_t
, setup
.fragicmp
), LST_bool
},
196 { ARG_STR
, offsetof(starter_config_t
, setup
.packetdefault
), LST_packetdefault
},
197 { ARG_ENUM
, offsetof(starter_config_t
, setup
.hidetos
), LST_bool
},
199 /* conn section keywords */
200 { ARG_STR
, offsetof(starter_conn_t
, name
), NULL
},
201 { ARG_ENUM
, offsetof(starter_conn_t
, startup
), LST_startup
},
202 { ARG_ENUM
, offsetof(starter_conn_t
, keyexchange
), LST_keyexchange
},
203 { ARG_MISC
, 0, NULL
/* KW_TYPE */ },
204 { ARG_MISC
, 0, NULL
/* KW_PFS */ },
205 { ARG_MISC
, 0, NULL
/* KW_COMPRESS */ },
206 { ARG_ENUM
, offsetof(starter_conn_t
, install_policy
), LST_bool
},
207 { ARG_MISC
, 0, NULL
/* KW_AUTH */ },
208 { ARG_MISC
, 0, NULL
/* KW_AUTHBY */ },
209 { ARG_MISC
, 0, NULL
/* KW_EAP */ },
210 { ARG_STR
, offsetof(starter_conn_t
, eap_identity
), NULL
},
211 { ARG_MISC
, 0, NULL
/* KW_MOBIKE */ },
212 { ARG_MISC
, 0, NULL
/* KW_FORCEENCAPS */ },
213 { ARG_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
214 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
215 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
216 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_bytes
), NULL
},
217 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_bytes
), NULL
},
218 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_packets
), NULL
},
219 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_packets
), NULL
},
220 { ARG_MISC
, 0, NULL
/* KW_KEYINGTRIES */ },
221 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
222 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
223 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
224 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
225 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
226 { ARG_STR
, offsetof(starter_conn_t
, pfsgroup
), LST_pfsgroup
},
227 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
228 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
229 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
230 { ARG_TIME
, offsetof(starter_conn_t
, inactivity
), NULL
},
231 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
232 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
233 { ARG_STR
, offsetof(starter_conn_t
, xauth_identity
), NULL
},
234 { ARG_ENUM
, offsetof(starter_conn_t
, me_mediation
), LST_bool
},
235 { ARG_STR
, offsetof(starter_conn_t
, me_mediated_by
), NULL
},
236 { ARG_STR
, offsetof(starter_conn_t
, me_peerid
), NULL
},
237 { ARG_UINT
, offsetof(starter_conn_t
, reqid
), NULL
},
239 /* ca section keywords */
240 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
241 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
242 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
243 { ARG_STR
, offsetof(starter_ca_t
, ldaphost
), NULL
},
244 { ARG_STR
, offsetof(starter_ca_t
, ldapbase
), NULL
},
245 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
246 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
247 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
248 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
249 { ARG_STR
, offsetof(starter_ca_t
, certuribase
), NULL
},
252 { ARG_MISC
, 0, NULL
/* KW_HOST */ },
253 { ARG_UINT
, offsetof(starter_end_t
, ikeport
), NULL
},
254 { ARG_MISC
, 0, NULL
/* KW_NEXTHOP */ },
255 { ARG_STR
, offsetof(starter_end_t
, subnet
), NULL
},
256 { ARG_MISC
, 0, NULL
/* KW_SUBNETWITHIN */ },
257 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
258 { ARG_STR
, offsetof(starter_end_t
, sourceip
), NULL
},
259 { ARG_MISC
, 0, NULL
/* KW_NATIP */ },
260 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
261 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
262 { ARG_ENUM
, offsetof(starter_end_t
, allow_any
), LST_bool
},
263 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
264 { ARG_STR
, offsetof(starter_end_t
, auth
), NULL
},
265 { ARG_STR
, offsetof(starter_end_t
, auth2
), NULL
},
266 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
267 { ARG_STR
, offsetof(starter_end_t
, id2
), NULL
},
268 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
269 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
270 { ARG_STR
, offsetof(starter_end_t
, cert2
), NULL
},
271 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
272 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
273 { ARG_STR
, offsetof(starter_end_t
, ca2
), NULL
},
274 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
275 { ARG_STR
, offsetof(starter_end_t
, iface
), NULL
}
278 static void free_list(char **list
)
282 for (s
= list
; *s
; s
++)
289 char** new_list(char *value
)
291 char *val
, *b
, *e
, *end
, **ret
;
294 val
= value
? clone_str(value
) : NULL
;
299 end
= val
+ strlen(val
);
300 for (b
= val
, count
= 0; b
< end
;)
302 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
315 ret
= (char **)malloc((count
+1) * sizeof(char *));
317 for (b
= val
, count
= 0; b
< end
; )
319 for (e
= b
; (*e
!= '\0'); e
++);
322 ret
[count
++] = clone_str(b
);
333 * assigns an argument value to a struct field
335 bool assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
,
338 char *p
= base
+ token_info
[token
].offset
;
339 const char **list
= token_info
[token
].list
;
341 int index
= -1; /* used for enumeration arguments */
343 lset_t
*seen
= (lset_t
*)base
; /* seen flags are at the top of the struct */
344 lset_t f
= LELEM(token
- first
); /* compute flag position of argument */
349 DBG_log(" %s=%s", kw
->entry
->name
, kw
->value
)
354 plog("# duplicate '%s' option", kw
->entry
->name
);
358 /* set flag that this argument has been seen */
361 /* is there a keyword list? */
362 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
366 while (*list
!= NULL
&& !match
)
369 match
= streq(kw
->value
, *list
++);
373 plog("# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
378 switch (token_info
[token
].type
)
381 plog("# option '%s' not supported yet", kw
->entry
->name
);
387 plog("# bad enumeration value: %s=%s (%d)"
388 , kw
->entry
->name
, kw
->value
, index
);
392 if (token_info
[token
].list
== LST_bool
)
408 u_int
*u
= (u_int
*)p
;
410 *u
= strtoul(kw
->value
, &endptr
, 10);
414 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
423 unsigned long *l
= (unsigned long *)p
;
425 *l
= strtoul(kw
->value
, &endptr
, 10);
427 if (token_info
[token
].type
== ARG_ULNG
)
431 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
437 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
439 plog("# bad percent value: %s=%s", kw
->entry
->name
, kw
->value
);
449 unsigned long long *ll
= (unsigned long long *)p
;
451 *ll
= strtoull(kw
->value
, &endptr
, 10);
455 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
463 time_t *t
= (time_t *)p
;
465 *t
= strtoul(kw
->value
, &endptr
, 10);
467 /* time in seconds? */
468 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
472 if (endptr
[1] == '\0')
474 if (*endptr
== 'm') /* time in minutes? */
479 if (*endptr
== 'h') /* time in hours? */
484 if (*endptr
== 'd') /* time in days? */
490 plog("# bad duration value: %s=%s", kw
->entry
->name
, kw
->value
);
495 char **cp
= (char **)p
;
497 /* free any existing string */
500 /* assign the new string */
501 *cp
= clone_str(kw
->value
);
506 char ***listp
= (char ***)p
;
508 /* free any existing list */
513 /* create a new list and assign values */
514 *listp
= new_list(kw
->value
);
516 /* is there a keyword list? */
521 for (lst
= *listp
; lst
&& *lst
; lst
++)
525 list
= token_info
[token
].list
;
527 while (*list
!= NULL
&& !match
)
529 match
= streq(*lst
, *list
++);
533 plog("# bad value: %s=%s", kw
->entry
->name
, *lst
);
548 * frees all dynamically allocated arguments in a struct
550 void free_args(kw_token_t first
, kw_token_t last
, char *base
)
554 for (token
= first
; token
<= last
; token
++)
556 char *p
= base
+ token_info
[token
].offset
;
558 switch (token_info
[token
].type
)
562 char **cp
= (char **)p
;
570 char ***listp
= (char ***)p
;
586 * clone all dynamically allocated arguments in a struct
588 void clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
592 for (token
= first
; token
<= last
; token
++)
594 if (token_info
[token
].type
== ARG_STR
)
596 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
597 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
599 *cp1
= clone_str(*cp2
);
604 static bool cmp_list(char **list1
, char **list2
)
606 if ((list1
== NULL
) && (list2
== NULL
))
610 if ((list1
== NULL
) || (list2
== NULL
))
615 for ( ; *list1
&& *list2
; list1
++, list2
++)
617 if (strcmp(*list1
,*list2
) != 0)
623 if ((*list1
!= NULL
) || (*list2
!= NULL
))
632 * compare all arguments in a struct
634 bool cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
638 for (token
= first
; token
<= last
; token
++)
640 char *p1
= base1
+ token_info
[token
].offset
;
641 char *p2
= base2
+ token_info
[token
].offset
;
643 switch (token_info
[token
].type
)
646 if (token_info
[token
].list
== LST_bool
)
648 bool *b1
= (bool *)p1
;
649 bool *b2
= (bool *)p2
;
669 u_int
*u1
= (u_int
*)p1
;
670 u_int
*u2
= (u_int
*)p2
;
681 unsigned long *l1
= (unsigned long *)p1
;
682 unsigned long *l2
= (unsigned long *)p2
;
692 unsigned long long *ll1
= (unsigned long long *)p1
;
693 unsigned long long *ll2
= (unsigned long long *)p2
;
703 time_t *t1
= (time_t *)p1
;
704 time_t *t2
= (time_t *)p2
;
714 char **cp1
= (char **)p1
;
715 char **cp2
= (char **)p2
;
717 if (*cp1
== NULL
&& *cp2
== NULL
)
721 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
729 char ***listp1
= (char ***)p1
;
730 char ***listp2
= (char ***)p2
;
732 if (!cmp_list(*listp1
, *listp2
))