sessprep.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * sessprep.c: centralise some preprocessing done on Conf objects
  3. * before launching them.
  4. */
  5. #include "putty.h"
  6. void prepare_session(Conf *conf)
  7. {
  8. char *hostbuf = dupstr(conf_get_str(conf, CONF_host));
  9. char *host = hostbuf;
  10. char *p, *q;
  11. /*
  12. * Trim leading whitespace from the hostname.
  13. */
  14. host += strspn(host, " \t");
  15. /*
  16. * See if host is of the form user@host, and separate out the
  17. * username if so.
  18. */
  19. if (host[0] != '\0') {
  20. /*
  21. * Use strrchr, in case the _username_ in turn is of the form
  22. * user@host, which has been known.
  23. */
  24. char *atsign = strrchr(host, '@');
  25. if (atsign) {
  26. *atsign = '\0';
  27. conf_set_str(conf, CONF_username, host);
  28. host = atsign + 1;
  29. }
  30. }
  31. /*
  32. * Trim a colon suffix off the hostname if it's there, and discard
  33. * the text after it.
  34. *
  35. * The exact reason why we _ignore_ this text, rather than
  36. * treating it as a port number, is unfortunately lost in the
  37. * mists of history: the commit which originally introduced this
  38. * change on 2001-05-06 was clear on _what_ it was doing but
  39. * didn't bother to explain _why_. But I [SGT, 2017-12-03] suspect
  40. * it has to do with priority order: what should a saved session
  41. * do if its CONF_host contains 'server.example.com:123' and its
  42. * CONF_port contains 456? If CONF_port contained the _default_
  43. * port number then it might be a good guess that the colon suffix
  44. * on the host name was intended to override that, but you don't
  45. * really want to get into making heuristic judgments on that
  46. * basis.
  47. *
  48. * (Then again, you could just as easily make the same argument
  49. * about whether a 'user@' prefix on the host name should override
  50. * CONF_username, which this code _does_ do. I don't have a good
  51. * answer, sadly. Both these pieces of behaviour have been around
  52. * for years and it would probably cause subtle breakage in all
  53. * sorts of long-forgotten scripting to go changing things around
  54. * now.)
  55. *
  56. * In order to protect unbracketed IPv6 address literals against
  57. * this treatment, we do not make this change at all if there's
  58. * _more_ than one (un-IPv6-bracketed) colon.
  59. */
  60. p = host_strchr(host, ':');
  61. if (p && p == host_strrchr(host, ':')) {
  62. *p = '\0';
  63. }
  64. /*
  65. * Remove any remaining whitespace.
  66. */
  67. p = hostbuf;
  68. q = host;
  69. while (*q) {
  70. if (*q != ' ' && *q != '\t')
  71. *p++ = *q;
  72. q++;
  73. }
  74. *p = '\0';
  75. conf_set_str(conf, CONF_host, hostbuf);
  76. sfree(hostbuf);
  77. }