anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

anastasis-dbinit.c (5004B)


      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   if (gc_db)
     81   {
     82     struct GNUNET_TIME_Absolute expire_backups;
     83     struct GNUNET_TIME_Absolute expire_payments;
     84     struct GNUNET_TIME_Absolute now;
     85     struct GNUNET_TIME_Relative backup_grace;
     86     struct GNUNET_TIME_Relative payment_retention;
     87 
     88     /* How long expired data is kept before it is collected is a
     89        data-minimization decision, so let the operator make it. */
     90     if (GNUNET_OK !=
     91         GNUNET_CONFIGURATION_get_value_time (cfg,
     92                                              "anastasis",
     93                                              "GC_BACKUP_GRACE",
     94                                              &backup_grace))
     95       backup_grace = GNUNET_TIME_relative_multiply (
     96         GNUNET_TIME_UNIT_MONTHS,
     97         6);
     98     if (GNUNET_OK !=
     99         GNUNET_CONFIGURATION_get_value_time (cfg,
    100                                              "anastasis",
    101                                              "GC_PAYMENT_RETENTION",
    102                                              &payment_retention))
    103       payment_retention = GNUNET_TIME_relative_multiply (
    104         GNUNET_TIME_UNIT_YEARS,
    105         10);
    106     now = GNUNET_TIME_absolute_get ();
    107     expire_backups = GNUNET_TIME_absolute_subtract (now,
    108                                                     backup_grace);
    109     expire_payments = GNUNET_TIME_absolute_subtract (now,
    110                                                      payment_retention);
    111     if (0 > ANASTASIS_DB_do_gc (
    112           expire_backups,
    113           expire_payments))
    114     {
    115       fprintf (stderr,
    116                "Garbage collection failed!\n");
    117     }
    118   }
    119   ANASTASIS_DB_fini ();
    120 }
    121 
    122 
    123 /**
    124  * The main function of the database initialization tool.
    125  * Used to initialize the Anastasis' database.
    126  *
    127  * @param argc number of arguments from the command line
    128  * @param argv command line arguments
    129  * @return 0 ok, 1 on error
    130  */
    131 int
    132 main (int argc,
    133       char *const *argv)
    134 {
    135   struct GNUNET_GETOPT_CommandLineOption options[] = {
    136     GNUNET_GETOPT_option_flag ('g',
    137                                "garbagecollect",
    138                                "remove state data from database",
    139                                &gc_db),
    140     GNUNET_GETOPT_option_flag ('r',
    141                                "reset",
    142                                "reset database (DANGEROUS: all existing data is lost!)",
    143                                &reset_db),
    144 
    145     GNUNET_GETOPT_OPTION_END
    146   };
    147   enum GNUNET_GenericReturnValue ret;
    148 
    149   ret = GNUNET_PROGRAM_run (ANASTASIS_project_data (),
    150                             argc, argv,
    151                             "anastasis-dbinit",
    152                             "Initialize anastasis database",
    153                             options,
    154                             &run, NULL);
    155   if (GNUNET_SYSERR == ret)
    156     return EXIT_INVALIDARGUMENT;
    157   if (GNUNET_NO == ret)
    158     return EXIT_SUCCESS;
    159   return global_ret;
    160 }
    161 
    162 
    163 /* end of anastasis-dbinit.c */