sasl_io.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include "slap.h"
  41. #include "slapi-plugin.h"
  42. #include "fe.h"
  43. #include <sasl.h>
  44. /*
  45. * I/O Shim Layer for SASL Encryption
  46. * The 'handle' is a pointer to a sasl_connection structure.
  47. */
  48. #define SASL_IO_BUFFER_SIZE 1024
  49. /*
  50. * SASL sends its encrypted PDU's with an embedded 4-byte length
  51. * at the beginning (in network byte order). We peek inside the
  52. * received data off the wire to find this length, and use it
  53. * to determine when we have read an entire SASL PDU.
  54. * So when we have that there is no need for the SASL layer
  55. * to do any fancy buffering with it, we always hand it
  56. * a full packet.
  57. */
  58. struct PRFilePrivate {
  59. char *decrypted_buffer;
  60. size_t decrypted_buffer_size;
  61. size_t decrypted_buffer_count;
  62. size_t decrypted_buffer_offset;
  63. char *encrypted_buffer;
  64. size_t encrypted_buffer_size;
  65. size_t encrypted_buffer_count;
  66. size_t encrypted_buffer_offset;
  67. Connection *conn; /* needed for connid and sasl_conn context */
  68. PRBool send_encrypted; /* can only send encrypted data after the first read -
  69. that is, we cannot send back an encrypted response
  70. to the bind request that established the sasl io */
  71. const char *send_buffer; /* encrypted buffer to send to client */
  72. unsigned int send_size; /* size of the encrypted buffer */
  73. unsigned int send_offset; /* number of bytes sent so far */
  74. };
  75. typedef PRFilePrivate sasl_io_private;
  76. static PRInt32 PR_CALLBACK
  77. sasl_io_recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
  78. PRIntervalTime timeout);
  79. static void
  80. debug_print_layers(PRFileDesc *fd)
  81. {
  82. #if 0
  83. PR_ASSERT(fd->higher == NULL); /* this is the topmost layer */
  84. while (fd) {
  85. PRSocketOptionData sod;
  86. PRInt32 err;
  87. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  88. "debug_print_layers: fd %d sasl_io_recv = %p\n",
  89. PR_FileDesc2NativeHandle(fd), sasl_io_recv );
  90. LDAPDebug( LDAP_DEBUG_CONNS,
  91. "debug_print_layers: fd name %s type = %d recv = %p\n",
  92. PR_GetNameForIdentity(fd->identity),
  93. PR_GetDescType(fd),
  94. fd->methods->recv ? fd->methods->recv : NULL );
  95. sod.option = PR_SockOpt_Nonblocking;
  96. if (PR_FAILURE == PR_GetSocketOption(fd, &sod)) {
  97. err = PR_GetError();
  98. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  99. "debug_print_layers: error getting nonblocking option: %d %s\n",
  100. err, slapd_pr_strerror(err) );
  101. } else {
  102. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  103. "debug_print_layers: non blocking %d\n", sod.value.non_blocking );
  104. }
  105. sod.option = PR_SockOpt_Reuseaddr;
  106. if (PR_FAILURE == PR_GetSocketOption(fd, &sod)) {
  107. err = PR_GetError();
  108. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  109. "debug_print_layers: error getting reuseaddr option: %d %s\n",
  110. err, slapd_pr_strerror(err) );
  111. } else {
  112. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  113. "debug_print_layers: reuseaddr %d\n", sod.value.reuse_addr );
  114. }
  115. sod.option = PR_SockOpt_RecvBufferSize;
  116. if (PR_FAILURE == PR_GetSocketOption(fd, &sod)) {
  117. err = PR_GetError();
  118. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  119. "debug_print_layers: error getting recvbuffer option: %d %s\n",
  120. err, slapd_pr_strerror(err) );
  121. } else {
  122. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  123. "debug_print_layers: recvbuffer %d\n", sod.value.recv_buffer_size );
  124. }
  125. fd = fd->lower;
  126. }
  127. #endif
  128. }
  129. static void
  130. sasl_io_init_buffers(sasl_io_private *sp)
  131. {
  132. sp->decrypted_buffer = slapi_ch_malloc(SASL_IO_BUFFER_SIZE);
  133. sp->decrypted_buffer_size = SASL_IO_BUFFER_SIZE;
  134. sp->encrypted_buffer = slapi_ch_malloc(SASL_IO_BUFFER_SIZE);
  135. sp->encrypted_buffer_size = SASL_IO_BUFFER_SIZE;
  136. }
  137. static void sasl_io_resize_encrypted_buffer(sasl_io_private *sp, size_t requested_size)
  138. {
  139. if (requested_size > sp->encrypted_buffer_size) {
  140. sp->encrypted_buffer = slapi_ch_realloc(sp->encrypted_buffer, requested_size);
  141. sp->encrypted_buffer_size = requested_size;
  142. }
  143. }
  144. static void sasl_io_resize_decrypted_buffer(sasl_io_private *sp, size_t requested_size)
  145. {
  146. if (requested_size > sp->decrypted_buffer_size) {
  147. sp->decrypted_buffer = slapi_ch_realloc(sp->decrypted_buffer, requested_size);
  148. sp->decrypted_buffer_size = requested_size;
  149. }
  150. }
  151. static int
  152. sasl_io_reading_packet(sasl_io_private *sp)
  153. {
  154. return (sp->encrypted_buffer_count > 0);
  155. }
  156. static int
  157. sasl_io_finished_packet(sasl_io_private *sp)
  158. {
  159. return (sp->encrypted_buffer_count && (sp->encrypted_buffer_offset == sp->encrypted_buffer_count) );
  160. }
  161. static const char* const sasl_LayerName = "SASL";
  162. static PRDescIdentity sasl_LayerID;
  163. static PRIOMethods sasl_IoMethods;
  164. static PRCallOnceType sasl_callOnce = {0,0};
  165. static sasl_io_private *
  166. sasl_get_io_private(PRFileDesc *fd)
  167. {
  168. sasl_io_private *sp;
  169. PR_ASSERT(fd != NULL);
  170. PR_ASSERT(fd->methods->file_type == PR_DESC_LAYERED);
  171. PR_ASSERT(fd->identity == sasl_LayerID);
  172. sp = (sasl_io_private *)fd->secret;
  173. return sp;
  174. }
  175. static PRInt32
  176. sasl_io_start_packet(PRFileDesc *fd, PRIntn flags, PRIntervalTime timeout, PRInt32 *err)
  177. {
  178. PRInt32 ret = 0;
  179. unsigned char buffer[4];
  180. size_t packet_length = 0;
  181. size_t saslio_limit;
  182. sasl_io_private *sp = sasl_get_io_private(fd);
  183. Connection *c = sp->conn;
  184. *err = 0;
  185. debug_print_layers(fd);
  186. /* first we need the length bytes */
  187. ret = PR_Recv(fd->lower, buffer, sizeof(buffer), flags, timeout);
  188. LDAPDebug( LDAP_DEBUG_CONNS,
  189. "read sasl packet length returned %d on connection %" NSPRIu64 "\n", ret, c->c_connid, 0 );
  190. if (ret <= 0) {
  191. *err = PR_GetError();
  192. LDAPDebug( LDAP_DEBUG_ANY,
  193. "sasl_io_start_packet: error reading sasl packet length on connection %" NSPRIu64 " %d:%s\n", c->c_connid, *err, slapd_pr_strerror(*err) );
  194. return PR_FAILURE;
  195. }
  196. /*
  197. * NOTE: A better way to do this would be to read the bytes and add them to
  198. * sp->encrypted_buffer - if offset < 4, tell caller we didn't read enough
  199. * bytes yet - if offset >= 4, decode the length and proceed. However, it
  200. * is highly unlikely that a request to read 4 bytes will return < 4 bytes,
  201. * perhaps only in error conditions, in which case the ret < 0 case above
  202. * will run
  203. */
  204. if (ret != 0 && ret < sizeof(buffer)) {
  205. LDAPDebug( LDAP_DEBUG_ANY,
  206. "sasl_io_start_packet: failed - read only %d bytes of sasl packet length on connection %" NSPRIu64 "\n", ret, c->c_connid, 0 );
  207. PR_SetError(PR_IO_ERROR, 0);
  208. return PR_FAILURE;
  209. }
  210. if (ret == sizeof(buffer)) {
  211. /* Decode the length (could use ntohl here ??) */
  212. packet_length = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
  213. /* add length itself (for Cyrus SASL library) */
  214. packet_length += 4;
  215. LDAPDebug( LDAP_DEBUG_CONNS,
  216. "read sasl packet length %ld on connection %" NSPRIu64 "\n", packet_length, c->c_connid, 0 );
  217. /* Check if the packet length is larger than our max allowed. A
  218. * setting of -1 means that we allow any size SASL IO packet. */
  219. saslio_limit = config_get_maxsasliosize();
  220. if(((long)saslio_limit != -1) && (packet_length > saslio_limit)) {
  221. LDAPDebug( LDAP_DEBUG_ANY,
  222. "SASL encrypted packet length exceeds maximum allowed limit (length=%ld, limit=%ld)."
  223. " Change the nsslapd-maxsasliosize attribute in cn=config to increase limit.\n",
  224. packet_length, config_get_maxsasliosize(), 0);
  225. PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
  226. *err = PR_BUFFER_OVERFLOW_ERROR;
  227. return PR_FAILURE;
  228. }
  229. sasl_io_resize_encrypted_buffer(sp, packet_length);
  230. /* Cyrus SASL implementation expects to have the length at the first
  231. 4 bytes */
  232. memcpy(sp->encrypted_buffer, buffer, 4);
  233. sp->encrypted_buffer_count = packet_length;
  234. sp->encrypted_buffer_offset = 4;
  235. }
  236. return PR_SUCCESS;
  237. }
  238. static PRInt32
  239. sasl_io_read_packet(PRFileDesc *fd, PRIntn flags, PRIntervalTime timeout, PRInt32 *err)
  240. {
  241. PRInt32 ret = 0;
  242. sasl_io_private *sp = sasl_get_io_private(fd);
  243. Connection *c = sp->conn;
  244. size_t bytes_remaining_to_read = sp->encrypted_buffer_count - sp->encrypted_buffer_offset;
  245. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  246. "sasl_io_read_packet: reading %d bytes for connection %" NSPRIu64 "\n",
  247. bytes_remaining_to_read,
  248. c->c_connid );
  249. ret = PR_Recv(fd->lower, sp->encrypted_buffer + sp->encrypted_buffer_offset, bytes_remaining_to_read, flags, timeout);
  250. if (ret < 0) {
  251. *err = PR_GetError();
  252. LDAPDebug( LDAP_DEBUG_ANY,
  253. "sasl_io_read_packet: error reading sasl packet on connection %" NSPRIu64 " %d:%s\n", c->c_connid, *err, slapd_pr_strerror(*err) );
  254. return PR_FAILURE;
  255. }
  256. sp->encrypted_buffer_offset += ret;
  257. return ret;
  258. }
  259. static PRInt32 PR_CALLBACK
  260. sasl_io_recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
  261. PRIntervalTime timeout)
  262. {
  263. sasl_io_private *sp = sasl_get_io_private(fd);
  264. Connection *c = sp->conn;
  265. PRInt32 ret = 0;
  266. size_t bytes_in_buffer = 0;
  267. PRInt32 err = 0;
  268. /* Do we have decrypted data buffered from 'before' ? */
  269. bytes_in_buffer = sp->decrypted_buffer_count - sp->decrypted_buffer_offset;
  270. LDAPDebug( LDAP_DEBUG_CONNS,
  271. "sasl_io_recv for connection %" NSPRIu64 " len %d bytes_in_buffer %d\n", c->c_connid, len, bytes_in_buffer );
  272. LDAPDebug( LDAP_DEBUG_CONNS,
  273. "sasl_io_recv for connection %" NSPRIu64 " len %d encrypted buffer count %d\n", c->c_connid, len, sp->encrypted_buffer_count );
  274. if (0 == bytes_in_buffer) {
  275. /* If there wasn't buffered decrypted data, we need to get some... */
  276. if (!sasl_io_reading_packet(sp)) {
  277. /* First read the packet length and so on */
  278. ret = sasl_io_start_packet(fd, flags, timeout, &err);
  279. if (0 != ret) {
  280. /* Most likely the i/o timed out */
  281. return ret;
  282. }
  283. }
  284. /* We now have the packet length
  285. * we now must read more data off the wire until we have the complete packet
  286. */
  287. ret = sasl_io_read_packet(fd, flags, timeout, &err);
  288. if (PR_FAILURE == ret) {
  289. return ret; /* read packet will set pr error */
  290. }
  291. /* If we have not read the packet yet, we cannot return any decrypted data to the
  292. * caller - so just tell the caller we don't have enough data yet
  293. * this is equivalent to recv() returning EAGAIN on a non-blocking socket
  294. * the caller must handle this condition and poll() or similar to know
  295. * when more data arrives
  296. */
  297. if (!sasl_io_finished_packet(sp)) {
  298. LDAPDebug( LDAP_DEBUG_CONNS,
  299. "sasl_io_recv for connection %" NSPRIu64 " - not finished reading packet yet\n", c->c_connid, 0, 0 );
  300. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  301. return PR_FAILURE;
  302. }
  303. /* We have the full encrypted buffer now - decrypt it */
  304. {
  305. const char *output_buffer = NULL;
  306. unsigned int output_length = 0;
  307. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  308. "sasl_io_recv finished reading packet for connection %" NSPRIu64 "\n", c->c_connid );
  309. /* Now decode it */
  310. ret = sasl_decode(c->c_sasl_conn,sp->encrypted_buffer,sp->encrypted_buffer_count,&output_buffer,&output_length);
  311. if (SASL_OK == ret) {
  312. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  313. "sasl_io_recv decoded packet length %d for connection %" NSPRIu64 "\n", output_length, c->c_connid );
  314. if (output_length) {
  315. sasl_io_resize_decrypted_buffer(sp,output_length);
  316. memcpy(sp->decrypted_buffer,output_buffer,output_length);
  317. sp->decrypted_buffer_count = output_length;
  318. sp->decrypted_buffer_offset = 0;
  319. sp->encrypted_buffer_offset = 0;
  320. sp->encrypted_buffer_count = 0;
  321. bytes_in_buffer = output_length;
  322. }
  323. } else {
  324. LDAPDebug1Arg( LDAP_DEBUG_ANY,
  325. "sasl_io_recv failed to decode packet for connection %" NSPRIu64 "\n", c->c_connid );
  326. PR_SetError(PR_IO_ERROR, 0);
  327. return PR_FAILURE;
  328. }
  329. }
  330. }
  331. /* Finally, return data from the buffer to the caller */
  332. {
  333. size_t bytes_to_return = sp->decrypted_buffer_count - sp->decrypted_buffer_offset;
  334. if (bytes_to_return > len) {
  335. bytes_to_return = len;
  336. }
  337. /* Copy data from the decrypted buffer starting at the offset */
  338. memcpy(buf, sp->decrypted_buffer + sp->decrypted_buffer_offset, bytes_to_return);
  339. if (bytes_in_buffer == bytes_to_return) {
  340. sp->decrypted_buffer_offset = 0;
  341. sp->decrypted_buffer_count = 0;
  342. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  343. "sasl_io_recv all decrypted data returned for connection %" NSPRIu64 "\n", c->c_connid );
  344. } else {
  345. sp->decrypted_buffer_offset += bytes_to_return;
  346. LDAPDebug( LDAP_DEBUG_CONNS,
  347. "sasl_io_recv returning %d bytes to caller %d bytes left to return for connection %" NSPRIu64 "\n",
  348. bytes_to_return,
  349. sp->decrypted_buffer_count - sp->decrypted_buffer_offset,
  350. c->c_connid );
  351. }
  352. ret = bytes_to_return;
  353. }
  354. if (ret > 0) {
  355. /* we actually read something - we can now send encrypted data */
  356. sp->send_encrypted = PR_TRUE;
  357. }
  358. return ret;
  359. }
  360. static void
  361. reset_send_info(sasl_io_private *sp)
  362. {
  363. sp->send_buffer = NULL;
  364. sp->send_size = 0;
  365. sp->send_offset = 0;
  366. }
  367. PRInt32
  368. sasl_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
  369. PRIntn flags, PRIntervalTime timeout)
  370. {
  371. PRInt32 ret = 0;
  372. sasl_io_private *sp = sasl_get_io_private(fd);
  373. Connection *c = sp->conn;
  374. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  375. "sasl_io_send writing %d bytes\n", amount );
  376. if (sp->send_encrypted) {
  377. /* Get SASL to encrypt the buffer */
  378. if (NULL == sp->send_buffer) {
  379. ret = sasl_encode(c->c_sasl_conn, buf, amount, &sp->send_buffer, &sp->send_size);
  380. if (ret != SASL_OK) {
  381. const char *saslerr = sasl_errdetail(c->c_sasl_conn);
  382. LDAPDebug2Args( LDAP_DEBUG_ANY,
  383. "sasl_io_send could not encode %d bytes - sasl error %s\n",
  384. amount, saslerr ? saslerr : "unknown" );
  385. reset_send_info(sp);
  386. PR_SetError(PR_IO_ERROR, 0);
  387. return PR_FAILURE;
  388. }
  389. LDAPDebug1Arg( LDAP_DEBUG_CONNS,
  390. "sasl_io_send encoded as %d bytes\n", sp->send_size );
  391. sp->send_offset = 0;
  392. } else if ((amount > 0) && (sp->send_offset >= sp->send_size)) {
  393. /* something went wrong - we sent too many bytes */
  394. LDAPDebug2Args( LDAP_DEBUG_ANY,
  395. "sasl_io_send - client requested to send %d bytes but we "
  396. "already sent %d bytes\n", amount, (sp->send_offset >= sp->send_size));
  397. reset_send_info(sp);
  398. PR_SetError(PR_BUFFER_OVERFLOW_ERROR, EMSGSIZE);
  399. return PR_FAILURE;
  400. }
  401. ret = PR_Send(fd->lower, sp->send_buffer + sp->send_offset,
  402. sp->send_size - sp->send_offset, flags, timeout);
  403. /* we need to return the amount of cleartext sent */
  404. if (ret == (sp->send_size - sp->send_offset)) {
  405. ret = amount; /* sent amount of data requested by caller */
  406. reset_send_info(sp); /* done with this buffer, ready for next buffer */
  407. } else if (ret > 0) { /* could not send the entire encrypted buffer - tell caller we're blocked */
  408. LDAPDebug2Args( LDAP_DEBUG_CONNS,
  409. "sasl_io_send error: only sent %d of %d encoded bytes\n", ret,
  410. (sp->send_size - sp->send_offset) );
  411. sp->send_offset += ret;
  412. ret = PR_FAILURE;
  413. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  414. }
  415. /* else - ret is error - caller will handle */
  416. } else {
  417. ret = PR_Send(fd->lower, buf, amount, flags, timeout);
  418. }
  419. return ret;
  420. }
  421. /*
  422. * Need to handle cases where caller uses PR_Write instead of
  423. * PR_Send on the network socket
  424. */
  425. static PRInt32 PR_CALLBACK
  426. sasl_io_write(PRFileDesc *fd, const void *buf, PRInt32 amount)
  427. {
  428. return sasl_io_send(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT);
  429. }
  430. static PRStatus PR_CALLBACK
  431. sasl_pop_IO_layer(PRFileDesc* stack)
  432. {
  433. PRFileDesc* layer = NULL;
  434. sasl_io_private *sp = NULL;
  435. /* see if stack has the sasl io layer */
  436. if (!sasl_LayerID || !stack || !PR_GetIdentitiesLayer(stack, sasl_LayerID)) {
  437. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  438. "sasl_pop_IO_layer: no SASL IO layer\n" );
  439. return PR_SUCCESS;
  440. }
  441. /* remove the layer from the stack */
  442. layer = PR_PopIOLayer(stack, sasl_LayerID);
  443. if (!layer) {
  444. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  445. "sasl_pop_IO_layer: error - could not pop SASL IO layer\n" );
  446. return PR_FAILURE;
  447. }
  448. /* get our private data and clean it up */
  449. sp = sasl_get_io_private(layer);
  450. if (sp) {
  451. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  452. "sasl_pop_IO_layer: removing SASL IO layer\n" );
  453. /* Free the buffers */
  454. slapi_ch_free_string(&sp->encrypted_buffer);
  455. slapi_ch_free_string(&sp->decrypted_buffer);
  456. slapi_ch_free((void**)&sp);
  457. }
  458. layer->secret = NULL;
  459. if (layer->dtor) {
  460. layer->dtor(layer);
  461. }
  462. return PR_SUCCESS;
  463. }
  464. static PRStatus PR_CALLBACK
  465. closeLayer(PRFileDesc* stack)
  466. {
  467. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  468. "closeLayer: closing SASL IO layer\n" );
  469. if (PR_FAILURE == sasl_pop_IO_layer(stack)) {
  470. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  471. "closeLayer: error closing SASL IO layer\n" );
  472. return PR_FAILURE;
  473. }
  474. LDAPDebug0Args( LDAP_DEBUG_CONNS,
  475. "closeLayer: calling PR_Close to close other layers\n" );
  476. return PR_Close(stack);
  477. }
  478. static PRStatus PR_CALLBACK
  479. initialize(void)
  480. {
  481. sasl_LayerID = PR_GetUniqueIdentity(sasl_LayerName);
  482. if (PR_INVALID_IO_LAYER == sasl_LayerID) {
  483. return PR_FAILURE;
  484. } else {
  485. const PRIOMethods* defaults = PR_GetDefaultIOMethods();
  486. if (!defaults) {
  487. return PR_FAILURE;
  488. } else {
  489. memcpy(&sasl_IoMethods, defaults, sizeof(sasl_IoMethods));
  490. }
  491. }
  492. /* Customize methods: */
  493. sasl_IoMethods.recv = sasl_io_recv;
  494. sasl_IoMethods.send = sasl_io_send;
  495. sasl_IoMethods.close = closeLayer;
  496. sasl_IoMethods.write = sasl_io_write; /* some code uses PR_Write instead of PR_Send */
  497. return PR_SUCCESS;
  498. }
  499. /*
  500. * Push the SASL I/O layer on top of the current NSPR I/O layer of the prfd used
  501. * by the connection.
  502. */
  503. int
  504. sasl_io_enable(Connection *c)
  505. {
  506. PRStatus rv = PR_CallOnce(&sasl_callOnce, initialize);
  507. if (PR_SUCCESS == rv) {
  508. PRFileDesc* layer = PR_CreateIOLayerStub(sasl_LayerID, &sasl_IoMethods);
  509. sasl_io_private *sp = (sasl_io_private*) slapi_ch_calloc(1, sizeof(sasl_io_private));
  510. sasl_io_init_buffers(sp);
  511. layer->secret = sp;
  512. PR_Lock( c->c_mutex );
  513. sp->conn = c;
  514. rv = PR_PushIOLayer(c->c_prfd, PR_TOP_IO_LAYER, layer);
  515. PR_Unlock( c->c_mutex );
  516. if (rv) {
  517. LDAPDebug( LDAP_DEBUG_ANY,
  518. "sasl_io_enable: error enabling sasl io on connection %" NSPRIu64 " %d:%s\n", c->c_connid, rv, slapd_pr_strerror(rv) );
  519. } else {
  520. LDAPDebug( LDAP_DEBUG_CONNS,
  521. "sasl_io_enable: enabled sasl io on connection %" NSPRIu64 " \n", c->c_connid, 0, 0 );
  522. debug_print_layers(c->c_prfd);
  523. }
  524. }
  525. return (int)rv;
  526. }
  527. /*
  528. * Remove the SASL I/O layer from the top of the current NSPR I/O layer of the prfd used
  529. * by the connection. Must either be called within the connection lock, or be
  530. * called while the connection is not being referenced by another thread.
  531. */
  532. int
  533. sasl_io_cleanup(Connection *c)
  534. {
  535. int ret = 0;
  536. LDAPDebug( LDAP_DEBUG_CONNS,
  537. "sasl_io_cleanup for connection %" NSPRIu64 "\n", c->c_connid, 0, 0 );
  538. ret = sasl_pop_IO_layer(c->c_prfd);
  539. c->c_sasl_ssf = 0;
  540. return ret;
  541. }