1 /* $NetBSD: localconf.c,v 1.6 2008/12/23 14:03:12 tteras Exp $ */
3 /* $KAME: localconf.c,v 1.33 2001/08/09 07:32:19 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/types.h>
37 #include <sys/param.h>
52 #include "localconf.h"
53 #include "algorithm.h"
56 #include "isakmp_var.h"
58 #include "ipsec_doi.h"
59 #include "grabmyaddr.h"
66 struct localconf
*lcconf
;
68 static void setdefault
__P((void));
73 lcconf
= racoon_calloc(1, sizeof(*lcconf
));
75 errx(1, "failed to allocate local conf.");
79 lcconf
->racoon_conf
= LC_DEFAULT_CF
;
90 for (i
= 0; i
< LC_PATHTYPE_MAX
; i
++) {
91 if (lcconf
->pathinfo
[i
]) {
92 racoon_free(lcconf
->pathinfo
[i
]);
93 lcconf
->pathinfo
[i
] = NULL
;
103 lcconf
->chroot
= NULL
;
104 lcconf
->port_isakmp
= PORT_ISAKMP
;
105 lcconf
->port_isakmp_natt
= PORT_ISAKMP_NATT
;
106 lcconf
->default_af
= AF_INET
;
107 lcconf
->pad_random
= LC_DEFAULT_PAD_RANDOM
;
108 lcconf
->pad_randomlen
= LC_DEFAULT_PAD_RANDOMLEN
;
109 lcconf
->pad_maxsize
= LC_DEFAULT_PAD_MAXSIZE
;
110 lcconf
->pad_strict
= LC_DEFAULT_PAD_STRICT
;
111 lcconf
->pad_excltail
= LC_DEFAULT_PAD_EXCLTAIL
;
112 lcconf
->retry_counter
= LC_DEFAULT_RETRY_COUNTER
;
113 lcconf
->retry_interval
= LC_DEFAULT_RETRY_INTERVAL
;
114 lcconf
->count_persend
= LC_DEFAULT_COUNT_PERSEND
;
115 lcconf
->secret_size
= LC_DEFAULT_SECRETSIZE
;
116 lcconf
->retry_checkph1
= LC_DEFAULT_RETRY_CHECKPH1
;
117 lcconf
->wait_ph2complete
= LC_DEFAULT_WAIT_PH2COMPLETE
;
118 lcconf
->strict_address
= FALSE
;
119 lcconf
->complex_bundle
= TRUE
; /*XXX FALSE;*/
120 lcconf
->gss_id_enc
= LC_GSSENC_UTF16LE
; /* Windows compatibility */
121 lcconf
->natt_ka_interval
= LC_DEFAULT_NATT_KA_INTERVAL
;
122 lcconf
->pfkey_buffer_size
= LC_DEFAULT_PFKEY_BUFFER_SIZE
;
135 id
= racoon_calloc(1, 1 + id0
->l
- sizeof(struct ipsecdoi_id_b
));
137 plog(LLV_ERROR
, LOCATION
, NULL
,
138 "failed to get psk buffer.\n");
141 memcpy(id
, id0
->v
+ sizeof(struct ipsecdoi_id_b
),
142 id0
->l
- sizeof(struct ipsecdoi_id_b
));
143 id
[id0
->l
- sizeof(struct ipsecdoi_id_b
)] = '\0';
145 key
= privsep_getpsk(id
, id0
->l
- sizeof(struct ipsecdoi_id_b
));
154 * get PSK by address.
158 struct sockaddr
*remote
;
161 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
163 GETNAMEINFO(remote
, addr
, port
);
165 key
= privsep_getpsk(addr
, strlen(addr
));
176 char buf
[1024]; /* XXX how is variable length ? */
182 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], 1) == 0)
183 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], "r");
187 plog(LLV_ERROR
, LOCATION
, NULL
,
188 "failed to open pre_share_key file %s\n",
189 lcconf
->pathinfo
[LC_PATHTYPE_PSK
]);
193 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
198 /* search the end of 1st string. */
199 for (p
= buf
; *p
!= '\0' && !isspace((int)*p
); p
++)
202 continue; /* no 2nd parameter */
204 /* search the 1st of 2nd string. */
205 while (isspace((int)*++p
))
208 continue; /* no 2nd parameter */
210 if (strncmp(buf
, str
, len
) == 0 && buf
[len
] == '\0') {
213 for (q
= p
; *q
!= '\0' && *q
!= '\n'; q
++)
217 /* fix key if hex string */
218 if (strncmp(p
, "0x", 2) == 0) {
219 k
= str2val(p
+ 2, 16, &keylen
);
221 plog(LLV_ERROR
, LOCATION
, NULL
,
222 "failed to get psk buffer.\n");
228 key
= vmalloc(keylen
);
230 plog(LLV_ERROR
, LOCATION
, NULL
,
231 "failed to allocate key buffer.\n");
234 memcpy(key
->v
, p
, key
->l
);
247 * get a file name of a type specified.
250 getpathname(path
, len
, type
, name
)
255 snprintf(path
, len
, "%s%s%s",
256 name
[0] == '/' ? "" : lcconf
->pathinfo
[type
],
257 name
[0] == '/' ? "" : "/",
260 plog(LLV_DEBUG
, LOCATION
, NULL
, "filename: %s\n", path
);
264 static int lc_doi2idtype
[] = {
268 LC_IDENTTYPE_USERFQDN
,
274 LC_IDENTTYPE_CERTNAME
,
280 * convert DOI value to idtype
288 if (ARRAYLEN(lc_doi2idtype
) > idtype
)
289 return lc_doi2idtype
[idtype
];
294 static int lc_sittype2doi
[] = {
295 IPSECDOI_SIT_IDENTITY_ONLY
,
296 IPSECDOI_SIT_SECRECY
,
297 IPSECDOI_SIT_INTEGRITY
,
301 * convert sittype to DOI value.
309 if (ARRAYLEN(lc_sittype2doi
) > sittype
)
310 return lc_sittype2doi
[sittype
];
314 static int lc_doitype2doi
[] = {
319 * convert doitype to DOI value.
327 if (ARRAYLEN(lc_doitype2doi
) > doitype
)
328 return lc_doitype2doi
[doitype
];
335 saverestore_params(f
)
338 static u_int16_t s_port_isakmp
;
340 /* 0: save, 1: restore */
342 lcconf
->port_isakmp
= s_port_isakmp
;
344 s_port_isakmp
= lcconf
->port_isakmp
;
351 saverestore_params(1);
357 saverestore_params(0);