cleartext.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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. * SPDX-License-Identifier: curl
  22. *
  23. * RFC4616 PLAIN authentication
  24. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  25. *
  26. ***************************************************************************/
  27. #include "curl_setup.h"
  28. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
  29. !defined(CURL_DISABLE_POP3) || \
  30. (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
  31. #include <curl/curl.h>
  32. #include "urldata.h"
  33. #include "vauth/vauth.h"
  34. #include "curl_md5.h"
  35. #include "warnless.h"
  36. #include "strtok.h"
  37. #include "sendf.h"
  38. #include "curl_printf.h"
  39. /* The last #include files should be: */
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. /*
  43. * Curl_auth_create_plain_message()
  44. *
  45. * This is used to generate an already encoded PLAIN message ready
  46. * for sending to the recipient.
  47. *
  48. * Parameters:
  49. *
  50. * authzid [in] - The authorization identity.
  51. * authcid [in] - The authentication identity.
  52. * passwd [in] - The password.
  53. * out [out] - The result storage.
  54. *
  55. * Returns CURLE_OK on success.
  56. */
  57. CURLcode Curl_auth_create_plain_message(const char *authzid,
  58. const char *authcid,
  59. const char *passwd,
  60. struct bufref *out)
  61. {
  62. char *plainauth;
  63. size_t plainlen;
  64. size_t zlen;
  65. size_t clen;
  66. size_t plen;
  67. zlen = (authzid == NULL ? 0 : strlen(authzid));
  68. clen = strlen(authcid);
  69. plen = strlen(passwd);
  70. /* Compute binary message length. Check for overflows. */
  71. if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) ||
  72. (plen > (SIZE_T_MAX/2 - 2)))
  73. return CURLE_OUT_OF_MEMORY;
  74. plainlen = zlen + clen + plen + 2;
  75. plainauth = malloc(plainlen + 1);
  76. if(!plainauth)
  77. return CURLE_OUT_OF_MEMORY;
  78. /* Calculate the reply */
  79. if(zlen)
  80. memcpy(plainauth, authzid, zlen);
  81. plainauth[zlen] = '\0';
  82. memcpy(plainauth + zlen + 1, authcid, clen);
  83. plainauth[zlen + clen + 1] = '\0';
  84. memcpy(plainauth + zlen + clen + 2, passwd, plen);
  85. plainauth[plainlen] = '\0';
  86. Curl_bufref_set(out, plainauth, plainlen, curl_free);
  87. return CURLE_OK;
  88. }
  89. /*
  90. * Curl_auth_create_login_message()
  91. *
  92. * This is used to generate an already encoded LOGIN message containing the
  93. * user name or password ready for sending to the recipient.
  94. *
  95. * Parameters:
  96. *
  97. * valuep [in] - The user name or user's password.
  98. * out [out] - The result storage.
  99. *
  100. * Returns CURLE_OK on success.
  101. */
  102. CURLcode Curl_auth_create_login_message(const char *valuep, struct bufref *out)
  103. {
  104. Curl_bufref_set(out, valuep, strlen(valuep), NULL);
  105. return CURLE_OK;
  106. }
  107. /*
  108. * Curl_auth_create_external_message()
  109. *
  110. * This is used to generate an already encoded EXTERNAL message containing
  111. * the user name ready for sending to the recipient.
  112. *
  113. * Parameters:
  114. *
  115. * user [in] - The user name.
  116. * out [out] - The result storage.
  117. *
  118. * Returns CURLE_OK on success.
  119. */
  120. CURLcode Curl_auth_create_external_message(const char *user,
  121. struct bufref *out)
  122. {
  123. /* This is the same formatting as the login message */
  124. return Curl_auth_create_login_message(user, out);
  125. }
  126. #endif /* if no users */