cleartext.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.haxx.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. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "vauth/vauth.h"
  29. #include "curl_base64.h"
  30. #include "curl_md5.h"
  31. #include "warnless.h"
  32. #include "strtok.h"
  33. #include "sendf.h"
  34. #include "curl_printf.h"
  35. /* The last #include files should be: */
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /*
  39. * Curl_auth_create_plain_message()
  40. *
  41. * This is used to generate an already encoded PLAIN message ready
  42. * for sending to the recipient.
  43. *
  44. * Parameters:
  45. *
  46. * data [in] - The session handle.
  47. * userp [in] - The user name.
  48. * passdwp [in] - The user's password.
  49. * outptr [in/out] - The address where a pointer to newly allocated memory
  50. * holding the result will be stored upon completion.
  51. * outlen [out] - The length of the output message.
  52. *
  53. * Returns CURLE_OK on success.
  54. */
  55. CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
  56. const char *userp,
  57. const char *passwdp,
  58. char **outptr, size_t *outlen)
  59. {
  60. CURLcode result;
  61. char *plainauth;
  62. size_t ulen;
  63. size_t plen;
  64. ulen = strlen(userp);
  65. plen = strlen(passwdp);
  66. plainauth = malloc(2 * ulen + plen + 2);
  67. if(!plainauth) {
  68. *outlen = 0;
  69. *outptr = NULL;
  70. return CURLE_OUT_OF_MEMORY;
  71. }
  72. /* Calculate the reply */
  73. memcpy(plainauth, userp, ulen);
  74. plainauth[ulen] = '\0';
  75. memcpy(plainauth + ulen + 1, userp, ulen);
  76. plainauth[2 * ulen + 1] = '\0';
  77. memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
  78. /* Base64 encode the reply */
  79. result = Curl_base64_encode(data, plainauth, 2 * ulen + plen + 2, outptr,
  80. outlen);
  81. free(plainauth);
  82. return result;
  83. }
  84. /*
  85. * Curl_auth_create_login_message()
  86. *
  87. * This is used to generate an already encoded LOGIN message containing the
  88. * user name or password ready for sending to the recipient.
  89. *
  90. * Parameters:
  91. *
  92. * data [in] - The session handle.
  93. * valuep [in] - The user name or user's password.
  94. * outptr [in/out] - The address where a pointer to newly allocated memory
  95. * holding the result will be stored upon completion.
  96. * outlen [out] - The length of the output message.
  97. *
  98. * Returns CURLE_OK on success.
  99. */
  100. CURLcode Curl_auth_create_login_message(struct Curl_easy *data,
  101. const char *valuep, char **outptr,
  102. size_t *outlen)
  103. {
  104. size_t vlen = strlen(valuep);
  105. if(!vlen) {
  106. /* Calculate an empty reply */
  107. *outptr = strdup("=");
  108. if(*outptr) {
  109. *outlen = (size_t) 1;
  110. return CURLE_OK;
  111. }
  112. *outlen = 0;
  113. return CURLE_OUT_OF_MEMORY;
  114. }
  115. /* Base64 encode the value */
  116. return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
  117. }
  118. /*
  119. * Curl_auth_create_external_message()
  120. *
  121. * This is used to generate an already encoded EXTERNAL message containing
  122. * the user name ready for sending to the recipient.
  123. *
  124. * Parameters:
  125. *
  126. * data [in] - The session handle.
  127. * user [in] - The user name.
  128. * outptr [in/out] - The address where a pointer to newly allocated memory
  129. * holding the result will be stored upon completion.
  130. * outlen [out] - The length of the output message.
  131. *
  132. * Returns CURLE_OK on success.
  133. */
  134. CURLcode Curl_auth_create_external_message(struct Curl_easy *data,
  135. const char *user, char **outptr,
  136. size_t *outlen)
  137. {
  138. /* This is the same formatting as the login message */
  139. return Curl_auth_create_login_message(data, user, outptr, outlen);
  140. }