cb_bind.c 11 KB

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