- added missing files for starter

This commit is contained in:
Martin Willi
2006-04-28 07:17:32 +00:00
parent 2ebcdbadd8
commit a3678ca252
4 changed files with 393 additions and 0 deletions
+174
View File
@@ -0,0 +1,174 @@
/* strongSwan charon launcher
* Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
* Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
*
* Ported from invokepluto.c to fit charons needs.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* RCSID $Id: invokecharon.c $
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <freeswan.h>
#include "../pluto/constants.h"
#include "../pluto/defs.h"
#include "../pluto/log.h"
#include "confread.h"
#include "invokecharon.h"
#include "files.h"
static int _charon_pid = 0;
static int _stop_requested;
pid_t
starter_charon_pid(void)
{
return _charon_pid;
}
void
starter_charon_sigchild(pid_t pid)
{
if (pid == _charon_pid)
{
_charon_pid = 0;
if (!_stop_requested)
{
plog("charon has died -- restart scheduled (%dsec)"
, CHARON_RESTART_DELAY);
alarm(CHARON_RESTART_DELAY); // restart in 5 sec
}
unlink(CHARON_PID_FILE);
}
}
int
starter_stop_charon (void)
{
pid_t pid;
int i;
pid = _charon_pid;
if (pid)
{
_stop_requested = 1;
/* be more and more aggressive */
for (i = 0; i < 20 && (pid = _charon_pid) != 0; i++)
{
if (i == 0)
kill(pid, SIGINT);
else if (i < 10)
kill(pid, SIGTERM);
else
kill(pid, SIGKILL);
usleep(20000);
}
if (_charon_pid == 0)
return 0;
plog("starter_stop_charon(): can't stop charon !!!");
return -1;
}
else
{
plog("stater_stop_charon(): charon is not started...");
}
return -1;
}
int
starter_start_charon (starter_config_t *cfg, bool debug)
{
int pid, i;
struct stat stb;
int argc = 1;
char *arg[] = {
CHARON_CMD, NULL, NULL,
};
if (!debug)
{
arg[argc++] = "--use-syslog";
}
if (_charon_pid)
{
plog("starter_start_charon(): charon already started...");
return -1;
}
else
{
unlink(CHARON_CTL_FILE);
_stop_requested = 0;
pid = fork();
switch (pid)
{
case -1:
plog("can't fork(): %s", strerror(errno));
return -1;
case 0:
/* child */
setsid();
sigprocmask(SIG_SETMASK, 0, NULL);
execv(arg[0], arg);
plog("can't execv(%s,...): %s", arg[0], strerror(errno));
exit(1);
default:
/* father */
_charon_pid = pid;
for (i = 0; i < 50 && _charon_pid; i++)
{
/* wait for charon */
usleep(20000);
if (stat(CHARON_PID_FILE, &stb) == 0)
{
DBG(DBG_CONTROL,
DBG_log("charon (%d) started", _charon_pid)
)
return 0;
}
}
if (_charon_pid)
{
/* If charon is started but with no ctl file, stop it */
plog("charon too long to start... - kill kill");
for (i = 0; i < 20 && (pid = _charon_pid) != 0; i++)
{
if (i == 0)
kill(pid, SIGINT);
else if (i < 10)
kill(pid, SIGTERM);
else
kill(pid, SIGKILL);
usleep(20000);
}
}
else
{
plog("charon refused to be started");
}
return -1;
}
}
return -1;
}
+31
View File
@@ -0,0 +1,31 @@
/* strongSwan charon launcher
* Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
* Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
*
* Ported from invokepluto.h to fit charons needs.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* RCSID $Id: invokecharon.h $
*/
#ifndef _STARTER_CHARON_H_
#define _STARTER_CHARON_H_
#define CHARON_RESTART_DELAY 5
extern void starter_charon_sigchild (pid_t pid);
extern pid_t starter_charon_pid (void);
extern int starter_stop_charon (void);
extern int starter_start_charon(struct starter_config *cfg, bool debug);
#endif /* _STARTER_CHARON_H_ */
+161
View File
@@ -0,0 +1,161 @@
/* Stroke for charon is the counterpart to whack from pluto
* Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* RCSID $Id: starterstroke.c $
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <freeswan.h>
#include "../pluto/constants.h"
#include "../pluto/defs.h"
#include "../pluto/log.h"
#include "../charon/stroke/stroke.h"
#include "starterstroke.h"
#include "confread.h"
#include "files.h"
static char* push_string(stroke_msg_t **strm, char *string)
{
stroke_msg_t *stroke_msg;
size_t string_length;
if (string == NULL)
{
return NULL;
}
stroke_msg = *strm;
string_length = strlen(string) + 1;
stroke_msg->length += string_length;
stroke_msg = realloc(stroke_msg, stroke_msg->length);
strcpy((char*)stroke_msg + stroke_msg->length - string_length, string);
*strm = stroke_msg;
return (char*)(u_int)stroke_msg->length - string_length;
}
static int
send_stroke_msg (stroke_msg_t *msg)
{
struct sockaddr_un ctl_addr = { AF_UNIX, CHARON_CTL_FILE };
int sock;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
plog("socket() failed: %s", strerror(errno));
return -1;
}
if (connect(sock, (struct sockaddr *)&ctl_addr,
offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
{
plog("connect(charon_ctl) failed: %s", strerror(errno));
close(sock);
return -1;
}
/* send message */
if (write(sock, msg, msg->length) != msg->length)
{
plog("write(charon_ctl) failed: %s", strerror(errno));
close(sock);
return -1;
}
close(sock);
return 0;
}
static char *
connection_name(starter_conn_t *conn)
{
/* if connection name is '%auto', create a new name like conn_xxxxx */
static char buf[32];
if (streq(conn->name, "%auto"))
{
sprintf(buf, "conn_%ld", conn->id);
return buf;
}
return conn->name;
}
int starter_stroke_add_conn(starter_conn_t *conn)
{
stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
int res;
msg->length = sizeof(stroke_msg_t);
msg->type = STR_ADD_CONN;
msg->add_conn.name = push_string(&msg, connection_name(conn));
msg->add_conn.me.id = push_string(&msg, conn->left.id);
msg->add_conn.me.cert = push_string(&msg, conn->left.cert);
msg->add_conn.me.address = push_string(&msg, inet_ntoa(conn->left.addr.u.v4.sin_addr));
msg->add_conn.me.subnet = push_string(&msg, inet_ntoa(conn->left.subnet.addr.u.v4.sin_addr));
msg->add_conn.me.subnet_mask = conn->left.subnet.maskbits;
msg->add_conn.other.id = push_string(&msg, conn->right.id);
msg->add_conn.other.cert = push_string(&msg, conn->right.cert);
msg->add_conn.other.address = push_string(&msg, inet_ntoa(conn->right.addr.u.v4.sin_addr));
msg->add_conn.other.subnet = push_string(&msg, inet_ntoa(conn->right.subnet.addr.u.v4.sin_addr));
msg->add_conn.other.subnet_mask = conn->right.subnet.maskbits;
res = send_stroke_msg(msg);
free(msg);
return res;
}
int starter_stroke_del_conn(starter_conn_t *conn)
{
return 0;
}
int starter_stroke_route_conn(starter_conn_t *conn)
{
stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
int res;
msg->length = sizeof(stroke_msg_t);
msg->type = STR_INSTALL;
msg->install.name = push_string(&msg, connection_name(conn));
res = send_stroke_msg(msg);
free(msg);
return res;
}
int starter_stroke_initiate_conn(starter_conn_t *conn)
{
stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
int res;
msg->length = sizeof(stroke_msg_t);
msg->type = STR_INITIATE;
msg->initiate.name = push_string(&msg, connection_name(conn));
res = send_stroke_msg(msg);
free(msg);
return res;
}
+27
View File
@@ -0,0 +1,27 @@
/* Stroke for charon is the counterpart to whack from pluto
* Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* RCSID $Id: starterstroke.h $
*/
#ifndef _STARTER_STROKE_H_
#define _STARTER_STROKE_H_
#include "confread.h"
extern int starter_stroke_add_conn(starter_conn_t *conn);
extern int starter_stroke_del_conn(starter_conn_t *conn);
extern int starter_stroke_route_conn(starter_conn_t *conn);
extern int starter_stroke_initiate_conn(starter_conn_t *conn);
#endif /* _STARTER_STROKE_H_ */