cb_modrdn.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include "cb.h"
  39. /*
  40. * Perform a modrdn operation
  41. *
  42. * Returns:
  43. * 0 - success
  44. * <0 - fail
  45. *
  46. */
  47. int
  48. chaining_back_modrdn ( Slapi_PBlock *pb )
  49. {
  50. Slapi_Backend * be;
  51. cb_backend_instance *cb;
  52. LDAPControl **ctrls, **serverctrls;
  53. int rc,parse_rc,msgid,i;
  54. LDAP *ld=NULL;
  55. char **referrals=NULL;
  56. LDAPMessage * res;
  57. char * matched_msg, *error_msg,* pdn, *newdn, *dn;
  58. int deleteoldrdn=0;
  59. char * newsuperior, *newrdn;
  60. char * cnxerrbuf=NULL;
  61. time_t endtime;
  62. cb_outgoing_conn * cnx;
  63. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  64. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  65. return -1;
  66. }
  67. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  68. cb = cb_get_instance(be);
  69. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_MODRDN);
  70. /* Check wether the chaining BE is available or not */
  71. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  72. return -1;
  73. }
  74. newsuperior=newdn=newrdn=dn=NULL;
  75. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn );
  76. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  77. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperior );
  78. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  79. /*
  80. * Construct the new dn
  81. */
  82. dn = slapi_dn_normalize_case(dn);
  83. if ( (pdn = slapi_dn_parent( dn )) != NULL ) {
  84. /* parent + rdn + separator(s) + null */
  85. newdn = (char *) slapi_ch_malloc( strlen( pdn ) + strlen( newrdn ) + 3 );
  86. strcpy( newdn, newrdn );
  87. strcat( newdn, "," );
  88. strcat( newdn, pdn );
  89. slapi_ch_free((void **)&pdn );
  90. } else {
  91. newdn = slapi_ch_strdup( newrdn );
  92. }
  93. /*
  94. * Make sure the current backend is managing
  95. * the new dn. We won't support moving entries
  96. * across backend this way.
  97. * Done in the front-end.
  98. */
  99. slapi_ch_free((void **)&newdn);
  100. if (cb->local_acl && !cb->associated_be_is_disabled) {
  101. /*
  102. * Check local acls
  103. * Keep in mind We don't have the entry for acl evaluation
  104. */
  105. char * errbuf=NULL;
  106. Slapi_Entry *te = slapi_entry_alloc();
  107. slapi_entry_set_dn(te,slapi_ch_strdup(dn));
  108. rc = cb_access_allowed (pb, te, NULL, NULL, SLAPI_ACL_WRITE,&errbuf);
  109. slapi_entry_free(te);
  110. if ( rc != LDAP_SUCCESS ) {
  111. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  112. slapi_ch_free((void **)&errbuf);
  113. return -1;
  114. }
  115. }
  116. /*
  117. * Grab a connection handle
  118. */
  119. if ((rc = cb_get_connection(cb->pool,&ld,&cnx,NULL,&cnxerrbuf)) != LDAP_SUCCESS) {
  120. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, cnxerrbuf, 0, NULL);
  121. if (cnxerrbuf) {
  122. PR_smprintf_free(cnxerrbuf);
  123. }
  124. /* ping the farm. If the farm is unreachable, we increment the counter */
  125. cb_ping_farm(cb,NULL,0);
  126. return -1;
  127. }
  128. /*
  129. * Control management
  130. */
  131. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  132. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  133. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  134. return -1;
  135. }
  136. if ( slapi_op_abandoned( pb )) {
  137. cb_release_op_connection(cb->pool,ld,0);
  138. if ( NULL != ctrls)
  139. ldap_controls_free(ctrls);
  140. return -1;
  141. }
  142. /* heart-beat management */
  143. if (cb->max_idle_time>0)
  144. endtime=current_time() + cb->max_idle_time;
  145. /*
  146. * Send LDAP operation to the remote host
  147. */
  148. rc = ldap_rename ( ld,dn,newrdn,newsuperior,deleteoldrdn,ctrls,NULL,&msgid);
  149. if ( NULL != ctrls)
  150. ldap_controls_free(ctrls);
  151. if ( rc != LDAP_SUCCESS ) {
  152. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  153. ldap_err2string(rc), 0, NULL);
  154. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  155. return -1;
  156. }
  157. while ( 1 ) {
  158. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  159. return -1;
  160. }
  161. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  162. switch ( rc ) {
  163. case -1:
  164. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  165. ldap_err2string(rc), 0, NULL);
  166. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  167. if (res)
  168. ldap_msgfree(res);
  169. return -1;
  170. case 0:
  171. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  172. /* does not respond. give up and return a*/
  173. /* error to the client. */
  174. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  175. ldap_err2string(rc), 0, NULL);*/
  176. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  177. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  178. if (res)
  179. ldap_msgfree(res);
  180. return -1;
  181. }
  182. #ifdef CB_YIELD
  183. DS_Sleep(PR_INTERVAL_NO_WAIT);
  184. #endif
  185. break;
  186. default:
  187. matched_msg=error_msg=NULL;
  188. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  189. &error_msg, &referrals, &serverctrls, 1 );
  190. if ( parse_rc != LDAP_SUCCESS ) {
  191. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  192. ldap_err2string(parse_rc), 0, NULL);
  193. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  194. slapi_ch_free((void **)&matched_msg);
  195. slapi_ch_free((void **)&error_msg);
  196. if (serverctrls)
  197. ldap_controls_free(serverctrls);
  198. /* jarnou: free referrals */
  199. if (referrals)
  200. charray_free(referrals);
  201. return -1;
  202. }
  203. if ( rc != LDAP_SUCCESS ) {
  204. struct berval ** refs = referrals2berval(referrals);
  205. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  206. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  207. slapi_ch_free((void **)&matched_msg);
  208. slapi_ch_free((void **)&error_msg);
  209. if (refs)
  210. ber_bvecfree(refs);
  211. if (referrals)
  212. charray_free(referrals);
  213. if (serverctrls)
  214. ldap_controls_free(serverctrls);
  215. return -1;
  216. }
  217. cb_release_op_connection(cb->pool,ld,0);
  218. /* Add control response sent by the farm server */
  219. for (i=0; serverctrls && serverctrls[i];i++)
  220. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  221. if (serverctrls)
  222. ldap_controls_free(serverctrls);
  223. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  224. slapi_ch_free((void **)&matched_msg);
  225. slapi_ch_free((void **)&error_msg);
  226. if (referrals)
  227. charray_free(referrals);
  228. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  229. return 0;
  230. }
  231. }
  232. /* Never reached */
  233. /* return 0; */
  234. }