Commit Graph

32 Commits

Author SHA1 Message Date
Levi Neely bf11ae3490
Fix authsave to save both username and password (#390)
Update do_authsave() to call psync_set_user_pass() instead of
psync_set_pass(), ensuring both username and password are persisted.

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-11 22:39:08 +01:00
Levi Neely 59a1a772e6
Add authsave command to save authentication credentials (#389)
Implement authsave command to persist authentication credentials:
- Add AUTHSAVE command definition to pcommands.h
- Expose do_authsave() in pclsync_lib.h
- Implement do_authsave() in pclsync_lib.cpp
- Add authsave handler in control_tools.cpp

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-11 22:23:09 +01:00
Levi Neely 358ae595e9
Add memory accounting per subsystem (#371)
* Implement memory accounting infrastructure

* Migrate malloc/calloc to pmem_malloc with subsystem tracking

* Add overflow-safe pmem_malloc_array function

* Migrate pfstasks.c malloc/free to pmem_malloc/pmem_free

* Migrate pnetlibs.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in pnetlibs.c with pmem_malloc_array

* Migrate ppagecache.c malloc/free to pmem_malloc/pmem_free

* Fix remaining multiplication pattern in ppagecache.c line 3239

* Migrate pssl.c malloc/free to pmem_malloc/pmem_free

* Migrate pcryptofolder.c malloc/free to pmem_malloc/pmem_free

* Migrate pfolder.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in pfolder.c with overflow checks

* Migrate publiclinks.c malloc/free to pmem_malloc/pmem_free

* Migrate plocalscan.c malloc/free to pmem_malloc/pmem_free

* Migrate psql.c malloc/free to pmem_malloc/pmem_free

* Migrate putil.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication patterns in putil.c encoding functions with overflow checks

* Migrate pfsupload.c malloc/free to pmem_malloc/pmem_free

* Migrate pdiff.c malloc/free to pmem_malloc/pmem_free

* Migrate pcrypto.c malloc/free to pmem_malloc/pmem_free

* Migrate pupload.c malloc/free to pmem_malloc/pmem_free

* Migrate psock.c malloc/free to pmem_malloc/pmem_free

* Fix multiplication pattern in psock.c with overflow checks

* Add panic() with backtrace to psignal module

* Register panic handlers early in main()

* Fix panic() infinite recursion by unregistering handlers before abort()

* Migrate remaining 34 files to pmem_malloc/pmem_free

* Migrate papi.c, ptools.c, pbusinessaccount.c to PMEM_SUBSYS_API

* Fix pcache.c compilation errors - replace he->pmem_free with pmem_free

* Add pmem.h include to pdbg.c

* Add pmem.h include to 10 files missing it

* Fix ptask.c compilation errors - replace s->pmem_free with s->free

* Fix ptask.c callback arguments - remove PMEM_SUBSYS_OTHER from s->free calls

* Fix pmem.c infinite recursion - use raw malloc/free internally

* Fix prand.c mixed allocation - use pmem_free for putil_strdup result

* Migrate psettings.c free() calls to pmem_free()

* Fix all remaining stray free() calls - migrate to pmem_free()

* Fix pnetlibs_debug.c free() call - migrate to pmem_free()

* Add missing pmem.h include to pdevice.c

* Migrate pclsync_lib.cpp free() calls to pmem_free()

* Fix pfolder.c psync_free_string_list to use pmem_free()

* Fix all psync_list_for_each_element_call to use pmem_free wrappers

* Fix all list free wrapper functions - add proper definitions

* Fix rpcclient.cpp mixed allocation - use pmem_free for prpc_sockpath

* Replace all realloc() calls with pmem_realloc()

* Replace all strdup() calls with putil_strdup()

* Fix rpcclient.cpp allocation boundary - use plain malloc/free for RPC responses

* Fix RPC response length calculation - include header size

* Fix control_tools.cpp mixed allocations - use pmem_free for pshm_read results

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-08 21:47:22 +01:00
Levi Neely 89ef8663b0
Add psignal module for centralized signal handling (#368)
* Add psignal module for centralized signal handling

- Added pclsync/psignal.h with psignal_register() and psignal_check_pending() API
- Added pclsync/psignal.c with async-signal-safe handlers for SIGINT, SIGTERM, SIGHUP
- Implements flag-based deferred signal handling pattern

* Integrate psignal into main loop and remove conflicting handlers

- Added extern "C" linkage to psignal.h for C++ compatibility
- Integrated psignal_check_pending() into pclsync_lib.cpp main loop
- Integrated psignal_check_pending() into ptimer.c timer loop
- Removed conflicting signal handlers from pfs.c

* Migrate psync_set_signal to psignal module

- Added psignal_set_custom_handler() to psignal.h and psignal.c
- Replaced psync_set_signal() calls in pfs.c with psignal_set_custom_handler()
- Centralizes all signal handling in psignal module

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-08 14:27:21 +01:00
Levi Neely 431f94725f
Fix pcl-r0j: rename psync_delete_sync_by_folderid to pfolder_delete_sync_by_id (#361)
Renamed psync_delete_sync_by_folderid to pfolder_delete_sync_by_id to align with pfolder API naming conventions. Updated all callers.

Fixes #100

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-07 19:54:21 +01:00
Levi Neely 3cc42792b5
Fix pcl-bga: make psync_status.status accesses atomic (#346)
psync_status.status is written in pstatus_set() (pstatus.c:263) after
releasing status_internal_mutex, while status_change() (pclsync_lib.cpp:435)
reads it concurrently from the callback thread without any lock. TSan
reports the race between T9 (write in pstatus_set) and the status callback
thread (read in status_change).

Replace all reads and writes of psync_status.status with
__atomic_load_n/__atomic_store_n (__ATOMIC_RELAXED) across pstatus.c,
pqevent.c, and pclsync_lib.cpp. In status_change_thread, capture the
atomic value once into cur_status before the condition to avoid multiple
inconsistent loads. In status_change(), capture cur_status at entry and
use it throughout, also propagating it to the copied status_ struct.

Closes #334

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-04 18:24:35 +01:00
Levi Neely 0f23a32793
Fix input validation: stoull exception and buffer bounds (#297)
- Wrap std::stoull() in try/catch to prevent daemon crash on invalid folder ID
- Add proper read loop for RPC messages to handle partial reads
- Validate msg->length before memcpy to prevent heap over-read
- Handle daemon bugs gracefully (EOF before full message)
- Fix operator precedence in sync remove command

Fixes #205, #206

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-03 11:55:54 +01:00
Levi Neely c0cc893cc7
Fix umask and password memory wipe (#294)
* Fix debug build segfault: add strong overrides for psql_lock/unlock functions

The weak/strong symbol approach requires that psql.c undefs the debug
macros to define function implementations. This means functions in psql.c
call psql_lock()/psql_unlock() as functions, not macros.

In debug builds, these must be strong overrides that call the _do_
variants to properly maintain lockctr/rdlockctr. Without these overrides,
the weak stubs are used which don't update counters, causing assertion
failures in psql_unlock().

Added strong overrides in psql_debug.c for:
- psql_lock() -> psql_do_lock(__FILE__, __LINE__)
- psql_rdlock() -> psql_do_rdlock(__FILE__, __LINE__)
- psql_statement() (calls psql_do_lock directly)

Also made psql_statement, psql_start, and all query/prepare functions
weak in psql.c to allow debug overrides.

Fixes #138

* Fix umask and password memory wipe

- Set umask(0077) in daemonize() to prevent world-readable files
- Wipe password from memory after psync_set_user_pass()

Fixes #203, #204

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
2026-03-03 11:20:07 +01:00
Levi Neely 4d64b645a1
Increase maintainability: namespace refactors for putil, pfs, and pfs sub-modules (#176)
* Fix #103: Refactor putil to use putil_ namespace and enforce encapsulation

All public functions in putil now use the putil_ prefix (e.g., putil_strdup,
putil_strcat, putil_time_format, putil_base64_encode, etc.). Internal
variables (normalize_table, base64_reverse_table) and the constructor remain
static. Updated all 34 call-site files across pclsync accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix #96: Refactor pfs to use pfs_ namespace and enforce encapsulation

All public functions in the pfs module renamed from psync_fs_ to pfs_
(e.g., pfs_update_openfile, pfs_refresh, pfs_start, pfs_stop, etc.).
Extern vars psync_fake_prefix/psync_fake_prefix_len renamed to
pfs_fake_prefix/pfs_fake_prefix_len. Functions only used within pfs.c
(pfs_update_openfile_fileid_locked, pfs_chown) made static. Updated all
call sites across 17 files including pclsync_lib.cpp.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor pfsxattr to use pfs_xattr_ namespace

All psync_fs_ prefixed functions in the pfsxattr module renamed to
pfs_xattr_ (e.g., pfs_xattr_set, pfs_xattr_get, pfs_xattr_list,
pfs_xattr_remove, pfs_xattr_file_deleted, pfs_xattr_task_to_file, etc.).
Local macro psync_fs_set_thread_name renamed to pfs_xattr_set_thread_name.
Internal static helpers (delete_object_id, update_object_id,
xattr_get_object_id_locked) remain static and unchanged.
Updated all call sites in pfs.c, pfsupload.c, and pdiff.c.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Rename pfs_xattr_ namespace to pfs_xatr_

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor pfs sub-modules to use namespaced function prefixes

- pfscrypto_* → pfs_crpt_*
- psync_fsstatic_* → pfs_stat_*
- psync_fsupload_* → pfs_upld_*
- psync_fstask_* → pfs_task_* (functions only; typedefs unchanged)
- psync_fsfolder_*/psync_fsfolderid_*/psync_fsfolderflags_*/
  get_decname_for_folder/psync_get_folderid → pfs_fldr_*

Type names, macro constants, and typedef aliases are unchanged.
Updated all call sites across 17 files.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 22:51:25 +01:00
Levi Neely d6858a0166
Fix #90, #109: Support 2FA when running as a daemon (#175)
* Fix #90: Support 2FA when running as a daemon

In daemon mode, when PSTATUS_TFA_REQUIRED fires:
- Automatically send an SMS 2FA code via psync_tfa_send_sms()
- Log delivery details to syslog with instructions to use
  `echo 'tfa CODE' | pcloudcc -k`
- Block on a condition variable until the code arrives via RPC

Add a new `tfa <code>` command to the pcloudcc -k control interface.
This sends the code to the daemon over the existing Unix socket RPC
channel (SENDTFA message type).

Bad codes (PSTATUS_BAD_TFA_CODE) are handled: the daemon logs a syslog
warning and waits for a corrected code without re-sending SMS.

Devices are trusted by default (trusted=1 in psync_tfa_set_code) so
repeated 2FA prompts are avoided for the configured trust period.
TFA codes are wiped from memory after use.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add auth command: supply password to running daemon

Mirrors the tfa command pattern. When PSTATUS_LOGIN_REQUIRED fires in
daemon mode, read_password() now logs a syslog notice and blocks on a
condition variable rather than calling exit(1).

The new `auth <password>` control command (SENDAUTH RPC type) signals
the condvar and unblocks the daemon. Usage:

  echo 'auth PASSWORD' | pcloudcc -k

The password is wiped from the CLI-side buffer immediately after the
RPC call. putil_wipe() is used for both the tfa and auth transient
strings on the sending side.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add status command: show current sync state from CLI

Adds a `status` (alias `st`) command to the interactive CLI and to the
pcloudcc -k control interface. The daemon-side handler calls
psync_get_status(), formats the status name plus download/upload
strings, and writes the result to shared memory. The client reads and
prints it.

Example output:
  Status:   READY
  Download: idle
  Upload:   idle

Also works non-interactively:
  echo 'status' | pcloudcc -k

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 21:36:33 +01:00
Levi Neely 79371d9aac
Fix #67: Add sync pause and sync resume commands (#173)
Wire psync_pause() and psync_resume() from the C library into the RPC
command layer, registering SYNCPAUSE and SYNCRESUME handlers and exposing
them as 'sync pause' and 'sync resume' CLI subcommands.

Note: daemon quit is already handled by the existing 'finalize' command,
which calls psync_destroy() and exits the daemon process cleanly.

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 20:12:53 +01:00
Jonathan Senkerik 441a0d69ed
Refactor logging, add cache-size, fs-event-log (#163)
Much appreciated! Merged. :)
2026-02-27 08:38:28 +01:00
Levi Neely 2c5e32e910 Fix #95: Remove unused get_token() wrapper from pclsync_lib
- Eliminates thin wrapper that just called psync_get_token()
- Function was declared but never used
- Simplifies pclsync_lib API as per issue #95
2026-02-14 20:08:01 +01:00
Levi Neely 11074655db Fix #8: Add pending transfer check feature
- Add new 'pending' command (alias 'p') to check for pending transfers
- Enhance 'finalize' command to warn about pending transfers
- Prompt for confirmation in interactive mode before shutdown
- Use psync_get_status() API for reliable transfer detection
- Shows upload/download counts separately
- Fix mbedtls 3.6 compatibility issues in pssl.c
- Update Makefile for mbedtls 3.x static linking
2026-02-14 19:43:13 +01:00
Levi Neely e566c7822a Fix #107: Add SMS 2FA support
- Enhance read_tfa_code() to offer SMS option
- User can type 'sms' to receive code via SMS
- Display phone number when SMS is sent
- Falls back to authenticator app if SMS fails
- Uses existing psync_tfa_send_sms() API
2026-02-14 19:20:10 +01:00
Levi Neely 6540e14732
move folder-related code from psynclib to pfolder 2025-04-18 15:23:50 +02:00
Levi Neely 9da561c767
131 remove custom memory management wrappers (#137)
* psync_free removed

* psync_malloc removed

* psync_realloc removed

* remove dead code

* remove pmemlock

* remove dead code
2025-03-11 22:27:30 +01:00
Levi Neely 79141898c3
84 security enhancements (#133)
- DoD 5220.22-M compliant memory wipe replaces single-pass zero wipe
- Wipe in-memory request and response messages before freeing
- Wipe in-memory private keys and salt data before freeing
- Wipe all passwords on unlink / logout
- Wipe crypto password on all pcryptofolder_unlock return paths
2025-03-11 15:05:43 +01:00
Levi Neely d3bea1c641
access pcryptofolder directly from pclsync_lib (#125)
* access pcryptofolder directly from pclsync_lib
* move issetup to pcryptofolder
2025-03-09 19:58:00 +01:00
Levi Neely f437fe840e
prpc namespace and api cleanup (#122)
* prpc namespace and api cleanup
2025-03-09 17:13:35 +01:00
Levi Neely 8ad1fe6086
Code cleanup and readline for better CLI UX (#116)
* pcommands.h for command ids

* remove compiler compat junk

* transient and unnecessary includes resolved

* unnecessary goto

* function naming and whatnot

* use readline for better command line UX

* add readline to CI workflow
2025-03-09 09:14:12 +01:00
Levi Neely 85e9eb3e77
forgot to remove a debug print (#115) 2025-03-08 14:07:39 +01:00
Levi Neely 1106028eba
WIP: Decompose namespaces and other cleanup (#94)
* papi namespace, remove some transient dependencies

* ptask namespace and cleanup

* pcache namespace and cleanup

* refactor pcallbacks into pqevent and ptevent

* pcryptofolder namespace and cleanup

* pcryptofolder missed a rename

* pdeflate namespace and cleanup

* pcrypto namespace

* pcrypto cleanup

* pcrypto consistency

* pcrypto more consistency

* pdevmon namespace and cleanup

* pdiff namespace

* pdownload namespace and cleanup

* pstatus namespace and cleanup

* pfileops namespace, fix header inlining abuse

* pfoldersync API, start chipping away at the psynclib monster

* pfscrypto namespace

* plocks namespace

* pnotify namespace

* pnotify formatting

* prpc namespace (got rid of confusing "overlay")

* pp2p namespace and cleanup

* ppagecache namespace

* ppassword namespace

* ppathstatus namespace

* prunratelimited => prun with throttling

* pscanner.c => psuggest namespace

* ptimer namespace

* ptools namespace (move to putil namespace someday)

* ptree namespace

* moved public link definitions into publiclinks.h, no clear namespace here though...

* pupload namespace

* formatting

* pcrc32c namespace
2025-03-01 22:21:54 +01:00
Levi Neely 2b6508a275
eliminate pcompat (#92)
* code cleanup and removal
- remove useless wrapper functions
- remove obfuscating typedefs
- code reorganization and reformatting
- function renames for clarification

* prepare pcompat.h for decomposition

* remove dead function psync_rebuild_icons, reorganize pfsstatic.c headers

* separate pdevice from pcompat

* reorg function prototypes, remove more compat stuff

* renamed mutex for brevity

* remove dead debug code; plibs version is more sophisticated

* remove dead code, stale comments

* moved psync_delete_cached_crypto_keys to psynclib

* forgot to remove the function prototype from pdiff.h

* reorganized functions by accessibility, inlined useless psync_cache_contacts function

* remove unused header

* ppath object decomposed from pcompat

* ppath namespace

* ppath struct renames

* ppath final cleanup

* refactor, remove compatibility code

* format

* prun decomposed from pcompat for thread mgmt

* slim down prun.c

* remove useless comments

* move pagesize to pmemlock, pmemlock namespace

* pmem decomposed from pcompat, pmem namespace

* decompose psys from pcompat

* fix psys function names, remove more compatibility junk

* prand decomposed from pcompat

* psock decomposed from pcompat

* types renamed for ns consistency, brevity

* cleanup psock, remove unused proxy code

* remove unused headers psock

* putil for utility macros and functions

* forgot to move some psock constants

* move time macro to psys

* eliminate pcompat completely; new pfile namespace

* cleanup
- moved thread name definition
- explicit externs in implementation instead of headers for now
- better extern name
2025-02-28 22:47:32 +01:00
Levi Neely 5ecece7547
#64 simplify messaging and #71 deserialize failed (#88)
* small lsp error fixes

* simplify messaging and remove serialize/deserialize
- messaging is reverted to API requests and responses only
- remove serialize and deserialize functions
- implement shared memory (pshm) for overlay callback return values

* fix memory leak

Fix #64 and #71
2025-02-25 09:46:46 +01:00
FloFri 5a5dec975f
Add basic 2FA (code from https://github.com/pcloudcom/console-client/pull/163) (#86)
Co-authored-by: Florian Friedrich <friedrich@procster.de>
2025-01-12 17:34:39 +01:00
Levi Neely c687484707
Implement add, remove, and list sync folder commands (#59)
* list_sync_folders seems to work

* list_sync_folders breaks with sync folders in db; message size problem?

* fixed sync folders list

* fix double free and buffer overflow

* wip: add and remove sync folder; need to fix request->value in instance_thread

* remove comment

* removed pclsync_lib_c.{h,cpp}

* cleanup get_answer_to_request

* update comment

* changed reply struct to type response containing payload and msg

* cleanup

* refactored overlay_client, and poverlay; added docs

* better naming

* sock path in poverlay_protocol.h

* clarify comment

* refactor psync_overlay_get_response

* clarifications

* response_messages work with and without payload

* removed commented code

* syncadd works, but i need to fix the error handling

* remove sync folder

* remove commented code, change printfs to debug or simply remove

* removed unnecessary print

* removed more prints

* removed dead var

* update error handling in add_sync_folder, fix memleak

* simplified finalize
2024-08-25 21:54:41 +02:00
Levi Neely ed06599b7d
Bug fixes, build containers, minor updates (#54)
* openssl is a dependency

* openssl is not a real dpendency

* static link by default

* wip: build containers

* rm comments

* build containers, fix DEADLYSIGNAL on run with no args

* debug tools in deb/ubuntu, fix #44

* no slack container :(

* ensure P_LINUX_OS always defined

* error check fuse unmount ops

* fsanitize=thread temporarily

* undo

* clang static analysis

* fix dead assignment

* makefile quality of life stuff

* make install and uninstall; ignore scan results

* addressed dead assignments from static analysis

* debug and release builds possible

* gcc still default compiler

* debug symbols on debug build only
2024-08-20 22:00:38 +02:00
Levi Neely 8d3c496754
memleak fixes (#40)
* fixed 2 of the 3 memory leaks

* fixed more memleaks

* more leaks

* another one

* BUGHUNT mode to look for more mem leaks
2024-08-15 23:06:55 +02:00
Levi Neely bec97722cd
reorg source tree, link system mbedtls (#38)
* reorg source tree, link system mbedtls

* remove cmakelists.txt

* update ci workflow

* -Werror to -Wall

* libpcloudcc_so in makefile

* single makefile works

* removed extra makefiles
2024-08-15 19:49:57 +02:00
Levi Neely 7801d1af76 more reorganization 2024-06-27 22:32:13 +02:00
Levi Neely 2a01c043ff simplified directory structure 2024-06-27 22:05:44 +02:00