1
0

modify.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2009 Red Hat, Inc.
  36. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  37. * All rights reserved.
  38. *
  39. * Contributors:
  40. * Hewlett-Packard Development Company, L.P.
  41. * Bugfix for bug #195302
  42. *
  43. * END COPYRIGHT BLOCK **/
  44. #ifdef HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. /*
  48. * Copyright (c) 1995 Regents of the University of Michigan.
  49. * All rights reserved.
  50. *
  51. * Redistribution and use in source and binary forms are permitted
  52. * provided that this notice is preserved and that due credit is given
  53. * to the University of Michigan at Ann Arbor. The name of the University
  54. * may not be used to endorse or promote products derived from this
  55. * software without specific prior written permission. This software
  56. * is provided ``as is'' without express or implied warranty.
  57. */
  58. #include <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. static void remove_mod (Slapi_Mods *smods, const char *type, Slapi_Mods *smod_unhashed);
  75. static int op_shared_allow_pw_change (Slapi_PBlock *pb, LDAPMod *mod, char **old_pw, Slapi_Mods *smods);
  76. #ifdef LDAP_DEBUG
  77. static const char*
  78. mod_op_image (int op)
  79. {
  80. switch (op & ~LDAP_MOD_BVALUES) {
  81. case LDAP_MOD_ADD: return "add";
  82. case LDAP_MOD_DELETE: return "delete";
  83. case LDAP_MOD_REPLACE: return "replace";
  84. default: break;
  85. }
  86. return "???";
  87. }
  88. #endif
  89. /* an AttrCheckFunc function should return an LDAP result code (LDAP_SUCCESS if all goes well). */
  90. typedef int (*AttrCheckFunc)(const char *attr_name, char *value, long minval, long maxval, char *errorbuf);
  91. static struct attr_value_check {
  92. const char *attr_name; /* the name of the attribute */
  93. AttrCheckFunc checkfunc;
  94. long minval;
  95. long maxval;
  96. } AttrValueCheckList[] = {
  97. {CONFIG_PW_SYNTAX_ATTRIBUTE, attr_check_onoff, 0, 0},
  98. {CONFIG_PW_CHANGE_ATTRIBUTE, attr_check_onoff, 0, 0},
  99. {CONFIG_PW_LOCKOUT_ATTRIBUTE, attr_check_onoff, 0, 0},
  100. {CONFIG_PW_MUSTCHANGE_ATTRIBUTE, attr_check_onoff, 0, 0},
  101. {CONFIG_PW_EXP_ATTRIBUTE, attr_check_onoff, 0, 0},
  102. {CONFIG_PW_UNLOCK_ATTRIBUTE, attr_check_onoff, 0, 0},
  103. {CONFIG_PW_HISTORY_ATTRIBUTE, attr_check_onoff, 0, 0},
  104. {CONFIG_PW_MINAGE_ATTRIBUTE, check_pw_minage_value, -1, -1},
  105. {CONFIG_PW_WARNING_ATTRIBUTE, attr_check_minmax, 0, -1},
  106. {CONFIG_PW_MINLENGTH_ATTRIBUTE, attr_check_minmax, 2, 512},
  107. {CONFIG_PW_MAXFAILURE_ATTRIBUTE, attr_check_minmax, 1, 32767},
  108. {CONFIG_PW_INHISTORY_ATTRIBUTE, attr_check_minmax, 2, 24},
  109. {CONFIG_PW_LOCKDURATION_ATTRIBUTE, check_pw_lockduration_value, -1, -1},
  110. {CONFIG_PW_RESETFAILURECOUNT_ATTRIBUTE, check_pw_resetfailurecount_value, -1, -1},
  111. {CONFIG_PW_GRACELIMIT_ATTRIBUTE, attr_check_minmax, 0, -1},
  112. {CONFIG_PW_STORAGESCHEME_ATTRIBUTE, check_pw_storagescheme_value, -1, -1}
  113. };
  114. /* This function is called to process operation that come over external connections */
  115. void
  116. do_modify( Slapi_PBlock *pb )
  117. {
  118. Slapi_Operation *operation;
  119. BerElement *ber;
  120. char *last, *type = NULL;
  121. ber_tag_t tag;
  122. ber_len_t len;
  123. LDAPMod *mod;
  124. LDAPMod **mods;
  125. Slapi_Mods smods;
  126. int err;
  127. int pw_change = 0; /* 0= no password change */
  128. int ignored_some_mods = 0;
  129. int has_password_mod = 0; /* number of password mods */
  130. char *old_pw = NULL; /* remember the old password */
  131. char *dn = NULL;
  132. LDAPDebug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
  133. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  134. ber = operation->o_ber;
  135. /* count the modify request */
  136. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps);
  137. /*
  138. * Parse the modify request. It looks like this:
  139. *
  140. * ModifyRequest := [APPLICATION 6] SEQUENCE {
  141. * name DistinguishedName,
  142. * mods SEQUENCE OF SEQUENCE {
  143. * operation ENUMERATED {
  144. * add (0),
  145. * delete (1),
  146. * replace (2)
  147. * },
  148. * modification SEQUENCE {
  149. * type AttributeType,
  150. * values SET OF AttributeValue
  151. * }
  152. * }
  153. * }
  154. */
  155. {
  156. char *rawdn = NULL;
  157. size_t dnlen = 0;
  158. int rc = 0;
  159. if ( ber_scanf( ber, "{a", &rawdn ) == LBER_ERROR )
  160. {
  161. LDAPDebug( LDAP_DEBUG_ANY,
  162. "ber_scanf failed (op=Modify; params=DN)\n", 0, 0, 0 );
  163. op_shared_log_error_access (pb, "MOD", "???", "decoding error");
  164. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0, NULL );
  165. slapi_ch_free_string(&rawdn);
  166. return;
  167. }
  168. /* Check if we should be performing strict validation. */
  169. if (config_get_dn_validate_strict()) {
  170. /* check that the dn is formatted correctly */
  171. rc = slapi_dn_syntax_check(pb, rawdn, 1);
  172. if (rc) { /* syntax check failed */
  173. op_shared_log_error_access(pb, "MOD", rawdn?rawdn:"",
  174. "strict: invalid dn");
  175. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  176. NULL, "invalid dn", 0, NULL);
  177. slapi_ch_free((void **) &rawdn);
  178. return;
  179. }
  180. }
  181. rc = slapi_dn_normalize_ext(rawdn, 0, &dn, &dnlen);
  182. if (rc < 0) {
  183. op_shared_log_error_access(pb, "MOD", rawdn?rawdn:"", "invalid dn");
  184. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  185. NULL, "invalid dn", 0, NULL);
  186. slapi_ch_free((void **) &rawdn);
  187. return;
  188. } else if (rc > 0) { /* if rc == 0, rawdn is passed in */
  189. slapi_ch_free_string(&rawdn);
  190. } else { /* rc == 0; rawdn is passed in; not null terminated */
  191. *(dn + dnlen) = '\0';
  192. }
  193. }
  194. LDAPDebug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
  195. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &pb->pb_op->o_isroot);
  196. slapi_pblock_set( pb, SLAPI_ORIGINAL_TARGET, dn );
  197. /* collect modifications & save for later */
  198. slapi_mods_init(&smods, 0);
  199. len = -1;
  200. for ( tag = ber_first_element( ber, &len, &last );
  201. tag != LBER_ERROR && tag != LBER_END_OF_SEQORSET;
  202. tag = ber_next_element( ber, &len, last ) )
  203. {
  204. ber_int_t mod_op;
  205. mod = (LDAPMod *) slapi_ch_malloc( sizeof(LDAPMod) );
  206. mod->mod_bvalues = NULL;
  207. len = -1; /* reset - len is not used */
  208. if ( ber_scanf( ber, "{i{a[V]}}", &mod_op, &type,
  209. &mod->mod_bvalues ) == LBER_ERROR )
  210. {
  211. op_shared_log_error_access (pb, "MOD", dn, "decoding error");
  212. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  213. "decoding error", 0, NULL );
  214. ber_bvecfree(mod->mod_bvalues);
  215. slapi_ch_free((void **)&mod);
  216. slapi_ch_free_string(&type);
  217. goto free_and_return;
  218. }
  219. mod->mod_op = mod_op;
  220. mod->mod_type = slapi_attr_syntax_normalize(type);
  221. if ( !mod->mod_type || !*mod->mod_type ) {
  222. char ebuf[BUFSIZ];
  223. PR_snprintf (ebuf, BUFSIZ, "invalid type '%s'", type);
  224. op_shared_log_error_access (pb, "MOD", dn, ebuf);
  225. send_ldap_result( pb, LDAP_INVALID_SYNTAX, NULL, ebuf, 0, NULL );
  226. slapi_ch_free((void **)&type);
  227. ber_bvecfree(mod->mod_bvalues);
  228. slapi_ch_free_string(&mod->mod_type);
  229. slapi_ch_free((void **)&mod);
  230. goto free_and_return;
  231. }
  232. slapi_ch_free((void **)&type);
  233. if ( mod->mod_op != LDAP_MOD_ADD &&
  234. mod->mod_op != LDAP_MOD_DELETE &&
  235. mod->mod_op != LDAP_MOD_REPLACE )
  236. {
  237. op_shared_log_error_access (pb, "MOD", dn, "unrecognized modify operation");
  238. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  239. "unrecognized modify operation", 0, NULL );
  240. ber_bvecfree(mod->mod_bvalues);
  241. slapi_ch_free((void **)&(mod->mod_type));
  242. slapi_ch_free((void **)&mod);
  243. goto free_and_return;
  244. }
  245. if ( mod->mod_bvalues == NULL
  246. && mod->mod_op != LDAP_MOD_DELETE
  247. && mod->mod_op != LDAP_MOD_REPLACE )
  248. {
  249. op_shared_log_error_access (pb, "MOD", dn, "no values given");
  250. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  251. "no values given", 0, NULL );
  252. ber_bvecfree(mod->mod_bvalues);
  253. slapi_ch_free((void **)&(mod->mod_type));
  254. slapi_ch_free((void **)&mod);
  255. goto free_and_return;
  256. }
  257. /* check if user is allowed to modify the specified attribute */
  258. if (!op_shared_is_allowed_attr (mod->mod_type, pb->pb_conn->c_isreplication_session))
  259. {
  260. /* for now we just ignore attributes that client is not allowed
  261. to modify so not to break existing clients */
  262. ++ignored_some_mods;
  263. ber_bvecfree(mod->mod_bvalues);
  264. slapi_ch_free((void **)&(mod->mod_type));
  265. slapi_ch_free((void **)&mod);
  266. continue;
  267. /* send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL, NULL, 0, NULL );
  268. goto free_and_return; */
  269. }
  270. /* check for password change */
  271. if ( mod->mod_bvalues != NULL &&
  272. strcasecmp( mod->mod_type, SLAPI_USERPWD_ATTR ) == 0 ){
  273. has_password_mod++;
  274. }
  275. mod->mod_op |= LDAP_MOD_BVALUES;
  276. slapi_mods_add_ldapmod (&smods, mod);
  277. }
  278. /* check for decoding error */
  279. if ( (tag != LBER_END_OF_SEQORSET) && (len != -1) )
  280. {
  281. op_shared_log_error_access (pb, "MOD", dn, "decoding error");
  282. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, "decoding error", 0, NULL );
  283. goto free_and_return;
  284. }
  285. /* decode the optional controls - put them in the pblock */
  286. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 )
  287. {
  288. op_shared_log_error_access (pb, "MOD", dn, "failed to decode LDAP controls");
  289. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  290. goto free_and_return;
  291. }
  292. /* if there are any password mods, see if they are allowed */
  293. if (has_password_mod) {
  294. /* iterate through the mods looking for password mods */
  295. for (mod = slapi_mods_get_first_mod(&smods);
  296. mod;
  297. mod = slapi_mods_get_next_mod(&smods)) {
  298. if ( mod->mod_bvalues != NULL &&
  299. strcasecmp( mod->mod_type, SLAPI_USERPWD_ATTR ) == 0 ) {
  300. /* assumes controls have already been decoded and placed
  301. in the pblock */
  302. pw_change = op_shared_allow_pw_change (pb, mod, &old_pw, &smods);
  303. if (pw_change == -1) {
  304. goto free_and_return;
  305. }
  306. }
  307. }
  308. }
  309. if (!pb->pb_conn->c_isreplication_session &&
  310. pb->pb_conn->c_needpw && pw_change == 0 )
  311. {
  312. (void)slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
  313. op_shared_log_error_access (pb, "MOD", dn, "need new password");
  314. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL, NULL, 0, NULL );
  315. goto free_and_return;
  316. }
  317. /* see if there were actually any mods to perform */
  318. if ( slapi_mods_get_num_mods (&smods) == 0 )
  319. {
  320. int lderr;
  321. char *emsg;
  322. if ( ignored_some_mods ) {
  323. lderr = LDAP_UNWILLING_TO_PERFORM;
  324. emsg = "no modifiable attributes specified";
  325. } else {
  326. lderr = LDAP_PROTOCOL_ERROR;
  327. emsg = "no modifications specified";
  328. }
  329. op_shared_log_error_access (pb, "MOD", dn, emsg);
  330. send_ldap_result( pb, lderr, NULL, emsg, 0, NULL );
  331. goto free_and_return;
  332. }
  333. #ifdef LDAP_DEBUG
  334. LDAPDebug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
  335. for (mod = slapi_mods_get_first_mod(&smods); mod != NULL;
  336. mod = slapi_mods_get_next_mod(&smods))
  337. {
  338. LDAPDebug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
  339. mod_op_image( mod->mod_op ), mod->mod_type, 0 );
  340. }
  341. #endif
  342. mods = slapi_mods_get_ldapmods_passout (&smods);
  343. slapi_pblock_set( pb, SLAPI_MODIFY_MODS, mods);
  344. op_shared_modify ( pb, pw_change, old_pw );
  345. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  346. ldap_mods_free (mods, 1 /* Free the Array and the Elements */);
  347. free_and_return:;
  348. slapi_ch_free ((void**)&dn);
  349. slapi_mods_done(&smods);
  350. }
  351. /* This function is used to issue internal modify operation
  352. This is an old style API. Its use is discoraged because it is not extendable and
  353. because it does not allow to check whether plugin has right to access part of the
  354. tree it is trying to modify. Use slapi_modify_internal_pb instead */
  355. Slapi_PBlock*
  356. slapi_modify_internal(const char *idn,
  357. LDAPMod **mods,
  358. LDAPControl **controls,
  359. int dummy)
  360. {
  361. Slapi_PBlock pb;
  362. Slapi_PBlock *result_pb = NULL;
  363. int opresult;
  364. pblock_init(&pb);
  365. slapi_modify_internal_set_pb (&pb, idn, (LDAPMod**)mods, controls, NULL,
  366. (void *)plugin_get_default_component_id(), 0);
  367. modify_internal_pb (&pb);
  368. result_pb = slapi_pblock_new();
  369. if (result_pb)
  370. {
  371. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  372. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  373. }
  374. pblock_done(&pb);
  375. return result_pb;
  376. }
  377. /* This is new style API to issue internal modify operation.
  378. pblock should contain the following data (can be set via call to slapi_modify_internal_set_pb):
  379. For uniqueid based operation:
  380. SLAPI_TARGET_DN set to dn that allows to select right backend, can be stale
  381. SLAPI_TARGET_UNIQUEID set to the uniqueid of the entry we are looking for
  382. SLAPI_MODIFY_MODS set to the mods
  383. SLAPI_CONTROLS_ARG set to request controls if present
  384. For dn based search:
  385. SLAPI_TARGET_DN set to the entry dn
  386. SLAPI_MODIFY_MODS set to the mods
  387. SLAPI_CONTROLS_ARG set to request controls if present
  388. */
  389. int slapi_modify_internal_pb (Slapi_PBlock *pb)
  390. {
  391. if (pb == NULL)
  392. return -1;
  393. if (!allow_operation (pb))
  394. {
  395. slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  396. "This plugin is not configured to access operation target data", 0, NULL );
  397. return 0;
  398. }
  399. return modify_internal_pb (pb);
  400. }
  401. /* Initialize a pblock for a call to slapi_modify_internal_pb() */
  402. void slapi_modify_internal_set_pb (Slapi_PBlock *pb, const char *dn, LDAPMod **mods, LDAPControl **controls,
  403. const char *uniqueid, Slapi_ComponentId *plugin_identity, int operation_flags)
  404. {
  405. Operation *op;
  406. PR_ASSERT (pb != NULL);
  407. if (pb == NULL || dn == NULL || mods == NULL)
  408. {
  409. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  410. "slapi_modify_internal_set_pb: NULL parameter\n");
  411. return;
  412. }
  413. op= internal_operation_new(SLAPI_OPERATION_MODIFY,operation_flags);
  414. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  415. slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET, (void*)dn);
  416. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  417. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  418. if (uniqueid)
  419. {
  420. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void*)uniqueid);
  421. }
  422. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  423. }
  424. /* Helper functions */
  425. static int modify_internal_pb (Slapi_PBlock *pb)
  426. {
  427. LDAPControl **controls;
  428. int pwpolicy_ctrl = 0;
  429. Operation *op;
  430. int opresult = 0;
  431. LDAPMod **normalized_mods = NULL;
  432. LDAPMod **mods;
  433. LDAPMod **mod;
  434. Slapi_Mods smods;
  435. int pw_change = 0;
  436. char *old_pw = NULL;
  437. PR_ASSERT (pb != NULL);
  438. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  439. slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
  440. /* See if pwpolicy control is present. We need to do
  441. * this before we call op_shared_allow_pw_change() since
  442. * it looks for SLAPI_PWPOLICY in the pblock to determine
  443. * if the response contorl is needed. */
  444. pwpolicy_ctrl = slapi_control_present( controls,
  445. LDAP_X_CONTROL_PWPOLICY_REQUEST, NULL, NULL );
  446. slapi_pblock_set( pb, SLAPI_PWPOLICY, &pwpolicy_ctrl );
  447. if(mods == NULL)
  448. {
  449. opresult = LDAP_PARAM_ERROR;
  450. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  451. return 0;
  452. }
  453. /* first normalize the mods so they are bvalue
  454. * Note: We don't add any special
  455. * attributes such as "creatorsname".
  456. * for CIR we don't want to change them, for other
  457. * plugins the writer should change these if it wants too by explicitly
  458. * adding them to the mods
  459. */
  460. normalized_mods = normalize_mods2bvals((const LDAPMod**)mods);
  461. if (normalized_mods == NULL)
  462. {
  463. opresult = LDAP_PARAM_ERROR;
  464. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  465. return 0;
  466. }
  467. /* check for password change */
  468. mod = normalized_mods;
  469. while (*mod)
  470. {
  471. if ((*mod)->mod_bvalues != NULL && strcasecmp((*mod)->mod_type, SLAPI_USERPWD_ATTR) == 0)
  472. {
  473. slapi_mods_init_passin(&smods, mods);
  474. pw_change = op_shared_allow_pw_change (pb, *mod, &old_pw, &smods);
  475. if (pw_change == -1)
  476. {
  477. /* The internal result code will already have been set by op_shared_allow_pw_change() */
  478. ldap_mods_free(normalized_mods, 1);
  479. return 0;
  480. }
  481. }
  482. mod ++;
  483. }
  484. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  485. op->o_handler_data = &opresult;
  486. op->o_result_handler = internal_getresult_callback;
  487. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, normalized_mods);
  488. slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
  489. /* set parameters common for all internal operations */
  490. set_common_params (pb);
  491. /* set actions taken to process the operation */
  492. set_config_params (pb);
  493. /* perform modify operation */
  494. op_shared_modify (pb, pw_change, old_pw);
  495. /* free the normalized_mods don't forget to add this*/
  496. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &normalized_mods);
  497. if (normalized_mods != NULL)
  498. {
  499. ldap_mods_free(normalized_mods, 1);
  500. }
  501. /* return original mods here */
  502. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  503. /* set result */
  504. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  505. return 0;
  506. }
  507. static void op_shared_modify (Slapi_PBlock *pb, int pw_change, char *old_pw)
  508. {
  509. Slapi_Backend *be = NULL;
  510. Slapi_Entry *pse;
  511. Slapi_Entry *referral;
  512. Slapi_Entry *e = NULL;
  513. char ebuf[BUFSIZ];
  514. char *dn;
  515. Slapi_DN sdn;
  516. LDAPMod **mods, *pw_mod, **tmpmods = NULL;
  517. Slapi_Mods smods;
  518. Slapi_Mods unhashed_pw_smod;
  519. int repl_op, internal_op, lastmod, skip_modified_attrs;
  520. char *unhashed_pw_attr = NULL;
  521. Slapi_Operation *operation;
  522. char errorbuf[BUFSIZ];
  523. int err;
  524. LDAPMod *lc_mod = NULL;
  525. struct slapdplugin *p = NULL;
  526. int numattr, i;
  527. char *proxydn = NULL;
  528. int proxy_err = LDAP_SUCCESS;
  529. char *errtext = NULL;
  530. slapi_pblock_get (pb, SLAPI_ORIGINAL_TARGET, &dn);
  531. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  532. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &tmpmods);
  533. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  534. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  535. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  536. slapi_pblock_get (pb, SLAPI_SKIP_MODIFIED_ATTRS, &skip_modified_attrs);
  537. if (dn == NULL)
  538. {
  539. slapi_sdn_init_dn_byref (&sdn, "");
  540. }
  541. else
  542. {
  543. slapi_sdn_init_dn_byref (&sdn, dn);
  544. }
  545. slapi_pblock_set(pb, SLAPI_MODIFY_TARGET, (void*)slapi_sdn_get_ndn (&sdn));
  546. slapi_mods_init_passin (&smods, mods);
  547. slapi_mods_init(&unhashed_pw_smod, 0);
  548. /* target spec is used to decide which plugins are applicable for the operation */
  549. operation_set_target_spec (pb->pb_op, &sdn);
  550. /* get the proxy auth dn if the proxy auth control is present */
  551. proxy_err = proxyauth_get_dn(pb, &proxydn, &errtext);
  552. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  553. {
  554. char *proxystr = NULL;
  555. if (proxydn)
  556. {
  557. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  558. }
  559. if ( !internal_op )
  560. {
  561. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s\n",
  562. pb->pb_conn->c_connid,
  563. pb->pb_op->o_opid,
  564. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  565. proxystr ? proxystr : "");
  566. }
  567. else
  568. {
  569. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s\n",
  570. LOG_INTERNAL_OP_CON_ID,
  571. LOG_INTERNAL_OP_OP_ID,
  572. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  573. proxystr ? proxystr : "");
  574. }
  575. slapi_ch_free_string(&proxystr);
  576. }
  577. /* If we encountered an error parsing the proxy control, return an error
  578. * to the client. We do this here to ensure that we log the operation first. */
  579. if (proxy_err != LDAP_SUCCESS)
  580. {
  581. send_ldap_result(pb, proxy_err, NULL, errtext, 0, NULL);
  582. goto free_and_return;
  583. }
  584. /*
  585. * We could be serving multiple database backends. Select the
  586. * appropriate one.
  587. */
  588. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  589. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  590. be = NULL;
  591. goto free_and_return;
  592. }
  593. if (referral)
  594. {
  595. int managedsait;
  596. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  597. if (managedsait)
  598. {
  599. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  600. "cannot update referral", 0, NULL);
  601. slapi_entry_free(referral);
  602. goto free_and_return;
  603. }
  604. send_referrals_from_entry(pb,referral);
  605. slapi_entry_free(referral);
  606. goto free_and_return;
  607. }
  608. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  609. /* The following section checks the valid values of fine-grained
  610. * password policy attributes.
  611. * 1. First, it checks if the entry has "passwordpolicy" objectclass.
  612. * 2. If yes, then if the mods contain any passwdpolicy specific attributes.
  613. * 3. If yes, then it invokes corrosponding checking function.
  614. */
  615. if ( !repl_op && !internal_op && dn &&
  616. (e = get_entry(pb, slapi_dn_normalize(dn))) )
  617. {
  618. Slapi_Value target;
  619. slapi_value_init(&target);
  620. slapi_value_set_string(&target,"passwordpolicy");
  621. if ((slapi_entry_attr_has_syntax_value(e, "objectclass", &target)) == 1)
  622. {
  623. numattr = sizeof(AttrValueCheckList)/sizeof(AttrValueCheckList[0]);
  624. while ( tmpmods && *tmpmods )
  625. {
  626. if ((*tmpmods)->mod_bvalues != NULL &&
  627. !SLAPI_IS_MOD_DELETE((*tmpmods)->mod_op))
  628. {
  629. for (i=0; i < numattr; i++)
  630. {
  631. if (slapi_attr_type_cmp((*tmpmods)->mod_type,
  632. AttrValueCheckList[i].attr_name, SLAPI_TYPE_CMP_SUBTYPE) == 0)
  633. {
  634. /* The below function call is good for
  635. * single-valued attrs only
  636. */
  637. if ( (err = AttrValueCheckList[i].checkfunc (AttrValueCheckList[i].attr_name,
  638. (*tmpmods)->mod_bvalues[0]->bv_val, AttrValueCheckList[i].minval,
  639. AttrValueCheckList[i].maxval, errorbuf))
  640. != LDAP_SUCCESS)
  641. {
  642. /* return error */
  643. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  644. goto free_and_return;
  645. }
  646. }
  647. }
  648. }
  649. tmpmods++;
  650. } /* end of (while */
  651. } /* end of if (found */
  652. value_done (&target);
  653. } /* end of if (!repl_op */
  654. /* can get lastmod only after backend is selected */
  655. slapi_pblock_get(pb, SLAPI_BE_LASTMOD, &lastmod);
  656. /* if this is replication session or the operation has been
  657. * flagged - leave mod attributes alone */
  658. if (!repl_op && !skip_modified_attrs && lastmod)
  659. {
  660. modify_update_last_modified_attr(pb, &smods);
  661. }
  662. /*
  663. * Add the unhashed password pseudo-attribute before
  664. * calling the preop plugins
  665. */
  666. if (pw_change)
  667. {
  668. Slapi_Value **va= NULL;
  669. unhashed_pw_attr = slapi_attr_syntax_normalize(PSEUDO_ATTR_UNHASHEDUSERPASSWORD);
  670. for ( pw_mod = slapi_mods_get_first_mod(&smods); pw_mod;
  671. pw_mod = slapi_mods_get_next_mod(&smods) )
  672. {
  673. if (strcasecmp (pw_mod->mod_type, SLAPI_USERPWD_ATTR) != 0)
  674. continue;
  675. /* add pseudo password attribute */
  676. valuearray_init_bervalarray(pw_mod->mod_bvalues, &va);
  677. slapi_mods_add_mod_values(&smods, pw_mod->mod_op, unhashed_pw_attr, va);
  678. valuearray_free(&va);
  679. /* Init new value array for hashed value */
  680. valuearray_init_bervalarray(pw_mod->mod_bvalues, &va);
  681. /* encode password */
  682. pw_encodevals_ext(pb, &sdn, va);
  683. /* remove current clear value of userpassword */
  684. ber_bvecfree(pw_mod->mod_bvalues);
  685. /* add the cipher in the structure */
  686. valuearray_get_bervalarray(va, &pw_mod->mod_bvalues);
  687. valuearray_free(&va);
  688. }
  689. }
  690. for ( p = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME); p != NULL; p = p->plg_next )
  691. {
  692. char *L_attr = NULL;
  693. int i = 0;
  694. /* Get the appropriate encoding function */
  695. for ( L_attr = p->plg_argv[i]; i<p->plg_argc; L_attr = p->plg_argv[++i])
  696. {
  697. char *L_normalized = slapi_attr_syntax_normalize(L_attr);
  698. for ( lc_mod = slapi_mods_get_first_mod(&smods); lc_mod;
  699. lc_mod = slapi_mods_get_next_mod(&smods) )
  700. {
  701. Slapi_Value **va= NULL;
  702. if (strcasecmp (lc_mod->mod_type, L_normalized) != 0)
  703. continue;
  704. switch (lc_mod->mod_op & ~LDAP_MOD_BVALUES)
  705. {
  706. case LDAP_MOD_ADD:
  707. case LDAP_MOD_REPLACE:
  708. /* Init new value array for hashed value */
  709. valuearray_init_bervalarray(lc_mod->mod_bvalues, &va);
  710. if ( va )
  711. {
  712. /* encode local credentials */
  713. pw_rever_encode(va, L_normalized);
  714. /* remove current clear value of userpassword */
  715. ber_bvecfree(lc_mod->mod_bvalues);
  716. /* add the cipher in the structure */
  717. valuearray_get_bervalarray(va, &lc_mod->mod_bvalues);
  718. valuearray_free(&va);
  719. }
  720. break;
  721. default:
  722. /* for LDAP_MOD_DELETE, don't do anything */
  723. /* for LDAP_MOD_BVALUES, don't do anything */
  724. ;
  725. }
  726. }
  727. if (L_normalized)
  728. slapi_ch_free ((void**)&L_normalized);
  729. }
  730. }
  731. /*
  732. * call the pre-mod plugins. if they succeed, call
  733. * the backend mod function. then call the post-mod
  734. * plugins.
  735. */
  736. slapi_pblock_set (pb, SLAPI_MODIFY_MODS, (void*)slapi_mods_get_ldapmods_passout (&smods));
  737. if (plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN :
  738. SLAPI_PLUGIN_PRE_MODIFY_FN) == 0)
  739. {
  740. int rc;
  741. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  742. set_db_default_result_handlers(pb);
  743. /* Remove the unhashed password pseudo-attribute prior */
  744. /* to db access */
  745. if (pw_change)
  746. {
  747. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  748. slapi_mods_init_passin (&smods, mods);
  749. remove_mod (&smods, unhashed_pw_attr, &unhashed_pw_smod);
  750. slapi_pblock_set (pb, SLAPI_MODIFY_MODS,
  751. (void*)slapi_mods_get_ldapmods_passout (&smods));
  752. }
  753. if (be->be_modify != NULL)
  754. {
  755. if ((rc = (*be->be_modify)(pb)) == 0)
  756. {
  757. /* acl is not used for internal operations */
  758. /* don't update aci store for remote acis */
  759. if ((!internal_op) &&
  760. (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  761. {
  762. plugin_call_acl_mods_update (pb, SLAPI_OPERATION_MODIFY);
  763. }
  764. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  765. write_audit_log_entry(pb); /* Record the operation in the audit log */
  766. if (pw_change && (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  767. {
  768. /* update the password info */
  769. update_pw_info (pb, old_pw);
  770. }
  771. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &pse);
  772. do_ps_service(pse, NULL, LDAP_CHANGETYPE_MODIFY, 0);
  773. }
  774. else
  775. {
  776. if (rc == SLAPI_FAIL_DISKFULL)
  777. {
  778. operation_out_of_disk_space();
  779. goto free_and_return;
  780. }
  781. }
  782. }
  783. else
  784. {
  785. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  786. "Function not implemented", 0, NULL);
  787. }
  788. /* Add the pseudo-attribute prior to calling the postop plugins */
  789. if (pw_change)
  790. {
  791. LDAPMod *lc_mod = NULL;
  792. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  793. slapi_mods_init_passin (&smods, mods);
  794. for ( lc_mod = slapi_mods_get_first_mod(&unhashed_pw_smod); lc_mod;
  795. lc_mod = slapi_mods_get_next_mod(&unhashed_pw_smod) )
  796. {
  797. Slapi_Mod lc_smod;
  798. slapi_mod_init_byval(&lc_smod, lc_mod); /* copies lc_mod */
  799. /* this extracts the copy of lc_mod and finalizes lc_smod too */
  800. slapi_mods_add_ldapmod(&smods,
  801. slapi_mod_get_ldapmod_passout(&lc_smod));
  802. }
  803. slapi_pblock_set (pb, SLAPI_MODIFY_MODS,
  804. (void*)slapi_mods_get_ldapmods_passout (&smods));
  805. slapi_mods_done(&unhashed_pw_smod); /* can finalize now */
  806. }
  807. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  808. plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN :
  809. SLAPI_PLUGIN_POST_MODIFY_FN);
  810. }
  811. free_and_return:
  812. {
  813. Slapi_Entry *epre = NULL, *epost = NULL;
  814. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &epre);
  815. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &epost);
  816. if (epre == e) {
  817. epre = NULL; /* to avoid possible double free below */
  818. }
  819. if (epost == e) {
  820. epost = NULL; /* to avoid possible double free below */
  821. }
  822. if (epre == epost) {
  823. epost = NULL; /* to avoid possible double free below */
  824. }
  825. slapi_pblock_set(pb, SLAPI_ENTRY_PRE_OP, NULL);
  826. slapi_pblock_set(pb, SLAPI_ENTRY_POST_OP, NULL);
  827. slapi_entry_free(epre);
  828. slapi_entry_free(epost);
  829. }
  830. slapi_entry_free(e);
  831. if (be)
  832. slapi_be_Unlock(be);
  833. slapi_sdn_done(&sdn);
  834. if (unhashed_pw_attr)
  835. slapi_ch_free ((void**)&unhashed_pw_attr);
  836. slapi_ch_free_string(&proxydn);
  837. }
  838. static void remove_mod (Slapi_Mods *smods, const char *type, Slapi_Mods *smod_unhashed)
  839. {
  840. LDAPMod *mod;
  841. Slapi_Mod smod;
  842. for (mod = slapi_mods_get_first_mod(smods); mod; mod = slapi_mods_get_next_mod(smods))
  843. {
  844. if (strcasecmp (mod->mod_type, type) == 0)
  845. {
  846. slapi_mod_init_byval (&smod, mod);
  847. slapi_mods_add_smod(smod_unhashed, &smod);
  848. slapi_mods_remove (smods);
  849. }
  850. }
  851. }
  852. static int op_shared_allow_pw_change (Slapi_PBlock *pb, LDAPMod *mod, char **old_pw, Slapi_Mods *smods)
  853. {
  854. int isroot, internal_op, repl_op, pwresponse_req = 0;
  855. int res = 0;
  856. char *dn;
  857. char *errtxt = NULL;
  858. Slapi_DN sdn;
  859. Slapi_Entry *e = NULL;
  860. passwdPolicy *pwpolicy;
  861. int rc = 0;
  862. char ebuf[BUFSIZ];
  863. Slapi_Value **values= NULL;
  864. Slapi_Operation *operation;
  865. int proxy_err = LDAP_SUCCESS;
  866. char *proxydn = NULL;
  867. char *proxystr = NULL;
  868. char *errtext = NULL;
  869. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  870. if (repl_op) {
  871. /* Treat like there's no password */
  872. return (0);
  873. }
  874. *old_pw = NULL;
  875. slapi_pblock_get (pb, SLAPI_ORIGINAL_TARGET, &dn);
  876. slapi_pblock_get (pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  877. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  878. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pwresponse_req);
  879. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  880. slapi_sdn_init_dn_byref (&sdn, dn);
  881. pwpolicy = new_passwdPolicy(pb, (char *)slapi_sdn_get_ndn(&sdn));
  882. /* get the proxy auth dn if the proxy auth control is present */
  883. if ((proxy_err = proxyauth_get_dn(pb, &proxydn, &errtext)) != LDAP_SUCCESS)
  884. {
  885. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  886. {
  887. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"\n",
  888. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  889. escape_string(slapi_sdn_get_dn(&sdn), ebuf));
  890. }
  891. send_ldap_result(pb, proxy_err, NULL, errtext, 0, NULL);
  892. rc = -1;
  893. goto done;
  894. }
  895. /* internal operation has root permisions for subtrees it is allowed to access */
  896. if (!internal_op)
  897. {
  898. /* slapi_acl_check_mods needs an array of LDAPMods, but
  899. * we're really only interested in the one password mod. */
  900. LDAPMod *mods[2];
  901. mods[0] = mod;
  902. mods[1] = NULL;
  903. /* We need to actually fetch the target here to use for ACI checking. */
  904. slapi_search_internal_get_entry(&sdn, NULL, &e, (void *)plugin_get_default_component_id());
  905. /* Create a bogus entry with just the target dn if we were unable to
  906. * find the actual entry. This will only be used for checking the ACIs. */
  907. if (e == NULL) {
  908. e = slapi_entry_alloc();
  909. slapi_entry_init( e, NULL, NULL );
  910. slapi_sdn_set_dn_byref(slapi_entry_get_sdn(e), dn);
  911. }
  912. /* Set the backend in the pblock. The slapi_access_allowed function
  913. * needs this set to work properly. */
  914. slapi_pblock_set( pb, SLAPI_BACKEND, slapi_be_select( &sdn ) );
  915. /* Check if ACIs allow password to be changed */
  916. if ( (res = slapi_acl_check_mods(pb, e, mods, &errtxt)) != LDAP_SUCCESS) {
  917. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  918. {
  919. if (proxydn)
  920. {
  921. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  922. }
  923. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s\n",
  924. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  925. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  926. proxystr ? proxystr : "");
  927. }
  928. /* Write access is denied to userPassword by ACIs */
  929. if ( pwresponse_req == 1 ) {
  930. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  931. LDAP_PWPOLICY_PWDMODNOTALLOWED );
  932. }
  933. send_ldap_result(pb, res, NULL, errtxt, 0, NULL);
  934. slapi_ch_free_string(&errtxt);
  935. rc = -1;
  936. goto done;
  937. }
  938. /* Check if password policy allows users to change their passwords.*/
  939. if (!pb->pb_op->o_isroot && slapi_sdn_compare(&sdn, &pb->pb_op->o_sdn)==0 &&
  940. !pb->pb_conn->c_needpw && !pwpolicy->pw_change)
  941. {
  942. if ( pwresponse_req == 1 ) {
  943. slapi_pwpolicy_make_response_control ( pb, -1, -1, LDAP_PWPOLICY_PWDMODNOTALLOWED );
  944. }
  945. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  946. "user is not allowed to change password", 0, NULL);
  947. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  948. {
  949. if (proxydn)
  950. {
  951. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  952. }
  953. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  954. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  955. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  956. proxystr ? proxystr : "",
  957. "user is not allowed to change password");
  958. }
  959. rc = -1;
  960. goto done;
  961. }
  962. }
  963. /* check if password is within password minimum age;
  964. error result is sent directly from check_pw_minage */
  965. if ((internal_op || !pb->pb_conn->c_needpw) &&
  966. check_pw_minage(pb, &sdn, mod->mod_bvalues) == 1)
  967. {
  968. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  969. {
  970. if (proxydn)
  971. {
  972. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  973. }
  974. if ( !internal_op )
  975. {
  976. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  977. pb->pb_conn->c_connid,
  978. pb->pb_op->o_opid,
  979. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  980. proxystr ? proxystr : "",
  981. "within password minimum age");
  982. }
  983. else
  984. {
  985. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s, %s\n",
  986. LOG_INTERNAL_OP_CON_ID,
  987. LOG_INTERNAL_OP_OP_ID,
  988. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  989. proxystr ? proxystr : "",
  990. "within password minimum age");
  991. }
  992. }
  993. rc = -1;
  994. goto done;
  995. }
  996. /* check password syntax; remember the old password;
  997. error sent directly from check_pw_syntax function */
  998. valuearray_init_bervalarray(mod->mod_bvalues, &values);
  999. switch (check_pw_syntax_ext (pb, &sdn, values, old_pw, NULL, 1, smods))
  1000. {
  1001. case 0: /* success */
  1002. rc = 1;
  1003. break;
  1004. case 1: /* failed checking */
  1005. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  1006. {
  1007. if (proxydn)
  1008. {
  1009. proxystr = slapi_ch_smprintf(" authzid=\"%s\"", proxydn);
  1010. }
  1011. if ( !internal_op )
  1012. {
  1013. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d MOD dn=\"%s\"%s, %s\n",
  1014. pb->pb_conn->c_connid,
  1015. pb->pb_op->o_opid,
  1016. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  1017. proxystr ? proxystr : "",
  1018. "invalid password syntax");
  1019. }
  1020. else
  1021. {
  1022. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d MOD dn=\"%s\"%s, %s\n",
  1023. LOG_INTERNAL_OP_CON_ID,
  1024. LOG_INTERNAL_OP_OP_ID,
  1025. escape_string(slapi_sdn_get_dn(&sdn), ebuf),
  1026. proxystr ? proxystr : "",
  1027. "invalid password syntax");
  1028. }
  1029. }
  1030. rc = -1;
  1031. break;
  1032. case -1: /* The entry is not found. No password checking is done. Countinue execution
  1033. and it should get caught later and send "no such object back. */
  1034. rc = 0;
  1035. break;
  1036. default: break;
  1037. }
  1038. valuearray_free(&values);
  1039. done:
  1040. slapi_entry_free( e );
  1041. slapi_sdn_done (&sdn);
  1042. delete_passwdPolicy(&pwpolicy);
  1043. slapi_ch_free_string(&proxydn);
  1044. slapi_ch_free_string(&proxystr);
  1045. return rc;
  1046. }