Annotation of embedaddon/strongswan/src/libcharon/plugins/sql/sql_plugin.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2013 Tobias Brunner
                      3:  * Copyright (C) 2008 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 "sql_plugin.h"
                     18: 
                     19: #include <daemon.h>
                     20: #include <plugins/plugin_feature.h>
                     21: 
                     22: #include "sql_config.h"
                     23: #include "sql_cred.h"
                     24: #include "sql_logger.h"
                     25: 
                     26: typedef struct private_sql_plugin_t private_sql_plugin_t;
                     27: 
                     28: /**
                     29:  * private data of sql plugin
                     30:  */
                     31: struct private_sql_plugin_t {
                     32: 
                     33:        /**
                     34:         * implements plugin interface
                     35:         */
                     36:        sql_plugin_t public;
                     37: 
                     38:        /**
                     39:         * database connection instance
                     40:         */
                     41:        database_t *db;
                     42: 
                     43:        /**
                     44:         * configuration backend
                     45:         */
                     46:        sql_config_t *config;
                     47: 
                     48:        /**
                     49:         * credential set
                     50:         */
                     51:        sql_cred_t *cred;
                     52: 
                     53:        /**
                     54:         * bus listener/logger
                     55:         */
                     56:        sql_logger_t *logger;
                     57: };
                     58: 
                     59: METHOD(plugin_t, get_name, char*,
                     60:        private_sql_plugin_t *this)
                     61: {
                     62:        return "sql";
                     63: }
                     64: 
                     65: /**
                     66:  * Connect to database
                     67:  */
                     68: static bool open_database(private_sql_plugin_t *this,
                     69:                                                  plugin_feature_t *feature, bool reg, void *cb_data)
                     70: {
                     71:        if (reg)
                     72:        {
                     73:                char *uri;
                     74: 
                     75:                uri = lib->settings->get_str(lib->settings, "%s.plugins.sql.database",
                     76:                                                                         NULL, lib->ns);
                     77:                if (!uri)
                     78:                {
                     79:                        DBG1(DBG_CFG, "sql plugin: database URI not set");
                     80:                        return FALSE;
                     81:                }
                     82: 
                     83:                this->db = lib->db->create(lib->db, uri);
                     84:                if (!this->db)
                     85:                {
                     86:                        DBG1(DBG_CFG, "sql plugin failed to connect to database");
                     87:                        return FALSE;
                     88:                }
                     89:                this->config = sql_config_create(this->db);
                     90:                this->cred = sql_cred_create(this->db);
                     91:                this->logger = sql_logger_create(this->db);
                     92: 
                     93:                charon->backends->add_backend(charon->backends, &this->config->backend);
                     94:                lib->credmgr->add_set(lib->credmgr, &this->cred->set);
                     95:                charon->bus->add_logger(charon->bus, &this->logger->logger);
                     96:        }
                     97:        else
                     98:        {
                     99:                charon->backends->remove_backend(charon->backends,
                    100:                                                                                 &this->config->backend);
                    101:                lib->credmgr->remove_set(lib->credmgr, &this->cred->set);
                    102:                charon->bus->remove_logger(charon->bus, &this->logger->logger);
                    103:                this->config->destroy(this->config);
                    104:                this->cred->destroy(this->cred);
                    105:                this->logger->destroy(this->logger);
                    106:                this->db->destroy(this->db);
                    107:        }
                    108:        return TRUE;
                    109: }
                    110: 
                    111: METHOD(plugin_t, get_features, int,
                    112:        private_sql_plugin_t *this, plugin_feature_t *features[])
                    113: {
                    114:        static plugin_feature_t f[] = {
                    115:                PLUGIN_CALLBACK((plugin_feature_callback_t)open_database, NULL),
                    116:                        PLUGIN_PROVIDE(CUSTOM, "sql"),
                    117:                                PLUGIN_DEPENDS(DATABASE, DB_ANY),
                    118:        };
                    119:        *features = f;
                    120:        return countof(f);
                    121: }
                    122: 
                    123: METHOD(plugin_t, destroy, void,
                    124:        private_sql_plugin_t *this)
                    125: {
                    126:        free(this);
                    127: }
                    128: 
                    129: /*
                    130:  * see header file
                    131:  */
                    132: plugin_t *sql_plugin_create()
                    133: {
                    134:        private_sql_plugin_t *this;
                    135: 
                    136:        INIT(this,
                    137:                .public = {
                    138:                        .plugin = {
                    139:                                .get_name = _get_name,
                    140:                                .get_features = _get_features,
                    141:                                .destroy = _destroy,
                    142:                        },
                    143:                },
                    144:        );
                    145: 
                    146:        return &this->public.plugin;
                    147: }

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