summaryrefslogtreecommitdiff
path: root/rust/src/collaboration/remote.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2024-12-22 01:21:02 -0500
committerMason Reed <mason@vector35.com>2025-01-25 00:52:29 -0500
commit6a63c17853009ce7a0071458e39c140a09440230 (patch)
treead59ab3b356b0a2c60423e55f62c996891788788 /rust/src/collaboration/remote.rs
parentbfa1d409b2d5e734804a5fdb4c68c90a93712f1f (diff)
Rust refactor
Moves a bunch of stuff out of src/types.rs that did not belong: - Confidence - Variable - Function specific stuff - Refactored InstructionInfo, see the msp430 and riscv examples. - Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice - Fixed FunctionRecognizer leaking functions (see above) - Fixed some apis incorrectly returning Result where Option is clearer - Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example - Started to remove bad module level imports (importing std modules like mem all over the place) - Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example - Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example - General code cleanup, purposely did not run rustfmt, that will be a single seperate commit More rust cleanup - Fixed invalid views being able to invoke UB when dealing with databases - Cleaned up some helper code in dwarf_import - Fixed inverted is_null checks causing crashes! Oops! More rust cleanup Still a WIP, I think branch info is still invalid, need to figure out the issue there. - Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example - Added some more comments - Removed some "magic" functions like MLIL Function::ssa_variables There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue. More rust cleanup Trying to comment more TODO's as I go along. - Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency - Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops - Added more raw to wrapper conversions - Removed UB prone apis - Getting rid of more core module references, use std! - Removed improper Guard usage in array impls for wrapper types with no context - Untangling the UB of the Label api, still only half done :/ More rust cleanup - Misc formatting - Made Logger ref counted - Fixed leaking name of logger every time something was logged - Fixed the last (hopefully) of the unresolved labels - Simplified some code Fix leak in DebugInfo::AddType componentArray was never freed Add more HLIL related functions to rust More rust cleanup improve the CustomBinaryView init process Canonicalize path in `create_delete_empty` test Link core in rust When linking you must depend on the -sys crate. This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it. For more information see: - https://github.com/rust-lang/cargo/issues/9554#issuecomment-857882964 - https://github.com/oxidecomputer/omicron/pull/225 Remove vendored pdb crate Use cargo to manage the git repo ref instead Fix misc rustdoc warnings Move actual plugins out of `rust/examples` and into `plugins` This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised. More rust cleanup - More clarification on plugin/executable requirements - Made examples actually rust examples - Add Display impl for InstructionTextToken - Renamed feature "noexports" to "no_exports" Move under_construction.png to assets directory This really did bother me Remove unneeded `extern crate bindgen` Replace nop'd log::info with println We don't register a compatible log sink so they will just get sent into the void Move inline tests into tests directory This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests Format rust code Update rust ci Still need to add support for running tests in ci More rust cleanup - Architecture id's are now typed accordingly - Fix some clippy lints - Make instruction index public in LLIL - Removed useless helper functions - LLIL expressions and instruction indexes are now typed accordingly Generate binaryninjacore-sys documentation This should show binaryninjacore-sys alongside binaryninja crate More rust cleanup - Remove lazy_static dependency - Remove hacky impl Debug for Type and just use the display impl - Add more debug impls - Reorder some top level namespace items - Add first type test Remove unneeded script helper in rust api More rust cleanup - Added main thread handler api - Register a headless main thread handler by default in initialization - Refactor QualifiedName to be properly owned - Loosened some type constraints on some apis involving QualifiedName - Fixed some apis that were crashing due to incorrect param types - Removed extern crate cruft for log crate - Simplified headless initialization using more wrapper apis - Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at - Added rstest to manage headless init in unit tests - Added some more unit tests - Refactored demangler api to be more ergonomic - Fixed minidump plugin not building More rust cleanup - Fixup usage of QualifiedName in plugins - Make QualifiedName more usable Implement rust TypeParser fix Platform TypeParser related functions separate User and System implementations of TypeParserResult Implement rust TypeContainer More rust cleanup - Hopefully fixed the rust.yml CI - Added TypePrinter API (this is still WIP and will crash) - Added TypeParser API - Added TypeContainer API - More work around QualifiedName apis Oh your suppose to do this Add workflow_dispatch trigger to rust.yml More rust fixes - Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers - New InstructionTextToken API, complete with owned data, this still needs a lot of testing. - InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending - Added some misc Default and Debug impls - Updated TypePrinter and architectures to use new InstructionTextToken API Misc formatting changes More rust cleanup - Fixed MANY memory leaks (most due to QualifiedName) - Made StructureBuilder more builder and less structure - Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation - Added more Debug impl's - Added some more tests - Fixed TypeParserResult UB - Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing. - PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation - Misc doc code formatting Misc clippy lints and clippy CI Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com> Fix typo in rust CI Misc rust formatting Fix misc typos and add typos to rust CI Add cargo workspace This will help tooling and external contributors get a map of the rust crates within binaryninja-api More rust cleanup - Format all rust plugins - Fix some tests that were out of date - Simplify WARP tests to only binaries, building object files from source is a pain - Link to core in all rust plugins - Fix some memory leaks - Update warp insta snapshots - Fix some misc clippy lints Run rust tests in CI This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja Install missing wayland dependency in github CI Apparently its needed for linux file picker for the WARP integration Set the BINARYNINJADIR so rust can find binaryninjacore in CI The chances of this working are low Misc remove unused dependency Rust misc formatting fixes Improve initialization in rust headless scripts Provide sensible errors and validation to rust headless scripts, solves https://github.com/Vector35/binaryninja-api/issues/5796 Add BN_LICENSE environment variable to rust CI We pass the serial to download binary ninja, but we never provided the license for core initialization Fix typo More rust cleanup - Improved binary view initialization (see init_with_opts) - Allow floating license to free itself before initialization - Add initialization unit test - Add better Debug impls for some common types - Use Path api for opening binary views, this is not breaking as it uses the AsRef impl - Bump rayon dependency and constrain dependencies to x.x Update readme and include that directly in the rustdocs More rust documentation changes Add format comment to InitializationOptions::with_license Misc formatting and clippy lint allow More rust cleanup - Remove under_construction.png from the build.rs it has been removed - Use InstructionIndex in more places - Add missing PartialOrd and Ord impls for id types More rust cleanup - Make workflow cloning explicit - Add workflow tests - Add missing property string list getting for settings - Remove IntoActivityName (see https://github.com/Vector35/binaryninja-api/pull/6257) More rust cleanup This commit is half done Misc rust formatting More rust cleanup - Renamed common name conflictions (I will put my justification in the PR) - Fixed invalid instruction retrieval for LLIL - Added common aliases for llil function, instruction and expression types (see my comment in the PR) - Refactored the instruction retrieval for LLIL, MLIL and HLIL - Added instruction index types to MLIL and HLIL - Moved llil module to lowlevelil module (mlil and hlil will be moved as well) - Added preliminary LLIL unit testing Fix typos Misc clippy fixes More rust cleanup - Normalized modules - Split some code out into own files - Fixed some UB in type archive and projects - Improved API around type archives and projects substantially - Added ProgressExecutor abstraction for functions which have a progress callback - Improved background task documentation and added unit tests - Added worker thread api and unit tests - Moved some owned types to ref types, this is still not complete, but this is the path forward. - Add external location/library accessors to the binary view - Added some misc documentation - Replaced mod.rs with the module name source file Still need to normalize some paths and also update some documentation surrounding that change. Update some tests and examples Fix background task tests colliding We were creating multiple background tasks with the same progress text on multiple threads More rust cleanup - Fixed progress executor freeing itself after one iteration - Updated the last of the doc imports - Moved mainthread to main_thread - Made project creation and opening failable We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways. Move binary view initialization into function body for LLIL test Normalize test file names More rust cleanup - Updated README to clarify offline documentation - Refactored settings api - Added settings example to show dumping settings value and specific properties - Use the workspace to depend on binaryninja and binaryninjacore-sys - Remove binaryninjacore-sys as a workspace member (its not really required) Update workflow test to new settings api More rust cleanup - Rename Label to LowLevelILLabel - Update the functions label map automatically This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked. Misc clippy lints More rust cleanup - Get rid of RawFunctionViewType - Add better Debug impl for Function More rust cleanup - Fixed the documentation icon using local files (thank you @mkrasnitski) - Fixed labels being updated and overwriting the label location used to update the label map More rust cleanup - Added unit tests for MLIL and HLIL - "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes - Renamed CallingConvention to CoreCallingConvention and removed architecture generic - Renamed CallingConventionBase to CallingConvention - Simplified calling convention code and fixed some bugs with implicit registers - Added impl Debug to MLIL and HLIL instructions Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind. Misc clippy lint More rust cleanup - Allow calling conventions to be registered for multiple architectures - Swapped a unreachable statement to an unimplemented statement More rust cleanup - Fixed the issue with PDB types, this has caused me an insane amount of grief - Fixed LLIL visit_tree missing LLIL_LOAD expressions - Added LLIL visitor test - Made all WARP file pickers use the rfd crate Use the dev branch of Binary Ninja in rust CI Misc rust fmt More rust cleanup - Refactored BinaryReader and BinaryWriter - Added some warnings to high_level_il and medium_level_il modules that they are unstable - Add BinaryReader and BinaryWriter tests - Changed BinaryView len to return u64 (that is what the core returns) - Misc formatting changes - Remove extern uses in lib.rs Add impl Debug for BinaryReader and BinaryWriter Turn off broken tests Add more info to the rust README.md More rust cleanup - Make EdgeStyle type not wrap raw - Regression tests for WARP will run on all bins in the out dir now impl rust Collaboration and Remote API Fix typo Update collaboration API Makes collaboration more in line with the refactor. Still a lot of work to do. Namely still need: - Proper errors - _with_opts functions - More ergonomic api - Better connection procedure - Updated documentation - A LOT of unit tests - An example - Typed id's for everything (i dont want BnString as the id!!!) - NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has - Elimination of apis that are dumb helpers Separate out the rust testing and use pull_request_target pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job. To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run. More rust cleanup - Use GroupId instead of u64 - Use ProgressCallback in place of ProgressExecutor - Misc cleanup of FileMetadata - Add `save_to_path` and `save_to_accessor` to save modified binaries - Added binary_view unit tests - Added collaboration unit tests - Fixed a few issues with the collaboration apis - Renamed Command registration functions so that there is no import ambiguity - Split out RemoteUndoEntry - Collaboration apis now have a explicit `_with_progress` set of apis - Misc clippy lint fixes Fix some typos More rust cleanup - Add extra info to README.md - Refactor components api - Add components unit test Add testing and documentation to contributing section in README.md Fix misc doc comments
Diffstat (limited to 'rust/src/collaboration/remote.rs')
-rw-r--r--rust/src/collaboration/remote.rs959
1 files changed, 959 insertions, 0 deletions
diff --git a/rust/src/collaboration/remote.rs b/rust/src/collaboration/remote.rs
new file mode 100644
index 00000000..baf2ce80
--- /dev/null
+++ b/rust/src/collaboration/remote.rs
@@ -0,0 +1,959 @@
+use binaryninjacore_sys::*;
+use std::ffi::{c_char, c_void};
+use std::ptr::NonNull;
+
+use super::{sync, GroupId, RemoteGroup, RemoteProject, RemoteUser};
+
+use crate::binary_view::BinaryView;
+use crate::database::Database;
+use crate::enterprise;
+use crate::progress::{NoProgressCallback, ProgressCallback};
+use crate::project::Project;
+use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
+use crate::string::{BnStrCompatible, BnString};
+
+#[repr(transparent)]
+pub struct Remote {
+ pub(crate) handle: NonNull<BNRemote>,
+}
+
+impl Remote {
+ pub(crate) unsafe fn from_raw(handle: NonNull<BNRemote>) -> Self {
+ Self { handle }
+ }
+
+ pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNRemote>) -> Ref<Self> {
+ Ref::new(Self { handle })
+ }
+
+ /// Create a Remote and add it to the list of known remotes (saved to Settings)
+ pub fn new<N: BnStrCompatible, A: BnStrCompatible>(name: N, address: A) -> Ref<Self> {
+ let name = name.into_bytes_with_nul();
+ let address = address.into_bytes_with_nul();
+ let result = unsafe {
+ BNCollaborationCreateRemote(
+ name.as_ref().as_ptr() as *const c_char,
+ address.as_ref().as_ptr() as *const c_char,
+ )
+ };
+ unsafe { Self::ref_from_raw(NonNull::new(result).unwrap()) }
+ }
+
+ /// Get the Remote for a Database
+ pub fn get_for_local_database(database: &Database) -> Result<Option<Ref<Remote>>, ()> {
+ sync::get_remote_for_local_database(database)
+ }
+
+ /// Get the Remote for a Binary View
+ pub fn get_for_binary_view(bv: &BinaryView) -> Result<Option<Ref<Remote>>, ()> {
+ sync::get_remote_for_binary_view(bv)
+ }
+
+ /// Checks if the remote has pulled metadata like its id, etc.
+ pub fn has_loaded_metadata(&self) -> bool {
+ unsafe { BNRemoteHasLoadedMetadata(self.handle.as_ptr()) }
+ }
+
+ /// Gets the unique id. If metadata has not been pulled, it will be pulled upon calling this.
+ pub fn unique_id(&self) -> Result<BnString, ()> {
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+ let result = unsafe { BNRemoteGetUniqueId(self.handle.as_ptr()) };
+ assert!(!result.is_null());
+ Ok(unsafe { BnString::from_raw(result) })
+ }
+
+ /// Gets the name of the remote.
+ pub fn name(&self) -> BnString {
+ let result = unsafe { BNRemoteGetName(self.handle.as_ptr()) };
+ assert!(!result.is_null());
+ unsafe { BnString::from_raw(result) }
+ }
+
+ /// Gets the address of the remote.
+ pub fn address(&self) -> BnString {
+ let result = unsafe { BNRemoteGetAddress(self.handle.as_ptr()) };
+ assert!(!result.is_null());
+ unsafe { BnString::from_raw(result) }
+ }
+
+ /// Checks if the remote is connected.
+ pub fn is_connected(&self) -> bool {
+ unsafe { BNRemoteIsConnected(self.handle.as_ptr()) }
+ }
+
+ /// Gets the username used to connect to the remote.
+ pub fn username(&self) -> BnString {
+ let result = unsafe { BNRemoteGetUsername(self.handle.as_ptr()) };
+ assert!(!result.is_null());
+ unsafe { BnString::from_raw(result) }
+ }
+
+ /// Gets the token used to connect to the remote.
+ pub fn token(&self) -> BnString {
+ let result = unsafe { BNRemoteGetToken(self.handle.as_ptr()) };
+ assert!(!result.is_null());
+ unsafe { BnString::from_raw(result) }
+ }
+
+ /// Gets the server version. If metadata has not been pulled, it will be pulled upon calling this.
+ pub fn server_version(&self) -> Result<i32, ()> {
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+ Ok(unsafe { BNRemoteGetServerVersion(self.handle.as_ptr()) })
+ }
+
+ /// Gets the server build id. If metadata has not been pulled, it will be pulled upon calling this.
+ pub fn server_build_id(&self) -> Result<BnString, ()> {
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+ unsafe {
+ Ok(BnString::from_raw(BNRemoteGetServerBuildId(
+ self.handle.as_ptr(),
+ )))
+ }
+ }
+
+ /// Gets the list of supported authentication backends on the server.
+ /// If metadata has not been pulled, it will be pulled upon calling this.
+ pub fn auth_backends(&self) -> Result<(Array<BnString>, Array<BnString>), ()> {
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+
+ let mut backend_ids = std::ptr::null_mut();
+ let mut backend_names = std::ptr::null_mut();
+ let mut count = 0;
+ let success = unsafe {
+ BNRemoteGetAuthBackends(
+ self.handle.as_ptr(),
+ &mut backend_ids,
+ &mut backend_names,
+ &mut count,
+ )
+ };
+ success
+ .then(|| unsafe {
+ (
+ Array::new(backend_ids, count, ()),
+ Array::new(backend_names, count, ()),
+ )
+ })
+ .ok_or(())
+ }
+
+ /// Checks if the current user is an administrator.
+ pub fn is_admin(&self) -> Result<bool, ()> {
+ if !self.has_pulled_users() {
+ self.pull_users()?;
+ }
+ Ok(unsafe { BNRemoteIsAdmin(self.handle.as_ptr()) })
+ }
+
+ /// Checks if the remote is the same as the Enterprise License server.
+ pub fn is_enterprise(&self) -> Result<bool, ()> {
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+ Ok(unsafe { BNRemoteIsEnterprise(self.handle.as_ptr()) })
+ }
+
+ /// Loads metadata from the remote, including unique id and versions.
+ pub fn load_metadata(&self) -> Result<(), ()> {
+ let success = unsafe { BNRemoteLoadMetadata(self.handle.as_ptr()) };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Requests an authentication token using a username and password.
+ pub fn request_authentication_token<U: BnStrCompatible, P: BnStrCompatible>(
+ &self,
+ username: U,
+ password: P,
+ ) -> Option<BnString> {
+ let username = username.into_bytes_with_nul();
+ let password = password.into_bytes_with_nul();
+ let token = unsafe {
+ BNRemoteRequestAuthenticationToken(
+ self.handle.as_ptr(),
+ username.as_ref().as_ptr() as *const c_char,
+ password.as_ref().as_ptr() as *const c_char,
+ )
+ };
+ if token.is_null() {
+ None
+ } else {
+ Some(unsafe { BnString::from_raw(token) })
+ }
+ }
+
+ /// Connects to the Remote, loading metadata and optionally acquiring a token.
+ ///
+ /// Use [Remote::connect_with_opts] if you cannot otherwise automatically connect using enterprise.
+ pub fn connect(&self) -> Result<(), ()> {
+ // TODO: implement SecretsProvider
+ if self.is_enterprise()? && enterprise::is_server_authenticated() {
+ self.connect_with_opts(ConnectionOptions::from_enterprise()?)
+ } else {
+ // TODO: Make this error instead.
+ let username =
+ std::env::var("BN_ENTERPRISE_USERNAME").expect("No username for connection!");
+ let password =
+ std::env::var("BN_ENTERPRISE_PASSWORD").expect("No password for connection!");
+ let connection_opts = ConnectionOptions::new_with_password(username, password);
+ self.connect_with_opts(connection_opts)
+ }
+ }
+
+ // TODO: This needs docs and proper error.
+ pub fn connect_with_opts(&self, options: ConnectionOptions) -> Result<(), ()> {
+ // TODO: Should we make used load metadata first?
+ if !self.has_loaded_metadata() {
+ self.load_metadata()?;
+ }
+ let token = match options.token {
+ Some(token) => token,
+ None => {
+ // TODO: If password not defined than error saying no token or password
+ let password = options
+ .password
+ .expect("No password or token for connection!");
+ let token = self.request_authentication_token(&options.username, password);
+ // TODO: Error if None.
+ token.unwrap().to_string()
+ }
+ };
+ let username = options.username.into_bytes_with_nul();
+ let username_ptr = username.as_ptr() as *const c_char;
+ let token = token.into_bytes_with_nul();
+ let token_ptr = token.as_ptr() as *const c_char;
+ let success = unsafe { BNRemoteConnect(self.handle.as_ptr(), username_ptr, token_ptr) };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Disconnects from the remote.
+ pub fn disconnect(&self) -> Result<(), ()> {
+ let success = unsafe { BNRemoteDisconnect(self.handle.as_ptr()) };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Checks if the project has pulled the projects yet.
+ pub fn has_pulled_projects(&self) -> bool {
+ unsafe { BNRemoteHasPulledProjects(self.handle.as_ptr()) }
+ }
+
+ /// Checks if the project has pulled the groups yet.
+ pub fn has_pulled_groups(&self) -> bool {
+ unsafe { BNRemoteHasPulledGroups(self.handle.as_ptr()) }
+ }
+
+ /// Checks if the project has pulled the users yet.
+ pub fn has_pulled_users(&self) -> bool {
+ unsafe { BNRemoteHasPulledUsers(self.handle.as_ptr()) }
+ }
+
+ /// Gets the list of projects in this project.
+ ///
+ /// NOTE: If projects have not been pulled, they will be pulled upon calling this.
+ pub fn projects(&self) -> Result<Array<RemoteProject>, ()> {
+ if !self.has_pulled_projects() {
+ self.pull_projects()?;
+ }
+
+ let mut count = 0;
+ let value = unsafe { BNRemoteGetProjects(self.handle.as_ptr(), &mut count) };
+ if value.is_null() {
+ return Err(());
+ }
+ Ok(unsafe { Array::new(value, count, ()) })
+ }
+
+ /// Gets a specific project in the Remote by its id.
+ ///
+ /// NOTE: If projects have not been pulled, they will be pulled upon calling this.
+ pub fn get_project_by_id<S: BnStrCompatible>(
+ &self,
+ id: S,
+ ) -> Result<Option<Ref<RemoteProject>>, ()> {
+ if !self.has_pulled_projects() {
+ self.pull_projects()?;
+ }
+
+ let id = id.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteGetProjectById(self.handle.as_ptr(), id.as_ref().as_ptr() as *const c_char)
+ };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteProject::ref_from_raw(handle) }))
+ }
+
+ /// Gets a specific project in the Remote by its name.
+ ///
+ /// NOTE: If projects have not been pulled, they will be pulled upon calling this.
+ pub fn get_project_by_name<S: BnStrCompatible>(
+ &self,
+ name: S,
+ ) -> Result<Option<Ref<RemoteProject>>, ()> {
+ if !self.has_pulled_projects() {
+ self.pull_projects()?;
+ }
+
+ let name = name.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteGetProjectByName(
+ self.handle.as_ptr(),
+ name.as_ref().as_ptr() as *const c_char,
+ )
+ };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteProject::ref_from_raw(handle) }))
+ }
+
+ /// Pulls the list of projects from the Remote.
+ pub fn pull_projects(&self) -> Result<(), ()> {
+ self.pull_projects_with_progress(NoProgressCallback)
+ }
+
+ /// Pulls the list of projects from the Remote.
+ ///
+ /// # Arguments
+ ///
+ /// * `progress` - Function to call for progress updates
+ pub fn pull_projects_with_progress<F: ProgressCallback>(
+ &self,
+ mut progress: F,
+ ) -> Result<(), ()> {
+ let success = unsafe {
+ BNRemotePullProjects(
+ self.handle.as_ptr(),
+ Some(F::cb_progress_callback),
+ &mut progress as *mut F as *mut c_void,
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Creates a new project on the remote (and pull it).
+ ///
+ /// # Arguments
+ ///
+ /// * `name` - Project name
+ /// * `description` - Project description
+ pub fn create_project<N: BnStrCompatible, D: BnStrCompatible>(
+ &self,
+ name: N,
+ description: D,
+ ) -> Result<Ref<RemoteProject>, ()> {
+ // TODO: Do we want this?
+ // TODO: If you have not yet pulled projects you will have never filled the map you will be placing your
+ // TODO: New project in.
+ if !self.has_pulled_projects() {
+ self.pull_projects()?;
+ }
+ let name = name.into_bytes_with_nul();
+ let description = description.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteCreateProject(
+ self.handle.as_ptr(),
+ name.as_ref().as_ptr() as *const c_char,
+ description.as_ref().as_ptr() as *const c_char,
+ )
+ };
+ NonNull::new(value)
+ .map(|handle| unsafe { RemoteProject::ref_from_raw(handle) })
+ .ok_or(())
+ }
+
+ /// Create a new project on the remote from a local project.
+ pub fn import_local_project(&self, project: &Project) -> Option<Ref<RemoteProject>> {
+ self.import_local_project_with_progress(project, NoProgressCallback)
+ }
+
+ /// Create a new project on the remote from a local project.
+ pub fn import_local_project_with_progress<P: ProgressCallback>(
+ &self,
+ project: &Project,
+ mut progress: P,
+ ) -> Option<Ref<RemoteProject>> {
+ let value = unsafe {
+ BNRemoteImportLocalProject(
+ self.handle.as_ptr(),
+ project.handle.as_ptr(),
+ Some(P::cb_progress_callback),
+ &mut progress as *mut P as *mut c_void,
+ )
+ };
+ NonNull::new(value).map(|handle| unsafe { RemoteProject::ref_from_raw(handle) })
+ }
+
+ /// Pushes an updated Project object to the Remote.
+ ///
+ /// # Arguments
+ ///
+ /// * `project` - Project object which has been updated
+ /// * `extra_fields` - Extra HTTP fields to send with the update
+ pub fn push_project<I, K, V>(&self, project: &RemoteProject, extra_fields: I) -> Result<(), ()>
+ where
+ I: Iterator<Item = (K, V)>,
+ K: BnStrCompatible,
+ V: BnStrCompatible,
+ {
+ let (keys, values): (Vec<_>, Vec<_>) = extra_fields
+ .into_iter()
+ .map(|(k, v)| (k.into_bytes_with_nul(), v.into_bytes_with_nul()))
+ .unzip();
+ let mut keys_raw = keys
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect::<Vec<_>>();
+ let mut values_raw = values
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect::<Vec<_>>();
+
+ let success = unsafe {
+ BNRemotePushProject(
+ self.handle.as_ptr(),
+ project.handle.as_ptr(),
+ keys_raw.as_mut_ptr(),
+ values_raw.as_mut_ptr(),
+ keys_raw.len(),
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Deletes a project from the remote.
+ pub fn delete_project(&self, project: &RemoteProject) -> Result<(), ()> {
+ let success =
+ unsafe { BNRemoteDeleteProject(self.handle.as_ptr(), project.handle.as_ptr()) };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Gets the list of groups in this project.
+ ///
+ /// If groups have not been pulled, they will be pulled upon calling this.
+ /// This function is only available to accounts with admin status on the Remote.
+ pub fn groups(&self) -> Result<Array<RemoteGroup>, ()> {
+ if !self.has_pulled_groups() {
+ self.pull_groups()?;
+ }
+
+ let mut count = 0;
+ let value = unsafe { BNRemoteGetGroups(self.handle.as_ptr(), &mut count) };
+ if value.is_null() {
+ return Err(());
+ }
+ Ok(unsafe { Array::new(value, count, ()) })
+ }
+
+ /// Gets a specific group in the Remote by its id.
+ ///
+ /// If groups have not been pulled, they will be pulled upon calling this.
+ /// This function is only available to accounts with admin status on the Remote.
+ pub fn get_group_by_id(&self, id: GroupId) -> Result<Option<Ref<RemoteGroup>>, ()> {
+ if !self.has_pulled_groups() {
+ self.pull_groups()?;
+ }
+
+ let value = unsafe { BNRemoteGetGroupById(self.handle.as_ptr(), id.0) };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteGroup::ref_from_raw(handle) }))
+ }
+
+ /// Gets a specific group in the Remote by its name.
+ ///
+ /// If groups have not been pulled, they will be pulled upon calling this.
+ /// This function is only available to accounts with admin status on the Remote.
+ pub fn get_group_by_name<S: BnStrCompatible>(
+ &self,
+ name: S,
+ ) -> Result<Option<Ref<RemoteGroup>>, ()> {
+ if !self.has_pulled_groups() {
+ self.pull_groups()?;
+ }
+
+ let name = name.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteGetGroupByName(
+ self.handle.as_ptr(),
+ name.as_ref().as_ptr() as *const c_char,
+ )
+ };
+
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteGroup::ref_from_raw(handle) }))
+ }
+
+ /// Searches for groups in the Remote with a given prefix.
+ ///
+ /// # Arguments
+ ///
+ /// * `prefix` - Prefix of name for groups
+ pub fn search_groups<S: BnStrCompatible>(
+ &self,
+ prefix: S,
+ ) -> Result<(Array<GroupId>, Array<BnString>), ()> {
+ let prefix = prefix.into_bytes_with_nul();
+ let mut count = 0;
+ let mut group_ids = std::ptr::null_mut();
+ let mut group_names = std::ptr::null_mut();
+
+ let success = unsafe {
+ BNRemoteSearchGroups(
+ self.handle.as_ptr(),
+ prefix.as_ref().as_ptr() as *const c_char,
+ &mut group_ids,
+ &mut group_names,
+ &mut count,
+ )
+ };
+ if !success {
+ return Err(());
+ }
+ Ok(unsafe {
+ (
+ Array::new(group_ids, count, ()),
+ Array::new(group_names, count, ()),
+ )
+ })
+ }
+
+ /// Pulls the list of groups from the Remote.
+ /// This function is only available to accounts with admin status on the Remote.
+ pub fn pull_groups(&self) -> Result<(), ()> {
+ self.pull_groups_with_progress(NoProgressCallback)
+ }
+
+ /// Pulls the list of groups from the Remote.
+ /// This function is only available to accounts with admin status on the Remote.
+ ///
+ /// # Arguments
+ ///
+ /// * `progress` - Function to call for progress updates
+ pub fn pull_groups_with_progress<F: ProgressCallback>(
+ &self,
+ mut progress: F,
+ ) -> Result<(), ()> {
+ let success = unsafe {
+ BNRemotePullGroups(
+ self.handle.as_ptr(),
+ Some(F::cb_progress_callback),
+ &mut progress as *mut F as *mut c_void,
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Creates a new group on the remote (and pull it).
+ /// This function is only available to accounts with admin status on the Remote.
+ ///
+ /// # Arguments
+ ///
+ /// * `name` - Group name
+ /// * `usernames` - List of usernames of users in the group
+ pub fn create_group<N, I>(&self, name: N, usernames: I) -> Result<Ref<RemoteGroup>, ()>
+ where
+ N: BnStrCompatible,
+ I: IntoIterator,
+ I::Item: BnStrCompatible,
+ {
+ let name = name.into_bytes_with_nul();
+ let usernames: Vec<_> = usernames
+ .into_iter()
+ .map(|s| s.into_bytes_with_nul())
+ .collect();
+ let mut username_ptrs: Vec<_> = usernames
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect();
+
+ let value = unsafe {
+ BNRemoteCreateGroup(
+ self.handle.as_ptr(),
+ name.as_ref().as_ptr() as *const c_char,
+ username_ptrs.as_mut_ptr(),
+ username_ptrs.len(),
+ )
+ };
+ NonNull::new(value)
+ .map(|handle| unsafe { RemoteGroup::ref_from_raw(handle) })
+ .ok_or(())
+ }
+
+ /// Pushes an updated Group object to the Remote.
+ /// This function is only available to accounts with admin status on the Remote.
+ ///
+ /// # Arguments
+ ///
+ /// * `group` - Group object which has been updated
+ /// * `extra_fields` - Extra HTTP fields to send with the update
+ pub fn push_group<I, K, V>(&self, group: &RemoteGroup, extra_fields: I) -> Result<(), ()>
+ where
+ I: IntoIterator<Item = (K, V)>,
+ K: BnStrCompatible,
+ V: BnStrCompatible,
+ {
+ let (keys, values): (Vec<_>, Vec<_>) = extra_fields
+ .into_iter()
+ .map(|(k, v)| (k.into_bytes_with_nul(), v.into_bytes_with_nul()))
+ .unzip();
+ let mut keys_raw: Vec<_> = keys
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect();
+ let mut values_raw: Vec<_> = values
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect();
+
+ let success = unsafe {
+ BNRemotePushGroup(
+ self.handle.as_ptr(),
+ group.handle.as_ptr(),
+ keys_raw.as_mut_ptr(),
+ values_raw.as_mut_ptr(),
+ keys.len(),
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Deletes the specified group from the remote.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ ///
+ /// # Arguments
+ ///
+ /// * `group` - Reference to the group to delete.
+ pub fn delete_group(&self, group: &RemoteGroup) -> Result<(), ()> {
+ let success = unsafe { BNRemoteDeleteGroup(self.handle.as_ptr(), group.handle.as_ptr()) };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Retrieves the list of users in the project.
+ ///
+ /// NOTE: If users have not been pulled, they will be pulled upon calling this.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ pub fn users(&self) -> Result<Array<RemoteUser>, ()> {
+ if !self.has_pulled_users() {
+ self.pull_users()?;
+ }
+ let mut count = 0;
+ let value = unsafe { BNRemoteGetUsers(self.handle.as_ptr(), &mut count) };
+ if value.is_null() {
+ return Err(());
+ }
+ Ok(unsafe { Array::new(value, count, ()) })
+ }
+
+ /// Retrieves a specific user in the project by their ID.
+ ///
+ /// NOTE: If users have not been pulled, they will be pulled upon calling this.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ ///
+ /// # Arguments
+ ///
+ /// * `id` - The identifier of the user to retrieve.
+ pub fn get_user_by_id<S: BnStrCompatible>(&self, id: S) -> Result<Option<Ref<RemoteUser>>, ()> {
+ if !self.has_pulled_users() {
+ self.pull_users()?;
+ }
+ let id = id.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteGetUserById(self.handle.as_ptr(), id.as_ref().as_ptr() as *const c_char)
+ };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteUser::ref_from_raw(handle) }))
+ }
+
+ /// Retrieves a specific user in the project by their username.
+ ///
+ /// NOTE: If users have not been pulled, they will be pulled upon calling this.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ ///
+ /// # Arguments
+ ///
+ /// * `username` - The username of the user to retrieve.
+ pub fn get_user_by_username<S: BnStrCompatible>(
+ &self,
+ username: S,
+ ) -> Result<Option<Ref<RemoteUser>>, ()> {
+ if !self.has_pulled_users() {
+ self.pull_users()?;
+ }
+ let username = username.into_bytes_with_nul();
+ let value = unsafe {
+ BNRemoteGetUserByUsername(
+ self.handle.as_ptr(),
+ username.as_ref().as_ptr() as *const c_char,
+ )
+ };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteUser::ref_from_raw(handle) }))
+ }
+
+ /// Retrieves the user object for the currently connected user.
+ ///
+ /// NOTE: If users have not been pulled, they will be pulled upon calling this.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ pub fn current_user(&self) -> Result<Option<Ref<RemoteUser>>, ()> {
+ if !self.has_pulled_users() {
+ self.pull_users()?;
+ }
+ let value = unsafe { BNRemoteGetCurrentUser(self.handle.as_ptr()) };
+ Ok(NonNull::new(value).map(|handle| unsafe { RemoteUser::ref_from_raw(handle) }))
+ }
+
+ /// Searches for users in the project with a given prefix.
+ ///
+ /// # Arguments
+ ///
+ /// * `prefix` - The prefix to search for in usernames.
+ pub fn search_users<S: BnStrCompatible>(
+ &self,
+ prefix: S,
+ ) -> Result<(Array<BnString>, Array<BnString>), ()> {
+ let prefix = prefix.into_bytes_with_nul();
+ let mut count = 0;
+ let mut user_ids = std::ptr::null_mut();
+ let mut usernames = std::ptr::null_mut();
+ let success = unsafe {
+ BNRemoteSearchUsers(
+ self.handle.as_ptr(),
+ prefix.as_ref().as_ptr() as *const c_char,
+ &mut user_ids,
+ &mut usernames,
+ &mut count,
+ )
+ };
+
+ if !success {
+ return Err(());
+ }
+ assert!(!user_ids.is_null());
+ assert!(!usernames.is_null());
+ Ok(unsafe {
+ (
+ Array::new(user_ids, count, ()),
+ Array::new(usernames, count, ()),
+ )
+ })
+ }
+
+ /// Pulls the list of users from the remote.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote.
+ /// Non-admin accounts attempting to call this function will pull an empty list of users.
+ pub fn pull_users(&self) -> Result<(), ()> {
+ self.pull_users_with_progress(NoProgressCallback)
+ }
+
+ /// Pulls the list of users from the remote.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote.
+ /// Non-admin accounts attempting to call this function will pull an empty list of users.
+ ///
+ /// # Arguments
+ ///
+ /// * `progress` - Closure called to report progress. Takes current and total progress counts.
+ pub fn pull_users_with_progress<P: ProgressCallback>(&self, mut progress: P) -> Result<(), ()> {
+ let success = unsafe {
+ BNRemotePullUsers(
+ self.handle.as_ptr(),
+ Some(P::cb_progress_callback),
+ &mut progress as *mut P as *mut c_void,
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ /// Creates a new user on the remote and returns a reference to the created user.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ ///
+ /// # Arguments
+ ///
+ /// * Various details about the new user to be created.
+ pub fn create_user<U: BnStrCompatible, E: BnStrCompatible, P: BnStrCompatible>(
+ &self,
+ username: U,
+ email: E,
+ is_active: bool,
+ password: P,
+ group_ids: &[u64],
+ user_permission_ids: &[u64],
+ ) -> Result<Ref<RemoteUser>, ()> {
+ let username = username.into_bytes_with_nul();
+ let email = email.into_bytes_with_nul();
+ let password = password.into_bytes_with_nul();
+
+ let value = unsafe {
+ BNRemoteCreateUser(
+ self.handle.as_ptr(),
+ username.as_ref().as_ptr() as *const c_char,
+ email.as_ref().as_ptr() as *const c_char,
+ is_active,
+ password.as_ref().as_ptr() as *const c_char,
+ group_ids.as_ptr(),
+ group_ids.len(),
+ user_permission_ids.as_ptr(),
+ user_permission_ids.len(),
+ )
+ };
+ NonNull::new(value)
+ .map(|handle| unsafe { RemoteUser::ref_from_raw(handle) })
+ .ok_or(())
+ }
+
+ /// Pushes updates to the specified user on the remote.
+ ///
+ /// NOTE: This function is only available to accounts with admin status on the Remote
+ ///
+ /// # Arguments
+ ///
+ /// * `user` - Reference to the `RemoteUser` object to push.
+ /// * `extra_fields` - Optional extra fields to send with the update.
+ pub fn push_user<I, K, V>(&self, user: &RemoteUser, extra_fields: I) -> Result<(), ()>
+ where
+ I: Iterator<Item = (K, V)>,
+ K: BnStrCompatible,
+ V: BnStrCompatible,
+ {
+ let (keys, values): (Vec<_>, Vec<_>) = extra_fields
+ .into_iter()
+ .map(|(k, v)| (k.into_bytes_with_nul(), v.into_bytes_with_nul()))
+ .unzip();
+ let mut keys_raw: Vec<_> = keys
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect();
+ let mut values_raw: Vec<_> = values
+ .iter()
+ .map(|s| s.as_ref().as_ptr() as *const c_char)
+ .collect();
+ let success = unsafe {
+ BNRemotePushUser(
+ self.handle.as_ptr(),
+ user.handle.as_ptr(),
+ keys_raw.as_mut_ptr(),
+ values_raw.as_mut_ptr(),
+ keys_raw.len(),
+ )
+ };
+ success.then_some(()).ok_or(())
+ }
+
+ // TODO identify the request and ret type of this function, it seems to use a C++ implementation of
+ // HTTP requests, composed mostly of `std:vector`.
+ //pub fn request(&self) {
+ // unsafe { BNRemoteRequest(self.handle.as_ptr(), todo!(), todo!()) }
+ //}
+}
+
+impl PartialEq for Remote {
+ fn eq(&self, other: &Self) -> bool {
+ // don't pull metadata if we hand't yet
+ if !self.has_loaded_metadata() || other.has_loaded_metadata() {
+ self.address() == other.address()
+ } else if let Some((slf, oth)) = self.unique_id().ok().zip(other.unique_id().ok()) {
+ slf == oth
+ } else {
+ // falback to comparing address
+ self.address() == other.address()
+ }
+ }
+}
+impl Eq for Remote {}
+
+impl ToOwned for Remote {
+ type Owned = Ref<Self>;
+
+ fn to_owned(&self) -> Self::Owned {
+ unsafe { RefCountable::inc_ref(self) }
+ }
+}
+
+unsafe impl RefCountable for Remote {
+ unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
+ Ref::new(Self {
+ handle: NonNull::new(BNNewRemoteReference(handle.handle.as_ptr())).unwrap(),
+ })
+ }
+
+ unsafe fn dec_ref(handle: &Self) {
+ BNFreeRemote(handle.handle.as_ptr());
+ }
+}
+
+impl CoreArrayProvider for Remote {
+ type Raw = *mut BNRemote;
+ type Context = ();
+ type Wrapped<'a> = Guard<'a, Self>;
+}
+
+unsafe impl CoreArrayProviderInner for Remote {
+ unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+ BNFreeRemoteList(raw, count)
+ }
+
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
+ let raw_ptr = NonNull::new(*raw).unwrap();
+ Guard::new(Self::from_raw(raw_ptr), context)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct ConnectionOptions {
+ pub username: String,
+ /// Provide this if you want to authenticate with a password.
+ pub password: Option<String>,
+ /// Provide this if you want to authenticate with a token.
+ ///
+ /// If you do not have a token you can use [ConnectionOptions::self].
+ pub token: Option<String>,
+}
+
+impl ConnectionOptions {
+ pub fn new_with_token(username: String, token: String) -> Self {
+ Self {
+ username,
+ token: Some(token),
+ password: None,
+ }
+ }
+
+ pub fn new_with_password(username: String, password: String) -> Self {
+ Self {
+ username,
+ token: None,
+ password: Some(password),
+ }
+ }
+
+ pub fn with_token(self, token: String) -> Self {
+ Self {
+ token: Some(token),
+ ..self
+ }
+ }
+
+ pub fn with_password(self, token: String) -> Self {
+ Self {
+ token: Some(token),
+ ..self
+ }
+ }
+
+ pub fn from_enterprise() -> Result<Self, ()> {
+ // TODO: Check if enterprise is initialized and error if not.
+ let username = enterprise::server_username();
+ let token = enterprise::server_token();
+ Ok(Self::new_with_token(
+ username.to_string(),
+ token.to_string(),
+ ))
+ }
+
+ // TODO: from_secrets_provider
+}