modify.c 49 KB

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