cb_modrdn.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "cb.h"
  42. /*
  43. * Perform a modrdn operation
  44. *
  45. * Returns:
  46. * 0 - success
  47. * <0 - fail
  48. *
  49. */
  50. int
  51. chaining_back_modrdn ( Slapi_PBlock *pb )
  52. {
  53. Slapi_Backend * be;
  54. cb_backend_instance *cb;
  55. LDAPControl **ctrls, **serverctrls;
  56. int rc,parse_rc,msgid,i;
  57. LDAP *ld=NULL;
  58. char **referrals=NULL;
  59. LDAPMessage *res;
  60. char *matched_msg, *error_msg;
  61. char *newdn = NULL;
  62. const char *pdn = NULL;
  63. char *ndn = NULL;
  64. Slapi_DN *sdn = NULL;
  65. Slapi_DN *psdn = NULL;
  66. int deleteoldrdn = 0;
  67. Slapi_DN *newsuperior = NULL;
  68. char *newrdn = NULL;
  69. char * cnxerrbuf=NULL;
  70. time_t endtime;
  71. cb_outgoing_conn *cnx;
  72. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  73. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  74. return -1;
  75. }
  76. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  77. cb = cb_get_instance(be);
  78. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_MODRDN);
  79. /* Check wether the chaining BE is available or not */
  80. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  81. return -1;
  82. }
  83. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &sdn );
  84. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  85. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior );
  86. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  87. ndn = (char *)slapi_sdn_get_ndn(sdn);
  88. /*
  89. * Construct the new dn
  90. */
  91. psdn = slapi_sdn_new();
  92. slapi_sdn_get_parent(sdn, psdn);
  93. pdn = slapi_sdn_get_ndn(psdn);
  94. if ( pdn ) {
  95. /* parent + rdn + separator(s) + null */
  96. newdn = slapi_ch_smprintf("%s,%s", newrdn, pdn);
  97. } else {
  98. newdn = slapi_ch_strdup( newrdn );
  99. }
  100. slapi_sdn_free(&psdn);
  101. /*
  102. * Make sure the current backend is managing
  103. * the new dn. We won't support moving entries
  104. * across backend this way.
  105. * Done in the front-end.
  106. */
  107. slapi_ch_free((void **)&newdn);
  108. if (cb->local_acl && !cb->associated_be_is_disabled) {
  109. /*
  110. * Check local acls
  111. * Keep in mind We don't have the entry for acl evaluation
  112. */
  113. char * errbuf=NULL;
  114. Slapi_Entry *te = slapi_entry_alloc();
  115. slapi_entry_set_sdn(te, sdn); /* sdn: copied */
  116. rc = cb_access_allowed (pb, te, NULL, NULL, SLAPI_ACL_WRITE,&errbuf);
  117. slapi_entry_free(te);
  118. if ( rc != LDAP_SUCCESS ) {
  119. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  120. slapi_ch_free((void **)&errbuf);
  121. return -1;
  122. }
  123. }
  124. /*
  125. * Grab a connection handle
  126. */
  127. rc = cb_get_connection(cb->pool, &ld, &cnx, NULL, &cnxerrbuf);
  128. if (LDAP_SUCCESS != rc) {
  129. static int warned_get_conn = 0;
  130. if (!warned_get_conn) {
  131. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  132. "cb_get_connection failed (%d) %s\n",
  133. rc, ldap_err2string(rc));
  134. warned_get_conn = 1;
  135. }
  136. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  137. cnxerrbuf, 0, NULL);
  138. slapi_ch_free_string(&cnxerrbuf);
  139. /* ping the farm.
  140. * If the farm is unreachable, we increment the counter */
  141. cb_ping_farm(cb, NULL, 0);
  142. return -1;
  143. }
  144. /*
  145. * Control management
  146. */
  147. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  148. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  149. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  150. return -1;
  151. }
  152. if ( slapi_op_abandoned( pb )) {
  153. cb_release_op_connection(cb->pool,ld,0);
  154. if ( NULL != ctrls)
  155. ldap_controls_free(ctrls);
  156. return -1;
  157. }
  158. /* heart-beat management */
  159. if (cb->max_idle_time>0)
  160. endtime=current_time() + cb->max_idle_time;
  161. /*
  162. * Send LDAP operation to the remote host
  163. */
  164. rc = ldap_rename ( ld, ndn, newrdn, slapi_sdn_get_dn(newsuperior),
  165. deleteoldrdn, ctrls, NULL, &msgid );
  166. if ( NULL != ctrls)
  167. ldap_controls_free(ctrls);
  168. if ( rc != LDAP_SUCCESS ) {
  169. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  170. ldap_err2string(rc), 0, NULL);
  171. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  172. return -1;
  173. }
  174. while ( 1 ) {
  175. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  176. return -1;
  177. }
  178. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  179. switch ( rc ) {
  180. case -1:
  181. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  182. ldap_err2string(rc), 0, NULL);
  183. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  184. if (res)
  185. ldap_msgfree(res);
  186. return -1;
  187. case 0:
  188. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  189. /* does not respond. give up and return a*/
  190. /* error to the client. */
  191. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  192. ldap_err2string(rc), 0, NULL);*/
  193. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  194. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  195. if (res)
  196. ldap_msgfree(res);
  197. return -1;
  198. }
  199. #ifdef CB_YIELD
  200. DS_Sleep(PR_INTERVAL_NO_WAIT);
  201. #endif
  202. break;
  203. default:
  204. matched_msg=error_msg=NULL;
  205. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  206. &error_msg, &referrals, &serverctrls, 1 );
  207. if ( parse_rc != LDAP_SUCCESS ) {
  208. static int warned_parse_rc = 0;
  209. if (!warned_parse_rc) {
  210. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  211. "%s%s%s\n",
  212. matched_msg?matched_msg:"",
  213. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  214. ldap_err2string(parse_rc));
  215. warned_parse_rc = 1;
  216. }
  217. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  218. ENDUSERMSG, 0, NULL );
  219. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  220. slapi_ch_free((void **)&matched_msg);
  221. slapi_ch_free((void **)&error_msg);
  222. if (serverctrls)
  223. ldap_controls_free(serverctrls);
  224. /* jarnou: free referrals */
  225. if (referrals)
  226. charray_free(referrals);
  227. return -1;
  228. }
  229. if ( rc != LDAP_SUCCESS ) {
  230. struct berval ** refs = referrals2berval(referrals);
  231. static int warned_rc = 0;
  232. if (!warned_rc && error_msg) {
  233. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  234. "%s%s%s\n",
  235. matched_msg?matched_msg:"",
  236. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  237. error_msg );
  238. warned_rc = 1;
  239. }
  240. cb_send_ldap_result( pb, rc, matched_msg, ENDUSERMSG, 0, refs);
  241. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  242. slapi_ch_free((void **)&matched_msg);
  243. slapi_ch_free((void **)&error_msg);
  244. if (refs)
  245. ber_bvecfree(refs);
  246. if (referrals)
  247. charray_free(referrals);
  248. if (serverctrls)
  249. ldap_controls_free(serverctrls);
  250. return -1;
  251. }
  252. cb_release_op_connection(cb->pool,ld,0);
  253. /* Add control response sent by the farm server */
  254. for (i=0; serverctrls && serverctrls[i];i++)
  255. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  256. if (serverctrls)
  257. ldap_controls_free(serverctrls);
  258. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  259. slapi_ch_free((void **)&matched_msg);
  260. slapi_ch_free((void **)&error_msg);
  261. if (referrals)
  262. charray_free(referrals);
  263. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  264. return 0;
  265. }
  266. }
  267. /* Never reached */
  268. /* return 0; */
  269. }