anastasis

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

anastasis-dbinit.c (4254B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2019-2021 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   struct ANASTASIS_DatabasePlugin *plugin;
     57 
     58   if (NULL ==
     59       (plugin = ANASTASIS_DB_plugin_load (cfg,
     60                                           true)))
     61   {
     62     fprintf (stderr,
     63              "Failed to initialize database plugin.\n");
     64     global_ret = EXIT_NOTINSTALLED;
     65     return;
     66   }
     67   if (reset_db)
     68   {
     69     if (GNUNET_OK != plugin->drop_tables (plugin->cls))
     70     {
     71       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     72                   "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");
     73     }
     74   }
     75   if (GNUNET_OK !=
     76       plugin->create_tables (plugin->cls))
     77   {
     78     global_ret = EXIT_FAILURE;
     79     ANASTASIS_DB_plugin_unload (plugin);
     80     return;
     81   }
     82   if (gc_db)
     83   {
     84     struct GNUNET_TIME_Absolute expire_backups;
     85     struct GNUNET_TIME_Absolute expire_payments;
     86     struct GNUNET_TIME_Absolute now;
     87 
     88     now = GNUNET_TIME_absolute_get ();
     89     expire_backups = GNUNET_TIME_absolute_subtract (
     90       now,
     91       GNUNET_TIME_relative_multiply (
     92         GNUNET_TIME_UNIT_MONTHS,
     93         6));
     94     expire_payments = GNUNET_TIME_absolute_subtract (
     95       now,
     96       GNUNET_TIME_relative_multiply (
     97         GNUNET_TIME_UNIT_YEARS,
     98         10));
     99     if (0 > plugin->gc (plugin->cls,
    100                         expire_backups,
    101                         expire_payments))
    102     {
    103       fprintf (stderr,
    104                "Garbage collection failed!\n");
    105     }
    106   }
    107   ANASTASIS_DB_plugin_unload (plugin);
    108 }
    109 
    110 
    111 /**
    112  * The main function of the database initialization tool.
    113  * Used to initialize the Anastasis' database.
    114  *
    115  * @param argc number of arguments from the command line
    116  * @param argv command line arguments
    117  * @return 0 ok, 1 on error
    118  */
    119 int
    120 main (int argc,
    121       char *const *argv)
    122 {
    123   struct GNUNET_GETOPT_CommandLineOption options[] = {
    124     GNUNET_GETOPT_option_flag ('g',
    125                                "garbagecollect",
    126                                "remove state data from database",
    127                                &gc_db),
    128     GNUNET_GETOPT_option_flag ('r',
    129                                "reset",
    130                                "reset database (DANGEROUS: all existing data is lost!)",
    131                                &reset_db),
    132 
    133     GNUNET_GETOPT_OPTION_END
    134   };
    135   enum GNUNET_GenericReturnValue ret;
    136 
    137   ret = GNUNET_PROGRAM_run (ANASTASIS_project_data (),
    138                             argc, argv,
    139                             "anastasis-dbinit",
    140                             "Initialize anastasis database",
    141                             options,
    142                             &run, NULL);
    143   if (GNUNET_SYSERR == ret)
    144     return EXIT_INVALIDARGUMENT;
    145   if (GNUNET_NO == ret)
    146     return EXIT_SUCCESS;
    147   return global_ret;
    148 }
    149 
    150 
    151 /* end of anastasis-dbinit.c */