anastasis_authorization_plugin.c (4578B)
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_AmountList costs; 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 /* The list has already been validated at startup by 107 anastasis-httpd's parse_prices(), so anything wrong here means the 108 configuration changed underneath us. */ 109 if (GNUNET_OK != 110 TALER_config_get_amount_list (AH_cfg, 111 sec_name, 112 "COST", 113 &costs)) 114 { 115 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 116 sec_name, 117 "COST"); 118 GNUNET_free (sec_name); 119 GNUNET_free (currency); 120 GNUNET_free (ap); 121 return NULL; 122 } 123 124 GNUNET_free (currency); 125 GNUNET_free (sec_name); 126 GNUNET_asprintf (&lib_name, 127 "libanastasis_plugin_authorization_%s", 128 method); 129 authorization = GNUNET_PLUGIN_load (ANASTASIS_project_data (), 130 lib_name, 131 &ap->ac); 132 if (NULL == authorization) 133 { 134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 135 "Authentication method `%s' not supported\n", 136 method); 137 GNUNET_free (lib_name); 138 TALER_amount_list_free (&costs); 139 GNUNET_free (ap); 140 return NULL; 141 } 142 authorization->costs = costs; 143 ap->name = GNUNET_strdup (method); 144 ap->lib_name = lib_name; 145 ap->authorization = authorization; 146 GNUNET_CONTAINER_DLL_insert (ap_head, 147 ap_tail, 148 ap); 149 return authorization; 150 } 151 152 153 void 154 ANASTASIS_authorization_plugin_shutdown (void) 155 { 156 struct AuthPlugin *ap; 157 158 while (NULL != (ap = ap_head)) 159 { 160 GNUNET_CONTAINER_DLL_remove (ap_head, 161 ap_tail, 162 ap); 163 TALER_amount_list_free (&ap->authorization->costs); 164 GNUNET_PLUGIN_unload (ap->lib_name, 165 ap->authorization); 166 GNUNET_free (ap->lib_name); 167 GNUNET_free (ap->name); 168 GNUNET_free (ap); 169 } 170 } 171 172 173 /* end of anastasis_authorization_plugin.c */