curl_sasl.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef HEADER_CURL_SASL_H
  2. #define HEADER_CURL_SASL_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include <curl/curl.h>
  27. #include "bufref.h"
  28. struct Curl_easy;
  29. struct connectdata;
  30. /* Authentication mechanism flags */
  31. #define SASL_MECH_LOGIN (1 << 0)
  32. #define SASL_MECH_PLAIN (1 << 1)
  33. #define SASL_MECH_CRAM_MD5 (1 << 2)
  34. #define SASL_MECH_DIGEST_MD5 (1 << 3)
  35. #define SASL_MECH_GSSAPI (1 << 4)
  36. #define SASL_MECH_EXTERNAL (1 << 5)
  37. #define SASL_MECH_NTLM (1 << 6)
  38. #define SASL_MECH_XOAUTH2 (1 << 7)
  39. #define SASL_MECH_OAUTHBEARER (1 << 8)
  40. #define SASL_MECH_SCRAM_SHA_1 (1 << 9)
  41. #define SASL_MECH_SCRAM_SHA_256 (1 << 10)
  42. /* Authentication mechanism values */
  43. #define SASL_AUTH_NONE 0
  44. #define SASL_AUTH_ANY 0xffff
  45. #define SASL_AUTH_DEFAULT (SASL_AUTH_ANY & ~SASL_MECH_EXTERNAL)
  46. /* Authentication mechanism strings */
  47. #define SASL_MECH_STRING_LOGIN "LOGIN"
  48. #define SASL_MECH_STRING_PLAIN "PLAIN"
  49. #define SASL_MECH_STRING_CRAM_MD5 "CRAM-MD5"
  50. #define SASL_MECH_STRING_DIGEST_MD5 "DIGEST-MD5"
  51. #define SASL_MECH_STRING_GSSAPI "GSSAPI"
  52. #define SASL_MECH_STRING_EXTERNAL "EXTERNAL"
  53. #define SASL_MECH_STRING_NTLM "NTLM"
  54. #define SASL_MECH_STRING_XOAUTH2 "XOAUTH2"
  55. #define SASL_MECH_STRING_OAUTHBEARER "OAUTHBEARER"
  56. #define SASL_MECH_STRING_SCRAM_SHA_1 "SCRAM-SHA-1"
  57. #define SASL_MECH_STRING_SCRAM_SHA_256 "SCRAM-SHA-256"
  58. /* SASL flags */
  59. #define SASL_FLAG_BASE64 0x0001 /* Messages are base64-encoded */
  60. /* SASL machine states */
  61. typedef enum {
  62. SASL_STOP,
  63. SASL_PLAIN,
  64. SASL_LOGIN,
  65. SASL_LOGIN_PASSWD,
  66. SASL_EXTERNAL,
  67. SASL_CRAMMD5,
  68. SASL_DIGESTMD5,
  69. SASL_DIGESTMD5_RESP,
  70. SASL_NTLM,
  71. SASL_NTLM_TYPE2MSG,
  72. SASL_GSSAPI,
  73. SASL_GSSAPI_TOKEN,
  74. SASL_GSSAPI_NO_DATA,
  75. SASL_OAUTH2,
  76. SASL_OAUTH2_RESP,
  77. SASL_GSASL,
  78. SASL_CANCEL,
  79. SASL_FINAL
  80. } saslstate;
  81. /* Progress indicator */
  82. typedef enum {
  83. SASL_IDLE,
  84. SASL_INPROGRESS,
  85. SASL_DONE
  86. } saslprogress;
  87. /* Protocol dependent SASL parameters */
  88. struct SASLproto {
  89. const char *service; /* The service name */
  90. CURLcode (*sendauth)(struct Curl_easy *data, const char *mech,
  91. const struct bufref *ir);
  92. /* Send authentication command */
  93. CURLcode (*contauth)(struct Curl_easy *data, const char *mech,
  94. const struct bufref *contauth);
  95. /* Send authentication continuation */
  96. CURLcode (*cancelauth)(struct Curl_easy *data, const char *mech);
  97. /* Cancel authentication. */
  98. CURLcode (*getmessage)(struct Curl_easy *data, struct bufref *out);
  99. /* Get SASL response message */
  100. size_t maxirlen; /* Maximum initial response + mechanism length,
  101. or zero if no max. This is normally the max
  102. command length - other characters count.
  103. This has to be zero for non-base64 protocols. */
  104. int contcode; /* Code to receive when continuation is expected */
  105. int finalcode; /* Code to receive upon authentication success */
  106. unsigned short defmechs; /* Mechanisms enabled by default */
  107. unsigned short flags; /* Configuration flags. */
  108. };
  109. /* Per-connection parameters */
  110. struct SASL {
  111. const struct SASLproto *params; /* Protocol dependent parameters */
  112. saslstate state; /* Current machine state */
  113. const char *curmech; /* Current mechanism id. */
  114. unsigned short authmechs; /* Accepted authentication mechanisms */
  115. unsigned short prefmech; /* Preferred authentication mechanism */
  116. unsigned short authused; /* Auth mechanism used for the connection */
  117. bool resetprefs; /* For URL auth option parsing. */
  118. bool mutual_auth; /* Mutual authentication enabled (GSSAPI only) */
  119. bool force_ir; /* Protocol always supports initial response */
  120. };
  121. /* This is used to test whether the line starts with the given mechanism */
  122. #define sasl_mech_equal(line, wordlen, mech) \
  123. (wordlen == (sizeof(mech) - 1) / sizeof(char) && \
  124. !memcmp(line, mech, wordlen))
  125. /* This is used to cleanup any libraries or curl modules used by the sasl
  126. functions */
  127. void Curl_sasl_cleanup(struct connectdata *conn, unsigned short authused);
  128. /* Convert a mechanism name to a token */
  129. unsigned short Curl_sasl_decode_mech(const char *ptr,
  130. size_t maxlen, size_t *len);
  131. /* Parse the URL login options */
  132. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  133. const char *value, size_t len);
  134. /* Initializes an SASL structure */
  135. void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
  136. const struct SASLproto *params);
  137. /* Check if we have enough auth data and capabilities to authenticate */
  138. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data);
  139. /* Calculate the required login details for SASL authentication */
  140. CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
  141. bool force_ir, saslprogress *progress);
  142. /* Continue an SASL authentication */
  143. CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
  144. int code, saslprogress *progress);
  145. #endif /* HEADER_CURL_SASL_H */