]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/main.c
1 /* $KAME: main.c,v 1.48 2002/11/20 02:06:07 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
36 #include <sys/random.h>
37 #include <sys/sysctl.h>
39 #include <netinet/in.h>
53 * If we're using a debugging malloc library, this may define our
56 #define RACOON_MAIN_PROGRAM
66 #include "isakmp_var.h"
67 #include "remoteconf.h"
68 #include "localconf.h"
72 #include "crypto_openssl.h"
74 #ifndef HAVE_ARC4RANDOM
75 #include "arc4random.h"
78 int f_foreground
= 0; /* force running in foreground. */
79 int f_local
= 0; /* local test mode. behave like a wall. */
80 int vflag
= 1; /* for print-isakmp.c */
81 static int loading_sa
= 0; /* install sa when racoon boots up. */
83 #define RACOON_VERSION "20001216 sakane@kame.net"
84 #ifdef RACOON_PKG_VERSION
85 static char version0
[] = "@(#)package version " RACOON_PKG_VERSION
;
86 static char version
[] = "@(#)internal version " RACOON_VERSION
;
88 static char version
[] = "@(#)racoon 20001216 " RACOON_VERSION
;
90 static pid_t racoon_pid
= 0;
92 int main
__P((int, char **));
93 static void usage
__P((void));
94 static void parse
__P((int, char **));
95 static void restore_params
__P((void));
96 static void save_params
__P((void));
97 static void saverestore_params
__P((int));
98 static void cleanup_pidfile
__P((void));
103 printf("usage: racoon [-BdFv%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
109 #ifdef ENABLE_ADMINPORT
115 printf(" -B: install SA to the kernel from the file "
116 "specified by the configuration file.\n");
117 printf(" -d: debug level, more -d will generate more debug message.\n");
118 printf(" -F: run in foreground, do not become daemon.\n");
119 printf(" -v: be more verbose\n");
121 printf(" -4: IPv4 mode.\n");
122 printf(" -6: IPv6 mode.\n");
124 #ifdef ENABLE_ADMINPORT
125 printf(" -a: port number for admin port.\n");
127 printf(" -f: pathname for configuration file.\n");
128 printf(" -l: pathname for log file.\n");
129 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
133 extern int cfparse(void);
142 if (geteuid() != 0) {
143 errx(1, "must be root to invoke this program.");
148 * Don't let anyone read files I write. Although some files (such as
149 * the PID file) can be other readable, we dare to use the global mask,
150 * because racoon uses fopen(3), which can't specify the permission
151 * at the creation time.
154 if (umask(077) != 077) {
155 errx(1, "could not set umask");
159 #ifdef DEBUG_RECORD_MALLOCATION
171 (void)arc4random(); /* XXX test if random number is available */
173 #ifdef RACOON_PKG_VERSION
174 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version0
);
176 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version
);
177 plog(LLV_INFO
, LOCATION
, NULL
, "@(#)"
178 "This product linked %s (http://www.openssl.org/)"
179 "\n", eay_version());
181 if (pfkey_init() < 0) {
182 errx(1, "something error happened "
183 "while pfkey initializing.");
188 * in order to prefer the parameters by command line,
189 * saving some parameters before parsing configuration file.
194 errx(1, "failed to parse configuration file.");
198 /* Tell the kernel which port to use for UDP encapsulation */
200 int udp_port
= PORT_ISAKMP_NATT
;
201 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
202 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
203 udp_port
, strerror(errno
));
208 * install SAs from the specified file. If the file is not specified
209 * by the configuration file, racoon will exit.
211 if (loading_sa
&& !f_local
) {
212 if (backupsa_from_file() != 0)
213 errx(1, "something error happened "
220 const char *pid_file
= _PATH_VARRUN
"racoon.pid";
223 if (daemon(0, 0) < 0) {
224 errx(1, "failed to be daemon. (%s)",
228 * In case somebody has started inetd manually, we need to
229 * clear the logname, so that old servers run as root do not
230 * get the user's logname..
232 if (setlogin("") < 0) {
233 plog(LLV_ERROR
, LOCATION
, NULL
,
234 "cannot clear logname: %s\n", strerror(errno
));
235 /* no big deal if it fails.. */
237 racoon_pid
= getpid();
238 fp
= fopen(pid_file
, "w");
240 if (fchmod(fileno(fp
),
241 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
242 syslog(LOG_ERR
, "%s", strerror(errno
));
246 fprintf(fp
, "%ld\n", (long)racoon_pid
);
249 plog(LLV_ERROR
, LOCATION
, NULL
,
250 "cannot open %s", pid_file
);
253 if (atexit(cleanup_pidfile
) < 0) {
254 plog(LLV_ERROR
, LOCATION
, NULL
,
255 "cannot register pidfile cleanup");
270 /* if it's not child process, clean everything */
271 if (racoon_pid
== p
) {
272 const char *pid_file
= _PATH_VARRUN
"racoon.pid";
274 (void) unlink(pid_file
);
290 pname
= strrchr(*av
, '/');
296 while ((c
= getopt(ac
, av
, "dFp:a:f:l:vZB"
309 printf("Foreground mode.\n");
313 lcconf
->port_isakmp
= atoi(optarg
);
316 #ifdef ENABLE_ADMINPORT
317 lcconf
->port_admin
= atoi(optarg
);
320 fprintf(stderr
, "%s: the option is disabled "
321 "in the configuration\n", pname
);
325 lcconf
->racoon_conf
= optarg
;
336 * To specify -Z option and to choice a appropriate
337 * port number for ISAKMP, you can launch some racoons
338 * on the local host for debug.
339 * pk_sendadd() on initiator side is always failed
340 * even if this flag is used. Because there is same
341 * spi in the SAD which is inserted by pk_sendgetspi()
344 printf("Local test mode.\n");
354 lcconf
->default_af
= AF_INET
;
357 lcconf
->default_af
= AF_INET6
;
382 saverestore_params(1);
388 saverestore_params(0);
392 saverestore_params(f
)
395 static u_int16_t s_port_isakmp
;
396 #ifdef ENABLE_ADMINPORT
397 static u_int16_t s_port_admin
;
400 /* 0: save, 1: restore */
402 lcconf
->port_isakmp
= s_port_isakmp
;
403 #ifdef ENABLE_ADMINPORT
404 lcconf
->port_admin
= s_port_admin
;
407 s_port_isakmp
= lcconf
->port_isakmp
;
408 #ifdef ENABLE_ADMINPORT
409 s_port_admin
= lcconf
->port_admin
;