curl_sasl.h 5.3 KB

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