add.c 32 KB

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