cleartext.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * 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.h"
  34. #include "../curlx/warnless.h"
  35. #include "../sendf.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. size_t len;
  60. char *auth;
  61. size_t zlen = (authzid == NULL ? 0 : strlen(authzid));
  62. size_t clen = strlen(authcid);
  63. size_t plen = strlen(passwd);
  64. if((zlen > CURL_MAX_INPUT_LENGTH) || (clen > CURL_MAX_INPUT_LENGTH) ||
  65. (plen > CURL_MAX_INPUT_LENGTH))
  66. return CURLE_TOO_LARGE;
  67. len = zlen + clen + plen + 2;
  68. auth = curl_maprintf("%s%c%s%c%s", authzid ? authzid : "", '\0',
  69. authcid, '\0', passwd);
  70. if(!auth)
  71. return CURLE_OUT_OF_MEMORY;
  72. Curl_bufref_set(out, auth, len, curl_free);
  73. return CURLE_OK;
  74. }
  75. /*
  76. * Curl_auth_create_login_message()
  77. *
  78. * This is used to generate an already encoded LOGIN message containing the
  79. * username or password ready for sending to the recipient.
  80. *
  81. * Parameters:
  82. *
  83. * valuep [in] - The username or user's password.
  84. * out [out] - The result storage.
  85. *
  86. * Returns void.
  87. */
  88. void Curl_auth_create_login_message(const char *valuep, struct bufref *out)
  89. {
  90. Curl_bufref_set(out, valuep, strlen(valuep), NULL);
  91. }
  92. /*
  93. * Curl_auth_create_external_message()
  94. *
  95. * This is used to generate an already encoded EXTERNAL message containing
  96. * the username ready for sending to the recipient.
  97. *
  98. * Parameters:
  99. *
  100. * user [in] - The username.
  101. * out [out] - The result storage.
  102. *
  103. * Returns void.
  104. */
  105. void Curl_auth_create_external_message(const char *user,
  106. struct bufref *out)
  107. {
  108. /* This is the same formatting as the login message */
  109. Curl_auth_create_login_message(user, out);
  110. }
  111. #endif /* if no users */