Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TSasl.h
Go to the documentation of this file.
1 // This file will be removed when the code is accepted into the Thrift library.
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20 
21 #ifndef _THRIFT_TRANSPORT_TSASL_H_
22 #define _THRIFT_TRANSPORT_TSASL_H_ 1
23 
24 #include <string>
25 #include <map>
26 #include <stdint.h>
27 #include <stdexcept>
28 
29 #ifdef _WIN32
30 #include <sasl.h>
31 #include <saslplug.h>
32 #include <saslutil.h>
33 #else /* _WIN32 */
34 #include <sasl/sasl.h>
35 #include <sasl/saslplug.h>
36 #include <sasl/saslutil.h>
37 #endif
38 
39 #include <thrift/transport/TTransportException.h>
40 
41 using namespace apache::thrift::transport;
42 
43 namespace sasl {
45  public:
46  SaslException(const char* msg) : TTransportException(msg) {
47  }
48 };
49 
55 class TSasl {
56  public:
57  ~TSasl() {
58  sasl_dispose(&conn);
59  }
60 
61  /*
62  * Called once per application to free resources.`
63  * Note that there is no distinction in the sasl library between being done
64  * with servers or done with clients. Internally the library maintains a which
65  * is being used. A call to SaslDone should only happen after all clients
66  * and servers are finished.
67  */
68  static void SaslDone() {
69  sasl_done();
70  }
71 
72  /* Evaluates the challenge or response data and generates a response. */
73  virtual uint8_t* evaluateChallengeOrResponse(const uint8_t* challenge,
74  uint32_t len, uint32_t* resLen) = 0;
75 
76  /* Determines whether the authentication exchange has completed. */
77  bool isComplete() {
78  return authComplete;
79  }
80 
81  /*
82  * Unwraps a received byte array.
83  * Returns a buffer for unwrapped result, and sets
84  * 'len' to the buffer's length. The buffer is only valid until the next call, or
85  * until the client is closed.
86  */
87  uint8_t* unwrap(const uint8_t* incoming, const int offset,
88  const uint32_t len, uint32_t* outLen);
89 
90  /*
91  * Wraps a byte array to be sent.
92  * Returns a buffer of wrapped result, and sets
93  * 'len' to the buffer's length. The buffer is only valid until the next call, or
94  * until the client is closed.
95  */
96  uint8_t* wrap(const uint8_t* outgoing, int offset,
97  const uint32_t len, uint32_t* outLen);
98 
99  /* Returns the IANA-registered mechanism name. */
100  virtual std::string getMechanismName() { return NULL; }
101 
102  /* Determines whether this mechanism has an optional initial response. */
103  virtual bool hasInitialResponse() { return false; }
104 
105  /* Returns the username from the underlying sasl connection. */
106  std::string getUsername();
107 
108  protected:
109  /* Authorization is complete. */
111  /* Sasl Connection. */
112  sasl_conn_t* conn;
113 };
114 
116  public:
117  SaslClientImplException(const char* errMsg)
118  : SaslException(errMsg) {
119  }
120 };
121 
122 /* Client sasl implementation class. */
123 class TSaslClient : public sasl::TSasl {
124  public:
125  TSaslClient(const std::string& mechanisms, const std::string& authorizationId,
126  const std::string& protocol, const std::string& serverName,
127  const std::map<std::string,std::string>& props,
128  sasl_callback_t* callbacks);
129 
130  static void SaslInit(sasl_callback_t* callbacks) {
131  int result = sasl_client_init(callbacks);
132  if (result != SASL_OK)
133  throw SaslClientImplException(sasl_errstring(result, NULL, NULL));
134  }
135 
136  /* Evaluates the challenge data and generates a response. */
137  uint8_t* evaluateChallengeOrResponse(const uint8_t* challenge,
138  const uint32_t len, uint32_t* outLen);
139 
140  /* Returns the IANA-registered mechanism name of this SASL client. */
141  virtual std::string getMechanismName();
142 
143  /* Retrieves the negotiated property */
144  std::string getNegotiatedProperty(const std::string& propName);
145 
146  /* Determines whether this mechanism has an optional initial response. */
147  virtual bool hasInitialResponse();
148 
149  private :
150  /* true if sasl_client_start has been called. */
152 
153  /* The chosen mechanism. */
154  std::string chosenMech;
155 
156  /* List of possible mechanisms. */
157  std::string mechList;
158 };
159 
161  public:
162  SaslServerImplException(const char* errMsg)
163  : SaslException(errMsg) {
164  }
165 };
166 
167 /* Server sasl implementation class. */
168 class TSaslServer : public sasl::TSasl {
169  public:
170  TSaslServer(const std::string& service, const std::string& serverFQDN,
171  const std::string& userRealm, unsigned flags, sasl_callback_t* callbacks);
172 
173  /*
174  * This initializes the sasl server library and should be called onece per application
175  */
176  static void SaslInit(const sasl_callback_t* callbacks, const std::string& appname) {
177  int result = sasl_server_init(callbacks, appname.c_str());
178  if (result != SASL_OK) {
179  throw SaslServerImplException(sasl_errstring(result, NULL, NULL));
180  }
181  }
182 
183  /* Evaluates the response data and generates a challenge. */
184  virtual uint8_t* evaluateChallengeOrResponse(const uint8_t* challenge,
185  const uint32_t len, uint32_t* resLen);
186  private:
187  /* true if sasl_server_start has been called. */
189 };
190 }
191 #endif /* _THRIFT_TRANSPORT_TSALS_H_ */
sasl_conn_t * conn
Definition: TSasl.h:112
const StringSearch UrlParser::protocol_search & protocol
Definition: url-parser.cc:36
~TSasl()
Definition: TSasl.h:57
SaslServerImplException(const char *errMsg)
Definition: TSasl.h:162
std::string chosenMech
Definition: TSasl.h:154
bool authComplete
Definition: TSasl.h:110
SaslException(const char *msg)
Definition: TSasl.h:46
static void SaslInit(const sasl_callback_t *callbacks, const std::string &appname)
Definition: TSasl.h:176
bool isComplete()
Definition: TSasl.h:77
virtual std::string getMechanismName()
Definition: TSasl.h:100
SaslClientImplException(const char *errMsg)
Definition: TSasl.h:117
bool serverStarted
Definition: TSasl.h:188
uint8_t offset[7 *64-sizeof(uint64_t)]
std::string mechList
Definition: TSasl.h:157
virtual bool hasInitialResponse()
Definition: TSasl.h:103
static void SaslInit(sasl_callback_t *callbacks)
Definition: TSasl.h:130
static void SaslDone()
Definition: TSasl.h:68
bool clientStarted
Definition: TSasl.h:151