]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libhydra/plugins/attr_sql/attr_sql_plugin.c
Moved debug.[ch] to utils folder
[thirdparty/strongswan.git] / src / libhydra / plugins / attr_sql / attr_sql_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <hydra.h>
17 #include <utils/debug.h>
18
19 #include "attr_sql_plugin.h"
20 #include "sql_attribute.h"
21
22 typedef struct private_attr_sql_plugin_t private_attr_sql_plugin_t;
23
24 /**
25 * private data of attr_sql plugin
26 */
27 struct private_attr_sql_plugin_t {
28
29 /**
30 * implements plugin interface
31 */
32 attr_sql_plugin_t public;
33
34 /**
35 * database connection instance
36 */
37 database_t *db;
38
39 /**
40 * configuration attributes
41 */
42 sql_attribute_t *attribute;
43 };
44
45 METHOD(plugin_t, get_name, char*,
46 private_attr_sql_plugin_t *this)
47 {
48 return "attr-sql";
49 }
50
51 METHOD(plugin_t, destroy, void,
52 private_attr_sql_plugin_t *this)
53 {
54 hydra->attributes->remove_provider(hydra->attributes, &this->attribute->provider);
55 this->attribute->destroy(this->attribute);
56 this->db->destroy(this->db);
57 free(this);
58 }
59
60 /*
61 * see header file
62 */
63 plugin_t *attr_sql_plugin_create()
64 {
65 private_attr_sql_plugin_t *this;
66 char *uri;
67
68 uri = lib->settings->get_str(lib->settings, "libhydra.plugins.attr-sql.database",
69 NULL);
70 if (!uri)
71 {
72 DBG1(DBG_CFG, "attr-sql plugin: database URI not set");
73 return NULL;
74 }
75
76 INIT(this,
77 .public = {
78 .plugin = {
79 .get_name = _get_name,
80 .reload = (void*)return_false,
81 .destroy = _destroy,
82 },
83 },
84 .db = lib->db->create(lib->db, uri),
85 );
86
87 if (!this->db)
88 {
89 DBG1(DBG_CFG, "attr-sql plugin failed to connect to database");
90 free(this);
91 return NULL;
92 }
93 this->attribute = sql_attribute_create(this->db);
94 hydra->attributes->add_provider(hydra->attributes, &this->attribute->provider);
95
96 return &this->public.plugin;
97 }
98