saslwrapper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include "saslwrapper.h"
  20. #include <sasl/sasl.h>
  21. #include <sstream>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <iostream>
  26. using namespace std;
  27. using namespace saslwrapper;
  28. namespace saslwrapper {
  29. class ClientImpl {
  30. friend class Client;
  31. ClientImpl() : conn(0), cbIndex(0), maxBufSize(65535), minSsf(0), maxSsf(65535), externalSsf(0), secret(0) {}
  32. ~ClientImpl() { if (conn) sasl_dispose(&conn); conn = 0; }
  33. bool setAttr(const string& key, const string& value);
  34. bool setAttr(const string& key, uint32_t value);
  35. bool init();
  36. bool start(const string& mechList, output_string& chosen, output_string& initialResponse);
  37. bool step(const string& challenge, output_string& response);
  38. bool encode(const string& clearText, output_string& cipherText);
  39. bool decode(const string& cipherText, output_string& clearText);
  40. bool getUserId(output_string& userId);
  41. void getError(output_string& error);
  42. void addCallback(unsigned long id, void* proc);
  43. void lastCallback() { addCallback(SASL_CB_LIST_END, 0); }
  44. void setError(const string& context, int code, const string& text = "", const string& text2 = "");
  45. void interact(sasl_interact_t* prompt);
  46. static int cbName(void *context, int id, const char **result, unsigned *len);
  47. static int cbPassword(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret);
  48. static bool initialized;
  49. sasl_conn_t* conn;
  50. sasl_callback_t callbacks[8];
  51. int cbIndex;
  52. string error;
  53. string serviceName;
  54. string userName;
  55. string authName;
  56. string password;
  57. string hostName;
  58. string externalUserName;
  59. uint32_t maxBufSize;
  60. uint32_t minSsf;
  61. uint32_t maxSsf;
  62. uint32_t externalSsf;
  63. sasl_secret_t* secret;
  64. };
  65. }
  66. bool ClientImpl::initialized = false;
  67. bool ClientImpl::init()
  68. {
  69. int result;
  70. if (!initialized) {
  71. initialized = true;
  72. result = sasl_client_init(0);
  73. if (result != SASL_OK) {
  74. setError("sasl_client_init", result, sasl_errstring(result, 0, 0));
  75. return false;
  76. }
  77. }
  78. int cbIndex = 0;
  79. addCallback(SASL_CB_GETREALM, 0);
  80. if (!userName.empty()) {
  81. addCallback(SASL_CB_USER, (void*) cbName);
  82. addCallback(SASL_CB_AUTHNAME, (void*) cbName);
  83. if (!password.empty())
  84. addCallback(SASL_CB_PASS, (void*) cbPassword);
  85. else
  86. addCallback(SASL_CB_PASS, 0);
  87. }
  88. lastCallback();
  89. unsigned flags;
  90. flags = 0;
  91. if (!authName.empty() && authName != userName)
  92. flags |= SASL_NEED_PROXY;
  93. result = sasl_client_new(serviceName.c_str(), hostName.c_str(), 0, 0, callbacks, flags, &conn);
  94. if (result != SASL_OK) {
  95. setError("sasl_client_new", result, sasl_errstring(result, 0, 0));
  96. return false;
  97. }
  98. sasl_security_properties_t secprops;
  99. secprops.min_ssf = minSsf;
  100. secprops.max_ssf = maxSsf;
  101. secprops.maxbufsize = maxBufSize;
  102. secprops.property_names = 0;
  103. secprops.property_values = 0;
  104. secprops.security_flags = 0;
  105. result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
  106. if (result != SASL_OK) {
  107. setError("sasl_setprop(SASL_SEC_PROPS)", result);
  108. sasl_dispose(&conn);
  109. conn = 0;
  110. return false;
  111. }
  112. if (!externalUserName.empty()) {
  113. result = sasl_setprop(conn, SASL_AUTH_EXTERNAL, externalUserName.c_str());
  114. if (result != SASL_OK) {
  115. setError("sasl_setprop(SASL_AUTH_EXTERNAL)", result);
  116. sasl_dispose(&conn);
  117. conn = 0;
  118. return false;
  119. }
  120. result = sasl_setprop(conn, SASL_SSF_EXTERNAL, &externalSsf);
  121. if (result != SASL_OK) {
  122. setError("sasl_setprop(SASL_SSF_EXTERNAL)", result);
  123. sasl_dispose(&conn);
  124. conn = 0;
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. bool ClientImpl::setAttr(const string& key, const string& value)
  131. {
  132. if (key == "service")
  133. serviceName = value;
  134. else if (key == "username")
  135. userName = value;
  136. else if (key == "authname")
  137. authName = value;
  138. else if (key == "password") {
  139. password = value;
  140. free(secret);
  141. secret = (sasl_secret_t*) malloc(sizeof(sasl_secret_t) + password.length());
  142. }
  143. else if (key == "host")
  144. hostName = value;
  145. else if (key == "externaluser")
  146. externalUserName = value;
  147. else {
  148. setError("setAttr", -1, "Unknown string attribute name", key);
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool ClientImpl::setAttr(const string& key, uint32_t value)
  154. {
  155. if (key == "minssf")
  156. minSsf = value;
  157. else if (key == "maxssf")
  158. maxSsf = value;
  159. else if (key == "externalssf")
  160. externalSsf = value;
  161. else if (key == "maxbufsize")
  162. maxBufSize = value;
  163. else {
  164. setError("setAttr", -1, "Unknown integer attribute name", key);
  165. return false;
  166. }
  167. return true;
  168. }
  169. bool ClientImpl::start(const string& mechList, output_string& chosen, output_string& initialResponse)
  170. {
  171. int result;
  172. sasl_interact_t* prompt = 0;
  173. const char* resp;
  174. const char* mech;
  175. unsigned int len;
  176. do {
  177. result = sasl_client_start(conn, mechList.c_str(), &prompt, &resp, &len, &mech);
  178. if (result == SASL_INTERACT)
  179. interact(prompt);
  180. } while (result == SASL_INTERACT);
  181. if (result != SASL_OK && result != SASL_CONTINUE) {
  182. setError("sasl_client_start", result);
  183. return false;
  184. }
  185. chosen = string(mech);
  186. initialResponse = string(resp, len);
  187. return true;
  188. }
  189. bool ClientImpl::step(const string& challenge, output_string& response)
  190. {
  191. int result;
  192. sasl_interact_t* prompt = 0;
  193. const char* resp;
  194. unsigned int len;
  195. do {
  196. result = sasl_client_step(conn, challenge.c_str(), challenge.size(), &prompt, &resp, &len);
  197. if (result == SASL_INTERACT)
  198. interact(prompt);
  199. } while (result == SASL_INTERACT);
  200. if (result != SASL_OK && result != SASL_CONTINUE) {
  201. setError("sasl_client_step", result);
  202. return false;
  203. }
  204. response = string(resp, len);
  205. return true;
  206. }
  207. bool ClientImpl::encode(const string& clearText, output_string& cipherText)
  208. {
  209. const char* output;
  210. unsigned int outlen;
  211. int result = sasl_encode(conn, clearText.c_str(), clearText.size(), &output, &outlen);
  212. if (result != SASL_OK) {
  213. setError("sasl_encode", result);
  214. return false;
  215. }
  216. cipherText = string(output, outlen);
  217. return true;
  218. }
  219. bool ClientImpl::decode(const string& cipherText, output_string& clearText)
  220. {
  221. const char* input = cipherText.c_str();
  222. unsigned int inLen = cipherText.size();
  223. unsigned int remaining = inLen;
  224. const char* cursor = input;
  225. const char* output;
  226. unsigned int outlen;
  227. clearText = string();
  228. while (remaining > 0) {
  229. unsigned int segmentLen = (remaining < maxBufSize) ? remaining : maxBufSize;
  230. int result = sasl_decode(conn, cursor, segmentLen, &output, &outlen);
  231. if (result != SASL_OK) {
  232. setError("sasl_decode", result);
  233. return false;
  234. }
  235. clearText = clearText + string(output, outlen);
  236. cursor += segmentLen;
  237. remaining -= segmentLen;
  238. }
  239. return true;
  240. }
  241. bool ClientImpl::getUserId(output_string& userId)
  242. {
  243. int result;
  244. const char* operName;
  245. result = sasl_getprop(conn, SASL_USERNAME, (const void**) &operName);
  246. if (result != SASL_OK) {
  247. setError("sasl_getprop(SASL_USERNAME)", result);
  248. return false;
  249. }
  250. userId = string(operName);
  251. return true;
  252. }
  253. void ClientImpl::getError(output_string& _error)
  254. {
  255. _error = error;
  256. error.clear();
  257. }
  258. void ClientImpl::addCallback(unsigned long id, void* proc)
  259. {
  260. callbacks[cbIndex].id = id;
  261. callbacks[cbIndex].proc = (int (*)()) proc;
  262. callbacks[cbIndex].context = this;
  263. cbIndex++;
  264. }
  265. void ClientImpl::setError(const string& context, int code, const string& text, const string& text2)
  266. {
  267. stringstream err;
  268. string etext(text.empty() ? sasl_errdetail(conn) : text);
  269. err << "Error in " << context << " (" << code << ") " << etext;
  270. if (!text2.empty())
  271. err << " - " << text2;
  272. error = err.str();
  273. }
  274. void ClientImpl::interact(sasl_interact_t* prompt)
  275. {
  276. string output;
  277. char* input;
  278. if (prompt->id == SASL_CB_PASS) {
  279. string ppt(prompt->prompt);
  280. ppt += ": ";
  281. char* pass = getpass(ppt.c_str());
  282. output = string(pass);
  283. } else {
  284. cout << prompt->prompt;
  285. if (prompt->defresult)
  286. cout << " [" << prompt->defresult << "]";
  287. cout << ": ";
  288. cin >> output;
  289. }
  290. prompt->result = output.c_str();
  291. prompt->len = output.length();
  292. }
  293. int ClientImpl::cbName(void *context, int id, const char **result, unsigned *len)
  294. {
  295. ClientImpl* impl = (ClientImpl*) context;
  296. if (id == SASL_CB_USER || (id == SASL_CB_AUTHNAME && impl->authName.empty())) {
  297. *result = impl->userName.c_str();
  298. //*len = impl->userName.length();
  299. } else if (id == SASL_CB_AUTHNAME) {
  300. *result = impl->authName.c_str();
  301. //*len = impl->authName.length();
  302. }
  303. return SASL_OK;
  304. }
  305. int ClientImpl::cbPassword(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret)
  306. {
  307. ClientImpl* impl = (ClientImpl*) context;
  308. size_t length = impl->password.length();
  309. if (id == SASL_CB_PASS) {
  310. impl->secret->len = length;
  311. ::memcpy(impl->secret->data, impl->password.c_str(), length);
  312. } else
  313. impl->secret->len = 0;
  314. *psecret = impl->secret;
  315. return SASL_OK;
  316. }
  317. //==========================================================
  318. // WRAPPERS
  319. //==========================================================
  320. Client::Client() : impl(new ClientImpl()) {}
  321. Client::~Client() { delete impl; }
  322. bool Client::setAttr(const string& key, const string& value) { return impl->setAttr(key, value); }
  323. bool Client::setAttr(const string& key, uint32_t value) { return impl->setAttr(key, value); }
  324. bool Client::init() { return impl->init(); }
  325. bool Client::start(const string& mechList, output_string& chosen, output_string& initialResponse) { return impl->start(mechList, chosen, initialResponse); }
  326. bool Client::step(const string& challenge, output_string& response) { return impl->step(challenge, response); }
  327. bool Client::encode(const string& clearText, output_string& cipherText) { return impl->encode(clearText, cipherText); }
  328. bool Client::decode(const string& cipherText, output_string& clearText) { return impl->decode(cipherText, clearText); }
  329. bool Client::getUserId(output_string& userId) { return impl->getUserId(userId); }
  330. void Client::getError(output_string& error) { impl->getError(error); }