1 /* strongSwan Pluto launcher
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * RCSID $Id: invokepluto.c,v 1.12 2006/02/17 21:41:50 as Exp $
17 #include <sys/types.h>
27 #include "../pluto/constants.h"
28 #include "../pluto/defs.h"
29 #include "../pluto/log.h"
32 #include "invokepluto.h"
34 #include "starterwhack.h"
36 static int _pluto_pid
= 0;
37 static int _stop_requested
;
40 starter_pluto_pid(void)
46 starter_pluto_sigchild(pid_t pid
)
48 if (pid
== _pluto_pid
)
53 plog("pluto has died -- restart scheduled (%dsec)"
54 , PLUTO_RESTART_DELAY
);
55 alarm(PLUTO_RESTART_DELAY
); // restart in 5 sec
57 unlink(PLUTO_PID_FILE
);
62 starter_stop_pluto (void)
71 if (starter_whack_shutdown() == 0)
73 for (i
= 0; i
< 20; i
++)
80 /* be more and more aggressive */
81 for (i
= 0; i
< 20 && (pid
= _pluto_pid
) != 0; i
++)
91 plog("starter_stop_pluto(): can't stop pluto !!!");
96 plog("stater_stop_pluto(): pluto is not started...");
101 #define ADD_DEBUG(v) { \
102 for (l = cfg->setup.plutodebug; l && *l; l++) if (streq(*l, v)) \
103 arg[argc++] = "--debug-" v; \
107 starter_start_pluto (starter_config_t
*cfg
, bool debug
)
115 PLUTO_CMD
, "--nofork"
116 , NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
117 , NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
118 , NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
119 , NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
122 printf ("starter_start_pluto entered\n");
126 arg
[argc
++] = "--stderrlog";
128 if (cfg
->setup
.uniqueids
)
130 arg
[argc
++] = "--uniqueids";
137 ADD_DEBUG("emitting")
139 ADD_DEBUG("lifecycle")
144 ADD_DEBUG("controlmore")
146 if (cfg
->setup
.crlcheckinterval
> 0)
148 static char buf1
[15];
150 arg
[argc
++] = "--crlcheckinterval";
151 snprintf(buf1
, sizeof(buf1
), "%u", cfg
->setup
.crlcheckinterval
);
154 if (cfg
->setup
.cachecrls
)
156 arg
[argc
++] = "--cachecrls";
158 if (cfg
->setup
.strictcrlpolicy
)
160 arg
[argc
++] = "--strictcrlpolicy";
162 if (cfg
->setup
.nocrsend
)
164 arg
[argc
++] = "--nocrsend";
166 if (cfg
->setup
.nat_traversal
)
168 arg
[argc
++] = "--nat_traversal";
170 if (cfg
->setup
.keep_alive
)
172 static char buf2
[15];
174 arg
[argc
++] = "--keep_alive";
175 snprintf(buf2
, sizeof(buf2
), "%u", cfg
->setup
.keep_alive
);
179 if (cfg
->setup
.virtual_private
)
181 arg
[argc
++] = "--virtual_private";
182 arg
[argc
++] = cfg
->setup
.virtual_private
;
185 if (cfg
->setup
.pkcs11module
)
187 arg
[argc
++] = "--pkcs11module";
188 arg
[argc
++] = cfg
->setup
.pkcs11module
;
190 if (cfg
->setup
.pkcs11initargs
)
192 arg
[argc
++] = "--pkcs11initargs";
193 arg
[argc
++] = cfg
->setup
.pkcs11initargs
;
195 if (cfg
->setup
.pkcs11keepstate
)
197 arg
[argc
++] = "--pkcs11keepstate";
199 if (cfg
->setup
.pkcs11proxy
)
201 arg
[argc
++] = "--pkcs11proxy";
206 plog("starter_start_pluto(): pluto already started...");
211 unlink(PLUTO_CTL_FILE
);
214 if (cfg
->setup
.prepluto
)
215 system(cfg
->setup
.prepluto
);
217 /* if ipsec.secrets file is missing then generate RSA default key pair */
218 if (stat(SECRETS_FILE
, &stb
) != 0)
223 plog("no %s file, generating RSA key", SECRETS_FILE
);
226 system("ipsec scepclient --out pkcs1 --out cert-self --quiet");
230 /* ipsec.secrets is root readable only */
231 oldmask
= umask(0066);
233 f
= fopen(SECRETS_FILE
, "w");
236 fprintf(f
, "# /etc/ipsec.secrets - strongSwan IPsec secrets file\n");
238 fprintf(f
, ": RSA myKey.der\n");
241 chown(SECRETS_FILE
, IPSEC_UID
, IPSEC_GID
);
249 plog("can't fork(): %s", strerror(errno
));
254 sigprocmask(SIG_SETMASK
, 0, NULL
);
256 plog("can't execv(%s,...): %s", arg
[0], strerror(errno
));
261 for (i
= 0; i
< 50 && _pluto_pid
; i
++)
265 if (stat(PLUTO_CTL_FILE
, &stb
) == 0)
268 DBG_log("pluto (%d) started", _pluto_pid
)
270 if (cfg
->setup
.postpluto
)
271 system(cfg
->setup
.postpluto
);
277 /* If pluto is started but with no ctl file, stop it */
278 plog("pluto too long to start... - kill kill");
279 for (i
= 0; i
< 20 && (pid
= _pluto_pid
) != 0; i
++)
290 plog("pluto refused to be started");