sasl_engine.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_RPC_SASLENGINE_H
  19. #define LIB_RPC_SASLENGINE_H
  20. #include "hdfspp/status.h"
  21. #include "optional.hpp"
  22. #ifdef USE_GSASL
  23. #include "gsasl.h"
  24. #endif
  25. #include <vector>
  26. namespace hdfs {
  27. template <class T>
  28. using optional = std::experimental::optional<T>;
  29. class SaslMethod {
  30. public:
  31. std::string protocol;
  32. std::string mechanism;
  33. std::string serverid;
  34. void * data;
  35. };
  36. class SaslEngine {
  37. public:
  38. enum State {
  39. kUnstarted,
  40. kWaitingForData,
  41. kSuccess,
  42. kFailure,
  43. kError,
  44. };
  45. // State transitions:
  46. // \--------------------------/
  47. // kUnstarted --start--> kWaitingForData --step-+--> kSuccess --finish--v
  48. // \-> kFailure -/
  49. SaslEngine() : state_ (kUnstarted) {}
  50. virtual ~SaslEngine();
  51. // Must be called when state is kUnstarted
  52. Status setKerberosInfo(const std::string &principal);
  53. // Must be called when state is kUnstarted
  54. Status setPasswordInfo(const std::string &id,
  55. const std::string &password);
  56. // Returns the current state
  57. State getState();
  58. // Must be called when state is kUnstarted
  59. virtual std::pair<Status,SaslMethod> start(
  60. const std::vector<SaslMethod> &protocols) = 0;
  61. // Must be called when state is kWaitingForData
  62. // Returns kOK and any data that should be sent to the server
  63. virtual std::pair<Status,std::string> step(const std::string data) = 0;
  64. // Must only be called when state is kSuccess, kFailure, or kError
  65. virtual Status finish() = 0;
  66. protected:
  67. State state_;
  68. optional<std::string> principal_;
  69. optional<std::string> id_;
  70. optional<std::string> password_;
  71. };
  72. #ifdef USE_GSASL
  73. class GSaslEngine : public SaslEngine
  74. {
  75. public:
  76. GSaslEngine() : SaslEngine(), ctx_(nullptr), session_(nullptr) {}
  77. virtual ~GSaslEngine();
  78. virtual std::pair<Status,SaslMethod> start(
  79. const std::vector<SaslMethod> &protocols);
  80. virtual std::pair<Status,std::string> step(const std::string data);
  81. virtual Status finish();
  82. private:
  83. Gsasl * ctx_;
  84. Gsasl_session * session_;
  85. Status init_kerberos(const SaslMethod & mechanism);
  86. };
  87. #endif
  88. #ifdef USE_CYRUS_SASL
  89. class CyrusSaslEngine : public SaslEngine
  90. {
  91. public:
  92. GSaslEngine() : SaslEngine(), ctx_(nullptr), session_(nullptr) {}
  93. virtual ~GSaslEngine();
  94. virtual std::pair<Status,SaslMethod> start(
  95. const std::vector<SaslMethod> &protocols);
  96. virtual std::pair<Status,std::string> step(const std::string data);
  97. virtual Status finish();
  98. private:
  99. };
  100. #endif
  101. }
  102. #endif /* LIB_RPC_SASLENGINE_H */