82e957f597b7d302b2d01809312830c25be81365
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
15 * RCSID $Id: args.c,v 1.9 2006/04/17 10:32:36 as Exp $
24 #include "../pluto/constants.h"
25 #include "../pluto/defs.h"
26 #include "../pluto/log.h"
47 /* various keyword lists */
49 static const char *LST_bool
[] = {
55 static const char *LST_sendcert
[] = {
64 static const char *LST_dpd_action
[] = {
72 static const char *LST_startup
[] = {
80 static const char *LST_packetdefault
[] = {
87 static const char *LST_keyexchange
[] = {
94 static const char *LST_pfsgroup
[] = {
105 static const char *LST_plutodebug
[] = {
123 static const char *LST_klipsdebug
[] = {
146 static const token_info_t token_info
[] =
148 /* config setup keywords */
149 { ARG_LST
, offsetof(starter_config_t
, setup
.interfaces
), NULL
},
150 { ARG_STR
, offsetof(starter_config_t
, setup
.dumpdir
), NULL
},
151 { ARG_ENUM
, offsetof(starter_config_t
, setup
.charonstart
), LST_bool
},
152 { ARG_ENUM
, offsetof(starter_config_t
, setup
.plutostart
), LST_bool
},
154 /* pluto/charon keywords */
155 { ARG_LST
, offsetof(starter_config_t
, setup
.plutodebug
), LST_plutodebug
},
156 { ARG_STR
, offsetof(starter_config_t
, setup
.charondebug
), NULL
},
157 { ARG_STR
, offsetof(starter_config_t
, setup
.prepluto
), NULL
},
158 { ARG_STR
, offsetof(starter_config_t
, setup
.postpluto
), NULL
},
159 { ARG_ENUM
, offsetof(starter_config_t
, setup
.uniqueids
), LST_bool
},
160 { ARG_UINT
, offsetof(starter_config_t
, setup
.overridemtu
), NULL
},
161 { ARG_TIME
, offsetof(starter_config_t
, setup
.crlcheckinterval
), NULL
},
162 { ARG_ENUM
, offsetof(starter_config_t
, setup
.cachecrls
), LST_bool
},
163 { ARG_ENUM
, offsetof(starter_config_t
, setup
.strictcrlpolicy
), LST_bool
},
164 { ARG_ENUM
, offsetof(starter_config_t
, setup
.nocrsend
), LST_bool
},
165 { ARG_ENUM
, offsetof(starter_config_t
, setup
.nat_traversal
), LST_bool
},
166 { ARG_TIME
, offsetof(starter_config_t
, setup
.keep_alive
), NULL
},
167 { ARG_STR
, offsetof(starter_config_t
, setup
.virtual_private
), NULL
},
168 { ARG_STR
, offsetof(starter_config_t
, setup
.eapdir
), NULL
},
169 { ARG_STR
, offsetof(starter_config_t
, setup
.pkcs11module
), NULL
},
170 { ARG_ENUM
, offsetof(starter_config_t
, setup
.pkcs11keepstate
), LST_bool
},
171 { ARG_ENUM
, offsetof(starter_config_t
, setup
.pkcs11proxy
), LST_bool
},
174 { ARG_LST
, offsetof(starter_config_t
, setup
.klipsdebug
), LST_klipsdebug
},
175 { ARG_ENUM
, offsetof(starter_config_t
, setup
.fragicmp
), LST_bool
},
176 { ARG_STR
, offsetof(starter_config_t
, setup
.packetdefault
), LST_packetdefault
},
177 { ARG_ENUM
, offsetof(starter_config_t
, setup
.hidetos
), LST_bool
},
179 /* conn section keywords */
180 { ARG_STR
, offsetof(starter_conn_t
, name
), NULL
},
181 { ARG_ENUM
, offsetof(starter_conn_t
, startup
), LST_startup
},
182 { ARG_ENUM
, offsetof(starter_conn_t
, keyexchange
), LST_keyexchange
},
183 { ARG_MISC
, 0, NULL
/* KW_TYPE */ },
184 { ARG_MISC
, 0, NULL
/* KW_PFS */ },
185 { ARG_MISC
, 0, NULL
/* KW_COMPRESS */ },
186 { ARG_MISC
, 0, NULL
/* KW_AUTH */ },
187 { ARG_MISC
, 0, NULL
/* KW_AUTHBY */ },
188 { ARG_MISC
, 0, NULL
/* KW_EAP */ },
189 { ARG_TIME
, offsetof(starter_conn_t
, sa_ike_life_seconds
), NULL
},
190 { ARG_TIME
, offsetof(starter_conn_t
, sa_ipsec_life_seconds
), NULL
},
191 { ARG_TIME
, offsetof(starter_conn_t
, sa_rekey_margin
), NULL
},
192 { ARG_ULNG
, offsetof(starter_conn_t
, sa_keying_tries
), NULL
},
193 { ARG_PCNT
, offsetof(starter_conn_t
, sa_rekey_fuzz
), NULL
},
194 { ARG_MISC
, 0, NULL
/* KW_REKEY */ },
195 { ARG_MISC
, 0, NULL
/* KW_REAUTH */ },
196 { ARG_STR
, offsetof(starter_conn_t
, ike
), NULL
},
197 { ARG_STR
, offsetof(starter_conn_t
, esp
), NULL
},
198 { ARG_STR
, offsetof(starter_conn_t
, pfsgroup
), LST_pfsgroup
},
199 { ARG_TIME
, offsetof(starter_conn_t
, dpd_delay
), NULL
},
200 { ARG_TIME
, offsetof(starter_conn_t
, dpd_timeout
), NULL
},
201 { ARG_ENUM
, offsetof(starter_conn_t
, dpd_action
), LST_dpd_action
},
202 { ARG_MISC
, 0, NULL
/* KW_MODECONFIG */ },
203 { ARG_MISC
, 0, NULL
/* KW_XAUTH */ },
205 /* ca section keywords */
206 { ARG_STR
, offsetof(starter_ca_t
, name
), NULL
},
207 { ARG_ENUM
, offsetof(starter_ca_t
, startup
), LST_startup
},
208 { ARG_STR
, offsetof(starter_ca_t
, cacert
), NULL
},
209 { ARG_STR
, offsetof(starter_ca_t
, ldaphost
), NULL
},
210 { ARG_STR
, offsetof(starter_ca_t
, ldapbase
), NULL
},
211 { ARG_STR
, offsetof(starter_ca_t
, crluri
), NULL
},
212 { ARG_STR
, offsetof(starter_ca_t
, crluri2
), NULL
},
213 { ARG_STR
, offsetof(starter_ca_t
, ocspuri
), NULL
},
214 { ARG_STR
, offsetof(starter_ca_t
, ocspuri2
), NULL
},
217 { ARG_MISC
, 0, NULL
/* KW_HOST */ },
218 { ARG_MISC
, 0, NULL
/* KW_NEXTHOP */ },
219 { ARG_MISC
, 0, NULL
/* KW_SUBNET */ },
220 { ARG_MISC
, 0, NULL
/* KW_SUBNETWITHIN */ },
221 { ARG_MISC
, 0, NULL
/* KW_PROTOPORT */ },
222 { ARG_MISC
, 0, NULL
/* KW_SOURCEIP */ },
223 { ARG_MISC
, 0, NULL
/* KW_NATIP */ },
224 { ARG_ENUM
, offsetof(starter_end_t
, firewall
), LST_bool
},
225 { ARG_ENUM
, offsetof(starter_end_t
, hostaccess
), LST_bool
},
226 { ARG_STR
, offsetof(starter_end_t
, updown
), NULL
},
227 { ARG_STR
, offsetof(starter_end_t
, id
), NULL
},
228 { ARG_STR
, offsetof(starter_end_t
, rsakey
), NULL
},
229 { ARG_STR
, offsetof(starter_end_t
, cert
), NULL
},
230 { ARG_ENUM
, offsetof(starter_end_t
, sendcert
), LST_sendcert
},
231 { ARG_STR
, offsetof(starter_end_t
, ca
), NULL
},
232 { ARG_STR
, offsetof(starter_end_t
, groups
), NULL
},
233 { ARG_STR
, offsetof(starter_end_t
, iface
), NULL
}
237 free_list(char **list
)
241 for (s
= list
; *s
; s
++)
247 new_list(char *value
)
249 char *val
, *b
, *e
, *end
, **ret
;
252 val
= value ?
clone_str(value
, "list value") : NULL
;
255 end
= val
+ strlen(val
);
256 for (b
= val
, count
= 0; b
< end
;)
258 for (e
= b
; ((*e
!= ' ') && (*e
!= '\0')); e
++);
269 ret
= (char **)alloc_bytes((count
+1) * sizeof(char *), "list");
271 for (b
= val
, count
= 0; b
< end
; )
273 for (e
= b
; (*e
!= '\0'); e
++);
275 ret
[count
++] = clone_str(b
, "list value");
285 * assigns an argument value to a struct field
288 assign_arg(kw_token_t token
, kw_token_t first
, kw_list_t
*kw
, char *base
291 char *p
= base
+ token_info
[token
].offset
;
292 const char **list
= token_info
[token
].list
;
294 int index
= -1; /* used for enumeration arguments */
296 lset_t
*seen
= (lset_t
*)base
; /* seen flags are at the top of the struct */
297 lset_t f
= LELEM(token
- first
); /* compute flag position of argument */
302 DBG_log(" %s=%s", kw
->entry
->name
, kw
->value
)
307 plog("# duplicate '%s' option", kw
->entry
->name
);
311 /* set flag that this argument has been seen */
314 /* is there a keyword list? */
315 if (list
!= NULL
&& token_info
[token
].type
!= ARG_LST
)
319 while (*list
!= NULL
&& !match
)
322 match
= streq(kw
->value
, *list
++);
326 plog("# bad value: %s=%s", kw
->entry
->name
, kw
->value
);
331 switch (token_info
[token
].type
)
334 plog("# option '%s' not supported yet", kw
->entry
->name
);
342 plog("# bad enumeration value: %s=%s (%d)"
343 , kw
->entry
->name
, kw
->value
, index
);
353 u_int
*u
= (u_int
*)p
;
355 *u
= strtoul(kw
->value
, &endptr
, 10);
359 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
368 unsigned long *l
= (unsigned long *)p
;
370 *l
= strtoul(kw
->value
, &endptr
, 10);
372 if (token_info
[token
].type
== ARG_ULNG
)
376 plog("# bad integer value: %s=%s", kw
->entry
->name
, kw
->value
);
382 if ((*endptr
!= '%') || (endptr
[1] != '\0') || endptr
== kw
->value
)
384 plog("# bad percent value: %s=%s", kw
->entry
->name
, kw
->value
);
394 time_t *t
= (time_t *)p
;
396 *t
= strtoul(kw
->value
, &endptr
, 10);
398 /* time in seconds? */
399 if (*endptr
== '\0' || (*endptr
== 's' && endptr
[1] == '\0'))
402 if (endptr
[1] == '\0')
404 if (*endptr
== 'm') /* time in minutes? */
409 if (*endptr
== 'h') /* time in hours? */
414 if (*endptr
== 'd') /* time in days? */
420 plog("# bad duration value: %s=%s", kw
->entry
->name
, kw
->value
);
425 char **cp
= (char **)p
;
427 /* free any existing string */
430 /* assign the new string */
431 *cp
= clone_str(kw
->value
, "str_value");
436 char ***listp
= (char ***)p
;
438 /* free any existing list */
442 /* create a new list and assign values */
443 *listp
= new_list(kw
->value
);
445 /* is there a keyword list? */
450 for (lst
= *listp
; lst
&& *lst
; lst
++)
454 list
= token_info
[token
].list
;
456 while (*list
!= NULL
&& !match
)
458 match
= streq(*lst
, *list
++);
462 plog("# bad value: %s=%s", kw
->entry
->name
, *lst
);
477 * frees all dynamically allocated arguments in a struct
480 free_args(kw_token_t first
, kw_token_t last
, char *base
)
484 for (token
= first
; token
<= last
; token
++)
486 char *p
= base
+ token_info
[token
].offset
;
488 switch (token_info
[token
].type
)
492 char **cp
= (char **)p
;
500 char ***listp
= (char ***)p
;
516 * clone all dynamically allocated arguments in a struct
519 clone_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
523 for (token
= first
; token
<= last
; token
++)
525 if (token_info
[token
].type
== ARG_STR
)
527 char **cp1
= (char **)(base1
+ token_info
[token
].offset
);
528 char **cp2
= (char **)(base2
+ token_info
[token
].offset
);
530 *cp1
= clone_str(*cp2
, "cloned str");
536 cmp_list(char **list1
, char **list2
)
538 if ((list1
== NULL
) && (list2
== NULL
))
540 if ((list1
== NULL
) || (list2
== NULL
))
543 for ( ; *list1
&& *list2
; list1
++, list2
++)
545 if (strcmp(*list1
,*list2
) != 0)
549 if ((*list1
!= NULL
) || (*list2
!= NULL
))
556 * compare all arguments in a struct
559 cmp_args(kw_token_t first
, kw_token_t last
, char *base1
, char *base2
)
563 for (token
= first
; token
<= last
; token
++)
565 char *p1
= base1
+ token_info
[token
].offset
;
566 char *p2
= base2
+ token_info
[token
].offset
;
568 switch (token_info
[token
].type
)
581 u_int
*u1
= (u_int
*)p1
;
582 u_int
*u2
= (u_int
*)p2
;
591 unsigned long *l1
= (unsigned long *)p1
;
592 unsigned long *l2
= (unsigned long *)p2
;
600 time_t *t1
= (time_t *)p1
;
601 time_t *t2
= (time_t *)p2
;
609 char **cp1
= (char **)p1
;
610 char **cp2
= (char **)p2
;
612 if (*cp1
== NULL
&& *cp2
== NULL
)
614 if (*cp1
== NULL
|| *cp2
== NULL
|| strcmp(*cp1
, *cp2
) != 0)
620 char ***listp1
= (char ***)p1
;
621 char ***listp2
= (char ***)p2
;
623 if (!cmp_list(*listp1
, *listp2
))