cb_modrdn.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 a modrdn operation
  9. *
  10. * Returns:
  11. * 0 - success
  12. * <0 - fail
  13. *
  14. */
  15. int
  16. chaining_back_modrdn ( Slapi_PBlock *pb )
  17. {
  18. Slapi_Backend * be;
  19. cb_backend_instance *cb;
  20. LDAPControl **ctrls, **serverctrls;
  21. int rc,parse_rc,msgid,i;
  22. LDAP *ld=NULL;
  23. char **referrals=NULL;
  24. LDAPMessage * res;
  25. char * matched_msg, *error_msg,* pdn, *newdn, *dn;
  26. int deleteoldrdn=0;
  27. char * newsuperior, *newrdn;
  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_MODRDN);
  38. /* Check wether the chaining BE is available or not */
  39. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  40. return -1;
  41. }
  42. newsuperior=newdn=newrdn=dn=NULL;
  43. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn );
  44. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  45. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperior );
  46. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  47. /*
  48. * Construct the new dn
  49. */
  50. dn = slapi_dn_normalize_case(dn);
  51. if ( (pdn = slapi_dn_parent( dn )) != NULL ) {
  52. /* parent + rdn + separator(s) + null */
  53. newdn = (char *) slapi_ch_malloc( strlen( pdn ) + strlen( newrdn ) + 3 );
  54. strcpy( newdn, newrdn );
  55. strcat( newdn, "," );
  56. strcat( newdn, pdn );
  57. slapi_ch_free((void **)&pdn );
  58. } else {
  59. newdn = slapi_ch_strdup( newrdn );
  60. }
  61. /*
  62. * Make sure the current backend is managing
  63. * the new dn. We won't support moving entries
  64. * across backend this way.
  65. * Done in the front-end.
  66. */
  67. slapi_ch_free((void **)&newdn);
  68. if (cb->local_acl && !cb->associated_be_is_disabled) {
  69. /*
  70. * Check local acls
  71. * Keep in mind We don't have the entry for acl evaluation
  72. */
  73. char * errbuf=NULL;
  74. Slapi_Entry *te = slapi_entry_alloc();
  75. slapi_entry_set_dn(te,slapi_ch_strdup(dn));
  76. rc = cb_access_allowed (pb, te, NULL, NULL, SLAPI_ACL_WRITE,&errbuf);
  77. slapi_entry_free(te);
  78. if ( rc != LDAP_SUCCESS ) {
  79. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  80. slapi_ch_free((void **)&errbuf);
  81. return -1;
  82. }
  83. }
  84. /*
  85. * Grab a connection handle
  86. */
  87. if ((rc = cb_get_connection(cb->pool,&ld,&cnx,NULL,&cnxerrbuf)) != LDAP_SUCCESS) {
  88. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, cnxerrbuf, 0, NULL);
  89. if (cnxerrbuf) {
  90. PR_smprintf_free(cnxerrbuf);
  91. }
  92. /* ping the farm. If the farm is unreachable, we increment the counter */
  93. cb_ping_farm(cb,NULL,0);
  94. return -1;
  95. }
  96. /*
  97. * Control management
  98. */
  99. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  100. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  101. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  102. return -1;
  103. }
  104. if ( slapi_op_abandoned( pb )) {
  105. cb_release_op_connection(cb->pool,ld,0);
  106. if ( NULL != ctrls)
  107. ldap_controls_free(ctrls);
  108. return -1;
  109. }
  110. /* heart-beat management */
  111. if (cb->max_idle_time>0)
  112. endtime=current_time() + cb->max_idle_time;
  113. /*
  114. * Send LDAP operation to the remote host
  115. */
  116. rc = ldap_rename ( ld,dn,newrdn,newsuperior,deleteoldrdn,ctrls,NULL,&msgid);
  117. if ( NULL != ctrls)
  118. ldap_controls_free(ctrls);
  119. if ( rc != LDAP_SUCCESS ) {
  120. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  121. ldap_err2string(rc), 0, NULL);
  122. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  123. return -1;
  124. }
  125. while ( 1 ) {
  126. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  127. return -1;
  128. }
  129. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  130. switch ( rc ) {
  131. case -1:
  132. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  133. ldap_err2string(rc), 0, NULL);
  134. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  135. if (res)
  136. ldap_msgfree(res);
  137. return -1;
  138. case 0:
  139. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  140. /* does not respond. give up and return a*/
  141. /* error to the client. */
  142. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  143. ldap_err2string(rc), 0, NULL);*/
  144. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  145. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  146. if (res)
  147. ldap_msgfree(res);
  148. return -1;
  149. }
  150. #ifdef CB_YIELD
  151. DS_Sleep(PR_INTERVAL_NO_WAIT);
  152. #endif
  153. break;
  154. default:
  155. matched_msg=error_msg=NULL;
  156. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  157. &error_msg, &referrals, &serverctrls, 1 );
  158. if ( parse_rc != LDAP_SUCCESS ) {
  159. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  160. ldap_err2string(parse_rc), 0, NULL);
  161. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  162. slapi_ch_free((void **)&matched_msg);
  163. slapi_ch_free((void **)&error_msg);
  164. if (serverctrls)
  165. ldap_controls_free(serverctrls);
  166. /* jarnou: free referrals */
  167. if (referrals)
  168. charray_free(referrals);
  169. return -1;
  170. }
  171. if ( rc != LDAP_SUCCESS ) {
  172. struct berval ** refs = referrals2berval(referrals);
  173. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  174. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  175. slapi_ch_free((void **)&matched_msg);
  176. slapi_ch_free((void **)&error_msg);
  177. if (refs)
  178. ber_bvecfree(refs);
  179. if (referrals)
  180. charray_free(referrals);
  181. if (serverctrls)
  182. ldap_controls_free(serverctrls);
  183. return -1;
  184. }
  185. cb_release_op_connection(cb->pool,ld,0);
  186. /* Add control response sent by the farm server */
  187. for (i=0; serverctrls && serverctrls[i];i++)
  188. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  189. if (serverctrls)
  190. ldap_controls_free(serverctrls);
  191. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  192. slapi_ch_free((void **)&matched_msg);
  193. slapi_ch_free((void **)&error_msg);
  194. if (referrals)
  195. charray_free(referrals);
  196. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  197. return 0;
  198. }
  199. }
  200. /* Never reached */
  201. /* return 0; */
  202. }