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"
86 #include <CoreFoundation/CoreFoundation.h>
87 #include <SystemConfiguration/SystemConfiguration.h>
90 //#include "package_version.h"
92 int f_local
= 0; /* local test mode. behave like a wall. */
93 int vflag
= 1; /* for print-isakmp.c */
94 static int loading_sa
= 0; /* install sa when racoon boots up. */
95 static int dump_config
= 0; /* dump parsed config file. */
96 static int exec_done
= 0; /* we've already been exec'd */
99 static char version
[] = "@(#)" TOP_PACKAGE_STRING
" (" TOP_PACKAGE_URL
")";
100 #else /* TOP_PACKAGE */
101 static char version
[] = "@(#) racoon / IPsec-tools";
102 #endif /* TOP_PACKAGE */
104 int main
__P((int, char **));
105 static void usage
__P((void));
106 static void parse
__P((int, char **));
107 static void restore_params
__P((void));
108 static void save_params
__P((void));
109 static void saverestore_params
__P((int));
110 static void cleanup_pidfile
__P((void));
111 static int launchedbylaunchd(void);
113 pid_t racoon_pid
= 0;
114 int print_pid
= 1; /* for racoon only */
119 printf("usage: racoon [-BdFvs%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
125 #ifdef ENABLE_ADMINPORT
131 printf(" -B: install SA to the kernel from the file "
132 "specified by the configuration file.\n");
133 printf(" -d: debug level, more -d will generate more debug message.\n");
134 printf(" -C: dump parsed config file.\n");
135 printf(" -L: include location in debug messages\n");
136 printf(" -F: run in foreground, do not become daemon.\n");
137 printf(" -v: be more verbose\n");
138 printf(" -s: override enable auto exit\n");
140 printf(" -4: IPv4 mode.\n");
141 printf(" -6: IPv6 mode.\n");
143 #ifdef ENABLE_ADMINPORT
144 printf(" -a: port number for admin port.\n");
146 printf(" -f: pathname for configuration file.\n");
147 printf(" -l: pathname for log file.\n");
148 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
149 printf(" -P: port number for NAT-T (default: %d).\n", PORT_ISAKMP_NATT
);
159 char logFileStr
[MAXPATHLEN
+1];
161 if (geteuid() != 0) {
162 errx(1, "must be root to invoke this program.");
167 * Don't let anyone read files I write. Although some files (such as
168 * the PID file) can be other readable, we dare to use the global mask,
169 * because racoon uses fopen(3), which can't specify the permission
170 * at the creation time.
173 if (umask(077) != 077) {
174 errx(1, "could not set umask");
178 #ifdef DEBUG_RECORD_MALLOCATION
197 SCPreferencesRef prefs
= NULL
;
198 CFPropertyListRef globals
;
199 CFStringRef logFileRef
;
200 CFNumberRef debugLevelRef
;
206 if ((prefs
= SCPreferencesCreate(0, CFSTR("racoon"), CFSTR("com.apple.ipsec.plist"))) == NULL
)
208 globals
= SCPreferencesGetValue(prefs
, CFSTR("Global"));
209 if (!globals
|| (CFGetTypeID(globals
) != CFDictionaryGetTypeID()))
211 debugLevelRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLevel"));
212 if (!debugLevelRef
|| (CFGetTypeID(debugLevelRef
) != CFNumberGetTypeID()))
214 CFNumberGetValue(debugLevelRef
, kCFNumberSInt32Type
, &level
);
228 break; /* invalid - ignore */
231 logFileRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLogfile"));
232 if (!logFileRef
|| (CFGetTypeID(logFileRef
) != CFStringGetTypeID())) {
235 CFStringGetCString(logFileRef
, logFileStr
, MAXPATHLEN
, kCFStringEncodingMacRoman
);
244 #endif /* __APPLE__ */
245 if (lcconf
->logfile_param
)
246 plogset(lcconf
->logfile_param
);
250 plog(LLV_INFO
, LOCATION
, NULL
, "***** racoon started: pid=%d started by: %d\n", getpid(), getppid());
251 plog(LLV_INFO
, LOCATION
, NULL
, "%s\n", version
);
252 plog(LLV_INFO
, LOCATION
, NULL
, "@(#)"
253 "This product linked %s (http://www.openssl.org/)"
254 "\n", eay_version());
255 plog(LLV_INFO
, LOCATION
, NULL
, "Reading configuration from \"%s\"\n",
256 lcconf
->racoon_conf
);
258 if (pfkey_init() < 0) {
259 errx(1, "something error happened "
260 "while pfkey initializing.");
265 if (isakmp_cfg_init(ISAKMP_CFG_INIT_COLD
))
266 errx(1, "could not initialize ISAKMP mode config structures");
270 if (xauth_ldap_init() != 0)
271 errx(1, "could not initialize libldap");
275 * in order to prefer the parameters by command line,
276 * saving some parameters before parsing configuration file.
281 errx(1, "failed to parse configuration file.");
284 if (lcconf
->logfile_param
== NULL
&& logFileStr
[0] == 0)
285 plogreset(lcconf
->pathinfo
[LC_PATHTYPE_LOGFILE
]);
288 /* Tell the kernel which port to use for UDP encapsulation */
290 int udp_port
= PORT_ISAKMP_NATT
;
291 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
292 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
293 udp_port
, strerror(errno
));
297 #ifdef HAVE_LIBRADIUS
298 if (xauth_radius_init() != 0) {
299 errx(1, "could not initialize libradius");
305 if(isakmp_cfg_config
.network4
&& isakmp_cfg_config
.pool_size
== 0)
306 if ((error
= isakmp_cfg_resize_pool(ISAKMP_CFG_MAX_CNX
)) != 0)
314 * install SAs from the specified file. If the file is not specified
315 * by the configuration file, racoon will exit.
317 if (loading_sa
&& !f_local
) {
318 if (backupsa_from_file() != 0)
319 errx(1, "something error happened "
326 if ( !exec_done
&& launchedbylaunchd() ){
327 plog(LLV_INFO
, LOCATION
, NULL
,
328 "racoon launched by launchd.\n");
330 if (atexit(cleanup_pidfile
) < 0) {
331 plog(LLV_ERROR
, LOCATION
, NULL
,
332 "cannot register pidfile cleanup");
337 if (atexit(cleanup_pidfile
) < 0) {
338 plog(LLV_ERROR
, LOCATION
, NULL
,
339 "cannot register pidfile cleanup");
342 #define MAX_EXEC_ARGS 32
344 char *args
[MAX_EXEC_ARGS
+ 2]; /* 2 extra, for '-x' and NULL */
348 if (ac
> MAX_EXEC_ARGS
) {
349 plog(LLV_ERROR
, LOCATION
, NULL
,
350 "too many arguments.\n");
354 if (daemon(0, 0) < 0) {
355 errx(1, "failed to be daemon. (%s)",
359 /* Radar 5129006 - Prevent non-root user from killing racoon
360 * when launched by setuid process
363 plog(LLV_ERROR
, LOCATION
, NULL
,
364 "cannot set uid.\n");
368 plog(LLV_ERROR
, LOCATION
, NULL
,
369 "cannot set gid.\n");
373 /* setup args to re-exec - for CoreFoundation issues */
374 args
[0] = PATHRACOON
;
375 for (i
= 1; i
< ac
; i
++)
377 args
[ac
] = "-x"; /* tells racoon its been exec'd */
380 execve(PATHRACOON
, args
, env
);
381 plog(LLV_ERROR
, LOCATION
, NULL
,
382 "failed to exec racoon. (%s)", strerror(errno
));
396 int launchdlaunched
= 1;
397 launch_data_t checkin_response
= NULL
;
398 launch_data_t checkin_request
= NULL
;
400 /* check in with launchd */
401 if ((checkin_request
= launch_data_new_string(LAUNCH_KEY_CHECKIN
)) == NULL
) {
402 plog(LLV_ERROR
, LOCATION
, NULL
,
403 "launch_data_new_string fails.\n");
407 if ((checkin_response
= launch_msg(checkin_request
)) == NULL
) {
408 plog(LLV_ERROR
, LOCATION
, NULL
,
409 "launch_msg fails.\n");
413 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
414 plog(LLV_ERROR
, LOCATION
, NULL
,
415 "launch_data_get_type fails errno %d.\n", launch_data_get_errno(checkin_response
));
421 /* clean up before we leave */
422 if ( checkin_request
)
423 launch_data_free(checkin_request
);
424 if ( checkin_response
)
425 launch_data_free(checkin_response
);
426 return launchdlaunched
;
432 char pid_file
[MAXPATHLEN
];
435 /* if it's not child process, clean everything */
436 if (racoon_pid
== p
) {
437 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
438 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", sizeof(pid_file
));
439 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
440 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
442 strlcat(pid_file
, _PATH_VARRUN
, sizeof(pid_file
));
443 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
445 (void) unlink(pid_file
);
462 pname
= strrchr(*av
, '/');
468 #if 0 /* for debugging */
470 plogset("/tmp/racoon.log");
473 while ((c
= getopt(ac
, av
, "dLFp:P:a:f:l:vsZBCx"
489 printf("Foreground mode.\n");
493 lcconf
->port_isakmp
= atoi(optarg
);
496 lcconf
->port_isakmp_natt
= atoi(optarg
);
499 #ifdef ENABLE_ADMINPORT
500 lcconf
->port_admin
= atoi(optarg
);
503 fprintf(stderr
, "%s: the option is disabled "
504 "in the configuration\n", pname
);
508 lcconf
->racoon_conf
= optarg
;
511 lcconf
->logfile_param
= optarg
;
517 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_CLIENT
; /* override default auto exit state */
525 * To specify -Z option and to choice a appropriate
526 * port number for ISAKMP, you can launch some racoons
527 * on the local host for debug.
528 * pk_sendadd() on initiator side is always failed
529 * even if this flag is used. Because there is same
530 * spi in the SAD which is inserted by pk_sendgetspi()
533 printf("Local test mode.\n");
543 lcconf
->default_af
= AF_INET
;
546 lcconf
->default_af
= AF_INET6
;
574 saverestore_params(1);
580 saverestore_params(0);
584 saverestore_params(f
)
587 static u_int16_t s_port_isakmp
;
588 #ifdef ENABLE_ADMINPORT
589 static u_int16_t s_port_admin
;
592 /* 0: save, 1: restore */
594 lcconf
->port_isakmp
= s_port_isakmp
;
595 #ifdef ENABLE_ADMINPORT
596 lcconf
->port_admin
= s_port_admin
;
599 s_port_isakmp
= lcconf
->port_isakmp
;
600 #ifdef ENABLE_ADMINPORT
601 s_port_admin
= lcconf
->port_admin
;