Annotation of embedaddon/strongswan/src/charon-nm/nm/nm_backend.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Copyright (C) 2012 Tobias Brunner
                      3:  * Copyright (C) 2008-2009 Martin Willi
                      4:  * HSR Hochschule fuer Technik Rapperswil
                      5:  *
                      6:  * This program is free software; you can redistribute it and/or modify it
                      7:  * under the terms of the GNU General Public License as published by the
                      8:  * Free Software Foundation; either version 2 of the License, or (at your
                      9:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                     10:  *
                     11:  * This program is distributed in the hope that it will be useful, but
                     12:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     13:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     14:  * for more details.
                     15:  */
                     16: 
1.1.1.2 ! misho      17: #include <sys/types.h>
        !            18: #include <unistd.h>
        !            19: 
1.1       misho      20: #include "nm_service.h"
                     21: #include "nm_creds.h"
                     22: #include "nm_handler.h"
                     23: 
                     24: #include <daemon.h>
                     25: #include <processing/jobs/callback_job.h>
                     26: 
                     27: typedef struct nm_backend_t nm_backend_t;
                     28: 
                     29: /**
                     30:  * Data for the NetworkManager backend.
                     31:  */
                     32: struct nm_backend_t {
                     33: 
                     34:        /**
                     35:         * NetworkManager service (VPNPlugin)
                     36:         */
                     37:        NMStrongswanPlugin *plugin;
                     38: 
                     39:        /**
                     40:         * Glib main loop for a thread, handles DBUS calls
                     41:         */
                     42:        GMainLoop *loop;
                     43: 
                     44:        /**
                     45:         * credential set registered at the daemon
                     46:         */
                     47:        nm_creds_t *creds;
                     48: 
                     49:        /**
                     50:         * attribute handler registered at the daemon
                     51:         */
                     52:        nm_handler_t *handler;
                     53: };
                     54: 
                     55: /**
                     56:  * Global (but private) instance of the NM backend.
                     57:  */
                     58: static nm_backend_t *nm_backend = NULL;
                     59: 
                     60: /**
1.1.1.2 ! misho      61:  * Terminate the daemon if signaled by NM
        !            62:  */
        !            63: static void terminate(void *plugin, void *arg)
        !            64: {
        !            65:        kill(getpid(), SIGTERM);
        !            66: }
        !            67: 
        !            68: /**
1.1       misho      69:  * NM plugin processing routine, creates and handles NMVpnServicePlugin
                     70:  */
                     71: static job_requeue_t run(nm_backend_t *this)
                     72: {
                     73:        this->loop = g_main_loop_new(NULL, FALSE);
1.1.1.2 ! misho      74:        g_signal_connect(this->plugin, "quit", G_CALLBACK(terminate), NULL);
1.1       misho      75:        g_main_loop_run(this->loop);
                     76:        return JOB_REQUEUE_NONE;
                     77: }
                     78: 
                     79: /**
                     80:  * Cancel the GLib Main Event Loop
                     81:  */
                     82: static bool cancel(nm_backend_t *this)
                     83: {
                     84:        if (this->loop)
                     85:        {
                     86:                if (g_main_loop_is_running(this->loop))
                     87:                {
                     88:                        g_main_loop_quit(this->loop);
                     89:                }
                     90:                g_main_loop_unref(this->loop);
                     91:        }
                     92:        return TRUE;
                     93: }
                     94: 
                     95: /**
                     96:  * Deinitialize NetworkManager backend
                     97:  */
                     98: static void nm_backend_deinit()
                     99: {
                    100:        nm_backend_t *this = nm_backend;
                    101: 
                    102:        if (!this)
                    103:        {
                    104:                return;
                    105:        }
                    106:        if (this->plugin)
                    107:        {
                    108:                g_object_unref(this->plugin);
                    109:        }
                    110:        lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
                    111:        charon->attributes->remove_handler(charon->attributes,
                    112:                                                                           &this->handler->handler);
                    113:        this->creds->destroy(this->creds);
                    114:        this->handler->destroy(this->handler);
                    115:        free(this);
                    116: 
                    117:        nm_backend = NULL;
                    118: }
                    119: 
                    120: /**
                    121:  * Initialize NetworkManager backend
                    122:  */
                    123: static bool nm_backend_init()
                    124: {
                    125:        nm_backend_t *this;
                    126: 
                    127: #if !GLIB_CHECK_VERSION(2,36,0)
                    128:        g_type_init ();
                    129: #endif
                    130: 
                    131: #if !GLIB_CHECK_VERSION(2,23,0)
                    132:        if (!g_thread_supported())
                    133:        {
                    134:                g_thread_init(NULL);
                    135:        }
                    136: #endif
                    137: 
                    138:        INIT(this,
                    139:                .creds = nm_creds_create(),
                    140:                .handler = nm_handler_create(),
                    141:        );
                    142:        this->plugin = nm_strongswan_plugin_new(this->creds, this->handler);
                    143:        nm_backend = this;
                    144: 
                    145:        charon->attributes->add_handler(charon->attributes, &this->handler->handler);
                    146:        lib->credmgr->add_set(lib->credmgr, &this->creds->set);
                    147:        if (!this->plugin)
                    148:        {
                    149:                DBG1(DBG_CFG, "DBUS binding failed");
                    150:                nm_backend_deinit();
                    151:                return FALSE;
                    152:        }
                    153: 
                    154:        lib->processor->queue_job(lib->processor,
                    155:                (job_t*)callback_job_create_with_prio((callback_job_cb_t)run, this,
                    156:                                NULL, (callback_job_cancel_t)cancel, JOB_PRIO_CRITICAL));
                    157:        return TRUE;
                    158: }
                    159: 
                    160: /**
                    161:  * Initialize/deinitialize NetworkManager backend
                    162:  */
                    163: static bool nm_backend_cb(void *plugin,
                    164:                                                  plugin_feature_t *feature, bool reg, void *data)
                    165: {
                    166:        if (reg)
                    167:        {
                    168:                return nm_backend_init();
                    169:        }
                    170:        nm_backend_deinit();
                    171:        return TRUE;
                    172: }
                    173: 
                    174: /*
                    175:  * see header file
                    176:  */
                    177: void nm_backend_register()
                    178: {
                    179:        static plugin_feature_t features[] = {
                    180:                PLUGIN_CALLBACK((plugin_feature_callback_t)nm_backend_cb, NULL),
                    181:                        PLUGIN_PROVIDE(CUSTOM, "NetworkManager backend"),
                    182:                                PLUGIN_DEPENDS(CUSTOM, "libcharon"),
                    183:                                PLUGIN_SDEPEND(PRIVKEY, KEY_RSA),
                    184:                                PLUGIN_SDEPEND(PRIVKEY, KEY_ECDSA),
                    185:                                PLUGIN_SDEPEND(CERT_DECODE, CERT_ANY),
                    186:                                PLUGIN_SDEPEND(CERT_DECODE, CERT_X509),
                    187:        };
                    188:        lib->plugins->add_static_features(lib->plugins, "nm-backend", features,
                    189:                                                                          countof(features), TRUE, NULL, NULL);
                    190: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>