modify.c 47 KB

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