secmod_common.h (6399B)
1 /* 2 This file is part of GNU Taler 3 Copyright (C) 2021, 2026 Taler Systems SA 4 5 GNU Taler is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 GNU 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 15 <http://www.gnu.org/licenses/> 16 */ 17 /** 18 * @file util/secmod_common.h 19 * @brief Common functions for the exchange security modules 20 * @author Florian Dold <dold@taler.net> 21 */ 22 #ifndef SECMOD_COMMON_H 23 #define SECMOD_COMMON_H 24 25 #include <gnunet/gnunet_util_lib.h> 26 #include <gnunet/gnunet_network_lib.h> 27 #include <pthread.h> 28 29 30 /** 31 * Convert a configuration section name to the lowercase spelling used for 32 * denomination key directories and announcements. 33 * 34 * @param section configuration section name 35 * @return newly allocated lowercase name 36 */ 37 char * 38 TES_normalize_section (const char *section); 39 40 41 /** 42 * Validate denomination section names and rename key directories to lowercase 43 * before loading any keys. Section names containing '/' and conflicting 44 * directory names are rejected before changing any directory. Entries in 45 * @a keydir must not be symbolic links. 46 * 47 * @param cfg configuration containing the denomination sections 48 * @param keydir directory containing denomination key directories 49 * @param cprefix configuration section prefix identifying denominations 50 * @return #GNUNET_OK on success, #GNUNET_SYSERR on an invalid section name, 51 * symlink, conflict or I/O error 52 */ 53 enum GNUNET_GenericReturnValue 54 TES_normalize_key_directory (const struct GNUNET_CONFIGURATION_Handle *cfg, 55 const char *keydir, 56 const char *cprefix); 57 58 59 /** 60 * Create the listen socket for a secmod daemon. 61 * 62 * This function is not thread-safe, as it changes and 63 * restores the process umask. 64 * 65 * @param unixpath socket path 66 */ 67 struct GNUNET_NETWORK_Handle * 68 TES_open_socket (const char *unixpath); 69 70 71 /** 72 * Send a message starting with @a hdr to @a sock. 73 * 74 * @param sock where to send the message 75 * @param hdr beginning of the message, length indicated in size field 76 * @return #GNUNET_OK on success 77 */ 78 enum GNUNET_GenericReturnValue 79 TES_transmit (int sock, 80 const struct GNUNET_MessageHeader *hdr); 81 82 83 /** 84 * Transmit @a end bytes from @a pos on @a sock. 85 * 86 * @param sock where to send the data 87 * @param end how many bytes to send 88 * @param pos first address with data 89 * @return #GNUNET_OK on success 90 */ 91 enum GNUNET_GenericReturnValue 92 TES_transmit_raw (int sock, 93 size_t end, 94 const void *pos); 95 96 /** 97 * Information we keep for a client connected to us. 98 */ 99 struct TES_Client; 100 101 /** 102 * Function that handles message @a hdr from @a client. 103 * 104 * @param client sender of the message 105 * @param hdr message we received 106 * @return #GNUNET_OK on success 107 */ 108 typedef enum GNUNET_GenericReturnValue 109 (*TES_MessageDispatch)(struct TES_Client *client, 110 const struct GNUNET_MessageHeader *hdr); 111 112 113 /** 114 * Function that updates the keys for @a client. 115 * 116 * @param client sender of the message 117 * @return #GNUNET_OK on success 118 */ 119 typedef enum GNUNET_GenericReturnValue 120 (*TES_KeyUpdater)(struct TES_Client *client); 121 122 123 /** 124 * Module-specific functions to be used. 125 */ 126 struct TES_Callbacks 127 { 128 /** 129 * Function to handle inbound messages. 130 */ 131 TES_MessageDispatch dispatch; 132 133 /** 134 * Function to update key material initially. 135 */ 136 TES_KeyUpdater init; 137 138 /** 139 * Function to update key material. 140 */ 141 TES_KeyUpdater updater; 142 143 }; 144 145 146 /** 147 * Information we keep for a client connected to us. 148 */ 149 struct TES_Client 150 { 151 152 /** 153 * Kept in a DLL. 154 */ 155 struct TES_Client *next; 156 157 /** 158 * Kept in a DLL. 159 */ 160 struct TES_Client *prev; 161 162 /** 163 * Callbacks to use for work. 164 */ 165 struct TES_Callbacks cb; 166 167 /** 168 * Worker thread for this client. 169 */ 170 pthread_t worker; 171 172 /** 173 * Key generation this client is on. 174 */ 175 uint64_t key_gen; 176 177 /** 178 * IO-buffer used by @a purpose. 179 */ 180 char iobuf[65536]; 181 182 /** 183 * Client socket. 184 */ 185 int csock; 186 187 #ifdef __linux__ 188 /** 189 * Event socket. 190 */ 191 int esock; 192 #else 193 /** 194 * Input end of the event pipe. 195 */ 196 int esock_in; 197 198 /** 199 * Output end of the event pipe. 200 */ 201 int esock_out; 202 #endif 203 }; 204 205 206 /** 207 * Head of DLL of clients connected to us. 208 */ 209 extern struct TES_Client *TES_clients_head; 210 211 /** 212 * Tail of DLL of clients connected to us. 213 */ 214 extern struct TES_Client *TES_clients_tail; 215 216 /** 217 * Lock for the client queue. 218 */ 219 extern pthread_mutex_t TES_clients_lock; 220 221 /** 222 * Private key of this security module. Used to sign denomination key 223 * announcements. 224 */ 225 extern struct TALER_SecurityModulePrivateKeyP TES_smpriv; 226 227 /** 228 * Public key of this security module. 229 */ 230 extern struct TALER_SecurityModulePublicKeyP TES_smpub; 231 232 233 /** 234 * Send a signal to all clients to notify them about a key generation change. 235 */ 236 void 237 TES_wake_clients (void); 238 239 240 /** 241 * Read work request from the client. 242 * 243 * @param cls a `struct TES_Client *` 244 * @param dispatch function to call with work requests received 245 * @return #GNUNET_OK on success 246 */ 247 enum GNUNET_GenericReturnValue 248 TES_read_work (void *cls, 249 TES_MessageDispatch dispatch); 250 251 252 /** 253 * Wait until the socket is ready to read. 254 * 255 * @param client the client to wait for 256 * @return true if we received an event 257 */ 258 bool 259 TES_await_ready (struct TES_Client *client); 260 261 262 /** 263 * Free resources occupied by @a client. 264 * 265 * @param[in] client resources to release 266 */ 267 void 268 TES_free_client (struct TES_Client *client); 269 270 271 /** 272 * Start listen task. 273 * 274 * @param cfg configuration to use 275 * @param section configuration section to use 276 * @param cb callback functions to use 277 * @return 0 on success, otherwise return value to return from main() 278 */ 279 int 280 TES_listen_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 281 const char *section, 282 const struct TES_Callbacks *cb); 283 284 285 /** 286 * Stop listen task. 287 */ 288 void 289 TES_listen_stop (void); 290 291 292 #endif