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>
55 * If we're using a debugging malloc library, this may define our
58 #define RACOON_MAIN_PROGRAM
67 #include "cfparse_proto.h"
68 #include "isakmp_var.h"
72 #include "isakmp_xauth.h"
73 #include "isakmp_cfg.h"
75 #include "remoteconf.h"
76 #include "localconf.h"
81 #include "crypto_openssl.h"
85 #include <CoreFoundation/CoreFoundation.h>
86 #include <SystemConfiguration/SystemConfiguration.h>
87 #ifndef TARGET_OS_EMBEDDED
89 #endif // !TARGET_OS_EMBEDDED
90 #include "power_mgmt.h"
92 //#include "package_version.h"
94 int f_local
= 0; /* local test mode. behave like a wall. */
95 int vflag
= 1; /* for print-isakmp.c */
96 static int loading_sa
= 0; /* install sa when racoon boots up. */
97 static int dump_config
= 0; /* dump parsed config file. */
98 static int exec_done
= 0; /* we've already been exec'd */
101 static char version
[] = "@(#)" TOP_PACKAGE_STRING
" (" TOP_PACKAGE_URL
")";
102 #else /* TOP_PACKAGE */
103 static char version
[] = "@(#) racoon / IPsec-tools";
104 #endif /* TOP_PACKAGE */
106 int main
__P((int, char **));
107 static void usage
__P((void));
108 static void parse
__P((int, char **));
109 static void restore_params
__P((void));
110 static void save_params
__P((void));
111 static void saverestore_params
__P((int));
112 static void cleanup_pidfile
__P((void));
113 int launchedbylaunchd
__P((void));
115 pid_t racoon_pid
= 0;
116 int print_pid
= 1; /* for racoon only */
117 char logFileStr
[MAXPATHLEN
+1];
122 printf("usage: racoon [-BdFvs%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
128 #ifdef ENABLE_ADMINPORT
134 printf(" -B: install SA to the kernel from the file "
135 "specified by the configuration file.\n");
136 printf(" -d: debug level, more -d will generate more debug message.\n");
137 printf(" -C: dump parsed config file.\n");
138 printf(" -L: include location in debug messages\n");
139 printf(" -F: run in foreground, do not become daemon.\n");
140 printf(" -v: be more verbose\n");
141 printf(" -s: override enable auto exit\n");
143 printf(" -4: IPv4 mode.\n");
144 printf(" -6: IPv6 mode.\n");
146 #ifdef ENABLE_ADMINPORT
147 printf(" -a: port number for admin port.\n");
149 printf(" -f: pathname for configuration file.\n");
150 printf(" -l: pathname for log file.\n");
151 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
152 printf(" -P: port number for NAT-T (default: %d).\n", PORT_ISAKMP_NATT
);
162 #ifndef TARGET_OS_EMBEDDED
163 char *sb_errorbuf
= NULL
;
164 #endif // !TARGET_OS_EMBEDDED
166 #ifndef TARGET_OS_EMBEDDED
167 if (sandbox_init("racoon", SANDBOX_NAMED
, &sb_errorbuf
) == -1) {
169 syslog(LOG_ERR
, "sandbox_init failed: %s\n", sb_errorbuf
);
170 sandbox_free_error(sb_errorbuf
);
173 syslog(LOG_ERR
, "sandbox_init failed\n");
176 #endif // !TARGET_OS_EMBEDDED
178 if (geteuid() != 0) {
179 errx(1, "must be root to invoke this program.");
184 * Don't let anyone read files I write. Although some files (such as
185 * the PID file) can be other readable, we dare to use the global mask,
186 * because racoon uses fopen(3), which can't specify the permission
187 * at the creation time.
190 if (umask(077) != 077) {
191 errx(1, "could not set umask");
195 #ifdef DEBUG_RECORD_MALLOCATION
217 SCPreferencesRef prefs
= NULL
;
218 CFPropertyListRef globals
;
219 CFStringRef logFileRef
;
220 CFNumberRef debugLevelRef
;
226 if ((prefs
= SCPreferencesCreate(0, CFSTR("racoon"), CFSTR("com.apple.ipsec.plist"))) == NULL
)
228 globals
= SCPreferencesGetValue(prefs
, CFSTR("Global"));
229 if (!globals
|| (CFGetTypeID(globals
) != CFDictionaryGetTypeID()))
231 debugLevelRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLevel"));
232 if (!debugLevelRef
|| (CFGetTypeID(debugLevelRef
) != CFNumberGetTypeID()))
234 CFNumberGetValue(debugLevelRef
, kCFNumberSInt32Type
, &level
);
248 break; /* invalid - ignore */
251 logFileRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLogfile"));
252 if (!logFileRef
|| (CFGetTypeID(logFileRef
) != CFStringGetTypeID())) {
255 CFStringGetCString(logFileRef
, logFileStr
, MAXPATHLEN
, kCFStringEncodingMacRoman
);
264 if (lcconf
->logfile_param
)
265 plogset(lcconf
->logfile_param
);
269 plog(LLV_INFO
, LOCATION
, NULL
, "***** racoon started: pid=%d started by: %d\n", getpid(), getppid());
270 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version
);
272 plog(LLV_INFO
, LOCATION
, NULL
, "@(#)"
273 "This product linked %s (http://www.openssl.org/)"
274 "\n", eay_version());
276 plog(LLV_INFO
, LOCATION
, NULL
, "Reading configuration from \"%s\"\n",
277 lcconf
->racoon_conf
);
279 if (pfkey_init() < 0) {
280 errx(1, "something error happened "
281 "while pfkey initializing.");
286 if (isakmp_cfg_init(ISAKMP_CFG_INIT_COLD
))
287 errx(1, "could not initialize ISAKMP mode config structures");
291 if (xauth_ldap_init() != 0)
292 errx(1, "could not initialize libldap");
296 * in order to prefer the parameters by command line,
297 * saving some parameters before parsing configuration file.
302 errx(1, "failed to parse configuration file.");
305 if (lcconf
->logfile_param
== NULL
&& logFileStr
[0] == 0)
306 plogreset(lcconf
->pathinfo
[LC_PATHTYPE_LOGFILE
]);
309 /* Tell the kernel which port to use for UDP encapsulation */
311 int udp_port
= PORT_ISAKMP_NATT
;
312 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
313 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
314 udp_port
, strerror(errno
));
318 #ifdef HAVE_LIBRADIUS
319 if (xauth_radius_init() != 0) {
320 errx(1, "could not initialize libradius");
326 if(isakmp_cfg_config
.network4
&& isakmp_cfg_config
.pool_size
== 0)
327 if ((error
= isakmp_cfg_resize_pool(ISAKMP_CFG_MAX_CNX
)) != 0)
335 * install SAs from the specified file. If the file is not specified
336 * by the configuration file, racoon will exit.
338 if (loading_sa
&& !f_local
) {
339 if (backupsa_from_file() != 0)
340 errx(1, "something error happened "
347 if ( !exec_done
&& launchedbylaunchd() ){
348 plog(LLV_INFO
, LOCATION
, NULL
,
349 "racoon launched by launchd.\n");
351 if (atexit(cleanup_pidfile
) < 0) {
352 plog(LLV_ERROR
, LOCATION
, NULL
,
353 "cannot register pidfile cleanup");
358 if (atexit(cleanup_pidfile
) < 0) {
359 plog(LLV_ERROR
, LOCATION
, NULL
,
360 "cannot register pidfile cleanup");
363 #define MAX_EXEC_ARGS 32
365 char *args
[MAX_EXEC_ARGS
+ 2]; /* 2 extra, for '-x' and NULL */
369 if (ac
> MAX_EXEC_ARGS
) {
370 plog(LLV_ERROR
, LOCATION
, NULL
,
371 "too many arguments.\n");
375 if (daemon(0, 0) < 0) {
376 errx(1, "failed to be daemon. (%s)",
380 /* Radar 5129006 - Prevent non-root user from killing racoon
381 * when launched by setuid process
384 plog(LLV_ERROR
, LOCATION
, NULL
,
385 "cannot set uid.\n");
389 plog(LLV_ERROR
, LOCATION
, NULL
,
390 "cannot set gid.\n");
394 /* setup args to re-exec - for CoreFoundation issues */
395 args
[0] = PATHRACOON
;
396 for (i
= 1; i
< ac
; i
++)
398 args
[ac
] = "-x"; /* tells racoon its been exec'd */
401 execve(PATHRACOON
, args
, env
);
402 plog(LLV_ERROR
, LOCATION
, NULL
,
403 "failed to exec racoon. (%s)", strerror(errno
));
417 int launchdlaunched
= 1;
418 launch_data_t checkin_response
= NULL
;
419 launch_data_t checkin_request
= NULL
;
421 /* check in with launchd */
422 if ((checkin_request
= launch_data_new_string(LAUNCH_KEY_CHECKIN
)) == NULL
) {
423 plog(LLV_ERROR
, LOCATION
, NULL
,
424 "launch_data_new_string fails.\n");
428 if ((checkin_response
= launch_msg(checkin_request
)) == NULL
) {
429 plog(LLV_ERROR
, LOCATION
, NULL
,
430 "launch_msg fails.\n");
434 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
435 plog(LLV_ERROR
, LOCATION
, NULL
,
436 "launch_data_get_type fails errno %d.\n", launch_data_get_errno(checkin_response
));
442 /* clean up before we leave */
443 if ( checkin_request
)
444 launch_data_free(checkin_request
);
445 if ( checkin_response
)
446 launch_data_free(checkin_response
);
447 return launchdlaunched
;
453 char pid_file
[MAXPATHLEN
];
456 /* if it's not child process, clean everything */
457 if (racoon_pid
== p
) {
458 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
459 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", sizeof(pid_file
));
460 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
461 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
463 strlcat(pid_file
, _PATH_VARRUN
, sizeof(pid_file
));
464 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
466 (void) unlink(pid_file
);
483 pname
= strrchr(*av
, '/');
489 #if 0 /* for debugging */
491 plogset("/tmp/racoon.log");
494 while ((c
= getopt(ac
, av
, "dLFp:P:a:f:l:vsZBCx"
510 printf("Foreground mode.\n");
514 lcconf
->port_isakmp
= atoi(optarg
);
517 lcconf
->port_isakmp_natt
= atoi(optarg
);
520 #ifdef ENABLE_ADMINPORT
521 lcconf
->port_admin
= atoi(optarg
);
524 fprintf(stderr
, "%s: the option is disabled "
525 "in the configuration\n", pname
);
529 lcconf
->racoon_conf
= optarg
;
532 lcconf
->logfile_param
= optarg
;
538 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_CLIENT
; /* override default auto exit state */
546 * To specify -Z option and to choice a appropriate
547 * port number for ISAKMP, you can launch some racoons
548 * on the local host for debug.
549 * pk_sendadd() on initiator side is always failed
550 * even if this flag is used. Because there is same
551 * spi in the SAD which is inserted by pk_sendgetspi()
554 printf("Local test mode.\n");
564 lcconf
->default_af
= AF_INET
;
567 lcconf
->default_af
= AF_INET6
;
595 saverestore_params(1);
601 saverestore_params(0);
605 saverestore_params(f
)
608 static u_int16_t s_port_isakmp
;
609 #ifdef ENABLE_ADMINPORT
610 static u_int16_t s_port_admin
;
613 /* 0: save, 1: restore */
615 lcconf
->port_isakmp
= s_port_isakmp
;
616 #ifdef ENABLE_ADMINPORT
617 lcconf
->port_admin
= s_port_admin
;
620 s_port_isakmp
= lcconf
->port_isakmp
;
621 #ifdef ENABLE_ADMINPORT
622 s_port_admin
= lcconf
->port_admin
;