0699eb058f2d5d0aeab76c25a264a1198ba57353
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"
45 /* various keyword lists */
47 static const char *LST_bool
[] = {
53 static const char *LST_sendcert
[] = {
62 static const char *LST_unique
[] = {
70 static const char *LST_strict
[] = {
76 static const char *LST_dpd_action
[] = {
84 static const char *LST_startup
[] = {
92 static const char *LST_packetdefault
[] = {
99 static const char *LST_keyexchange
[] = {
106 static const char *LST_pfsgroup
[] = {
122 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_ENUM
, offsetof(starter_conn_t
, aggressive
), LST_bool
},
208 { ARG_MISC
, 0, NULL
/* KW_AUTH */ },
209 { ARG_MISC
, 0, NULL
/* KW_AUTHBY */ },
210 { ARG_STR
, offsetof(starter_conn_t
, eap_identity
), NULL
},
211 { ARG_STR
, offsetof(starter_conn_t
, aaa_identity
), NULL
},
212 { ARG_MISC
, 0, NULL
/* KW_MOBIKE */ },
213 { ARG_MISC
, 0, NULL
/* KW_FORCEENCAPS */ },
214 { ARG_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
215 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
216 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
217 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_bytes
), NULL
},
218 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_bytes
), NULL
},
219 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_life_packets
), NULL
},
220 { ARG_ULLI
, offsetof(starter_conn_t
, sa_ipsec_margin_packets
), NULL
},
221 { ARG_MISC
, 0, NULL
/* KW_KEYINGTRIES */ },
222 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
223 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
224 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
225 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
226 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
227 { ARG_STR
, offsetof(starter_conn_t
, pfsgroup
), LST_pfsgroup
},
228 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
229 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
230 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
231 { ARG_ENUM
, offsetof(starter_conn_t
, close_action
), LST_dpd_action
},
232 { ARG_TIME
, offsetof(starter_conn_t
, inactivity
), NULL
},
233 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
234 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
235 { ARG_STR
, offsetof(starter_conn_t
, xauth_identity
), NULL
},
236 { ARG_ENUM
, offsetof(starter_conn_t
, me_mediation
), LST_bool
},
237 { ARG_STR
, offsetof(starter_conn_t
, me_mediated_by
), NULL
},
238 { ARG_STR
, offsetof(starter_conn_t
, me_peerid
), NULL
},
239 { ARG_UINT
, offsetof(starter_conn_t
, reqid
), NULL
},
240 { ARG_MISC
, 0, NULL
/* KW_MARK */ },
241 { ARG_MISC
, 0, NULL
/* KW_MARK_IN */ },
242 { ARG_MISC
, 0, NULL
/* KW_MARK_OUT */ },
243 { ARG_MISC
, 0, NULL
/* KW_TFC */ },
245 /* ca section keywords */
246 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
247 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
248 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
249 { ARG_STR
, offsetof(starter_ca_t
, ldaphost
), NULL
},
250 { ARG_STR
, offsetof(starter_ca_t
, ldapbase
), NULL
},
251 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
252 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
253 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
254 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
255 { ARG_STR
, offsetof(starter_ca_t
, certuribase
), NULL
},
258 { ARG_STR
, offsetof(starter_end_t
, host
), NULL
},
259 { ARG_UINT
, offsetof(starter_end_t
, ikeport
), NULL
},
260 { ARG_MISC
, 0, NULL
/* KW_NEXTHOP */ },
261 { ARG_STR
, offsetof(starter_end_t
, subnet
), NULL
},
262 { ARG_MISC
, 0, NULL
/* KW_SUBNETWITHIN */ },
263 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
264 { ARG_STR
, offsetof(starter_end_t
, sourceip
), NULL
},
265 { ARG_MISC
, 0, NULL
/* KW_NATIP */ },
266 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
267 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
268 { ARG_ENUM
, offsetof(starter_end_t
, allow_any
), LST_bool
},
269 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
270 { ARG_STR
, offsetof(starter_end_t
, auth
), NULL
},
271 { ARG_STR
, offsetof(starter_end_t
, auth2
), NULL
},
272 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
273 { ARG_STR
, offsetof(starter_end_t
, id2
), NULL
},
274 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
275 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
276 { ARG_STR
, offsetof(starter_end_t
, cert2
), NULL
},
277 { ARG_STR
, offsetof(starter_end_t
, cert_policy
), NULL
},
278 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
279 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
280 { ARG_STR
, offsetof(starter_end_t
, ca2
), NULL
},
281 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
282 { ARG_STR
, offsetof(starter_end_t
, iface
), NULL
}
285 static void free_list(char **list
)
289 for (s
= list
; *s
; s
++)
296 char** new_list(char *value
)
298 char *val
, *b
, *e
, *end
, **ret
;
301 val
= value ?
clone_str(value
) : NULL
;
306 end
= val
+ strlen(val
);
307 for (b
= val
, count
= 0; b
< end
;)
309 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
322 ret
= (char **)malloc((count
+1) * sizeof(char *));
324 for (b
= val
, count
= 0; b
< end
; )
326 for (e
= b
; (*e
!= '\0'); e
++);
329 ret
[count
++] = clone_str(b
);
340 * assigns an argument value to a struct field
342 bool assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
,
345 char *p
= base
+ token_info
[token
].offset
;
346 const char **list
= token_info
[token
].list
;
348 int index
= -1; /* used for enumeration arguments */
350 lset_t
*seen
= (lset_t
*)base
; /* seen flags are at the top of the struct */
351 lset_t f
= LELEM(token
- first
); /* compute flag position of argument */
356 DBG_log(" %s=%s", kw
->entry
->name
, kw
->value
)
361 plog("# duplicate '%s' option", kw
->entry
->name
);
365 /* set flag that this argument has been seen */
368 /* is there a keyword list? */
369 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
373 while (*list
!= NULL
&& !match
)
376 match
= streq(kw
->value
, *list
++);
380 plog("# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
385 switch (token_info
[token
].type
)
388 plog("# option '%s' not supported yet", kw
->entry
->name
);
394 plog("# bad enumeration value: %s=%s (%d)"
395 , kw
->entry
->name
, kw
->value
, index
);
399 if (token_info
[token
].list
== LST_bool
)
415 u_int
*u
= (u_int
*)p
;
417 *u
= strtoul(kw
->value
, &endptr
, 10);
421 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
430 unsigned long *l
= (unsigned long *)p
;
432 *l
= strtoul(kw
->value
, &endptr
, 10);
434 if (token_info
[token
].type
== ARG_ULNG
)
438 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
444 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
446 plog("# bad percent value: %s=%s", kw
->entry
->name
, kw
->value
);
456 unsigned long long *ll
= (unsigned long long *)p
;
458 *ll
= strtoull(kw
->value
, &endptr
, 10);
462 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
470 time_t *t
= (time_t *)p
;
472 *t
= strtoul(kw
->value
, &endptr
, 10);
474 /* time in seconds? */
475 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
479 if (endptr
[1] == '\0')
481 if (*endptr
== 'm') /* time in minutes? */
486 if (*endptr
== 'h') /* time in hours? */
491 if (*endptr
== 'd') /* time in days? */
497 plog("# bad duration value: %s=%s", kw
->entry
->name
, kw
->value
);
502 char **cp
= (char **)p
;
504 /* free any existing string */
507 /* assign the new string */
508 *cp
= clone_str(kw
->value
);
513 char ***listp
= (char ***)p
;
515 /* free any existing list */
520 /* create a new list and assign values */
521 *listp
= new_list(kw
->value
);
523 /* is there a keyword list? */
528 for (lst
= *listp
; lst
&& *lst
; lst
++)
532 list
= token_info
[token
].list
;
534 while (*list
!= NULL
&& !match
)
536 match
= streq(*lst
, *list
++);
540 plog("# bad value: %s=%s", kw
->entry
->name
, *lst
);
556 * frees all dynamically allocated arguments in a struct
558 void free_args(kw_token_t first
, kw_token_t last
, char *base
)
562 for (token
= first
; token
<= last
; token
++)
564 char *p
= base
+ token_info
[token
].offset
;
566 switch (token_info
[token
].type
)
570 char **cp
= (char **)p
;
578 char ***listp
= (char ***)p
;
594 * clone all dynamically allocated arguments in a struct
596 void clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
600 for (token
= first
; token
<= last
; token
++)
602 if (token_info
[token
].type
== ARG_STR
)
604 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
605 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
607 *cp1
= clone_str(*cp2
);
612 static bool cmp_list(char **list1
, char **list2
)
614 if ((list1
== NULL
) && (list2
== NULL
))
618 if ((list1
== NULL
) || (list2
== NULL
))
623 for ( ; *list1
&& *list2
; list1
++, list2
++)
625 if (strcmp(*list1
,*list2
) != 0)
631 if ((*list1
!= NULL
) || (*list2
!= NULL
))
640 * compare all arguments in a struct
642 bool cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
646 for (token
= first
; token
<= last
; token
++)
648 char *p1
= base1
+ token_info
[token
].offset
;
649 char *p2
= base2
+ token_info
[token
].offset
;
651 switch (token_info
[token
].type
)
654 if (token_info
[token
].list
== LST_bool
)
656 bool *b1
= (bool *)p1
;
657 bool *b2
= (bool *)p2
;
677 u_int
*u1
= (u_int
*)p1
;
678 u_int
*u2
= (u_int
*)p2
;
689 unsigned long *l1
= (unsigned long *)p1
;
690 unsigned long *l2
= (unsigned long *)p2
;
700 unsigned long long *ll1
= (unsigned long long *)p1
;
701 unsigned long long *ll2
= (unsigned long long *)p2
;
711 time_t *t1
= (time_t *)p1
;
712 time_t *t2
= (time_t *)p2
;
722 char **cp1
= (char **)p1
;
723 char **cp2
= (char **)p2
;
725 if (*cp1
== NULL
&& *cp2
== NULL
)
729 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
737 char ***listp1
= (char ***)p1
;
738 char ***listp2
= (char ***)p2
;
740 if (!cmp_list(*listp1
, *listp2
))