]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/main.c
1 /* $Id: main.c,v 1.14.2.3 2005/11/06 17:18:26 monas 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
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <netinet/in.h>
54 * If we're using a debugging malloc library, this may define our
57 #define RACOON_MAIN_PROGRAM
66 #include "cfparse_proto.h"
67 #include "isakmp_var.h"
70 #include "isakmp_xauth.h"
72 #include "remoteconf.h"
73 #include "localconf.h"
77 #include "crypto_openssl.h"
81 //#include "package_version.h"
83 int f_local
= 0; /* local test mode. behave like a wall. */
84 int vflag
= 1; /* for print-isakmp.c */
85 static int loading_sa
= 0; /* install sa when racoon boots up. */
86 static int dump_config
= 0; /* dump parsed config file. */
87 static int exec_done
= 0; /* we've already been exec'd */
90 static char version
[] = "@(#)" TOP_PACKAGE_STRING
" (" TOP_PACKAGE_URL
")";
91 #else /* TOP_PACKAGE */
92 static char version
[] = "@(#) racoon / IPsec-tools";
93 #endif /* TOP_PACKAGE */
95 int main
__P((int, char **));
96 static void usage
__P((void));
97 static void parse
__P((int, char **));
98 static void restore_params
__P((void));
99 static void save_params
__P((void));
100 static void saverestore_params
__P((int));
101 static void cleanup_pidfile
__P((void));
103 pid_t racoon_pid
= 0;
104 int print_pid
= 1; /* for racoon only */
109 printf("usage: racoon [-BdFve%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
115 #ifdef ENABLE_ADMINPORT
121 printf(" -B: install SA to the kernel from the file "
122 "specified by the configuration file.\n");
123 printf(" -d: debug level, more -d will generate more debug message.\n");
124 printf(" -C: dump parsed config file.\n");
125 printf(" -L: include location in debug messages\n");
126 printf(" -F: run in foreground, do not become daemon.\n");
127 printf(" -v: be more verbose\n");
128 printf(" -e: enable auto exit\n");
130 printf(" -4: IPv4 mode.\n");
131 printf(" -6: IPv6 mode.\n");
133 #ifdef ENABLE_ADMINPORT
134 printf(" -a: port number for admin port.\n");
136 printf(" -f: pathname for configuration file.\n");
137 printf(" -l: pathname for log file.\n");
138 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
139 printf(" -P: port number for NAT-T (default: %d).\n", PORT_ISAKMP_NATT
);
150 if (geteuid() != 0) {
151 errx(1, "must be root to invoke this program.");
156 * Don't let anyone read files I write. Although some files (such as
157 * the PID file) can be other readable, we dare to use the global mask,
158 * because racoon uses fopen(3), which can't specify the permission
159 * at the creation time.
162 if (umask(077) != 077) {
163 errx(1, "could not set umask");
167 #ifdef DEBUG_RECORD_MALLOCATION
178 if (lcconf
->logfile_param
)
179 plogset(lcconf
->logfile_param
);
182 plog(LLV_INFO
, LOCATION
, NULL
, "***** racoon started: pid=%d started by: %d\n", getpid(), getppid());
183 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version
);
184 plog(LLV_INFO
, LOCATION
, NULL
, "@(#)"
185 "This product linked %s (http://www.openssl.org/)"
186 "\n", eay_version());
188 if (pfkey_init() < 0) {
189 errx(1, "something error happened "
190 "while pfkey initializing.");
195 * in order to prefer the parameters by command line,
196 * saving some parameters before parsing configuration file.
201 errx(1, "failed to parse configuration file.");
203 if (lcconf
->logfile_param
== NULL
)
204 plogreset(lcconf
->pathinfo
[LC_PATHTYPE_LOGFILE
]);
207 /* Tell the kernel which port to use for UDP encapsulation */
209 int udp_port
= PORT_ISAKMP_NATT
;
210 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
211 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
212 udp_port
, strerror(errno
));
216 #ifdef HAVE_LIBRADIUS
217 if (xauth_radius_init() != 0) {
218 errx(1, "could not initialize libradius");
227 * install SAs from the specified file. If the file is not specified
228 * by the configuration file, racoon will exit.
230 if (loading_sa
&& !f_local
) {
231 if (backupsa_from_file() != 0)
232 errx(1, "something error happened "
238 else if (exec_done
) {
239 if (atexit(cleanup_pidfile
) < 0) {
240 plog(LLV_ERROR
, LOCATION
, NULL
,
241 "cannot register pidfile cleanup");
244 #define MAX_EXEC_ARGS 32
246 char *args
[MAX_EXEC_ARGS
+ 1];
250 if (ac
> MAX_EXEC_ARGS
) {
251 plog(LLV_ERROR
, LOCATION
, NULL
,
252 "too many arguments.\n");
256 if (daemon(0, 0) < 0) {
257 errx(1, "failed to be daemon. (%s)",
261 /* Radar 5129006 - Prevent non-root user from killing racoon
262 * when launched by setuid process
265 plog(LLV_ERROR
, LOCATION
, NULL
,
266 "cannot set uid.\n");
270 plog(LLV_ERROR
, LOCATION
, NULL
,
271 "cannot set gid.\n");
275 /* setup args to re-exec - for CoreFoundation issues */
276 args
[0] = PATHRACOON
;
277 for (i
= 1; i
< ac
; i
++)
279 args
[ac
] = "-x"; /* tells racoon its been exec'd */
282 execve(PATHRACOON
, args
, env
);
283 plog(LLV_ERROR
, LOCATION
, NULL
,
284 "failed to exec racoon. (%s)", strerror(errno
));
297 char pid_file
[MAXPATHLEN
];
300 /* if it's not child process, clean everything */
301 if (racoon_pid
== p
) {
302 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
303 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", MAXPATHLEN
);
304 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
305 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], MAXPATHLEN
);
307 strlcat(pid_file
, _PATH_VARRUN
, MAXPATHLEN
);
308 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], MAXPATHLEN
);
310 (void) unlink(pid_file
);
327 pname
= strrchr(*av
, '/');
333 #if 0 /* for debugging */
335 plogset("/tmp/racoon.log");
338 while ((c
= getopt(ac
, av
, "dLFp:P:a:f:l:veZBCx"
354 printf("Foreground mode.\n");
358 lcconf
->port_isakmp
= atoi(optarg
);
361 lcconf
->port_isakmp_natt
= atoi(optarg
);
364 #ifdef ENABLE_ADMINPORT
365 lcconf
->port_admin
= atoi(optarg
);
368 fprintf(stderr
, "%s: the option is disabled "
369 "in the configuration\n", pname
);
373 lcconf
->racoon_conf
= optarg
;
376 lcconf
->logfile_param
= optarg
;
382 lcconf
->auto_exit_state
|= LC_AUTOEXITSTATE_CLIENT
;
390 * To specify -Z option and to choice a appropriate
391 * port number for ISAKMP, you can launch some racoons
392 * on the local host for debug.
393 * pk_sendadd() on initiator side is always failed
394 * even if this flag is used. Because there is same
395 * spi in the SAD which is inserted by pk_sendgetspi()
398 printf("Local test mode.\n");
408 lcconf
->default_af
= AF_INET
;
411 lcconf
->default_af
= AF_INET6
;
439 saverestore_params(1);
445 saverestore_params(0);
449 saverestore_params(f
)
452 static u_int16_t s_port_isakmp
;
453 #ifdef ENABLE_ADMINPORT
454 static u_int16_t s_port_admin
;
457 /* 0: save, 1: restore */
459 lcconf
->port_isakmp
= s_port_isakmp
;
460 #ifdef ENABLE_ADMINPORT
461 lcconf
->port_admin
= s_port_admin
;
464 s_port_isakmp
= lcconf
->port_isakmp
;
465 #ifdef ENABLE_ADMINPORT
466 s_port_admin
= lcconf
->port_admin
;