Skip to content

Use backslash for path displayed on Windows #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: latestw_all
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions pathnames.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
* called by a name other than "ssh" or "Secure Shell".
*/

#ifdef WINDOWS
#define _PATH_SEPARATOR "\\"
#define HOME_ENV_VAR "USERPROFILE"
#else
#define _PATH_SEPARATOR "/"
#define HOME_ENV_VAR "HOME"
#endif

#define ETCDIR "/etc"

#ifndef SSHDIR
Expand Down Expand Up @@ -64,29 +72,29 @@
* readable by anyone except the user him/herself, though this does not
* contain anything particularly secret.
*/
#define _PATH_SSH_USER_HOSTFILE "~/" _PATH_SSH_USER_DIR "/known_hosts"
#define _PATH_SSH_USER_HOSTFILE "~" _PATH_SEPARATOR _PATH_SSH_USER_DIR _PATH_SEPARATOR "known_hosts"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, a tab character was used here to align the text, but this did not work consistently across the editor and the terminal (the following line did not align in git diff). I replaced it with spaces so that it is consistent across viewing environments.

/* backward compat for protocol 2 */
#define _PATH_SSH_USER_HOSTFILE2 "~/" _PATH_SSH_USER_DIR "/known_hosts2"
#define _PATH_SSH_USER_HOSTFILE2 "~" _PATH_SEPARATOR _PATH_SSH_USER_DIR _PATH_SEPARATOR "/known_hosts2"

/*
* Name of the default file containing client-side authentication key. This
* file should only be readable by the user him/herself.
*/
#define _PATH_SSH_CLIENT_ID_DSA _PATH_SSH_USER_DIR "/id_dsa"
#define _PATH_SSH_CLIENT_ID_ECDSA _PATH_SSH_USER_DIR "/id_ecdsa"
#define _PATH_SSH_CLIENT_ID_RSA _PATH_SSH_USER_DIR "/id_rsa"
#define _PATH_SSH_CLIENT_ID_ED25519 _PATH_SSH_USER_DIR "/id_ed25519"
#define _PATH_SSH_CLIENT_ID_XMSS _PATH_SSH_USER_DIR "/id_xmss"
#define _PATH_SSH_CLIENT_ID_ECDSA_SK _PATH_SSH_USER_DIR "/id_ecdsa_sk"
#define _PATH_SSH_CLIENT_ID_ED25519_SK _PATH_SSH_USER_DIR "/id_ed25519_sk"
#define _PATH_SSH_CLIENT_ID_DSA _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_dsa"
#define _PATH_SSH_CLIENT_ID_ECDSA _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_ecdsa"
#define _PATH_SSH_CLIENT_ID_RSA _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_rsa"
#define _PATH_SSH_CLIENT_ID_ED25519 _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_ed25519"
#define _PATH_SSH_CLIENT_ID_XMSS _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_xmss"
#define _PATH_SSH_CLIENT_ID_ECDSA_SK _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_ecdsa_sk"
#define _PATH_SSH_CLIENT_ID_ED25519_SK _PATH_SSH_USER_DIR _PATH_SEPARATOR "id_ed25519_sk"

/*
* Configuration file in user's home directory. This file need not be
* readable by anyone but the user him/herself, but does not contain anything
* particularly secret. If the user's home directory resides on an NFS
* volume where root is mapped to nobody, this may need to be world-readable.
*/
#define _PATH_SSH_USER_CONFFILE _PATH_SSH_USER_DIR "/config"
#define _PATH_SSH_USER_CONFFILE _PATH_SSH_USER_DIR _PATH_SEPARATOR "config"

/*
* File containing a list of those rsa keys that permit logging in as this
Expand All @@ -96,18 +104,18 @@
* may need to be world-readable. (This file is read by the daemon which is
* running as root.)
*/
#define _PATH_SSH_USER_PERMITTED_KEYS _PATH_SSH_USER_DIR "/authorized_keys"
#define _PATH_SSH_USER_PERMITTED_KEYS _PATH_SSH_USER_DIR _PATH_SEPARATOR "authorized_keys"

/* backward compat for protocol v2 */
#define _PATH_SSH_USER_PERMITTED_KEYS2 _PATH_SSH_USER_DIR "/authorized_keys2"
#define _PATH_SSH_USER_PERMITTED_KEYS2 _PATH_SSH_USER_DIR _PATH_SEPARATOR "authorized_keys2"

/*
* Per-user and system-wide ssh "rc" files. These files are executed with
* /bin/sh before starting the shell or command if they exist. They will be
* passed "proto cookie" as arguments if X11 forwarding with spoofing is in
* use. xauth will be run if neither of these exists.
*/
#define _PATH_SSH_USER_RC _PATH_SSH_USER_DIR "/rc"
#define _PATH_SSH_USER_RC _PATH_SSH_USER_DIR _PATH_SEPARATOR "rc"
#define _PATH_SSH_SYSTEM_RC SSHDIR "/sshrc"

/*
Expand Down
2 changes: 1 addition & 1 deletion ssh-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ main(int argc, char **argv)
}

for (i = 0; default_files[i]; i++) {
snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
snprintf(buf, sizeof(buf), "%s" _PATH_SEPARATOR "%s", pw->pw_dir,
default_files[i]);
if (stat(buf, &st) == -1)
continue;
Expand Down
2 changes: 1 addition & 1 deletion ssh-keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ ask_filename(struct passwd *pw, const char *prompt)
}
}
snprintf(identity_file, sizeof(identity_file),
"%s/%s", pw->pw_dir, name);
"%s" _PATH_SEPARATOR "%s", pw->pw_dir, name);
printf("%s (%s): ", prompt, identity_file);
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) == NULL)
Expand Down
2 changes: 1 addition & 1 deletion ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ process_config_files(const char *host_name, struct passwd *pw, int final_pass,
fatal("Can't open user config file %.100s: "
"%.100s", config, strerror(errno));
} else {
r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
r = snprintf(buf, sizeof buf, "%s" _PATH_SEPARATOR "%s", pw->pw_dir,
_PATH_SSH_USER_CONFFILE);
if (r > 0 && (size_t)r < sizeof(buf))
(void)read_config_file(buf, pw, host, host_name,
Expand Down
15 changes: 11 additions & 4 deletions sshconnect.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "ssherr.h"
#include "authfd.h"
#include "kex.h"
#include "pathnames.h"

struct sshkey *previous_host_key = NULL;

Expand Down Expand Up @@ -735,22 +736,28 @@ try_tilde_unexpand(const char *path)
char *home, *ret = NULL;
size_t l;

#ifndef WINDOWS
if (*path != '/')
return xstrdup(path);
if ((home = getenv("HOME")) == NULL || (l = strlen(home)) == 0)
#endif // !WINDOWS
if ((home = getenv(HOME_ENV_VAR)) == NULL || (l = strlen(home)) == 0)
return xstrdup(path);
if (strncmp(path, home, l) != 0)
return xstrdup(path);
/*
* ensure we have matched on a path boundary: either the $HOME that
* we just compared ends with a '/' or the next character of the path
* must be a '/'.
*
* *_PATH_SEPARATOR is used to obtain the char '/' from the path
* separator string. When compiled for Windows, this will be '\\'
* instead.
*/
if (home[l - 1] != '/' && path[l] != '/')
if (home[l - 1] != *_PATH_SEPARATOR && path[l] != *_PATH_SEPARATOR)
return xstrdup(path);
if (path[l] == '/')
if (path[l] == *_PATH_SEPARATOR)
l++;
xasprintf(&ret, "~/%s", path + l);
xasprintf(&ret, "~" _PATH_SEPARATOR "%s", path + l);
return ret;
}

Expand Down