blob: ed0ea7eab178c1f6d3c4e98ab16fc04cfb8b0743 [file] [log] [blame]
Chia-chi Yeh837a1c72009-06-26 09:40:31 +08001/*
Chia-chi Yehe9fc3762011-07-07 03:20:34 -07002 * Copyright (C) 2011 The Android Open Source Project
Chia-chi Yeh837a1c72009-06-26 09:40:31 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070020#include <unistd.h>
Chia-chi Yeh8f3b3882011-07-07 13:43:20 -070021#include <sys/param.h>
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080022#include <sys/types.h>
23#include <sys/socket.h>
Chia-chi Yehc4549542009-07-22 06:46:14 +080024#include <netinet/in.h>
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080025#include <netinet/ip.h>
Chia-chi Yeh7197eb72009-07-13 16:43:29 +080026#include <netdb.h>
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070027#include <fcntl.h>
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080028
29#include "config.h"
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070030#include "gcmalloc.h"
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080031#include "libpfkey.h"
32#include "var.h"
33#include "isakmp_var.h"
34#include "isakmp.h"
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -070035#include "isakmp_xauth.h"
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080036#include "vmbuf.h"
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070037#include "crypto_openssl.h"
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080038#include "oakley.h"
39#include "ipsec_doi.h"
40#include "algorithm.h"
41#include "vendorid.h"
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070042#include "schedule.h"
43#include "pfkey.h"
44#include "nattraversal.h"
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080045#include "proposal.h"
46#include "sainfo.h"
47#include "localconf.h"
48#include "remoteconf.h"
49#include "sockmisc.h"
50#include "grabmyaddr.h"
51#include "plog.h"
Chia-chi Yehbd5fa3c2009-07-07 16:24:13 +080052#include "admin.h"
53#include "privsep.h"
Chia-chi Yeh514ffe22011-07-07 13:52:27 -070054#include "throttle.h"
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070055#include "misc.h"
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080056
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080057static struct localconf localconf;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080058static struct sainfo sainfo;
59static char *pre_shared_key;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070060
61static char *interface;
Chia-chi Yehb0d8f172011-07-12 15:14:38 -070062static struct sockaddr *targets[2];
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070063static struct {
64 struct sockaddr *addr;
65 int fd;
Chia-chi Yehb0d8f172011-07-12 15:14:38 -070066} sources[2];
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080067
68struct localconf *lcconf = &localconf;
69char *script_names[SCRIPT_MAX + 1];
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070070int f_local = 0;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080071
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070072/*****************************************************************************/
Chia-chi Yeh837a1c72009-06-26 09:40:31 +080073
74static void add_sainfo_algorithm(int class, int algorithm, int length)
75{
76 struct sainfoalg *p = calloc(1, sizeof(struct sainfoalg));
77 p->alg = algorithm;
78 p->encklen = length;
79
80 if (!sainfo.algs[class]) {
81 sainfo.algs[class] = p;
82 } else {
83 struct sainfoalg *q = sainfo.algs[class];
84 while (q->next) {
85 q = q->next;
86 }
87 q->next = p;
88 }
89}
90
Chia-chi Yehf8a6a762011-07-04 17:21:23 -070091static void set_globals(char *interfaze, char *server)
92{
93 struct addrinfo hints = {
94 .ai_flags = AI_NUMERICSERV,
95#ifndef INET6
96 .ai_family = AF_INET,
97#else
98 .ai_family = AF_UNSPEC,
99#endif
100 .ai_socktype = SOCK_DGRAM,
101 };
102 struct addrinfo *info;
103
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700104 if (getaddrinfo(server, "500", &hints, &info) != 0) {
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700105 do_plog(LLV_ERROR, "Cannot resolve address: %s\n", server);
106 exit(1);
107 }
108 if (info->ai_next) {
109 do_plog(LLV_WARNING, "Found multiple addresses. Use the first one.\n");
110 }
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700111 targets[0] = dupsaddr(info->ai_addr);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700112 freeaddrinfo(info);
113
114 interface = interfaze;
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700115 sources[0].addr = getlocaladdr(targets[0]);
116 if (!sources[0].addr) {
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700117 do_plog(LLV_ERROR, "Cannot get local address\n");
118 exit(1);
119 }
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700120 set_port(targets[0], 0);
121 set_port(sources[0].addr, 0);
122 sources[0].fd = -1;
123 sources[1].addr = dupsaddr(sources[0].addr);
124 sources[1].fd = -1;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700125
126 localconf.port_isakmp = PORT_ISAKMP;
127 localconf.port_isakmp_natt = PORT_ISAKMP_NATT;
128 localconf.default_af = AF_INET;
Chia-chi Yehe9fc3762011-07-07 03:20:34 -0700129 localconf.pathinfo[LC_PATHTYPE_CERT] = "./";
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700130 localconf.pad_random = LC_DEFAULT_PAD_RANDOM;
131 localconf.pad_randomlen = LC_DEFAULT_PAD_RANDOM;
132 localconf.pad_strict = LC_DEFAULT_PAD_STRICT;
133 localconf.pad_excltail = LC_DEFAULT_PAD_EXCLTAIL;
134 localconf.retry_counter = 10;
135 localconf.retry_interval = 3;
136 localconf.count_persend = LC_DEFAULT_COUNT_PERSEND;
137 localconf.secret_size = LC_DEFAULT_SECRETSIZE;
138 localconf.retry_checkph1 = LC_DEFAULT_RETRY_CHECKPH1;
139 localconf.wait_ph2complete = LC_DEFAULT_WAIT_PH2COMPLETE;
140 localconf.natt_ka_interval = LC_DEFAULT_NATT_KA_INTERVAL;
141
142 sainfo.lifetime = IPSECDOI_ATTR_SA_LD_SEC_DEFAULT;
143 sainfo.lifebyte = IPSECDOI_ATTR_SA_LD_KB_MAX;
144 add_sainfo_algorithm(algclass_ipsec_auth, IPSECDOI_ATTR_AUTH_HMAC_SHA1, 0);
145 add_sainfo_algorithm(algclass_ipsec_auth, IPSECDOI_ATTR_AUTH_HMAC_MD5, 0);
Chia-chi Yehf82b8262011-07-13 18:07:54 -0700146 add_sainfo_algorithm(algclass_ipsec_enc, IPSECDOI_ESP_AES, 256);
147 add_sainfo_algorithm(algclass_ipsec_enc, IPSECDOI_ESP_AES, 128);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700148 add_sainfo_algorithm(algclass_ipsec_enc, IPSECDOI_ESP_3DES, 0);
149 add_sainfo_algorithm(algclass_ipsec_enc, IPSECDOI_ESP_DES, 0);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700150}
151
152/*****************************************************************************/
153
154static int policy_match(struct sadb_address *address)
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700155{
156 if (address) {
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700157 struct sockaddr *addr = PFKEY_ADDR_SADDR(address);
158 return cmpsaddr(addr, targets[0]) < CMPSADDR_MISMATCH ||
159 cmpsaddr(addr, targets[1]) < CMPSADDR_MISMATCH;
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700160 }
161 return 0;
162}
163
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800164/* flush; spdflush; */
165static void flush()
166{
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700167 struct sadb_msg *p;
168 int replies = 0;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800169 int key = pfkey_open();
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700170
171 if (pfkey_send_dump(key, SADB_SATYPE_UNSPEC) <= 0 ||
172 pfkey_send_spddump(key) <= 0) {
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700173 do_plog(LLV_ERROR, "Cannot dump SAD and SPD\n");
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700174 exit(1);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800175 }
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700176
177 for (p = NULL; replies < 2 && (p = pfkey_recv(key)) != NULL; free(p)) {
178 caddr_t q[SADB_EXT_MAX + 1];
179
180 if (p->sadb_msg_type != SADB_DUMP &&
181 p->sadb_msg_type != SADB_X_SPDDUMP) {
182 continue;
183 }
184 replies += !p->sadb_msg_seq;
185
186 if (p->sadb_msg_errno || pfkey_align(p, q) || pfkey_check(q)) {
187 continue;
188 }
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700189 if (policy_match((struct sadb_address *)q[SADB_EXT_ADDRESS_SRC]) ||
190 policy_match((struct sadb_address *)q[SADB_EXT_ADDRESS_DST])) {
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700191 p->sadb_msg_type = (p->sadb_msg_type == SADB_DUMP) ?
192 SADB_DELETE : SADB_X_SPDDELETE;
193 p->sadb_msg_reserved = 0;
194 p->sadb_msg_seq = 0;
195 pfkey_send(key, p, PFKEY_UNUNIT64(p->sadb_msg_len));
196 }
197 }
198
199 pfkey_close(key);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800200}
201
202/* flush; spdflush;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700203 * spdadd src dst protocol -P out ipsec esp/transport//require; OR
204 * spdadd src any protocol -P out ipsec esp/tunnel/local-remote/require; */
205static void spdadd(struct sockaddr *src, struct sockaddr *dst,
206 int protocol, struct sockaddr *local, struct sockaddr *remote)
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800207{
208 struct __attribute__((packed)) {
209 struct sadb_x_policy p;
210 struct sadb_x_ipsecrequest q;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700211 char addresses[sizeof(struct sockaddr_storage) * 2];
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800212 } policy;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800213
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700214 struct sockaddr_storage any = {
215#ifndef __linux__
216 .ss_len = src->sa_len,
217#endif
218 .ss_family = src->sa_family,
219 };
220
221 int src_prefix = (src->sa_family == AF_INET) ? 32 : 128;
222 int dst_prefix = src_prefix;
223 int length = 0;
224 int key;
225
226 /* Fill default values. */
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800227 memset(&policy, 0, sizeof(policy));
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800228 policy.p.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
229 policy.p.sadb_x_policy_type = IPSEC_POLICY_IPSEC;
230 policy.p.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
231#ifdef HAVE_PFKEY_POLICY_PRIORITY
232 policy.p.sadb_x_policy_priority = PRIORITY_DEFAULT;
233#endif
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800234 policy.q.sadb_x_ipsecrequest_proto = IPPROTO_ESP;
235 policy.q.sadb_x_ipsecrequest_mode = IPSEC_MODE_TRANSPORT;
236 policy.q.sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
237
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700238 /* Deal with tunnel mode. */
239 if (!dst) {
240 policy.q.sadb_x_ipsecrequest_mode = IPSEC_MODE_TUNNEL;
241 dst = (struct sockaddr *)&any;
242 dst_prefix = 0;
243
244 length = sysdep_sa_len(local);
245 memcpy(policy.addresses, local, length);
246 memcpy(&policy.addresses[length], remote, length);
247 length += length;
248
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700249 /* Also use the source address to filter policies. */
250 targets[1] = dupsaddr(src);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700251 }
252
253 /* Fix lengths. */
254 length += sizeof(policy.q);
255 policy.q.sadb_x_ipsecrequest_len = length;
256 length += sizeof(policy.p);
257 policy.p.sadb_x_policy_len = PFKEY_UNIT64(length);
258
259 /* Always do a flush before adding the new policy. */
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700260 flush();
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700261 key = pfkey_open();
262 if (pfkey_send_spdadd(key, src, src_prefix, dst, dst_prefix, protocol,
263 (caddr_t)&policy, length, 0) <= 0) {
Chia-chi Yeh3473d8e2011-05-31 13:26:30 -0700264 do_plog(LLV_ERROR, "Cannot initialize SAD and SPD\n");
Chia-chi Yeh7197eb72009-07-13 16:43:29 +0800265 exit(1);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800266 }
267 pfkey_close(key);
Chia-chi Yehc4549542009-07-22 06:46:14 +0800268 atexit(flush);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800269}
270
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700271/*****************************************************************************/
272
273static void add_proposal(struct remoteconf *remoteconf,
274 int auth, int hash, int encryption, int length)
275{
276 struct isakmpsa *p = racoon_calloc(1, sizeof(struct isakmpsa));
277 p->prop_no = 1;
278 p->lifetime = OAKLEY_ATTR_SA_LD_SEC_DEFAULT;
279 p->enctype = encryption;
280 p->encklen = length;
281 p->authmethod = auth;
282 p->hashtype = hash;
283 p->dh_group = OAKLEY_ATTR_GRP_DESC_MODP1024;
284 p->vendorid = VENDORID_UNKNOWN;
285
286 if (!remoteconf->proposal) {
287 p->trns_no = 1;
288 remoteconf->proposal = p;
289 } else {
290 struct isakmpsa *q = remoteconf->proposal;
291 while (q->next) {
292 q = q->next;
293 }
294 p->trns_no = q->trns_no + 1;
295 q->next = p;
296 }
297}
298
Chia-chi Yeh71076532011-08-08 12:20:14 -0700299static vchar_t *strtovchar(char *string)
300{
301 vchar_t *vchar = string ? vmalloc(strlen(string) + 1) : NULL;
302 if (vchar) {
303 memcpy(vchar->v, string, vchar->l);
304 }
305 return vchar;
306}
307
308static void set_pre_shared_key(struct remoteconf *remoteconf,
309 char *identifier, char *key)
310{
311 pre_shared_key = key;
312 if (identifier[0]) {
313 remoteconf->idv = strtovchar(identifier);
314 remoteconf->idv->l -= 1;
315 remoteconf->etypes->type = ISAKMP_ETYPE_AGG;
316
317 remoteconf->idvtype = IDTYPE_KEYID;
318 if (strchr(identifier, '.')) {
319 remoteconf->idvtype = IDTYPE_FQDN;
320 if (strchr(identifier, '@')) {
321 remoteconf->idvtype = IDTYPE_USERFQDN;
322 }
323 }
324 }
325}
326
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700327static vchar_t *get_certificate(char *type, char *file)
328{
329 char path[PATH_MAX + 1];
330 vchar_t *certificate = NULL;
331
332 getpathname(path, sizeof(path), LC_PATHTYPE_CERT, file);
333 certificate = eay_get_x509cert(path);
334 if (!certificate) {
335 do_plog(LLV_ERROR, "Cannot load %s certificate\n", type);
336 exit(1);
337 }
338 return certificate;
339}
340
341static void set_certificates(struct remoteconf *remoteconf,
342 char *user_private_key, char *user_certificate, char *ca_certificate)
343{
344 remoteconf->myprivfile = user_private_key;
345 remoteconf->mycertfile = user_certificate;
346 if (user_certificate) {
Chia-chi Yeh71076532011-08-08 12:20:14 -0700347 remoteconf->idvtype = IDTYPE_ASN1DN;
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700348 remoteconf->mycert = get_certificate("user", user_certificate);
349 }
350 if (!ca_certificate[0]) {
351 remoteconf->verify_cert = FALSE;
352 } else {
353 remoteconf->cacertfile = ca_certificate;
354 remoteconf->cacert = get_certificate("CA", ca_certificate);
355 }
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700356}
357
358#ifdef ENABLE_HYBRID
359
360static void set_xauth_and_more(struct remoteconf *remoteconf,
361 char *username, char *password, char *phase1_up, char *script_arg)
362{
363 struct xauth_rmconf *xauth = racoon_calloc(1, sizeof(struct xauth_rmconf));
Chia-chi Yeh10700972011-07-12 18:06:57 -0700364 xauth->login = strtovchar(username);
365 xauth->pass = strtovchar(password);
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700366 remoteconf->xauth = xauth;
367 remoteconf->mode_cfg = TRUE;
368 remoteconf->script[SCRIPT_PHASE1_UP] = strtovchar(phase1_up);
369 script_names[SCRIPT_PHASE1_UP] = script_arg;
370}
371
372#endif
373
Chia-chi Yeh7197eb72009-07-13 16:43:29 +0800374void setup(int argc, char **argv)
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800375{
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700376 struct remoteconf *remoteconf = NULL;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800377 int auth;
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800378
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700379 if (argc > 2) {
380 set_globals(argv[1], argv[2]);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800381
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700382 /* Initialize everything else. */
383 eay_init();
384 initrmconf();
385 oakley_dhinit();
386 compute_vendorids();
387 sched_init();
388 if (pfkey_init() < 0 || isakmp_init() < 0) {
389 exit(1);
390 }
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800391#ifdef ENABLE_NATT
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700392 natt_keepalive_init();
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800393#endif
394
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700395 /* Create remote configuration. */
396 remoteconf = newrmconf();
397 remoteconf->etypes = racoon_calloc(1, sizeof(struct etypes));
398 remoteconf->etypes->type = ISAKMP_ETYPE_IDENT;
Chia-chi Yeh71076532011-08-08 12:20:14 -0700399 remoteconf->idvtype = IDTYPE_ADDRESS;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700400 remoteconf->ike_frag = TRUE;
Chia-chi Yehf82b8262011-07-13 18:07:54 -0700401 remoteconf->pcheck_level = PROP_CHECK_CLAIM;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700402 remoteconf->gen_policy = TRUE;
403 remoteconf->nat_traversal = TRUE;
Chia-chi Yeh71076532011-08-08 12:20:14 -0700404 remoteconf->dh_group = OAKLEY_ATTR_GRP_DESC_MODP1024;
405 oakley_setdhgroup(remoteconf->dh_group, &remoteconf->dhgrp);
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700406 remoteconf->remote = dupsaddr(targets[0]);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700407 set_port(remoteconf->remote, localconf.port_isakmp);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800408 }
409
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700410 /* Set authentication method and credentials. */
Chia-chi Yeh71076532011-08-08 12:20:14 -0700411 if (argc == 7 && !strcmp(argv[3], "udppsk")) {
412 set_pre_shared_key(remoteconf, argv[4], argv[5]);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700413 auth = OAKLEY_ATTR_AUTH_METHOD_PSKEY;
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700414
Chia-chi Yeh71076532011-08-08 12:20:14 -0700415 set_port(targets[0], atoi(argv[6]));
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700416 spdadd(sources[0].addr, targets[0], IPPROTO_UDP, NULL, NULL);
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700417 } else if (argc == 8 && !strcmp(argv[3], "udprsa")) {
418 set_certificates(remoteconf, argv[4], argv[5], argv[6]);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700419 auth = OAKLEY_ATTR_AUTH_METHOD_RSASIG;
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700420
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700421 set_port(targets[0], atoi(argv[7]));
422 spdadd(sources[0].addr, targets[0], IPPROTO_UDP, NULL, NULL);
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700423#ifdef ENABLE_HYBRID
424 } else if (argc == 10 && !strcmp(argv[3], "xauthpsk")) {
Chia-chi Yeh71076532011-08-08 12:20:14 -0700425 set_pre_shared_key(remoteconf, argv[4], argv[5]);
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700426 set_xauth_and_more(remoteconf, argv[6], argv[7], argv[8], argv[9]);
427 auth = OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I;
428 } else if (argc == 11 && !strcmp(argv[3], "xauthrsa")) {
429 set_certificates(remoteconf, argv[4], argv[5], argv[6]);
430 set_xauth_and_more(remoteconf, argv[7], argv[8], argv[9], argv[10]);
431 auth = OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I;
432 } else if (argc == 9 && !strcmp(argv[3], "hybridrsa")) {
433 set_certificates(remoteconf, NULL, NULL, argv[4]);
434 set_xauth_and_more(remoteconf, argv[5], argv[6], argv[7], argv[8]);
435 auth = OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I;
436#endif
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700437 } else {
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700438 printf("Usage: %s <interface> <server> [...], where [...] can be:\n"
Chia-chi Yeh71076532011-08-08 12:20:14 -0700439 " udppsk <identifier> <pre-shared-key> <port>\n"
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700440 " udprsa <user-private-key> <user-cert> <ca-cert> <port>\n"
441#ifdef ENABLE_HYBRID
442 " xauthpsk <identifier> <pre-shared-key>"
443 " <username> <password> <phase1-up> <script-arg>\n"
444 " xauthrsa <user-private-key> <user-cert> <ca-cert>"
445 " <username> <password> <phase1-up> <script-arg>\n"
446 " hybridrsa <ca-cert>"
447 " <username> <password> <phase1-up> <script-arg>\n"
448#endif
449 "", argv[0]);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700450 exit(0);
451 }
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800452
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700453 /* Add proposals. */
454 add_proposal(remoteconf, auth,
Chia-chi Yehf82b8262011-07-13 18:07:54 -0700455 OAKLEY_ATTR_HASH_ALG_SHA, OAKLEY_ATTR_ENC_ALG_AES, 256);
456 add_proposal(remoteconf, auth,
457 OAKLEY_ATTR_HASH_ALG_MD5, OAKLEY_ATTR_ENC_ALG_AES, 256);
458 add_proposal(remoteconf, auth,
459 OAKLEY_ATTR_HASH_ALG_SHA, OAKLEY_ATTR_ENC_ALG_AES, 128);
460 add_proposal(remoteconf, auth,
461 OAKLEY_ATTR_HASH_ALG_MD5, OAKLEY_ATTR_ENC_ALG_AES, 128);
462 add_proposal(remoteconf, auth,
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700463 OAKLEY_ATTR_HASH_ALG_SHA, OAKLEY_ATTR_ENC_ALG_3DES, 0);
464 add_proposal(remoteconf, auth,
465 OAKLEY_ATTR_HASH_ALG_MD5, OAKLEY_ATTR_ENC_ALG_3DES, 0);
466 add_proposal(remoteconf, auth,
467 OAKLEY_ATTR_HASH_ALG_SHA, OAKLEY_ATTR_ENC_ALG_DES, 0);
468 add_proposal(remoteconf, auth,
469 OAKLEY_ATTR_HASH_ALG_MD5, OAKLEY_ATTR_ENC_ALG_DES, 0);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700470
471 /* Install remote configuration. */
472 insrmconf(remoteconf);
473
474 /* Create ISAKMP sockets. */
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700475 set_port(sources[0].addr, localconf.port_isakmp);
476 sources[0].fd = isakmp_open(sources[0].addr, FALSE);
477 if (sources[0].fd == -1) {
Chia-chi Yeh0ed32712011-07-12 14:06:46 -0700478 do_plog(LLV_ERROR, "Cannot create ISAKMP socket\n");
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700479 exit(1);
480 }
481#ifdef ENABLE_NATT
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700482 set_port(sources[1].addr, localconf.port_isakmp_natt);
483 sources[1].fd = isakmp_open(sources[1].addr, TRUE);
484 if (sources[1].fd == -1) {
Chia-chi Yeh0ed32712011-07-12 14:06:46 -0700485 do_plog(LLV_WARNING, "Cannot create ISAKMP socket for NAT-T\n");
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700486 }
487#endif
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700488
489 /* Start phase 1 negotiation for xauth. */
490 if (remoteconf->xauth) {
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700491 isakmp_ph1begin_i(remoteconf, remoteconf->remote, sources[0].addr);
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700492 }
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800493}
494
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700495/*****************************************************************************/
496
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800497/* localconf.h */
498
499vchar_t *getpskbyaddr(struct sockaddr *addr)
500{
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700501 return strtovchar(pre_shared_key);
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800502}
503
504vchar_t *getpskbyname(vchar_t *name)
505{
506 return NULL;
507}
508
509void getpathname(char *path, int length, int type, const char *name)
510{
Chia-chi Yeh8f3b3882011-07-07 13:43:20 -0700511 if (pname) {
512 snprintf(path, length, pname, name);
Chia-chi Yehe9fc3762011-07-07 03:20:34 -0700513 } else {
514 strncpy(path, name, length);
515 }
Chia-chi Yehfd76ec52011-07-07 12:28:27 -0700516 path[length - 1] = '\0';
Chia-chi Yeh837a1c72009-06-26 09:40:31 +0800517}
518
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700519/* grabmyaddr.h */
520
521int myaddr_getsport(struct sockaddr *addr)
522{
523 return 0;
524}
525
526int myaddr_getfd(struct sockaddr *addr)
527{
528#ifdef ENABLE_NATT
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700529 if (sources[1].fd != -1 &&
530 cmpsaddr(addr, sources[1].addr) == CMPSADDR_MATCH) {
531 return sources[1].fd;
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700532 }
533#endif
Chia-chi Yehb0d8f172011-07-12 15:14:38 -0700534 if (cmpsaddr(addr, sources[0].addr) < CMPSADDR_MISMATCH) {
535 return sources[0].fd;
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700536 }
537 return -1;
538}
539
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700540/* privsep.h */
541
542int privsep_socket(int domain, int type, int protocol)
543{
544 int fd = socket(domain, type, protocol);
545 if ((domain == AF_INET || domain == AF_INET6) && setsockopt(
546 fd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface))) {
Chia-chi Yeh0ed32712011-07-12 14:06:46 -0700547 do_plog(LLV_WARNING, "Cannot bind socket to %s\n", interface);
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700548 }
549 return fd;
550}
551
552int privsep_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
553{
554 return bind(fd, addr, addrlen);
555}
556
557vchar_t *privsep_eay_get_pkcs1privkey(char *file)
558{
559 return eay_get_pkcs1privkey(file);
560}
561
Chia-chi Yehe4b12472011-07-12 17:14:47 -0700562static char *get_env(char * const *envp, char *key)
563{
564 int length = strlen(key);
565 while (*envp && (strncmp(*envp, key, length) || (*envp)[length] != '=')) {
566 ++envp;
567 }
568 return *envp ? &(*envp)[length + 1] : "";
569}
570
Chia-chi Yehcfc417e2011-07-13 14:05:22 -0700571static int skip_script = 0;
Chia-chi Yeha9a07ac2011-07-19 18:29:16 -0700572extern const char *android_hook(char **envp);
Chia-chi Yeh10700972011-07-12 18:06:57 -0700573
Chia-chi Yehdbbbd5f2011-07-12 13:21:42 -0700574int privsep_script_exec(char *script, int name, char * const *envp)
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700575{
Chia-chi Yehcfc417e2011-07-13 14:05:22 -0700576 if (skip_script) {
577 do_plog(LLV_WARNING,
578 "Phase 1 is up again. This time skip executing the script.\n");
Chia-chi Yehe4b12472011-07-12 17:14:47 -0700579 } else {
Chia-chi Yehcfc417e2011-07-13 14:05:22 -0700580 /* Racoon ignores INTERNAL_IP6_ADDRESS, so we only do IPv4. */
581 struct sockaddr *addr4 = str2saddr(get_env(envp, "INTERNAL_ADDR4"),
582 NULL);
583 struct sockaddr *local = str2saddr(get_env(envp, "LOCAL_ADDR"),
584 get_env(envp, "LOCAL_PORT"));
585 struct sockaddr *remote = str2saddr(get_env(envp, "REMOTE_ADDR"),
586 get_env(envp, "REMOTE_PORT"));
Chia-chi Yehe4b12472011-07-12 17:14:47 -0700587
Chia-chi Yehcfc417e2011-07-13 14:05:22 -0700588 if (addr4 && local && remote) {
589#ifdef ANDROID_CHANGES
Chia-chi Yeha9a07ac2011-07-19 18:29:16 -0700590 if (pname) {
591 script = (char *)android_hook((char **)envp);
592 }
Chia-chi Yehcfc417e2011-07-13 14:05:22 -0700593#endif
594 spdadd(addr4, NULL, IPPROTO_IP, local, remote);
595 } else {
596 do_plog(LLV_ERROR, "Cannot get parameters for SPD policy.\n");
597 exit(1);
598 }
599
600 skip_script = 1;
601 racoon_free(addr4);
602 racoon_free(local);
603 racoon_free(remote);
604 return script_exec(script, name, envp);
605 }
606 return 0;
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700607}
608
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700609int privsep_accounting_system(int port, struct sockaddr *addr,
610 char *user, int status)
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700611{
612 return 0;
613}
614
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700615int privsep_xauth_login_system(char *user, char *password)
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700616{
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700617 return -1;
618}
619
Chia-chi Yeh0ed32712011-07-12 14:06:46 -0700620/* misc.h */
621
622int racoon_hexdump(void *data, size_t length)
623{
624 return 0;
625}
626
627void close_on_exec(int fd)
628{
629 fcntl(fd, F_SETFD, FD_CLOEXEC);
630}
631
632/* sainfo.h */
633
634struct sainfo *getsainfo(const vchar_t *src, const vchar_t *dst,
635 const vchar_t *peer, const vchar_t *client, uint32_t remoteid)
636{
637 return &sainfo;
638}
639
640const char *sainfo2str(const struct sainfo *si)
641{
642 return "*";
643}
644
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700645/* throttle.h */
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700646
Chia-chi Yeh514ffe22011-07-07 13:52:27 -0700647int throttle_host(struct sockaddr *addr, int fail)
Chia-chi Yehf8a6a762011-07-04 17:21:23 -0700648{
649 return 0;
650}