cleartext.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "warnless.h"
  35. #include "strtok.h"
  36. #include "sendf.h"
  37. #include "curl_printf.h"
  38. /* The last #include files should be: */
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. /*
  42. * Curl_auth_create_plain_message()
  43. *
  44. * This is used to generate an already encoded PLAIN message ready
  45. * for sending to the recipient.
  46. *
  47. * Parameters:
  48. *
  49. * authzid [in] - The authorization identity.
  50. * authcid [in] - The authentication identity.
  51. * passwd [in] - The password.
  52. * out [out] - The result storage.
  53. *
  54. * Returns CURLE_OK on success.
  55. */
  56. CURLcode Curl_auth_create_plain_message(const char *authzid,
  57. const char *authcid,
  58. const char *passwd,
  59. struct bufref *out)
  60. {
  61. char *plainauth;
  62. size_t plainlen;
  63. size_t zlen;
  64. size_t clen;
  65. size_t plen;
  66. zlen = (authzid == NULL ? 0 : strlen(authzid));
  67. clen = strlen(authcid);
  68. plen = strlen(passwd);
  69. /* Compute binary message length. Check for overflows. */
  70. if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) ||
  71. (plen > (SIZE_T_MAX/2 - 2)))
  72. return CURLE_OUT_OF_MEMORY;
  73. plainlen = zlen + clen + plen + 2;
  74. plainauth = malloc(plainlen + 1);
  75. if(!plainauth)
  76. return CURLE_OUT_OF_MEMORY;
  77. /* Calculate the reply */
  78. if(zlen)
  79. memcpy(plainauth, authzid, zlen);
  80. plainauth[zlen] = '\0';
  81. memcpy(plainauth + zlen + 1, authcid, clen);
  82. plainauth[zlen + clen + 1] = '\0';
  83. memcpy(plainauth + zlen + clen + 2, passwd, plen);
  84. plainauth[plainlen] = '\0';
  85. Curl_bufref_set(out, plainauth, plainlen, curl_free);
  86. return CURLE_OK;
  87. }
  88. /*
  89. * Curl_auth_create_login_message()
  90. *
  91. * This is used to generate an already encoded LOGIN message containing the
  92. * username or password ready for sending to the recipient.
  93. *
  94. * Parameters:
  95. *
  96. * valuep [in] - The username or user's password.
  97. * out [out] - The result storage.
  98. *
  99. * Returns void.
  100. */
  101. void Curl_auth_create_login_message(const char *valuep, struct bufref *out)
  102. {
  103. Curl_bufref_set(out, valuep, strlen(valuep), NULL);
  104. }
  105. /*
  106. * Curl_auth_create_external_message()
  107. *
  108. * This is used to generate an already encoded EXTERNAL message containing
  109. * the username ready for sending to the recipient.
  110. *
  111. * Parameters:
  112. *
  113. * user [in] - The username.
  114. * out [out] - The result storage.
  115. *
  116. * Returns void.
  117. */
  118. void Curl_auth_create_external_message(const char *user,
  119. struct bufref *out)
  120. {
  121. /* This is the same formatting as the login message */
  122. Curl_auth_create_login_message(user, out);
  123. }
  124. #endif /* if no users */