anastasis_authorization_plugin.c (4342B)
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 struct ANASTASIS_DatabasePlugin *db, 81 const struct GNUNET_CONFIGURATION_Handle *AH_cfg) 82 { 83 struct ANASTASIS_AuthorizationPlugin *authorization; 84 char *lib_name; 85 char *sec_name; 86 struct AuthPlugin *ap; 87 char *currency; 88 struct TALER_Amount cost; 89 90 for (ap = ap_head; NULL != ap; ap = ap->next) 91 if (0 == strcmp (method, 92 ap->name)) 93 return ap->authorization; 94 GNUNET_asprintf (&sec_name, 95 "authorization-%s", 96 method); 97 if (GNUNET_OK != 98 TALER_config_get_currency (AH_cfg, 99 sec_name, 100 ¤cy)) 101 { 102 GNUNET_free (sec_name); 103 return NULL; 104 } 105 ap = GNUNET_new (struct AuthPlugin); 106 ap->ac.db = db; 107 ap->ac.cfg = AH_cfg; 108 if (GNUNET_OK != 109 TALER_config_get_amount (AH_cfg, 110 sec_name, 111 "COST", 112 &cost)) 113 { 114 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 115 sec_name, 116 "COST"); 117 GNUNET_free (sec_name); 118 GNUNET_free (currency); 119 GNUNET_free (ap); 120 return NULL; 121 } 122 123 GNUNET_free (currency); 124 GNUNET_free (sec_name); 125 GNUNET_asprintf (&lib_name, 126 "libanastasis_plugin_authorization_%s", 127 method); 128 authorization = GNUNET_PLUGIN_load (ANASTASIS_project_data (), 129 lib_name, 130 &ap->ac); 131 if (NULL == authorization) 132 { 133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 134 "Authentication method `%s' not supported\n", 135 method); 136 GNUNET_free (lib_name); 137 GNUNET_free (ap); 138 return NULL; 139 } 140 authorization->cost = cost; 141 ap->name = GNUNET_strdup (method); 142 ap->lib_name = lib_name; 143 ap->authorization = authorization; 144 GNUNET_CONTAINER_DLL_insert (ap_head, 145 ap_tail, 146 ap); 147 return authorization; 148 } 149 150 151 void 152 ANASTASIS_authorization_plugin_shutdown (void) 153 { 154 struct AuthPlugin *ap; 155 156 while (NULL != (ap = ap_head)) 157 { 158 GNUNET_CONTAINER_DLL_remove (ap_head, 159 ap_tail, 160 ap); 161 GNUNET_PLUGIN_unload (ap->lib_name, 162 ap->authorization); 163 GNUNET_free (ap->lib_name); 164 GNUNET_free (ap->name); 165 GNUNET_free (ap); 166 } 167 } 168 169 170 /* end of anastasis_authorization_plugin.c */