cb_add.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /*
  40. * Perform an add operation
  41. *
  42. * Returns:
  43. * 0 - success
  44. * <0 - fail
  45. *
  46. */
  47. int
  48. chaining_back_add ( Slapi_PBlock *pb )
  49. {
  50. Slapi_Backend *be;
  51. Slapi_Entry *e;
  52. cb_backend_instance *cb;
  53. LDAPControl **serverctrls=NULL;
  54. LDAPControl **ctrls=NULL;
  55. int rc,parse_rc,msgid,i;
  56. LDAP *ld=NULL;
  57. char **referrals=NULL;
  58. LDAPMod ** mods;
  59. LDAPMessage * res;
  60. char *dn,* matched_msg, *error_msg;
  61. char *cnxerrbuf=NULL;
  62. time_t endtime;
  63. cb_outgoing_conn *cnx;
  64. if ( (rc=cb_forward_operation(pb)) != LDAP_SUCCESS ) {
  65. cb_send_ldap_result( pb, rc, NULL, "Remote data access disabled", 0, NULL );
  66. return -1;
  67. }
  68. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  69. cb = cb_get_instance(be);
  70. /* Update monitor info */
  71. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_ADD);
  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_ADD_TARGET, &dn );
  77. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &e );
  78. /* Check local access controls */
  79. if (cb->local_acl && !cb->associated_be_is_disabled) {
  80. char * errbuf=NULL;
  81. rc = cb_access_allowed (pb, e, NULL, NULL, SLAPI_ACL_ADD, &errbuf);
  82. if ( rc != LDAP_SUCCESS ) {
  83. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  84. slapi_ch_free((void **)&errbuf);
  85. return -1;
  86. }
  87. }
  88. /* Build LDAPMod from the SLapi_Entry */
  89. cb_eliminate_illegal_attributes(cb,e);
  90. if ((rc = slapi_entry2mods ((const Slapi_Entry *)e, NULL, &mods)) != LDAP_SUCCESS) {
  91. cb_send_ldap_result( pb, rc,NULL,NULL, 0, NULL);
  92. return -1;
  93. }
  94. /* Grab a connection handle */
  95. if ((rc = cb_get_connection(cb->pool,&ld,&cnx,NULL,&cnxerrbuf)) != LDAP_SUCCESS) {
  96. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR,NULL,cnxerrbuf, 0, NULL);
  97. ldap_mods_free(mods,1);
  98. if (cnxerrbuf) {
  99. PR_smprintf_free(cnxerrbuf);
  100. }
  101. /* ping the farm. If the farm is unreachable, we increment the counter */
  102. cb_ping_farm(cb,NULL,0);
  103. return -1;
  104. }
  105. /* Control management */
  106. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH)) != LDAP_SUCCESS ) {
  107. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  108. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  109. ldap_mods_free(mods,1);
  110. return -1;
  111. }
  112. if ( slapi_op_abandoned( pb )) {
  113. cb_release_op_connection(cb->pool,ld,0);
  114. ldap_mods_free(mods,1);
  115. if ( NULL != ctrls)
  116. ldap_controls_free(ctrls);
  117. return -1;
  118. }
  119. /* heart-beat management */
  120. if (cb->max_idle_time>0)
  121. endtime=current_time() + cb->max_idle_time;
  122. /* Send LDAP operation to the remote host */
  123. rc = ldap_add_ext( ld, dn, mods, ctrls, NULL, &msgid );
  124. if ( NULL != ctrls)
  125. ldap_controls_free(ctrls);
  126. if ( rc != LDAP_SUCCESS ) {
  127. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  128. ldap_err2string(rc), 0, NULL);
  129. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  130. ldap_mods_free(mods,1);
  131. return -1;
  132. }
  133. /*
  134. * Poll the server for the results of the add operation.
  135. * Check for abandoned operation regularly.
  136. */
  137. while ( 1 ) {
  138. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  139. /* connection handle released in cb_check_forward_abandon() */
  140. ldap_mods_free(mods,1);
  141. return -1;
  142. }
  143. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  144. switch ( rc ) {
  145. case -1:
  146. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  147. ldap_err2string(rc), 0, NULL);
  148. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  149. ldap_mods_free(mods,1);
  150. if (res)
  151. ldap_msgfree(res);
  152. return -1;
  153. case 0:
  154. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  155. /* does not respond. give up and return a*/
  156. /* error to the client. */
  157. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  158. ldap_err2string(rc), 0, NULL);*/
  159. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  160. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  161. ldap_mods_free(mods,1);
  162. if (res)
  163. ldap_msgfree(res);
  164. return -1;
  165. }
  166. #ifdef CB_YIELD
  167. DS_Sleep(PR_INTERVAL_NO_WAIT);
  168. #endif
  169. break;
  170. default:
  171. serverctrls=NULL;
  172. matched_msg=error_msg=NULL;
  173. referrals=NULL;
  174. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  175. &error_msg, &referrals, &serverctrls, 1 );
  176. if ( parse_rc != LDAP_SUCCESS ) {
  177. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  178. ldap_err2string(parse_rc), 0, NULL);
  179. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  180. ldap_mods_free(mods,1);
  181. slapi_ch_free((void **)&matched_msg);
  182. slapi_ch_free((void **)&error_msg);
  183. if (serverctrls)
  184. ldap_controls_free(serverctrls);
  185. /* jarnou: free referrals */
  186. if (referrals)
  187. charray_free(referrals);
  188. return -1;
  189. }
  190. if ( rc != LDAP_SUCCESS ) {
  191. struct berval ** refs = referrals2berval(referrals);
  192. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  193. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  194. ldap_mods_free(mods,1);
  195. slapi_ch_free((void **)&matched_msg);
  196. slapi_ch_free((void **)&error_msg);
  197. if (refs)
  198. ber_bvecfree(refs);
  199. if (referrals)
  200. charray_free(referrals);
  201. if (serverctrls)
  202. ldap_controls_free(serverctrls);
  203. return -1;
  204. }
  205. ldap_mods_free(mods,1 );
  206. cb_release_op_connection(cb->pool,ld,0);
  207. /* Add control response sent by the farm server */
  208. for (i=0; serverctrls && serverctrls[i];i++)
  209. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  210. if (serverctrls)
  211. ldap_controls_free(serverctrls);
  212. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  213. slapi_ch_free((void **)&matched_msg);
  214. slapi_ch_free((void **)&error_msg);
  215. if (referrals)
  216. charray_free(referrals);
  217. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  218. slapi_entry_free(e);
  219. slapi_pblock_set( pb, SLAPI_ADD_ENTRY, NULL );
  220. return 0;
  221. }
  222. }
  223. /* Never reached */
  224. }