1
0

cb_bind.c 11 KB

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