Moving attr-sql plugin from libstrongswan to libhydra.
[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 <library.h>
17 #include <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
46 /**
47 * Implementation of plugin_t.destroy
48 */
49 static void destroy(private_attr_sql_plugin_t *this)
50 {
51 lib->attributes->remove_provider(lib->attributes, &this->attribute->provider);
52 this->attribute->destroy(this->attribute);
53 this->db->destroy(this->db);
54 free(this);
55 }
56
57 /*
58 * see header file
59 */
60 plugin_t *attr_sql_plugin_create()
61 {
62 char *uri;
63 private_attr_sql_plugin_t *this;
64
65 uri = lib->settings->get_str(lib->settings, "libstrongswan.plugins.attr-sql.database", NULL);
66 if (!uri)
67 {
68 DBG1("attr-sql plugin: database URI not set");
69 return NULL;
70 }
71
72 this = malloc_thing(private_attr_sql_plugin_t);
73
74 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
75
76 this->db = lib->db->create(lib->db, uri);
77 if (!this->db)
78 {
79 DBG1("attr-sql plugin failed to connect to database");
80 free(this);
81 return NULL;
82 }
83 this->attribute = sql_attribute_create(this->db);
84 lib->attributes->add_provider(lib->attributes, &this->attribute->provider);
85
86 return &this->public.plugin;
87 }
88