From d3bea1c641c88d201a98efe07a35eb55ebb8f55b Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Sun, 9 Mar 2025 19:58:00 +0100 Subject: [PATCH] access pcryptofolder directly from pclsync_lib (#125) * access pcryptofolder directly from pclsync_lib * move issetup to pcryptofolder --- pclsync/pcryptofolder.c | 8 ++++++-- pclsync/pcryptofolder.h | 1 + pclsync/pdiff.c | 3 ++- pclsync/pfolder.c | 2 +- pclsync/psynclib.c | 22 +-------------------- pclsync/psynclib.h | 5 ----- pclsync_lib.cpp | 42 ++++++++++++++++++++--------------------- 7 files changed, 32 insertions(+), 51 deletions(-) diff --git a/pclsync/pcryptofolder.c b/pclsync/pcryptofolder.c index ac46973..d51a142 100644 --- a/pclsync/pcryptofolder.c +++ b/pclsync/pcryptofolder.c @@ -357,6 +357,11 @@ setup_upload(const unsigned char *rsapriv, size_t rsaprivlen, return ret; } +int pcryptofolder_issetup() { + return psync_sql_cellint("SELECT value FROM setting WHERE id='cryptosetup'", 0); +} + + /* * generate 64 byte (512 bit) salt for PBKDF2 * generate AES key and IV with PBKDF2 @@ -364,7 +369,6 @@ setup_upload(const unsigned char *rsapriv, size_t rsaprivlen, * upload to server salt, encrypted private and public * */ - int pcryptofolder_setup(const char *password, const char *hint) { unsigned char salt[PSYNC_CRYPTO_PBKDF2_SALT_LEN]; char publicsha1[PSYNC_SHA1_DIGEST_HEXLEN + 2], @@ -719,7 +723,7 @@ int pcryptofolder_reset() { binresult *res; uint32_t result; int tries; - if (!psync_crypto_issetup()) + if (!pcryptofolder_issetup()) return PRINT_RETURN_CONST(PSYNC_CRYPTO_RESET_NOT_SETUP); debug(D_NOTICE, "resetting crypto"); tries = 0; diff --git a/pclsync/pcryptofolder.h b/pclsync/pcryptofolder.h index 488985d..95e3a08 100644 --- a/pclsync/pcryptofolder.h +++ b/pclsync/pcryptofolder.h @@ -77,6 +77,7 @@ int pcryptofolder_mkdir(psync_folderid_t folderid, const char *name, const char int pcryptofolder_reset(); int pcryptofolder_setup(const char *password, const char *hint); int pcryptofolder_unlock(const char *password); +int pcryptofolder_issetup(); #ifdef __cplusplus } diff --git a/pclsync/pdiff.c b/pclsync/pdiff.c index bb81ed8..be3af98 100644 --- a/pclsync/pdiff.c +++ b/pclsync/pdiff.c @@ -45,6 +45,7 @@ #include "papi.h" #include "pbusinessaccount.h" #include "pcache.h" +#include "pcryptofolder.h" #include "pqevent.h" #include "pcontacts.h" #include "pdevice.h" @@ -1646,7 +1647,7 @@ static void start_download() { } static void stop_crypto_thread() { - psync_crypto_stop(); + pcryptofolder_lock(); psync_delete_cached_crypto_keys(); } diff --git a/pclsync/pfolder.c b/pclsync/pfolder.c index 3d4ea94..15f6cdb 100644 --- a/pclsync/pfolder.c +++ b/pclsync/pfolder.c @@ -645,7 +645,7 @@ pfolder_list_t *pfolder_remote_folders(psync_folderid_t folderid, entry.folder.canshare = (psync_my_userid == psync_get_number(row[3])); entry.folder.isencrypted = (psync_get_number(row[4]) & PSYNC_FOLDER_FLAG_ENCRYPTED) ? 1 : 0; - if (parentencrypted && psync_crypto_isstarted()) { + if (parentencrypted && pcryptofolder_is_unlocked()) { tmp = (char *)psync_get_lstring(row[2], &namelen); entry.name = get_decname_for_folder(folderid, tmp, namelen); if (!entry.name) { diff --git a/pclsync/psynclib.c b/pclsync/psynclib.c index 61455ef..8bda933 100644 --- a/pclsync/psynclib.c +++ b/pclsync/psynclib.c @@ -216,7 +216,7 @@ void psync_set_alloc(psync_malloc_t malloc_call, psync_realloc_t realloc_call, static void psync_stop_crypto_on_sleep() { if (psync_setting_get_bool(_PS(sleepstopcrypto)) && - psync_crypto_isstarted()) { + pcryptofolder_is_unlocked()) { pcryptofolder_lock(); debug(D_NOTICE, "stopped crypto due to sleep"); } @@ -2180,13 +2180,6 @@ char *psync_derive_password_from_passphrase(const char *username, return psymkey_derive(username, passphrase); } -int psync_crypto_setup(const char *password, const char *hint) { - if (psync_status_is_offline()) - return PSYNC_CRYPTO_SETUP_CANT_CONNECT; - else - return pcryptofolder_setup(password, hint); -} - int psync_crypto_get_hint(char **hint) { if (psync_status_is_offline()) return PSYNC_CRYPTO_HINT_CANT_CONNECT; @@ -2194,14 +2187,6 @@ int psync_crypto_get_hint(char **hint) { return pcryptofolder_get_hint(hint); } -int psync_crypto_start(const char *password) { - return pcryptofolder_unlock(password); -} - -int psync_crypto_stop() { return pcryptofolder_lock(); } - -int psync_crypto_isstarted() { return pcryptofolder_is_unlocked(); } - int psync_crypto_mkdir(psync_folderid_t folderid, const char *name, const char **err, psync_folderid_t *newfolderid) { if (psync_status_is_offline()) @@ -2210,11 +2195,6 @@ int psync_crypto_mkdir(psync_folderid_t folderid, const char *name, return pcryptofolder_mkdir(folderid, name, err, newfolderid); } -int psync_crypto_issetup() { - return psync_sql_cellint("SELECT value FROM setting WHERE id='cryptosetup'", - 0); -} - int psync_crypto_hassubscription() { return psync_sql_cellint( "SELECT value FROM setting WHERE id='cryptosubscription'", 0); diff --git a/pclsync/psynclib.h b/pclsync/psynclib.h index 0985a15..4f7cb55 100644 --- a/pclsync/psynclib.h +++ b/pclsync/psynclib.h @@ -1324,14 +1324,9 @@ char *psync_derive_password_from_passphrase(const char *username, */ -int psync_crypto_setup(const char *password, const char *hint); int psync_crypto_get_hint(char **hint); -int psync_crypto_start(const char *password); -int psync_crypto_stop(); -int psync_crypto_isstarted(); int psync_crypto_mkdir(psync_folderid_t folderid, const char *name, const char **err, psync_folderid_t *newfolderid); -int psync_crypto_issetup(); int psync_crypto_hassubscription(); int psync_crypto_isexpired(); time_t psync_crypto_expires(); diff --git a/pclsync_lib.cpp b/pclsync_lib.cpp index beff6d5..609f2ee 100644 --- a/pclsync_lib.cpp +++ b/pclsync_lib.cpp @@ -208,7 +208,7 @@ static int lib_setup_cripto() { return PSYNC_CRYPTO_CANT_CONNECT; } - if(!psync_crypto_issetup()) { + if(!pcryptofolder_issetup()) { std::cout << "crypto is not setup, setting it up now..." << std::endl; if(int ret = pcryptofolder_setup(pwd, "no hint") != PSYNC_CRYPTO_SETUP_SUCCESS) { std::cout << "crypto setup failed, error code was " << ret << std::endl; @@ -342,34 +342,34 @@ static void status_change(pstatus_t *status) { psync_free(err); } -int clib::pclsync_lib::start_crypto(const char *pass) { - get_lib().crypto_pass_ = pass; +int clib::pclsync_lib::start_crypto(const char *pwd) { + get_lib().crypto_pass_ = pwd; return lib_setup_cripto(); } -int clib::pclsync_lib::stop_crypto(const char *path) { - (void)path; +int clib::pclsync_lib::stop_crypto(const char *unused) { + (void)unused; - psync_crypto_stop(); + pcryptofolder_lock(); get_lib().crypto_on_ = false; return 0; } -int clib::pclsync_lib::finalize(const char *path) { - (void)path; +int clib::pclsync_lib::finalize(const char *unused) { + (void)unused; psync_destroy(); exit(0); } // path is the local and remote path delimited by '|' -int clib::pclsync_lib::add_sync_folder(const char *path) { - if (path == nullptr) { +int clib::pclsync_lib::add_sync_folder(const char *combined_path) { + if (combined_path == nullptr) { std::cerr << "Error: path is nullptr" << std::endl; return -255; } const char delimiter = '|'; - std::string combined(path); + std::string combined(combined_path); size_t delimiter_pos = combined.find(delimiter); if (delimiter_pos == std::string::npos) { std::cerr << "Error: Invalid path format. Expected 'localpath|remotepath'" @@ -395,15 +395,15 @@ int clib::pclsync_lib::add_sync_folder(const char *path) { } // path is the folderid to remove -int clib::pclsync_lib::remove_sync_folder(const char *path) { +int clib::pclsync_lib::remove_sync_folder(const char *fid) { psync_folderid_t folderid; - folderid = static_cast(std::stoull(path, nullptr, 10)); + folderid = static_cast(std::stoull(fid, nullptr, 10)); return psync_delete_sync_by_folderid(folderid); } // path is not used -int clib::pclsync_lib::list_sync_folders(const char *path) { - (void)path; +int clib::pclsync_lib::list_sync_folders(const char *unused) { + (void)unused; psync_folder_list_t *folders; size_t folderssz; @@ -459,12 +459,12 @@ int clib::pclsync_lib::init() { psync_free(username_old); } - prpc_register(STARTCRYPTO, &clib::pclsync_lib::start_crypto); - prpc_register(STOPCRYPTO, &clib::pclsync_lib::stop_crypto); - prpc_register(FINALIZE, &clib::pclsync_lib::finalize); - prpc_register(LISTSYNC, &clib::pclsync_lib::list_sync_folders); - prpc_register(ADDSYNC, &clib::pclsync_lib::add_sync_folder); - prpc_register(STOPSYNC, &clib::pclsync_lib::remove_sync_folder); + prpc_register(STARTCRYPTO, &start_crypto); + prpc_register(STOPCRYPTO, &stop_crypto); + prpc_register(FINALIZE, &finalize); + prpc_register(LISTSYNC, &list_sync_folders); + prpc_register(ADDSYNC, &add_sync_folder); + prpc_register(STOPSYNC, &remove_sync_folder); return 0; }