1a02364b5107a4125ea3cb76fcdb6beabaebf3be.diff 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. diff --git a/include/libssh/config_parser.h b/include/libssh/config_parser.h
  2. index a7dd42a2c8016789a2dad1c409447da11603a0f0..ca353432b8139e5be334f1b815e910ca03562c84 100644
  3. --- a/include/libssh/config_parser.h
  4. +++ b/include/libssh/config_parser.h
  5. @@ -30,6 +30,8 @@
  6. extern "C" {
  7. #endif
  8. +#include <stdbool.h>
  9. +
  10. char *ssh_config_get_cmd(char **str);
  11. char *ssh_config_get_token(char **str);
  12. @@ -49,14 +51,17 @@ int ssh_config_get_yesno(char **str, int notfound);
  13. * be stored or NULL if we do not care about the result.
  14. * @param[out] port Pointer to the location, where the new port will
  15. * be stored or NULL if we do not care about the result.
  16. + * @param[in] ignore_port Set to true if the we should not attempt to parse
  17. + * port number.
  18. *
  19. * @returns SSH_OK if the provided string is in format of SSH URI,
  20. * SSH_ERROR on failure
  21. */
  22. int ssh_config_parse_uri(const char *tok,
  23. - char **username,
  24. - char **hostname,
  25. - char **port);
  26. + char **username,
  27. + char **hostname,
  28. + char **port,
  29. + bool ignore_port);
  30. #ifdef __cplusplus
  31. }
  32. diff --git a/src/config.c b/src/config.c
  33. index c5c40125ac243e4d137601d340f91bb4eba8c973..273db7c8c477dd435b8310e8b7784cb0dc578e0a 100644
  34. --- a/src/config.c
  35. +++ b/src/config.c
  36. @@ -464,7 +464,7 @@ ssh_config_parse_proxy_jump(ssh_session session, const char *s, bool do_parsing)
  37. }
  38. if (parse_entry) {
  39. /* We actually care only about the first item */
  40. - rv = ssh_config_parse_uri(cp, &username, &hostname, &port);
  41. + rv = ssh_config_parse_uri(cp, &username, &hostname, &port, false);
  42. /* The rest of the list needs to be passed on */
  43. if (endp != NULL) {
  44. next = strdup(endp + 1);
  45. @@ -475,7 +475,7 @@ ssh_config_parse_proxy_jump(ssh_session session, const char *s, bool do_parsing)
  46. }
  47. } else {
  48. /* The rest is just sanity-checked to avoid failures later */
  49. - rv = ssh_config_parse_uri(cp, NULL, NULL, NULL);
  50. + rv = ssh_config_parse_uri(cp, NULL, NULL, NULL, false);
  51. }
  52. if (rv != SSH_OK) {
  53. goto out;
  54. diff --git a/src/config_parser.c b/src/config_parser.c
  55. index b8b94611af54f5524e16afc14f2517c3dbadfdd3..d4b2d2c3b74db7ff564ebfdfd09ec31a9ae485be 100644
  56. --- a/src/config_parser.c
  57. +++ b/src/config_parser.c
  58. @@ -162,9 +162,10 @@ int ssh_config_get_yesno(char **str, int notfound)
  59. }
  60. int ssh_config_parse_uri(const char *tok,
  61. - char **username,
  62. - char **hostname,
  63. - char **port)
  64. + char **username,
  65. + char **hostname,
  66. + char **port,
  67. + bool ignore_port)
  68. {
  69. char *endp = NULL;
  70. long port_n;
  71. @@ -210,12 +211,17 @@ int ssh_config_parse_uri(const char *tok,
  72. if (endp == NULL) {
  73. goto error;
  74. }
  75. - } else {
  76. - /* Hostnames or aliases expand to the last colon or to the end */
  77. + } else if (!ignore_port) {
  78. + /* Hostnames or aliases expand to the last colon (if port is requested)
  79. + * or to the end */
  80. endp = strrchr(tok, ':');
  81. if (endp == NULL) {
  82. endp = strchr(tok, '\0');
  83. }
  84. + } else {
  85. + /* If no port is requested, expand to the end of line
  86. + * (to accommodate the IPv6 addresses) */
  87. + endp = strchr(tok, '\0');
  88. }
  89. if (tok == endp) {
  90. /* Zero-length hostnames are not valid */
  91. diff --git a/src/options.c b/src/options.c
  92. index 3851145557a9ef6fd21a664e5bce7506143873c5..b3ecffe15f23e51c5217a58a28dc15256666efa6 100644
  93. --- a/src/options.c
  94. +++ b/src/options.c
  95. @@ -516,17 +516,11 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
  96. ssh_set_error_invalid(session);
  97. return -1;
  98. } else {
  99. - char *username = NULL, *hostname = NULL, *port = NULL;
  100. - rc = ssh_config_parse_uri(value, &username, &hostname, &port);
  101. + char *username = NULL, *hostname = NULL;
  102. + rc = ssh_config_parse_uri(value, &username, &hostname, NULL, true);
  103. if (rc != SSH_OK) {
  104. return -1;
  105. }
  106. - if (port != NULL) {
  107. - SAFE_FREE(username);
  108. - SAFE_FREE(hostname);
  109. - SAFE_FREE(port);
  110. - return -1;
  111. - }
  112. if (username != NULL) {
  113. SAFE_FREE(session->opts.username);
  114. session->opts.username = username;