Annotation of embedaddon/strongswan/src/libcharon/plugins/attr_sql/attr_sql_plugin.c, revision 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 <daemon.h>
! 18: #include <utils/debug.h>
! 19: #include <plugins/plugin_feature.h>
! 20:
! 21: #include "attr_sql_plugin.h"
! 22: #include "attr_sql_provider.h"
! 23:
! 24: typedef struct private_attr_sql_plugin_t private_attr_sql_plugin_t;
! 25:
! 26: /**
! 27: * private data of attr_sql plugin
! 28: */
! 29: struct private_attr_sql_plugin_t {
! 30:
! 31: /**
! 32: * implements plugin interface
! 33: */
! 34: attr_sql_plugin_t public;
! 35:
! 36: /**
! 37: * database connection instance
! 38: */
! 39: database_t *db;
! 40:
! 41: /**
! 42: * configuration attributes
! 43: */
! 44: attr_sql_provider_t *attribute;
! 45: };
! 46:
! 47: METHOD(plugin_t, get_name, char*,
! 48: private_attr_sql_plugin_t *this)
! 49: {
! 50: return "attr-sql";
! 51: }
! 52:
! 53: /**
! 54: * Connect to database
! 55: */
! 56: static bool open_database(private_attr_sql_plugin_t *this,
! 57: plugin_feature_t *feature, bool reg, void *cb_data)
! 58: {
! 59: if (reg)
! 60: {
! 61: char *uri;
! 62:
! 63: uri = lib->settings->get_str(lib->settings,
! 64: "%s.plugins.attr-sql.database", NULL, lib->ns);
! 65: if (!uri)
! 66: {
! 67: DBG1(DBG_CFG, "attr-sql plugin: database URI not set");
! 68: return FALSE;
! 69: }
! 70:
! 71: this->db = lib->db->create(lib->db, uri);
! 72: if (!this->db)
! 73: {
! 74: DBG1(DBG_CFG, "attr-sql plugin failed to connect to database");
! 75: return FALSE;
! 76: }
! 77: this->attribute = attr_sql_provider_create(this->db);
! 78: charon->attributes->add_provider(charon->attributes,
! 79: &this->attribute->provider);
! 80: }
! 81: else
! 82: {
! 83: charon->attributes->remove_provider(charon->attributes,
! 84: &this->attribute->provider);
! 85: this->attribute->destroy(this->attribute);
! 86: this->db->destroy(this->db);
! 87: }
! 88: return TRUE;
! 89: }
! 90:
! 91: METHOD(plugin_t, get_features, int,
! 92: private_attr_sql_plugin_t *this, plugin_feature_t *features[])
! 93: {
! 94: static plugin_feature_t f[] = {
! 95: PLUGIN_CALLBACK((plugin_feature_callback_t)open_database, NULL),
! 96: PLUGIN_PROVIDE(CUSTOM, "attr-sql"),
! 97: PLUGIN_DEPENDS(DATABASE, DB_ANY),
! 98: };
! 99: *features = f;
! 100: return countof(f);
! 101: }
! 102:
! 103: METHOD(plugin_t, destroy, void,
! 104: private_attr_sql_plugin_t *this)
! 105: {
! 106: free(this);
! 107: }
! 108:
! 109: /*
! 110: * see header file
! 111: */
! 112: plugin_t *attr_sql_plugin_create()
! 113: {
! 114: private_attr_sql_plugin_t *this;
! 115:
! 116: INIT(this,
! 117: .public = {
! 118: .plugin = {
! 119: .get_name = _get_name,
! 120: .get_features = _get_features,
! 121: .destroy = _destroy,
! 122: },
! 123: },
! 124: );
! 125: lib->settings->add_fallback(lib->settings, "%s.plugins.attr-sql",
! 126: "libhydra.plugins.attr-sql", lib->ns);
! 127:
! 128: return &this->public.plugin;
! 129: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>