cb_modify.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. 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. if (cnxerrbuf) {
  66. PR_smprintf_free(cnxerrbuf);
  67. }
  68. /* ping the farm. If the farm is unreachable, we increment the counter */
  69. cb_ping_farm(cb,NULL,0);
  70. return -1;
  71. }
  72. /* Control management */
  73. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  74. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  75. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  76. /* Don't free mods here: are freed at the do_modify level */
  77. return -1;
  78. }
  79. if ( slapi_op_abandoned( pb )) {
  80. cb_release_op_connection(cb->pool,ld,0);
  81. /* Don't free mods here: are freed at the do_modify level */
  82. if ( NULL != ctrls)
  83. ldap_controls_free(ctrls);
  84. return -1;
  85. }
  86. /* Remove illegal attributes from the mods */
  87. cb_remove_illegal_mods(cb,mods);
  88. /* heart-beat management */
  89. if (cb->max_idle_time>0)
  90. endtime=current_time() + cb->max_idle_time;
  91. /* Send LDAP operation to the remote host */
  92. rc = ldap_modify_ext( ld, dn, mods, ctrls, NULL, &msgid );
  93. if ( NULL != ctrls)
  94. ldap_controls_free(ctrls);
  95. if ( rc != LDAP_SUCCESS ) {
  96. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
  97. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  98. return -1;
  99. }
  100. while ( 1 ) {
  101. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  102. /* connection handle released */
  103. return -1;
  104. }
  105. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  106. switch ( rc ) {
  107. case -1:
  108. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  109. ldap_err2string(rc), 0, NULL);
  110. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  111. if (res)
  112. ldap_msgfree(res);
  113. return -1;
  114. case 0:
  115. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  116. /* does not respond. give up and return a*/
  117. /* error to the client. */
  118. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  119. ldap_err2string(rc), 0, NULL);*/
  120. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  121. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  122. if (res)
  123. ldap_msgfree(res);
  124. return -1;
  125. }
  126. #ifdef CB_YIELD
  127. DS_Sleep(PR_INTERVAL_NO_WAIT);
  128. #endif
  129. break;
  130. default:
  131. matched_msg=error_msg=NULL;
  132. serverctrls=NULL;
  133. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  134. &error_msg, &referrals, &serverctrls, 1 );
  135. if ( parse_rc != LDAP_SUCCESS ) {
  136. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  137. ldap_err2string(parse_rc), 0, NULL);
  138. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  139. slapi_ch_free((void **)&matched_msg);
  140. slapi_ch_free((void **)&error_msg);
  141. if (serverctrls)
  142. ldap_controls_free(serverctrls);
  143. /* jarnou: free referrals */
  144. if (referrals)
  145. charray_free(referrals);
  146. return -1;
  147. }
  148. if ( rc != LDAP_SUCCESS ) {
  149. struct berval ** refs = referrals2berval(referrals);
  150. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  151. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  152. slapi_ch_free((void **)&matched_msg);
  153. slapi_ch_free((void **)&error_msg);
  154. if (refs)
  155. ber_bvecfree(refs);
  156. if (referrals)
  157. charray_free(referrals);
  158. if (serverctrls)
  159. ldap_controls_free(serverctrls);
  160. return -1;
  161. }
  162. cb_release_op_connection(cb->pool,ld,0);
  163. /* Add control response sent by the farm server */
  164. for (i=0; serverctrls && serverctrls[i];i++)
  165. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  166. /* SLAPI_ADD_RESCONTROL dups controls */
  167. if (serverctrls)
  168. ldap_controls_free(serverctrls);
  169. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  170. slapi_ch_free((void **)&matched_msg);
  171. slapi_ch_free((void **)&error_msg);
  172. if (referrals)
  173. charray_free(referrals);
  174. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  175. return 0;
  176. }
  177. }
  178. /* Never reached */
  179. /* return 0; */
  180. }
  181. /* Function removes mods which are not allowed over-the-wire */
  182. static void
  183. cb_remove_illegal_mods(cb_backend_instance *inst, LDAPMod **mods)
  184. {
  185. int i, j;
  186. LDAPMod *tmp;
  187. if ( inst->illegal_attributes != NULL ) { /* Unlikely to happen */
  188. PR_RWLock_Wlock(inst->rwl_config_lock);
  189. for (j=0; inst->illegal_attributes[j]; j++) {
  190. for ( i = 0; mods[i] != NULL; i++ ) {
  191. if (slapi_attr_types_equivalent(inst->illegal_attributes[j],mods[i]->mod_type)) {
  192. tmp = mods[i];
  193. for ( j = i; mods[j] != NULL; j++ ) {
  194. mods[j] = mods[j + 1];
  195. }
  196. slapi_ch_free( (void**)&(tmp->mod_type) );
  197. if ( tmp->mod_bvalues != NULL ) {
  198. ber_bvecfree( tmp->mod_bvalues );
  199. }
  200. slapi_ch_free( (void**)&tmp );
  201. i--;
  202. }
  203. }
  204. }
  205. PR_RWLock_Unlock(inst->rwl_config_lock);
  206. }
  207. }