]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/main.c
109f96d1844143b5c9664a2dc72bcd5302ae1825
1 /* $KAME: main.c,v 1.43 2001/11/16 04:34:57 sakane 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>
38 #include <netinet/in.h>
52 * If we're using a debugging malloc library, this may define our
55 #define RACOON_MAIN_PROGRAM
65 #include "isakmp_var.h"
66 #include "remoteconf.h"
67 #include "localconf.h"
71 #include "crypto_openssl.h"
74 int f_foreground
= 0; /* force running in foreground. */
75 int f_local
= 0; /* local test mode. behave like a wall. */
76 int vflag
= 1; /* for print-isakmp.c */
77 static int loading_sa
= 0; /* install sa when racoon boots up. */
79 #define RACOON_VERSION "20001216 sakane@kame.net"
80 #ifdef RACOON_PKG_VERSION
81 static char version0
[] = "@(#)package version " RACOON_PKG_VERSION
;
82 static char version
[] = "@(#)internal version " RACOON_VERSION
;
84 static char version
[] = "@(#)racoon 20001216 " RACOON_VERSION
;
86 static pid_t racoon_pid
= 0;
88 int main
__P((int, char **));
89 static void usage
__P((void));
90 static void parse
__P((int, char **));
91 static void restore_params
__P((void));
92 static void save_params
__P((void));
93 static void saverestore_params
__P((int));
94 static void cleanup_pidfile
__P((void));
99 printf("usage: racoon [-BdFv%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
105 #ifdef ENABLE_ADMINPORT
111 printf(" -B: install SA to the kernel from the file "
112 "specified by the configuration file.\n");
113 printf(" -d: debug level, more -d will generate more debug message.\n");
114 printf(" -F: run in foreground, do not become daemon.\n");
115 printf(" -v: be more verbose\n");
117 printf(" -4: IPv4 mode.\n");
118 printf(" -6: IPv6 mode.\n");
120 #ifdef ENABLE_ADMINPORT
121 printf(" -a: port number for admin port.\n");
123 printf(" -f: pathname for configuration file.\n");
124 printf(" -l: pathname for log file.\n");
125 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
136 if (geteuid() != 0) {
137 errx(1, "must be root to invoke this program.");
142 * Don't let anyone read files I write. Although some files (such as
143 * the PID file) can be other readable, we dare to use the global mask,
144 * because racoon uses fopen(3), which can't specify the permission
145 * at the creation time.
148 if (umask(077) != 077) {
149 errx(1, "could not set umask");
153 #ifdef DEBUG_RECORD_MALLOCATION
166 #ifdef RACOON_PKG_VERSION
167 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version0
);
169 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version
);
170 plog(LLV_INFO
, LOCATION
, NULL
, "@(#)"
171 "This product linked %s (http://www.openssl.org/)"
172 "\n", eay_version());
174 if (pfkey_init() < 0) {
175 errx(1, "something error happened "
176 "while pfkey initializing.");
181 * in order to prefer the parameters by command line,
182 * saving some parameters before parsing configuration file.
187 errx(1, "failed to parse configuration file.");
191 * install SAs from the specified file. If the file is not specified
192 * by the configuration file, racoon will exit.
194 if (loading_sa
&& !f_local
) {
195 if (backupsa_from_file() != 0)
196 errx(1, "something error happened "
203 const char *pid_file
= _PATH_VARRUN
"racoon.pid";
206 if (daemon(0, 0) < 0) {
207 errx(1, "failed to be daemon. (%s)",
211 * In case somebody has started inetd manually, we need to
212 * clear the logname, so that old servers run as root do not
213 * get the user's logname..
215 if (setlogin("") < 0) {
216 plog(LLV_ERROR
, LOCATION
, NULL
,
217 "cannot clear logname: %s\n", strerror(errno
));
218 /* no big deal if it fails.. */
220 racoon_pid
= getpid();
221 fp
= fopen(pid_file
, "w");
223 if (fchmod(fileno(fp
),
224 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
225 syslog(LOG_ERR
, "%s", strerror(errno
));
229 fprintf(fp
, "%ld\n", (long)racoon_pid
);
232 plog(LLV_ERROR
, LOCATION
, NULL
,
233 "cannot open %s", pid_file
);
236 if (atexit(cleanup_pidfile
) < 0) {
237 plog(LLV_ERROR
, LOCATION
, NULL
,
238 "cannot register pidfile cleanup");
253 /* if it's not child process, clean everything */
254 if (racoon_pid
== p
) {
255 const char *pid_file
= _PATH_VARRUN
"racoon.pid";
257 (void) unlink(pid_file
);
273 pname
= strrchr(*av
, '/');
279 while ((c
= getopt(ac
, av
, "dFp:a:f:l:vZB"
292 printf("Foreground mode.\n");
296 lcconf
->port_isakmp
= atoi(optarg
);
299 #ifdef ENABLE_ADMINPORT
300 lcconf
->port_admin
= atoi(optarg
);
303 fprintf(stderr
, "%s: the option is disabled "
304 "in the configuration\n", pname
);
308 lcconf
->racoon_conf
= optarg
;
319 * To specify -Z option and to choice a appropriate
320 * port number for ISAKMP, you can launch some racoons
321 * on the local host for debug.
322 * pk_sendadd() on initiator side is always failed
323 * even if this flag is used. Because there is same
324 * spi in the SAD which is inserted by pk_sendgetspi()
327 printf("Local test mode.\n");
337 lcconf
->default_af
= AF_INET
;
340 lcconf
->default_af
= AF_INET6
;
365 saverestore_params(1);
371 saverestore_params(0);
375 saverestore_params(f
)
378 static u_int16_t s_port_isakmp
;
379 #ifdef ENABLE_ADMINPORT
380 static u_int16_t s_port_admin
;
383 /* 0: save, 1: restore */
385 lcconf
->port_isakmp
= s_port_isakmp
;
386 #ifdef ENABLE_ADMINPORT
387 lcconf
->port_admin
= s_port_admin
;
390 s_port_isakmp
= lcconf
->port_isakmp
;
391 #ifdef ENABLE_ADMINPORT
392 s_port_admin
= lcconf
->port_admin
;