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");
308 * install SAs from the specified file. If the file is not specified
309 * by the configuration file, racoon will exit.
311 if (loading_sa
&& !f_local
) {
312 if (backupsa_from_file() != 0)
313 errx(1, "something error happened "
320 if ( !exec_done
&& launchedbylaunchd() ){
321 plog(LLV_INFO
, LOCATION
, NULL
,
322 "racoon launched by launchd.\n");
324 if (atexit(cleanup_pidfile
) < 0) {
325 plog(LLV_ERROR
, LOCATION
, NULL
,
326 "cannot register pidfile cleanup");
331 if (atexit(cleanup_pidfile
) < 0) {
332 plog(LLV_ERROR
, LOCATION
, NULL
,
333 "cannot register pidfile cleanup");
336 #define MAX_EXEC_ARGS 32
338 char *args
[MAX_EXEC_ARGS
+ 2]; /* 2 extra, for '-x' and NULL */
342 if (ac
> MAX_EXEC_ARGS
) {
343 plog(LLV_ERROR
, LOCATION
, NULL
,
344 "too many arguments.\n");
348 if (daemon(0, 0) < 0) {
349 errx(1, "failed to be daemon. (%s)",
353 /* Radar 5129006 - Prevent non-root user from killing racoon
354 * when launched by setuid process
357 plog(LLV_ERROR
, LOCATION
, NULL
,
358 "cannot set uid.\n");
362 plog(LLV_ERROR
, LOCATION
, NULL
,
363 "cannot set gid.\n");
367 /* setup args to re-exec - for CoreFoundation issues */
368 args
[0] = PATHRACOON
;
369 for (i
= 1; i
< ac
; i
++)
371 args
[ac
] = "-x"; /* tells racoon its been exec'd */
374 execve(PATHRACOON
, args
, env
);
375 plog(LLV_ERROR
, LOCATION
, NULL
,
376 "failed to exec racoon. (%s)", strerror(errno
));
390 int launchdlaunched
= 1;
391 launch_data_t checkin_response
= NULL
;
392 launch_data_t checkin_request
= NULL
;
394 /* check in with launchd */
395 if ((checkin_request
= launch_data_new_string(LAUNCH_KEY_CHECKIN
)) == NULL
) {
396 plog(LLV_ERROR
, LOCATION
, NULL
,
397 "launch_data_new_string fails.\n");
401 if ((checkin_response
= launch_msg(checkin_request
)) == NULL
) {
402 plog(LLV_ERROR
, LOCATION
, NULL
,
403 "launch_msg fails.\n");
407 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
408 plog(LLV_ERROR
, LOCATION
, NULL
,
409 "launch_data_get_type fails errno %d.\n", launch_data_get_errno(checkin_response
));
415 /* clean up before we leave */
416 if ( checkin_request
)
417 launch_data_free(checkin_request
);
418 if ( checkin_response
)
419 launch_data_free(checkin_response
);
420 return launchdlaunched
;
426 char pid_file
[MAXPATHLEN
];
429 /* if it's not child process, clean everything */
430 if (racoon_pid
== p
) {
431 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
432 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", sizeof(pid_file
));
433 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
434 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
436 strlcat(pid_file
, _PATH_VARRUN
, sizeof(pid_file
));
437 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
439 (void) unlink(pid_file
);
456 pname
= strrchr(*av
, '/');
462 #if 0 /* for debugging */
464 plogset("/tmp/racoon.log");
467 while ((c
= getopt(ac
, av
, "dLFp:P:a:f:l:vsZBCx"
483 printf("Foreground mode.\n");
487 lcconf
->port_isakmp
= atoi(optarg
);
490 lcconf
->port_isakmp_natt
= atoi(optarg
);
493 #ifdef ENABLE_ADMINPORT
494 lcconf
->port_admin
= atoi(optarg
);
497 fprintf(stderr
, "%s: the option is disabled "
498 "in the configuration\n", pname
);
502 lcconf
->racoon_conf
= optarg
;
505 lcconf
->logfile_param
= optarg
;
511 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_CLIENT
; /* override default auto exit state */
519 * To specify -Z option and to choice a appropriate
520 * port number for ISAKMP, you can launch some racoons
521 * on the local host for debug.
522 * pk_sendadd() on initiator side is always failed
523 * even if this flag is used. Because there is same
524 * spi in the SAD which is inserted by pk_sendgetspi()
527 printf("Local test mode.\n");
537 lcconf
->default_af
= AF_INET
;
540 lcconf
->default_af
= AF_INET6
;
568 saverestore_params(1);
574 saverestore_params(0);
578 saverestore_params(f
)
581 static u_int16_t s_port_isakmp
;
582 #ifdef ENABLE_ADMINPORT
583 static u_int16_t s_port_admin
;
586 /* 0: save, 1: restore */
588 lcconf
->port_isakmp
= s_port_isakmp
;
589 #ifdef ENABLE_ADMINPORT
590 lcconf
->port_admin
= s_port_admin
;
593 s_port_isakmp
= lcconf
->port_isakmp
;
594 #ifdef ENABLE_ADMINPORT
595 s_port_admin
= lcconf
->port_admin
;