add.c 33 KB

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