summaryrefslogtreecommitdiff
path: root/src/authorization/anastasis_authorization_plugin.c
blob: 787459470cb2e1b15027f69436275bab7d6b301e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
  This file is part of TALER
  Copyright (C) 2015, 2016, 2021 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file anastasis_authorization_plugin.c
 * @brief Logic to load database plugin
 * @author Christian Grothoff
 * @author Dominik Meister
 */
#include "platform.h"
#include "anastasis_authorization_plugin.h"
#include <ltdl.h>


/**
 * Head of linked list for all loaded plugins
 */
static struct AuthPlugin *ap_head;

/**
 * Tail ofinked list for all loaded plugins
 */
static struct AuthPlugin *ap_tail;


/**
 * Authentication plugin which is used to verify code based authentication
 * like SMS, E-Mail.
 */
struct AuthPlugin
{
  /**
   * Kept in a DLL.
   */
  struct AuthPlugin *next;

  /**
   * Kept in a DLL.
   */
  struct AuthPlugin *prev;

  /**
   * Actual plugin handle.
   */
  struct ANASTASIS_AuthorizationPlugin *authorization;

  /**
   * I.e. "sms", "phone".
   */
  char *name;

  /**
   * Name of the shared object providing the plugin logic.
   */
  char *lib_name;

  /**
   * Cost of using this plugin.
   */
  struct TALER_Amount cost;
};


struct ANASTASIS_AuthorizationPlugin *
ANASTASIS_authorization_plugin_load (
  const char *method,
  const struct GNUNET_CONFIGURATION_Handle *AH_cfg,
  struct TALER_Amount *cost)
{
  struct ANASTASIS_AuthorizationPlugin *authorization;
  char *lib_name;
  char *sec_name;
  struct AuthPlugin *ap;
  char *currency;

  for (ap = ap_head; NULL != ap; ap = ap->next)
    if (0 == strcmp (method,
                     ap->name))
    {
      *cost = ap->cost;
      return ap->authorization;
    }
  if (GNUNET_OK !=
      TALER_config_get_currency (AH_cfg,
                                 &currency))
    return NULL;
  ap = GNUNET_new (struct AuthPlugin);
  GNUNET_asprintf (&sec_name,
                   "authorization-%s",
                   method);
  if (GNUNET_OK !=
      TALER_config_get_amount (AH_cfg,
                               sec_name,
                               "COST",
                               &ap->cost))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
                               sec_name,
                               "COST");
    GNUNET_free (sec_name);
    GNUNET_free (currency);
    GNUNET_free (ap);
    return NULL;
  }

  if (0 !=
      strcasecmp (currency,
                  ap->cost.currency))
  {
    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                               sec_name,
                               "COST",
                               "currency mismatch");
    GNUNET_free (currency);
    GNUNET_free (sec_name);
    GNUNET_free (ap);
    return NULL;
  }
  GNUNET_free (currency);
  GNUNET_free (sec_name);
  GNUNET_asprintf (&lib_name,
                   "libanastasis_plugin_authorization_%s",
                   method);
  authorization = GNUNET_PLUGIN_load (lib_name,
                                      (void *) AH_cfg);
  if (NULL == authorization)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Authentication method `%s' not supported\n",
                method);
    GNUNET_free (lib_name);
    GNUNET_free (ap);
    return NULL;
  }
  ap->name = GNUNET_strdup (method);
  ap->lib_name = lib_name;
  ap->authorization = authorization;
  GNUNET_CONTAINER_DLL_insert (ap_head,
                               ap_tail,
                               ap);
  *cost = ap->cost;
  return authorization;
}


void
ANASTASIS_authorization_plugin_shutdown (void)
{
  struct AuthPlugin *ap;

  while (NULL != (ap = ap_head))
  {
    GNUNET_CONTAINER_DLL_remove (ap_head,
                                 ap_tail,
                                 ap);
    GNUNET_PLUGIN_unload (ap->lib_name,
                          ap->authorization);
    GNUNET_free (ap->lib_name);
    GNUNET_free (ap->name);
    GNUNET_free (ap);
  }
}


/**
 * Libtool search path before we started.
 */
static char *old_dlsearchpath;


/**
 * Setup libtool paths.
 */
void __attribute__ ((constructor))
anastasis_authorization_plugin_init (void)
{
  int err;
  const char *opath;
  char *path;
  char *cpath;

  err = lt_dlinit ();
  if (err > 0)
  {
    fprintf (stderr,
             _ ("Initialization of plugin mechanism failed: %s!\n"),
             lt_dlerror ());
    return;
  }
  opath = lt_dlgetsearchpath ();
  if (NULL != opath)
    old_dlsearchpath = GNUNET_strdup (opath);
  path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
  if (NULL != path)
  {
    if (NULL != opath)
    {
      GNUNET_asprintf (&cpath, "%s:%s", opath, path);
      lt_dlsetsearchpath (cpath);
      GNUNET_free (path);
      GNUNET_free (cpath);
    }
    else
    {
      lt_dlsetsearchpath (path);
      GNUNET_free (path);
    }
  }
}


/**
 * Shutdown libtool.
 */
void __attribute__ ((destructor))
anastasis_authorization_plugin_fini (void)
{
  lt_dlsetsearchpath (old_dlsearchpath);
  if (NULL != old_dlsearchpath)
  {
    GNUNET_free (old_dlsearchpath);
  }
  lt_dlexit ();
}


/* end of anastasis_authorization_plugin.c */