cb_add.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. #include "cb.h"
  7. /*
  8. * Perform an add operation
  9. *
  10. * Returns:
  11. * 0 - success
  12. * <0 - fail
  13. *
  14. */
  15. int
  16. chaining_back_add ( Slapi_PBlock *pb )
  17. {
  18. Slapi_Backend *be;
  19. Slapi_Entry *e;
  20. cb_backend_instance *cb;
  21. LDAPControl **serverctrls=NULL;
  22. LDAPControl **ctrls=NULL;
  23. int rc,parse_rc,msgid,i;
  24. LDAP *ld=NULL;
  25. char **referrals=NULL;
  26. LDAPMod ** mods;
  27. LDAPMessage * res;
  28. char *dn,* matched_msg, *error_msg;
  29. char *cnxerrbuf=NULL;
  30. time_t endtime;
  31. cb_outgoing_conn *cnx;
  32. if ( (rc=cb_forward_operation(pb)) != LDAP_SUCCESS ) {
  33. cb_send_ldap_result( pb, rc, NULL, "Remote data access disabled", 0, NULL );
  34. return -1;
  35. }
  36. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  37. cb = cb_get_instance(be);
  38. /* Update monitor info */
  39. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_ADD);
  40. /* Check wether the chaining BE is available or not */
  41. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  42. return -1;
  43. }
  44. slapi_pblock_get( pb, SLAPI_ADD_TARGET, &dn );
  45. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &e );
  46. /* Check local access controls */
  47. if (cb->local_acl && !cb->associated_be_is_disabled) {
  48. char * errbuf=NULL;
  49. rc = cb_access_allowed (pb, e, NULL, NULL, SLAPI_ACL_ADD, &errbuf);
  50. if ( rc != LDAP_SUCCESS ) {
  51. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  52. slapi_ch_free((void **)&errbuf);
  53. return -1;
  54. }
  55. }
  56. /* Build LDAPMod from the SLapi_Entry */
  57. cb_eliminate_illegal_attributes(cb,e);
  58. if ((rc = slapi_entry2mods ((const Slapi_Entry *)e, NULL, &mods)) != LDAP_SUCCESS) {
  59. cb_send_ldap_result( pb, rc,NULL,NULL, 0, NULL);
  60. return -1;
  61. }
  62. /* Grab a connection handle */
  63. if ((rc = cb_get_connection(cb->pool,&ld,&cnx,NULL,&cnxerrbuf)) != LDAP_SUCCESS) {
  64. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR,NULL,cnxerrbuf, 0, NULL);
  65. ldap_mods_free(mods,1);
  66. slapi_ch_free((void **)&cnxerrbuf);
  67. /* ping the farm. If the farm is unreachable, we increment the counter */
  68. cb_ping_farm(cb,NULL,0);
  69. return -1;
  70. }
  71. /* Control management */
  72. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH)) != LDAP_SUCCESS ) {
  73. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  74. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  75. ldap_mods_free(mods,1);
  76. return -1;
  77. }
  78. if ( slapi_op_abandoned( pb )) {
  79. cb_release_op_connection(cb->pool,ld,0);
  80. ldap_mods_free(mods,1);
  81. if ( NULL != ctrls)
  82. ldap_controls_free(ctrls);
  83. return -1;
  84. }
  85. /* heart-beat management */
  86. if (cb->max_idle_time>0)
  87. endtime=current_time() + cb->max_idle_time;
  88. /* Send LDAP operation to the remote host */
  89. rc = ldap_add_ext( ld, dn, mods, ctrls, NULL, &msgid );
  90. if ( NULL != ctrls)
  91. ldap_controls_free(ctrls);
  92. if ( rc != LDAP_SUCCESS ) {
  93. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  94. ldap_err2string(rc), 0, NULL);
  95. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  96. ldap_mods_free(mods,1);
  97. return -1;
  98. }
  99. /*
  100. * Poll the server for the results of the add operation.
  101. * Check for abandoned operation regularly.
  102. */
  103. while ( 1 ) {
  104. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  105. /* connection handle released in cb_check_forward_abandon() */
  106. ldap_mods_free(mods,1);
  107. return -1;
  108. }
  109. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  110. switch ( rc ) {
  111. case -1:
  112. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  113. ldap_err2string(rc), 0, NULL);
  114. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  115. ldap_mods_free(mods,1);
  116. if (res)
  117. ldap_msgfree(res);
  118. return -1;
  119. case 0:
  120. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  121. /* does not respond. give up and return a*/
  122. /* error to the client. */
  123. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  124. ldap_err2string(rc), 0, NULL);*/
  125. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  126. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  127. ldap_mods_free(mods,1);
  128. if (res)
  129. ldap_msgfree(res);
  130. return -1;
  131. }
  132. #ifdef CB_YIELD
  133. DS_Sleep(PR_INTERVAL_NO_WAIT);
  134. #endif
  135. break;
  136. default:
  137. serverctrls=NULL;
  138. matched_msg=error_msg=NULL;
  139. referrals=NULL;
  140. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  141. &error_msg, &referrals, &serverctrls, 1 );
  142. if ( parse_rc != LDAP_SUCCESS ) {
  143. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  144. ldap_err2string(parse_rc), 0, NULL);
  145. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  146. ldap_mods_free(mods,1);
  147. slapi_ch_free((void **)&matched_msg);
  148. slapi_ch_free((void **)&error_msg);
  149. if (serverctrls)
  150. ldap_controls_free(serverctrls);
  151. /* jarnou: free referrals */
  152. if (referrals)
  153. charray_free(referrals);
  154. return -1;
  155. }
  156. if ( rc != LDAP_SUCCESS ) {
  157. struct berval ** refs = referrals2berval(referrals);
  158. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  159. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  160. ldap_mods_free(mods,1);
  161. slapi_ch_free((void **)&matched_msg);
  162. slapi_ch_free((void **)&error_msg);
  163. if (refs)
  164. ber_bvecfree(refs);
  165. if (referrals)
  166. charray_free(referrals);
  167. if (serverctrls)
  168. ldap_controls_free(serverctrls);
  169. return -1;
  170. }
  171. ldap_mods_free(mods,1 );
  172. cb_release_op_connection(cb->pool,ld,0);
  173. /* Add control response sent by the farm server */
  174. for (i=0; serverctrls && serverctrls[i];i++)
  175. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  176. if (serverctrls)
  177. ldap_controls_free(serverctrls);
  178. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  179. slapi_ch_free((void **)&matched_msg);
  180. slapi_ch_free((void **)&error_msg);
  181. if (referrals)
  182. charray_free(referrals);
  183. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  184. slapi_entry_free(e);
  185. slapi_pblock_set( pb, SLAPI_ADD_ENTRY, NULL );
  186. return 0;
  187. }
  188. }
  189. /* Never reached */
  190. }