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"
84 #include <CoreFoundation/CoreFoundation.h>
85 #include "power_mgmt.h"
86 #include "preferences.h"
88 //#include "package_version.h"
90 int f_local
= 0; /* local test mode. behave like a wall. */
91 int vflag
= 1; /* for print-isakmp.c */
92 static int dump_config
= 0; /* dump parsed config file. */
93 static int exec_done
= 0; /* we've already been exec'd */
96 static char version
[] = "@(#)" TOP_PACKAGE_STRING
" (" TOP_PACKAGE_URL
")";
97 #else /* TOP_PACKAGE */
98 static char version
[] = "@(#) racoon / IPsec-tools";
99 #endif /* TOP_PACKAGE */
101 int main (int, char **);
102 static void usage (void);
103 static void parse (int, char **);
104 static void restore_params (void);
105 static void save_params (void);
106 static void saverestore_params (int);
107 static void cleanup_pidfile (void);
108 #if 0 // <rdar://problem/9286626>
109 int launchedbylaunchd (void);
112 pid_t racoon_pid
= 0;
113 int launchdlaunched
= 0;
114 int print_pid
= 1; /* for racoon only */
120 printf("usage: racoon [-BdDFvs%s] %s[-f (file)] [-l (file)] [-p (port)]\n",
128 printf(" -d: debug level, more -d will generate more debug message.\n");
129 printf(" -D: started by LaunchD (implies daemon mode).\n");
130 printf(" -C: dump parsed config file.\n");
131 printf(" -L: include location in debug messages\n");
132 printf(" -F: run in foreground, do not become daemon.\n");
133 printf(" -v: be more verbose\n");
134 printf(" -s: override enable auto exit\n");
136 printf(" -4: IPv4 mode.\n");
137 printf(" -6: IPv6 mode.\n");
139 printf(" -f: pathname for configuration file.\n");
140 printf(" -l: pathname for log file.\n");
141 printf(" -p: port number for isakmp (default: %d).\n", PORT_ISAKMP
);
142 printf(" -P: port number for NAT-T (default: %d).\n", PORT_ISAKMP_NATT
);
160 * racoon is not sandboxed on Mac OS.
161 * On embedded, racoon is sandboxed with a seatbelt-profiles entitlement.
164 if (geteuid() != 0) {
165 errx(1, "must be root to invoke this program.");
170 * Don't let anyone read files I write. Although some files (such as
171 * the PID file) can be other readable, we dare to use the global mask,
172 * because racoon uses fopen(3), which can't specify the permission
173 * at the creation time.
176 if (umask(077) != 077) {
177 errx(1, "could not set umask");
192 plog(ASL_LEVEL_INFO
, "***** racoon started: pid=%d started by: %d, launchdlaunched %d\n", getpid(), getppid(), launchdlaunched
);
193 plog(ASL_LEVEL_INFO
, "%s\n", version
);
195 plog(ASL_LEVEL_INFO
, "@(#)"
196 "This product linked %s (http://www.openssl.org/)"
197 "\n", eay_version());
199 plog(ASL_LEVEL_INFO
, "Reading configuration from \"%s\"\n",
200 lcconf
->racoon_conf
);
202 //%%%%% this sould probably be moved to session()
203 if (pfkey_init() < 0) {
204 errx(1, "failed to initialize pfkey.\n");
209 * in order to prefer the parameters by command line,
210 * saving some parameters before parsing configuration file.
215 errx(1, "failed to parse configuration file.");
218 if (lcconf
->logfile_param
== NULL
&& logFileStr
[0] == 0)
219 plogresetfile(lcconf
->pathinfo
[LC_PATHTYPE_LOGFILE
]);
222 /* Tell the kernel which port to use for UDP encapsulation */
224 int udp_port
= PORT_ISAKMP_NATT
;
225 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &udp_port
, sizeof(udp_port
)) != 0)
226 errx(1, "couldn't set net.inet.ipsec.esp_port to %d. (%s)",
227 udp_port
, strerror(errno
));
233 if(isakmp_cfg_config
.network4
&& isakmp_cfg_config
.pool_size
== 0)
234 if ((error
= isakmp_cfg_resize_pool(ISAKMP_CFG_MAX_CNX
)) != 0)
242 * install SAs from the specified file. If the file is not specified
243 * by the configuration file, racoon will exit.
249 if ( !exec_done
&& launchdlaunched
){
251 "racoon launched by launchd.\n");
253 if (atexit(cleanup_pidfile
) < 0) {
255 "cannot register pidfile cleanup");
260 if (atexit(cleanup_pidfile
) < 0) {
262 "cannot register pidfile cleanup");
265 #define MAX_EXEC_ARGS 32
267 char *args
[MAX_EXEC_ARGS
+ 2]; /* 2 extra, for '-x' and NULL */
271 if (ac
> MAX_EXEC_ARGS
) {
273 "too many arguments.\n");
277 if (daemon(0, 0) < 0) {
278 errx(1, "failed to be daemon. (%s)",
282 /* Radar 5129006 - Prevent non-root user from killing racoon
283 * when launched by setuid process
287 "cannot set uid.\n");
292 "cannot set gid.\n");
296 /* setup args to re-exec - for CoreFoundation issues */
297 args
[0] = PATHRACOON
;
298 for (i
= 1; i
< ac
; i
++)
300 args
[ac
] = "-x"; /* tells racoon its been exec'd */
303 execve(PATHRACOON
, args
, env
);
305 "failed to exec racoon. (%s)", strerror(errno
));
312 /* start the session */
316 #if 0 // <rdar://problem/9286626>
319 launch_data_t checkin_response
= NULL
;
321 if ((checkin_response
= launch_socket_service_check_in()) == NULL
) {
322 plog(LLV_ERROR
, LOCATION
, NULL
,
323 "launch_socket_service_check_in fails.\n");
327 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
328 plog(LLV_ERROR
, LOCATION
, NULL
,
329 "launch_data_get_type fails errno %d.\n", launch_data_get_errno(checkin_response
));
335 /* clean up before we leave */
336 if ( checkin_response
)
337 launch_data_free(checkin_response
);
338 return launchdlaunched
;
345 char pid_file
[MAXPATHLEN
];
348 /* if it's not child process, clean everything */
349 if (racoon_pid
== p
) {
350 if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
] == NULL
)
351 strlcpy(pid_file
, _PATH_VARRUN
"racoon.pid", sizeof(pid_file
));
352 else if (lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
][0] == '/')
353 strlcpy(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
355 strlcat(pid_file
, _PATH_VARRUN
, sizeof(pid_file
));
356 strlcat(pid_file
, lcconf
->pathinfo
[LC_PATHTYPE_PIDFILE
], sizeof(pid_file
));
358 (void) unlink(pid_file
);
375 pname
= strrchr(*av
, '/');
381 while ((c
= getopt(ac
, av
, "dDLFp:P:a:f:l:vsZBCx"
391 plogsetlevel(ASL_LEVEL_DEBUG
);
395 fprintf(stderr
, "-D and -F are mutually exclusive\n");
404 if (launchdlaunched
) {
405 fprintf(stderr
, "-D and -F are mutually exclusive\n");
408 printf("Foreground mode.\n");
412 lcconf
->port_isakmp
= atoi(optarg
);
415 lcconf
->port_isakmp_natt
= atoi(optarg
);
418 fprintf(stderr
, "%s: the option is disabled "
419 "in the configuration\n", pname
);
422 lcconf
->racoon_conf
= optarg
;
425 lcconf
->logfile_param
= optarg
;
431 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_CLIENT
; /* override default auto exit state */
439 * To specify -Z option and to choice a appropriate
440 * port number for ISAKMP, you can launch some racoons
441 * on the local host for debug.
442 * pk_sendadd() on initiator side is always failed
443 * even if this flag is used. Because there is same
444 * spi in the SAD which is inserted by pk_sendgetspi()
447 printf("Local test mode.\n");
457 lcconf
->default_af
= AF_INET
;
460 lcconf
->default_af
= AF_INET6
;
485 saverestore_params(1);
491 saverestore_params(0);
495 saverestore_params(f
)
498 static u_int16_t s_port_isakmp
;
500 /* 0: save, 1: restore */
502 lcconf
->port_isakmp
= s_port_isakmp
;
504 s_port_isakmp
= lcconf
->port_isakmp
;