cb_modify.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. static void cb_remove_illegal_mods(cb_backend_instance * inst, LDAPMod **mods);
  8. /*
  9. * Perform a modify operation
  10. *
  11. * Returns:
  12. * 0 - success
  13. * <0 - fail
  14. *
  15. */
  16. int
  17. chaining_back_modify ( Slapi_PBlock *pb )
  18. {
  19. Slapi_Backend *be;
  20. cb_backend_instance *cb;
  21. LDAPControl **ctrls, **serverctrls;
  22. int rc,parse_rc,msgid,i;
  23. LDAP *ld=NULL;
  24. char **referrals=NULL;
  25. LDAPMod ** mods;
  26. LDAPMessage * res;
  27. char *dn,* matched_msg, *error_msg;
  28. char *cnxerrbuf=NULL;
  29. time_t endtime;
  30. cb_outgoing_conn *cnx;
  31. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  32. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  33. return -1;
  34. }
  35. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  36. cb = cb_get_instance(be);
  37. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_MODIFY);
  38. /* Check wether the chaining BE is available or not */
  39. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  40. return -1;
  41. }
  42. slapi_pblock_get( pb, SLAPI_MODIFY_TARGET, &dn );
  43. if (cb_debug_on()) {
  44. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,"modify: target:<%s>\n",dn);
  45. }
  46. ctrls=serverctrls=NULL;
  47. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  48. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &ctrls );
  49. /* Check acls */
  50. if ( cb->local_acl && !cb->associated_be_is_disabled ) {
  51. char * errbuf=NULL;
  52. Slapi_Entry *te = slapi_entry_alloc();
  53. slapi_entry_set_dn(te,slapi_ch_strdup(dn));
  54. rc = slapi_acl_check_mods( pb, te, mods, &errbuf);
  55. slapi_entry_free(te);
  56. if ( rc != LDAP_SUCCESS ) {
  57. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  58. slapi_ch_free((void **)&errbuf);
  59. return -1;
  60. }
  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. slapi_ch_free((void **)&cnxerrbuf);
  66. /* ping the farm. If the farm is unreachable, we increment the counter */
  67. cb_ping_farm(cb,NULL,0);
  68. return -1;
  69. }
  70. /* Control management */
  71. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  72. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  73. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  74. /* Don't free mods here: are freed at the do_modify level */
  75. return -1;
  76. }
  77. if ( slapi_op_abandoned( pb )) {
  78. cb_release_op_connection(cb->pool,ld,0);
  79. /* Don't free mods here: are freed at the do_modify level */
  80. if ( NULL != ctrls)
  81. ldap_controls_free(ctrls);
  82. return -1;
  83. }
  84. /* Remove illegal attributes from the mods */
  85. cb_remove_illegal_mods(cb,mods);
  86. /* heart-beat management */
  87. if (cb->max_idle_time>0)
  88. endtime=current_time() + cb->max_idle_time;
  89. /* Send LDAP operation to the remote host */
  90. rc = ldap_modify_ext( ld, dn, mods, ctrls, NULL, &msgid );
  91. if ( NULL != ctrls)
  92. ldap_controls_free(ctrls);
  93. if ( rc != LDAP_SUCCESS ) {
  94. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
  95. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  96. return -1;
  97. }
  98. while ( 1 ) {
  99. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  100. /* connection handle released */
  101. return -1;
  102. }
  103. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  104. switch ( rc ) {
  105. case -1:
  106. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  107. ldap_err2string(rc), 0, NULL);
  108. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  109. if (res)
  110. ldap_msgfree(res);
  111. return -1;
  112. case 0:
  113. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  114. /* does not respond. give up and return a*/
  115. /* error to the client. */
  116. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  117. ldap_err2string(rc), 0, NULL);*/
  118. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  119. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  120. if (res)
  121. ldap_msgfree(res);
  122. return -1;
  123. }
  124. #ifdef CB_YIELD
  125. DS_Sleep(PR_INTERVAL_NO_WAIT);
  126. #endif
  127. break;
  128. default:
  129. matched_msg=error_msg=NULL;
  130. serverctrls=NULL;
  131. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  132. &error_msg, &referrals, &serverctrls, 1 );
  133. if ( parse_rc != LDAP_SUCCESS ) {
  134. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  135. ldap_err2string(parse_rc), 0, NULL);
  136. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  137. slapi_ch_free((void **)&matched_msg);
  138. slapi_ch_free((void **)&error_msg);
  139. if (serverctrls)
  140. ldap_controls_free(serverctrls);
  141. /* jarnou: free referrals */
  142. if (referrals)
  143. charray_free(referrals);
  144. return -1;
  145. }
  146. if ( rc != LDAP_SUCCESS ) {
  147. struct berval ** refs = referrals2berval(referrals);
  148. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  149. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  150. slapi_ch_free((void **)&matched_msg);
  151. slapi_ch_free((void **)&error_msg);
  152. if (refs)
  153. ber_bvecfree(refs);
  154. if (referrals)
  155. charray_free(referrals);
  156. if (serverctrls)
  157. ldap_controls_free(serverctrls);
  158. return -1;
  159. }
  160. cb_release_op_connection(cb->pool,ld,0);
  161. /* Add control response sent by the farm server */
  162. for (i=0; serverctrls && serverctrls[i];i++)
  163. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  164. /* SLAPI_ADD_RESCONTROL dups controls */
  165. if (serverctrls)
  166. ldap_controls_free(serverctrls);
  167. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  168. slapi_ch_free((void **)&matched_msg);
  169. slapi_ch_free((void **)&error_msg);
  170. if (referrals)
  171. charray_free(referrals);
  172. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  173. return 0;
  174. }
  175. }
  176. /* Never reached */
  177. /* return 0; */
  178. }
  179. /* Function removes mods which are not allowed over-the-wire */
  180. static void
  181. cb_remove_illegal_mods(cb_backend_instance *inst, LDAPMod **mods)
  182. {
  183. int i, j;
  184. LDAPMod *tmp;
  185. if ( inst->illegal_attributes != NULL ) { /* Unlikely to happen */
  186. PR_RWLock_Wlock(inst->rwl_config_lock);
  187. for (j=0; inst->illegal_attributes[j]; j++) {
  188. for ( i = 0; mods[i] != NULL; i++ ) {
  189. if (slapi_attr_types_equivalent(inst->illegal_attributes[j],mods[i]->mod_type)) {
  190. tmp = mods[i];
  191. for ( j = i; mods[j] != NULL; j++ ) {
  192. mods[j] = mods[j + 1];
  193. }
  194. slapi_ch_free( (void**)&(tmp->mod_type) );
  195. if ( tmp->mod_bvalues != NULL ) {
  196. ber_bvecfree( tmp->mod_bvalues );
  197. }
  198. slapi_ch_free( (void**)&tmp );
  199. i--;
  200. }
  201. }
  202. }
  203. PR_RWLock_Unlock(inst->rwl_config_lock);
  204. }
  205. }