@@ -76,6 +76,13 @@ pub fn managed_ssh_config_path() -> PathBuf {
7676 crate :: config:: config_dir ( ) . join ( "ssh_config" )
7777}
7878
79+ /// Extracts the hostname, optional user, and optional port from an [`SshTarget`].
80+ ///
81+ /// OpenSSH `Host` stanzas match against the hostname argument provided on the CLI
82+ /// without any `user@` prefix. Therefore, we split user from host and emit `User` as
83+ /// an inner directive in the managed host block. We also parse any explicit port
84+ /// (`-p <port>` in `extra_opts` or `:port` in destination) so that commands connecting
85+ /// to an alias on a custom port attach only to the matching master socket.
7986#[ cfg( unix) ]
8087fn target_details ( target : & SshTarget ) -> ( String , Option < String > , Option < u16 > ) {
8188 let ( dest_user, dest_host) = match target. dest . split_once ( '@' ) {
@@ -100,6 +107,8 @@ fn target_details(target: &SshTarget) -> (String, Option<String>, Option<u16>) {
100107 ( host, dest_user, port)
101108}
102109
110+ /// Renders an OpenSSH `Host` stanza configuring `ControlMaster auto` and `ControlPath`
111+ /// pointing to `orx`'s active session socket.
103112#[ cfg( unix) ]
104113fn render_managed_host_entry (
105114 host : & str ,
@@ -121,6 +130,11 @@ fn render_managed_host_entry(
121130 out
122131}
123132
133+ /// Updates or inserts a managed `Host` entry in the `orx`-managed SSH config.
134+ ///
135+ /// Preserves any other host blocks present in the config. The rendered configuration
136+ /// always ends with `Match all` to prevent directives in parent configs that include
137+ /// this file from leaking into the last `Host` block on OpenSSH versions prior to 8.4.
124138#[ cfg( unix) ]
125139pub ( crate ) fn upsert_managed_host_entry (
126140 content : & str ,
@@ -184,6 +198,12 @@ pub(crate) fn upsert_managed_host_entry(
184198 out
185199}
186200
201+ /// Checks whether an `Include` target in the user's SSH config points to the
202+ /// `orx` managed SSH config.
203+ ///
204+ /// Matches exact paths, tilde-prefixed paths (`~/.config/...`), and canonicalized
205+ /// paths to avoid false positives on unrelated files with similar basenames
206+ /// (e.g., `/etc/ssh/ssh_config`).
187207#[ cfg( unix) ]
188208fn is_matching_include ( target : & str , managed_path : & Path ) -> bool {
189209 let target = target. trim ( ) . trim_matches ( '"' ) . trim_matches ( '\'' ) ;
@@ -213,6 +233,15 @@ fn is_matching_include(target: &str, managed_path: &Path) -> bool {
213233 false
214234}
215235
236+ /// Ensures that `user_config_path` contains an `Include` directive for `managed_path`.
237+ ///
238+ /// To preserve global SSH configuration settings (e.g., options specified before any
239+ /// `Host` or `Match` blocks), the `Include` line is inserted immediately before the
240+ /// first `Host` or `Match` section, rather than at the very top of the file. If no
241+ /// sections exist, it is appended.
242+ ///
243+ /// If `user_config_path` is a symlink (common for managed dotfiles), the real canonical
244+ /// destination is written to directly so the symlink remains intact and atomic writes succeed.
216245#[ cfg( unix) ]
217246pub ( crate ) fn ensure_ssh_config_include_in (
218247 user_config_path : & Path ,
@@ -325,6 +354,9 @@ pub(crate) fn ensure_ssh_config_include() -> Result<bool> {
325354}
326355
327356/// Syncs the target's control socket to the managed SSH config.
357+ ///
358+ /// Serializes concurrent syncs using an exclusive lockfile (`managed_ssh_config.lock`)
359+ /// to prevent race conditions when multiple `orx` sessions initialize hosts in parallel.
328360#[ cfg( unix) ]
329361pub ( crate ) fn sync_managed_ssh_host ( target : & SshTarget ) -> Result < ( ) > {
330362 let lock_path = crate :: config:: config_dir ( ) . join ( "managed_ssh_config.lock" ) ;
@@ -1135,7 +1167,7 @@ mod tests {
11351167
11361168 #[ cfg( unix) ]
11371169 #[ test]
1138- fn ensure_ssh_config_include_in_prepends_and_is_idempotent ( ) {
1170+ fn ensure_ssh_config_include_in_inserts_and_is_idempotent ( ) {
11391171 let temp_dir =
11401172 std:: env:: temp_dir ( ) . join ( format ! ( "orx-ssh-include-test-{}" , uuid:: Uuid :: new_v4( ) ) ) ;
11411173 std:: fs:: create_dir_all ( & temp_dir) . unwrap ( ) ;
0 commit comments