cb_bind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "cb.h"
  42. static void
  43. cb_free_bervals( struct berval **bvs );
  44. static int
  45. cb_sasl_bind_once_s( cb_conn_pool *pool, char *dn, int method, char * mechanism,
  46. struct berval *creds, LDAPControl **reqctrls,
  47. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  48. LDAPControl ***resctrlsp , int * status);
  49. /*
  50. * Attempt to chain a bind request off to "srvr." We return an LDAP error
  51. * code that indicates whether we successfully got a response from the
  52. * other server or not. If we succeed, we return LDAP_SUCCESS and *lderrnop
  53. * is set to the result code from the remote server.
  54. *
  55. * Note that in the face of "ldap server down" or "ldap connect failed" errors
  56. * we make up to "tries" attempts to bind to the remote server. Since we
  57. * are only interested in recovering silently when the remote server is up
  58. * but decided to close our connection, we retry without pausing between
  59. * attempts.
  60. */
  61. static int
  62. cb_sasl_bind_s(Slapi_PBlock * pb, cb_conn_pool *pool, int tries,
  63. char *dn, int method,char * mechanism, struct berval *creds, LDAPControl **reqctrls,
  64. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  65. LDAPControl ***resctrlsp ,int *status) {
  66. int rc;
  67. do {
  68. /* check to see if operation has been abandoned...*/
  69. if (LDAP_AUTH_SIMPLE!=method)
  70. return LDAP_AUTH_METHOD_NOT_SUPPORTED;
  71. if ( slapi_op_abandoned( pb )) {
  72. rc = LDAP_USER_CANCELLED;
  73. } else {
  74. rc = cb_sasl_bind_once_s( pool, dn, method,mechanism, creds, reqctrls,
  75. matcheddnp, errmsgp, refurlsp, resctrlsp ,status);
  76. }
  77. } while ( CB_LDAP_CONN_ERROR( rc ) && --tries > 0 );
  78. return( rc );
  79. }
  80. static int
  81. cb_sasl_bind_once_s( cb_conn_pool *pool, char *dn, int method, char * mechanism,
  82. struct berval *creds, LDAPControl **reqctrls,
  83. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  84. LDAPControl ***resctrlsp , int * status )
  85. {
  86. int rc, msgid;
  87. char **referrals;
  88. struct timeval timeout_copy, *timeout;
  89. LDAPMessage *result=NULL;
  90. LDAP *ld=NULL;
  91. char *cnxerrbuf=NULL;
  92. cb_outgoing_conn *cnx;
  93. int version=LDAP_VERSION3;
  94. /* Grab an LDAP connection to use for this bind. */
  95. PR_RWLock_Rlock(pool->rwl_config_lock);
  96. timeout_copy.tv_sec = pool->conn.bind_timeout.tv_sec;
  97. timeout_copy.tv_usec = pool->conn.bind_timeout.tv_usec;
  98. PR_RWLock_Unlock(pool->rwl_config_lock);
  99. rc = cb_get_connection(pool, &ld, &cnx, NULL, &cnxerrbuf);
  100. if (LDAP_SUCCESS != rc) {
  101. static int warned_get_conn = 0;
  102. if (!warned_get_conn) {
  103. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  104. "cb_get_connection failed (%d) %s\n",
  105. rc, ldap_err2string(rc));
  106. warned_get_conn = 1;
  107. }
  108. *errmsgp = cnxerrbuf;
  109. goto release_and_return;
  110. }
  111. /* Send the bind operation (need to retry on LDAP_SERVER_DOWN) */
  112. ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  113. if (( rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, creds, reqctrls,
  114. NULL, &msgid )) != LDAP_SUCCESS ) {
  115. goto release_and_return;
  116. }
  117. /* XXXSD what is the exact semantics of bind_to ? it is used to get a
  118. connection handle and later to bind ==> bind op may last 2*bind_to
  119. from the user point of view
  120. confusion comes from teh fact that bind to is used 2for 3 differnt thinks,
  121. */
  122. /*
  123. * determine timeout value (how long we will wait for a response)
  124. * if timeout is zero'd, we wait indefinitely.
  125. */
  126. if ( timeout_copy.tv_sec == 0 && timeout_copy.tv_usec == 0 ) {
  127. timeout = NULL;
  128. } else {
  129. timeout = &timeout_copy;
  130. }
  131. /*
  132. * Wait for a result.
  133. */
  134. rc = ldap_result( ld, msgid, 1, timeout, &result );
  135. /*
  136. * Interpret the result.
  137. */
  138. if ( rc == 0 ) { /* timeout */
  139. /*
  140. * Timed out waiting for a reply from the server.
  141. */
  142. rc = LDAP_TIMEOUT;
  143. } else if ( rc < 0 ) {
  144. /* Some other error occurred (no result received). */
  145. char * matcheddnp2, * errmsgp2;
  146. matcheddnp2=errmsgp2=NULL;
  147. rc = slapi_ldap_get_lderrno( ld, &matcheddnp2, &errmsgp2 );
  148. /* Need to allocate errmsgs */
  149. if (matcheddnp2)
  150. *matcheddnp=slapi_ch_strdup(matcheddnp2);
  151. if (errmsgp2)
  152. *errmsgp=slapi_ch_strdup(errmsgp2);
  153. if ( LDAP_SUCCESS != rc ) {
  154. static int warned_bind_once = 0;
  155. if (!warned_bind_once) {
  156. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  157. "cb_sasl_bind_once_s failed (%s%s%s)\n",
  158. matcheddnp?matcheddnp:"",
  159. (matcheddnp&&(*matcheddnp!='\0'))?": ":"",
  160. ldap_err2string(rc));
  161. warned_bind_once = 1;
  162. }
  163. }
  164. } else {
  165. /* Got a result from remote server -- parse it.*/
  166. char * matcheddnp2, * errmsgp2;
  167. matcheddnp2=errmsgp2=NULL;
  168. *resctrlsp=NULL;
  169. rc = ldap_parse_result( ld, result, status, &matcheddnp2, &errmsgp2,
  170. &referrals, resctrlsp, 1 );
  171. if ( referrals != NULL ) {
  172. *refurlsp = referrals2berval( referrals );
  173. slapi_ldap_value_free( referrals );
  174. }
  175. /* realloc matcheddn & errmsg because the mem alloc model */
  176. /* may differ from malloc */
  177. if (matcheddnp2) {
  178. *matcheddnp=slapi_ch_strdup(matcheddnp2);
  179. ldap_memfree(matcheddnp2);
  180. }
  181. if (errmsgp2) {
  182. *errmsgp=slapi_ch_strdup(errmsgp2);
  183. ldap_memfree(errmsgp2);
  184. }
  185. }
  186. release_and_return:
  187. if ( ld != NULL ) {
  188. cb_release_op_connection( pool, ld, CB_LDAP_CONN_ERROR( rc ));
  189. }
  190. return( rc );
  191. }
  192. int
  193. chainingdb_bind( Slapi_PBlock *pb ) {
  194. int status=LDAP_SUCCESS;
  195. int allocated_errmsg;
  196. int rc=LDAP_SUCCESS;
  197. cb_backend_instance *cb;
  198. Slapi_Backend *be;
  199. char *dn;
  200. int method;
  201. struct berval *creds, **urls;
  202. char *matcheddn,*errmsg;
  203. LDAPControl **reqctrls, **resctrls, **ctrls;
  204. char * mechanism;
  205. int freectrls=1;
  206. int bind_retry;
  207. if ( LDAP_SUCCESS != (rc = cb_forward_operation(pb) )) {
  208. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  209. return SLAPI_BIND_FAIL;
  210. }
  211. ctrls=NULL;
  212. /* don't add proxy auth control. use this call to check for supported */
  213. /* controls only. */
  214. if ( LDAP_SUCCESS != ( rc = cb_update_controls( pb, NULL, &ctrls, 0 )) ) {
  215. cb_send_ldap_result( pb, rc, NULL, NULL, 0, NULL );
  216. if (ctrls)
  217. ldap_controls_free(ctrls);
  218. return SLAPI_BIND_FAIL;
  219. }
  220. if (ctrls)
  221. ldap_controls_free(ctrls);
  222. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  223. slapi_pblock_get( pb, SLAPI_BIND_TARGET, &dn );
  224. slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method );
  225. slapi_pblock_get( pb, SLAPI_BIND_SASLMECHANISM, &mechanism);
  226. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &creds );
  227. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &reqctrls );
  228. cb = cb_get_instance(be);
  229. if ( NULL == dn )
  230. dn="";
  231. /* always allow noauth simple binds */
  232. if (( method == LDAP_AUTH_SIMPLE) && creds->bv_len == 0 ) {
  233. return( SLAPI_BIND_ANONYMOUS );
  234. }
  235. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_BIND);
  236. matcheddn=errmsg=NULL;
  237. allocated_errmsg = 0;
  238. resctrls=NULL;
  239. urls=NULL;
  240. /* Check wether the chaining BE is available or not */
  241. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  242. return -1;
  243. }
  244. PR_RWLock_Rlock(cb->rwl_config_lock);
  245. bind_retry=cb->bind_retry;
  246. PR_RWLock_Unlock(cb->rwl_config_lock);
  247. rc = cb_sasl_bind_s(pb, cb->bind_pool, bind_retry, dn, method,
  248. mechanism, creds, reqctrls, &matcheddn, &errmsg,
  249. &urls, &resctrls, &status);
  250. if ( LDAP_SUCCESS == rc ) {
  251. rc = status;
  252. allocated_errmsg = 1;
  253. } else if ( LDAP_USER_CANCELLED != rc ) {
  254. errmsg = ldap_err2string( rc );
  255. if (rc == LDAP_TIMEOUT) {
  256. cb_ping_farm(cb,NULL,0);
  257. }
  258. rc = LDAP_OPERATIONS_ERROR;
  259. }
  260. if ( rc != LDAP_USER_CANCELLED ) { /* not abandoned */
  261. if ( resctrls != NULL ) {
  262. slapi_pblock_set( pb, SLAPI_RESCONTROLS, resctrls );
  263. freectrls=0;
  264. }
  265. if ( rc != LDAP_SUCCESS ) {
  266. cb_send_ldap_result( pb, rc, matcheddn, errmsg, 0, urls );
  267. }
  268. }
  269. if ( urls != NULL ) {
  270. cb_free_bervals( urls );
  271. }
  272. if ( freectrls && ( resctrls != NULL )) {
  273. ldap_controls_free( resctrls );
  274. }
  275. slapi_ch_free((void **)& matcheddn );
  276. if ( allocated_errmsg && errmsg != NULL ) {
  277. slapi_ch_free((void **)& errmsg );
  278. }
  279. return ((rc == LDAP_SUCCESS ) ? SLAPI_BIND_SUCCESS : SLAPI_BIND_FAIL );
  280. }
  281. static void
  282. cb_free_bervals( struct berval **bvs )
  283. {
  284. int i;
  285. if ( bvs != NULL ) {
  286. for ( i = 0; bvs[ i ] != NULL; ++i ) {
  287. slapi_ch_free( (void **)&bvs[ i ] );
  288. }
  289. }
  290. slapi_ch_free( (void **)&bvs );
  291. }