modify.c 32 KB

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