LCOV - code coverage report
Current view: top level - merchant-tools - taler-merchant-passwd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 55 58.2 %
Date: 2025-06-23 16:22:09 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2023 Taler Systems SA
       4             : 
       5             :   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             :   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 <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file merchant-tools/taler-merchant-passwd.c
      18             :  * @brief Reset access tokens for instances.
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <taler/taler_util.h>
      23             : #include <taler/taler_dbevents.h>
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include "taler_merchant_util.h"
      26             : #include "taler_merchantdb_lib.h"
      27             : #include "taler_merchantdb_lib.h"
      28             : 
      29             : /**
      30             :  * Instance to set password for.
      31             :  */
      32             : static char *instance;
      33             : 
      34             : /**
      35             :  * Return value from main().
      36             :  */
      37             : static int global_ret;
      38             : 
      39             : /**
      40             :  * Main function that will be run.
      41             :  *
      42             :  * @param cls closure
      43             :  * @param args remaining command-line arguments
      44             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      45             :  * @param config configuration
      46             :  */
      47             : static void
      48           1 : run (void *cls,
      49             :      char *const *args,
      50             :      const char *cfgfile,
      51             :      const struct GNUNET_CONFIGURATION_Handle *config)
      52             : {
      53             :   struct TALER_MERCHANTDB_Plugin *plugin;
      54             :   struct GNUNET_CONFIGURATION_Handle *cfg;
      55           1 :   const char *pw = args[0];
      56             :   struct TALER_MERCHANTDB_InstanceAuthSettings ias;
      57             :   enum GNUNET_DB_QueryStatus qs;
      58             : 
      59           1 :   if (NULL == pw)
      60           0 :     pw = getenv ("TALER_MERCHANT_PASSWORD");
      61           1 :   if (NULL == pw)
      62             :   {
      63           0 :     fprintf (stderr,
      64             :              "New password not specified (pass on command-line or via TALER_MERCHANT_PASSWORD)\n");
      65           0 :     global_ret = -1;
      66           0 :     return;
      67             :   }
      68           1 :   if (NULL == instance)
      69           1 :     instance = GNUNET_strdup ("admin");
      70           1 :   cfg = GNUNET_CONFIGURATION_dup (config);
      71           1 :   if (NULL ==
      72           1 :       (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
      73             :   {
      74           0 :     fprintf (stderr,
      75             :              "Failed to initialize database plugin.\n");
      76           0 :     global_ret = 1;
      77           0 :     GNUNET_CONFIGURATION_destroy (cfg);
      78           0 :     return;
      79             :   }
      80             : 
      81           1 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
      82             :                               &ias.auth_salt,
      83             :                               sizeof (ias.auth_salt));
      84           1 :   TALER_merchant_instance_auth_hash_with_salt (&ias.auth_hash,
      85             :                                                &ias.auth_salt,
      86             :                                                pw);
      87           1 :   if (GNUNET_OK !=
      88           1 :       plugin->connect (plugin->cls))
      89             :   {
      90           0 :     fprintf (stderr,
      91             :              "Failed to connect to database. Consider running taler-merchant-dbinit!\n");
      92           0 :     global_ret = 1;
      93           0 :     TALER_MERCHANTDB_plugin_unload (plugin);
      94           0 :     GNUNET_CONFIGURATION_destroy (cfg);
      95           0 :     return;
      96             :   }
      97           1 :   qs = plugin->update_instance_auth (plugin->cls,
      98             :                                      instance,
      99             :                                      &ias);
     100           1 :   switch (qs)
     101             :   {
     102           1 :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     103             :     {
     104           1 :       struct GNUNET_DB_EventHeaderP es = {
     105           1 :         .size = ntohs (sizeof (es)),
     106           1 :         .type = ntohs (TALER_DBEVENT_MERCHANT_INSTANCE_SETTINGS)
     107             :       };
     108             : 
     109           1 :       plugin->event_notify (plugin->cls,
     110             :                             &es,
     111             :                             instance,
     112           1 :                             strlen (instance) + 1);
     113             :     }
     114           1 :     break;
     115           0 :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     116           0 :     fprintf (stderr,
     117             :              "Instance `%s' unknown, cannot reset token\n",
     118             :              instance);
     119           0 :     global_ret = 2;
     120           0 :     break;
     121           0 :   case GNUNET_DB_STATUS_SOFT_ERROR:
     122             :   case GNUNET_DB_STATUS_HARD_ERROR:
     123           0 :     fprintf (stderr,
     124             :              "Internal database error.\n");
     125           0 :     global_ret = 3;
     126           0 :     break;
     127             :   }
     128           1 :   TALER_MERCHANTDB_plugin_unload (plugin);
     129           1 :   GNUNET_CONFIGURATION_destroy (cfg);
     130             : }
     131             : 
     132             : 
     133             : /**
     134             :  * The main function of the database initialization tool.
     135             :  * Used to initialize the Taler Exchange's database.
     136             :  *
     137             :  * @param argc number of arguments from the command line
     138             :  * @param argv command line arguments
     139             :  * @return 0 ok, 1 on error
     140             :  */
     141             : int
     142           1 : main (int argc,
     143             :       char *const *argv)
     144             : {
     145           1 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
     146           1 :     GNUNET_GETOPT_option_string ('i',
     147             :                                  "instance",
     148             :                                  "ID",
     149             :                                  "which instance to reset the password of",
     150             :                                  &instance),
     151             : 
     152           1 :     GNUNET_GETOPT_option_version (PACKAGE_VERSION "-" VCS_VERSION),
     153             :     GNUNET_GETOPT_OPTION_END
     154             :   };
     155             :   enum GNUNET_GenericReturnValue ret;
     156             : 
     157           1 :   ret = GNUNET_PROGRAM_run (
     158             :     TALER_MERCHANT_project_data (),
     159             :     argc, argv,
     160             :     "taler-merchant-passwd",
     161             :     gettext_noop ("Reset instance password"),
     162             :     options,
     163             :     &run, NULL);
     164           1 :   if (GNUNET_SYSERR == ret)
     165           0 :     return 3;
     166           1 :   if (GNUNET_NO == ret)
     167           0 :     return 0;
     168           1 :   return global_ret;
     169             : }
     170             : 
     171             : 
     172             : /* end of taler-merchant-passwd.c */

Generated by: LCOV version 1.16