1
0

cb_modrdn.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "cb.h"
  13. /*
  14. * Perform a modrdn operation
  15. *
  16. * Returns:
  17. * 0 - success
  18. * <0 - fail
  19. *
  20. */
  21. int
  22. chaining_back_modrdn ( Slapi_PBlock *pb )
  23. {
  24. cb_outgoing_conn *cnx;
  25. Slapi_Backend *be;
  26. cb_backend_instance *cb;
  27. LDAPControl **ctrls, **serverctrls;
  28. LDAPMessage *res;
  29. LDAP *ld = NULL;
  30. Slapi_DN *newsuperior = NULL;
  31. Slapi_DN *sdn = NULL;
  32. char **referrals = NULL;
  33. char *matched_msg, *error_msg;
  34. char *ndn = NULL;
  35. char *newrdn = NULL;
  36. char *cnxerrbuf = NULL;
  37. time_t endtime = 0;
  38. int deleteoldrdn = 0;
  39. int rc, parse_rc, msgid, i;
  40. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  41. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  42. return -1;
  43. }
  44. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  45. cb = cb_get_instance(be);
  46. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_MODRDN);
  47. /* Check wether the chaining BE is available or not */
  48. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  49. return -1;
  50. }
  51. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &sdn );
  52. if (NULL == sdn) {
  53. cb_send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL, "Null target DN", 0, NULL);
  54. return -1;
  55. }
  56. /* newrdn is passed to ldap_rename, which does not require case-ignored
  57. * newrdn. */
  58. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  59. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior );
  60. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  61. ndn = (char *)slapi_sdn_get_ndn(sdn);
  62. if (cb->local_acl && !cb->associated_be_is_disabled) {
  63. /*
  64. * Check local acls
  65. * Keep in mind We don't have the entry for acl evaluation
  66. */
  67. char * errbuf=NULL;
  68. Slapi_Entry *te = slapi_entry_alloc();
  69. slapi_entry_set_sdn(te, sdn); /* sdn: copied */
  70. rc = cb_access_allowed (pb, te, NULL, NULL, SLAPI_ACL_WRITE,&errbuf);
  71. slapi_entry_free(te);
  72. if ( rc != LDAP_SUCCESS ) {
  73. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  74. slapi_ch_free_string(&errbuf);
  75. return -1;
  76. }
  77. }
  78. /*
  79. * Grab a connection handle
  80. */
  81. rc = cb_get_connection(cb->pool, &ld, &cnx, NULL, &cnxerrbuf);
  82. if (LDAP_SUCCESS != rc) {
  83. static int warned_get_conn = 0;
  84. if (!warned_get_conn) {
  85. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  86. "cb_get_connection failed (%d) %s\n",
  87. rc, ldap_err2string(rc));
  88. warned_get_conn = 1;
  89. }
  90. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  91. cnxerrbuf, 0, NULL);
  92. slapi_ch_free_string(&cnxerrbuf);
  93. /* ping the farm.
  94. * If the farm is unreachable, we increment the counter */
  95. cb_ping_farm(cb, NULL, 0);
  96. return -1;
  97. }
  98. /*
  99. * Control management
  100. */
  101. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  102. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  103. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  104. return -1;
  105. }
  106. if ( slapi_op_abandoned( pb )) {
  107. cb_release_op_connection(cb->pool,ld,0);
  108. ldap_controls_free(ctrls);
  109. return -1;
  110. }
  111. /* heart-beat management */
  112. if (cb->max_idle_time>0)
  113. endtime=current_time() + cb->max_idle_time;
  114. /*
  115. * Call the backend preoperation plugins
  116. */
  117. if((rc = slapi_plugin_call_preop_be_plugins(pb, SLAPI_PLUGIN_MODRDN_OP))){
  118. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM, "modrdn (%s): pre betxn failed, error (%d)\n",ndn,rc);
  119. cb_release_op_connection(cb->pool,ld,0);
  120. ldap_controls_free(ctrls);
  121. return -1;
  122. }
  123. /*
  124. * Send LDAP operation to the remote host
  125. */
  126. rc = ldap_rename ( ld, ndn, newrdn, slapi_sdn_get_dn(newsuperior),
  127. deleteoldrdn, ctrls, NULL, &msgid );
  128. ldap_controls_free(ctrls);
  129. if ( rc != LDAP_SUCCESS ) {
  130. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  131. ldap_err2string(rc), 0, NULL);
  132. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  133. return -1;
  134. }
  135. while ( 1 ) {
  136. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  137. return -1;
  138. }
  139. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  140. switch ( rc ) {
  141. case -1:
  142. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  143. ldap_err2string(rc), 0, NULL);
  144. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  145. ldap_msgfree(res);
  146. return -1;
  147. case 0:
  148. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  149. /* does not respond. give up and return a error to the client. */
  150. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  151. ldap_err2string(rc), 0, NULL);*/
  152. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  153. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  154. ldap_msgfree(res);
  155. return -1;
  156. }
  157. #ifdef CB_YIELD
  158. DS_Sleep(PR_INTERVAL_NO_WAIT);
  159. #endif
  160. break;
  161. default:
  162. matched_msg=error_msg=NULL;
  163. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  164. &error_msg, &referrals, &serverctrls, 1 );
  165. if ( parse_rc != LDAP_SUCCESS ) {
  166. static int warned_parse_rc = 0;
  167. if (!warned_parse_rc) {
  168. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  169. "%s%s%s\n",
  170. matched_msg?matched_msg:"",
  171. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  172. ldap_err2string(parse_rc));
  173. warned_parse_rc = 1;
  174. }
  175. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  176. ENDUSERMSG, 0, NULL );
  177. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  178. slapi_ch_free_string(&matched_msg);
  179. slapi_ch_free_string(&error_msg);
  180. ldap_controls_free(serverctrls);
  181. charray_free(referrals);
  182. return -1;
  183. }
  184. if ( rc != LDAP_SUCCESS ) {
  185. struct berval ** refs = referrals2berval(referrals);
  186. static int warned_rc = 0;
  187. if (!warned_rc && error_msg) {
  188. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  189. "%s%s%s\n",
  190. matched_msg?matched_msg:"",
  191. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  192. error_msg );
  193. warned_rc = 1;
  194. }
  195. cb_send_ldap_result( pb, rc, matched_msg, ENDUSERMSG, 0, refs);
  196. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  197. slapi_ch_free_string(&matched_msg);
  198. slapi_ch_free_string(&error_msg);
  199. if (refs)
  200. ber_bvecfree(refs);
  201. charray_free(referrals);
  202. ldap_controls_free(serverctrls);
  203. return -1;
  204. }
  205. cb_release_op_connection(cb->pool,ld,0);
  206. /* Call the backend postoperation plugins */
  207. if((rc = slapi_plugin_call_postop_be_plugins(pb, SLAPI_PLUGIN_MODRDN_OP))){
  208. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM, "modrdn (%s): post betxn failed, error (%d)\n",ndn,rc);
  209. }
  210. /* Add control response sent by the farm server */
  211. for (i=0; serverctrls && serverctrls[i]; i++)
  212. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  213. ldap_controls_free(serverctrls);
  214. slapi_ch_free_string(&matched_msg);
  215. slapi_ch_free_string(&error_msg);
  216. charray_free(referrals);
  217. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  218. return 0;
  219. }
  220. }
  221. /* Never reached */
  222. /* return 0; */
  223. }