modify.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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, 2010 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 <stdio.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include "slap.h"
  35. #include "pratom.h"
  36. #if defined(irix) || defined(aix)
  37. #include <time.h>
  38. #endif
  39. /* Forward declarations */
  40. static int modify_internal_pb (Slapi_PBlock *pb);
  41. static void op_shared_modify (Slapi_PBlock *pb, int pw_change, char *old_pw);
  42. #if 0 /* not used */
  43. static void remove_mod (Slapi_Mods *smods, const char *type, Slapi_Mods *smod_unhashed);
  44. #endif
  45. static int op_shared_allow_pw_change (Slapi_PBlock *pb, LDAPMod *mod, char **old_pw, Slapi_Mods *smods);
  46. static int hash_rootpw (LDAPMod **mods);
  47. static int valuearray_init_bervalarray_unhashed_only(struct berval **bvals, Slapi_Value ***cvals);
  48. static void optimize_mods(Slapi_Mods *smods);
  49. #ifdef LDAP_DEBUG
  50. static const char*
  51. mod_op_image (int op)
  52. {
  53. switch (op & ~LDAP_MOD_BVALUES) {
  54. case LDAP_MOD_ADD: return "add";
  55. case LDAP_MOD_DELETE: return "delete";
  56. case LDAP_MOD_REPLACE: return "replace";
  57. default: break;
  58. }
  59. return "???";
  60. }
  61. #endif
  62. /* an AttrCheckFunc function should return an LDAP result code (LDAP_SUCCESS if all goes well). */
  63. typedef int (*AttrCheckFunc)(const char *attr_name, char *value, long minval, long maxval, char *errorbuf, size_t ebuflen);
  64. static struct attr_value_check {
  65. const char *attr_name; /* the name of the attribute */
  66. AttrCheckFunc checkfunc;
  67. long minval;
  68. long maxval;
  69. } AttrValueCheckList[] = {
  70. {CONFIG_PW_SYNTAX_ATTRIBUTE, attr_check_onoff, 0, 0},
  71. {CONFIG_PW_CHANGE_ATTRIBUTE, attr_check_onoff, 0, 0},
  72. {CONFIG_PW_LOCKOUT_ATTRIBUTE, attr_check_onoff, 0, 0},
  73. {CONFIG_PW_MUSTCHANGE_ATTRIBUTE, attr_check_onoff, 0, 0},
  74. {CONFIG_PW_EXP_ATTRIBUTE, attr_check_onoff, 0, 0},
  75. {CONFIG_PW_UNLOCK_ATTRIBUTE, attr_check_onoff, 0, 0},
  76. {CONFIG_PW_HISTORY_ATTRIBUTE, attr_check_onoff, 0, 0},
  77. {CONFIG_PW_MINAGE_ATTRIBUTE, check_pw_duration_value, -1, -1},
  78. {CONFIG_PW_WARNING_ATTRIBUTE, check_pw_duration_value, 0, -1},
  79. {CONFIG_PW_MINLENGTH_ATTRIBUTE, attr_check_minmax, 2, 512},
  80. {CONFIG_PW_MAXFAILURE_ATTRIBUTE, attr_check_minmax, 1, 32767},
  81. {CONFIG_PW_INHISTORY_ATTRIBUTE, attr_check_minmax, 1, 24},
  82. {CONFIG_PW_LOCKDURATION_ATTRIBUTE, check_pw_duration_value, -1, -1},
  83. {CONFIG_PW_RESETFAILURECOUNT_ATTRIBUTE, check_pw_resetfailurecount_value, -1, -1},
  84. {CONFIG_PW_GRACELIMIT_ATTRIBUTE, attr_check_minmax, 0, -1},
  85. {CONFIG_PW_STORAGESCHEME_ATTRIBUTE, check_pw_storagescheme_value, -1, -1},
  86. {CONFIG_PW_MAXAGE_ATTRIBUTE, check_pw_duration_value, -1, -1}
  87. };
  88. /* This function is called to process operation that come over external connections */
  89. void
  90. do_modify( Slapi_PBlock *pb )
  91. {
  92. Slapi_Operation *operation;
  93. Slapi_Mods smods;
  94. BerElement *ber;
  95. ber_tag_t tag;
  96. ber_len_t len;
  97. LDAPMod **normalized_mods = NULL;
  98. LDAPMod *mod;
  99. LDAPMod **mods;
  100. char *last, *type = NULL;
  101. char *old_pw = NULL; /* remember the old password */
  102. char *rawdn = NULL;
  103. int minssf_exclude_rootdse = 0;
  104. int ignored_some_mods = 0;
  105. int has_password_mod = 0; /* number of password mods */
  106. int pw_change = 0; /* 0 = no password change */
  107. int err;
  108. LDAPDebug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
  109. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  110. ber = operation->o_ber;
  111. /* count the modify request */
  112. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps);
  113. /*
  114. * Parse the modify request. It looks like this:
  115. *
  116. * ModifyRequest := [APPLICATION 6] SEQUENCE {
  117. * name DistinguishedName,
  118. * mods SEQUENCE OF SEQUENCE {
  119. * operation ENUMERATED {
  120. * add (0),
  121. * delete (1),
  122. * replace (2)
  123. * },
  124. * modification SEQUENCE {
  125. * type AttributeType,
  126. * values SET OF AttributeValue
  127. * }
  128. * }
  129. * }
  130. */
  131. {
  132. int rc = 0;
  133. if ( ber_scanf( ber, "{a", &rawdn ) == LBER_ERROR )
  134. {
  135. LDAPDebug( LDAP_DEBUG_ANY,
  136. "ber_scanf failed (op=Modify; params=DN)\n", 0, 0, 0 );
  137. op_shared_log_error_access (pb, "MOD", "???", "decoding error");
  138. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0, NULL );
  139. slapi_ch_free_string(&rawdn);
  140. return;
  141. }
  142. /* Check if we should be performing strict validation. */
  143. if (config_get_dn_validate_strict()) {
  144. /* check that the dn is formatted correctly */
  145. rc = slapi_dn_syntax_check(pb, rawdn, 1);
  146. if (rc) { /* syntax check failed */
  147. op_shared_log_error_access(pb, "MOD", rawdn?rawdn:"",
  148. "strict: invalid dn");
  149. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  150. NULL, "invalid dn", 0, NULL);
  151. slapi_ch_free((void **) &rawdn);
  152. return;
  153. }
  154. }
  155. }
  156. LDAPDebug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", rawdn, 0, 0 );
  157. /*
  158. * If nsslapd-minssf-exclude-rootdse is on, the minssf check has been
  159. * postponed until here. We should do it now.
  160. */
  161. minssf_exclude_rootdse = config_get_minssf_exclude_rootdse();
  162. if (minssf_exclude_rootdse) {
  163. int minssf = 0;
  164. /* Check if the minimum SSF requirement has been met. */
  165. minssf = config_get_minssf();
  166. if ((pb->pb_conn->c_sasl_ssf < minssf) &&
  167. (pb->pb_conn->c_ssl_ssf < minssf) &&
  168. (pb->pb_conn->c_local_ssf < minssf)) {
  169. op_shared_log_error_access(pb, "MOD", rawdn?rawdn:"",
  170. "Minimum SSF not met");
  171. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  172. "Minimum SSF not met.", 0, NULL);
  173. slapi_ch_free((void **) &rawdn);
  174. return;
  175. }
  176. }
  177. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &pb->pb_op->o_isroot);
  178. slapi_pblock_set( pb, SLAPI_ORIGINAL_TARGET, rawdn );
  179. /* collect modifications & save for later */
  180. slapi_mods_init(&smods, 0);
  181. len = -1;
  182. for ( tag = ber_first_element( ber, &len, &last );
  183. tag != LBER_ERROR && tag != LBER_END_OF_SEQORSET;
  184. tag = ber_next_element( ber, &len, last ) )
  185. {
  186. ber_int_t mod_op;
  187. mod = (LDAPMod *) slapi_ch_malloc( sizeof(LDAPMod) );
  188. mod->mod_bvalues = NULL;
  189. len = -1; /* reset - len is not used */
  190. if ( ber_scanf( ber, "{i{a[V]}}", &mod_op, &type,
  191. &mod->mod_bvalues ) == LBER_ERROR )
  192. {
  193. op_shared_log_error_access (pb, "MOD", rawdn, "decoding error");
  194. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  195. "decoding error", 0, NULL );
  196. ber_bvecfree(mod->mod_bvalues);
  197. slapi_ch_free((void **)&mod);
  198. slapi_ch_free_string(&type);
  199. goto free_and_return;
  200. }
  201. mod->mod_op = mod_op;
  202. mod->mod_type = slapi_attr_syntax_normalize(type);
  203. if ( !mod->mod_type || !*mod->mod_type ) {
  204. char ebuf[SLAPI_DSE_RETURNTEXT_SIZE];
  205. PR_snprintf (ebuf, sizeof(ebuf), "invalid type '%s'", type);
  206. op_shared_log_error_access (pb, "MOD", rawdn, ebuf);
  207. send_ldap_result( pb, LDAP_INVALID_SYNTAX, NULL, ebuf, 0, NULL );
  208. slapi_ch_free((void **)&type);
  209. ber_bvecfree(mod->mod_bvalues);
  210. slapi_ch_free_string(&mod->mod_type);
  211. slapi_ch_free((void **)&mod);
  212. goto free_and_return;
  213. }
  214. slapi_ch_free((void **)&type);
  215. if ( mod->mod_op != LDAP_MOD_ADD &&
  216. mod->mod_op != LDAP_MOD_DELETE &&
  217. mod->mod_op != LDAP_MOD_REPLACE )
  218. {
  219. op_shared_log_error_access (pb, "MOD", rawdn, "unrecognized modify operation");
  220. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  221. "unrecognized modify operation", 0, NULL );
  222. ber_bvecfree(mod->mod_bvalues);
  223. slapi_ch_free((void **)&(mod->mod_type));
  224. slapi_ch_free((void **)&mod);
  225. goto free_and_return;
  226. }
  227. if ( mod->mod_bvalues == NULL
  228. && mod->mod_op != LDAP_MOD_DELETE
  229. && mod->mod_op != LDAP_MOD_REPLACE )
  230. {
  231. op_shared_log_error_access (pb, "MOD", rawdn, "no values given");
  232. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  233. "no values given", 0, NULL );
  234. ber_bvecfree(mod->mod_bvalues);
  235. slapi_ch_free((void **)&(mod->mod_type));
  236. slapi_ch_free((void **)&mod);
  237. goto free_and_return;
  238. }
  239. /* check if user is allowed to modify the specified attribute */
  240. if (!op_shared_is_allowed_attr (mod->mod_type, pb->pb_conn->c_isreplication_session))
  241. {
  242. /*
  243. * For now we just ignore attributes that client is not allowed
  244. * to modify so not to break existing clients
  245. */
  246. ++ignored_some_mods;
  247. ber_bvecfree(mod->mod_bvalues);
  248. slapi_ch_free((void **)&(mod->mod_type));
  249. slapi_ch_free((void **)&mod);
  250. continue;
  251. }
  252. /* check for password change (including deletion) */
  253. if ( strcasecmp( mod->mod_type, SLAPI_USERPWD_ATTR ) == 0 ){
  254. has_password_mod++;
  255. }
  256. mod->mod_op |= LDAP_MOD_BVALUES;
  257. slapi_mods_add_ldapmod (&smods, mod);
  258. }
  259. if (ignored_some_mods && (0 == smods.num_elements)) {
  260. if(pb->pb_conn->c_isreplication_session){
  261. int connid, opid;
  262. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  263. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  264. LDAPDebug( LDAP_DEBUG_ANY,"Rejecting replicated password policy operation(conn=%d op=%d) for "
  265. "entry %s. To allow these changes to be accepted, set passwordIsGlobalPolicy to 'on' in "
  266. "cn=config.\n", connid, opid, rawdn);
  267. }
  268. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL, NULL, 0, NULL );
  269. goto free_and_return;
  270. }
  271. /* check for decoding error */
  272. /*
  273. if using mozldap - will return LBER_END_OF_SEQORSET if loop
  274. completed successfully, otherwise, other value
  275. if using openldap - will return LBER_DEFAULT in either case
  276. if there was at least one element read, len will be -1
  277. if there were no elements read (empty modify) len will be 0
  278. */
  279. #if defined(USE_OPENLDAP)
  280. if ( tag != LBER_END_OF_SEQORSET )
  281. {
  282. if ( ( len == 0 ) && ( 0 == smods.num_elements ) && !ignored_some_mods ) {
  283. /* ok - empty modify - allow empty modifies */
  284. } else if ( len != -1 ) {
  285. op_shared_log_error_access (pb, "MOD", rawdn, "decoding error");
  286. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, "decoding error", 0, NULL );
  287. goto free_and_return;
  288. }
  289. /* else ok */
  290. }
  291. #else
  292. if ( tag != LBER_END_OF_SEQORSET )
  293. {
  294. op_shared_log_error_access (pb, "MOD", rawdn, "decoding error");
  295. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, "decoding error", 0, NULL );
  296. goto free_and_return;
  297. }
  298. #endif
  299. /* decode the optional controls - put them in the pblock */
  300. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 )
  301. {
  302. op_shared_log_error_access (pb, "MOD", rawdn, "failed to decode LDAP controls");
  303. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  304. goto free_and_return;
  305. }
  306. /* if there are any password mods, see if they are allowed */
  307. if (has_password_mod) {
  308. /* iterate through the mods looking for password mods */
  309. for (mod = slapi_mods_get_first_mod(&smods);
  310. mod;
  311. mod = slapi_mods_get_next_mod(&smods)) {
  312. /* check for password change (including deletion) */
  313. if ( strcasecmp( mod->mod_type, SLAPI_USERPWD_ATTR ) == 0 ) {
  314. /* assumes controls have already been decoded and placed
  315. in the pblock */
  316. pw_change = op_shared_allow_pw_change(pb, mod, &old_pw, &smods);
  317. if (pw_change == -1) {
  318. goto free_and_return;
  319. }
  320. }
  321. }
  322. }
  323. if (!pb->pb_conn->c_isreplication_session &&
  324. pb->pb_conn->c_needpw && pw_change == 0 )
  325. {
  326. (void)slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
  327. op_shared_log_error_access (pb, "MOD", rawdn, "need new password");
  328. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL, NULL, 0, NULL );
  329. goto free_and_return;
  330. }
  331. #ifdef LDAP_DEBUG
  332. LDAPDebug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
  333. for (mod = slapi_mods_get_first_mod(&smods); mod != NULL;
  334. mod = slapi_mods_get_next_mod(&smods))
  335. {
  336. LDAPDebug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
  337. mod_op_image( mod->mod_op ), mod->mod_type, 0 );
  338. }
  339. #endif
  340. mods = slapi_mods_get_ldapmods_passout (&smods);
  341. /* normalize the mods */
  342. if (mods) {
  343. normalized_mods = normalize_mods2bvals((const LDAPMod**)mods);
  344. ldap_mods_free (mods, 1 /* Free the Array and the Elements */);
  345. if (normalized_mods == NULL) {
  346. /* NOTE: normalize_mods2bvals only handles DN syntax currently */
  347. op_shared_log_error_access(pb, "MOD", rawdn?rawdn:"",
  348. "mod includes invalid dn format");
  349. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  350. "mod includes invalid dn format", 0, NULL);
  351. goto free_and_return;
  352. }
  353. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, normalized_mods);
  354. }
  355. op_shared_modify ( pb, pw_change, old_pw );
  356. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &normalized_mods);
  357. ldap_mods_free (normalized_mods, 1 /* Free the Array and the Elements */);
  358. free_and_return:;
  359. slapi_ch_free ((void**)&rawdn);
  360. slapi_mods_done(&smods);
  361. }
  362. /* This function is used to issue internal modify operation
  363. This is an old style API. Its use is discoraged because it is not extendable and
  364. because it does not allow to check whether plugin has right to access part of the
  365. tree it is trying to modify. Use slapi_modify_internal_pb instead */
  366. Slapi_PBlock*
  367. slapi_modify_internal(const char *idn,
  368. LDAPMod **mods,
  369. LDAPControl **controls,
  370. int dummy)
  371. {
  372. Slapi_PBlock pb;
  373. Slapi_PBlock *result_pb = NULL;
  374. int opresult;
  375. pblock_init(&pb);
  376. slapi_modify_internal_set_pb (&pb, idn, (LDAPMod**)mods, controls, NULL,
  377. (void *)plugin_get_default_component_id(), 0);
  378. modify_internal_pb (&pb);
  379. result_pb = slapi_pblock_new();
  380. if (result_pb)
  381. {
  382. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  383. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  384. }
  385. pblock_done(&pb);
  386. return result_pb;
  387. }
  388. /* This is new style API to issue internal modify operation.
  389. pblock should contain the following data (can be set via call to slapi_modify_internal_set_pb):
  390. For uniqueid based operation:
  391. SLAPI_TARGET_DN set to dn that allows to select right backend, can be stale
  392. SLAPI_TARGET_UNIQUEID set to the uniqueid of the entry we are looking for
  393. SLAPI_MODIFY_MODS set to the mods
  394. SLAPI_CONTROLS_ARG set to request controls if present
  395. For dn based search:
  396. SLAPI_TARGET_DN set to the entry dn
  397. SLAPI_MODIFY_MODS set to the mods
  398. SLAPI_CONTROLS_ARG set to request controls if present
  399. */
  400. int slapi_modify_internal_pb (Slapi_PBlock *pb)
  401. {
  402. if (pb == NULL)
  403. return -1;
  404. if (!allow_operation (pb))
  405. {
  406. slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  407. "This plugin is not configured to access operation target data", 0, NULL );
  408. return 0;
  409. }
  410. return modify_internal_pb (pb);
  411. }
  412. /* Initialize a pblock for a call to slapi_modify_internal_pb() */
  413. void
  414. slapi_modify_internal_set_pb (Slapi_PBlock *pb, const char *dn,
  415. LDAPMod **mods, LDAPControl **controls,
  416. const char *uniqueid,
  417. Slapi_ComponentId *plugin_identity,
  418. int operation_flags)
  419. {
  420. Operation *op;
  421. PR_ASSERT (pb != NULL);
  422. if (pb == NULL || dn == NULL || mods == NULL)
  423. {
  424. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  425. "slapi_modify_internal_set_pb: NULL parameter\n");
  426. return;
  427. }
  428. op= internal_operation_new(SLAPI_OPERATION_MODIFY,operation_flags);
  429. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  430. slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET, (void*)dn);
  431. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  432. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  433. if (uniqueid)
  434. {
  435. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void*)uniqueid);
  436. }
  437. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  438. }
  439. /* Initialize a pblock for a call to slapi_modify_internal_pb() */
  440. void
  441. slapi_modify_internal_set_pb_ext(Slapi_PBlock *pb, const Slapi_DN *sdn,
  442. LDAPMod **mods, LDAPControl **controls,
  443. const char *uniqueid,
  444. Slapi_ComponentId *plugin_identity,
  445. int operation_flags)
  446. {
  447. Operation *op;
  448. PR_ASSERT (pb != NULL);
  449. if (pb == NULL || sdn == NULL || mods == NULL)
  450. {
  451. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  452. "slapi_modify_internal_set_pb_ext: NULL parameter\n");
  453. return;
  454. }
  455. op= internal_operation_new(SLAPI_OPERATION_MODIFY,operation_flags);
  456. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  457. slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET, (void *)slapi_sdn_get_dn(sdn));
  458. slapi_pblock_set(pb, SLAPI_TARGET_SDN, (void *)sdn);
  459. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  460. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  461. if (uniqueid)
  462. {
  463. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void*)uniqueid);
  464. }
  465. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  466. }
  467. /* Helper functions */
  468. static int modify_internal_pb (Slapi_PBlock *pb)
  469. {
  470. LDAPControl **controls;
  471. int pwpolicy_ctrl = 0;
  472. Operation *op;
  473. int opresult = 0;
  474. LDAPMod **normalized_mods = NULL;
  475. LDAPMod **mods;
  476. LDAPMod **mod;
  477. Slapi_Mods smods;
  478. int pw_change = 0;
  479. char *old_pw = NULL;
  480. PR_ASSERT (pb != NULL);
  481. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  482. slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
  483. /* See if pwpolicy control is present. We need to do
  484. * this before we call op_shared_allow_pw_change() since
  485. * it looks for SLAPI_PWPOLICY in the pblock to determine
  486. * if the response contorl is needed. */
  487. pwpolicy_ctrl = slapi_control_present( controls,
  488. LDAP_X_CONTROL_PWPOLICY_REQUEST, NULL, NULL );
  489. slapi_pblock_set( pb, SLAPI_PWPOLICY, &pwpolicy_ctrl );
  490. if(mods == NULL)
  491. {
  492. opresult = LDAP_PARAM_ERROR;
  493. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  494. return 0;
  495. }
  496. /* first normalize the mods so they are bvalue
  497. * Note: We don't add any special
  498. * attributes such as "creatorsname".
  499. * for CIR we don't want to change them, for other
  500. * plugins the writer should change these if it wants too by explicitly
  501. * adding them to the mods
  502. */
  503. normalized_mods = normalize_mods2bvals((const LDAPMod**)mods);
  504. if (normalized_mods == NULL)
  505. {
  506. opresult = LDAP_PARAM_ERROR;
  507. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  508. return 0;
  509. }
  510. /* check for password change */
  511. mod = normalized_mods;
  512. while (*mod)
  513. {
  514. if ((*mod)->mod_bvalues != NULL && strcasecmp((*mod)->mod_type, SLAPI_USERPWD_ATTR) == 0)
  515. {
  516. slapi_mods_init_passin(&smods, mods);
  517. pw_change = op_shared_allow_pw_change (pb, *mod, &old_pw, &smods);
  518. if (pw_change == -1)
  519. {
  520. /* The internal result code will already have been set by op_shared_allow_pw_change() */
  521. ldap_mods_free(normalized_mods, 1);
  522. return 0;
  523. }
  524. }
  525. mod ++;
  526. }
  527. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  528. op->o_handler_data = &opresult;
  529. op->o_result_handler = internal_getresult_callback;
  530. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, normalized_mods);
  531. slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
  532. /* set parameters common for all internal operations */
  533. set_common_params (pb);
  534. /* set actions taken to process the operation */
  535. set_config_params (pb);
  536. /* perform modify operation */
  537. op_shared_modify (pb, pw_change, old_pw);
  538. /* free the normalized_mods don't forget to add this*/
  539. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &normalized_mods);
  540. if (normalized_mods != NULL)
  541. {
  542. ldap_mods_free(normalized_mods, 1);
  543. }
  544. /* return original mods here */
  545. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  546. /* set result */
  547. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  548. return 0;
  549. }
  550. static void op_shared_modify (Slapi_PBlock *pb, int pw_change, char *old_pw)
  551. {
  552. Slapi_Backend *be = NULL;
  553. Slapi_Entry *pse;
  554. Slapi_Entry *referral;
  555. Slapi_Entry *e = NULL;
  556. char *dn = NULL;
  557. char *normdn = NULL;
  558. Slapi_DN *sdn = NULL;
  559. int passin_sdn = 0;
  560. LDAPMod **mods, *pw_mod, **tmpmods = NULL;
  561. Slapi_Mods smods;
  562. int repl_op, internal_op, lastmod, skip_modified_attrs;
  563. char *unhashed_pw_attr = NULL;
  564. Slapi_Operation *operation;
  565. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  566. int err;
  567. LDAPMod *lc_mod = NULL;
  568. struct slapdplugin *p = NULL;
  569. int numattr, i;
  570. char *proxydn = NULL;
  571. int proxy_err = LDAP_SUCCESS;
  572. char *errtext = NULL;
  573. slapi_pblock_get (pb, SLAPI_ORIGINAL_TARGET, &dn);
  574. slapi_pblock_get (pb, SLAPI_MODIFY_TARGET_SDN, &sdn);
  575. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  576. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &tmpmods);
  577. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  578. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  579. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  580. slapi_pblock_get (pb, SLAPI_SKIP_MODIFIED_ATTRS, &skip_modified_attrs);
  581. if (sdn) {
  582. passin_sdn = 1;
  583. } else {
  584. sdn = slapi_sdn_new_dn_byval(dn);
  585. slapi_pblock_set(pb, SLAPI_MODIFY_TARGET_SDN, (void*)sdn);
  586. }
  587. normdn = (char *)slapi_sdn_get_dn(sdn);
  588. if (dn && (strlen(dn) > 0) && (NULL == normdn)) {
  589. /* normalization failed */
  590. op_shared_log_error_access(pb, "MOD", dn, "invalid dn");
  591. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  592. "invalid dn", 0, NULL);
  593. goto free_and_return;
  594. }
  595. slapi_mods_init_passin (&smods, mods);
  596. /* target spec is used to decide which plugins are applicable for the operation */
  597. operation_set_target_spec (pb->pb_op, sdn);
  598. /* get the proxy auth dn if the proxy auth control is present */
  599. proxy_err = proxyauth_get_dn(pb, &proxydn, &errtext);
  600. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  601. {
  602. char *proxystr = NULL;
  603. if (proxydn)
  604. {
  605. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  606. }
  607. if ( !internal_op )
  608. {
  609. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s\n",
  610. pb->pb_conn->c_connid,
  611. pb->pb_op->o_opid,
  612. slapi_sdn_get_dn(sdn),
  613. proxystr ? proxystr : "");
  614. }
  615. else
  616. {
  617. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s\n",
  618. LOG_INTERNAL_OP_CON_ID,
  619. LOG_INTERNAL_OP_OP_ID,
  620. slapi_sdn_get_dn(sdn),
  621. proxystr ? proxystr : "");
  622. }
  623. slapi_ch_free_string(&proxystr);
  624. }
  625. /* If we encountered an error parsing the proxy control, return an error
  626. * to the client. We do this here to ensure that we log the operation first. */
  627. if (proxy_err != LDAP_SUCCESS)
  628. {
  629. send_ldap_result(pb, proxy_err, NULL, errtext, 0, NULL);
  630. goto free_and_return;
  631. }
  632. /*
  633. * We could be serving multiple database backends. Select the
  634. * appropriate one.
  635. */
  636. errorbuf[0] = '\0';
  637. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf, sizeof(errorbuf))) != LDAP_SUCCESS) {
  638. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  639. be = NULL;
  640. goto free_and_return;
  641. }
  642. if (referral)
  643. {
  644. int managedsait;
  645. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  646. if (managedsait)
  647. {
  648. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  649. "cannot update referral", 0, NULL);
  650. slapi_entry_free(referral);
  651. goto free_and_return;
  652. }
  653. send_referrals_from_entry(pb,referral);
  654. slapi_entry_free(referral);
  655. goto free_and_return;
  656. }
  657. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  658. /* The following section checks the valid values of fine-grained
  659. * password policy attributes.
  660. * 1. First, it checks if the entry has "passwordpolicy" objectclass.
  661. * 2. If yes, then if the mods contain any passwdpolicy specific attributes.
  662. * 3. If yes, then it invokes corrosponding checking function.
  663. */
  664. if ( !repl_op && !internal_op && normdn && (e = get_entry(pb, normdn)) )
  665. {
  666. Slapi_Value target;
  667. slapi_value_init(&target);
  668. slapi_value_set_string(&target,"passwordpolicy");
  669. if ((slapi_entry_attr_has_syntax_value(e, "objectclass", &target)) == 1)
  670. {
  671. numattr = sizeof(AttrValueCheckList)/sizeof(AttrValueCheckList[0]);
  672. while ( tmpmods && *tmpmods )
  673. {
  674. if ((*tmpmods)->mod_bvalues != NULL &&
  675. !SLAPI_IS_MOD_DELETE((*tmpmods)->mod_op))
  676. {
  677. for (i=0; i < numattr; i++)
  678. {
  679. if (slapi_attr_type_cmp((*tmpmods)->mod_type,
  680. AttrValueCheckList[i].attr_name, SLAPI_TYPE_CMP_SUBTYPE) == 0)
  681. {
  682. /* The below function call is good for
  683. * single-valued attrs only
  684. */
  685. if ( (err = AttrValueCheckList[i].checkfunc (AttrValueCheckList[i].attr_name,
  686. (*tmpmods)->mod_bvalues[0]->bv_val, AttrValueCheckList[i].minval,
  687. AttrValueCheckList[i].maxval, errorbuf, sizeof(errorbuf)))
  688. != LDAP_SUCCESS)
  689. {
  690. /* return error */
  691. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  692. goto free_and_return;
  693. }
  694. }
  695. }
  696. }
  697. tmpmods++;
  698. } /* end of (while */
  699. } /* end of if (found */
  700. value_done (&target);
  701. } /* end of if (!repl_op */
  702. /* can get lastmod only after backend is selected */
  703. slapi_pblock_get(pb, SLAPI_BE_LASTMOD, &lastmod);
  704. /* if this is replication session or the operation has been
  705. * flagged - leave mod attributes alone */
  706. if (!repl_op && !skip_modified_attrs && lastmod)
  707. {
  708. modify_update_last_modified_attr(pb, &smods);
  709. }
  710. if (0 == slapi_mods_get_num_mods(&smods)) {
  711. /* nothing to do - no mods - this is not an error - just
  712. send back LDAP_SUCCESS */
  713. send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  714. goto free_and_return;
  715. }
  716. /*
  717. * Add the unhashed password pseudo-attribute (for add) OR
  718. * Delete the unhashed password pseudo-attribute (for delete)
  719. * before calling the preop plugins
  720. */
  721. if (pw_change && !repl_op ) {
  722. Slapi_Value **va = NULL;
  723. unhashed_pw_attr = slapi_attr_syntax_normalize(PSEUDO_ATTR_UNHASHEDUSERPASSWORD);
  724. for ( pw_mod = slapi_mods_get_first_mod(&smods); pw_mod;
  725. pw_mod = slapi_mods_get_next_mod(&smods) )
  726. {
  727. if (strcasecmp (pw_mod->mod_type, SLAPI_USERPWD_ATTR) != 0)
  728. continue;
  729. if (SLAPI_IS_MOD_DELETE(pw_mod->mod_op)) {
  730. Slapi_Attr *a = NULL;
  731. struct pw_scheme *pwsp = NULL;
  732. int remove_unhashed_pw = 1;
  733. char *password = NULL;
  734. char *valpwd = NULL;
  735. /* if there are mod values, we need to delete a specific userpassword */
  736. for ( i = 0; pw_mod->mod_bvalues != NULL && pw_mod->mod_bvalues[i] != NULL; i++ ) {
  737. password = slapi_ch_strdup(pw_mod->mod_bvalues[i]->bv_val);
  738. pwsp = pw_val2scheme( password, &valpwd, 1 );
  739. if(strcmp(pwsp->pws_name, "CLEAR") == 0){
  740. /*
  741. * CLEAR password
  742. *
  743. * Ok, so now we to check the entry's userpassword values.
  744. * First, find out the password encoding of the entry's pw.
  745. * Then compare our clear text password to the encoded userpassword
  746. * using the proper scheme. If we have a match, we know which
  747. * userpassword value to delete.
  748. */
  749. Slapi_Attr *pw = NULL;
  750. struct berval bval, *bv[2];
  751. if(slapi_entry_attr_find(e, SLAPI_USERPWD_ATTR, &pw) == 0 && pw){
  752. struct pw_scheme *pass_scheme = NULL;
  753. Slapi_Value **present_values = NULL;
  754. char *pval = NULL;
  755. int ii;
  756. present_values = attr_get_present_values(pw);
  757. for(ii = 0; present_values && present_values[ii]; ii++){
  758. const char *userpwd = slapi_value_get_string(present_values[ii]);
  759. pass_scheme = pw_val2scheme( (char *)userpwd, &pval, 1 );
  760. if(strcmp(pass_scheme->pws_name,"CLEAR")){
  761. /* its encoded, so compare it */
  762. if((*(pass_scheme->pws_cmp))( valpwd, pval ) == 0 ){
  763. /*
  764. * Match, replace the mod value with the encoded password
  765. */
  766. slapi_ch_free_string(&pw_mod->mod_bvalues[i]->bv_val);
  767. pw_mod->mod_bvalues[i]->bv_val = strdup(userpwd);
  768. pw_mod->mod_bvalues[i]->bv_len = strlen(userpwd);
  769. free_pw_scheme( pass_scheme );
  770. break;
  771. }
  772. } else {
  773. /* userpassword is already clear text, nothing to do */
  774. free_pw_scheme( pass_scheme );
  775. break;
  776. }
  777. free_pw_scheme( pass_scheme );
  778. }
  779. }
  780. /*
  781. * Finally, delete the unhashed userpassword
  782. * (this will update the password entry extension)
  783. */
  784. if (SLAPD_UNHASHED_PW_OFF != config_get_unhashed_pw_switch()) {
  785. bval.bv_val = password;
  786. bval.bv_len = strlen(password);
  787. bv[0] = &bval;
  788. bv[1] = NULL;
  789. valuearray_init_bervalarray(bv, &va);
  790. slapi_mods_add_mod_values(&smods, pw_mod->mod_op, unhashed_pw_attr, va);
  791. valuearray_free(&va);
  792. }
  793. } else {
  794. /*
  795. * Password is encoded, try and find a matching unhashed_password to delete
  796. */
  797. Slapi_Value **vals;
  798. /*
  799. * Grab the current unhashed passwords from the password entry extension,
  800. * as the "attribute" is no longer present in the entry.
  801. */
  802. if(slapi_pw_get_entry_ext(e, &vals) == LDAP_SUCCESS){
  803. int ii;
  804. for(ii = 0; vals && vals[ii]; ii++){
  805. const char *unhashed_pwd = slapi_value_get_string(vals[ii]);
  806. struct pw_scheme *unhashed_pwsp = NULL;
  807. struct berval bval, *bv[2];
  808. /* prepare the value to delete from the list of unhashed userpasswords */
  809. bval.bv_val = (char *)unhashed_pwd;
  810. bval.bv_len = strlen(unhashed_pwd);
  811. bv[0] = &bval;
  812. bv[1] = NULL;
  813. /*
  814. * Compare the clear text unhashed password, to the encoded password
  815. * provided by the client.
  816. */
  817. unhashed_pwsp = pw_val2scheme( (char *)unhashed_pwd, NULL, 1 );
  818. if(strcmp(unhashed_pwsp->pws_name, "CLEAR") == 0){
  819. if((*(pwsp->pws_cmp))((char *)unhashed_pwd , valpwd) == 0 ){
  820. /* match, add the delete mod for this particular unhashed userpassword */
  821. if (SLAPD_UNHASHED_PW_OFF != config_get_unhashed_pw_switch()) {
  822. valuearray_init_bervalarray(bv, &va);
  823. slapi_mods_add_mod_values(&smods, pw_mod->mod_op, unhashed_pw_attr, va);
  824. valuearray_free(&va);
  825. free_pw_scheme( unhashed_pwsp );
  826. }
  827. break;
  828. }
  829. } else {
  830. /*
  831. * We have a hashed unhashed_userpassword! We must delete it.
  832. */
  833. if (SLAPD_UNHASHED_PW_OFF != config_get_unhashed_pw_switch()) {
  834. valuearray_init_bervalarray(bv, &va);
  835. slapi_mods_add_mod_values(&smods, pw_mod->mod_op, unhashed_pw_attr, va);
  836. valuearray_free(&va);
  837. }
  838. }
  839. free_pw_scheme( unhashed_pwsp );
  840. }
  841. } else {
  842. }
  843. }
  844. remove_unhashed_pw = 0; /* mark that we already removed the unhashed userpassword */
  845. slapi_ch_free_string(&password);
  846. free_pw_scheme( pwsp );
  847. }
  848. if (remove_unhashed_pw && !slapi_entry_attr_find(e, unhashed_pw_attr, &a)){
  849. slapi_mods_add_mod_values(&smods, pw_mod->mod_op,unhashed_pw_attr, va);
  850. }
  851. } else if (SLAPD_UNHASHED_PW_OFF != config_get_unhashed_pw_switch()) {
  852. /* add pseudo password attribute */
  853. valuearray_init_bervalarray_unhashed_only(pw_mod->mod_bvalues, &va);
  854. if(va && va[0]){
  855. slapi_mods_add_mod_values(&smods, pw_mod->mod_op, unhashed_pw_attr, va);
  856. }
  857. valuearray_free(&va);
  858. }
  859. /* Init new value array for hashed value */
  860. valuearray_init_bervalarray(pw_mod->mod_bvalues, &va);
  861. /* encode password */
  862. pw_encodevals_ext(pb, sdn, va);
  863. /* remove current clear value of userpassword */
  864. ber_bvecfree(pw_mod->mod_bvalues);
  865. /* add the cipher in the structure */
  866. valuearray_get_bervalarray(va, &pw_mod->mod_bvalues);
  867. valuearray_free(&va);
  868. }
  869. }
  870. for ( p = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME); p != NULL && !repl_op; p = p->plg_next )
  871. {
  872. char *L_attr = NULL;
  873. int i = 0;
  874. /* Get the appropriate encoding function */
  875. for ( L_attr = p->plg_argv[i]; i<p->plg_argc; L_attr = p->plg_argv[++i])
  876. {
  877. char *L_normalized = slapi_attr_syntax_normalize(L_attr);
  878. for ( lc_mod = slapi_mods_get_first_mod(&smods); lc_mod;
  879. lc_mod = slapi_mods_get_next_mod(&smods) )
  880. {
  881. Slapi_Value **va= NULL;
  882. if (strcasecmp (lc_mod->mod_type, L_normalized) != 0)
  883. continue;
  884. switch (lc_mod->mod_op & ~LDAP_MOD_BVALUES)
  885. {
  886. case LDAP_MOD_ADD:
  887. case LDAP_MOD_REPLACE:
  888. /* Init new value array for hashed value */
  889. valuearray_init_bervalarray(lc_mod->mod_bvalues, &va);
  890. if ( va )
  891. {
  892. /* encode local credentials */
  893. pw_rever_encode(va, L_normalized);
  894. /* remove current clear value of userpassword */
  895. ber_bvecfree(lc_mod->mod_bvalues);
  896. /* add the cipher in the structure */
  897. valuearray_get_bervalarray(va, &lc_mod->mod_bvalues);
  898. valuearray_free(&va);
  899. }
  900. break;
  901. default:
  902. /* for LDAP_MOD_DELETE, don't do anything */
  903. /* for LDAP_MOD_BVALUES, don't do anything */
  904. ;
  905. }
  906. }
  907. if (L_normalized)
  908. slapi_ch_free ((void**)&L_normalized);
  909. }
  910. }
  911. /*
  912. * Optimize the mods - this combines sequential identical attribute modifications.
  913. */
  914. optimize_mods(&smods);
  915. /*
  916. * call the pre-mod plugins. if they succeed, call
  917. * the backend mod function. then call the post-mod
  918. * plugins.
  919. */
  920. slapi_pblock_set (pb, SLAPI_MODIFY_MODS, (void*)slapi_mods_get_ldapmods_passout (&smods));
  921. if (plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN :
  922. SLAPI_PLUGIN_PRE_MODIFY_FN) == SLAPI_PLUGIN_SUCCESS)
  923. {
  924. int rc;
  925. /*
  926. * Hash any rootpw attribute values. We hash them after pre-op
  927. * plugins are called in case any pre-op plugin needs the clear value.
  928. * They do need to be hashed here so they wont get audit logged in the
  929. * clear. Note that config_set_rootpw will also do hashing if needed,
  930. * but it will detect that the password is already hashed.
  931. */
  932. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  933. if (hash_rootpw (mods) != 0) {
  934. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  935. "Failed to hash root user's password", 0, NULL);
  936. goto free_and_return;
  937. }
  938. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  939. set_db_default_result_handlers(pb);
  940. if (be->be_modify != NULL)
  941. {
  942. if ((rc = (*be->be_modify)(pb)) == 0)
  943. {
  944. /* acl is not used for internal operations */
  945. /* don't update aci store for remote acis */
  946. if ((!internal_op) &&
  947. (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  948. {
  949. plugin_call_acl_mods_update (pb, SLAPI_OPERATION_MODIFY);
  950. }
  951. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  952. write_audit_log_entry(pb); /* Record the operation in the audit log */
  953. if (pw_change && (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  954. {
  955. /* update the password info */
  956. update_pw_info (pb, old_pw);
  957. }
  958. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  959. do_ps_service(pse, NULL, LDAP_CHANGETYPE_MODIFY, 0);
  960. }
  961. else
  962. {
  963. if (rc == SLAPI_FAIL_DISKFULL)
  964. {
  965. operation_out_of_disk_space();
  966. goto free_and_return;
  967. }
  968. /* If the disk is full we don't want to make it worse ... */
  969. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  970. {
  971. write_auditfail_log_entry(pb); /* Record the operation in the audit log */
  972. }
  973. }
  974. }
  975. else
  976. {
  977. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  978. "Function not implemented", 0, NULL);
  979. }
  980. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  981. plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN :
  982. SLAPI_PLUGIN_POST_MODIFY_FN);
  983. }
  984. free_and_return:
  985. {
  986. Slapi_Entry *epre = NULL, *epost = NULL;
  987. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &epre);
  988. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &epost);
  989. if (epre == e) {
  990. epre = NULL; /* to avoid possible double free below */
  991. }
  992. if (epost == e) {
  993. epost = NULL; /* to avoid possible double free below */
  994. }
  995. if (epre == epost) {
  996. epost = NULL; /* to avoid possible double free below */
  997. }
  998. slapi_pblock_set(pb, SLAPI_ENTRY_PRE_OP, NULL);
  999. slapi_pblock_set(pb, SLAPI_ENTRY_POST_OP, NULL);
  1000. slapi_entry_free(epre);
  1001. slapi_entry_free(epost);
  1002. }
  1003. slapi_entry_free(e);
  1004. if (be)
  1005. slapi_be_Unlock(be);
  1006. if (unhashed_pw_attr)
  1007. slapi_ch_free ((void**)&unhashed_pw_attr);
  1008. slapi_ch_free_string(&proxydn);
  1009. slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &sdn);
  1010. if (!passin_sdn) {
  1011. slapi_sdn_free(&sdn);
  1012. }
  1013. }
  1014. /*
  1015. * Only add password mods that are in clear text. The console likes to send two mods:
  1016. * - Already encoded password
  1017. * - Clear text password
  1018. *
  1019. * We don't want to add the encoded value to the unhashed_userpassword attr
  1020. */
  1021. static int
  1022. valuearray_init_bervalarray_unhashed_only(struct berval **bvals, Slapi_Value ***cvals)
  1023. {
  1024. int n;
  1025. for(n=0; bvals != NULL && bvals[n] != NULL; n++);
  1026. if(n==0){
  1027. *cvals = NULL;
  1028. } else {
  1029. struct pw_scheme *pwsp = NULL;
  1030. int i,p;
  1031. *cvals = (Slapi_Value **) slapi_ch_malloc((n + 1) * sizeof(Slapi_Value *));
  1032. for(i=0,p=0;i<n;i++){
  1033. pwsp = pw_val2scheme( bvals[i]->bv_val, NULL, 1 );
  1034. if(strcmp(pwsp->pws_name, "CLEAR") == 0){
  1035. (*cvals)[p++] = slapi_value_new_berval(bvals[i]);
  1036. }
  1037. free_pw_scheme( pwsp );
  1038. }
  1039. (*cvals)[p] = NULL;
  1040. }
  1041. return n;
  1042. }
  1043. #if 0 /* not used */
  1044. static void remove_mod (Slapi_Mods *smods, const char *type, Slapi_Mods *smod_unhashed)
  1045. {
  1046. LDAPMod *mod;
  1047. Slapi_Mod smod;
  1048. for (mod = slapi_mods_get_first_mod(smods); mod; mod = slapi_mods_get_next_mod(smods))
  1049. {
  1050. if (strcasecmp (mod->mod_type, type) == 0)
  1051. {
  1052. slapi_mod_init_byval (&smod, mod);
  1053. slapi_mods_add_smod(smod_unhashed, &smod);
  1054. slapi_mods_remove (smods);
  1055. }
  1056. }
  1057. }
  1058. #endif
  1059. static int op_shared_allow_pw_change (Slapi_PBlock *pb, LDAPMod *mod, char **old_pw, Slapi_Mods *smods)
  1060. {
  1061. int isroot, internal_op, repl_op, pwresponse_req = 0;
  1062. int res = 0;
  1063. char *dn;
  1064. char *errtxt = NULL;
  1065. Slapi_DN sdn;
  1066. Slapi_Entry *e = NULL;
  1067. passwdPolicy *pwpolicy;
  1068. int rc = 0;
  1069. Slapi_Value **values= NULL;
  1070. Slapi_Operation *operation;
  1071. int proxy_err = LDAP_SUCCESS;
  1072. char *proxydn = NULL;
  1073. char *proxystr = NULL;
  1074. char *errtext = NULL;
  1075. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  1076. if (repl_op) {
  1077. /* Treat like there's no password */
  1078. return (0);
  1079. }
  1080. *old_pw = NULL;
  1081. slapi_pblock_get (pb, SLAPI_ORIGINAL_TARGET, &dn);
  1082. slapi_pblock_get (pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  1083. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  1084. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pwresponse_req);
  1085. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  1086. slapi_sdn_init_dn_byref (&sdn, dn);
  1087. pwpolicy = new_passwdPolicy(pb, (char *)slapi_sdn_get_ndn(&sdn));
  1088. /* get the proxy auth dn if the proxy auth control is present */
  1089. if ((proxy_err = proxyauth_get_dn(pb, &proxydn, &errtext)) != LDAP_SUCCESS)
  1090. {
  1091. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  1092. {
  1093. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"\n",
  1094. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  1095. slapi_sdn_get_dn(&sdn));
  1096. }
  1097. send_ldap_result(pb, proxy_err, NULL, errtext, 0, NULL);
  1098. rc = -1;
  1099. goto done;
  1100. }
  1101. /* internal operation has root permissions for subtrees it is allowed to access */
  1102. if (!internal_op)
  1103. {
  1104. /* slapi_acl_check_mods needs an array of LDAPMods, but
  1105. * we're really only interested in the one password mod. */
  1106. LDAPMod *mods[2];
  1107. mods[0] = mod;
  1108. mods[1] = NULL;
  1109. /* We need to actually fetch the target here to use for ACI checking. */
  1110. slapi_search_internal_get_entry(&sdn, NULL, &e, (void *)plugin_get_default_component_id());
  1111. /* Create a bogus entry with just the target dn if we were unable to
  1112. * find the actual entry. This will only be used for checking the ACIs. */
  1113. if (e == NULL) {
  1114. e = slapi_entry_alloc();
  1115. slapi_entry_init( e, NULL, NULL );
  1116. slapi_sdn_set_dn_byref(slapi_entry_get_sdn(e), dn);
  1117. }
  1118. /* Set the backend in the pblock. The slapi_access_allowed function
  1119. * needs this set to work properly. */
  1120. slapi_pblock_set( pb, SLAPI_BACKEND, slapi_be_select( &sdn ) );
  1121. /* Check if ACIs allow password to be changed */
  1122. if ( !pw_is_pwp_admin(pb, pwpolicy) && (res = slapi_acl_check_mods(pb, e, mods, &errtxt)) != LDAP_SUCCESS){
  1123. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS)){
  1124. if (proxydn){
  1125. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  1126. }
  1127. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s\n",
  1128. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  1129. slapi_sdn_get_dn(&sdn), proxystr ? proxystr : "");
  1130. }
  1131. /* Write access is denied to userPassword by ACIs */
  1132. if ( pwresponse_req == 1 ) {
  1133. slapi_pwpolicy_make_response_control ( pb, -1, -1, LDAP_PWPOLICY_PWDMODNOTALLOWED );
  1134. }
  1135. send_ldap_result(pb, res, NULL, errtxt, 0, NULL);
  1136. slapi_ch_free_string(&errtxt);
  1137. rc = -1;
  1138. goto done;
  1139. }
  1140. /*
  1141. * If this mod is being performed by a password administrator/rootDN,
  1142. * just return success.
  1143. */
  1144. if(pw_is_pwp_admin(pb, pwpolicy)){
  1145. if (!SLAPI_IS_MOD_DELETE(mod->mod_op) && pwpolicy->pw_history){
  1146. /* Updating pw history, get the old password */
  1147. get_old_pw(pb, &sdn, old_pw);
  1148. }
  1149. rc = 1;
  1150. goto done;
  1151. }
  1152. /* Check if password policy allows users to change their passwords.*/
  1153. if (!pb->pb_op->o_isroot && slapi_sdn_compare(&sdn, &pb->pb_op->o_sdn)==0 &&
  1154. !pb->pb_conn->c_needpw && !pwpolicy->pw_change)
  1155. {
  1156. if ( pwresponse_req == 1 ) {
  1157. slapi_pwpolicy_make_response_control ( pb, -1, -1, LDAP_PWPOLICY_PWDMODNOTALLOWED );
  1158. }
  1159. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  1160. "user is not allowed to change password", 0, NULL);
  1161. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  1162. {
  1163. if (proxydn)
  1164. {
  1165. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  1166. }
  1167. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  1168. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  1169. slapi_sdn_get_dn(&sdn),
  1170. proxystr ? proxystr : "",
  1171. "user is not allowed to change password");
  1172. }
  1173. rc = -1;
  1174. goto done;
  1175. }
  1176. }
  1177. /* check if password is within password minimum age;
  1178. error result is sent directly from check_pw_minage */
  1179. if ((internal_op || !pb->pb_conn->c_needpw) &&
  1180. check_pw_minage(pb, &sdn, mod->mod_bvalues) == 1)
  1181. {
  1182. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  1183. {
  1184. if (proxydn)
  1185. {
  1186. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  1187. }
  1188. if ( !internal_op )
  1189. {
  1190. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  1191. pb->pb_conn->c_connid,
  1192. pb->pb_op->o_opid,
  1193. slapi_sdn_get_dn(&sdn),
  1194. proxystr ? proxystr : "",
  1195. "within password minimum age");
  1196. }
  1197. else
  1198. {
  1199. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s, %s\n",
  1200. LOG_INTERNAL_OP_CON_ID,
  1201. LOG_INTERNAL_OP_OP_ID,
  1202. slapi_sdn_get_dn(&sdn),
  1203. proxystr ? proxystr : "",
  1204. "within password minimum age");
  1205. }
  1206. }
  1207. rc = -1;
  1208. goto done;
  1209. }
  1210. /* check password syntax; remember the old password;
  1211. error sent directly from check_pw_syntax function */
  1212. valuearray_init_bervalarray(mod->mod_bvalues, &values);
  1213. switch (check_pw_syntax_ext (pb, &sdn, values, old_pw, NULL,
  1214. mod->mod_op, smods))
  1215. {
  1216. case 0: /* success */
  1217. rc = 1;
  1218. break;
  1219. case 1: /* failed checking */
  1220. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  1221. {
  1222. if (proxydn)
  1223. {
  1224. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  1225. }
  1226. if ( !internal_op )
  1227. {
  1228. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  1229. pb->pb_conn->c_connid,
  1230. pb->pb_op->o_opid,
  1231. slapi_sdn_get_dn(&sdn),
  1232. proxystr ? proxystr : "",
  1233. "invalid password syntax");
  1234. }
  1235. else
  1236. {
  1237. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s, %s\n",
  1238. LOG_INTERNAL_OP_CON_ID,
  1239. LOG_INTERNAL_OP_OP_ID,
  1240. slapi_sdn_get_dn(&sdn),
  1241. proxystr ? proxystr : "",
  1242. "invalid password syntax");
  1243. }
  1244. }
  1245. rc = -1;
  1246. break;
  1247. case -1: /* The entry is not found. No password checking is done. Countinue execution
  1248. and it should get caught later and send "no such object back. */
  1249. rc = 0;
  1250. break;
  1251. default: break;
  1252. }
  1253. valuearray_free(&values);
  1254. done:
  1255. slapi_entry_free( e );
  1256. slapi_sdn_done (&sdn);
  1257. slapi_ch_free_string(&proxydn);
  1258. slapi_ch_free_string(&proxystr);
  1259. return rc;
  1260. }
  1261. /*
  1262. * Hashes any nsslapd-rootpw attribute values using the password storage
  1263. * scheme specified in cn=config:nsslapd-rootpwstoragescheme.
  1264. * Note: This is only done for modify, because rootdn's password lives
  1265. * in cn=config, which is never added.
  1266. */
  1267. static int
  1268. hash_rootpw (LDAPMod **mods)
  1269. {
  1270. int i, j;
  1271. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  1272. if (strcasecmp(slapdFrontendConfig->rootpwstoragescheme->pws_name, "clear") == 0) {
  1273. /* No work to do if the rootpw storage scheme is clear */
  1274. return 0;
  1275. }
  1276. for (i=0; (mods != NULL) && (mods[i] != NULL); i++) {
  1277. LDAPMod *mod = mods[i];
  1278. if (strcasecmp (mod->mod_type, CONFIG_ROOTPW_ATTRIBUTE) != 0)
  1279. continue;
  1280. if (mod->mod_bvalues != NULL) {
  1281. for (j = 0; mod->mod_bvalues[j] != NULL; j++) {
  1282. char *val = mod->mod_bvalues[j]->bv_val;
  1283. char *hashedval = NULL;
  1284. struct pw_scheme *pws = pw_val2scheme (val, NULL, 0);
  1285. if (pws) {
  1286. free_pw_scheme(pws);
  1287. /* Value is pre-hashed, no work to do for this value */
  1288. continue;
  1289. } else if (! slapd_nss_is_initialized() ) {
  1290. /* We need to hash a value but NSS is not initialized; bail */
  1291. return -1;
  1292. }
  1293. hashedval=(slapdFrontendConfig->rootpwstoragescheme->pws_enc)(val);
  1294. slapi_ch_free_string (&val);
  1295. mod->mod_bvalues[j]->bv_val = hashedval;
  1296. mod->mod_bvalues[j]->bv_len = strlen (hashedval);
  1297. }
  1298. }
  1299. }
  1300. return 0;
  1301. }
  1302. /*
  1303. * optimize_mods()
  1304. *
  1305. * If the client send a string identical modifications we might
  1306. * be able to optimize it for add and delete operations:
  1307. *
  1308. * mods[0].mod_op: LDAP_MOD_ADD
  1309. * mods[0].mod_type: uniqueMember
  1310. * mods[0].mod_values: <value_0>
  1311. * mods[1].mod_op: LDAP_MOD_ADD
  1312. * mods[1].mod_type: uniqueMember
  1313. * mods[1].mod_values: <value_1>
  1314. * ...
  1315. * mods[N].mod_op: LDAP_MOD_ADD
  1316. * mods[N].mod_type: uniqueMember
  1317. * mods[N]mod_values: <value_N>
  1318. *
  1319. * Optimized to:
  1320. *
  1321. * mods[0].mod_op: LDAP_MOD_ADD
  1322. * mods[0].mod_type: uniqueMember
  1323. * mods[0].mod_values: <value_0>
  1324. * <value_1>
  1325. * ...
  1326. * <value_N>
  1327. *
  1328. * We only optimize operations (ADDs and DELETEs) that are sequential. We
  1329. * can not look at the all mods(non-sequentially) because we need to keep
  1330. * the order preserved, and keep processing to a minimum.
  1331. */
  1332. static void
  1333. optimize_mods(Slapi_Mods *smods){
  1334. LDAPMod *mod, *prev_mod;
  1335. int i, mod_count = 0, max_vals = 0;
  1336. prev_mod = slapi_mods_get_first_mod(smods);
  1337. while((mod = slapi_mods_get_next_mod(smods))){
  1338. if((SLAPI_IS_MOD_ADD(prev_mod->mod_op) || SLAPI_IS_MOD_DELETE(prev_mod->mod_op)) &&
  1339. (prev_mod->mod_op == mod->mod_op) &&
  1340. (!strcasecmp(prev_mod->mod_type, mod->mod_type)))
  1341. {
  1342. /* Get the current number of mod values from the previous mod. Do it once per attr */
  1343. if(mod_count == 0){
  1344. for(;prev_mod->mod_bvalues != NULL && prev_mod->mod_bvalues[mod_count] != NULL; mod_count++);
  1345. if(mod_count == 0){
  1346. /* The previous mod did not contain any values, so lets move to the next mod */
  1347. prev_mod = mod;
  1348. continue;
  1349. }
  1350. }
  1351. /* Add the values from the current mod to the prev mod */
  1352. for ( i = 0; mod->mod_bvalues != NULL && mod->mod_bvalues[i] != NULL; i++ ) {
  1353. bervalarray_add_berval_fast(&(prev_mod->mod_bvalues),mod->mod_bvalues[i],mod_count, &max_vals);
  1354. mod_count++;
  1355. }
  1356. if(i > 0){
  1357. /* Ok, we did optimize the "mod" values, so set the current mod to be ignored */
  1358. mod->mod_op = LDAP_MOD_IGNORE;
  1359. } else {
  1360. /* No mod values, probably a full delete of the attribute... reset counters and move on */
  1361. mod_count = max_vals = 0;
  1362. prev_mod = mod;
  1363. }
  1364. } else {
  1365. /* no match, reset counters and move on */
  1366. mod_count = max_vals = 0;
  1367. prev_mod = mod;
  1368. }
  1369. }
  1370. }