anastasis-dbinit.c (5838B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2019-2021, 2026 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 stasis/anastasis-dbinit.c 18 * @brief Create tables for the merchant database. 19 * @author Dennis Neufeld 20 * @author Dominik Meister 21 */ 22 #include "platform.h" 23 #include "anastasis_util_lib.h" 24 #include "anastasis_database_lib.h" 25 26 27 /** 28 * Return value from main(). 29 */ 30 static int global_ret; 31 32 /** 33 * -r option: do full DB reset 34 */ 35 static int reset_db; 36 37 /** 38 * -g option: do garbate collection 39 */ 40 static int gc_db; 41 42 /** 43 * Main function that will be run. 44 * 45 * @param cls closure 46 * @param args remaining command-line arguments 47 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 48 * @param cfg configuration 49 */ 50 static void 51 run (void *cls, 52 char *const *args, 53 const char *cfgfile, 54 const struct GNUNET_CONFIGURATION_Handle *cfg) 55 { 56 if (GNUNET_OK != 57 ANASTASIS_DB_init_admin (cfg)) 58 { 59 fprintf (stderr, 60 "Failed to initialize database.\n"); 61 global_ret = EXIT_NOTINSTALLED; 62 return; 63 } 64 if (reset_db) 65 { 66 if (GNUNET_OK != 67 ANASTASIS_DB_drop_tables ()) 68 { 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 70 "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n"); 71 } 72 } 73 if (GNUNET_OK != 74 ANASTASIS_DB_create_tables ()) 75 { 76 global_ret = EXIT_FAILURE; 77 ANASTASIS_DB_fini (); 78 return; 79 } 80 /* Finish what the stasis-0004 migration could not: SQL has no access to 81 [anastasis] CURRENCY, so the IBAN transfers it widened were left with a 82 NULL currency. Idempotent, and a no-op for a freshly created database. */ 83 { 84 enum GNUNET_DB_QueryStatus qs; 85 86 qs = ANASTASIS_DB_update_auth_iban_in_currency (); 87 if (0 > qs) 88 { 89 fprintf (stderr, 90 "Failed to set the currency of migrated IBAN transfers.\n"); 91 global_ret = EXIT_FAILURE; 92 ANASTASIS_DB_fini (); 93 return; 94 } 95 if (0 < qs) 96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 97 "Stamped currency onto %d migrated IBAN transfer(s)\n", 98 (int) qs); 99 } 100 if (gc_db) 101 { 102 struct GNUNET_TIME_Absolute expire_backups; 103 struct GNUNET_TIME_Absolute expire_payments; 104 struct GNUNET_TIME_Absolute now; 105 struct GNUNET_TIME_Relative backup_grace; 106 struct GNUNET_TIME_Relative payment_retention; 107 108 /* How long expired data is kept before it is collected is a 109 data-minimization decision, so let the operator make it. */ 110 if (GNUNET_OK != 111 GNUNET_CONFIGURATION_get_value_time (cfg, 112 "anastasis", 113 "GC_BACKUP_GRACE", 114 &backup_grace)) 115 backup_grace = GNUNET_TIME_relative_multiply ( 116 GNUNET_TIME_UNIT_MONTHS, 117 6); 118 if (GNUNET_OK != 119 GNUNET_CONFIGURATION_get_value_time (cfg, 120 "anastasis", 121 "GC_PAYMENT_RETENTION", 122 &payment_retention)) 123 payment_retention = GNUNET_TIME_relative_multiply ( 124 GNUNET_TIME_UNIT_YEARS, 125 10); 126 now = GNUNET_TIME_absolute_get (); 127 expire_backups = GNUNET_TIME_absolute_subtract (now, 128 backup_grace); 129 expire_payments = GNUNET_TIME_absolute_subtract (now, 130 payment_retention); 131 if (0 > ANASTASIS_DB_do_gc ( 132 expire_backups, 133 expire_payments)) 134 { 135 fprintf (stderr, 136 "Garbage collection failed!\n"); 137 /* A cron job or timer unit driving `-g' must be able to see that 138 nothing was reclaimed. */ 139 global_ret = EXIT_FAILURE; 140 } 141 } 142 ANASTASIS_DB_fini (); 143 } 144 145 146 /** 147 * The main function of the database initialization tool. 148 * Used to initialize the Anastasis' database. 149 * 150 * @param argc number of arguments from the command line 151 * @param argv command line arguments 152 * @return 0 ok, 1 on error 153 */ 154 int 155 main (int argc, 156 char *const *argv) 157 { 158 struct GNUNET_GETOPT_CommandLineOption options[] = { 159 GNUNET_GETOPT_option_flag ('g', 160 "garbagecollect", 161 "remove state data from database", 162 &gc_db), 163 GNUNET_GETOPT_option_flag ('r', 164 "reset", 165 "reset database (DANGEROUS: all existing data is lost!)", 166 &reset_db), 167 168 GNUNET_GETOPT_OPTION_END 169 }; 170 enum GNUNET_GenericReturnValue ret; 171 172 ret = GNUNET_PROGRAM_run (ANASTASIS_project_data (), 173 argc, argv, 174 "anastasis-dbinit", 175 "Initialize anastasis database", 176 options, 177 &run, NULL); 178 if (GNUNET_SYSERR == ret) 179 return EXIT_INVALIDARGUMENT; 180 if (GNUNET_NO == ret) 181 return EXIT_SUCCESS; 182 return global_ret; 183 } 184 185 186 /* end of anastasis-dbinit.c */