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"
71 #include "isakmp_xauth.h"
72 #include "isakmp_cfg.h"
74 #include "remoteconf.h"
75 #include "localconf.h"
80 #include "crypto_openssl.h"
83 #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
85 #endif // !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
88 #include <CoreFoundation/CoreFoundation.h>
89 #include "power_mgmt.h"
90 #include "preferences.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 dump_config
= 0; /* dump parsed config file. */
97 static int exec_done
= 0; /* we've already been exec'd */
100 static char version
[] = "@(#)" TOP_PACKAGE_STRING
" (" TOP_PACKAGE_URL
")";
101 #else /* TOP_PACKAGE */
102 static char version
[] = "@(#) racoon / IPsec-tools";
103 #endif /* TOP_PACKAGE */
105 int main (int, char **);
106 static void usage (void);
107 static void parse (int, char **);
108 static void restore_params (void);
109 static void save_params (void);
110 static void saverestore_params (int);
111 static void cleanup_pidfile (void);
112 #if 0 // <rdar://problem/9286626>
113 int launchedbylaunchd (void);
116 pid_t racoon_pid
= 0;
117 int launchdlaunched
= 0;
118 int print_pid
= 1; /* for racoon only */
124 printf("usage: racoon [-BdDFvs%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
132 printf(" -d: debug level, more -d will generate more debug message.\n");
133 printf(" -D: started by LaunchD (implies daemon mode).\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 printf(" -f: pathname for configuration file.\n");
144 printf(" -l: pathname for log file.\n");
145 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
146 printf(" -P: port number for NAT-T (default: %d).\n", PORT_ISAKMP_NATT
);
157 #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
159 if (sandbox_init("racoon", SANDBOX_NAMED
, &errorbuf
) == -1) {
160 plog(ASL_LEVEL_ERR
, "initializing sandbox failed %s", errorbuf
);
161 sandbox_free_error(errorbuf
);
164 #endif // !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
172 if (geteuid() != 0) {
173 errx(1, "must be root to invoke this program.");
178 * Don't let anyone read files I write. Although some files (such as
179 * the PID file) can be other readable, we dare to use the global mask,
180 * because racoon uses fopen(3), which can't specify the permission
181 * at the creation time.
184 if (umask(077) != 077) {
185 errx(1, "could not set umask");
200 plog(ASL_LEVEL_NOTICE
, "racoon started: pid=%d started by: %d, launchdlaunched %d\n", getpid(), getppid(), launchdlaunched
);
201 plog(ASL_LEVEL_NOTICE
, "%s\n", version
);
203 plog(ASL_LEVEL_NOTICE
, "@(#)"
204 "This product linked %s (http://www.openssl.org/)"
205 "\n", eay_version());
207 plog(ASL_LEVEL_NOTICE
, "Reading configuration from \"%s\"\n",
208 lcconf
->racoon_conf
);
210 //%%%%% this sould probably be moved to session()
211 if (pfkey_init() < 0) {
212 errx(1, "failed to initialize pfkey.\n");
217 * in order to prefer the parameters by command line,
218 * saving some parameters before parsing configuration file.
223 errx(1, "failed to parse configuration file.");
226 if (lcconf
->logfile_param
== NULL
&& logFileStr
[0] == 0)
227 plogresetfile(lcconf
->pathinfo
[LC_PATHTYPE_LOGFILE
]);
230 /* Tell the kernel which port to use for UDP encapsulation */
232 int udp_port
= PORT_ISAKMP_NATT
;
233 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
234 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
235 udp_port
, strerror(errno
));
241 if(isakmp_cfg_config
.network4
&& isakmp_cfg_config
.pool_size
== 0)
242 if ((error
= isakmp_cfg_resize_pool(ISAKMP_CFG_MAX_CNX
)) != 0)
250 * install SAs from the specified file. If the file is not specified
251 * by the configuration file, racoon will exit.
257 if ( !exec_done
&& launchdlaunched
){
258 plog(ASL_LEVEL_NOTICE
,
259 "racoon launched by launchd.\n");
261 if (atexit(cleanup_pidfile
) < 0) {
263 "cannot register pidfile cleanup");
268 if (atexit(cleanup_pidfile
) < 0) {
270 "cannot register pidfile cleanup");
273 #define MAX_EXEC_ARGS 32
275 char *args
[MAX_EXEC_ARGS
+ 2]; /* 2 extra, for '-x' and NULL */
279 if (ac
> MAX_EXEC_ARGS
) {
281 "too many arguments.\n");
285 if (daemon(0, 0) < 0) {
286 errx(1, "failed to be daemon. (%s)",
290 /* Radar 5129006 - Prevent non-root user from killing racoon
291 * when launched by setuid process
295 "cannot set uid.\n");
300 "cannot set gid.\n");
304 /* setup args to re-exec - for CoreFoundation issues */
305 args
[0] = PATHRACOON
;
306 for (i
= 1; i
< ac
; i
++)
308 args
[ac
] = "-x"; /* tells racoon its been exec'd */
311 execve(PATHRACOON
, args
, env
);
313 "failed to exec racoon. (%s)", strerror(errno
));
320 /* start the session */
324 #if 0 // <rdar://problem/9286626>
327 launch_data_t checkin_response
= NULL
;
329 if ((checkin_response
= launch_socket_service_check_in()) == NULL
) {
330 plog(LLV_ERROR
, LOCATION
, NULL
,
331 "launch_socket_service_check_in fails.\n");
335 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
336 plog(LLV_ERROR
, LOCATION
, NULL
,
337 "launch_data_get_type fails errno %d.\n", launch_data_get_errno(checkin_response
));
343 /* clean up before we leave */
344 if ( checkin_response
)
345 launch_data_free(checkin_response
);
346 return launchdlaunched
;
353 char pid_file
[MAXPATHLEN
];
356 /* if it's not child process, clean everything */
357 if (racoon_pid
== p
) {
358 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
359 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", sizeof(pid_file
));
360 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
361 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
363 strlcat(pid_file
, _PATH_VARRUN
, sizeof(pid_file
));
364 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
366 (void) unlink(pid_file
);
383 pname
= strrchr(*av
, '/');
389 while ((c
= getopt(ac
, av
, "dDLFp:P:a:f:l:vsZBCx"
399 plogsetlevel(ASL_LEVEL_DEBUG
);
403 fprintf(stderr
, "-D and -F are mutually exclusive\n");
412 if (launchdlaunched
) {
413 fprintf(stderr
, "-D and -F are mutually exclusive\n");
416 printf("Foreground mode.\n");
420 lcconf
->port_isakmp
= atoi(optarg
);
423 lcconf
->port_isakmp_natt
= atoi(optarg
);
426 fprintf(stderr
, "%s: the option is disabled "
427 "in the configuration\n", pname
);
430 lcconf
->racoon_conf
= optarg
;
433 lcconf
->logfile_param
= optarg
;
439 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_CLIENT
; /* override default auto exit state */
447 * To specify -Z option and to choice a appropriate
448 * port number for ISAKMP, you can launch some racoons
449 * on the local host for debug.
450 * pk_sendadd() on initiator side is always failed
451 * even if this flag is used. Because there is same
452 * spi in the SAD which is inserted by pk_sendgetspi()
455 printf("Local test mode.\n");
465 lcconf
->default_af
= AF_INET
;
468 lcconf
->default_af
= AF_INET6
;
493 saverestore_params(1);
499 saverestore_params(0);
503 saverestore_params(f
)
506 static u_int16_t s_port_isakmp
;
508 /* 0: save, 1: restore */
510 lcconf
->port_isakmp
= s_port_isakmp
;
512 s_port_isakmp
= lcconf
->port_isakmp
;