cleartext.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC4616 PLAIN authentication
  22. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
  27. !defined(CURL_DISABLE_POP3)
  28. #include <curl/curl.h>
  29. #include "urldata.h"
  30. #include "vauth/vauth.h"
  31. #include "curl_md5.h"
  32. #include "warnless.h"
  33. #include "strtok.h"
  34. #include "sendf.h"
  35. #include "curl_printf.h"
  36. /* The last #include files should be: */
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. /*
  40. * Curl_auth_create_plain_message()
  41. *
  42. * This is used to generate an already encoded PLAIN message ready
  43. * for sending to the recipient.
  44. *
  45. * Parameters:
  46. *
  47. * authzid [in] - The authorization identity.
  48. * authcid [in] - The authentication identity.
  49. * passwd [in] - The password.
  50. * out [out] - The result storage.
  51. *
  52. * Returns CURLE_OK on success.
  53. */
  54. CURLcode Curl_auth_create_plain_message(const char *authzid,
  55. const char *authcid,
  56. const char *passwd,
  57. struct bufref *out)
  58. {
  59. char *plainauth;
  60. size_t plainlen;
  61. size_t zlen;
  62. size_t clen;
  63. size_t plen;
  64. zlen = (authzid == NULL ? 0 : strlen(authzid));
  65. clen = strlen(authcid);
  66. plen = strlen(passwd);
  67. /* Compute binary message length. Check for overflows. */
  68. if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) ||
  69. (plen > (SIZE_T_MAX/2 - 2)))
  70. return CURLE_OUT_OF_MEMORY;
  71. plainlen = zlen + clen + plen + 2;
  72. plainauth = malloc(plainlen + 1);
  73. if(!plainauth)
  74. return CURLE_OUT_OF_MEMORY;
  75. /* Calculate the reply */
  76. if(zlen)
  77. memcpy(plainauth, authzid, zlen);
  78. plainauth[zlen] = '\0';
  79. memcpy(plainauth + zlen + 1, authcid, clen);
  80. plainauth[zlen + clen + 1] = '\0';
  81. memcpy(plainauth + zlen + clen + 2, passwd, plen);
  82. plainauth[plainlen] = '\0';
  83. Curl_bufref_set(out, plainauth, plainlen, curl_free);
  84. return CURLE_OK;
  85. }
  86. /*
  87. * Curl_auth_create_login_message()
  88. *
  89. * This is used to generate an already encoded LOGIN message containing the
  90. * user name or password ready for sending to the recipient.
  91. *
  92. * Parameters:
  93. *
  94. * valuep [in] - The user name or user's password.
  95. * out [out] - The result storage.
  96. *
  97. * Returns CURLE_OK on success.
  98. */
  99. CURLcode Curl_auth_create_login_message(const char *valuep, struct bufref *out)
  100. {
  101. Curl_bufref_set(out, valuep, strlen(valuep), NULL);
  102. return CURLE_OK;
  103. }
  104. /*
  105. * Curl_auth_create_external_message()
  106. *
  107. * This is used to generate an already encoded EXTERNAL message containing
  108. * the user name ready for sending to the recipient.
  109. *
  110. * Parameters:
  111. *
  112. * user [in] - The user name.
  113. * out [out] - The result storage.
  114. *
  115. * Returns CURLE_OK on success.
  116. */
  117. CURLcode Curl_auth_create_external_message(const char *user,
  118. struct bufref *out)
  119. {
  120. /* This is the same formatting as the login message */
  121. return Curl_auth_create_login_message(user, out);
  122. }
  123. #endif /* if no users */