secmod_common.h (7064B)
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 * Open the private key file @a filename for reading and check that it may 61 * be used: it must be a regular file (not a symbolic link), be owned by our 62 * effective user, have exactly one hard link and grant no permissions to 63 * group or others. All checks are made with fstat() on the opened file 64 * descriptor. A refused file is logged at ERROR level with the reason and 65 * left untouched. 66 * 67 * @param filename name of the key file 68 * @param[out] sbuf set to the status of the opened file 69 * @return file descriptor open for reading, -1 if the file must not be used 70 */ 71 int 72 TES_open_key_file (const char *filename, 73 struct stat *sbuf); 74 75 76 /** 77 * Create the listen socket for a secmod daemon. 78 * 79 * This function is not thread-safe, as it changes and 80 * restores the process umask. 81 * 82 * @param unixpath socket path 83 */ 84 struct GNUNET_NETWORK_Handle * 85 TES_open_socket (const char *unixpath); 86 87 88 /** 89 * Send a message starting with @a hdr to @a sock. 90 * 91 * @param sock where to send the message 92 * @param hdr beginning of the message, length indicated in size field 93 * @return #GNUNET_OK on success 94 */ 95 enum GNUNET_GenericReturnValue 96 TES_transmit (int sock, 97 const struct GNUNET_MessageHeader *hdr); 98 99 100 /** 101 * Transmit @a end bytes from @a pos on @a sock. 102 * 103 * @param sock where to send the data 104 * @param end how many bytes to send 105 * @param pos first address with data 106 * @return #GNUNET_OK on success 107 */ 108 enum GNUNET_GenericReturnValue 109 TES_transmit_raw (int sock, 110 size_t end, 111 const void *pos); 112 113 /** 114 * Information we keep for a client connected to us. 115 */ 116 struct TES_Client; 117 118 /** 119 * Function that handles message @a hdr from @a client. 120 * 121 * @param client sender of the message 122 * @param hdr message we received 123 * @return #GNUNET_OK on success 124 */ 125 typedef enum GNUNET_GenericReturnValue 126 (*TES_MessageDispatch)(struct TES_Client *client, 127 const struct GNUNET_MessageHeader *hdr); 128 129 130 /** 131 * Function that updates the keys for @a client. 132 * 133 * @param client sender of the message 134 * @return #GNUNET_OK on success 135 */ 136 typedef enum GNUNET_GenericReturnValue 137 (*TES_KeyUpdater)(struct TES_Client *client); 138 139 140 /** 141 * Module-specific functions to be used. 142 */ 143 struct TES_Callbacks 144 { 145 /** 146 * Function to handle inbound messages. 147 */ 148 TES_MessageDispatch dispatch; 149 150 /** 151 * Function to update key material initially. 152 */ 153 TES_KeyUpdater init; 154 155 /** 156 * Function to update key material. 157 */ 158 TES_KeyUpdater updater; 159 160 }; 161 162 163 /** 164 * Information we keep for a client connected to us. 165 */ 166 struct TES_Client 167 { 168 169 /** 170 * Kept in a DLL. 171 */ 172 struct TES_Client *next; 173 174 /** 175 * Kept in a DLL. 176 */ 177 struct TES_Client *prev; 178 179 /** 180 * Callbacks to use for work. 181 */ 182 struct TES_Callbacks cb; 183 184 /** 185 * Worker thread for this client. 186 */ 187 pthread_t worker; 188 189 /** 190 * Key generation this client is on. 191 */ 192 uint64_t key_gen; 193 194 /** 195 * IO-buffer used by @a purpose. 196 */ 197 char iobuf[65536]; 198 199 /** 200 * Client socket. 201 */ 202 int csock; 203 204 #ifdef __linux__ 205 /** 206 * Event socket. 207 */ 208 int esock; 209 #else 210 /** 211 * Input end of the event pipe. 212 */ 213 int esock_in; 214 215 /** 216 * Output end of the event pipe. 217 */ 218 int esock_out; 219 #endif 220 }; 221 222 223 /** 224 * Head of DLL of clients connected to us. 225 */ 226 extern struct TES_Client *TES_clients_head; 227 228 /** 229 * Tail of DLL of clients connected to us. 230 */ 231 extern struct TES_Client *TES_clients_tail; 232 233 /** 234 * Lock for the client queue. 235 */ 236 extern pthread_mutex_t TES_clients_lock; 237 238 /** 239 * Private key of this security module. Used to sign denomination key 240 * announcements. 241 */ 242 extern struct TALER_SecurityModulePrivateKeyP TES_smpriv; 243 244 /** 245 * Public key of this security module. 246 */ 247 extern struct TALER_SecurityModulePublicKeyP TES_smpub; 248 249 250 /** 251 * Send a signal to all clients to notify them about a key generation change. 252 */ 253 void 254 TES_wake_clients (void); 255 256 257 /** 258 * Read work request from the client. 259 * 260 * @param cls a `struct TES_Client *` 261 * @param dispatch function to call with work requests received 262 * @return #GNUNET_OK on success 263 */ 264 enum GNUNET_GenericReturnValue 265 TES_read_work (void *cls, 266 TES_MessageDispatch dispatch); 267 268 269 /** 270 * Wait until the socket is ready to read. 271 * 272 * @param client the client to wait for 273 * @return true if we received an event 274 */ 275 bool 276 TES_await_ready (struct TES_Client *client); 277 278 279 /** 280 * Free resources occupied by @a client. 281 * 282 * @param[in] client resources to release 283 */ 284 void 285 TES_free_client (struct TES_Client *client); 286 287 288 /** 289 * Start listen task. 290 * 291 * @param cfg configuration to use 292 * @param section configuration section to use 293 * @param cb callback functions to use 294 * @return 0 on success, otherwise return value to return from main() 295 */ 296 int 297 TES_listen_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 298 const char *section, 299 const struct TES_Callbacks *cb); 300 301 302 /** 303 * Stop listen task. 304 */ 305 void 306 TES_listen_stop (void); 307 308 309 #endif