2
0

curl_sasl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef HEADER_CURL_SASL_H
  2. #define HEADER_CURL_SASL_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 2012 - 2021, 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. ***************************************************************************/
  24. #include <curl/curl.h>
  25. struct Curl_easy;
  26. struct connectdata;
  27. /* Authentication mechanism flags */
  28. #define SASL_MECH_LOGIN (1 << 0)
  29. #define SASL_MECH_PLAIN (1 << 1)
  30. #define SASL_MECH_CRAM_MD5 (1 << 2)
  31. #define SASL_MECH_DIGEST_MD5 (1 << 3)
  32. #define SASL_MECH_GSSAPI (1 << 4)
  33. #define SASL_MECH_EXTERNAL (1 << 5)
  34. #define SASL_MECH_NTLM (1 << 6)
  35. #define SASL_MECH_XOAUTH2 (1 << 7)
  36. #define SASL_MECH_OAUTHBEARER (1 << 8)
  37. #define SASL_MECH_SCRAM_SHA_1 (1 << 9)
  38. #define SASL_MECH_SCRAM_SHA_256 (1 << 10)
  39. /* Authentication mechanism values */
  40. #define SASL_AUTH_NONE 0
  41. #define SASL_AUTH_ANY 0xffff
  42. #define SASL_AUTH_DEFAULT (SASL_AUTH_ANY & ~SASL_MECH_EXTERNAL)
  43. /* Authentication mechanism strings */
  44. #define SASL_MECH_STRING_LOGIN "LOGIN"
  45. #define SASL_MECH_STRING_PLAIN "PLAIN"
  46. #define SASL_MECH_STRING_CRAM_MD5 "CRAM-MD5"
  47. #define SASL_MECH_STRING_DIGEST_MD5 "DIGEST-MD5"
  48. #define SASL_MECH_STRING_GSSAPI "GSSAPI"
  49. #define SASL_MECH_STRING_EXTERNAL "EXTERNAL"
  50. #define SASL_MECH_STRING_NTLM "NTLM"
  51. #define SASL_MECH_STRING_XOAUTH2 "XOAUTH2"
  52. #define SASL_MECH_STRING_OAUTHBEARER "OAUTHBEARER"
  53. #define SASL_MECH_STRING_SCRAM_SHA_1 "SCRAM-SHA-1"
  54. #define SASL_MECH_STRING_SCRAM_SHA_256 "SCRAM-SHA-256"
  55. /* SASL machine states */
  56. typedef enum {
  57. SASL_STOP,
  58. SASL_PLAIN,
  59. SASL_LOGIN,
  60. SASL_LOGIN_PASSWD,
  61. SASL_EXTERNAL,
  62. SASL_CRAMMD5,
  63. SASL_DIGESTMD5,
  64. SASL_DIGESTMD5_RESP,
  65. SASL_NTLM,
  66. SASL_NTLM_TYPE2MSG,
  67. SASL_GSSAPI,
  68. SASL_GSSAPI_TOKEN,
  69. SASL_GSSAPI_NO_DATA,
  70. SASL_OAUTH2,
  71. SASL_OAUTH2_RESP,
  72. SASL_GSASL,
  73. SASL_CANCEL,
  74. SASL_FINAL
  75. } saslstate;
  76. /* Progress indicator */
  77. typedef enum {
  78. SASL_IDLE,
  79. SASL_INPROGRESS,
  80. SASL_DONE
  81. } saslprogress;
  82. /* Protocol dependent SASL parameters */
  83. struct SASLproto {
  84. const char *service; /* The service name */
  85. int contcode; /* Code to receive when continuation is expected */
  86. int finalcode; /* Code to receive upon authentication success */
  87. size_t maxirlen; /* Maximum initial response length */
  88. CURLcode (*sendauth)(struct Curl_easy *data,
  89. struct connectdata *conn,
  90. const char *mech, const char *ir);
  91. /* Send authentication command */
  92. CURLcode (*sendcont)(struct Curl_easy *data,
  93. struct connectdata *conn, const char *contauth);
  94. /* Send authentication continuation */
  95. void (*getmessage)(char *buffer, char **outptr);
  96. /* Get SASL response message */
  97. };
  98. /* Per-connection parameters */
  99. struct SASL {
  100. const struct SASLproto *params; /* Protocol dependent parameters */
  101. saslstate state; /* Current machine state */
  102. unsigned short authmechs; /* Accepted authentication mechanisms */
  103. unsigned short prefmech; /* Preferred authentication mechanism */
  104. unsigned short authused; /* Auth mechanism used for the connection */
  105. bool resetprefs; /* For URL auth option parsing. */
  106. bool mutual_auth; /* Mutual authentication enabled (GSSAPI only) */
  107. bool force_ir; /* Protocol always supports initial response */
  108. };
  109. /* This is used to test whether the line starts with the given mechanism */
  110. #define sasl_mech_equal(line, wordlen, mech) \
  111. (wordlen == (sizeof(mech) - 1) / sizeof(char) && \
  112. !memcmp(line, mech, wordlen))
  113. /* This is used to cleanup any libraries or curl modules used by the sasl
  114. functions */
  115. void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused);
  116. /* Convert a mechanism name to a token */
  117. unsigned short Curl_sasl_decode_mech(const char *ptr,
  118. size_t maxlen, size_t *len);
  119. /* Parse the URL login options */
  120. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  121. const char *value, size_t len);
  122. /* Initializes an SASL structure */
  123. void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params);
  124. /* Check if we have enough auth data and capabilities to authenticate */
  125. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn);
  126. /* Calculate the required login details for SASL authentication */
  127. CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
  128. struct connectdata *conn,
  129. bool force_ir, saslprogress *progress);
  130. /* Continue an SASL authentication */
  131. CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
  132. struct connectdata *conn,
  133. int code, saslprogress *progress);
  134. #endif /* HEADER_CURL_SASL_H */