add.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. /*
  42. * Copyright (c) 1995 Regents of the University of Michigan.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms are permitted
  46. * provided that this notice is preserved and that due credit is given
  47. * to the University of Michigan at Ann Arbor. The name of the University
  48. * may not be used to endorse or promote products derived from this
  49. * software without specific prior written permission. This software
  50. * is provided ``as is'' without express or implied warranty.
  51. */
  52. #include <errno.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <time.h>
  56. #include <sys/types.h>
  57. #ifndef _WIN32
  58. #include <sys/socket.h>
  59. #endif
  60. #include "slap.h"
  61. #include "pratom.h"
  62. #include "csngen.h"
  63. /* Forward declarations */
  64. static int add_internal_pb (Slapi_PBlock *pb);
  65. static void op_shared_add (Slapi_PBlock *pb);
  66. static void add_created_attrs(Operation *op, Slapi_Entry *e);
  67. static int check_rdn_for_created_attrs(Slapi_Entry *e);
  68. static void handle_fast_add(Slapi_PBlock *pb, Slapi_Entry *entry);
  69. static void add_uniqueid (Slapi_Entry *e);
  70. static PRBool check_oc_subentry(Slapi_Entry *e, struct berval **vals, char *normtype);
  71. /* This function is called to process operation that come over external connections */
  72. void
  73. do_add( Slapi_PBlock *pb )
  74. {
  75. Slapi_Operation *operation;
  76. BerElement *ber;
  77. char *last;
  78. ber_len_t len = -1;
  79. ber_tag_t tag;
  80. Slapi_Entry *e = NULL;
  81. int err;
  82. int rc;
  83. char ebuf[ BUFSIZ ];
  84. PRBool searchsubentry=PR_TRUE;
  85. LDAPDebug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
  86. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  87. ber = operation->o_ber;
  88. /* count the add request */
  89. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps);
  90. /*
  91. * Parse the add request. It looks like this:
  92. *
  93. * AddRequest := [APPLICATION 14] SEQUENCE {
  94. * name DistinguishedName,
  95. * attrs SEQUENCE OF SEQUENCE {
  96. * type AttributeType,
  97. * values SET OF AttributeValue
  98. * }
  99. * }
  100. */
  101. /* get the name */
  102. {
  103. char *dn = NULL;
  104. if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
  105. slapi_ch_free_string(&dn);
  106. LDAPDebug( LDAP_DEBUG_ANY,
  107. "ber_scanf failed (op=Add; params=DN)\n", 0, 0, 0 );
  108. op_shared_log_error_access (pb, "ADD", "???", "decoding error");
  109. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  110. "decoding error", 0, NULL );
  111. return;
  112. }
  113. e = slapi_entry_alloc();
  114. slapi_entry_init(e,dn,NULL); /* Responsibility for DN is passed to the Entry. */
  115. }
  116. LDAPDebug( LDAP_DEBUG_ARGS, " do_add: dn (%s)\n", slapi_entry_get_dn_const(e), 0, 0 );
  117. /* get the attrs */
  118. for ( tag = ber_first_element( ber, &len, &last );
  119. tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET;
  120. tag = ber_next_element( ber, &len, last ) ) {
  121. char *type = NULL, *normtype = NULL;
  122. struct berval **vals = NULL;
  123. len = -1; /* reset - not used in loop */
  124. if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
  125. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)), "decoding error");
  126. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  127. "decoding error", 0, NULL );
  128. slapi_ch_free_string(&type);
  129. ber_bvecfree( vals );
  130. goto free_and_return;
  131. }
  132. if ( vals == NULL ) {
  133. LDAPDebug( LDAP_DEBUG_ANY, "no values for type %s\n", type, 0, 0 );
  134. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)), "null value");
  135. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL,
  136. 0, NULL );
  137. slapi_ch_free_string(&type);
  138. goto free_and_return;
  139. }
  140. normtype = slapi_attr_syntax_normalize(type);
  141. if ( !normtype || !*normtype ) {
  142. rc = LDAP_INVALID_SYNTAX;
  143. PR_snprintf (ebuf, BUFSIZ, "invalid type '%s'", type);
  144. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)), ebuf);
  145. send_ldap_result( pb, rc, NULL, ebuf, 0, NULL );
  146. slapi_ch_free_string(&type);
  147. slapi_ch_free( (void**)&normtype );
  148. ber_bvecfree( vals );
  149. goto free_and_return;
  150. }
  151. slapi_ch_free_string(&type);
  152. /* for now we just ignore attributes that client is not allowed
  153. to modify so not to break existing clients */
  154. if (op_shared_is_allowed_attr (normtype, pb->pb_conn->c_isreplication_session)){
  155. if (( rc = slapi_entry_add_values( e, normtype, vals ))
  156. != LDAP_SUCCESS ) {
  157. slapi_log_access( LDAP_DEBUG_STATS,
  158. "conn=%" NSPRIu64 " op=%d ADD dn=\"%s\", add values for type %s failed\n",
  159. pb->pb_conn->c_connid, operation->o_opid,
  160. escape_string( slapi_entry_get_dn_const(e), ebuf ), normtype );
  161. send_ldap_result( pb, rc, NULL, NULL, 0, NULL );
  162. slapi_ch_free( (void**)&normtype );
  163. ber_bvecfree( vals );
  164. goto free_and_return;
  165. }
  166. /* if this is uniqueid attribute, set uniqueid field of the entry */
  167. if (strcasecmp (normtype, SLAPI_ATTR_UNIQUEID) == 0)
  168. {
  169. e->e_uniqueid = slapi_ch_strdup (vals[0]->bv_val);
  170. }
  171. if(searchsubentry) searchsubentry=check_oc_subentry(e,vals,normtype);
  172. }
  173. slapi_ch_free( (void**)&normtype );
  174. ber_bvecfree( vals );
  175. }
  176. /* Ensure that created attributes are not used in the RDN. */
  177. if (check_rdn_for_created_attrs(e)) {
  178. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), "invalid DN");
  179. send_ldap_result( pb, LDAP_INVALID_DN_SYNTAX, NULL, "illegal attribute in RDN", 0, NULL );
  180. goto free_and_return;
  181. }
  182. if ( (tag != LBER_END_OF_SEQORSET) && (len != -1) ) {
  183. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)), "decoding error");
  184. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  185. "decoding error", 0, NULL );
  186. goto free_and_return;
  187. }
  188. /*
  189. * in LDAPv3 there can be optional control extensions on
  190. * the end of an LDAPMessage. we need to read them in and
  191. * pass them to the backend.
  192. */
  193. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  194. op_shared_log_error_access (pb, "ADD", slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)),
  195. "failed to decode LDAP controls");
  196. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  197. goto free_and_return;
  198. }
  199. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &operation->o_isroot );
  200. slapi_pblock_set( pb, SLAPI_ADD_ENTRY, e );
  201. if (pb->pb_conn->c_flags & CONN_FLAG_IMPORT) {
  202. /* this add is actually part of a bulk import -- punt */
  203. handle_fast_add(pb, e);
  204. } else {
  205. op_shared_add ( pb );
  206. }
  207. /* make sure that we don't free entry if it is successfully added */
  208. e = NULL;
  209. free_and_return:;
  210. if (e)
  211. slapi_entry_free (e);
  212. }
  213. /* This function is used to issue internal add operation
  214. This is an old style API. Its use is discoraged because it is not extendable and
  215. because it does not allow to check whether plugin has right to access part of the
  216. tree it is trying to modify. Use slapi_add_internal_pb instead */
  217. Slapi_PBlock *
  218. slapi_add_internal(const char *idn,
  219. LDAPMod **iattrs,
  220. LDAPControl **controls,
  221. int dummy)
  222. {
  223. Slapi_Entry *e;
  224. Slapi_PBlock *result_pb = NULL;
  225. int opresult= -1;
  226. if(iattrs == NULL)
  227. {
  228. opresult = LDAP_PARAM_ERROR;
  229. goto done;
  230. }
  231. opresult = slapi_mods2entry (&e, (char*)idn, iattrs);
  232. if (opresult != LDAP_SUCCESS)
  233. {
  234. goto done;
  235. }
  236. result_pb= slapi_add_entry_internal(e, controls, dummy);
  237. done:
  238. if(result_pb==NULL)
  239. {
  240. result_pb = slapi_pblock_new();
  241. pblock_init(result_pb);
  242. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  243. }
  244. return result_pb;
  245. }
  246. /* This function is used to issue internal add operation
  247. This is an old style API. Its use is discoraged because it is not extendable and
  248. because it does not allow to check whether plugin has right to access part of the
  249. tree it is trying to modify. Use slapi_add_internal_pb instead
  250. Beware: The entry is consumed. */
  251. Slapi_PBlock *
  252. slapi_add_entry_internal(Slapi_Entry *e, LDAPControl **controls, int dummy)
  253. {
  254. Slapi_PBlock pb;
  255. Slapi_PBlock *result_pb = NULL;
  256. int opresult;
  257. pblock_init(&pb);
  258. slapi_add_entry_internal_set_pb (&pb, e, controls, plugin_get_default_component_id(), 0);
  259. add_internal_pb (&pb);
  260. result_pb = slapi_pblock_new();
  261. if (result_pb)
  262. {
  263. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  264. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  265. }
  266. pblock_done(&pb);
  267. return result_pb;
  268. }
  269. /* This is new style API to issue internal add operation.
  270. pblock should contain the following data (can be set via call to slapi_add_internal_set_pb):
  271. SLAPI_TARGET_DN set to dn of the new entry
  272. SLAPI_CONTROLS_ARG set to request controls if present
  273. SLAPI_ADD_ENTRY set to Slapi_Entry to add
  274. Beware: The entry is consumed. */
  275. int slapi_add_internal_pb (Slapi_PBlock *pb)
  276. {
  277. if (pb == NULL)
  278. return -1;
  279. if (!allow_operation (pb))
  280. {
  281. slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  282. "This plugin is not configured to access operation target data", 0, NULL );
  283. return 0;
  284. }
  285. return add_internal_pb (pb);
  286. }
  287. int slapi_add_internal_set_pb (Slapi_PBlock *pb, const char *dn, LDAPMod **attrs, LDAPControl **controls,
  288. Slapi_ComponentId *plugin_identity, int operation_flags)
  289. {
  290. Slapi_Entry *e;
  291. int rc;
  292. if (pb == NULL || dn == NULL || attrs == NULL)
  293. {
  294. slapi_log_error(SLAPI_LOG_PLUGIN, NULL, "slapi_add_internal_set_pb: invalid argument\n");
  295. return LDAP_PARAM_ERROR;
  296. }
  297. rc = slapi_mods2entry (&e, dn, attrs);
  298. if (rc == LDAP_SUCCESS)
  299. {
  300. slapi_add_entry_internal_set_pb (pb, e, controls, plugin_identity, operation_flags);
  301. }
  302. return rc;
  303. }
  304. /* Initialize a pblock for a call to slapi_add_internal_pb() */
  305. void slapi_add_entry_internal_set_pb (Slapi_PBlock *pb, Slapi_Entry *e, LDAPControl **controls,
  306. Slapi_ComponentId *plugin_identity, int operation_flags)
  307. {
  308. Operation *op;
  309. PR_ASSERT (pb != NULL);
  310. if (pb == NULL || e == NULL)
  311. {
  312. slapi_log_error(SLAPI_LOG_PLUGIN, NULL, "slapi_add_entry_internal_set_pb: invalid argument\n");
  313. return;
  314. }
  315. op = internal_operation_new(SLAPI_OPERATION_ADD,operation_flags);
  316. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  317. slapi_pblock_set(pb, SLAPI_ADD_ENTRY, e);
  318. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  319. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  320. }
  321. /* Helper functions */
  322. static int add_internal_pb (Slapi_PBlock *pb)
  323. {
  324. LDAPControl **controls;
  325. Operation *op;
  326. int opresult = 0;
  327. Slapi_Entry *e;
  328. PR_ASSERT (pb != NULL);
  329. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  330. slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
  331. if (e == NULL)
  332. {
  333. opresult = LDAP_PARAM_ERROR;
  334. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  335. return 0;
  336. }
  337. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  338. op->o_handler_data = &opresult;
  339. op->o_result_handler = internal_getresult_callback;
  340. slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
  341. /* set parameters common to all internal operations */
  342. set_common_params (pb);
  343. /* set actions taken to process the operation */
  344. set_config_params (pb);
  345. /* perform the add operation */
  346. op_shared_add (pb);
  347. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  348. return 0;
  349. }
  350. /* Code shared between regular and internal add operation */
  351. static void op_shared_add (Slapi_PBlock *pb)
  352. {
  353. Slapi_Operation *operation;
  354. Slapi_Entry *e, *pse;
  355. Slapi_Backend *be = NULL;
  356. int err;
  357. char ebuf[BUFSIZ];
  358. int internal_op, repl_op, legacy_op, lastmod;
  359. char *pwdtype = NULL;
  360. Slapi_Value **unhashed_password_vals = NULL;
  361. Slapi_Attr *attr = NULL;
  362. Slapi_Entry *referral;
  363. char errorbuf[BUFSIZ];
  364. struct slapdplugin *p = NULL;
  365. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  366. slapi_pblock_get (pb, SLAPI_ADD_ENTRY, &e);
  367. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  368. slapi_pblock_get (pb, SLAPI_IS_LEGACY_REPLICATED_OPERATION, &legacy_op);
  369. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  370. /* target spec is used to decide which plugins are applicable for the operation */
  371. operation_set_target_spec (operation, slapi_entry_get_sdn (e));
  372. if ((err = slapi_entry_add_rdn_values(e)) != LDAP_SUCCESS)
  373. {
  374. send_ldap_result(pb, err, NULL, "failed to add RDN values", 0, NULL);
  375. goto done;
  376. }
  377. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  378. {
  379. if ( !internal_op )
  380. {
  381. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d ADD dn=\"%s\"\n",
  382. pb->pb_conn->c_connid,
  383. operation->o_opid,
  384. escape_string(slapi_entry_get_dn_const(e), ebuf));
  385. }
  386. else
  387. {
  388. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d ADD dn=\"%s\"\n",
  389. LOG_INTERNAL_OP_CON_ID,
  390. LOG_INTERNAL_OP_OP_ID,
  391. escape_string(slapi_entry_get_dn_const(e), ebuf));
  392. }
  393. }
  394. /*
  395. * We could be serving multiple database backends. Select the
  396. * appropriate one.
  397. */
  398. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  399. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  400. be = NULL;
  401. goto done;
  402. }
  403. if (referral)
  404. {
  405. int managedsait;
  406. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  407. if (managedsait)
  408. {
  409. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  410. "cannot update referral", 0, NULL);
  411. slapi_entry_free(referral);
  412. goto done;
  413. }
  414. slapi_pblock_set(pb, SLAPI_TARGET_DN, (void*)slapi_sdn_get_ndn(operation_get_target_spec (operation)));
  415. send_referrals_from_entry(pb,referral);
  416. slapi_entry_free(referral);
  417. goto done;
  418. }
  419. if (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)) {
  420. /* look for user password attribute */
  421. slapi_entry_attr_find(e, SLAPI_USERPWD_ATTR, &attr);
  422. if (attr && !repl_op)
  423. {
  424. Slapi_Value **present_values;
  425. present_values= attr_get_present_values(attr);
  426. /* Set the backend in the pblock. The slapi_access_allowed function
  427. * needs this set to work properly. */
  428. slapi_pblock_set( pb, SLAPI_BACKEND, slapi_be_select( slapi_entry_get_sdn_const(e) ) );
  429. /* Check ACI before checking password syntax */
  430. if ( (err = slapi_access_allowed(pb, e, SLAPI_USERPWD_ATTR, NULL,
  431. SLAPI_ACL_ADD)) != LDAP_SUCCESS) {
  432. send_ldap_result(pb, err, NULL,
  433. "Insufficient 'add' privilege to the "
  434. "'userPassword' attribute", 0, NULL);
  435. goto done;
  436. }
  437. /* check password syntax */
  438. if (check_pw_syntax(pb, slapi_entry_get_sdn_const(e), present_values, NULL, e, 0) == 0)
  439. {
  440. Slapi_Value **vals= NULL;
  441. valuearray_add_valuearray(&unhashed_password_vals, present_values, 0);
  442. valuearray_add_valuearray(&vals, present_values, 0);
  443. pw_encodevals(vals);
  444. add_password_attrs(pb, operation, e);
  445. slapi_entry_attr_replace_sv(e, SLAPI_USERPWD_ATTR, vals);
  446. valuearray_free(&vals);
  447. /* Add the unhashed password pseudo-attribute to the entry */
  448. pwdtype = slapi_attr_syntax_normalize(PSEUDO_ATTR_UNHASHEDUSERPASSWORD);
  449. slapi_entry_add_values_sv(e, pwdtype, unhashed_password_vals);
  450. } else {
  451. /* error result is sent from check_pw_syntax */
  452. goto done;
  453. }
  454. }
  455. /* look for multiple backend local credentials or replication local credentials */
  456. for ( p = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME); p != NULL;
  457. p = p->plg_next )
  458. {
  459. char *L_attr = NULL;
  460. int i=0;
  461. /* Get the appropriate decoding function */
  462. for ( L_attr = p->plg_argv[i]; i<p->plg_argc; L_attr = p->plg_argv[++i])
  463. {
  464. /* look for multiple backend local credentials or replication local credentials */
  465. char *L_normalized = slapi_attr_syntax_normalize(L_attr);
  466. slapi_entry_attr_find(e, L_normalized, &attr);
  467. if (attr)
  468. {
  469. Slapi_Value **present_values = NULL;
  470. Slapi_Value **vals = NULL;
  471. present_values= attr_get_present_values(attr);
  472. valuearray_add_valuearray(&vals, present_values, 0);
  473. pw_rever_encode(vals, L_normalized);
  474. slapi_entry_attr_replace_sv(e, L_normalized, vals);
  475. valuearray_free(&vals);
  476. }
  477. if (L_normalized)
  478. slapi_ch_free ((void**)&L_normalized);
  479. }
  480. }
  481. }
  482. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  483. /* we set local password policy ACI for non-replicated operations only */
  484. if (!repl_op &&
  485. !operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP) &&
  486. !operation_is_flag_set(operation, OP_FLAG_LEGACY_REPLICATION_DN) &&
  487. !slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA) &&
  488. !slapi_be_private(be) &&
  489. slapi_be_issuffix (be, slapi_entry_get_sdn_const(e)))
  490. {
  491. /* this is a suffix. update the pw aci */
  492. slapdFrontendConfig_t *slapdFrontendConfig;
  493. slapdFrontendConfig = getFrontendConfig();
  494. pw_add_allowchange_aci(e, !slapdFrontendConfig->pw_policy.pw_change &&
  495. !slapdFrontendConfig->pw_policy.pw_must_change);
  496. }
  497. /* can get lastmod only after backend is selected */
  498. slapi_pblock_get(pb, SLAPI_BE_LASTMOD, &lastmod);
  499. if (!repl_op && lastmod)
  500. {
  501. add_created_attrs(operation, e);
  502. /* JCM - We could end up with an entry without a uniqueid...??? */
  503. }
  504. /* expand objectClass values to reflect the inheritance hierarchy */
  505. slapi_schema_expand_objectclasses( e );
  506. /* uniqueid needs to be generated for entries added during legacy replication */
  507. if (legacy_op)
  508. add_uniqueid (e);
  509. /*
  510. * call the pre-add plugins. if they succeed, call
  511. * the backend add function. then call the post-add
  512. * plugins.
  513. */
  514. slapi_pblock_set(pb, SLAPI_ADD_TARGET,
  515. (char*)slapi_sdn_get_ndn(slapi_entry_get_sdn_const(e)));
  516. if (plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN :
  517. SLAPI_PLUGIN_PRE_ADD_FN) == 0)
  518. {
  519. int rc;
  520. Slapi_Entry *ec;
  521. char *add_target_dn;
  522. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  523. set_db_default_result_handlers(pb);
  524. /* Remove the unhashed password pseudo-attribute
  525. from the entry before duplicating the entry */
  526. if (unhashed_password_vals)
  527. {
  528. slapi_entry_delete_values(e, pwdtype, NULL);
  529. }
  530. /* because be_add frees the entry */
  531. ec = slapi_entry_dup(e);
  532. add_target_dn= slapi_ch_strdup(slapi_sdn_get_ndn(slapi_entry_get_sdn_const(ec)));
  533. slapi_pblock_set(pb, SLAPI_ADD_TARGET, add_target_dn);
  534. if (be->be_add != NULL)
  535. {
  536. rc = (*be->be_add)(pb);
  537. slapi_pblock_set(pb, SLAPI_ADD_ENTRY, ec);
  538. if (rc == 0)
  539. {
  540. /* acl is not enabled for internal operations */
  541. /* don't update aci store for remote acis */
  542. if ((!internal_op) &&
  543. (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  544. {
  545. plugin_call_acl_mods_update (pb, SLAPI_OPERATION_ADD);
  546. }
  547. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  548. {
  549. write_audit_log_entry(pb); /* Record the operation in the audit log */
  550. }
  551. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  552. do_ps_service(pse, NULL, LDAP_CHANGETYPE_ADD, 0);
  553. /* If be_add succeeded, then e is consumed. We
  554. * set e to NULL to prevent freeing it ourselves. */
  555. e = NULL;
  556. }
  557. else
  558. {
  559. if (rc == SLAPI_FAIL_DISKFULL)
  560. {
  561. operation_out_of_disk_space();
  562. goto done;
  563. }
  564. }
  565. }
  566. else
  567. {
  568. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  569. "Function not implemented", 0, NULL);
  570. }
  571. /* Reattach the unhashed password pseudo-attribute
  572. to the entry copy (ec), before calling the postop plugin */
  573. if(unhashed_password_vals)
  574. {
  575. slapi_entry_add_values_sv(ec, pwdtype, unhashed_password_vals);
  576. }
  577. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  578. plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_POST_ADD_FN :
  579. SLAPI_PLUGIN_POST_ADD_FN);
  580. slapi_entry_free(ec);
  581. slapi_pblock_get(pb, SLAPI_ADD_TARGET, &add_target_dn);
  582. slapi_ch_free((void**)&add_target_dn);
  583. }
  584. done:
  585. if (be)
  586. slapi_be_Unlock(be);
  587. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  588. slapi_entry_free(pse);
  589. slapi_ch_free((void **)&operation->o_params.p.p_add.parentuniqueid);
  590. slapi_entry_free(e);
  591. valuearray_free(&unhashed_password_vals);
  592. slapi_ch_free((void**)&pwdtype);
  593. }
  594. static void
  595. add_created_attrs(Operation *op, Slapi_Entry *e)
  596. {
  597. char buf[20];
  598. struct berval bv;
  599. struct berval *bvals[2];
  600. time_t curtime;
  601. struct tm ltm;
  602. LDAPDebug(LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0);
  603. bvals[0] = &bv;
  604. bvals[1] = NULL;
  605. if (slapi_sdn_isempty(&op->o_sdn)) {
  606. bv.bv_val = "";
  607. bv.bv_len = strlen(bv.bv_val);
  608. } else {
  609. bv.bv_val = (char*)slapi_sdn_get_dn(&op->o_sdn);
  610. bv.bv_len = strlen(bv.bv_val);
  611. }
  612. slapi_entry_attr_replace(e, "creatorsname", bvals);
  613. slapi_entry_attr_replace(e, "modifiersname", bvals);
  614. curtime = current_time();
  615. #ifdef _WIN32
  616. {
  617. struct tm *pt;
  618. pt = gmtime(&curtime);
  619. memcpy(&ltm, pt, sizeof(struct tm));
  620. }
  621. #else
  622. gmtime_r(&curtime, &ltm);
  623. #endif
  624. strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &ltm);
  625. bv.bv_val = buf;
  626. bv.bv_len = strlen(bv.bv_val);
  627. slapi_entry_attr_replace(e, "createtimestamp", bvals);
  628. bv.bv_val = buf;
  629. bv.bv_len = strlen(bv.bv_val);
  630. slapi_entry_attr_replace(e, "modifytimestamp", bvals);
  631. add_uniqueid (e);
  632. }
  633. /* Checks if created attributes are used in the RDN.
  634. * Returns 1 if created attrs are in the RDN, and
  635. * 0 if created attrs are not in the RDN. Returns
  636. * -1 if an error occurred.
  637. */
  638. static int check_rdn_for_created_attrs(Slapi_Entry *e)
  639. {
  640. int i, rc = 0;
  641. Slapi_RDN *rdn = NULL;
  642. char *value = NULL;
  643. char *type[] = {SLAPI_ATTR_UNIQUEID, "modifytimestamp", "createtimestamp",
  644. "creatorsname", "modifiersname", 0};
  645. if ((rdn = slapi_rdn_new())) {
  646. slapi_rdn_init_dn(rdn, slapi_entry_get_dn_const(e));
  647. for (i = 0; type[i] != NULL; i++) {
  648. if (slapi_rdn_contains_attr(rdn, type[i], &value)) {
  649. LDAPDebug(LDAP_DEBUG_TRACE, "Invalid DN. RDN contains %s attribute\n", type[i], 0, 0);
  650. rc = 1;
  651. break;
  652. }
  653. }
  654. slapi_rdn_free(&rdn);
  655. } else {
  656. LDAPDebug(LDAP_DEBUG_TRACE, "check_rdn_for_created_attrs: Error allocating RDN\n", 0, 0, 0);
  657. rc = -1;
  658. }
  659. return rc;
  660. }
  661. static void handle_fast_add(Slapi_PBlock *pb, Slapi_Entry *entry)
  662. {
  663. Slapi_Backend *be;
  664. Slapi_Operation *operation;
  665. int ret;
  666. be = pb->pb_conn->c_bi_backend;
  667. if ((be == NULL) || (be->be_wire_import == NULL)) {
  668. /* can this even happen? */
  669. LDAPDebug(LDAP_DEBUG_ANY,
  670. "handle_fast_add: backend not supported\n", 0, 0, 0);
  671. send_ldap_result(pb, LDAP_NOT_SUPPORTED, NULL, NULL, 0, NULL);
  672. return;
  673. }
  674. /* ensure that the RDN values are present as attribute values */
  675. if ((ret = slapi_entry_add_rdn_values(entry)) != LDAP_SUCCESS) {
  676. send_ldap_result(pb, ret, NULL, "failed to add RDN values", 0, NULL);
  677. return;
  678. }
  679. /* schema check */
  680. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  681. if (operation_is_flag_set(operation, OP_FLAG_ACTION_SCHEMA_CHECK) &&
  682. (slapi_entry_schema_check(pb, entry) != 0)) {
  683. char *errtext;
  684. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0, 0);
  685. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
  686. send_ldap_result(pb, LDAP_OBJECT_CLASS_VIOLATION, NULL, errtext, 0, NULL);
  687. slapi_entry_free(entry);
  688. return;
  689. }
  690. /* syntax check */
  691. if (slapi_entry_syntax_check(pb, entry, 0) != 0) {
  692. char *errtext;
  693. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed syntax check\n", 0, 0, 0);
  694. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
  695. send_ldap_result(pb, LDAP_INVALID_SYNTAX, NULL, errtext, 0, NULL);
  696. slapi_entry_free(entry);
  697. return;
  698. }
  699. /* Check if the entry being added is a Tombstone. Could be if we are
  700. * doing a replica init. */
  701. if (slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS,
  702. SLAPI_ATTR_VALUE_TOMBSTONE)) {
  703. entry->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
  704. }
  705. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  706. slapi_pblock_set(pb, SLAPI_BULK_IMPORT_ENTRY, entry);
  707. ret = SLAPI_BI_STATE_ADD;
  708. slapi_pblock_set(pb, SLAPI_BULK_IMPORT_STATE, &ret);
  709. ret = (*be->be_wire_import)(pb);
  710. if (ret != 0) {
  711. LDAPDebug(LDAP_DEBUG_ANY,
  712. "wire import: error during import (%d)\n",
  713. ret, 0, 0);
  714. send_ldap_result(pb, LDAP_OPERATIONS_ERROR,
  715. NULL, NULL, 0, NULL);
  716. /* It's our responsibility to free the entry if
  717. * be_wire_import doesn't succeed. */
  718. slapi_entry_free(entry);
  719. /* turn off fast replica init -- import is now aborted */
  720. pb->pb_conn->c_bi_backend = NULL;
  721. pb->pb_conn->c_flags &= ~CONN_FLAG_IMPORT;
  722. return;
  723. }
  724. send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  725. return;
  726. }
  727. static void
  728. add_uniqueid (Slapi_Entry *e)
  729. {
  730. char *uniqueid;
  731. int rc;
  732. /* generate uniqueID for the entry */
  733. rc = slapi_uniqueIDGenerateString (&uniqueid);
  734. if (rc == UID_SUCCESS)
  735. {
  736. slapi_entry_set_uniqueid (e, uniqueid);
  737. }
  738. else
  739. {
  740. LDAPDebug(LDAP_DEBUG_ANY, "add_created_attrs: uniqueid generation failed for %s; error = %d\n",
  741. slapi_entry_get_dn_const(e), rc, 0);
  742. }
  743. }
  744. static PRBool
  745. check_oc_subentry(Slapi_Entry *e, struct berval **vals, char *normtype) {
  746. int n;
  747. PRBool subentry=PR_TRUE;
  748. for(n=0; vals != NULL && vals[n] != NULL; n++) {
  749. if((strcasecmp(normtype,"objectclass") == 0)
  750. && (strncasecmp((const char *)vals[n]->bv_val,"ldapsubentry",vals[n]->bv_len) == 0)) {
  751. e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
  752. subentry=PR_FALSE;
  753. break;
  754. }
  755. }
  756. return subentry;
  757. }