1 | /* $NetBSD: kmpstat.c,v 1.7 2010/11/12 09:08:26 tteras Exp $ */ |
---|
2 | |
---|
3 | /* $KAME: kmpstat.c,v 1.33 2004/08/16 08:20:28 itojun 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 | #include <sys/socket.h> |
---|
39 | #include <sys/un.h> |
---|
40 | |
---|
41 | #include <netinet/in.h> |
---|
42 | #include <arpa/inet.h> |
---|
43 | #include <net/pfkeyv2.h> |
---|
44 | |
---|
45 | #include <stdlib.h> |
---|
46 | #include <stdio.h> |
---|
47 | #include <string.h> |
---|
48 | #include <errno.h> |
---|
49 | #if TIME_WITH_SYS_TIME |
---|
50 | # include <sys/time.h> |
---|
51 | # include <time.h> |
---|
52 | #else |
---|
53 | # if HAVE_SYS_TIME_H |
---|
54 | # include <sys/time.h> |
---|
55 | # else |
---|
56 | # include <time.h> |
---|
57 | # endif |
---|
58 | #endif |
---|
59 | #include <netdb.h> |
---|
60 | #ifdef HAVE_UNISTD_H |
---|
61 | #include <unistd.h> |
---|
62 | #endif |
---|
63 | #include <err.h> |
---|
64 | #include <sys/ioctl.h> |
---|
65 | #include <resolv.h> |
---|
66 | |
---|
67 | #include "libpfkey.h" |
---|
68 | |
---|
69 | #include "var.h" |
---|
70 | #include "misc.h" |
---|
71 | #include "vmbuf.h" |
---|
72 | #include "plog.h" |
---|
73 | #include "debug.h" |
---|
74 | #include "sockmisc.h" |
---|
75 | |
---|
76 | #include "racoonctl.h" |
---|
77 | #include "admin.h" |
---|
78 | #include "schedule.h" |
---|
79 | #include "isakmp_var.h" |
---|
80 | #include "isakmp.h" |
---|
81 | #include "isakmp_xauth.h" |
---|
82 | #include "isakmp_var.h" |
---|
83 | #include "isakmp_cfg.h" |
---|
84 | #include "oakley.h" |
---|
85 | #include "handler.h" |
---|
86 | #include "pfkey.h" |
---|
87 | #include "admin.h" |
---|
88 | #include "evt.h" |
---|
89 | #include "admin_var.h" |
---|
90 | #include "ipsec_doi.h" |
---|
91 | |
---|
92 | u_int32_t racoonctl_interface = RACOONCTL_INTERFACE; |
---|
93 | u_int32_t racoonctl_interface_major = RACOONCTL_INTERFACE_MAJOR; |
---|
94 | |
---|
95 | static int so; |
---|
96 | u_int32_t loglevel = 0; |
---|
97 | |
---|
98 | int |
---|
99 | com_init() |
---|
100 | { |
---|
101 | struct sockaddr_un name; |
---|
102 | |
---|
103 | memset(&name, 0, sizeof(name)); |
---|
104 | name.sun_family = AF_UNIX; |
---|
105 | snprintf(name.sun_path, sizeof(name.sun_path), |
---|
106 | "%s", adminsock_path); |
---|
107 | |
---|
108 | so = socket(AF_UNIX, SOCK_STREAM, 0); |
---|
109 | if (so < 0) |
---|
110 | return -1; |
---|
111 | |
---|
112 | if (connect(so, (struct sockaddr *)&name, sizeof(name)) < 0) { |
---|
113 | (void)close(so); |
---|
114 | return -1; |
---|
115 | } |
---|
116 | |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | int |
---|
121 | com_send(combuf) |
---|
122 | vchar_t *combuf; |
---|
123 | { |
---|
124 | int len; |
---|
125 | |
---|
126 | if ((len = send(so, combuf->v, combuf->l, 0)) == -1) { |
---|
127 | perror("send"); |
---|
128 | (void)close(so); |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | int |
---|
136 | com_recv(combufp) |
---|
137 | vchar_t **combufp; |
---|
138 | { |
---|
139 | struct admin_com h, *com; |
---|
140 | caddr_t buf; |
---|
141 | int len, rlen; |
---|
142 | int l = 0; |
---|
143 | caddr_t p; |
---|
144 | |
---|
145 | if (combufp == NULL) |
---|
146 | return -1; |
---|
147 | |
---|
148 | /* receive by PEEK */ |
---|
149 | if ((len = recv(so, &h, sizeof(h), MSG_PEEK)) == -1) |
---|
150 | goto bad1; |
---|
151 | |
---|
152 | /* sanity check */ |
---|
153 | if (len < sizeof(h)) |
---|
154 | goto bad1; |
---|
155 | |
---|
156 | if (h.ac_errno && !(h.ac_cmd & ADMIN_FLAG_LONG_REPLY)) { |
---|
157 | errno = h.ac_errno; |
---|
158 | goto bad1; |
---|
159 | } |
---|
160 | |
---|
161 | /* real length */ |
---|
162 | if (h.ac_cmd & ADMIN_FLAG_LONG_REPLY) |
---|
163 | rlen = ((u_int32_t)h.ac_len) + (((u_int32_t)h.ac_len_high) << 16); |
---|
164 | else |
---|
165 | rlen = h.ac_len; |
---|
166 | |
---|
167 | /* allocate buffer */ |
---|
168 | if ((*combufp = vmalloc(rlen)) == NULL) |
---|
169 | goto bad1; |
---|
170 | |
---|
171 | /* read real message */ |
---|
172 | p = (*combufp)->v; |
---|
173 | while (l < rlen) { |
---|
174 | if ((len = recv(so, p, rlen - l, 0)) < 0) { |
---|
175 | perror("recv"); |
---|
176 | goto bad2; |
---|
177 | } |
---|
178 | l += len; |
---|
179 | p += len; |
---|
180 | } |
---|
181 | |
---|
182 | return 0; |
---|
183 | |
---|
184 | bad2: |
---|
185 | vfree(*combufp); |
---|
186 | bad1: |
---|
187 | *combufp = NULL; |
---|
188 | return -1; |
---|
189 | } |
---|
190 | |
---|
191 | /* |
---|
192 | * Dumb plog functions (used by sockmisc.c) |
---|
193 | */ |
---|
194 | void |
---|
195 | _plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...) |
---|
196 | { |
---|
197 | va_list ap; |
---|
198 | |
---|
199 | va_start(ap, fmt); |
---|
200 | vprintf(fmt, ap); |
---|
201 | va_end(ap); |
---|
202 | } |
---|
203 | |
---|
204 | void |
---|
205 | plogdump(pri, data, len) |
---|
206 | int pri; |
---|
207 | void *data; |
---|
208 | size_t len; |
---|
209 | { |
---|
210 | return; |
---|
211 | } |
---|
212 | |
---|
213 | struct sockaddr * |
---|
214 | get_sockaddr(family, name, port) |
---|
215 | int family; |
---|
216 | char *name, *port; |
---|
217 | { |
---|
218 | struct addrinfo hint, *ai; |
---|
219 | int error; |
---|
220 | |
---|
221 | memset(&hint, 0, sizeof(hint)); |
---|
222 | hint.ai_family = PF_UNSPEC; |
---|
223 | hint.ai_family = family; |
---|
224 | hint.ai_socktype = SOCK_STREAM; |
---|
225 | |
---|
226 | error = getaddrinfo(name, port, &hint, &ai); |
---|
227 | if (error != 0) { |
---|
228 | printf("%s: %s/%s\n", gai_strerror(error), name, port); |
---|
229 | return NULL; |
---|
230 | } |
---|
231 | |
---|
232 | return ai->ai_addr; |
---|
233 | } |
---|