LCOV - code coverage report
Current view: top level - app/shared_dev/commands/configure - keys.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 65 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #include "../../../shared/commands/configure/configure.h"
       2             : 
       3             : #include "../../../platform/fd_file_util.h"
       4             : 
       5             : #include <errno.h>
       6             : #include <unistd.h>
       7             : #include <sys/stat.h>
       8             : 
       9             : #define NAME "keys"
      10             : 
      11             : void FD_FN_SENSITIVE
      12             : generate_keypair( char const *     keyfile,
      13             :                   uint             uid,
      14             :                   uint             gid,
      15             :                   int              use_grnd_random );
      16             : 
      17             : static int
      18             : path_parent( char const * path,
      19             :              char *       parent,
      20           0 :              ulong        parent_sz ) {
      21           0 :   char * last_slash = strrchr( path, '/' );
      22             : 
      23           0 :   if( FD_UNLIKELY( !last_slash ) ) return -1;
      24             : 
      25           0 :   ulong len = (ulong)(last_slash - path);
      26           0 :   if( FD_UNLIKELY( len>=parent_sz ) ) return -1;
      27             : 
      28           0 :   fd_memcpy( parent, path, len );
      29           0 :   parent[ len ] = '\0';
      30           0 :   return 0;
      31           0 : }
      32             : 
      33             : static void
      34           0 : init( config_t const * config ) {
      35           0 :   char identity_key_parent[ PATH_MAX ];
      36           0 :   if( FD_LIKELY( strrchr( config->paths.identity_key, '/' ) ) ) {
      37           0 :     if( FD_UNLIKELY( -1==path_parent( config->paths.identity_key, identity_key_parent, sizeof(identity_key_parent) ) ) )
      38           0 :       FD_LOG_ERR(( "failed to get parent directory of `%s`", config->paths.identity_key ));
      39             : 
      40           0 :     if( FD_UNLIKELY( -1==fd_file_util_mkdir_all( identity_key_parent, config->uid, config->gid ) ) )
      41           0 :       FD_LOG_ERR(( "could not create identity directory `%s` (%i-%s)", identity_key_parent, errno, fd_io_strerror( errno ) ));
      42           0 :   }
      43             : 
      44           0 :   struct stat st;
      45           0 :   if( FD_UNLIKELY( stat( config->paths.identity_key, &st ) && errno==ENOENT ) )
      46           0 :     generate_keypair( config->paths.identity_key, config->uid, config->gid, 0 );
      47             : 
      48           0 :   char vote_account_parent[ PATH_MAX ];
      49           0 :   if( FD_LIKELY( strrchr( config->paths.vote_account, '/' ) ) ) {
      50           0 :     if( FD_UNLIKELY( -1==path_parent( config->paths.vote_account, vote_account_parent, sizeof(vote_account_parent) ) ) )
      51           0 :       FD_LOG_ERR(( "failed to get parent directory of `%s`", config->paths.vote_account ));
      52             : 
      53           0 :     if( FD_UNLIKELY( -1==fd_file_util_mkdir_all( vote_account_parent, config->uid, config->gid ) ) )
      54           0 :       FD_LOG_ERR(( "could not create vote account directory `%s` (%i-%s)", vote_account_parent, errno, fd_io_strerror( errno ) ));
      55           0 :   }
      56             : 
      57           0 :   if( FD_LIKELY( strcmp( config->paths.vote_account, "" ) ) ) {
      58           0 :     if( FD_UNLIKELY( stat( config->paths.vote_account, &st ) && errno==ENOENT ) )
      59           0 :       generate_keypair( config->paths.vote_account, config->uid, config->gid, 0 );
      60           0 :   }
      61             : 
      62           0 :   if( FD_UNLIKELY( -1==fd_file_util_mkdir_all( config->paths.base, config->uid, config->gid ) ) )
      63           0 :     FD_LOG_ERR(( "could not create scratch directory `%s` (%i-%s)", config->paths.base, errno, fd_io_strerror( errno ) ));
      64             : 
      65           0 :   char faucet[ PATH_MAX ];
      66           0 :   FD_TEST( fd_cstr_printf_check( faucet, PATH_MAX, NULL, "%s/faucet.json", config->paths.base ) );
      67           0 :   if( FD_UNLIKELY( stat( faucet, &st ) && errno==ENOENT ) )
      68           0 :     generate_keypair( faucet, config->uid, config->gid, 0 );
      69             : 
      70           0 :   char stake[ PATH_MAX ];
      71           0 :   FD_TEST( fd_cstr_printf_check( stake, PATH_MAX, NULL, "%s/stake-account.json", config->paths.base ) );
      72           0 :   if( FD_UNLIKELY( stat( stake, &st ) && errno==ENOENT ) )
      73           0 :     generate_keypair( stake, config->uid, config->gid, 0 );
      74           0 : }
      75             : 
      76             : static configure_result_t
      77           0 : check( config_t const * config ) {
      78           0 :   char faucet[ PATH_MAX ], stake[ PATH_MAX ];
      79             : 
      80           0 :   FD_TEST( fd_cstr_printf_check( faucet, PATH_MAX, NULL, "%s/faucet.json", config->paths.base ) );
      81           0 :   FD_TEST( fd_cstr_printf_check( stake,  PATH_MAX, NULL, "%s/stake-account.json", config->paths.base ) );
      82             : 
      83           0 :   char const * paths[] = {
      84           0 :     config->paths.identity_key,
      85           0 :     config->paths.vote_account,
      86           0 :     faucet,
      87           0 :     stake,
      88           0 :   };
      89             : 
      90           0 :   struct stat st;
      91             : 
      92           0 :   int all_exist = 1;
      93           0 :   for( ulong i=0UL; i<4UL; i++ ) {
      94           0 :     if( FD_UNLIKELY( !strcmp( "", paths[ i ] ) ) ) continue;
      95           0 :     if( FD_UNLIKELY( stat( paths[ i ], &st ) && errno==ENOENT ) ) {
      96           0 :       all_exist = 0;
      97           0 :       continue;
      98           0 :     }
      99           0 :     CHECK( check_file( paths[ i ], config->uid, config->gid, S_IFREG | S_IRUSR | S_IWUSR ) );
     100           0 :   }
     101             : 
     102           0 :   if( FD_UNLIKELY( !all_exist ) ) NOT_CONFIGURED( " identity.json, vote-account.json, faucet.json, or stake-account.json does not exist" );
     103           0 :   else                         CONFIGURE_OK();
     104           0 : }
     105             : 
     106             : configure_stage_t fd_cfg_stage_keys = {
     107             :   .name            = NAME,
     108             :   .always_recreate = 0,
     109             :   .enabled         = NULL,
     110             :   .init_perm       = NULL,
     111             :   .fini_perm       = NULL,
     112             :   .init            = init,
     113             :   .fini            = NULL,
     114             :   .check           = check,
     115             : };
     116             : 
     117             : #undef NAME

Generated by: LCOV version 1.14