properly implemented interface_managers initiate, terminte_[ike|child]
proper thread release when stroke is CTRL+C'ed fixed some permission issues
This commit is contained in:
@@ -30,8 +30,6 @@
|
||||
#include <daemon.h>
|
||||
#include <library.h>
|
||||
#include <control/interfaces/interface.h>
|
||||
#include <processing/job_queue.h>
|
||||
#include <processing/jobs/initiate_job.h>
|
||||
|
||||
|
||||
typedef struct private_interface_manager_t private_interface_manager_t;
|
||||
@@ -82,6 +80,11 @@ struct interface_bus_listener_t {
|
||||
* user parameter to pass to callback
|
||||
*/
|
||||
void *param;
|
||||
|
||||
/**
|
||||
* caller has cancelled its listening subscription
|
||||
*/
|
||||
bool cancelled;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -93,86 +96,46 @@ static iterator_t* create_ike_sa_iterator(interface_manager_t *this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of interface_manager_t.initiate.
|
||||
* listener function for initiate
|
||||
*/
|
||||
static status_t initiate(private_interface_manager_t *this,
|
||||
peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
|
||||
interface_manager_cb_t cb, void *param)
|
||||
{
|
||||
ike_sa_t *ours = NULL;
|
||||
job_t *job;
|
||||
status_t retval;
|
||||
|
||||
charon->bus->set_listen_state(charon->bus, TRUE);
|
||||
|
||||
job = (job_t*)initiate_job_create(peer_cfg, child_cfg);
|
||||
charon->job_queue->add(charon->job_queue, job);
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
level_t level;
|
||||
signal_t signal;
|
||||
int thread;
|
||||
ike_sa_t *ike_sa;
|
||||
char* format;
|
||||
va_list args;
|
||||
|
||||
signal = charon->bus->listen(charon->bus, &level, &thread,
|
||||
&ike_sa, &format, &args);
|
||||
|
||||
if (cb && (ike_sa == ours || ours == NULL))
|
||||
{
|
||||
if (!cb(param, signal, level, ike_sa, format, args))
|
||||
{
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
return NEED_MORE;
|
||||
}
|
||||
}
|
||||
|
||||
switch (signal)
|
||||
{
|
||||
case CHILD_UP_SUCCESS:
|
||||
if (ike_sa == ours)
|
||||
{
|
||||
retval = SUCCESS;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
case CHILD_UP_FAILED:
|
||||
case IKE_UP_FAILED:
|
||||
if (ike_sa == ours)
|
||||
{
|
||||
retval = FAILED;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
case CHILD_UP_START:
|
||||
case IKE_UP_START:
|
||||
if (ours == NULL)
|
||||
{
|
||||
ours = ike_sa;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* listener function for terminate_ike
|
||||
*/
|
||||
static bool terminate_listener(interface_bus_listener_t *this, signal_t signal,
|
||||
level_t level, int thread, ike_sa_t *ike_sa,
|
||||
char* format, va_list args)
|
||||
static bool initiate_listener(interface_bus_listener_t *this, signal_t signal,
|
||||
level_t level, int thread, ike_sa_t *ike_sa,
|
||||
char* format, va_list args)
|
||||
{
|
||||
if (this->ike_sa == ike_sa)
|
||||
{
|
||||
if (!this->callback(this->param, signal, level, ike_sa, format, args))
|
||||
{
|
||||
this->cancelled = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
switch (signal)
|
||||
{
|
||||
case IKE_UP_FAILED:
|
||||
case CHILD_UP_FAILED:
|
||||
case CHILD_UP_SUCCESS:
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* listener function for terminate_ike
|
||||
*/
|
||||
static bool terminate_ike_listener(interface_bus_listener_t *this, signal_t signal,
|
||||
level_t level, int thread, ike_sa_t *ike_sa,
|
||||
char* format, va_list args)
|
||||
{
|
||||
if (this->ike_sa == ike_sa)
|
||||
{
|
||||
if (!this->callback(this->param, signal, level, ike_sa, format, args))
|
||||
{
|
||||
this->cancelled = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
switch (signal)
|
||||
@@ -189,6 +152,117 @@ static bool terminate_listener(interface_bus_listener_t *this, signal_t signal,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* listener function for terminate_child
|
||||
*/
|
||||
static bool terminate_child_listener(interface_bus_listener_t *this, signal_t signal,
|
||||
level_t level, int thread, ike_sa_t *ike_sa,
|
||||
char* format, va_list args)
|
||||
{
|
||||
if (this->ike_sa == ike_sa)
|
||||
{
|
||||
if (!this->callback(this->param, signal, level, ike_sa, format, args))
|
||||
{
|
||||
this->cancelled = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
switch (signal)
|
||||
{
|
||||
case IKE_DOWN_FAILED:
|
||||
case IKE_DOWN_SUCCESS:
|
||||
case CHILD_DOWN_FAILED:
|
||||
case CHILD_DOWN_SUCCESS:
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of interface_manager_t.initiate.
|
||||
*/
|
||||
static status_t initiate(private_interface_manager_t *this,
|
||||
peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
|
||||
interface_manager_cb_t callback, void *param)
|
||||
{
|
||||
ike_sa_t *ike_sa;
|
||||
ike_cfg_t *ike_cfg;
|
||||
status_t retval = FAILED;
|
||||
interface_bus_listener_t listener;
|
||||
|
||||
ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_peer(charon->ike_sa_manager,
|
||||
ike_cfg->get_my_host(ike_cfg), ike_cfg->get_other_host(ike_cfg),
|
||||
peer_cfg->get_my_id(peer_cfg), peer_cfg->get_other_id(peer_cfg));
|
||||
|
||||
if (ike_sa->get_peer_cfg(ike_sa) == NULL)
|
||||
{
|
||||
ike_sa->set_peer_cfg(ike_sa, peer_cfg);
|
||||
}
|
||||
|
||||
listener.listener.signal = (void*)initiate_listener;
|
||||
listener.callback = callback;
|
||||
listener.ike_sa = ike_sa;
|
||||
listener.param = param;
|
||||
listener.cancelled = FALSE;
|
||||
|
||||
/* we listen passively to catch the signals we are raising in
|
||||
* ike_sa->delete(). */
|
||||
if (callback)
|
||||
{
|
||||
charon->bus->add_listener(charon->bus, &listener.listener);
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, TRUE);
|
||||
if (ike_sa->initiate(ike_sa, child_cfg) != SUCCESS)
|
||||
{
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
|
||||
return FAILED;
|
||||
}
|
||||
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
||||
|
||||
/* wait until we get a result */
|
||||
while (TRUE)
|
||||
{
|
||||
level_t level;
|
||||
signal_t signal;
|
||||
int thread;
|
||||
ike_sa_t *current;
|
||||
char* format;
|
||||
va_list args;
|
||||
|
||||
/* stop listening if the passive listener returned FALSE */
|
||||
if (listener.cancelled)
|
||||
{
|
||||
retval = NEED_MORE;
|
||||
break;
|
||||
}
|
||||
signal = charon->bus->listen(charon->bus, &level, &thread,
|
||||
¤t, &format, &args);
|
||||
/* ike_sa is a valid pointer until we get one of the signals */
|
||||
if (ike_sa == current)
|
||||
{
|
||||
switch (signal)
|
||||
{
|
||||
case CHILD_UP_SUCCESS:
|
||||
retval = SUCCESS;
|
||||
case CHILD_UP_FAILED:
|
||||
case IKE_UP_FAILED:
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of interface_manager_t.terminate_ike.
|
||||
*/
|
||||
@@ -196,7 +270,8 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
interface_manager_cb_t callback, void *param)
|
||||
{
|
||||
ike_sa_t *ike_sa;
|
||||
status_t status;
|
||||
status_t status = FAILED;;
|
||||
interface_bus_listener_t listener;
|
||||
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
|
||||
unique_id, FALSE);
|
||||
@@ -205,15 +280,15 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
/* we listen passively first, to catch the signals we are raising */
|
||||
/* we listen passively to catch the signals we are raising in
|
||||
* ike_sa->delete(). */
|
||||
listener.listener.signal = (void*)terminate_ike_listener;
|
||||
listener.callback = callback;
|
||||
listener.ike_sa = ike_sa;
|
||||
listener.param = param;
|
||||
listener.cancelled = FALSE;
|
||||
if (callback)
|
||||
{
|
||||
interface_bus_listener_t listener;
|
||||
|
||||
listener.listener.signal = (void*)terminate_listener;
|
||||
listener.callback = callback;
|
||||
listener.ike_sa = ike_sa;
|
||||
listener.param = param;
|
||||
charon->bus->add_listener(charon->bus, &listener.listener);
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, TRUE);
|
||||
@@ -236,9 +311,17 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
char* format;
|
||||
va_list args;
|
||||
|
||||
/* stop listening if the passive listener returned FALSE */
|
||||
if (listener.cancelled)
|
||||
{
|
||||
status = NEED_MORE;
|
||||
break;
|
||||
}
|
||||
signal = charon->bus->listen(charon->bus, &level, &thread,
|
||||
¤t, &format, &args);
|
||||
|
||||
|
||||
/* even if we checked in the IKE_SA, the pointer is valid until
|
||||
* we get an IKE_DOWN_... */
|
||||
if (ike_sa == current)
|
||||
{
|
||||
switch (signal)
|
||||
@@ -246,7 +329,8 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
case IKE_DOWN_FAILED:
|
||||
case IKE_DOWN_SUCCESS:
|
||||
{
|
||||
break;
|
||||
status = SUCCESS;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
continue;
|
||||
@@ -257,7 +341,7 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
|
||||
return SUCCESS;
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,7 +350,100 @@ static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id,
|
||||
static status_t terminate_child(interface_manager_t *this, u_int32_t reqid,
|
||||
interface_manager_cb_t callback, void *param)
|
||||
{
|
||||
return FAILED;
|
||||
ike_sa_t *ike_sa;
|
||||
child_sa_t *child_sa;
|
||||
iterator_t *iterator;
|
||||
status_t status = FAILED;
|
||||
interface_bus_listener_t listener;
|
||||
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
|
||||
reqid, TRUE);
|
||||
if (ike_sa == NULL)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
iterator = ike_sa->create_child_sa_iterator(ike_sa);
|
||||
while (iterator->iterate(iterator, (void**)&child_sa))
|
||||
{
|
||||
if (child_sa->get_state(child_sa) != CHILD_ROUTED &&
|
||||
child_sa->get_reqid(child_sa) == reqid)
|
||||
{
|
||||
break;
|
||||
}
|
||||
child_sa = NULL;
|
||||
}
|
||||
iterator->destroy(iterator);
|
||||
|
||||
if (child_sa == NULL)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
listener.listener.signal = (void*)terminate_child_listener;
|
||||
listener.callback = callback;
|
||||
listener.ike_sa = ike_sa;
|
||||
listener.param = param;
|
||||
listener.cancelled = FALSE;
|
||||
|
||||
/* we listen passively to catch the signals we are raising */
|
||||
if (callback)
|
||||
{
|
||||
charon->bus->add_listener(charon->bus, &listener.listener);
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, TRUE);
|
||||
status = ike_sa->delete_child_sa(ike_sa, child_sa->get_protocol(child_sa),
|
||||
child_sa->get_spi(child_sa, TRUE));
|
||||
if (status == DESTROY_ME)
|
||||
{
|
||||
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
|
||||
}
|
||||
else
|
||||
{
|
||||
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
||||
|
||||
/* wait until CHILD_SA is cleanly deleted using a delete message */
|
||||
while (TRUE)
|
||||
{
|
||||
level_t level;
|
||||
signal_t signal;
|
||||
int thread;
|
||||
ike_sa_t *current;
|
||||
char* format;
|
||||
va_list args;
|
||||
|
||||
/* stop listening if the passive listener returned FALSE */
|
||||
if (listener.cancelled)
|
||||
{
|
||||
status = NEED_MORE;
|
||||
break;
|
||||
}
|
||||
signal = charon->bus->listen(charon->bus, &level, &thread,
|
||||
¤t, &format, &args);
|
||||
/* even if we checked in the IKE_SA, the pointer is valid until
|
||||
* we get an IKE_DOWN_... */
|
||||
if (ike_sa == current)
|
||||
{
|
||||
switch (signal)
|
||||
{
|
||||
case IKE_DOWN_FAILED:
|
||||
case IKE_DOWN_SUCCESS:
|
||||
case CHILD_DOWN_FAILED:
|
||||
case CHILD_DOWN_SUCCESS:
|
||||
{
|
||||
status = SUCCESS;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
charon->bus->set_listen_state(charon->bus, FALSE);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,7 +42,6 @@
|
||||
#include <crypto/crl.h>
|
||||
#include <control/interface_manager.h>
|
||||
#include <control/interfaces/interface.h>
|
||||
#include <processing/jobs/initiate_job.h>
|
||||
#include <processing/jobs/route_job.h>
|
||||
#include <utils/leak_detective.h>
|
||||
|
||||
@@ -661,9 +660,12 @@ static bool stroke_log(stroke_log_info_t *info, signal_t signal, level_t level,
|
||||
{
|
||||
if (level <= info->level)
|
||||
{
|
||||
vfprintf(info->out, format, args);
|
||||
fprintf(info->out, "\n");
|
||||
fflush(info->out);
|
||||
if (vfprintf(info->out, format, args) < 0 ||
|
||||
fprintf(info->out, "\n") < 0 ||
|
||||
fflush(info->out) != 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
@@ -748,6 +750,8 @@ static void stroke_initiate(private_stroke_interface_t *this,
|
||||
|
||||
charon->interfaces->initiate(charon->interfaces, peer_cfg, child_cfg,
|
||||
(interface_manager_cb_t)stroke_log, &info);
|
||||
peer_cfg->destroy(peer_cfg);
|
||||
child_cfg->destroy(child_cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -799,8 +803,9 @@ static void stroke_terminate(private_stroke_interface_t *this,
|
||||
u_int32_t id = 0;
|
||||
bool child;
|
||||
int len;
|
||||
status_t status = SUCCESS;;
|
||||
ike_sa_t *ike_sa;
|
||||
iterator_t *iterator;
|
||||
stroke_log_info_t info;
|
||||
|
||||
pop_string(msg, &(msg->terminate.name));
|
||||
string = msg->terminate.name;
|
||||
@@ -829,20 +834,16 @@ static void stroke_terminate(private_stroke_interface_t *this,
|
||||
}
|
||||
|
||||
if (name)
|
||||
{ /* must be a single name */
|
||||
DBG1(DBG_CFG, "check out by single name '%s'", name);
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_name(charon->ike_sa_manager,
|
||||
name, child);
|
||||
{
|
||||
/* is a single name */
|
||||
}
|
||||
else if (pos == string + len - 2)
|
||||
{ /* must be name[] or name{} */
|
||||
{ /* is name[] or name{} */
|
||||
string[len-2] = '\0';
|
||||
DBG1(DBG_CFG, "check out by name '%s'", string);
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_name(charon->ike_sa_manager,
|
||||
string, child);
|
||||
name = string;
|
||||
}
|
||||
else
|
||||
{ /* must be name[123] or name{23} */
|
||||
{ /* is name[123] or name{23} */
|
||||
string[len-1] = '\0';
|
||||
id = atoi(pos + 1);
|
||||
if (id == 0)
|
||||
@@ -850,45 +851,51 @@ static void stroke_terminate(private_stroke_interface_t *this,
|
||||
DBG1(DBG_CFG, "error parsing string");
|
||||
return;
|
||||
}
|
||||
DBG1(DBG_CFG, "check out by id '%d'", id);
|
||||
ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
|
||||
id, child);
|
||||
}
|
||||
if (ike_sa == NULL)
|
||||
{
|
||||
DBG1(DBG_CFG, "no such IKE_SA found");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!child)
|
||||
{
|
||||
status = ike_sa->delete(ike_sa);
|
||||
}
|
||||
else
|
||||
info.out = out;
|
||||
info.level = msg->output_verbosity;
|
||||
|
||||
iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
|
||||
while (iterator->iterate(iterator, (void**)&ike_sa))
|
||||
{
|
||||
child_sa_t *child_sa;
|
||||
iterator_t *iterator = ike_sa->create_child_sa_iterator(ike_sa);
|
||||
while (iterator->iterate(iterator, (void**)&child_sa))
|
||||
iterator_t *children;
|
||||
|
||||
if (child)
|
||||
{
|
||||
if ((id && id == child_sa->get_reqid(child_sa)) ||
|
||||
(string && streq(string, child_sa->get_name(child_sa))))
|
||||
children = ike_sa->create_child_sa_iterator(ike_sa);
|
||||
while (children->iterate(children, (void**)&child_sa))
|
||||
{
|
||||
u_int32_t spi = child_sa->get_spi(child_sa, TRUE);
|
||||
protocol_id_t proto = child_sa->get_protocol(child_sa);
|
||||
|
||||
status = ike_sa->delete_child_sa(ike_sa, proto, spi);
|
||||
break;
|
||||
if ((name && streq(name, child_sa->get_name(child_sa))) ||
|
||||
(id && id == child_sa->get_reqid(child_sa)))
|
||||
{
|
||||
id = child_sa->get_reqid(child_sa);
|
||||
children->destroy(children);
|
||||
iterator->destroy(iterator);
|
||||
|
||||
charon->interfaces->terminate_child(charon->interfaces, id,
|
||||
(interface_manager_cb_t)stroke_log, &info);
|
||||
return;
|
||||
}
|
||||
}
|
||||
children->destroy(children);
|
||||
}
|
||||
iterator->destroy(iterator);
|
||||
else if ((name && streq(name, ike_sa->get_name(ike_sa))) ||
|
||||
(id && id == ike_sa->get_unique_id(ike_sa)))
|
||||
{
|
||||
id = ike_sa->get_unique_id(ike_sa);
|
||||
/* unlock manager first */
|
||||
iterator->destroy(iterator);
|
||||
|
||||
charon->interfaces->terminate_ike(charon->interfaces, id,
|
||||
(interface_manager_cb_t)stroke_log, &info);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
if (status == DESTROY_ME)
|
||||
{
|
||||
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
|
||||
ike_sa);
|
||||
return;
|
||||
}
|
||||
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
||||
iterator->destroy(iterator);
|
||||
DBG1(DBG_CFG, "no such SA found");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1528,8 +1535,8 @@ static void stroke_receive(private_stroke_interface_t *this)
|
||||
int oldstate;
|
||||
int strokefd;
|
||||
|
||||
/* drop threads capabilities */
|
||||
charon->drop_capabilities(charon, TRUE, FALSE, FALSE);
|
||||
/* drop threads capabilities, keep NET_ADMIN to query use times for status */
|
||||
charon->drop_capabilities(charon, TRUE, TRUE, FALSE);
|
||||
|
||||
/* ignore sigpipe. writing over the pipe back to the console
|
||||
* only fails if SIGPIPE is ignored. */
|
||||
|
||||
Reference in New Issue
Block a user