anastasis_authorization_plugin.c (4285B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2015, 2016, 2021 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Anastasis; 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_lib.h" 24 #include "anastasis_util_lib.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 * Authorization context passed to the plugin. 71 */ 72 struct ANASTASIS_AuthorizationContext ac; 73 74 }; 75 76 77 struct ANASTASIS_AuthorizationPlugin * 78 ANASTASIS_authorization_plugin_load ( 79 const char *method, 80 const struct GNUNET_CONFIGURATION_Handle *AH_cfg) 81 { 82 struct ANASTASIS_AuthorizationPlugin *authorization; 83 char *lib_name; 84 char *sec_name; 85 struct AuthPlugin *ap; 86 char *currency; 87 struct TALER_Amount cost; 88 89 for (ap = ap_head; NULL != ap; ap = ap->next) 90 if (0 == strcmp (method, 91 ap->name)) 92 return ap->authorization; 93 GNUNET_asprintf (&sec_name, 94 "authorization-%s", 95 method); 96 if (GNUNET_OK != 97 TALER_config_get_currency (AH_cfg, 98 sec_name, 99 ¤cy)) 100 { 101 GNUNET_free (sec_name); 102 return NULL; 103 } 104 ap = GNUNET_new (struct AuthPlugin); 105 ap->ac.cfg = AH_cfg; 106 if (GNUNET_OK != 107 TALER_config_get_amount (AH_cfg, 108 sec_name, 109 "COST", 110 &cost)) 111 { 112 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 113 sec_name, 114 "COST"); 115 GNUNET_free (sec_name); 116 GNUNET_free (currency); 117 GNUNET_free (ap); 118 return NULL; 119 } 120 121 GNUNET_free (currency); 122 GNUNET_free (sec_name); 123 GNUNET_asprintf (&lib_name, 124 "libanastasis_plugin_authorization_%s", 125 method); 126 authorization = GNUNET_PLUGIN_load (ANASTASIS_project_data (), 127 lib_name, 128 &ap->ac); 129 if (NULL == authorization) 130 { 131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 132 "Authentication method `%s' not supported\n", 133 method); 134 GNUNET_free (lib_name); 135 GNUNET_free (ap); 136 return NULL; 137 } 138 authorization->cost = cost; 139 ap->name = GNUNET_strdup (method); 140 ap->lib_name = lib_name; 141 ap->authorization = authorization; 142 GNUNET_CONTAINER_DLL_insert (ap_head, 143 ap_tail, 144 ap); 145 return authorization; 146 } 147 148 149 void 150 ANASTASIS_authorization_plugin_shutdown (void) 151 { 152 struct AuthPlugin *ap; 153 154 while (NULL != (ap = ap_head)) 155 { 156 GNUNET_CONTAINER_DLL_remove (ap_head, 157 ap_tail, 158 ap); 159 GNUNET_PLUGIN_unload (ap->lib_name, 160 ap->authorization); 161 GNUNET_free (ap->lib_name); 162 GNUNET_free (ap->name); 163 GNUNET_free (ap); 164 } 165 } 166 167 168 /* end of anastasis_authorization_plugin.c */