diff options
Diffstat (limited to 'src/authorization/anastasis_authorization_plugin.c')
-rw-r--r-- | src/authorization/anastasis_authorization_plugin.c | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/src/authorization/anastasis_authorization_plugin.c b/src/authorization/anastasis_authorization_plugin.c new file mode 100644 index 0000000..7874594 --- /dev/null +++ b/src/authorization/anastasis_authorization_plugin.c | |||
@@ -0,0 +1,239 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | Copyright (C) 2015, 2016, 2021 Taler Systems SA | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU Lesser General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file anastasis_authorization_plugin.c | ||
18 | * @brief Logic to load database plugin | ||
19 | * @author Christian Grothoff | ||
20 | * @author Dominik Meister | ||
21 | */ | ||
22 | #include "platform.h" | ||
23 | #include "anastasis_authorization_plugin.h" | ||
24 | #include <ltdl.h> | ||
25 | |||
26 | |||
27 | /** | ||
28 | * Head of linked list for all loaded plugins | ||
29 | */ | ||
30 | static struct AuthPlugin *ap_head; | ||
31 | |||
32 | /** | ||
33 | * Tail ofinked list for all loaded plugins | ||
34 | */ | ||
35 | static struct AuthPlugin *ap_tail; | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Authentication plugin which is used to verify code based authentication | ||
40 | * like SMS, E-Mail. | ||
41 | */ | ||
42 | struct AuthPlugin | ||
43 | { | ||
44 | /** | ||
45 | * Kept in a DLL. | ||
46 | */ | ||
47 | struct AuthPlugin *next; | ||
48 | |||
49 | /** | ||
50 | * Kept in a DLL. | ||
51 | */ | ||
52 | struct AuthPlugin *prev; | ||
53 | |||
54 | /** | ||
55 | * Actual plugin handle. | ||
56 | */ | ||
57 | struct ANASTASIS_AuthorizationPlugin *authorization; | ||
58 | |||
59 | /** | ||
60 | * I.e. "sms", "phone". | ||
61 | */ | ||
62 | char *name; | ||
63 | |||
64 | /** | ||
65 | * Name of the shared object providing the plugin logic. | ||
66 | */ | ||
67 | char *lib_name; | ||
68 | |||
69 | /** | ||
70 | * Cost of using this plugin. | ||
71 | */ | ||
72 | struct TALER_Amount cost; | ||
73 | }; | ||
74 | |||
75 | |||
76 | struct ANASTASIS_AuthorizationPlugin * | ||
77 | ANASTASIS_authorization_plugin_load ( | ||
78 | const char *method, | ||
79 | const struct GNUNET_CONFIGURATION_Handle *AH_cfg, | ||
80 | struct TALER_Amount *cost) | ||
81 | { | ||
82 | struct ANASTASIS_AuthorizationPlugin *authorization; | ||
83 | char *lib_name; | ||
84 | char *sec_name; | ||
85 | struct AuthPlugin *ap; | ||
86 | char *currency; | ||
87 | |||
88 | for (ap = ap_head; NULL != ap; ap = ap->next) | ||
89 | if (0 == strcmp (method, | ||
90 | ap->name)) | ||
91 | { | ||
92 | *cost = ap->cost; | ||
93 | return ap->authorization; | ||
94 | } | ||
95 | if (GNUNET_OK != | ||
96 | TALER_config_get_currency (AH_cfg, | ||
97 | ¤cy)) | ||
98 | return NULL; | ||
99 | ap = GNUNET_new (struct AuthPlugin); | ||
100 | GNUNET_asprintf (&sec_name, | ||
101 | "authorization-%s", | ||
102 | method); | ||
103 | if (GNUNET_OK != | ||
104 | TALER_config_get_amount (AH_cfg, | ||
105 | sec_name, | ||
106 | "COST", | ||
107 | &ap->cost)) | ||
108 | { | ||
109 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, | ||
110 | sec_name, | ||
111 | "COST"); | ||
112 | GNUNET_free (sec_name); | ||
113 | GNUNET_free (currency); | ||
114 | GNUNET_free (ap); | ||
115 | return NULL; | ||
116 | } | ||
117 | |||
118 | if (0 != | ||
119 | strcasecmp (currency, | ||
120 | ap->cost.currency)) | ||
121 | { | ||
122 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, | ||
123 | sec_name, | ||
124 | "COST", | ||
125 | "currency mismatch"); | ||
126 | GNUNET_free (currency); | ||
127 | GNUNET_free (sec_name); | ||
128 | GNUNET_free (ap); | ||
129 | return NULL; | ||
130 | } | ||
131 | GNUNET_free (currency); | ||
132 | GNUNET_free (sec_name); | ||
133 | GNUNET_asprintf (&lib_name, | ||
134 | "libanastasis_plugin_authorization_%s", | ||
135 | method); | ||
136 | authorization = GNUNET_PLUGIN_load (lib_name, | ||
137 | (void *) AH_cfg); | ||
138 | if (NULL == authorization) | ||
139 | { | ||
140 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
141 | "Authentication method `%s' not supported\n", | ||
142 | method); | ||
143 | GNUNET_free (lib_name); | ||
144 | GNUNET_free (ap); | ||
145 | return NULL; | ||
146 | } | ||
147 | ap->name = GNUNET_strdup (method); | ||
148 | ap->lib_name = lib_name; | ||
149 | ap->authorization = authorization; | ||
150 | GNUNET_CONTAINER_DLL_insert (ap_head, | ||
151 | ap_tail, | ||
152 | ap); | ||
153 | *cost = ap->cost; | ||
154 | return authorization; | ||
155 | } | ||
156 | |||
157 | |||
158 | void | ||
159 | ANASTASIS_authorization_plugin_shutdown (void) | ||
160 | { | ||
161 | struct AuthPlugin *ap; | ||
162 | |||
163 | while (NULL != (ap = ap_head)) | ||
164 | { | ||
165 | GNUNET_CONTAINER_DLL_remove (ap_head, | ||
166 | ap_tail, | ||
167 | ap); | ||
168 | GNUNET_PLUGIN_unload (ap->lib_name, | ||
169 | ap->authorization); | ||
170 | GNUNET_free (ap->lib_name); | ||
171 | GNUNET_free (ap->name); | ||
172 | GNUNET_free (ap); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Libtool search path before we started. | ||
179 | */ | ||
180 | static char *old_dlsearchpath; | ||
181 | |||
182 | |||
183 | /** | ||
184 | * Setup libtool paths. | ||
185 | */ | ||
186 | void __attribute__ ((constructor)) | ||
187 | anastasis_authorization_plugin_init (void) | ||
188 | { | ||
189 | int err; | ||
190 | const char *opath; | ||
191 | char *path; | ||
192 | char *cpath; | ||
193 | |||
194 | err = lt_dlinit (); | ||
195 | if (err > 0) | ||
196 | { | ||
197 | fprintf (stderr, | ||
198 | _ ("Initialization of plugin mechanism failed: %s!\n"), | ||
199 | lt_dlerror ()); | ||
200 | return; | ||
201 | } | ||
202 | opath = lt_dlgetsearchpath (); | ||
203 | if (NULL != opath) | ||
204 | old_dlsearchpath = GNUNET_strdup (opath); | ||
205 | path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); | ||
206 | if (NULL != path) | ||
207 | { | ||
208 | if (NULL != opath) | ||
209 | { | ||
210 | GNUNET_asprintf (&cpath, "%s:%s", opath, path); | ||
211 | lt_dlsetsearchpath (cpath); | ||
212 | GNUNET_free (path); | ||
213 | GNUNET_free (cpath); | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | lt_dlsetsearchpath (path); | ||
218 | GNUNET_free (path); | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | |||
224 | /** | ||
225 | * Shutdown libtool. | ||
226 | */ | ||
227 | void __attribute__ ((destructor)) | ||
228 | anastasis_authorization_plugin_fini (void) | ||
229 | { | ||
230 | lt_dlsetsearchpath (old_dlsearchpath); | ||
231 | if (NULL != old_dlsearchpath) | ||
232 | { | ||
233 | GNUNET_free (old_dlsearchpath); | ||
234 | } | ||
235 | lt_dlexit (); | ||
236 | } | ||
237 | |||
238 | |||
239 | /* end of anastasis_authorization_plugin.c */ | ||