Home | History | Annotate | Line # | Download | only in inetd
ipsec.c revision 1.4.40.1
      1  1.4.40.1  christos /*	$NetBSD: ipsec.c,v 1.4.40.1 2019/06/10 22:10:30 christos Exp $	*/
      2       1.1    itojun 
      3       1.1    itojun /*
      4       1.1    itojun  * Copyright (C) 1999 WIDE Project.
      5       1.1    itojun  * All rights reserved.
      6       1.1    itojun  *
      7       1.1    itojun  * Redistribution and use in source and binary forms, with or without
      8       1.1    itojun  * modification, are permitted provided that the following conditions
      9       1.1    itojun  * are met:
     10       1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     11       1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     12       1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    itojun  *    documentation and/or other materials provided with the distribution.
     15       1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     16       1.1    itojun  *    may be used to endorse or promote products derived from this software
     17       1.1    itojun  *    without specific prior written permission.
     18       1.1    itojun  *
     19       1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20       1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23       1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1    itojun  * SUCH DAMAGE.
     30       1.1    itojun  */
     31       1.1    itojun 
     32       1.1    itojun #include <sys/param.h>
     33       1.1    itojun #include <sys/stat.h>
     34       1.1    itojun #include <sys/socket.h>
     35       1.1    itojun 
     36       1.1    itojun #include <netinet/in.h>
     37       1.1    itojun #include <arpa/inet.h>
     38       1.1    itojun 
     39       1.1    itojun #include <stdio.h>
     40       1.1    itojun #include <stdlib.h>
     41       1.1    itojun #include <string.h>
     42       1.1    itojun #include <unistd.h>
     43       1.1    itojun #include <ctype.h>
     44       1.1    itojun 
     45       1.1    itojun #ifdef IPSEC
     46       1.4  drochner #include <netipsec/ipsec.h>
     47       1.1    itojun #ifndef IPSEC_POLICY_IPSEC	/* no ipsec support on old ipsec */
     48       1.1    itojun #undef IPSEC
     49       1.1    itojun #endif
     50       1.1    itojun #endif
     51       1.1    itojun 
     52       1.1    itojun #include "ipsec.h"
     53       1.1    itojun 
     54       1.1    itojun #ifdef IPSEC
     55       1.1    itojun int
     56       1.3  christos ipsecsetup(int af, int fd, const char *policy)
     57       1.1    itojun {
     58       1.1    itojun 	char *p0, *p;
     59       1.1    itojun 	int error;
     60       1.1    itojun 
     61  1.4.40.1  christos 	if (!policy || *policy == '\0')
     62       1.1    itojun 		p0 = p = strdup("in entrust; out entrust");
     63       1.1    itojun 	else
     64       1.1    itojun 		p0 = p = strdup(policy);
     65       1.1    itojun 
     66       1.1    itojun 	error = 0;
     67       1.3  christos 	for (;;) {
     68       1.1    itojun 		p = strtok(p, ";");
     69       1.1    itojun 		if (p == NULL)
     70       1.1    itojun 			break;
     71       1.2       dsl 		while (*p && isspace((unsigned char)*p))
     72       1.1    itojun 			p++;
     73       1.1    itojun 		if (!*p) {
     74       1.1    itojun 			p = NULL;
     75       1.1    itojun 			continue;
     76       1.1    itojun 		}
     77       1.1    itojun 		error = ipsecsetup0(af, fd, p, 1);
     78       1.1    itojun 		if (error < 0)
     79       1.1    itojun 			break;
     80       1.1    itojun 		p = NULL;
     81       1.1    itojun 	}
     82       1.1    itojun 
     83       1.1    itojun 	free(p0);
     84       1.1    itojun 	return error;
     85       1.1    itojun }
     86       1.1    itojun 
     87       1.1    itojun int
     88       1.3  christos ipsecsetup_test(const char *policy)
     89       1.1    itojun {
     90       1.1    itojun 	char *p0, *p;
     91       1.1    itojun 	char *buf;
     92       1.1    itojun 	int error;
     93       1.1    itojun 
     94       1.1    itojun 	if (!policy)
     95       1.1    itojun 		return -1;
     96       1.3  christos 	p0 = p = strdup(policy);
     97       1.3  christos 	if (p == NULL)
     98       1.3  christos 		return -1;
     99       1.1    itojun 
    100       1.1    itojun 	error = 0;
    101       1.3  christos 	for (;;) {
    102       1.1    itojun 		p = strtok(p, ";");
    103       1.1    itojun 		if (p == NULL)
    104       1.1    itojun 			break;
    105       1.2       dsl 		while (*p && isspace((unsigned char)*p))
    106       1.1    itojun 			p++;
    107       1.1    itojun 		if (!*p) {
    108       1.1    itojun 			p = NULL;
    109       1.1    itojun 			continue;
    110       1.1    itojun 		}
    111       1.3  christos 		buf = ipsec_set_policy(p, (int)strlen(p));
    112       1.1    itojun 		if (buf == NULL) {
    113       1.1    itojun 			error = -1;
    114       1.1    itojun 			break;
    115       1.1    itojun 		}
    116       1.1    itojun 		free(buf);
    117       1.1    itojun 		p = NULL;
    118       1.1    itojun 	}
    119       1.1    itojun 
    120       1.1    itojun 	free(p0);
    121       1.1    itojun 	return error;
    122       1.1    itojun }
    123       1.1    itojun 
    124       1.1    itojun int
    125       1.3  christos ipsecsetup0(int af, int fd, const char *policy, int commit)
    126       1.1    itojun {
    127       1.1    itojun 	int level;
    128       1.1    itojun 	int opt;
    129       1.1    itojun 	char *buf;
    130       1.1    itojun 	int error;
    131       1.1    itojun 
    132       1.1    itojun 	switch (af) {
    133       1.1    itojun 	case AF_INET:
    134       1.1    itojun 		level = IPPROTO_IP;
    135       1.1    itojun 		opt = IP_IPSEC_POLICY;
    136       1.1    itojun 		break;
    137       1.1    itojun #ifdef INET6
    138       1.1    itojun 	case AF_INET6:
    139       1.1    itojun 		level = IPPROTO_IPV6;
    140       1.1    itojun 		opt = IPV6_IPSEC_POLICY;
    141       1.1    itojun 		break;
    142       1.1    itojun #endif
    143       1.1    itojun 	default:
    144       1.1    itojun 		return -1;
    145       1.1    itojun 	}
    146       1.1    itojun 
    147       1.3  christos 	buf = ipsec_set_policy(policy, (int)strlen(policy));
    148       1.1    itojun 	if (buf != NULL) {
    149       1.1    itojun 		error = 0;
    150       1.1    itojun 		if (commit && setsockopt(fd, level, opt,
    151       1.3  christos 		    buf, (socklen_t)ipsec_get_policylen(buf)) < 0) {
    152       1.1    itojun 			error = -1;
    153       1.1    itojun 		}
    154       1.1    itojun 		free(buf);
    155       1.1    itojun 	} else
    156       1.1    itojun 		error = -1;
    157       1.1    itojun 	return error;
    158       1.1    itojun }
    159       1.1    itojun #endif
    160