No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / ipsec-tools / src / racoon / localconf.c
blobc4a4c020398cb79fd1e4f1b45a2cc2de68e1f3bc
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 $ */
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.
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.
34 #include "config.h"
36 #include <sys/types.h>
37 #include <sys/param.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <ctype.h>
44 #include <err.h>
46 #include "var.h"
47 #include "misc.h"
48 #include "vmbuf.h"
49 #include "plog.h"
50 #include "debug.h"
52 #include "localconf.h"
53 #include "algorithm.h"
54 #include "admin.h"
55 #include "privsep.h"
56 #include "isakmp_var.h"
57 #include "isakmp.h"
58 #include "ipsec_doi.h"
59 #include "grabmyaddr.h"
60 #include "vendorid.h"
61 #include "str2val.h"
62 #include "safefile.h"
63 #include "admin.h"
64 #include "gcmalloc.h"
66 struct localconf *lcconf;
68 static void setdefault __P((void));
70 void
71 initlcconf()
73 lcconf = racoon_calloc(1, sizeof(*lcconf));
74 if (lcconf == NULL)
75 errx(1, "failed to allocate local conf.");
77 setdefault();
79 lcconf->racoon_conf = LC_DEFAULT_CF;
82 void
83 flushlcconf()
85 int i;
87 setdefault();
88 myaddr_flush();
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;
98 static void
99 setdefault()
101 lcconf->uid = 0;
102 lcconf->gid = 0;
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;
126 * get PSK by string.
128 vchar_t *
129 getpskbyname(id0)
130 vchar_t *id0;
132 char *id;
133 vchar_t *key = NULL;
135 id = racoon_calloc(1, 1 + id0->l - sizeof(struct ipsecdoi_id_b));
136 if (id == NULL) {
137 plog(LLV_ERROR, LOCATION, NULL,
138 "failed to get psk buffer.\n");
139 goto end;
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));
147 end:
148 if (id)
149 racoon_free(id);
150 return key;
154 * get PSK by address.
156 vchar_t *
157 getpskbyaddr(remote)
158 struct sockaddr *remote;
160 vchar_t *key = NULL;
161 char addr[NI_MAXHOST], port[NI_MAXSERV];
163 GETNAMEINFO(remote, addr, port);
165 key = privsep_getpsk(addr, strlen(addr));
167 return key;
170 vchar_t *
171 getpsk(str, len)
172 const char *str;
173 const int len;
175 FILE *fp;
176 char buf[1024]; /* XXX how is variable length ? */
177 vchar_t *key = NULL;
178 char *p, *q;
179 size_t keylen;
180 char *k = NULL;
182 if (safefile(lcconf->pathinfo[LC_PATHTYPE_PSK], 1) == 0)
183 fp = fopen(lcconf->pathinfo[LC_PATHTYPE_PSK], "r");
184 else
185 fp = NULL;
186 if (fp == NULL) {
187 plog(LLV_ERROR, LOCATION, NULL,
188 "failed to open pre_share_key file %s\n",
189 lcconf->pathinfo[LC_PATHTYPE_PSK]);
190 return NULL;
193 while (fgets(buf, sizeof(buf), fp) != NULL) {
194 /* comment line */
195 if (buf[0] == '#')
196 continue;
198 /* search the end of 1st string. */
199 for (p = buf; *p != '\0' && !isspace((int)*p); p++)
201 if (*p == '\0')
202 continue; /* no 2nd parameter */
203 *p = '\0';
204 /* search the 1st of 2nd string. */
205 while (isspace((int)*++p))
207 if (*p == '\0')
208 continue; /* no 2nd parameter */
209 p--;
210 if (strncmp(buf, str, len) == 0 && buf[len] == '\0') {
211 p++;
212 keylen = 0;
213 for (q = p; *q != '\0' && *q != '\n'; q++)
214 keylen++;
215 *q = '\0';
217 /* fix key if hex string */
218 if (strncmp(p, "0x", 2) == 0) {
219 k = str2val(p + 2, 16, &keylen);
220 if (k == NULL) {
221 plog(LLV_ERROR, LOCATION, NULL,
222 "failed to get psk buffer.\n");
223 goto end;
225 p = k;
228 key = vmalloc(keylen);
229 if (key == NULL) {
230 plog(LLV_ERROR, LOCATION, NULL,
231 "failed to allocate key buffer.\n");
232 goto end;
234 memcpy(key->v, p, key->l);
235 if (k)
236 racoon_free(k);
237 goto end;
241 end:
242 fclose(fp);
243 return key;
247 * get a file name of a type specified.
249 void
250 getpathname(path, len, type, name)
251 char *path;
252 int len, type;
253 const char *name;
255 snprintf(path, len, "%s%s%s",
256 name[0] == '/' ? "" : lcconf->pathinfo[type],
257 name[0] == '/' ? "" : "/",
258 name);
260 plog(LLV_DEBUG, LOCATION, NULL, "filename: %s\n", path);
263 #if 0 /* DELETEIT */
264 static int lc_doi2idtype[] = {
267 LC_IDENTTYPE_FQDN,
268 LC_IDENTTYPE_USERFQDN,
274 LC_IDENTTYPE_CERTNAME,
276 LC_IDENTTYPE_KEYID,
280 * convert DOI value to idtype
281 * OUT -1 : NG
282 * other: converted.
285 doi2idtype(idtype)
286 int idtype;
288 if (ARRAYLEN(lc_doi2idtype) > idtype)
289 return lc_doi2idtype[idtype];
290 return -1;
292 #endif
294 static int lc_sittype2doi[] = {
295 IPSECDOI_SIT_IDENTITY_ONLY,
296 IPSECDOI_SIT_SECRECY,
297 IPSECDOI_SIT_INTEGRITY,
301 * convert sittype to DOI value.
302 * OUT -1 : NG
303 * other: converted.
306 sittype2doi(sittype)
307 int sittype;
309 if (ARRAYLEN(lc_sittype2doi) > sittype)
310 return lc_sittype2doi[sittype];
311 return -1;
314 static int lc_doitype2doi[] = {
315 IPSEC_DOI,
319 * convert doitype to DOI value.
320 * OUT -1 : NG
321 * other: converted.
324 doitype2doi(doitype)
325 int doitype;
327 if (ARRAYLEN(lc_doitype2doi) > doitype)
328 return lc_doitype2doi[doitype];
329 return -1;
334 static void
335 saverestore_params(f)
336 int f;
338 static u_int16_t s_port_isakmp;
340 /* 0: save, 1: restore */
341 if (f) {
342 lcconf->port_isakmp = s_port_isakmp;
343 } else {
344 s_port_isakmp = lcconf->port_isakmp;
348 void
349 restore_params()
351 saverestore_params(1);
354 void
355 save_params()
357 saverestore_params(0);