cb_compare.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /*
  43. * Perform a compare operation
  44. *
  45. * Returns:
  46. * 0 - success
  47. * <0 - fail
  48. *
  49. */
  50. int
  51. chaining_back_compare ( Slapi_PBlock *pb )
  52. {
  53. Slapi_Backend * be;
  54. cb_backend_instance *cb=NULL;
  55. struct berval *bval=NULL;
  56. LDAPControl **ctrls, **serverctrls;
  57. int rc,parse_rc,msgid,i,checkacl;
  58. LDAP *ld=NULL;
  59. char **referrals=NULL;
  60. LDAPMessage * res;
  61. char *type,*dn,* matched_msg, *error_msg;
  62. char *cnxerrbuf=NULL;
  63. time_t endtime;
  64. cb_outgoing_conn *cnx;
  65. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  66. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  67. return -1;
  68. }
  69. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  70. cb = cb_get_instance(be);
  71. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_COMPARE);
  72. /* Check wether the chaining BE is available or not */
  73. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  74. return -1;
  75. }
  76. slapi_pblock_get( pb, SLAPI_COMPARE_TARGET, &dn );
  77. slapi_pblock_get( pb, SLAPI_COMPARE_TYPE, &type );
  78. slapi_pblock_get( pb, SLAPI_COMPARE_VALUE, &bval );
  79. /*
  80. * Check local acls
  81. * No need to lock the config to access cb->local_acl
  82. */
  83. checkacl=cb->local_acl && !cb->associated_be_is_disabled;
  84. if (checkacl) {
  85. char * errbuf=NULL;
  86. Slapi_Entry *te = slapi_entry_alloc();
  87. slapi_entry_set_dn(te,slapi_ch_strdup(dn));
  88. rc = cb_access_allowed (pb, te, type, bval, SLAPI_ACL_COMPARE,&errbuf);
  89. slapi_entry_free(te);
  90. if ( rc != LDAP_SUCCESS ) {
  91. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  92. slapi_ch_free((void **) &errbuf);
  93. return 1;
  94. }
  95. }
  96. /*
  97. * Grab a connection handle
  98. */
  99. rc = cb_get_connection(cb->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. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  109. cnxerrbuf, 0, NULL);
  110. slapi_ch_free_string(&cnxerrbuf);
  111. /* ping the farm.
  112. * If the farm is unreachable, we increment the counter */
  113. cb_ping_farm(cb, NULL, 0);
  114. return 1;
  115. }
  116. /*
  117. * Control management
  118. */
  119. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  120. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  121. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  122. return 1;
  123. }
  124. if ( slapi_op_abandoned( pb )) {
  125. cb_release_op_connection(cb->pool,ld,0);
  126. if ( NULL != ctrls)
  127. ldap_controls_free(ctrls);
  128. return -1;
  129. }
  130. /* heart-beat management */
  131. if (cb->max_idle_time>0)
  132. endtime=current_time() + cb->max_idle_time;
  133. /*
  134. * Send LDAP operation to the remote host
  135. */
  136. rc = ldap_compare_ext( ld, dn, type, bval, ctrls, NULL, &msgid );
  137. if ( NULL != ctrls)
  138. ldap_controls_free(ctrls);
  139. if ( rc != LDAP_SUCCESS ) {
  140. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  141. ldap_err2string(rc), 0, NULL);
  142. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  143. return 1;
  144. }
  145. while ( 1 ) {
  146. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  147. return -1;
  148. }
  149. /* No need to lock the config to access cb->abandon_timeout */
  150. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  151. switch ( rc ) {
  152. case -1:
  153. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  154. ldap_err2string(rc), 0, NULL);
  155. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  156. if (res)
  157. ldap_msgfree(res);
  158. return 1;
  159. case 0:
  160. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  161. /* does not respond. give up and return a*/
  162. /* error to the client. */
  163. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  164. ldap_err2string(rc), 0, NULL);*/
  165. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  166. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  167. if (res)
  168. ldap_msgfree(res);
  169. return 1;
  170. }
  171. #ifdef CB_YIELD
  172. DS_Sleep(PR_INTERVAL_NO_WAIT);
  173. #endif
  174. break;
  175. default:
  176. matched_msg=error_msg=NULL;
  177. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  178. &error_msg, &referrals, &serverctrls, 1 );
  179. if ( parse_rc != LDAP_SUCCESS ) {
  180. static int warned_parse_rc = 0;
  181. if (!warned_parse_rc) {
  182. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  183. "%s%s%s\n",
  184. matched_msg?matched_msg:"",
  185. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  186. ldap_err2string(parse_rc));
  187. warned_parse_rc = 1;
  188. }
  189. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  190. ENDUSERMSG, 0, NULL );
  191. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  192. slapi_ch_free((void **)&matched_msg);
  193. slapi_ch_free((void **)&error_msg);
  194. if (serverctrls)
  195. ldap_controls_free(serverctrls);
  196. /* jarnou: free referrals */
  197. if (referrals)
  198. charray_free(referrals);
  199. return 1;
  200. }
  201. switch ( rc ) {
  202. case LDAP_COMPARE_TRUE:
  203. case LDAP_COMPARE_FALSE:
  204. break;
  205. default: {
  206. struct berval ** refs = referrals2berval(referrals);
  207. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  208. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  209. slapi_ch_free((void **)&matched_msg);
  210. slapi_ch_free((void **)&error_msg);
  211. if (refs)
  212. ber_bvecfree(refs);
  213. if (referrals)
  214. charray_free(referrals);
  215. if (serverctrls)
  216. ldap_controls_free(serverctrls);
  217. return 1;
  218. }
  219. }
  220. /* Add control response sent by the farm server */
  221. for (i=0; serverctrls && serverctrls[i];i++)
  222. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  223. if (serverctrls)
  224. ldap_controls_free(serverctrls);
  225. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  226. slapi_ch_free((void **)&matched_msg);
  227. slapi_ch_free((void **)&error_msg);
  228. if (referrals)
  229. charray_free(referrals);
  230. cb_send_ldap_result( pb, rc , NULL, NULL, 0, NULL );
  231. cb_release_op_connection(cb->pool,ld,0);
  232. return 0;
  233. }
  234. }
  235. /* Never reached */
  236. /* return 0; */
  237. }