diff --git a/control_tools.cpp b/control_tools.cpp index 88cb916..f573b7d 100644 --- a/control_tools.cpp +++ b/control_tools.cpp @@ -73,6 +73,7 @@ static char* command_generator(const char* text, int state) { "status", "st", "tfa", "auth", + "authsave", "finalize", "f", "quit", "q", nullptr @@ -120,6 +121,7 @@ void setup_app(CLI::App *app) { << " status(st): Show current sync state" << std::endl << " tfa : Supply 2FA code to a running daemon" << std::endl << " auth : Supply password to a running daemon" << std::endl + << " authsave : Supply password and save to database" << std::endl << " finalize(f): Kill daemon and quit" << std::endl << " quit(q): Exit this program" << std::endl; }); @@ -210,6 +212,29 @@ void setup_app(CLI::App *app) { return 0; }); + // authsave command + auto authsave_cmd = app->add_subcommand("authsave", "Supply password to a running daemon and save to database"); + static std::string authsave_pass_input; + authsave_cmd->add_option("password", authsave_pass_input, "Password")->required(); + authsave_cmd->callback([] { + char *errm = NULL; + size_t errm_size = 0; + RpcClient *rpc = new RpcClient(); + int result = rpc->Call(SENDAUTHSAVE, authsave_pass_input.c_str(), &errm, &errm_size); + putil_wipe(authsave_pass_input.data(), authsave_pass_input.size()); + authsave_pass_input.clear(); + if (result != 0) { + std::cerr << "Failed to send auth: " << (errm ? errm : "no message") << std::endl; + if (errm) { free(errm); } + delete rpc; + return result; + } + delete rpc; + std::cout << "Auth sent and saved." << std::endl; + if (errm) { free(errm); } + return 0; + }); + // status command app->add_subcommand("status", "Show current sync state")->alias("st")->callback([] { char *errm = NULL; diff --git a/pclsync/pcommands.h b/pclsync/pcommands.h index 2dd95e3..173771a 100644 --- a/pclsync/pcommands.h +++ b/pclsync/pcommands.h @@ -10,5 +10,6 @@ enum command_ids_ { SYNCRESUME, SENDTFA, SENDAUTH, + SENDAUTHSAVE, GETSTATUS }; diff --git a/pclsync_lib.cpp b/pclsync_lib.cpp index c82a428..34961ba 100644 --- a/pclsync_lib.cpp +++ b/pclsync_lib.cpp @@ -191,6 +191,16 @@ int clib::pclsync_lib::receive_auth(const char *pass) { return 0; } +int clib::pclsync_lib::receive_auth_save(const char *pass) { + psync_set_pass(pass, 1); + pthread_mutex_lock(&auth_mtx); + get_lib().password_ = std::string(pass); + pthread_cond_signal(&auth_cond); + pthread_mutex_unlock(&auth_mtx); + std::cout << "Auth received and saved." << std::endl; + return 0; +} + void clib::pclsync_lib::read_tfa_code(bool auto_sms) { if (daemon_) { @@ -713,6 +723,7 @@ int clib::pclsync_lib::init() { prpc_register(SYNCRESUME, &resume_sync); prpc_register(SENDTFA, &receive_tfa_code); prpc_register(SENDAUTH, &receive_auth); + prpc_register(SENDAUTHSAVE, &receive_auth_save); prpc_register(GETSTATUS, &get_status); return 0; diff --git a/pclsync_lib.h b/pclsync_lib.h index babe72b..7a45c3b 100644 --- a/pclsync_lib.h +++ b/pclsync_lib.h @@ -106,6 +106,7 @@ public: static int resume_sync(const char *unused); static int receive_tfa_code(const char *code); static int receive_auth(const char *pass); + static int receive_auth_save(const char *pass); static int get_status(const char *unused); int logout();