modrdn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. /*
  13. * Copyright (c) 1995 Regents of the University of Michigan.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms are permitted
  17. * provided that this notice is preserved and that due credit is given
  18. * to the University of Michigan at Ann Arbor. The name of the University
  19. * may not be used to endorse or promote products derived from this
  20. * software without specific prior written permission. This software
  21. * is provided ``as is'' without express or implied warranty.
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include "slap.h"
  28. #include "pratom.h"
  29. /* Forward declarations */
  30. static int rename_internal_pb (Slapi_PBlock *pb);
  31. static void op_shared_rename (Slapi_PBlock *pb, int passin_args );
  32. static int check_rdn_for_created_attrs(const char *newrdn);
  33. /* This function is called to process operation that come over external connections */
  34. void
  35. do_modrdn( Slapi_PBlock *pb )
  36. {
  37. Slapi_Operation *operation;
  38. BerElement *ber;
  39. char *rawdn = NULL, *rawnewsuperior = NULL;
  40. const char *dn = NULL, *newsuperior = NULL;
  41. char *newrdn = NULL;
  42. int err = 0, deloldrdn = 0;
  43. ber_len_t len = 0;
  44. char *newdn = NULL;
  45. char *parent = NULL;
  46. Slapi_DN sdn;
  47. Slapi_DN snewdn;
  48. Slapi_DN *snewsuperior = NULL;
  49. LDAPDebug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
  50. /* count the modrdn request */
  51. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps);
  52. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  53. ber = operation->o_ber;
  54. slapi_sdn_init(&sdn);
  55. slapi_sdn_init(&snewdn);
  56. /*
  57. * Parse the modrdn request. It looks like this:
  58. *
  59. * ModifyRDNRequest := SEQUENCE {
  60. * entry DistinguishedName,
  61. * newrdn RelativeDistinguishedName,
  62. * deleteoldrdn BOOLEAN,
  63. * newSuperior [0] LDAPDN OPTIONAL -- v3 only
  64. * }
  65. */
  66. if (ber_scanf(ber, "{aab", &rawdn, &newrdn, &deloldrdn) == LBER_ERROR) {
  67. LDAPDebug( LDAP_DEBUG_ANY,
  68. "ber_scanf failed (op=ModRDN; params=DN,newRDN,deleteOldRDN)\n",
  69. 0, 0, 0 );
  70. op_shared_log_error_access (pb, "MODRDN", "???", "decoding error");
  71. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  72. "unable to decode DN, newRDN, or deleteOldRDN parameters",
  73. 0, NULL );
  74. goto free_and_return;
  75. }
  76. if ( ber_peek_tag( ber, &len ) == LDAP_TAG_NEWSUPERIOR ) {
  77. /* This "len" is not used... */
  78. if ( pb->pb_conn->c_ldapversion < LDAP_VERSION3 ) {
  79. LDAPDebug( LDAP_DEBUG_ANY,
  80. "got newSuperior in LDAPv2 modrdn op\n", 0, 0, 0 );
  81. op_shared_log_error_access (pb, "MODRDN",
  82. rawdn?rawdn:"", "decoding error");
  83. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  84. "received newSuperior in LDAPv2 modrdn", 0, NULL );
  85. slapi_ch_free_string( &rawdn );
  86. slapi_ch_free_string( &newrdn );
  87. goto free_and_return;
  88. }
  89. if ( ber_scanf( ber, "a", &rawnewsuperior ) == LBER_ERROR ) {
  90. LDAPDebug( LDAP_DEBUG_ANY,
  91. "ber_scanf failed (op=ModRDN; params=newSuperior)\n",
  92. 0, 0, 0 );
  93. op_shared_log_error_access (pb, "MODRDN", rawdn, "decoding error");
  94. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  95. "unable to decode newSuperior parameter", 0, NULL );
  96. slapi_ch_free_string( &rawdn );
  97. slapi_ch_free_string( &newrdn );
  98. goto free_and_return;
  99. }
  100. }
  101. /* Check if we should be performing strict validation. */
  102. if (config_get_dn_validate_strict()) {
  103. /* check that the dn is formatted correctly */
  104. err = slapi_dn_syntax_check(pb, rawdn, 1);
  105. if (err) { /* syntax check failed */
  106. op_shared_log_error_access(pb, "MODRDN", rawdn?rawdn:"",
  107. "strict: invalid dn");
  108. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  109. NULL, "invalid dn", 0, NULL);
  110. slapi_ch_free_string( &rawdn );
  111. slapi_ch_free_string( &newrdn );
  112. slapi_ch_free_string( &rawnewsuperior );
  113. goto free_and_return;
  114. }
  115. /* check that the new rdn is formatted correctly */
  116. err = slapi_dn_syntax_check(pb, newrdn, 1);
  117. if (err) { /* syntax check failed */
  118. op_shared_log_error_access(pb, "MODRDN", newrdn?newrdn:"",
  119. "strict: invalid new rdn");
  120. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  121. NULL, "invalid new rdn", 0, NULL);
  122. slapi_ch_free_string( &rawdn );
  123. slapi_ch_free_string( &newrdn );
  124. slapi_ch_free_string( &rawnewsuperior );
  125. goto free_and_return;
  126. }
  127. }
  128. slapi_sdn_init_dn_passin(&sdn, rawdn);
  129. dn = slapi_sdn_get_dn(&sdn);
  130. if (rawdn && (strlen(rawdn) > 0) && (NULL == dn)) {
  131. /* normalization failed */
  132. op_shared_log_error_access(pb, "MODRDN", rawdn, "invalid dn");
  133. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  134. "invalid dn", 0, NULL);
  135. slapi_ch_free_string( &newrdn );
  136. slapi_ch_free_string( &rawnewsuperior );
  137. goto free_and_return;
  138. }
  139. if (rawnewsuperior) {
  140. if (config_get_dn_validate_strict()) {
  141. /* check that the dn is formatted correctly */
  142. err = slapi_dn_syntax_check(pb, rawnewsuperior, 1);
  143. if (err) { /* syntax check failed */
  144. op_shared_log_error_access(pb, "MODRDN", rawnewsuperior,
  145. "strict: invalid new superior");
  146. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  147. NULL, "invalid new superior", 0, NULL);
  148. slapi_ch_free_string( &rawnewsuperior );
  149. goto free_and_return;
  150. }
  151. }
  152. snewsuperior = slapi_sdn_new_dn_passin(rawnewsuperior);
  153. newsuperior = slapi_sdn_get_dn(snewsuperior);
  154. }
  155. /*
  156. * If newsuperior is myself or my descendent, the modrdn should fail.
  157. * Note: need to check the case newrdn is given, and newsuperior
  158. * uses the newrdn, as well.
  159. */
  160. parent = slapi_dn_parent(slapi_sdn_get_ndn(&sdn));
  161. newdn = slapi_ch_smprintf("%s,%s", newrdn, parent);
  162. /* slapi_sdn_init_normdn_passin expects normalized but NOT
  163. * decapitalized dn */
  164. slapi_sdn_init_dn_passin(&snewdn, newdn);
  165. if (0 == slapi_sdn_compare(&sdn, snewsuperior) ||
  166. 0 == slapi_sdn_compare(&snewdn, snewsuperior)) {
  167. op_shared_log_error_access(pb, "MODRDN", newsuperior,
  168. "new superior is identical to the entry dn");
  169. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  170. "new superior is identical to the entry dn", 0, NULL);
  171. goto free_and_return;
  172. }
  173. if (slapi_sdn_issuffix(snewsuperior, &sdn) ||
  174. slapi_sdn_issuffix(snewsuperior, &snewdn)) {
  175. /* E.g.,
  176. * newsuperior: ou=sub,ou=people,dc=example,dc=com
  177. * dn: ou=people,dc=example,dc=com
  178. */
  179. op_shared_log_error_access(pb, "MODRDN", newsuperior,
  180. "new superior is descendent of the entry");
  181. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  182. "new superior is descendent of the entry", 0, NULL);
  183. goto free_and_return;
  184. }
  185. /*
  186. * in LDAPv3 there can be optional control extensions on
  187. * the end of an LDAPMessage. we need to read them in and
  188. * pass them to the backend.
  189. */
  190. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  191. op_shared_log_error_access (pb, "MODRDN", dn, "failed to decode LDAP controls");
  192. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  193. goto free_and_return;
  194. }
  195. LDAPDebug( LDAP_DEBUG_ARGS,
  196. "do_modrdn: dn (%s) newrdn (%s) deloldrdn (%d)\n", dn, newrdn,
  197. deloldrdn );
  198. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &pb->pb_op->o_isroot );
  199. /* dn, newrdn and newsuperior are all normalized */
  200. slapi_pblock_set( pb, SLAPI_ORIGINAL_TARGET,
  201. (void *)slapi_sdn_get_udn(&sdn) );
  202. slapi_pblock_set( pb, SLAPI_MODRDN_TARGET_SDN, &sdn );
  203. slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn );
  204. slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, (void *)snewsuperior );
  205. slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn );
  206. op_shared_rename(pb, 0 /* do not pass in ownership of string arguments */ );
  207. free_and_return:
  208. slapi_sdn_done(&sdn);
  209. slapi_ch_free_string(&newrdn);
  210. slapi_sdn_free(&snewsuperior);
  211. slapi_sdn_done(&snewdn);
  212. slapi_ch_free_string(&parent);
  213. return;
  214. }
  215. /* This function is used to issue internal modrdn operation
  216. This is an old style API. Its use is discoraged because it is not extendable and
  217. because it does not allow to check whether plugin has right to access part of the
  218. tree it is trying to modify. Use slapi_modrdn_internal_pb instead */
  219. Slapi_PBlock *
  220. slapi_modrdn_internal(const char *iodn, const char *inewrdn, int deloldrdn, LDAPControl **controls, int dummy)
  221. {
  222. return slapi_rename_internal(iodn, inewrdn, NULL, deloldrdn, controls, dummy);
  223. }
  224. Slapi_PBlock *
  225. slapi_rename_internal(const char *iodn, const char *inewrdn, const char *inewsuperior, int deloldrdn, LDAPControl **controls, int dummy)
  226. {
  227. Slapi_PBlock pb;
  228. Slapi_PBlock *result_pb = NULL;
  229. int opresult= 0;
  230. Slapi_DN sdn;
  231. Slapi_DN newsuperiorsdn;
  232. pblock_init (&pb);
  233. slapi_sdn_init_dn_byref(&sdn, iodn);
  234. slapi_sdn_init_dn_byref(&newsuperiorsdn, inewsuperior);
  235. slapi_rename_internal_set_pb_ext(&pb, &sdn, inewrdn, &newsuperiorsdn,
  236. deloldrdn, controls, NULL,
  237. plugin_get_default_component_id(), 0);
  238. rename_internal_pb (&pb);
  239. result_pb = slapi_pblock_new();
  240. if (result_pb) {
  241. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  242. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  243. }
  244. slapi_sdn_done(&sdn);
  245. slapi_sdn_done(&newsuperiorsdn);
  246. pblock_done(&pb);
  247. return result_pb;
  248. }
  249. /* This is new style API to issue internal add operation.
  250. pblock should contain the following data (can be set via call to slapi_rename_internal_set_pb):
  251. For uniqueid based operation:
  252. SLAPI_TARGET_DN set to dn that allows to select right backend, can be stale
  253. SLAPI_TARGET_UNIQUEID set to the uniqueid of the entry we are looking for
  254. SLAPI_MODRDN_NEWRDN set to new rdn of the entry
  255. SLAPI_MODRDN_DELOLDRDN tells whether old rdn should be kept in the entry
  256. LAPI_CONTROLS_ARG set to request controls if present
  257. For dn based search:
  258. SLAPI_TARGET_DN set to the entry dn
  259. SLAPI_MODRDN_NEWRDN set to new rdn of the entry
  260. SLAPI_MODRDN_DELOLDRDN tells whether old rdn should be kept in the entry
  261. SLAPI_CONTROLS_ARG set to request controls if present
  262. */
  263. int slapi_modrdn_internal_pb (Slapi_PBlock *pb)
  264. {
  265. if (pb == NULL)
  266. return -1;
  267. return rename_internal_pb (pb);
  268. }
  269. void
  270. slapi_rename_internal_set_pb_ext(Slapi_PBlock *pb,
  271. const Slapi_DN *olddn,
  272. const char *newrdn, /* normalized */
  273. const Slapi_DN *newsuperior, int deloldrdn,
  274. LDAPControl **controls, const char *uniqueid,
  275. Slapi_ComponentId *plugin_identity,
  276. int operation_flags)
  277. {
  278. Operation *op;
  279. PR_ASSERT (pb != NULL);
  280. if (pb == NULL || olddn == NULL || newrdn == NULL)
  281. {
  282. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  283. "slapi_rename_internal_set_pb: NULL parameter\n");
  284. return;
  285. }
  286. op = internal_operation_new(SLAPI_OPERATION_MODRDN,operation_flags);
  287. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  288. slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET,
  289. (void*)slapi_sdn_get_dn(olddn));
  290. slapi_pblock_set(pb, SLAPI_MODRDN_TARGET_SDN, (void*)olddn);
  291. slapi_pblock_set(pb, SLAPI_MODRDN_NEWRDN, (void*)newrdn);
  292. slapi_pblock_set(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, (void*)newsuperior);
  293. slapi_pblock_set(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  294. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  295. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, NULL);
  296. if (uniqueid)
  297. {
  298. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void*)uniqueid);
  299. }
  300. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  301. }
  302. /* Helper functions */
  303. static int rename_internal_pb (Slapi_PBlock *pb)
  304. {
  305. LDAPControl **controls;
  306. Operation *op;
  307. int opresult = 0;
  308. PR_ASSERT (pb != NULL);
  309. slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
  310. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  311. if (!op) {
  312. opresult = 1;
  313. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  314. slapi_log_error(SLAPI_LOG_FATAL, "rename_internal_pb",
  315. "Internal error: pblock was not properly initialized\n");
  316. return -1;
  317. }
  318. op->o_handler_data = &opresult;
  319. op->o_result_handler = internal_getresult_callback;
  320. slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
  321. /* set parameters common for all internal operations */
  322. set_common_params (pb);
  323. /* set actions taken to process the operation */
  324. set_config_params (pb);
  325. op_shared_rename (pb, 0 /* not passing ownership of args */ );
  326. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  327. return 0;
  328. }
  329. /*
  330. * op_shared_rename() -- common frontend code for modDN operations.
  331. *
  332. * Beware: this function resets the following pblock elements that were
  333. * set by the caller:
  334. *
  335. * SLAPI_MODRDN_TARGET_SDN
  336. * SLAPI_MODRDN_NEWRDN
  337. * SLAPI_MODRDN_NEWSUPERIOR_SDN
  338. */
  339. static void
  340. op_shared_rename(Slapi_PBlock *pb, int passin_args)
  341. {
  342. char *dn, *newrdn, *newdn = NULL;
  343. const char *newsuperior;
  344. char **rdns;
  345. int deloldrdn;
  346. Slapi_Backend *be = NULL;
  347. Slapi_DN *origsdn = NULL;
  348. Slapi_Mods smods;
  349. int internal_op, repl_op, lastmod;
  350. Slapi_Operation *operation;
  351. Slapi_Entry *referral;
  352. char errorbuf[BUFSIZ];
  353. int err;
  354. char *proxydn = NULL;
  355. char *proxystr = NULL;
  356. int proxy_err = LDAP_SUCCESS;
  357. char *errtext = NULL;
  358. Slapi_DN *sdn = NULL;
  359. Slapi_DN *newsuperiorsdn = NULL;
  360. slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET, &dn);
  361. slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
  362. slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperiorsdn);
  363. slapi_pblock_get(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  364. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  365. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  366. slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &origsdn);
  367. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  368. /*
  369. * If ownership has not been passed to this function, we replace the
  370. * string input fields within the pblock with strdup'd copies. Why?
  371. * Because some pre- and post-op plugins may change them, and the
  372. * convention is that plugins should place a malloc'd string in the
  373. * pblock. Therefore, we need to be able to retrieve and free them
  374. * later. But the callers of the internal modrdn calls are promised
  375. * that we will not free these parameters... so if passin_args is
  376. * zero, we need to make copies.
  377. *
  378. * In the case of SLAPI_MODRDN_TARGET_SDN and SLAPI_MODRDN_NEWSUPERIOR_SDN,
  379. * we replace the existing values with normalized values (because plugins
  380. * expect these DNs to be normalized).
  381. */
  382. if (NULL == origsdn) {
  383. sdn = slapi_sdn_new_dn_byval(dn);
  384. slapi_pblock_set(pb, SLAPI_MODRDN_TARGET_SDN, sdn);
  385. }
  386. if (passin_args) {
  387. if (NULL == sdn) { /* origsdn is not NULL, so use it. */
  388. sdn = origsdn;
  389. }
  390. } else {
  391. if (NULL == sdn) {
  392. sdn = slapi_sdn_dup(origsdn);
  393. }
  394. newrdn = slapi_ch_strdup(newrdn);
  395. newsuperiorsdn = slapi_sdn_dup(newsuperiorsdn);
  396. slapi_pblock_set(pb, SLAPI_MODRDN_TARGET_SDN, sdn);
  397. slapi_pblock_set(pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn);
  398. slapi_pblock_set(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, newsuperiorsdn);
  399. }
  400. /* normdn = slapi_sdn_get_dn(sdn); */
  401. newsuperior = slapi_sdn_get_dn(newsuperiorsdn);
  402. /* get the proxy auth dn if the proxy auth control is present */
  403. proxy_err = proxyauth_get_dn(pb, &proxydn, &errtext);
  404. /*
  405. * first, log the operation to the access log,
  406. * then check rdn and newsuperior,
  407. * and - if applicable - log reason of any error to the errors log
  408. */
  409. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  410. {
  411. if (proxydn)
  412. {
  413. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  414. }
  415. if ( !internal_op )
  416. {
  417. slapi_log_access(LDAP_DEBUG_STATS,
  418. "conn=%" NSPRIu64 " op=%d MODRDN dn=\"%s\" newrdn=\"%s\" newsuperior=\"%s\"%s\n",
  419. pb->pb_conn->c_connid,
  420. pb->pb_op->o_opid,
  421. dn,
  422. newrdn ? newrdn : "(null)",
  423. newsuperior ? newsuperior : "(null)",
  424. proxystr ? proxystr : "");
  425. }
  426. else
  427. {
  428. slapi_log_access(LDAP_DEBUG_ARGS,
  429. "conn=%s op=%d MODRDN dn=\"%s\" newrdn=\"%s\" newsuperior=\"%s\"%s\n",
  430. LOG_INTERNAL_OP_CON_ID,
  431. LOG_INTERNAL_OP_OP_ID,
  432. dn,
  433. newrdn ? newrdn : "(null)",
  434. newsuperior ? newsuperior : "(null)",
  435. proxystr ? proxystr : "");
  436. }
  437. }
  438. /* If we encountered an error parsing the proxy control, return an error
  439. * to the client. We do this here to ensure that we log the operation first. */
  440. if (proxy_err != LDAP_SUCCESS)
  441. {
  442. send_ldap_result(pb, proxy_err, NULL, errtext, 0, NULL);
  443. goto free_and_return_nolock;
  444. }
  445. /* check that the rdn is formatted correctly */
  446. if ((rdns = slapi_ldap_explode_rdn(newrdn, 0)) == NULL)
  447. {
  448. if ( !internal_op ) {
  449. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  450. "conn=%" NSPRIu64 " op=%d MODRDN invalid new RDN (\"%s\")\n",
  451. pb->pb_conn->c_connid,
  452. pb->pb_op->o_opid,
  453. (NULL == newrdn) ? "(null)" : newrdn);
  454. } else {
  455. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  456. "conn=%s op=%d MODRDN invalid new RDN (\"%s\")\n",
  457. LOG_INTERNAL_OP_CON_ID,
  458. LOG_INTERNAL_OP_OP_ID,
  459. (NULL == newrdn) ? "(null)" : newrdn);
  460. }
  461. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL, "invalid RDN", 0, NULL);
  462. goto free_and_return_nolock;
  463. }
  464. else
  465. {
  466. slapi_ldap_value_free(rdns);
  467. }
  468. /* check if created attributes are used in the new RDN */
  469. /* check_rdn_for_created_attrs ignores the cases */
  470. if (check_rdn_for_created_attrs((const char *)newrdn)) {
  471. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL, "invalid attribute in RDN", 0, NULL);
  472. goto free_and_return_nolock;
  473. }
  474. /* check that the dn is formatted correctly */
  475. err = slapi_dn_syntax_check(pb, newsuperior, 1);
  476. if (err)
  477. {
  478. LDAPDebug0Args(LDAP_DEBUG_ARGS, "Syntax check of newSuperior failed\n");
  479. if (!internal_op) {
  480. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  481. "conn=%" NSPRIu64 " op=%d MODRDN invalid new superior (\"%s\")",
  482. pb->pb_conn->c_connid,
  483. pb->pb_op->o_opid,
  484. newsuperior ? newsuperior : "(null)");
  485. } else {
  486. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  487. "conn=%s op=%d MODRDN invalid new superior (\"%s\")",
  488. LOG_INTERNAL_OP_CON_ID,
  489. LOG_INTERNAL_OP_OP_ID,
  490. newsuperior ? newsuperior : "(null)");
  491. }
  492. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  493. "newSuperior does not look like a DN", 0, NULL);
  494. goto free_and_return_nolock;
  495. }
  496. if (newsuperior != NULL)
  497. {
  498. LDAPDebug(LDAP_DEBUG_ARGS, "do_moddn: newsuperior (%s)\n", newsuperior, 0, 0);
  499. }
  500. /* target spec is used to decide which plugins are applicable for the operation */
  501. operation_set_target_spec (pb->pb_op, sdn);
  502. /*
  503. * Construct the new DN (code sdn from backend
  504. * and modified to handle newsuperior)
  505. */
  506. newdn = slapi_moddn_get_newdn(sdn, newrdn, newsuperior);
  507. /*
  508. * We could be serving multiple database backends. Select the
  509. * appropriate one, or send a referral to our "referral server"
  510. * if we don't hold it.
  511. */
  512. /* slapi_mapping_tree_select_and_check ignores the case of newdn
  513. * which is generated using newrdn above. */
  514. if ((err = slapi_mapping_tree_select_and_check(pb, newdn, &be, &referral, errorbuf)) != LDAP_SUCCESS)
  515. {
  516. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  517. goto free_and_return_nolock;
  518. }
  519. if (referral)
  520. {
  521. int managedsait;
  522. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  523. if (managedsait)
  524. {
  525. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  526. "cannot update referral", 0, NULL);
  527. slapi_entry_free(referral);
  528. goto free_and_return;
  529. }
  530. send_referrals_from_entry(pb,referral);
  531. slapi_entry_free(referral);
  532. goto free_and_return;
  533. }
  534. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  535. /* can get lastmod only after backend is selected */
  536. slapi_pblock_get(pb, SLAPI_BE_LASTMOD, &lastmod);
  537. /* if it is a replicated operation - leave lastmod attributes alone */
  538. slapi_mods_init (&smods, 2);
  539. if (!repl_op && lastmod)
  540. {
  541. modify_update_last_modified_attr(pb, &smods);
  542. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, (void*)slapi_mods_get_ldapmods_passout(&smods));
  543. }
  544. else {
  545. slapi_mods_done (&smods);
  546. }
  547. /*
  548. * call the pre-modrdn plugins. if they succeed, call
  549. * the backend modrdn function. then call the
  550. * post-modrdn plugins.
  551. */
  552. if (plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN :
  553. SLAPI_PLUGIN_PRE_MODRDN_FN) == SLAPI_PLUGIN_SUCCESS)
  554. {
  555. int rc= LDAP_OPERATIONS_ERROR;
  556. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  557. set_db_default_result_handlers(pb);
  558. if (be->be_modrdn != NULL)
  559. {
  560. if ((rc = (*be->be_modrdn)(pb)) == 0)
  561. {
  562. Slapi_Entry *pse;
  563. Slapi_Entry *ecopy;
  564. /* we don't perform acl check for internal operations */
  565. /* dont update aci store for remote acis */
  566. if ((!internal_op) &&
  567. (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  568. plugin_call_acl_mods_update (pb, SLAPI_OPERATION_MODRDN);
  569. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  570. write_audit_log_entry(pb); /* Record the operation in the audit log */
  571. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  572. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &ecopy);
  573. /* GGOODREPL persistent search system needs the changenumber, oops. */
  574. do_ps_service(pse, ecopy, LDAP_CHANGETYPE_MODDN, 0);
  575. } else {
  576. /* Should we also be doing a disk space check here? */
  577. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  578. {
  579. write_auditfail_log_entry(pb); /* Record the operation in the audit log */
  580. }
  581. }
  582. }
  583. else
  584. {
  585. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Function not implemented", 0, NULL);
  586. }
  587. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  588. plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN :
  589. SLAPI_PLUGIN_POST_MODRDN_FN);
  590. }
  591. free_and_return:
  592. if (be)
  593. slapi_be_Unlock(be);
  594. free_and_return_nolock:
  595. {
  596. /* Free up everything left in the PBlock */
  597. Slapi_Entry *pse;
  598. Slapi_Entry *ecopy;
  599. LDAPMod **mods;
  600. char *s;
  601. if (passin_args) {
  602. if (NULL == origsdn) {
  603. slapi_sdn_free(&sdn);
  604. }
  605. } else {
  606. slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &sdn);
  607. slapi_sdn_free(&sdn);
  608. /* get newrdn to free the string */
  609. slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
  610. slapi_ch_free_string(&newrdn);
  611. slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperiorsdn);
  612. slapi_sdn_free(&newsuperiorsdn);
  613. }
  614. slapi_ch_free_string(&newdn);
  615. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &ecopy);
  616. slapi_entry_free(ecopy);
  617. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  618. slapi_entry_free(pse);
  619. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  620. ldap_mods_free( mods, 1 );
  621. slapi_ch_free_string(&proxydn);
  622. slapi_ch_free_string(&proxystr);
  623. slapi_pblock_get(pb, SLAPI_URP_NAMING_COLLISION_DN, &s);
  624. slapi_ch_free((void **)&s);
  625. }
  626. }
  627. /* Checks if created attributes are used in the RDN.
  628. * Returns 1 if created attrs are in the RDN, and
  629. * 0 if created attrs are not in the RDN. Returns
  630. * -1 if an error occurs.
  631. */
  632. static int check_rdn_for_created_attrs(const char *newrdn)
  633. {
  634. int i, rc = 0;
  635. Slapi_RDN *rdn = NULL;
  636. char *value = NULL;
  637. char *type[] = {"modifytimestamp", "createtimestamp",
  638. "creatorsname", "modifiersname", 0};
  639. if (newrdn && *newrdn && (rdn = slapi_rdn_new())) {
  640. slapi_rdn_init_dn(rdn, newrdn);
  641. for (i = 0; type[i] != NULL; i++) {
  642. if (slapi_rdn_contains_attr(rdn, type[i], &value)) {
  643. LDAPDebug(LDAP_DEBUG_TRACE, "Invalid DN. RDN contains %s attribute\n", type[i], 0, 0);
  644. rc = 1;
  645. break;
  646. }
  647. }
  648. slapi_rdn_free(&rdn);
  649. } else {
  650. LDAPDebug(LDAP_DEBUG_TRACE, "check_rdn_for_created_attrs: Error allocating RDN\n", 0, 0, 0);
  651. rc = -1;
  652. }
  653. return rc;
  654. }