modify.c 39 KB

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