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

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: 
        !            17: #include "nm_service.h"
        !            18: #include "nm_creds.h"
        !            19: #include "nm_handler.h"
        !            20: 
        !            21: #include <daemon.h>
        !            22: #include <processing/jobs/callback_job.h>
        !            23: 
        !            24: typedef struct nm_backend_t nm_backend_t;
        !            25: 
        !            26: /**
        !            27:  * Data for the NetworkManager backend.
        !            28:  */
        !            29: struct nm_backend_t {
        !            30: 
        !            31:        /**
        !            32:         * NetworkManager service (VPNPlugin)
        !            33:         */
        !            34:        NMStrongswanPlugin *plugin;
        !            35: 
        !            36:        /**
        !            37:         * Glib main loop for a thread, handles DBUS calls
        !            38:         */
        !            39:        GMainLoop *loop;
        !            40: 
        !            41:        /**
        !            42:         * credential set registered at the daemon
        !            43:         */
        !            44:        nm_creds_t *creds;
        !            45: 
        !            46:        /**
        !            47:         * attribute handler registered at the daemon
        !            48:         */
        !            49:        nm_handler_t *handler;
        !            50: };
        !            51: 
        !            52: /**
        !            53:  * Global (but private) instance of the NM backend.
        !            54:  */
        !            55: static nm_backend_t *nm_backend = NULL;
        !            56: 
        !            57: /**
        !            58:  * NM plugin processing routine, creates and handles NMVpnServicePlugin
        !            59:  */
        !            60: static job_requeue_t run(nm_backend_t *this)
        !            61: {
        !            62:        this->loop = g_main_loop_new(NULL, FALSE);
        !            63:        g_main_loop_run(this->loop);
        !            64:        return JOB_REQUEUE_NONE;
        !            65: }
        !            66: 
        !            67: /**
        !            68:  * Cancel the GLib Main Event Loop
        !            69:  */
        !            70: static bool cancel(nm_backend_t *this)
        !            71: {
        !            72:        if (this->loop)
        !            73:        {
        !            74:                if (g_main_loop_is_running(this->loop))
        !            75:                {
        !            76:                        g_main_loop_quit(this->loop);
        !            77:                }
        !            78:                g_main_loop_unref(this->loop);
        !            79:        }
        !            80:        return TRUE;
        !            81: }
        !            82: 
        !            83: /**
        !            84:  * Deinitialize NetworkManager backend
        !            85:  */
        !            86: static void nm_backend_deinit()
        !            87: {
        !            88:        nm_backend_t *this = nm_backend;
        !            89: 
        !            90:        if (!this)
        !            91:        {
        !            92:                return;
        !            93:        }
        !            94:        if (this->plugin)
        !            95:        {
        !            96:                g_object_unref(this->plugin);
        !            97:        }
        !            98:        lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
        !            99:        charon->attributes->remove_handler(charon->attributes,
        !           100:                                                                           &this->handler->handler);
        !           101:        this->creds->destroy(this->creds);
        !           102:        this->handler->destroy(this->handler);
        !           103:        free(this);
        !           104: 
        !           105:        nm_backend = NULL;
        !           106: }
        !           107: 
        !           108: /**
        !           109:  * Initialize NetworkManager backend
        !           110:  */
        !           111: static bool nm_backend_init()
        !           112: {
        !           113:        nm_backend_t *this;
        !           114: 
        !           115: #if !GLIB_CHECK_VERSION(2,36,0)
        !           116:        g_type_init ();
        !           117: #endif
        !           118: 
        !           119: #if !GLIB_CHECK_VERSION(2,23,0)
        !           120:        if (!g_thread_supported())
        !           121:        {
        !           122:                g_thread_init(NULL);
        !           123:        }
        !           124: #endif
        !           125: 
        !           126:        INIT(this,
        !           127:                .creds = nm_creds_create(),
        !           128:                .handler = nm_handler_create(),
        !           129:        );
        !           130:        this->plugin = nm_strongswan_plugin_new(this->creds, this->handler);
        !           131:        nm_backend = this;
        !           132: 
        !           133:        charon->attributes->add_handler(charon->attributes, &this->handler->handler);
        !           134:        lib->credmgr->add_set(lib->credmgr, &this->creds->set);
        !           135:        if (!this->plugin)
        !           136:        {
        !           137:                DBG1(DBG_CFG, "DBUS binding failed");
        !           138:                nm_backend_deinit();
        !           139:                return FALSE;
        !           140:        }
        !           141: 
        !           142:        lib->processor->queue_job(lib->processor,
        !           143:                (job_t*)callback_job_create_with_prio((callback_job_cb_t)run, this,
        !           144:                                NULL, (callback_job_cancel_t)cancel, JOB_PRIO_CRITICAL));
        !           145:        return TRUE;
        !           146: }
        !           147: 
        !           148: /**
        !           149:  * Initialize/deinitialize NetworkManager backend
        !           150:  */
        !           151: static bool nm_backend_cb(void *plugin,
        !           152:                                                  plugin_feature_t *feature, bool reg, void *data)
        !           153: {
        !           154:        if (reg)
        !           155:        {
        !           156:                return nm_backend_init();
        !           157:        }
        !           158:        nm_backend_deinit();
        !           159:        return TRUE;
        !           160: }
        !           161: 
        !           162: /*
        !           163:  * see header file
        !           164:  */
        !           165: void nm_backend_register()
        !           166: {
        !           167:        static plugin_feature_t features[] = {
        !           168:                PLUGIN_CALLBACK((plugin_feature_callback_t)nm_backend_cb, NULL),
        !           169:                        PLUGIN_PROVIDE(CUSTOM, "NetworkManager backend"),
        !           170:                                PLUGIN_DEPENDS(CUSTOM, "libcharon"),
        !           171:                                PLUGIN_SDEPEND(PRIVKEY, KEY_RSA),
        !           172:                                PLUGIN_SDEPEND(PRIVKEY, KEY_ECDSA),
        !           173:                                PLUGIN_SDEPEND(CERT_DECODE, CERT_ANY),
        !           174:                                PLUGIN_SDEPEND(CERT_DECODE, CERT_X509),
        !           175:        };
        !           176:        lib->plugins->add_static_features(lib->plugins, "nm-backend", features,
        !           177:                                                                          countof(features), TRUE, NULL, NULL);
        !           178: }

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