]>
Commit | Line | Data |
---|---|---|
d1e348cf A |
1 | /* $NetBSD: remoteconf.c,v 1.9.4.1 2007/08/01 11:52:22 vanhu Exp $ */ |
2 | ||
3 | /* Id: remoteconf.c,v 1.38 2006/05/06 15:52:44 manubsd Exp */ | |
52b7d2ce A |
4 | |
5 | /* | |
6 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * 3. Neither the name of the project nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #include "config.h" | |
35 | ||
36 | #include <sys/types.h> | |
37 | #include <sys/param.h> | |
38 | #include <sys/socket.h> | |
39 | #include <sys/queue.h> | |
40 | ||
41 | #include <netinet/in.h> | |
42 | #include <netinet/in_systm.h> | |
43 | #include <netinet/ip.h> | |
44 | ||
45 | #ifndef HAVE_NETINET6_IPSEC | |
46 | #include <netinet/ipsec.h> | |
47 | #else | |
48 | #include <netinet6/ipsec.h> | |
49 | #endif | |
50 | ||
51 | #include <stdlib.h> | |
52 | #include <stdio.h> | |
53 | #include <string.h> | |
54 | #include <errno.h> | |
55 | ||
56 | #include "var.h" | |
57 | #include "misc.h" | |
58 | #include "vmbuf.h" | |
59 | #include "plog.h" | |
60 | #include "sockmisc.h" | |
61 | #include "genlist.h" | |
62 | #include "debug.h" | |
63 | ||
64 | #include "isakmp_var.h" | |
d1e348cf A |
65 | #ifdef ENABLE_HYBRID |
66 | #include "isakmp_xauth.h" | |
67 | #endif | |
52b7d2ce A |
68 | #include "isakmp.h" |
69 | #include "ipsec_doi.h" | |
70 | #include "oakley.h" | |
71 | #include "remoteconf.h" | |
72 | #include "localconf.h" | |
73 | #include "grabmyaddr.h" | |
d1e348cf | 74 | #include "policy.h" |
52b7d2ce A |
75 | #include "proposal.h" |
76 | #include "vendorid.h" | |
77 | #include "gcmalloc.h" | |
78 | #include "strnames.h" | |
79 | #include "algorithm.h" | |
80 | #include "nattraversal.h" | |
d1e348cf | 81 | #include "isakmp_frag.h" |
52b7d2ce A |
82 | #include "genlist.h" |
83 | #include "rsalist.h" | |
84 | ||
85 | static TAILQ_HEAD(_rmtree, remoteconf) rmtree; | |
86 | ||
87 | /* | |
88 | * Script hook names and script hook paths | |
89 | */ | |
90 | char *script_names[SCRIPT_MAX + 1] = { "phase1_up", "phase1_down" }; | |
52b7d2ce A |
91 | |
92 | /*%%%*/ | |
93 | /* | |
94 | * search remote configuration. | |
95 | * don't use port number to search if its value is either IPSEC_PORT_ANY. | |
96 | * If matching anonymous entry, then new entry is copied from anonymous entry. | |
97 | * If no anonymous entry found, then return NULL. | |
98 | * OUT: NULL: NG | |
99 | * Other: remote configuration entry. | |
100 | */ | |
101 | struct remoteconf * | |
102 | getrmconf_strict(remote, allow_anon) | |
103 | struct sockaddr *remote; | |
104 | int allow_anon; | |
105 | { | |
106 | struct remoteconf *p; | |
47612122 | 107 | struct remoteconf *p_withport_besteffort = NULL; |
52b7d2ce A |
108 | struct remoteconf *anon = NULL; |
109 | int withport; | |
110 | char buf[NI_MAXHOST + NI_MAXSERV + 10]; | |
111 | char addr[NI_MAXHOST], port[NI_MAXSERV]; | |
112 | ||
113 | withport = 0; | |
114 | ||
52b7d2ce A |
115 | /* |
116 | * We never have ports set in our remote configurations, but when | |
117 | * NAT-T is enabled, the kernel can have policies with ports and | |
118 | * send us an acquire message for a destination that has a port set. | |
47612122 | 119 | * If we do this port check here, we have to fallback to a best-effort result (without the port). |
52b7d2ce A |
120 | * |
121 | * In an ideal world, we would be able to have remote conf with | |
122 | * port, and the port could be a wildcard. That test could be used. | |
123 | */ | |
124 | switch (remote->sa_family) { | |
125 | case AF_INET: | |
126 | if (((struct sockaddr_in *)remote)->sin_port != IPSEC_PORT_ANY) | |
127 | withport = 1; | |
128 | break; | |
129 | #ifdef INET6 | |
130 | case AF_INET6: | |
131 | if (((struct sockaddr_in6 *)remote)->sin6_port != IPSEC_PORT_ANY) | |
132 | withport = 1; | |
133 | break; | |
134 | #endif | |
135 | case AF_UNSPEC: | |
136 | break; | |
137 | ||
138 | default: | |
139 | plog(LLV_ERROR2, LOCATION, NULL, | |
140 | "invalid ip address family: %d\n", remote->sa_family); | |
47612122 | 141 | return NULL; |
52b7d2ce | 142 | } |
52b7d2ce A |
143 | |
144 | if (remote->sa_family == AF_UNSPEC) | |
145 | snprintf (buf, sizeof(buf), "%s", "anonymous"); | |
146 | else { | |
147 | GETNAMEINFO(remote, addr, port); | |
148 | snprintf(buf, sizeof(buf), "%s%s%s%s", addr, | |
149 | withport ? "[" : "", | |
150 | withport ? port : "", | |
151 | withport ? "]" : ""); | |
152 | } | |
153 | ||
154 | TAILQ_FOREACH(p, &rmtree, chain) { | |
d1e348cf A |
155 | #ifdef __APPLE__ |
156 | if (p->to_delete || p->to_remove) { | |
157 | continue; | |
158 | } | |
159 | #endif | |
52b7d2ce A |
160 | if ((remote->sa_family == AF_UNSPEC |
161 | && remote->sa_family == p->remote->sa_family) | |
162 | || (!withport && cmpsaddrwop(remote, p->remote) == 0) | |
163 | || (withport && cmpsaddrstrict(remote, p->remote) == 0)) { | |
164 | plog(LLV_DEBUG, LOCATION, NULL, | |
165 | "configuration found for %s.\n", buf); | |
166 | return p; | |
47612122 A |
167 | } else if (withport && cmpsaddrwop(remote, p->remote) == 0) { |
168 | // for withport: save the pointer for the best-effort search | |
169 | p_withport_besteffort = p; | |
52b7d2ce A |
170 | } |
171 | ||
172 | /* save the pointer to the anonymous configuration */ | |
173 | if (p->remote->sa_family == AF_UNSPEC) | |
174 | anon = p; | |
175 | } | |
176 | ||
47612122 A |
177 | if (p_withport_besteffort) { |
178 | plog(LLV_DEBUG, LOCATION, NULL, | |
179 | "configuration found for %s.\n", buf); | |
180 | return p_withport_besteffort; | |
181 | } | |
182 | ||
52b7d2ce A |
183 | if (allow_anon && anon != NULL) { |
184 | plog(LLV_DEBUG, LOCATION, NULL, | |
185 | "anonymous configuration selected for %s.\n", buf); | |
186 | return anon; | |
187 | } | |
188 | ||
189 | plog(LLV_DEBUG, LOCATION, NULL, | |
190 | "no remote configuration found.\n"); | |
191 | ||
192 | return NULL; | |
193 | } | |
194 | ||
d1e348cf | 195 | int |
93762ec7 A |
196 | no_remote_configs(ignore_anonymous) |
197 | int ignore_anonymous; | |
d1e348cf A |
198 | { |
199 | ||
200 | struct remoteconf *p; | |
93762ec7 A |
201 | #if !TARGET_OS_EMBEDDED |
202 | static const char default_idv[] = "macuser@localhost"; | |
203 | static const int default_idv_len = sizeof(default_idv) - 1; | |
204 | #endif | |
d1e348cf A |
205 | |
206 | TAILQ_FOREACH(p, &rmtree, chain) { | |
93762ec7 A |
207 | if (ignore_anonymous) { |
208 | if (p->remote->sa_family == AF_UNSPEC) /* anonymous */ | |
209 | continue; | |
210 | } | |
211 | #if !TARGET_OS_EMBEDDED | |
212 | // ignore the default btmm ipv6 config thats always present in racoon.conf | |
213 | if (p->remote->sa_family == AF_INET6 && | |
214 | p->idvtype == IDTYPE_USERFQDN && | |
215 | p->idv != NULL && | |
216 | p->idv->l == default_idv_len && | |
217 | strncmp(p->idv->v, default_idv, p->idv->l) == 0) { | |
d1e348cf | 218 | continue; |
93762ec7 A |
219 | } |
220 | #endif | |
d1e348cf A |
221 | return 0; |
222 | } | |
223 | return 1; | |
224 | } | |
225 | ||
52b7d2ce A |
226 | struct remoteconf * |
227 | getrmconf(remote) | |
228 | struct sockaddr *remote; | |
229 | { | |
230 | return getrmconf_strict(remote, 1); | |
231 | } | |
232 | ||
d1e348cf A |
233 | #ifdef __APPLE__ |
234 | int | |
235 | link_rmconf_to_ph1 (struct remoteconf *new) | |
236 | { | |
237 | if (!new) { | |
238 | return(-1); | |
239 | } | |
240 | if (new->to_delete || | |
241 | new->to_remove) { | |
242 | return(-1); | |
243 | } | |
244 | new->linked_to_ph1++; | |
245 | return(0); | |
246 | } | |
247 | ||
248 | int | |
249 | unlink_rmconf_from_ph1 (struct remoteconf *old) | |
250 | { | |
251 | if (!old) { | |
252 | return(-1); | |
253 | } | |
254 | if (old->linked_to_ph1 <= 0) { | |
255 | return(-1); | |
256 | } | |
257 | old->linked_to_ph1--; | |
258 | if (old->linked_to_ph1 == 0) { | |
259 | if (old->to_remove) { | |
260 | remrmconf(old); | |
261 | } | |
262 | if (old->to_delete) { | |
263 | delrmconf(old); | |
264 | } | |
265 | } | |
266 | return(0); | |
267 | } | |
268 | #endif | |
269 | ||
52b7d2ce A |
270 | struct remoteconf * |
271 | newrmconf() | |
272 | { | |
273 | struct remoteconf *new; | |
274 | int i; | |
275 | ||
276 | new = racoon_calloc(1, sizeof(*new)); | |
277 | if (new == NULL) | |
278 | return NULL; | |
279 | ||
280 | new->proposal = NULL; | |
281 | ||
282 | /* set default */ | |
283 | new->doitype = IPSEC_DOI; | |
284 | new->sittype = IPSECDOI_SIT_IDENTITY_ONLY; | |
285 | new->idvtype = IDTYPE_UNDEFINED; | |
286 | new->idvl_p = genlist_init(); | |
287 | new->nonce_size = DEFAULT_NONCE_SIZE; | |
288 | new->passive = FALSE; | |
289 | new->ike_frag = FALSE; | |
290 | new->esp_frag = IP_MAXPACKET; | |
291 | new->ini_contact = TRUE; | |
292 | new->mode_cfg = FALSE; | |
293 | new->pcheck_level = PROP_CHECK_STRICT; | |
294 | new->verify_identifier = FALSE; | |
295 | new->verify_cert = TRUE; | |
296 | new->getcert_method = ISAKMP_GETCERT_PAYLOAD; | |
297 | new->getcacert_method = ISAKMP_GETCERT_LOCALFILE; | |
298 | new->cacerttype = ISAKMP_CERT_X509SIGN; | |
d1e348cf | 299 | new->certtype = ISAKMP_CERT_NONE; |
52b7d2ce A |
300 | new->cacertfile = NULL; |
301 | new->send_cert = TRUE; | |
302 | new->send_cr = TRUE; | |
303 | new->support_proxy = FALSE; | |
304 | for (i = 0; i <= SCRIPT_MAX; i++) | |
d1e348cf | 305 | new->script[i] = NULL; |
52b7d2ce A |
306 | new->gen_policy = FALSE; |
307 | new->retry_counter = lcconf->retry_counter; | |
308 | new->retry_interval = lcconf->retry_interval; | |
309 | #ifdef __APPLE__ | |
310 | new->nat_traversal = NATT_ON; | |
311 | new->natt_multiple_user = FALSE; | |
d1e348cf A |
312 | new->natt_keepalive = TRUE; |
313 | new->to_remove = FALSE; | |
314 | new->to_delete = FALSE; | |
315 | new->linked_to_ph1 = 0; | |
52b7d2ce A |
316 | #else |
317 | new->nat_traversal = NATT_OFF; | |
318 | #endif | |
319 | new->rsa_private = genlist_init(); | |
320 | new->rsa_public = genlist_init(); | |
321 | new->idv = NULL; | |
322 | new->key = NULL; | |
323 | ||
324 | new->dpd = TRUE; /* Enable DPD support by default */ | |
325 | new->dpd_interval = 0; /* Disable DPD checks by default */ | |
326 | new->dpd_retry = 5; | |
327 | new->dpd_maxfails = 5; | |
d1e348cf A |
328 | new->dpd_algo = DPD_ALGO_INBOUND_DETECT; |
329 | new->idle_timeout = 0; | |
52b7d2ce | 330 | |
d1e348cf A |
331 | new->weak_phase1_check = 0; |
332 | ||
333 | #ifdef ENABLE_HYBRID | |
334 | new->xauth = NULL; | |
335 | #endif | |
336 | new->initiate_ph1rekey = TRUE; | |
52b7d2ce A |
337 | return new; |
338 | } | |
339 | ||
340 | struct remoteconf * | |
341 | copyrmconf(remote) | |
342 | struct sockaddr *remote; | |
343 | { | |
344 | struct remoteconf *new, *old; | |
345 | ||
346 | old = getrmconf_strict (remote, 0); | |
347 | if (old == NULL) { | |
348 | plog (LLV_ERROR, LOCATION, NULL, | |
349 | "Remote configuration for '%s' not found!\n", | |
350 | saddr2str (remote)); | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | new = duprmconf (old); | |
355 | ||
356 | return new; | |
357 | } | |
358 | ||
359 | void * | |
360 | dupidvl(entry, arg) | |
361 | void *entry; | |
362 | void *arg; | |
363 | { | |
364 | struct idspec *id; | |
365 | struct idspec *old = (struct idspec *) entry; | |
366 | id = newidspec(); | |
367 | if (!id) return (void *) -1; | |
368 | ||
d1e348cf A |
369 | if (set_identifier(&id->id, old->idtype, old->id) != 0) { |
370 | racoon_free(id); | |
52b7d2ce | 371 | return (void *) -1; |
d1e348cf | 372 | } |
52b7d2ce A |
373 | |
374 | id->idtype = old->idtype; | |
375 | ||
376 | genlist_append(arg, id); | |
377 | return NULL; | |
378 | } | |
379 | ||
380 | struct remoteconf * | |
381 | duprmconf (rmconf) | |
382 | struct remoteconf *rmconf; | |
383 | { | |
384 | struct remoteconf *new; | |
385 | ||
386 | new = racoon_calloc(1, sizeof(*new)); | |
387 | if (new == NULL) | |
388 | return NULL; | |
389 | memcpy (new, rmconf, sizeof (*new)); | |
390 | // FIXME: We should duplicate the proposal as well. | |
391 | // This is now handled in the cfparse.y | |
392 | // new->proposal = ...; | |
393 | ||
394 | /* duplicate dynamic structures */ | |
395 | if (new->etypes) | |
396 | new->etypes=dupetypes(new->etypes); | |
397 | new->idvl_p = genlist_init(); | |
398 | genlist_foreach(rmconf->idvl_p, dupidvl, new->idvl_p); | |
399 | ||
400 | return new; | |
401 | } | |
402 | ||
403 | static void | |
404 | idspec_free(void *data) | |
405 | { | |
406 | vfree (((struct idspec *)data)->id); | |
407 | free (data); | |
408 | } | |
409 | ||
410 | static void | |
411 | proposalspec_free(struct proposalspec *head) | |
412 | { | |
413 | ||
414 | struct proposalspec* next_propsp = head; | |
415 | ||
416 | while (next_propsp) { | |
417 | struct proposalspec* curr_propsp; | |
418 | struct secprotospec* next_protosp; | |
419 | ||
420 | curr_propsp = next_propsp; | |
421 | next_propsp = next_propsp->next; | |
422 | next_protosp = curr_propsp->spspec; | |
423 | while (next_protosp) { | |
424 | struct secprotospec* curr_protosp; | |
425 | ||
426 | curr_protosp = next_protosp; | |
427 | next_protosp = next_protosp->next; | |
428 | ||
429 | if (curr_protosp->gssid) | |
430 | free(curr_protosp->gssid); | |
431 | if (curr_protosp->remote) | |
432 | free(curr_protosp->remote); | |
433 | racoon_free(curr_protosp); | |
434 | } | |
435 | racoon_free(curr_propsp); | |
436 | } | |
437 | } | |
438 | ||
439 | void | |
440 | delrmconf(rmconf) | |
441 | struct remoteconf *rmconf; | |
442 | { | |
d1e348cf A |
443 | #ifdef __APPLE__ |
444 | if (rmconf->linked_to_ph1) { | |
445 | rmconf->to_delete = TRUE; | |
446 | return; | |
447 | } | |
448 | #endif | |
52b7d2ce A |
449 | if (rmconf->remote) |
450 | racoon_free(rmconf->remote); | |
d1e348cf A |
451 | #ifdef ENABLE_HYBRID |
452 | if (rmconf->xauth) | |
453 | xauth_rmconf_delete(&rmconf->xauth); | |
454 | #endif | |
455 | if (rmconf->etypes) { | |
52b7d2ce | 456 | deletypes(rmconf->etypes); |
d1e348cf A |
457 | rmconf->etypes=NULL; |
458 | } | |
52b7d2ce A |
459 | if (rmconf->idv) |
460 | vfree(rmconf->idv); | |
461 | if (rmconf->idvl_p) | |
462 | genlist_free(rmconf->idvl_p, idspec_free); | |
463 | if (rmconf->dhgrp) | |
464 | oakley_dhgrp_free(rmconf->dhgrp); | |
465 | if (rmconf->proposal) | |
466 | delisakmpsa(rmconf->proposal); | |
467 | if (rmconf->mycertfile) | |
468 | racoon_free(rmconf->mycertfile); | |
469 | if (rmconf->myprivfile) | |
470 | racoon_free(rmconf->myprivfile); | |
471 | if (rmconf->peerscertfile) | |
472 | racoon_free(rmconf->peerscertfile); | |
473 | if (rmconf->cacertfile) | |
474 | racoon_free(rmconf->cacertfile); | |
475 | if (rmconf->prhead) | |
476 | proposalspec_free(rmconf->prhead); | |
477 | if (rmconf->rsa_private) | |
478 | genlist_free(rmconf->rsa_private, rsa_key_free); | |
479 | if (rmconf->rsa_public) | |
480 | genlist_free(rmconf->rsa_public, rsa_key_free); | |
481 | #ifdef __APPLE__ | |
482 | if (rmconf->shared_secret) | |
483 | vfree(rmconf->shared_secret); | |
484 | if (rmconf->keychainCertRef) | |
485 | vfree(rmconf->keychainCertRef); | |
486 | if (rmconf->open_dir_auth_group) | |
487 | vfree(rmconf->open_dir_auth_group); | |
488 | #endif | |
489 | ||
490 | racoon_free(rmconf); | |
491 | } | |
492 | ||
493 | void | |
494 | delisakmpsa(sa) | |
495 | struct isakmpsa *sa; | |
496 | { | |
497 | if (sa->dhgrp) | |
498 | oakley_dhgrp_free(sa->dhgrp); | |
499 | if (sa->next) | |
500 | delisakmpsa(sa->next); | |
501 | #ifdef HAVE_GSSAPI | |
502 | if (sa->gssid) | |
503 | vfree(sa->gssid); | |
504 | #endif | |
505 | racoon_free(sa); | |
506 | } | |
507 | ||
508 | struct etypes * | |
509 | dupetypes(orig) | |
510 | struct etypes *orig; | |
511 | { | |
512 | struct etypes *new; | |
513 | ||
514 | if (!orig) | |
515 | return NULL; | |
516 | ||
517 | new = racoon_malloc(sizeof(struct etypes)); | |
518 | if (new == NULL) | |
519 | return NULL; | |
520 | ||
521 | new->type = orig->type; | |
522 | new->next = NULL; | |
523 | ||
524 | if (orig->next) | |
525 | new->next=dupetypes(orig->next); | |
526 | ||
527 | return new; | |
528 | } | |
529 | ||
530 | void | |
531 | deletypes(e) | |
532 | struct etypes *e; | |
533 | { | |
534 | if (e->next) | |
535 | deletypes(e->next); | |
536 | racoon_free(e); | |
537 | } | |
538 | ||
539 | /* | |
540 | * insert into head of list. | |
541 | */ | |
542 | void | |
543 | insrmconf(new) | |
544 | struct remoteconf *new; | |
545 | { | |
546 | TAILQ_INSERT_HEAD(&rmtree, new, chain); | |
547 | } | |
548 | ||
549 | void | |
550 | remrmconf(rmconf) | |
551 | struct remoteconf *rmconf; | |
552 | { | |
d1e348cf A |
553 | #ifdef __APPLE__ |
554 | if (rmconf->linked_to_ph1) { | |
555 | rmconf->to_remove = TRUE; | |
556 | return; | |
557 | } | |
558 | #endif | |
52b7d2ce A |
559 | TAILQ_REMOVE(&rmtree, rmconf, chain); |
560 | } | |
561 | ||
562 | void | |
563 | flushrmconf() | |
564 | { | |
565 | struct remoteconf *p, *next; | |
566 | ||
567 | for (p = TAILQ_FIRST(&rmtree); p; p = next) { | |
568 | next = TAILQ_NEXT(p, chain); | |
569 | remrmconf(p); | |
570 | delrmconf(p); | |
571 | } | |
572 | } | |
573 | ||
574 | void | |
575 | initrmconf() | |
576 | { | |
577 | TAILQ_INIT(&rmtree); | |
578 | } | |
579 | ||
580 | /* check exchange type to be acceptable */ | |
581 | struct etypes * | |
582 | check_etypeok(rmconf, etype) | |
583 | struct remoteconf *rmconf; | |
584 | u_int8_t etype; | |
585 | { | |
586 | struct etypes *e; | |
587 | ||
588 | for (e = rmconf->etypes; e != NULL; e = e->next) { | |
589 | if (e->type == etype) | |
590 | break; | |
591 | } | |
592 | ||
593 | return e; | |
594 | } | |
595 | ||
596 | /*%%%*/ | |
597 | struct isakmpsa * | |
598 | newisakmpsa() | |
599 | { | |
600 | struct isakmpsa *new; | |
601 | ||
602 | new = racoon_calloc(1, sizeof(*new)); | |
603 | if (new == NULL) | |
604 | return NULL; | |
605 | ||
606 | /* | |
607 | * Just for sanity, make sure this is initialized. This is | |
608 | * filled in for real when the ISAKMP proposal is configured. | |
609 | */ | |
610 | new->vendorid = VENDORID_UNKNOWN; | |
611 | ||
612 | new->next = NULL; | |
613 | new->rmconf = NULL; | |
614 | #ifdef HAVE_GSSAPI | |
615 | new->gssid = NULL; | |
616 | #endif | |
617 | ||
618 | return new; | |
619 | } | |
620 | ||
621 | /* | |
622 | * insert into tail of list. | |
623 | */ | |
624 | void | |
625 | insisakmpsa(new, rmconf) | |
626 | struct isakmpsa *new; | |
627 | struct remoteconf *rmconf; | |
628 | { | |
629 | struct isakmpsa *p; | |
630 | ||
631 | new->rmconf = rmconf; | |
632 | ||
633 | if (rmconf->proposal == NULL) { | |
634 | rmconf->proposal = new; | |
635 | return; | |
636 | } | |
637 | ||
638 | for (p = rmconf->proposal; p->next != NULL; p = p->next) | |
639 | ; | |
640 | p->next = new; | |
641 | ||
642 | return; | |
643 | } | |
644 | ||
645 | struct remoteconf * | |
646 | foreachrmconf(rmconf_func_t rmconf_func, void *data) | |
647 | { | |
648 | struct remoteconf *p, *ret = NULL; | |
649 | ||
650 | TAILQ_FOREACH_REVERSE(p, &rmtree, _rmtree, chain) { | |
651 | ret = (*rmconf_func)(p, data); | |
652 | if (ret) | |
653 | break; | |
654 | } | |
655 | ||
656 | return ret; | |
657 | } | |
658 | ||
659 | static void * | |
660 | dump_peers_identifiers (void *entry, void *arg) | |
661 | { | |
662 | struct idspec *id = (struct idspec*) entry; | |
663 | char buf[1024], *pbuf; | |
664 | pbuf = buf; | |
665 | pbuf += snprintf (pbuf, sizeof(buf) - (pbuf - buf), "\tpeers_identifier %s", | |
666 | s_idtype (id->idtype)); | |
667 | if (id->id) | |
668 | pbuf += snprintf (pbuf, sizeof(buf) - (pbuf - buf), " \"%s\"", id->id->v); | |
669 | plog(LLV_INFO, LOCATION, NULL, "%s;\n", buf); | |
670 | return NULL; | |
671 | } | |
672 | ||
673 | static struct remoteconf * | |
674 | dump_rmconf_single (struct remoteconf *p, void *data) | |
675 | { | |
676 | struct etypes *etype = p->etypes; | |
677 | struct isakmpsa *prop = p->proposal; | |
678 | char buf[1024], *pbuf; | |
679 | ||
680 | pbuf = buf; | |
681 | pbuf += snprintf(pbuf, sizeof(buf) - (pbuf - buf), "remote %s", saddr2str(p->remote)); | |
682 | if (p->inherited_from) | |
683 | pbuf += snprintf(pbuf, sizeof(buf) - (pbuf - buf), " inherit %s", | |
684 | saddr2str(p->inherited_from->remote)); | |
685 | plog(LLV_INFO, LOCATION, NULL, "%s {\n", buf); | |
686 | pbuf = buf; | |
687 | pbuf += snprintf(pbuf, sizeof(buf) - (pbuf - buf), "\texchange_type "); | |
688 | while (etype) { | |
689 | pbuf += snprintf (pbuf, sizeof(buf) - (pbuf - buf), "%s%s", s_etype(etype->type), | |
690 | etype->next != NULL ? ", " : ";\n"); | |
691 | etype = etype->next; | |
692 | } | |
693 | plog(LLV_INFO, LOCATION, NULL, "%s", buf); | |
694 | plog(LLV_INFO, LOCATION, NULL, "\tdoi %s;\n", s_doi(p->doitype)); | |
695 | pbuf = buf; | |
696 | pbuf += snprintf(pbuf, sizeof(buf) - (pbuf - buf), "\tmy_identifier %s", s_idtype (p->idvtype)); | |
697 | if (p->idvtype == IDTYPE_ASN1DN) { | |
698 | plog(LLV_INFO, LOCATION, NULL, "%s;\n", buf); | |
699 | plog(LLV_INFO, LOCATION, NULL, "\tcertificate_type %s \"%s\" \"%s\";\n", | |
700 | p->certtype == ISAKMP_CERT_X509SIGN ? "x509" : "*UNKNOWN*", | |
701 | p->mycertfile, p->myprivfile); | |
702 | switch (p->getcert_method) { | |
703 | case 0: | |
704 | break; | |
705 | case ISAKMP_GETCERT_PAYLOAD: | |
706 | plog(LLV_INFO, LOCATION, NULL, "\t/* peers certificate from payload */\n"); | |
707 | break; | |
708 | case ISAKMP_GETCERT_LOCALFILE: | |
709 | plog(LLV_INFO, LOCATION, NULL, "\tpeers_certfile \"%s\";\n", p->peerscertfile); | |
710 | break; | |
711 | case ISAKMP_GETCERT_DNS: | |
712 | plog(LLV_INFO, LOCATION, NULL, "\tpeer_certfile dnssec;\n"); | |
713 | break; | |
714 | default: | |
715 | plog(LLV_INFO, LOCATION, NULL, "\tpeers_certfile *UNKNOWN* (%d)\n", p->getcert_method); | |
716 | } | |
717 | } | |
718 | else { | |
719 | if (p->idv) | |
720 | pbuf += snprintf (pbuf, sizeof(buf) - (pbuf - buf), " \"%s\"", p->idv->v); | |
721 | plog(LLV_INFO, LOCATION, NULL, "%s;\n", buf); | |
722 | genlist_foreach(p->idvl_p, &dump_peers_identifiers, NULL); | |
723 | } | |
724 | ||
725 | plog(LLV_INFO, LOCATION, NULL, "\tsend_cert %s;\n", | |
726 | s_switch (p->send_cert)); | |
727 | plog(LLV_INFO, LOCATION, NULL, "\tsend_cr %s;\n", | |
728 | s_switch (p->send_cr)); | |
729 | plog(LLV_INFO, LOCATION, NULL, "\tverify_cert %s;\n", | |
730 | s_switch (p->verify_cert)); | |
731 | plog(LLV_INFO, LOCATION, NULL, "\tverify_identifier %s;\n", | |
732 | s_switch (p->verify_identifier)); | |
733 | plog(LLV_INFO, LOCATION, NULL, "\tnat_traversal %s;\n", | |
734 | p->nat_traversal == NATT_FORCE ? | |
735 | "force" : s_switch (p->nat_traversal)); | |
736 | #ifdef __APPLE__ | |
737 | plog(LLV_INFO, LOCATION, NULL, "\tnatt_multiple_user %s;\n", | |
738 | s_switch (p->natt_multiple_user)); | |
739 | #endif | |
740 | plog(LLV_INFO, LOCATION, NULL, "\tnonce_size %d;\n", | |
741 | p->nonce_size); | |
742 | plog(LLV_INFO, LOCATION, NULL, "\tpassive %s;\n", | |
743 | s_switch (p->passive)); | |
744 | plog(LLV_INFO, LOCATION, NULL, "\tike_frag %s;\n", | |
d1e348cf A |
745 | p->ike_frag == ISAKMP_FRAG_FORCE ? |
746 | "force" : s_switch (p->ike_frag)); | |
52b7d2ce A |
747 | plog(LLV_INFO, LOCATION, NULL, "\tesp_frag %d;\n", p->esp_frag); |
748 | plog(LLV_INFO, LOCATION, NULL, "\tinitial_contact %s;\n", | |
749 | s_switch (p->ini_contact)); | |
750 | plog(LLV_INFO, LOCATION, NULL, "\tgenerate_policy %s;\n", | |
751 | s_switch (p->gen_policy)); | |
752 | plog(LLV_INFO, LOCATION, NULL, "\tsupport_proxy %s;\n", | |
753 | s_switch (p->support_proxy)); | |
754 | ||
755 | while (prop) { | |
756 | plog(LLV_INFO, LOCATION, NULL, "\n"); | |
757 | plog(LLV_INFO, LOCATION, NULL, | |
758 | "\t/* prop_no=%d, trns_no=%d, rmconf=%s */\n", | |
759 | prop->prop_no, prop->trns_no, | |
760 | saddr2str(prop->rmconf->remote)); | |
761 | plog(LLV_INFO, LOCATION, NULL, "\tproposal {\n"); | |
762 | plog(LLV_INFO, LOCATION, NULL, "\t\tlifetime time %lu sec;\n", | |
763 | (long)prop->lifetime); | |
764 | plog(LLV_INFO, LOCATION, NULL, "\t\tlifetime bytes %zd;\n", | |
765 | prop->lifebyte); | |
766 | plog(LLV_INFO, LOCATION, NULL, "\t\tdh_group %s;\n", | |
767 | alg_oakley_dhdef_name(prop->dh_group)); | |
768 | plog(LLV_INFO, LOCATION, NULL, "\t\tencryption_algorithm %s;\n", | |
769 | alg_oakley_encdef_name(prop->enctype)); | |
770 | plog(LLV_INFO, LOCATION, NULL, "\t\thash_algorithm %s;\n", | |
771 | alg_oakley_hashdef_name(prop->hashtype)); | |
772 | plog(LLV_INFO, LOCATION, NULL, "\t\tauthentication_method %s;\n", | |
773 | alg_oakley_authdef_name(prop->authmethod)); | |
774 | plog(LLV_INFO, LOCATION, NULL, "\t}\n"); | |
775 | prop = prop->next; | |
776 | } | |
777 | plog(LLV_INFO, LOCATION, NULL, "}\n"); | |
778 | plog(LLV_INFO, LOCATION, NULL, "\n"); | |
779 | ||
780 | return NULL; | |
781 | } | |
782 | ||
783 | void | |
784 | dumprmconf() | |
785 | { | |
786 | foreachrmconf (dump_rmconf_single, NULL); | |
787 | } | |
788 | ||
789 | struct idspec * | |
790 | newidspec() | |
791 | { | |
792 | struct idspec *new; | |
793 | ||
794 | new = racoon_calloc(1, sizeof(*new)); | |
795 | if (new == NULL) | |
796 | return NULL; | |
797 | new->idtype = IDTYPE_ADDRESS; | |
798 | ||
799 | return new; | |
800 | } | |
801 | ||
d1e348cf | 802 | vchar_t * |
52b7d2ce A |
803 | script_path_add(path) |
804 | vchar_t *path; | |
805 | { | |
806 | char *script_dir; | |
52b7d2ce | 807 | vchar_t *new_path; |
d1e348cf | 808 | vchar_t *new_storage; |
52b7d2ce A |
809 | vchar_t **sp; |
810 | size_t len; | |
811 | size_t size; | |
812 | ||
813 | script_dir = lcconf->pathinfo[LC_PATHTYPE_SCRIPT]; | |
814 | ||
815 | /* Try to find the script in the script directory */ | |
816 | if ((path->v[0] != '/') && (script_dir != NULL)) { | |
817 | len = strlen(script_dir) + sizeof("/") + path->l + 1; | |
818 | ||
819 | if ((new_path = vmalloc(len)) == NULL) { | |
820 | plog(LLV_ERROR, LOCATION, NULL, | |
821 | "Cannot allocate memory: %s\n", strerror(errno)); | |
d1e348cf | 822 | return NULL; |
52b7d2ce A |
823 | } |
824 | ||
825 | new_path->v[0] = '\0'; | |
d1e348cf A |
826 | (void)strlcat(new_path->v, script_dir, new_path->l); |
827 | (void)strlcat(new_path->v, "/", new_path->l); | |
828 | (void)strlcat(new_path->v, path->v, new_path->l); | |
52b7d2ce A |
829 | |
830 | vfree(path); | |
831 | path = new_path; | |
832 | } | |
833 | ||
d1e348cf | 834 | return path; |
52b7d2ce A |
835 | } |
836 | ||
d1e348cf | 837 | |
52b7d2ce | 838 | struct isakmpsa * |
d1e348cf | 839 | dupisakmpsa(struct isakmpsa *sa) |
52b7d2ce A |
840 | { |
841 | struct isakmpsa *res = NULL; | |
842 | ||
843 | if (sa == NULL) | |
844 | return NULL; | |
845 | ||
846 | res = newisakmpsa(); | |
847 | if(res == NULL) | |
848 | return NULL; | |
849 | ||
850 | *res = *sa; | |
851 | #ifdef HAVE_GSSAPI | |
d1e348cf | 852 | res->gssid=vdup(sa->gssid); |
52b7d2ce A |
853 | #endif |
854 | res->next=NULL; | |
855 | ||
856 | if (sa->dhgrp != NULL) | |
857 | oakley_setdhgroup(sa->dh_group, &(res->dhgrp)); | |
858 | ||
859 | return res; | |
860 | ||
861 | } | |
862 | ||
863 | void | |
864 | rsa_key_free(void *entry) | |
865 | { | |
866 | struct rsa_key *key = (struct rsa_key *)entry; | |
867 | ||
868 | if (key->src) | |
869 | free(key->src); | |
870 | if (key->dst) | |
871 | free(key->dst); | |
872 | if (key->rsa) | |
873 | RSA_free(key->rsa); | |
874 | free(key); | |
875 | } |