Bug 2481 - Tilde expansion in -i option inconsistent
Summary: Tilde expansion in -i option inconsistent
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh (show other bugs)
Version: 6.6p1
Hardware: All Linux
: P5 trivial
Assignee: Darren Tucker
URL:
Keywords:
Depends on:
Blocks: V_7_2
  Show dependency treegraph
 
Reported: 2015-10-22 03:03 AEDT by nick
Modified: 2016-08-02 10:41 AEST (History)
2 users (show)

See Also:


Attachments
Expand tildes for arguments passed to -i. (605 bytes, patch)
2015-10-22 09:49 AEDT, Darren Tucker
no flags Details | Diff
Expand tildes for arguments passed to -i. (645 bytes, patch)
2015-10-22 10:08 AEDT, Darren Tucker
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description nick 2015-10-22 03:03:41 AEDT
Just noticed some odd behaviour in the identity file option with ~. If you don't leave a space between -i and the filename the ~ is not expanded and thus the file is not found. If you leave a space it works as expected.

ie This doesn't work:

    ssh -i~/key_file

This does:

    ssh -i ~/key_file


Excerpts from respective strace:
---snip--
stat("~/.ssh/keyfile", 0x7fff73320010) = -1 ENOENT (No such file or directory)
write(2, "Warning: Identity file ~/.ssh/ke"..., 82Warning: Identity file ~/.ssh/keyfile not accessible: No such file or directory.
---snip--

---snip--
stat("/home/ec2-user/.ssh/keyfile", {st_mode=S_IFREG|0600, st_size=1676, ...}) = 0
write(2, "usage: ssh [-1246AaCfgKkMNnqsTtV"..., 524usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
---snip--

Without a tilde it works with or without a space.
Comment 1 Darren Tucker 2015-10-22 09:37:33 AEDT
The reason for the discrepancy is that in this case, -i doesn't do tilde expansion, the shell does.  It only does it at the start of an argument, and when you combine the arguments like that it doesn't expand:

$ echo -i~/bin
-i~/bin
$ echo -i ~/bin
-i /home/dtucker/bin

This then falls foul of the file existence check inside the -i handling:

case 'i':
        if (stat(optarg, &st) < 0) {
              fprintf(stderr, "Warning: Identity file %s "
                            "not accessible: %s.\n", optarg,
                            strerror(errno));
                        break;
                }
                add_identity_file(&options, NULL, optarg, 1);
                break;

and the key doesn't get added before the call to tilde_expand_filename later.  We could probably fix this by adding a call to tilde_expand_filename in the -i handling.
Comment 2 Darren Tucker 2015-10-22 09:49:02 AEDT
Created attachment 2731 [details]
Expand tildes for arguments passed to -i.

Please try this patch.
Comment 3 Darren Tucker 2015-10-22 10:08:09 AEDT
Created attachment 2732 [details]
Expand tildes for arguments passed to -i.

oops, missed a line.
Comment 4 nick 2015-10-22 19:16:03 AEDT
Ah, didn't occur to me that is was the shell.

Yep, that patch seems to work for me.
Comment 5 Darren Tucker 2015-10-26 10:42:59 AEDT
(slightly simplified) patch applied and will be in the 7.2 release.  Thanks.
Comment 6 Damien Miller 2016-08-02 10:41:43 AEST
Close all resolved bugs after 7.3p1 release