diff options
author | Martin Willi <martin@revosec.ch> | 2012-10-04 15:39:26 +0200 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2012-10-24 11:43:34 +0200 |
commit | 7650dd9a4f5d551dc0aa4f222c10a3707d3a22eb (patch) | |
tree | 468a50a296c0b1df36658a79a6a19514cea616a0 /src/libcharon/plugins/lookip | |
parent | d59e6db6141518537aa2df6657d74fc4474999dd (diff) | |
download | strongswan-7650dd9a4f5d551dc0aa4f222c10a3707d3a22eb.tar.bz2 strongswan-7650dd9a4f5d551dc0aa4f222c10a3707d3a22eb.tar.xz |
Add a lookip server side UNIX socket processing LOOKUP and DUMP requests
Diffstat (limited to 'src/libcharon/plugins/lookip')
-rw-r--r-- | src/libcharon/plugins/lookip/Makefile.am | 3 | ||||
-rw-r--r-- | src/libcharon/plugins/lookip/lookip_plugin.c | 8 | ||||
-rw-r--r-- | src/libcharon/plugins/lookip/lookip_socket.c | 255 | ||||
-rw-r--r-- | src/libcharon/plugins/lookip/lookip_socket.h | 44 |
4 files changed, 309 insertions, 1 deletions
diff --git a/src/libcharon/plugins/lookip/Makefile.am b/src/libcharon/plugins/lookip/Makefile.am index 89614e473..450995c9c 100644 --- a/src/libcharon/plugins/lookip/Makefile.am +++ b/src/libcharon/plugins/lookip/Makefile.am @@ -12,7 +12,8 @@ plugin_LTLIBRARIES = libstrongswan-lookip.la endif libstrongswan_lookip_la_SOURCES = lookip_plugin.h lookip_plugin.c \ - lookip_listener.h lookip_listener.c lookip_msg.h + lookip_listener.h lookip_listener.c lookip_msg.h \ + lookip_socket.h lookip_socket.c libstrongswan_lookip_la_LDFLAGS = -module -avoid-version diff --git a/src/libcharon/plugins/lookip/lookip_plugin.c b/src/libcharon/plugins/lookip/lookip_plugin.c index 73fa6098d..360864849 100644 --- a/src/libcharon/plugins/lookip/lookip_plugin.c +++ b/src/libcharon/plugins/lookip/lookip_plugin.c @@ -16,6 +16,7 @@ #include "lookip_plugin.h" #include "lookip_listener.h" +#include "lookip_socket.h" #include <daemon.h> @@ -35,6 +36,11 @@ struct private_lookip_plugin_t { * Listener collecting virtual IP assignements */ lookip_listener_t *listener; + + /** + * UNIX socket to serve client queries + */ + lookip_socket_t *socket; }; METHOD(plugin_t, get_name, char*, @@ -46,6 +52,7 @@ METHOD(plugin_t, get_name, char*, METHOD(plugin_t, destroy, void, private_lookip_plugin_t *this) { + this->socket->destroy(this->socket); charon->bus->remove_listener(charon->bus, &this->listener->listener); this->listener->destroy(this->listener); free(this); @@ -70,6 +77,7 @@ plugin_t *lookip_plugin_create() ); charon->bus->add_listener(charon->bus, &this->listener->listener); + this->socket = lookip_socket_create(this->listener); return &this->public.plugin; } diff --git a/src/libcharon/plugins/lookip/lookip_socket.c b/src/libcharon/plugins/lookip/lookip_socket.c new file mode 100644 index 000000000..f53e04a33 --- /dev/null +++ b/src/libcharon/plugins/lookip/lookip_socket.c @@ -0,0 +1,255 @@ +/* + * Copyright (C) 2012 Martin Willi + * Copyright (C) 2012 revosec AG + * + * 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. + */ + +#include "lookip_socket.h" + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <errno.h> + +#include <daemon.h> +#include <threading/thread.h> +#include <processing/jobs/callback_job.h> + +#include "lookip_msg.h" + +typedef struct private_lookip_socket_t private_lookip_socket_t; + +/** + * Private data of an lookip_socket_t object. + */ +struct private_lookip_socket_t { + + /** + * Public lookip_socket_t interface. + */ + lookip_socket_t public; + + /** + * lookip + */ + lookip_listener_t *listener; + + /** + * lookip unix socket file descriptor + */ + int socket; +}; + +/** + * Open lookip unix socket + */ +static bool open_socket(private_lookip_socket_t *this) +{ + struct sockaddr_un addr; + mode_t old; + + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, LOOKIP_SOCKET); + + this->socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); + if (this->socket == -1) + { + DBG1(DBG_CFG, "creating lookip socket failed"); + return FALSE; + } + unlink(addr.sun_path); + old = umask(~(S_IRWXU | S_IRWXG)); + if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) + { + DBG1(DBG_CFG, "binding lookip socket failed: %s", strerror(errno)); + close(this->socket); + return FALSE; + } + umask(old); + if (chown(addr.sun_path, charon->caps->get_uid(charon->caps), + charon->caps->get_gid(charon->caps)) != 0) + { + DBG1(DBG_CFG, "changing lookip socket permissions failed: %s", + strerror(errno)); + } + if (listen(this->socket, 10) < 0) + { + DBG1(DBG_CFG, "listening on lookip socket failed: %s", strerror(errno)); + close(this->socket); + unlink(addr.sun_path); + return FALSE; + } + return TRUE; +} + +/** + * Listener callback data + */ +typedef struct { + /* FD to write to */ + int fd; + /* message type to send */ + int type; +} cb_data_t; + +/** + * Callback function for listener + */ +static bool listener_cb(cb_data_t *data, bool up, host_t *vip, + host_t *other, identification_t *id, char *name) +{ + lookip_response_t resp = { + .type = data->type, + }; + + snprintf(resp.vip, sizeof(resp.vip), "%H", vip); + snprintf(resp.ip, sizeof(resp.ip), "%H", other); + snprintf(resp.id, sizeof(resp.id), "%Y", id); + snprintf(resp.name, sizeof(resp.name), "%s", name); + + switch (send(data->fd, &resp, sizeof(resp), 0)) + { + case sizeof(resp): + return TRUE; + case 0: + /* client disconnected, adios */ + return FALSE; + default: + DBG1(DBG_CFG, "sending lookip response failed: %s", strerror(errno)); + return FALSE; + } +} + +/** + * Perform a entry lookup + */ +static void query(private_lookip_socket_t *this, int fd, lookip_request_t *req) +{ + cb_data_t data = { + .fd = fd, + .type = LOOKIP_ENTRY, + }; + host_t *vip = NULL; + + if (req) + { /* lookup */ + req->vip[sizeof(req->vip) - 1] = 0; + vip = host_create_from_string(req->vip, 0); + if (vip) + { + this->listener->lookup(this->listener, vip, + (void*)listener_cb, &data); + vip->destroy(vip); + } + } + else + { /* dump */ + this->listener->lookup(this->listener, NULL, + (void*)listener_cb, &data); + } +} + +/** + * Accept client connections, dispatch + */ +static job_requeue_t receive(private_lookip_socket_t *this) +{ + struct sockaddr_un addr; + int fd, len = sizeof(addr); + lookip_request_t req; + bool oldstate; + + oldstate = thread_cancelability(TRUE); + fd = accept(this->socket, (struct sockaddr*)&addr, &len); + thread_cancelability(oldstate); + + if (fd != -1) + { + while (TRUE) + { + oldstate = thread_cancelability(TRUE); + len = recv(fd, &req, sizeof(req), 0); + thread_cancelability(oldstate); + + if (len == sizeof(req)) + { + switch (req.type) + { + case LOOKIP_LOOKUP: + query(this, fd, &req); + continue; + case LOOKIP_DUMP: + query(this, fd, NULL); + continue; + case LOOKIP_END: + break; + default: + DBG1(DBG_CFG, "received unknown lookip command"); + break; + } + } + else + { + if (len != 0) + { + DBG1(DBG_CFG, "receiving lookip request failed: %s", + strerror(errno)); + } + break; + } + break; + } + close(fd); + } + else + { + DBG1(DBG_CFG, "accepting lookip connection failed: %s", + strerror(errno)); + } + return JOB_REQUEUE_FAIR; +} + +METHOD(lookip_socket_t, destroy, void, + private_lookip_socket_t *this) +{ + close(this->socket); + free(this); +} + +/** + * See header + */ +lookip_socket_t *lookip_socket_create(lookip_listener_t *listener) +{ + private_lookip_socket_t *this; + + INIT(this, + .public = { + .destroy = _destroy, + }, + .listener = listener, + ); + + if (!open_socket(this)) + { + free(this); + return NULL; + } + + lib->processor->queue_job(lib->processor, + (job_t*)callback_job_create_with_prio((callback_job_cb_t)receive, this, + NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL)); + + return &this->public; +} diff --git a/src/libcharon/plugins/lookip/lookip_socket.h b/src/libcharon/plugins/lookip/lookip_socket.h new file mode 100644 index 000000000..c1c50246d --- /dev/null +++ b/src/libcharon/plugins/lookip/lookip_socket.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2012 Martin Willi + * Copyright (C) 2012 revosec AG + * + * 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. + */ + +/** + * @defgroup lookip_socket lookip_socket + * @{ @ingroup lookip + */ + +#ifndef LOOKIP_SOCKET_H_ +#define LOOKIP_SOCKET_H_ + +#include "lookip_listener.h" + +typedef struct lookip_socket_t lookip_socket_t; + +/** + * Lookip plugin UNIX query socket. + */ +struct lookip_socket_t { + + /** + * Destroy a lookip_socket_t. + */ + void (*destroy)(lookip_socket_t *this); +}; + +/** + * Create a lookip_socket instance. + */ +lookip_socket_t *lookip_socket_create(lookip_listener_t *listener); + +#endif /** LOOKIP_SOCKET_H_ @}*/ |