cb_add.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  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. if (cnxerrbuf) {
  67. PR_smprintf_free(cnxerrbuf);
  68. }
  69. /* ping the farm. If the farm is unreachable, we increment the counter */
  70. cb_ping_farm(cb,NULL,0);
  71. return -1;
  72. }
  73. /* Control management */
  74. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH)) != LDAP_SUCCESS ) {
  75. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  76. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  77. ldap_mods_free(mods,1);
  78. return -1;
  79. }
  80. if ( slapi_op_abandoned( pb )) {
  81. cb_release_op_connection(cb->pool,ld,0);
  82. ldap_mods_free(mods,1);
  83. if ( NULL != ctrls)
  84. ldap_controls_free(ctrls);
  85. return -1;
  86. }
  87. /* heart-beat management */
  88. if (cb->max_idle_time>0)
  89. endtime=current_time() + cb->max_idle_time;
  90. /* Send LDAP operation to the remote host */
  91. rc = ldap_add_ext( ld, dn, mods, ctrls, NULL, &msgid );
  92. if ( NULL != ctrls)
  93. ldap_controls_free(ctrls);
  94. if ( rc != LDAP_SUCCESS ) {
  95. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  96. ldap_err2string(rc), 0, NULL);
  97. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  98. ldap_mods_free(mods,1);
  99. return -1;
  100. }
  101. /*
  102. * Poll the server for the results of the add operation.
  103. * Check for abandoned operation regularly.
  104. */
  105. while ( 1 ) {
  106. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  107. /* connection handle released in cb_check_forward_abandon() */
  108. ldap_mods_free(mods,1);
  109. return -1;
  110. }
  111. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  112. switch ( rc ) {
  113. case -1:
  114. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  115. ldap_err2string(rc), 0, NULL);
  116. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  117. ldap_mods_free(mods,1);
  118. if (res)
  119. ldap_msgfree(res);
  120. return -1;
  121. case 0:
  122. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  123. /* does not respond. give up and return a*/
  124. /* error to the client. */
  125. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  126. ldap_err2string(rc), 0, NULL);*/
  127. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  128. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  129. ldap_mods_free(mods,1);
  130. if (res)
  131. ldap_msgfree(res);
  132. return -1;
  133. }
  134. #ifdef CB_YIELD
  135. DS_Sleep(PR_INTERVAL_NO_WAIT);
  136. #endif
  137. break;
  138. default:
  139. serverctrls=NULL;
  140. matched_msg=error_msg=NULL;
  141. referrals=NULL;
  142. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  143. &error_msg, &referrals, &serverctrls, 1 );
  144. if ( parse_rc != LDAP_SUCCESS ) {
  145. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  146. ldap_err2string(parse_rc), 0, NULL);
  147. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  148. ldap_mods_free(mods,1);
  149. slapi_ch_free((void **)&matched_msg);
  150. slapi_ch_free((void **)&error_msg);
  151. if (serverctrls)
  152. ldap_controls_free(serverctrls);
  153. /* jarnou: free referrals */
  154. if (referrals)
  155. charray_free(referrals);
  156. return -1;
  157. }
  158. if ( rc != LDAP_SUCCESS ) {
  159. struct berval ** refs = referrals2berval(referrals);
  160. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  161. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  162. ldap_mods_free(mods,1);
  163. slapi_ch_free((void **)&matched_msg);
  164. slapi_ch_free((void **)&error_msg);
  165. if (refs)
  166. ber_bvecfree(refs);
  167. if (referrals)
  168. charray_free(referrals);
  169. if (serverctrls)
  170. ldap_controls_free(serverctrls);
  171. return -1;
  172. }
  173. ldap_mods_free(mods,1 );
  174. cb_release_op_connection(cb->pool,ld,0);
  175. /* Add control response sent by the farm server */
  176. for (i=0; serverctrls && serverctrls[i];i++)
  177. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  178. if (serverctrls)
  179. ldap_controls_free(serverctrls);
  180. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  181. slapi_ch_free((void **)&matched_msg);
  182. slapi_ch_free((void **)&error_msg);
  183. if (referrals)
  184. charray_free(referrals);
  185. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  186. slapi_entry_free(e);
  187. slapi_pblock_set( pb, SLAPI_ADD_ENTRY, NULL );
  188. return 0;
  189. }
  190. }
  191. /* Never reached */
  192. }