Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / setkey / test-policy.c
1 /*
2  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/usr.sbin/setkey/test-policy.c,v 1.1 2000/01/06 12:40:53 shin Exp $
30  * $DragonFly: src/usr.sbin/setkey/test-policy.c,v 1.2 2003/06/17 04:30:03 dillon Exp $
31  */
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet6/in6.h>
38 #include <netkey/keyv2.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <netinet6/ipsec.h>
43
44 char *requests[] = {
45 "must_error",           /* must be error */
46 "ipsec must_error",     /* must be error */
47 "ipsec esp/must_error", /* must be error */
48 "discard",
49 "none",
50 "entrust",
51 "bypass",               /* may be error */
52 "ipsec esp",            /* must be error */
53 "ipsec ah/require",
54 "ipsec ah/use/",
55 "ipsec esp/require ah/default/203.178.141.194",
56 "ipsec ah/use/203.178.141.195 esp/use/203.178.141.194",
57 "ipsec esp/elf.wide.ydc.co.jp esp/www.wide.ydc.co.jp"
58 "
59 ipsec esp/require ah/use esp/require/10.0.0.1
60 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
61 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
62 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
63 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
64 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
65 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
66 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
67 ah/use/3ffe:501:481d::1  ah/use/3ffe:501:481d::1ah/use/3ffe:501:481d::1
68 ",
69 };
70
71 u_char  *p_secpolicy;
72
73 int     test(char *buf, int family);
74 char    *setpolicy(char *req);
75
76 main()
77 {
78         int i;
79         char *buf;
80
81         for (i = 0; i < sizeof(requests)/sizeof(requests[0]); i++) {
82                 printf("* requests:[%s]\n", requests[i]);
83                 if ((buf = setpolicy(requests[i])) == NULL)
84                         continue;
85                 printf("\tsetlen:%d\n", PFKEY_EXTLEN(buf));
86
87                 printf("\tPF_INET:\n");
88                 test(buf, PF_INET);
89
90                 printf("\tPF_INET6:\n");
91                 test(buf, PF_INET6);
92                 free(buf);
93         }
94 }
95
96 int test(char *policy, int family)
97 {
98         int so, proto, optname;
99         int len;
100         char getbuf[1024];
101
102         switch (family) {
103         case PF_INET:
104                 proto = IPPROTO_IP;
105                 optname = IP_IPSEC_POLICY;
106                 break;
107         case PF_INET6:
108                 proto = IPPROTO_IPV6;
109                 optname = IPV6_IPSEC_POLICY;
110                 break;
111         }
112
113         if ((so = socket(family, SOCK_DGRAM, 0)) < 0)
114                 perror("socket");
115
116         if (setsockopt(so, proto, optname, policy, PFKEY_EXTLEN(policy)) < 0)
117                 perror("setsockopt");
118
119         len = sizeof(getbuf);
120         memset(getbuf, 0, sizeof(getbuf));
121         if (getsockopt(so, proto, optname, getbuf, &len) < 0)
122                 perror("getsockopt");
123
124     {
125         char *buf = NULL;
126
127         printf("\tgetlen:%d\n", len);
128
129         if ((buf = ipsec_dump_policy(getbuf, NULL)) == NULL)
130                 ipsec_strerror();
131         else
132                 printf("\t[%s]\n", buf);
133
134         free(buf);
135     }
136
137         close (so);
138 }
139
140 char *setpolicy(char *req)
141 {
142         int len;
143         char *buf;
144
145         if ((len = ipsec_get_policylen(req)) < 0) {
146                 printf("ipsec_get_policylen: %s\n", ipsec_strerror());
147                 return NULL;
148         }
149
150         if ((buf = malloc(len)) == NULL) {
151                 perror("malloc");
152                 return NULL;
153         }
154
155         if ((len = ipsec_set_policy(buf, len, req)) < 0) {
156                 printf("ipsec_set_policy: %s\n", ipsec_strerror());
157                 free(buf);
158                 return NULL;
159         }
160
161         return buf;
162 }