source: rtems-libbsd/ipsec-tools/src/racoon/localconf.c @ 8645c9d7

55-freebsd-126-freebsd-12
Last change on this file since 8645c9d7 was 8645c9d7, checked in by Christian Mauderer <christian.mauderer@…>, on 06/06/18 at 09:11:52

ipsec-tools: Apply patches from FreeBSD ports.

Source: https://svnweb.freebsd.org/ports/head/security/ipsec-tools/files/ revision 468617.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*      $NetBSD: localconf.c,v 1.7 2008/12/23 14:04:42 tteras Exp $     */
2
3/*      $KAME: localconf.c,v 1.33 2001/08/09 07:32:19 sakane Exp $      */
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
39#include <stdlib.h>
40#include <stdio.h>
41#include <string.h>
42#include <errno.h>
43#include <ctype.h>
44#include <err.h>
45
46#include "var.h"
47#include "misc.h"
48#include "vmbuf.h"
49#include "plog.h"
50#include "debug.h"
51
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"
65
66struct localconf *lcconf;
67
68static void setdefault __P((void));
69
70void
71initlcconf()
72{
73        lcconf = racoon_calloc(1, sizeof(*lcconf));
74        if (lcconf == NULL)
75                errx(1, "failed to allocate local conf.");
76
77        setdefault();
78
79        lcconf->racoon_conf = LC_DEFAULT_CF;
80}
81
82void
83flushlcconf()
84{
85        int i;
86
87        setdefault();
88        myaddr_flush();
89
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;
94                }
95        }
96}
97
98static void
99setdefault()
100{
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;
123}
124
125/*
126 * get PSK by string.
127 */
128vchar_t *
129getpskbyname(id0)
130        vchar_t *id0;
131{
132        char *id;
133        vchar_t *key = NULL;
134
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;
140        }
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';
144
145        key = privsep_getpsk(id, id0->l - sizeof(struct ipsecdoi_id_b));
146
147end:
148        if (id)
149                racoon_free(id);
150        return key;
151}
152
153/*
154 * get PSK by address.
155 */
156vchar_t *
157getpskbyaddr(remote)
158        struct sockaddr *remote;
159{
160        vchar_t *key = NULL;
161        char addr[NI_MAXHOST], port[NI_MAXSERV];
162
163        GETNAMEINFO(remote, addr, port);
164
165        key = privsep_getpsk(addr, strlen(addr));
166
167        return key;
168}
169
170vchar_t *
171getpsk(str, len)
172        const char *str;
173        const int len;
174{
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;
181
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;
191        }
192
193        while (fgets(buf, sizeof(buf), fp) != NULL) {
194                /* comment line */
195                if (buf[0] == '#')
196                        continue;
197
198                /* search the end of 1st string. */
199                for (p = buf; *p != '\0' && !isspace((int)*p); p++)
200                        ;
201                if (*p == '\0')
202                        continue;       /* no 2nd parameter */
203                *p = '\0';
204                /* search the 1st of 2nd string. */
205                while (isspace((int)*++p))
206                        ;
207                if (*p == '\0')
208                        continue;       /* no 2nd parameter */
209                p--;
210                if (strcmp(buf, "*") == 0
211                 || (strncmp(buf, str, len) == 0 && buf[len] == '\0')) {
212                        p++;
213                        keylen = 0;
214                        for (q = p; *q != '\0' && *q != '\n'; q++)
215                                keylen++;
216                        *q = '\0';
217
218                        /* fix key if hex string */
219                        if (strncmp(p, "0x", 2) == 0) {
220                                k = str2val(p + 2, 16, &keylen);
221                                if (k == NULL) {
222                                        plog(LLV_ERROR, LOCATION, NULL,
223                                                "failed to get psk buffer.\n");
224                                        goto end;
225                                }
226                                p = k;
227                        }
228
229                        key = vmalloc(keylen);
230                        if (key == NULL) {
231                                plog(LLV_ERROR, LOCATION, NULL,
232                                        "failed to allocate key buffer.\n");
233                                goto end;
234                        }
235                        memcpy(key->v, p, key->l);
236                        if (k)
237                                racoon_free(k);
238                        goto end;
239                }
240        }
241
242end:
243        fclose(fp);
244        return key;
245}
246
247/*
248 * get a file name of a type specified.
249 */
250void
251getpathname(path, len, type, name)
252        char *path;
253        int len, type;
254        const char *name;
255{
256        snprintf(path, len, "%s%s%s",
257                name[0] == '/' ? "" : lcconf->pathinfo[type],
258                name[0] == '/' ? "" : "/",
259                name);
260
261        plog(LLV_DEBUG, LOCATION, NULL, "filename: %s\n", path);
262}
263
264#if 0 /* DELETEIT */
265static int lc_doi2idtype[] = {
266        -1,
267        -1,
268        LC_IDENTTYPE_FQDN,
269        LC_IDENTTYPE_USERFQDN,
270        -1,
271        -1,
272        -1,
273        -1,
274        -1,
275        LC_IDENTTYPE_CERTNAME,
276        -1,
277        LC_IDENTTYPE_KEYID,
278};
279
280/*
281 * convert DOI value to idtype
282 * OUT  -1   : NG
283 *      other: converted.
284 */
285int
286doi2idtype(idtype)
287        int idtype;
288{
289        if (ARRAYLEN(lc_doi2idtype) > idtype)
290                return lc_doi2idtype[idtype];
291        return -1;
292}
293#endif
294
295static int lc_sittype2doi[] = {
296        IPSECDOI_SIT_IDENTITY_ONLY,
297        IPSECDOI_SIT_SECRECY,
298        IPSECDOI_SIT_INTEGRITY,
299};
300
301/*
302 * convert sittype to DOI value.
303 * OUT  -1   : NG
304 *      other: converted.
305 */
306int
307sittype2doi(sittype)
308        int sittype;
309{
310        if (ARRAYLEN(lc_sittype2doi) > sittype)
311                return lc_sittype2doi[sittype];
312        return -1;
313}
314
315static int lc_doitype2doi[] = {
316        IPSEC_DOI,
317};
318
319/*
320 * convert doitype to DOI value.
321 * OUT  -1   : NG
322 *      other: converted.
323 */
324int
325doitype2doi(doitype)
326        int doitype;
327{
328        if (ARRAYLEN(lc_doitype2doi) > doitype)
329                return lc_doitype2doi[doitype];
330        return -1;
331}
332
333
334
335static void
336saverestore_params(f)
337        int f;
338{
339        static u_int16_t s_port_isakmp;
340
341        /* 0: save, 1: restore */
342        if (f) {
343                lcconf->port_isakmp = s_port_isakmp;
344        } else {
345                s_port_isakmp = lcconf->port_isakmp;
346        }
347}
348
349void
350restore_params()
351{
352        saverestore_params(1);
353}
354
355void
356save_params()
357{
358        saverestore_params(0);
359}
Note: See TracBrowser for help on using the repository browser.