1 /* $KAME: localconf.c,v 1.32 2001/06/01 08:26:05 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/param.h>
48 #include "localconf.h"
49 #include "algorithm.h"
50 #include "isakmp_var.h"
52 #include "ipsec_doi.h"
53 #include "grabmyaddr.h"
60 struct localconf
*lcconf
;
62 static void setdefault
__P((void));
63 static vchar_t
*getpsk
__P((const char *, const int));
68 lcconf
= racoon_calloc(1, sizeof(*lcconf
));
70 errx(1, "failed to allocate local conf.");
74 lcconf
->racoon_conf
= LC_DEFAULT_CF
;
83 clear_myaddr(&lcconf
->myaddrs
);
84 for (i
= 0; i
< LC_PATHTYPE_MAX
; i
++) {
85 if (lcconf
->pathinfo
[i
]) {
86 racoon_free(lcconf
->pathinfo
[i
]);
87 lcconf
->pathinfo
[i
] = NULL
;
90 for (i
= 0; i
< LC_IDENTTYPE_MAX
; i
++) {
92 vfree(lcconf
->ident
[i
]);
93 lcconf
->ident
[i
] = NULL
;
100 lcconf
->autograbaddr
= 1;
101 lcconf
->port_isakmp
= PORT_ISAKMP
;
102 lcconf
->default_af
= AF_INET
;
103 lcconf
->pad_random
= LC_DEFAULT_PAD_RANDOM
;
104 lcconf
->pad_randomlen
= LC_DEFAULT_PAD_RANDOMLEN
;
105 lcconf
->pad_maxsize
= LC_DEFAULT_PAD_MAXSIZE
;
106 lcconf
->pad_strict
= LC_DEFAULT_PAD_STRICT
;
107 lcconf
->pad_excltail
= LC_DEFAULT_PAD_EXCLTAIL
;
108 lcconf
->retry_counter
= LC_DEFAULT_RETRY_COUNTER
;
109 lcconf
->retry_interval
= LC_DEFAULT_RETRY_INTERVAL
;
110 lcconf
->count_persend
= LC_DEFAULT_COUNT_PERSEND
;
111 lcconf
->secret_size
= LC_DEFAULT_SECRETSIZE
;
112 lcconf
->retry_checkph1
= LC_DEFAULT_RETRY_CHECKPH1
;
113 lcconf
->wait_ph2complete
= LC_DEFAULT_WAIT_PH2COMPLETE
;
114 lcconf
->strict_address
= FALSE
;
115 lcconf
->complex_bundle
= TRUE
; /*XXX FALSE;*/
128 id
= racoon_calloc(1, 1 + id0
->l
- sizeof(struct ipsecdoi_id_b
));
130 plog(LLV_ERROR
, LOCATION
, NULL
,
131 "failed to get psk buffer.\n");
134 memcpy(id
, id0
->v
+ sizeof(struct ipsecdoi_id_b
),
135 id0
->l
- sizeof(struct ipsecdoi_id_b
));
136 id
[id0
->l
- sizeof(struct ipsecdoi_id_b
)] = '\0';
138 key
= getpsk(id
, id0
->l
- sizeof(struct ipsecdoi_id_b
));
147 * get PSK by address.
151 struct sockaddr
*remote
;
154 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
156 GETNAMEINFO(remote
, addr
, port
);
158 key
= getpsk(addr
, strlen(addr
));
169 char buf
[1024]; /* XXX how is variable length ? */
175 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], 1) == 0)
176 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], "r");
180 plog(LLV_ERROR
, LOCATION
, NULL
,
181 "failed to open pre_share_key file %s\n",
182 lcconf
->pathinfo
[LC_PATHTYPE_PSK
]);
186 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
191 /* search the end of 1st string. */
192 for (p
= buf
; *p
!= '\0' && !isspace(*p
); p
++)
195 continue; /* no 2nd parameter */
197 /* search the 1st of 2nd string. */
198 while (isspace(*++p
))
201 continue; /* no 2nd parameter */
203 if (strncmp(buf
, str
, len
) == 0 && buf
[len
] == '\0') {
206 for (q
= p
; *q
!= '\0' && *q
!= '\n'; q
++)
210 /* fix key if hex string */
211 if (strncmp(p
, "0x", 2) == 0) {
212 k
= str2val(p
+ 2, 16, &keylen
);
214 plog(LLV_ERROR
, LOCATION
, NULL
,
215 "failed to get psk buffer.\n");
221 key
= vmalloc(keylen
);
223 plog(LLV_ERROR
, LOCATION
, NULL
,
224 "failed to allocate key buffer.\n");
227 memcpy(key
->v
, p
, key
->l
);
240 * get a file name of a type specified.
243 getpathname(path
, len
, type
, name
)
248 snprintf(path
, len
, "%s%s%s",
249 name
[0] == '/' ? "" : lcconf
->pathinfo
[type
],
250 name
[0] == '/' ? "" : "/",
253 plog(LLV_DEBUG
, LOCATION
, NULL
, "filename: %s\n", path
);
257 static int lc_doi2idtype
[] = {
261 LC_IDENTTYPE_USERFQDN
,
267 LC_IDENTTYPE_CERTNAME
,
273 * convert DOI value to idtype
281 if (ARRAYLEN(lc_doi2idtype
) > idtype
)
282 return lc_doi2idtype
[idtype
];
287 static int lc_sittype2doi
[] = {
288 IPSECDOI_SIT_IDENTITY_ONLY
,
289 IPSECDOI_SIT_SECRECY
,
290 IPSECDOI_SIT_INTEGRITY
,
294 * convert sittype to DOI value.
302 if (ARRAYLEN(lc_sittype2doi
) > sittype
)
303 return lc_sittype2doi
[sittype
];
307 static int lc_doitype2doi
[] = {
312 * convert doitype to DOI value.
320 if (ARRAYLEN(lc_doitype2doi
) > doitype
)
321 return lc_doitype2doi
[doitype
];