477be1faaa159fb1042d13f753109c28b694cdc7
1 /* strongSwan charon launcher
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
5 * Ported from invokepluto.c to fit charons needs.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include <sys/types.h>
30 #include "../pluto/constants.h"
31 #include "../pluto/defs.h"
32 #include "../pluto/log.h"
35 #include "invokecharon.h"
38 static int _charon_pid
= 0;
39 static int _stop_requested
;
42 starter_charon_pid(void)
48 starter_charon_sigchild(pid_t pid
)
50 if (pid
== _charon_pid
)
55 plog("charon has died -- restart scheduled (%dsec)"
56 , CHARON_RESTART_DELAY
);
57 alarm(CHARON_RESTART_DELAY
); // restart in 5 sec
59 unlink(CHARON_PID_FILE
);
64 starter_stop_charon (void)
74 /* be more and more aggressive */
75 for (i
= 0; i
< 20 && (pid
= _charon_pid
) != 0; i
++)
84 plog("starter_stop_charon(): charon does not respond, sending KILL");
92 plog("starter_stop_charon(): can't stop charon !!!");
97 plog("stater_stop_charon(): charon is not started...");
104 starter_start_charon (starter_config_t
*cfg
, bool debug
)
108 char buffer
[BUF_LEN
];
111 CHARON_CMD
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
112 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
113 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
114 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
119 arg
[argc
++] = "--use-syslog";
122 { /* parse debug string */
123 char *pos
, *level
, *buf_pos
, type
[4];
124 pos
= cfg
->setup
.charondebug
;
126 while (pos
&& sscanf(pos
, "%4s %d,", type
, &level
) == 2)
128 snprintf(buf_pos
, buffer
+ sizeof(buffer
) - buf_pos
, "--debug-%s", type
);
129 arg
[argc
++] = buf_pos
;
130 buf_pos
+= strlen(buf_pos
) + 1;
131 if (buf_pos
>= buffer
+ sizeof(buffer
))
135 snprintf(buf_pos
, buffer
+ sizeof(buffer
) - buf_pos
, "%d", level
);
136 arg
[argc
++] = buf_pos
;
137 buf_pos
+= strlen(buf_pos
) + 1;
138 if (buf_pos
>= buffer
+ sizeof(buffer
))
144 pos
= strchr(pos
, ',');
154 plog("starter_start_charon(): charon already started...");
159 unlink(CHARON_CTL_FILE
);
162 /* if ipsec.secrets file is missing then generate RSA default key pair */
163 if (stat(SECRETS_FILE
, &stb
) != 0)
168 plog("no %s file, generating RSA key", SECRETS_FILE
);
171 system("ipsec scepclient --out pkcs1 --out cert-self --quiet");
175 /* ipsec.secrets is root readable only */
176 oldmask
= umask(0066);
178 f
= fopen(SECRETS_FILE
, "w");
181 fprintf(f
, "# /etc/ipsec.secrets - strongSwan IPsec secrets file\n");
183 fprintf(f
, ": RSA myKey.der\n");
186 chown(SECRETS_FILE
, IPSEC_UID
, IPSEC_GID
);
194 plog("can't fork(): %s", strerror(errno
));
199 sigprocmask(SIG_SETMASK
, 0, NULL
);
200 /* disable glibc's malloc checker, conflicts with leak detective */
201 setenv("MALLOC_CHECK_", "0", 1);
203 plog("can't execv(%s,...): %s", arg
[0], strerror(errno
));
208 for (i
= 0; i
< 50 && _charon_pid
; i
++)
210 /* wait for charon */
212 if (stat(CHARON_PID_FILE
, &stb
) == 0)
215 DBG_log("charon (%d) started", _charon_pid
)
222 /* If charon is started but with no ctl file, stop it */
223 plog("charon too long to start... - kill kill");
224 for (i
= 0; i
< 20 && (pid
= _charon_pid
) != 0; i
++)
237 plog("charon refused to be started");