pw.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  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. * slapd hashed password routines
  49. *
  50. */
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <sys/types.h>
  54. #include <sechash.h>
  55. #if defined( _WIN32 )
  56. #undef DEBUG
  57. #endif /* _WIN32 */
  58. #if defined( _WIN32 )
  59. #undef LDAPDebug
  60. #endif /* _WIN32 */
  61. #include "slap.h"
  62. #define DENY_PW_CHANGE_ACI "(targetattr = \"userPassword\") ( version 3.0; acl \"disallow_pw_change_aci\"; deny (write ) userdn = \"ldap:///self\";)"
  63. #define GENERALIZED_TIME_LENGTH 15
  64. static int pw_in_history(Slapi_Value **history_vals, const Slapi_Value *pw_val);
  65. static int update_pw_history( Slapi_PBlock *pb, const Slapi_DN *sdn, char *old_pw );
  66. static int check_trivial_words (Slapi_PBlock *, Slapi_Entry *, Slapi_Value **,
  67. char *attrtype, int toklen, Slapi_Mods *smods );
  68. static int pw_boolean_str2value (const char *str);
  69. /* static LDAPMod* pw_malloc_mod (char* name, char* value, int mod_op); */
  70. /*
  71. * We want to be able to return errors to internal operations (which
  72. * can come from the password change extended operation). So we have
  73. * a special result function that does the right thing for an internal op.
  74. */
  75. static void
  76. pw_send_ldap_result(
  77. Slapi_PBlock *pb,
  78. int err,
  79. char *matched,
  80. char *text,
  81. int nentries,
  82. struct berval **urls
  83. )
  84. {
  85. int internal_op = 0;
  86. Slapi_Operation *operation = NULL;
  87. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  88. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  89. if (internal_op) {
  90. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &err);
  91. } else {
  92. send_ldap_result(pb, err, matched, text, nentries, urls);
  93. }
  94. }
  95. /*
  96. * Like slapi_value_find, except for passwords.
  97. * returns 0 if password "v" is found in "vals"; non-zero otherwise
  98. */
  99. SLAPI_DEPRECATED int
  100. slapi_pw_find(
  101. struct berval **vals,
  102. struct berval *v
  103. )
  104. {
  105. int rc;
  106. Slapi_Value **svin_vals= NULL;
  107. Slapi_Value svin_v;
  108. slapi_value_init_berval(&svin_v,v);
  109. valuearray_init_bervalarray(vals,&svin_vals); /* JCM SLOW FUNCTION */
  110. rc= slapi_pw_find_sv(svin_vals,&svin_v);
  111. valuearray_free(&svin_vals);
  112. value_done(&svin_v);
  113. return rc;
  114. }
  115. /*
  116. * Like slapi_value_find, except for passwords.
  117. * returns 0 if password "v" is found in "vals"; non-zero otherwise
  118. */
  119. int
  120. slapi_pw_find_sv(
  121. Slapi_Value **vals,
  122. const Slapi_Value *v
  123. )
  124. {
  125. struct pw_scheme *pwsp;
  126. char *valpwd;
  127. int i;
  128. LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_pw_find value: \"%s\"\n", slapi_value_get_string(v), 0, 0 ); /* JCM Innards */
  129. for ( i = 0; vals[i] != NULL; i++ )
  130. {
  131. pwsp = pw_val2scheme( (char*)slapi_value_get_string(vals[i]), &valpwd, 1 ); /* JCM Innards*/
  132. if ( pwsp != NULL &&
  133. (*(pwsp->pws_cmp))( (char*)slapi_value_get_string(v), valpwd ) == 0 ) /* JCM Innards*/
  134. {
  135. LDAPDebug( LDAP_DEBUG_TRACE,
  136. "<= slapi_pw_find matched \"%s\" using scheme \"%s\"\n",
  137. valpwd, pwsp->pws_name, 0 );
  138. free_pw_scheme( pwsp );
  139. return( 0 ); /* found it */
  140. }
  141. free_pw_scheme( pwsp );
  142. }
  143. LDAPDebug( LDAP_DEBUG_TRACE, "<= slapi_pw_find no matching password\n", 0, 0, 0 );
  144. return( 1 ); /* no match */
  145. }
  146. /* Checks if the specified value is encoded.
  147. Returns 1 if it is and 0 otherwise
  148. */
  149. int slapi_is_encoded (char *value)
  150. {
  151. struct pw_scheme *is_hashed = NULL;
  152. int is_encoded = 0;
  153. is_hashed = pw_val2scheme ( value, NULL, 0 );
  154. if ( is_hashed != NULL )
  155. {
  156. free_pw_scheme( is_hashed );
  157. is_encoded = 1;
  158. }
  159. return (is_encoded);
  160. }
  161. char* slapi_encode (char *value, char *alg)
  162. {
  163. return( slapi_encode_ext( NULL, NULL, value, alg ) );
  164. }
  165. char* slapi_encode_ext (Slapi_PBlock *pb, const Slapi_DN *sdn, char *value, char *alg)
  166. {
  167. struct pw_scheme *enc_scheme = NULL;
  168. char *(*pws_enc) ( char *pwd ) = NULL;
  169. char *hashedval = NULL;
  170. passwdPolicy *pwpolicy=NULL;
  171. if (alg == NULL) /* use local scheme, or global if we can't fetch local */
  172. {
  173. pwpolicy = new_passwdPolicy(pb, (char*)slapi_sdn_get_ndn(sdn) );
  174. pws_enc = pwpolicy->pw_storagescheme->pws_enc;
  175. delete_passwdPolicy(&pwpolicy);
  176. if (pws_enc == NULL)
  177. {
  178. slapi_log_error( SLAPI_LOG_FATAL, NULL,
  179. "slapi_encode: no server scheme\n" );
  180. return NULL;
  181. }
  182. }
  183. else
  184. {
  185. enc_scheme = pw_name2scheme(alg);
  186. if ( enc_scheme == NULL)
  187. {
  188. char * scheme_list = plugin_get_pwd_storage_scheme_list(PLUGIN_LIST_PWD_STORAGE_SCHEME);
  189. if ( scheme_list != NULL ) {
  190. slapi_log_error( SLAPI_LOG_FATAL, NULL,
  191. "slapi_encode: invalid scheme - %s\n"
  192. "Valid values are: %s\n", alg, scheme_list );
  193. slapi_ch_free((void **)&scheme_list);
  194. } else {
  195. slapi_log_error( SLAPI_LOG_FATAL, NULL,
  196. "slapi_encode: invalid scheme - %s\n"
  197. "no pwdstorage scheme plugin loaded", alg);
  198. }
  199. return NULL;
  200. }
  201. pws_enc = enc_scheme->pws_enc;
  202. free_pw_scheme(enc_scheme);
  203. }
  204. hashedval = (*pws_enc)(value);
  205. return hashedval;
  206. }
  207. /*
  208. * Return a pointer to the pw_scheme struct for scheme "name"
  209. * NULL is returned is no matching scheme is found.
  210. */
  211. struct pw_scheme *
  212. pw_name2scheme( char *name )
  213. {
  214. struct pw_scheme *pwsp;
  215. struct slapdplugin *p;
  216. if ( (p = plugin_get_pwd_storage_scheme(name, strlen(name), PLUGIN_LIST_PWD_STORAGE_SCHEME)) != NULL ) {
  217. pwsp = (struct pw_scheme *) slapi_ch_malloc (sizeof(struct pw_scheme));
  218. if ( pwsp != NULL ) {
  219. typedef int (*CMPFP)(char *, char *);
  220. typedef char * (*ENCFP)(char *);
  221. pwsp->pws_name = slapi_ch_strdup( p->plg_pwdstorageschemename );
  222. pwsp->pws_cmp = (CMPFP)p->plg_pwdstorageschemecmp;
  223. pwsp->pws_enc = (ENCFP)p->plg_pwdstorageschemeenc;
  224. pwsp->pws_len = strlen(pwsp->pws_name) ;
  225. return(pwsp);
  226. }
  227. }
  228. return( NULL );
  229. }
  230. void free_pw_scheme(struct pw_scheme *pwsp)
  231. {
  232. if ( pwsp != NULL )
  233. {
  234. slapi_ch_free( (void**)&pwsp->pws_name );
  235. slapi_ch_free( (void**)&pwsp );
  236. }
  237. }
  238. /*
  239. * Return the password scheme for value "val". This is determined by
  240. * checking "val" against our scheme prefixes.
  241. *
  242. * If "valpwdp" is not NULL, it is set to point to the value with any
  243. * prefix removed.
  244. *
  245. * If no matching scheme is found and first_is_default is non-zero, the
  246. * first scheme is returned. If no matching scheme is found and
  247. * first_is_default is zero, NULL is returned.
  248. */
  249. struct pw_scheme *
  250. pw_val2scheme( char *val, char **valpwdp, int first_is_default )
  251. {
  252. struct pw_scheme *pwsp;
  253. int namelen, prefixlen;
  254. char *end, buf[ PWD_MAX_NAME_LEN + 1 ];
  255. if ( *val != PWD_HASH_PREFIX_START ||
  256. ( end = strchr( val, PWD_HASH_PREFIX_END )) == NULL ||
  257. ( namelen = end - val - 1 ) > PWD_MAX_NAME_LEN ) {
  258. if ( !first_is_default ) {
  259. return( NULL );
  260. }
  261. pwsp = pw_name2scheme("CLEAR"); /* default to first scheme */
  262. prefixlen = 0;
  263. } else {
  264. memcpy( buf, val + 1, namelen );
  265. buf[ namelen ] = '\0';
  266. pwsp = pw_name2scheme(buf);
  267. if ( pwsp == NULL ) {
  268. if ( !first_is_default ) {
  269. return( NULL );
  270. }
  271. pwsp = pw_name2scheme("CLEAR");
  272. prefixlen = 0;
  273. } else {
  274. prefixlen = pwsp->pws_len + 2;
  275. }
  276. }
  277. if ( valpwdp != NULL ) {
  278. *valpwdp = val + prefixlen;
  279. }
  280. return( pwsp );
  281. }
  282. /*
  283. * re-encode the password values in "vals" using a hashing algorithm
  284. * vals[n] is assumed to be an alloc'd Slapi_Value that can be free'd and
  285. * replaced. If a value is already encoded, we do not re-encode it.
  286. * Return 0 if all goes well and < 0 if an error occurs.
  287. */
  288. int
  289. pw_encodevals( Slapi_Value **vals )
  290. {
  291. return( pw_encodevals_ext( NULL, NULL, vals ) );
  292. }
  293. /*
  294. * Same as pw_encodevals, except if a pb and sdn are passed in, we will try
  295. * to check the password scheme specified by local password policy.
  296. */
  297. int
  298. pw_encodevals_ext( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals )
  299. {
  300. int i;
  301. passwdPolicy *pwpolicy=NULL;
  302. char *(*pws_enc) ( char *pwd ) = NULL;
  303. if ( (NULL == pb) || (NULL == vals) ) {
  304. return( 0 );
  305. }
  306. /* new_passwdPolicy gives us a local policy if sdn and pb are set and
  307. can be used to find a local policy, else we get the global policy */
  308. pwpolicy = new_passwdPolicy(pb, sdn ? (char*)slapi_sdn_get_ndn(sdn) : NULL);
  309. if (pwpolicy) {
  310. if (pwpolicy->pw_storagescheme) {
  311. pws_enc = pwpolicy->pw_storagescheme->pws_enc;
  312. }
  313. delete_passwdPolicy(&pwpolicy);
  314. }
  315. /* Password scheme encryption function was not found */
  316. if ( pws_enc == NULL ) {
  317. return( 0 );
  318. }
  319. for ( i = 0; vals[ i ] != NULL; ++i ) {
  320. struct pw_scheme *pwsp = NULL;
  321. char *enc = NULL;
  322. if ( (pwsp=pw_val2scheme( (char*)slapi_value_get_string(vals[ i ]), NULL, 0)) != NULL ) { /* JCM Innards */
  323. /* If the value already specifies clear storage, call the
  324. * clear storage plug-in */
  325. if (strcasecmp( pwsp->pws_name, "clear" ) == 0) {
  326. enc = (*pwsp->pws_enc)( (char*)slapi_value_get_string(vals[ i ]) );
  327. } else {
  328. free_pw_scheme( pwsp );
  329. continue; /* don't touch pre-encoded values */
  330. }
  331. }
  332. free_pw_scheme( pwsp );
  333. if ((!enc) && (( enc = (*pws_enc)( (char*)slapi_value_get_string(vals[ i ]) )) == NULL )) {
  334. return( -1 );
  335. }
  336. slapi_value_free(&vals[ i ]);
  337. vals[ i ] = slapi_value_new_string_passin(enc);
  338. }
  339. return( 0 );
  340. }
  341. /*
  342. * Check if the prefix of the cipher is the one that is supposed to be
  343. * Extract from the whole cipher the encrypted password (remove the prefix)
  344. */
  345. int checkPrefix(char *cipher, char *schemaName, char **encrypt)
  346. {
  347. int namelen;
  348. /* buf contains the extracted schema name */
  349. char *end, buf[ 3*PWD_MAX_NAME_LEN + 1 ];
  350. if ( (*cipher == PWD_HASH_PREFIX_START) &&
  351. ((end = strchr(cipher, PWD_HASH_PREFIX_END)) != NULL) &&
  352. ((namelen = end - cipher - 1 ) <= (3*PWD_MAX_NAME_LEN)) )
  353. {
  354. memcpy( buf, cipher + 1, namelen );
  355. buf[ namelen ] = '\0';
  356. if ( strcasecmp( buf, schemaName) != 0 )
  357. {
  358. /* schema names are different, error */
  359. return 1;
  360. }
  361. else
  362. {
  363. /* extract the encrypted password */
  364. *encrypt = cipher + strlen(schemaName) + 2;
  365. return 0;
  366. }
  367. }
  368. /* cipher is not prefixed, already in clear ? */
  369. return -1;
  370. }
  371. /*
  372. * Decode the attribute "attr_name" with one of the reversible encryption mechanism
  373. * Returns -1 on error
  374. * Returns 0 on success with strdup'ed plain
  375. * Returns 1 on success with *plain=cipher
  376. */
  377. int
  378. pw_rever_decode(char *cipher, char **plain, const char * attr_name)
  379. {
  380. struct pw_scheme *pwsp = NULL;
  381. struct slapdplugin *p = NULL;
  382. int ret_code = 1;
  383. for ( p = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME); p != NULL; p = p->plg_next )
  384. {
  385. char *L_attr = NULL;
  386. int i = 0;
  387. char *encrypt = NULL;
  388. int prefixOK = -1;
  389. /* Get the appropriate decoding function */
  390. for ( L_attr = p->plg_argv[i]; i<p->plg_argc; L_attr = p->plg_argv[++i] )
  391. {
  392. if (slapi_attr_types_equivalent(L_attr, attr_name))
  393. {
  394. typedef int (*CMPFP)(char *, char *);
  395. typedef char * (*ENCFP)(char *);
  396. pwsp = (struct pw_scheme *) slapi_ch_calloc (1, sizeof(struct pw_scheme));
  397. pwsp->pws_dec = (ENCFP)p->plg_pwdstorageschemedec;
  398. pwsp->pws_name = slapi_ch_strdup( p->plg_pwdstorageschemename );
  399. pwsp->pws_len = strlen(pwsp->pws_name) ;
  400. if ( pwsp->pws_dec != NULL )
  401. {
  402. /* check that the prefix of the cipher is the same name
  403. as the scheme name */
  404. prefixOK = checkPrefix(cipher, pwsp->pws_name, &encrypt);
  405. if ( prefixOK == -1 )
  406. {
  407. /* no prefix, already in clear ? */
  408. *plain = cipher;
  409. ret_code = 1;
  410. goto free_and_return;
  411. }
  412. else if ( prefixOK == 1 )
  413. {
  414. /* scheme names are different */
  415. ret_code = -1;
  416. goto free_and_return;
  417. }
  418. else
  419. {
  420. if ( ( *plain = (pwsp->pws_dec)( encrypt )) == NULL )
  421. {
  422. /* pb during decoding */
  423. ret_code = -1;
  424. goto free_and_return;
  425. }
  426. /* decoding is OK */
  427. ret_code = 0;
  428. goto free_and_return;
  429. }
  430. }
  431. free_pw_scheme( pwsp );
  432. pwsp = NULL;
  433. }
  434. }
  435. }
  436. free_and_return:
  437. if ( pwsp != NULL )
  438. {
  439. free_pw_scheme( pwsp );
  440. }
  441. return(ret_code);
  442. }
  443. /*
  444. * Encode the attribute "attr_name" with one of the reversible encryption mechanism
  445. */
  446. int
  447. pw_rever_encode(Slapi_Value **vals, char * attr_name)
  448. {
  449. char *enc;
  450. struct pw_scheme *pwsp = NULL;
  451. struct slapdplugin *p;
  452. if (vals == NULL){
  453. return (0);
  454. }
  455. for ( p = get_plugin_list(PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME); p != NULL; p = p->plg_next )
  456. {
  457. char *L_attr = NULL;
  458. int i = 0;
  459. /* Get the appropriate encoding function */
  460. for ( L_attr = p->plg_argv[i]; i<p->plg_argc; L_attr = p->plg_argv[++i] )
  461. {
  462. if (slapi_attr_types_equivalent(L_attr, attr_name))
  463. {
  464. typedef int (*CMPFP)(char *, char *);
  465. typedef char * (*ENCFP)(char *);
  466. pwsp = (struct pw_scheme *) slapi_ch_calloc (1, sizeof(struct pw_scheme));
  467. pwsp->pws_enc = (ENCFP)p->plg_pwdstorageschemeenc;
  468. pwsp->pws_name = slapi_ch_strdup( p->plg_pwdstorageschemename );
  469. if ( pwsp->pws_enc != NULL )
  470. {
  471. for ( i = 0; vals[i] != NULL; ++i )
  472. {
  473. char *encrypt = NULL;
  474. int prefixOK;
  475. prefixOK = checkPrefix((char*)slapi_value_get_string(vals[i]),
  476. pwsp->pws_name,
  477. &encrypt);
  478. if ( prefixOK == 0 )
  479. {
  480. /* Don't touch already encoded value */
  481. continue; /* don't touch pre-encoded values */
  482. }
  483. else if (prefixOK == 1 )
  484. {
  485. /* credential is already encoded, but not with this schema. Error */
  486. free_pw_scheme( pwsp );
  487. return( -1 );
  488. }
  489. if ( ( enc = (pwsp->pws_enc)( (char*)slapi_value_get_string(vals[ i ]) )) == NULL )
  490. {
  491. free_pw_scheme( pwsp );
  492. return( -1 );
  493. }
  494. slapi_value_free(&vals[ i ]);
  495. vals[ i ] = slapi_value_new_string_passin(enc);
  496. free_pw_scheme( pwsp );
  497. return (0);
  498. }
  499. }
  500. free_pw_scheme( pwsp );
  501. }
  502. }
  503. }
  504. return(-1);
  505. }
  506. /* ONREPL - below are the functions moved from pw_mgmt.c.
  507. this is done to allow the functions to be used
  508. by functions linked into libslapd.
  509. */
  510. /* update_pw_info is called after password is modified successfully */
  511. /* it should update passwordHistory, and passwordExpirationTime */
  512. /* SLAPI_ENTRY_POST_OP must be set */
  513. int
  514. update_pw_info ( Slapi_PBlock *pb , char *old_pw) {
  515. Slapi_Mods smods;
  516. char *timestr;
  517. time_t pw_exp_date;
  518. time_t cur_time;
  519. const char *dn;
  520. Slapi_DN *sdn = NULL;
  521. passwdPolicy *pwpolicy = NULL;
  522. int internal_op = 0;
  523. Slapi_Operation *operation = NULL;
  524. void *txn = NULL;
  525. slapi_pblock_get(pb, SLAPI_TXN, &txn);
  526. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  527. internal_op = slapi_operation_is_flag_set(operation, SLAPI_OP_FLAG_INTERNAL);
  528. cur_time = current_time();
  529. slapi_pblock_get( pb, SLAPI_TARGET_SDN, &sdn );
  530. dn = slapi_sdn_get_dn(sdn);
  531. pwpolicy = new_passwdPolicy(pb, dn);
  532. /* update passwordHistory */
  533. if ( old_pw != NULL && pwpolicy->pw_history == 1 ) {
  534. update_pw_history(pb, sdn, old_pw);
  535. slapi_ch_free ( (void**)&old_pw );
  536. }
  537. slapi_mods_init(&smods, 0);
  538. /* update password allow change time */
  539. if ( pwpolicy->pw_minage != 0) {
  540. timestr = format_genTime( time_plus_sec( cur_time,
  541. pwpolicy->pw_minage ));
  542. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordAllowChangeTime", timestr);
  543. slapi_ch_free((void **)&timestr);
  544. }
  545. /* Fix for Bug 560707
  546. Removed the restriction that the lock variables (retry count) will
  547. be set only when root resets the passwd.
  548. Now admins will also have these privileges.
  549. */
  550. if (pwpolicy->pw_lockout) {
  551. set_retry_cnt_mods (pb, &smods, 0 );
  552. }
  553. /* Clear the passwordgraceusertime from the user entry */
  554. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordgraceusertime", "0");
  555. /* If the password is reset by root, mark it the first time logon. If this is an internal
  556. * operation, we have a special case for the password modify extended operation where
  557. * we stuff the actual user who initiated the password change in pb_conn. We check
  558. * for this special case to ensure we reset the expiration date properly. */
  559. if ((internal_op && pwpolicy->pw_must_change && (!pb->pb_conn || slapi_dn_isroot(pb->pb_conn->c_dn))) ||
  560. (!internal_op && pwpolicy->pw_must_change && (pb->pb_requestor_isroot == 1))) {
  561. pw_exp_date = NO_TIME;
  562. } else if ( pwpolicy->pw_exp == 1 ) {
  563. Slapi_Entry *pse = NULL;
  564. /* update password expiration date */
  565. pw_exp_date = time_plus_sec ( cur_time,
  566. pwpolicy->pw_maxage );
  567. slapi_pblock_get(pb,SLAPI_ENTRY_POST_OP,&pse);
  568. if (pse) {
  569. char *prev_exp_date_str;
  570. /* if the password expiry time is SLAPD_END_TIME,
  571. * don't roll it back
  572. */
  573. prev_exp_date_str = slapi_entry_attr_get_charptr(pse,"passwordExpirationTime");
  574. if (prev_exp_date_str) {
  575. time_t prev_exp_date;
  576. prev_exp_date = parse_genTime(prev_exp_date_str);
  577. if (prev_exp_date == NO_TIME ||
  578. prev_exp_date == NOT_FIRST_TIME) {
  579. /* ignore as will replace */
  580. } else if (prev_exp_date == SLAPD_END_TIME) {
  581. /* Special entries' passwords never expire */
  582. slapi_ch_free((void**)&prev_exp_date_str);
  583. pw_apply_mods_ext(sdn, &smods, txn);
  584. slapi_mods_done(&smods);
  585. delete_passwdPolicy(&pwpolicy);
  586. return 0;
  587. }
  588. slapi_ch_free((void**)&prev_exp_date_str);
  589. }
  590. } /* post op entry */
  591. } else if (pwpolicy->pw_must_change) {
  592. /*
  593. * pw is not changed by root, and must change pw first time
  594. * log on
  595. */
  596. pw_exp_date = NOT_FIRST_TIME;
  597. } else {
  598. pw_apply_mods_ext(sdn, &smods, txn);
  599. slapi_mods_done(&smods);
  600. delete_passwdPolicy(&pwpolicy);
  601. return 0;
  602. }
  603. delete_passwdPolicy(&pwpolicy);
  604. timestr = format_genTime ( pw_exp_date );
  605. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestr);
  606. slapi_ch_free((void **)&timestr);
  607. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpWarned", "0");
  608. pw_apply_mods_ext(sdn, &smods, txn);
  609. slapi_mods_done(&smods);
  610. if (pb->pb_conn) { /* no conn for internal op */
  611. /* reset c_needpw to 0 */
  612. pb->pb_conn->c_needpw = 0;
  613. }
  614. return 0;
  615. }
  616. int
  617. check_pw_minage ( Slapi_PBlock *pb, const Slapi_DN *sdn, struct berval **vals)
  618. {
  619. char *dn= (char*)slapi_sdn_get_ndn(sdn); /* jcm - Had to cast away const */
  620. passwdPolicy *pwpolicy=NULL;
  621. int pwresponse_req = 0;
  622. pwpolicy = new_passwdPolicy(pb, dn);
  623. slapi_pblock_get ( pb, SLAPI_PWPOLICY, &pwresponse_req );
  624. if ( !pb->pb_op->o_isroot &&
  625. pwpolicy->pw_minage != 0 ) {
  626. Slapi_Entry *e;
  627. char *passwordAllowChangeTime;
  628. /* retrieve the entry */
  629. e = get_entry ( pb, dn );
  630. if ( e == NULL ) {
  631. delete_passwdPolicy(&pwpolicy);
  632. return ( -1 );
  633. }
  634. /* get passwordAllowChangeTime attribute */
  635. passwordAllowChangeTime= slapi_entry_attr_get_charptr(e, "passwordAllowChangeTime");
  636. if (passwordAllowChangeTime!=NULL)
  637. {
  638. time_t pw_allowchange_date;
  639. char *cur_time_str = NULL;
  640. pw_allowchange_date = parse_genTime(passwordAllowChangeTime);
  641. slapi_ch_free((void **) &passwordAllowChangeTime );
  642. /* check if allow to change the password */
  643. cur_time_str = format_genTime ( current_time() );
  644. if ( difftime ( pw_allowchange_date,
  645. parse_genTime ( cur_time_str )) > 0 )
  646. {
  647. if ( pwresponse_req == 1 ) {
  648. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  649. LDAP_PWPOLICY_PWDTOOYOUNG );
  650. }
  651. pw_send_ldap_result ( pb,
  652. LDAP_CONSTRAINT_VIOLATION, NULL,
  653. "within password minimum age", 0, NULL );
  654. slapi_entry_free( e );
  655. slapi_ch_free((void **) &cur_time_str );
  656. delete_passwdPolicy(&pwpolicy);
  657. return ( 1 );
  658. }
  659. slapi_ch_free((void **) &cur_time_str );
  660. }
  661. slapi_entry_free( e );
  662. }
  663. delete_passwdPolicy(&pwpolicy);
  664. return ( 0 );
  665. }
  666. /* check_pw_syntax is called before add or modify operation on userpassword attribute*/
  667. int
  668. check_pw_syntax ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
  669. char **old_pw, Slapi_Entry *e, int mod_op)
  670. {
  671. return ( check_pw_syntax_ext(pb, sdn, vals, old_pw, e, mod_op, NULL) );
  672. }
  673. int
  674. check_pw_syntax_ext ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
  675. char **old_pw, Slapi_Entry *e, int mod_op, Slapi_Mods *smods)
  676. {
  677. Slapi_Attr *attr;
  678. int i, pwresponse_req = 0;
  679. int is_replication = 0;
  680. int internal_op = 0;
  681. char *dn= (char*)slapi_sdn_get_ndn(sdn); /* jcm - Had to cast away const */
  682. char *pwd = NULL;
  683. char *p = NULL;
  684. char errormsg[ BUFSIZ ];
  685. passwdPolicy *pwpolicy = NULL;
  686. Slapi_Operation *operation = NULL;
  687. pwpolicy = new_passwdPolicy(pb, dn);
  688. slapi_pblock_get ( pb, SLAPI_PWPOLICY, &pwresponse_req );
  689. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replication);
  690. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  691. internal_op = slapi_operation_is_flag_set(operation, SLAPI_OP_FLAG_INTERNAL);
  692. if ( pwpolicy->pw_syntax == 1 ) {
  693. for ( i = 0; vals[ i ] != NULL; ++i ) {
  694. int syntax_violation = 0;
  695. int num_digits = 0;
  696. int num_alphas = 0;
  697. int num_uppers = 0;
  698. int num_lowers = 0;
  699. int num_specials = 0;
  700. int num_8bit = 0;
  701. int num_repeated = 0;
  702. int max_repeated = 0;
  703. int num_categories = 0;
  704. /* Check if password is already hashed and reject if so. We
  705. * We need to allow the root DN and replicated ops to send
  706. * pre-hashed passwords. We also check for a connection object
  707. * when processing an internal operation to handle a special
  708. * case for the password modify extended operation. */
  709. if (slapi_is_encoded((char *)slapi_value_get_string(vals[i]))) {
  710. if ((!is_replication && ((internal_op && pb->pb_conn && !slapi_dn_isroot(pb->pb_conn->c_dn)) ||
  711. (!internal_op && !pb->pb_requestor_isroot)))) {
  712. PR_snprintf( errormsg, BUFSIZ,
  713. "invalid password syntax - passwords with storage scheme are not allowed");
  714. if ( pwresponse_req == 1 ) {
  715. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  716. LDAP_PWPOLICY_INVALIDPWDSYNTAX );
  717. }
  718. pw_send_ldap_result ( pb, LDAP_CONSTRAINT_VIOLATION, NULL, errormsg, 0, NULL );
  719. delete_passwdPolicy(&pwpolicy);
  720. return( 1 );
  721. } else {
  722. /* We want to skip syntax checking since this is a pre-hashed
  723. * password from replication or the root DN. */
  724. delete_passwdPolicy(&pwpolicy);
  725. return( 0 );
  726. }
  727. }
  728. /* check for the minimum password length */
  729. if ( pwpolicy->pw_minlength >
  730. ldap_utf8characters((char *)slapi_value_get_string( vals[i] )) )
  731. {
  732. PR_snprintf( errormsg, BUFSIZ,
  733. "invalid password syntax - password must be at least %d characters long",
  734. pwpolicy->pw_minlength );
  735. if ( pwresponse_req == 1 ) {
  736. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  737. LDAP_PWPOLICY_PWDTOOSHORT );
  738. }
  739. pw_send_ldap_result ( pb, LDAP_CONSTRAINT_VIOLATION, NULL, errormsg, 0, NULL );
  740. delete_passwdPolicy(&pwpolicy);
  741. return ( 1 );
  742. }
  743. /* check character types */
  744. pwd = (char *)slapi_value_get_string( vals[i] );
  745. p = pwd;
  746. while ( p && *p )
  747. {
  748. if ( ldap_utf8isdigit( p ) ) {
  749. num_digits++;
  750. } else if ( ldap_utf8isalpha( p ) ) {
  751. num_alphas++;
  752. if ( slapi_utf8isLower( (unsigned char *)p ) ) {
  753. num_lowers++;
  754. } else {
  755. num_uppers++;
  756. }
  757. } else {
  758. /* check if this is an 8-bit char */
  759. if ( *p & 128 ) {
  760. num_8bit++;
  761. } else {
  762. num_specials++;
  763. }
  764. }
  765. /* check for repeating characters. If this is the
  766. first char of the password, no need to check */
  767. if ( pwd != p ) {
  768. int len = ldap_utf8len( p );
  769. char *prev_p = ldap_utf8prev( p );
  770. if ( len == ldap_utf8len( prev_p ) )
  771. {
  772. if ( memcmp( p, prev_p, len ) == 0 )
  773. {
  774. num_repeated++;
  775. if ( max_repeated < num_repeated ) {
  776. max_repeated = num_repeated;
  777. }
  778. } else {
  779. num_repeated = 0;
  780. }
  781. } else {
  782. num_repeated = 0;
  783. }
  784. }
  785. p = ldap_utf8next( p );
  786. }
  787. /* tally up the number of character categories */
  788. if ( num_digits > 0 )
  789. ++num_categories;
  790. if ( num_uppers > 0 )
  791. ++num_categories;
  792. if ( num_lowers > 0 )
  793. ++num_categories;
  794. if ( num_specials > 0 )
  795. ++num_categories;
  796. if ( num_8bit > 0 )
  797. ++num_categories;
  798. /* check for character based syntax limits */
  799. if ( pwpolicy->pw_mindigits > num_digits ) {
  800. syntax_violation = 1;
  801. PR_snprintf ( errormsg, BUFSIZ,
  802. "invalid password syntax - password must contain at least %d digit characters",
  803. pwpolicy->pw_mindigits );
  804. } else if ( pwpolicy->pw_minalphas > num_alphas ) {
  805. syntax_violation = 1;
  806. PR_snprintf ( errormsg, BUFSIZ,
  807. "invalid password syntax - password must contain at least %d alphabetic characters",
  808. pwpolicy->pw_minalphas );
  809. } else if ( pwpolicy->pw_minuppers > num_uppers ) {
  810. syntax_violation = 1;
  811. PR_snprintf ( errormsg, BUFSIZ,
  812. "invalid password syntax - password must contain at least %d uppercase characters",
  813. pwpolicy->pw_minuppers );
  814. } else if ( pwpolicy->pw_minlowers > num_lowers ) {
  815. syntax_violation = 1;
  816. PR_snprintf ( errormsg, BUFSIZ,
  817. "invalid password syntax - password must contain at least %d lowercase characters",
  818. pwpolicy->pw_minlowers );
  819. } else if ( pwpolicy->pw_minspecials > num_specials ) {
  820. syntax_violation = 1;
  821. PR_snprintf ( errormsg, BUFSIZ,
  822. "invalid password syntax - password must contain at least %d special characters",
  823. pwpolicy->pw_minspecials );
  824. } else if ( pwpolicy->pw_min8bit > num_8bit ) {
  825. syntax_violation = 1;
  826. PR_snprintf ( errormsg, BUFSIZ,
  827. "invalid password syntax - password must contain at least %d 8-bit characters",
  828. pwpolicy->pw_min8bit );
  829. } else if ( (pwpolicy->pw_maxrepeats != 0) && (pwpolicy->pw_maxrepeats < (max_repeated + 1)) ) {
  830. syntax_violation = 1;
  831. PR_snprintf ( errormsg, BUFSIZ,
  832. "invalid password syntax - a character cannot be repeated more than %d times",
  833. (pwpolicy->pw_maxrepeats + 1) );
  834. } else if ( pwpolicy->pw_mincategories > num_categories ) {
  835. syntax_violation = 1;
  836. PR_snprintf ( errormsg, BUFSIZ,
  837. "invalid password syntax - password must contain at least %d character "
  838. "categories (valid categories are digit, uppercase, lowercase, special, and 8-bit characters)",
  839. pwpolicy->pw_mincategories );
  840. }
  841. /* If the password failed syntax checking, send the result and return */
  842. if (syntax_violation) {
  843. if ( pwresponse_req == 1 ) {
  844. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  845. LDAP_PWPOLICY_INVALIDPWDSYNTAX );
  846. }
  847. pw_send_ldap_result ( pb, LDAP_CONSTRAINT_VIOLATION, NULL, errormsg, 0, NULL );
  848. delete_passwdPolicy(&pwpolicy);
  849. return ( 1 );
  850. }
  851. }
  852. }
  853. /* get the entry and check for the password history if this is called by a modify operation */
  854. if ( mod_op ) {
  855. /* retrieve the entry */
  856. e = get_entry ( pb, dn );
  857. if ( e == NULL ) {
  858. delete_passwdPolicy(&pwpolicy);
  859. return ( -1 );
  860. }
  861. /* check for password history */
  862. if ( pwpolicy->pw_history == 1 ) {
  863. attr = attrlist_find(e->e_attrs, "passwordHistory");
  864. if (attr &&
  865. !valueset_isempty(&attr->a_present_values))
  866. {
  867. Slapi_Value **va= attr_get_present_values(attr);
  868. if ( pw_in_history( va, vals[0] ) == 0 ) {
  869. if ( pwresponse_req == 1 ) {
  870. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  871. LDAP_PWPOLICY_PWDINHISTORY );
  872. }
  873. pw_send_ldap_result ( pb,
  874. LDAP_CONSTRAINT_VIOLATION, NULL,
  875. "password in history", 0, NULL );
  876. slapi_entry_free( e );
  877. delete_passwdPolicy(&pwpolicy);
  878. return ( 1 );
  879. }
  880. }
  881. /* get current password. check it and remember it */
  882. attr = attrlist_find(e->e_attrs, "userpassword");
  883. if (attr && !valueset_isempty(&attr->a_present_values))
  884. {
  885. Slapi_Value **va= valueset_get_valuearray(&attr->a_present_values);
  886. if (slapi_is_encoded((char*)slapi_value_get_string(vals[0])))
  887. {
  888. if (slapi_attr_value_find(attr, (struct berval *)slapi_value_get_berval(vals[0])) == 0 )
  889. {
  890. pw_send_ldap_result ( pb,
  891. LDAP_CONSTRAINT_VIOLATION ,NULL,
  892. "password in history", 0, NULL);
  893. slapi_entry_free( e );
  894. delete_passwdPolicy(&pwpolicy);
  895. return ( 1 );
  896. }
  897. } else
  898. {
  899. if ( slapi_pw_find_sv ( va, vals[0] ) == 0 )
  900. {
  901. pw_send_ldap_result ( pb,
  902. LDAP_CONSTRAINT_VIOLATION ,NULL,
  903. "password in history", 0, NULL);
  904. slapi_entry_free( e );
  905. delete_passwdPolicy(&pwpolicy);
  906. return ( 1 );
  907. }
  908. }
  909. /* We copy the 1st value of the userpassword attribute.
  910. * This is because password policy assumes that there's only one
  911. * password in the userpassword attribute.
  912. */
  913. *old_pw = slapi_ch_strdup(slapi_value_get_string(va[0]));
  914. } else {
  915. *old_pw = NULL;
  916. }
  917. }
  918. }
  919. /* check for trivial words if syntax checking is enabled */
  920. if ( pwpolicy->pw_syntax == 1 ) {
  921. /* e is null if this is an add operation*/
  922. if ( check_trivial_words ( pb, e, vals, "uid", pwpolicy->pw_mintokenlength, smods ) == 1 ||
  923. check_trivial_words ( pb, e, vals, "cn", pwpolicy->pw_mintokenlength, smods ) == 1 ||
  924. check_trivial_words ( pb, e, vals, "sn", pwpolicy->pw_mintokenlength, smods ) == 1 ||
  925. check_trivial_words ( pb, e, vals, "givenname", pwpolicy->pw_mintokenlength, smods ) == 1 ||
  926. check_trivial_words ( pb, e, vals, "ou", pwpolicy->pw_mintokenlength, smods ) == 1 ||
  927. check_trivial_words ( pb, e, vals, "mail", pwpolicy->pw_mintokenlength, smods ) == 1)
  928. {
  929. if ( mod_op ) {
  930. slapi_entry_free( e );
  931. }
  932. delete_passwdPolicy(&pwpolicy);
  933. return 1;
  934. }
  935. }
  936. delete_passwdPolicy(&pwpolicy);
  937. if ( mod_op ) {
  938. /* free e only when called by modify operation */
  939. slapi_entry_free( e );
  940. }
  941. return 0; /* success */
  942. }
  943. static int
  944. update_pw_history( Slapi_PBlock *pb, const Slapi_DN *sdn, char *old_pw )
  945. {
  946. time_t t, old_t, cur_time;
  947. int i = 0, oldest = 0;
  948. int res;
  949. Slapi_Entry *e;
  950. Slapi_Attr *attr;
  951. LDAPMod attribute;
  952. char *values_replace[25]; /* 2-24 passwords in history */
  953. LDAPMod *list_of_mods[2];
  954. Slapi_PBlock mod_pb;
  955. char *history_str;
  956. char *str;
  957. passwdPolicy *pwpolicy = NULL;
  958. const char *dn = slapi_sdn_get_dn(sdn);
  959. void *txn = NULL;
  960. pwpolicy = new_passwdPolicy(pb, dn);
  961. /* retrieve the entry */
  962. e = get_entry ( pb, dn );
  963. if ( e == NULL ) {
  964. delete_passwdPolicy(&pwpolicy);
  965. return ( 1 );
  966. }
  967. history_str = (char *)slapi_ch_malloc(GENERALIZED_TIME_LENGTH + strlen(old_pw) + 1);
  968. /* get password history, and find the oldest password in history */
  969. cur_time = current_time ();
  970. old_t = cur_time;
  971. str = format_genTime ( cur_time );
  972. attr = attrlist_find(e->e_attrs, "passwordHistory");
  973. if (attr && !valueset_isempty(&attr->a_present_values))
  974. {
  975. Slapi_Value **va= valueset_get_valuearray(&attr->a_present_values);
  976. for ( i = oldest = 0 ;
  977. (va[i] != NULL) && (slapi_value_get_length(va[i]) > 0) ;
  978. i++ ) {
  979. values_replace[i] = (char*)slapi_value_get_string(va[i]);
  980. strncpy( history_str, values_replace[i], GENERALIZED_TIME_LENGTH);
  981. history_str[GENERALIZED_TIME_LENGTH] = '\0';
  982. if (history_str[GENERALIZED_TIME_LENGTH - 1] != 'Z'){
  983. /* The time is not a generalized Time. Probably a password history from 4.x */
  984. history_str[GENERALIZED_TIME_LENGTH - 1] = '\0';
  985. }
  986. t = parse_genTime ( history_str );
  987. if ( difftime ( t, old_t ) < 0 ) {
  988. oldest = i;
  989. old_t = t;
  990. }
  991. }
  992. }
  993. strcpy ( history_str, str );
  994. strcat ( history_str, old_pw );
  995. if ( i >= pwpolicy->pw_inhistory ) {
  996. /* replace the oldest password in history */
  997. values_replace[oldest] = history_str;
  998. values_replace[pwpolicy->pw_inhistory] = NULL;
  999. } else {
  1000. /* add old_pw at the end of password history */
  1001. values_replace[i] = history_str;
  1002. values_replace[++i]=NULL;
  1003. }
  1004. /* modify the attribute */
  1005. attribute.mod_type = "passwordHistory";
  1006. attribute.mod_op = LDAP_MOD_REPLACE;
  1007. attribute.mod_values = values_replace;
  1008. list_of_mods[0] = &attribute;
  1009. list_of_mods[1] = NULL;
  1010. pblock_init(&mod_pb);
  1011. slapi_pblock_get(pb, SLAPI_TXN, &txn);
  1012. slapi_modify_internal_set_pb_ext(&mod_pb, sdn, list_of_mods, NULL, NULL,
  1013. pw_get_componentID(), 0);
  1014. slapi_pblock_set(&mod_pb, SLAPI_TXN, txn);
  1015. slapi_modify_internal_pb(&mod_pb);
  1016. slapi_pblock_get(&mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  1017. if (res != LDAP_SUCCESS){
  1018. LDAPDebug2Args(LDAP_DEBUG_ANY,
  1019. "WARNING: passwordPolicy modify error %d on entry '%s'\n", res, dn);
  1020. }
  1021. pblock_done(&mod_pb);
  1022. slapi_ch_free((void **) &str );
  1023. slapi_ch_free((void **) &history_str );
  1024. slapi_entry_free( e );
  1025. delete_passwdPolicy(&pwpolicy);
  1026. return 0;
  1027. }
  1028. static
  1029. int pw_in_history( Slapi_Value **history_vals, const Slapi_Value *pw_val)
  1030. {
  1031. Slapi_Value **trimmed_history = NULL;
  1032. int num_history_vals = 0;
  1033. int i;
  1034. int ret = -1;
  1035. const char *pw_str = slapi_value_get_string(pw_val);
  1036. if (slapi_is_encoded((char*)pw_str)){
  1037. /* If the password is encoded, we just do a string match with all previous passwords */
  1038. for ( i = 0; history_vals[i] != NULL; i++){
  1039. const char * h_val = slapi_value_get_string(history_vals[i]);
  1040. if ( h_val != NULL &&
  1041. slapi_value_get_length(history_vals[i]) >= 14 )
  1042. {
  1043. int pos = 14;
  1044. if (h_val[pos] == 'Z')
  1045. pos++;
  1046. if (strcmp(&(h_val[pos]), pw_str) == 0){
  1047. /* Password found */
  1048. /* Let's just return */
  1049. return (0);
  1050. }
  1051. }
  1052. }
  1053. } else { /* Password is in clear */
  1054. /* Count the number of history vals. */
  1055. for (i = 0; history_vals[i] != NULL; i++ )
  1056. {
  1057. num_history_vals++;
  1058. }
  1059. /* Allocate the array */
  1060. trimmed_history = (Slapi_Value **)slapi_ch_calloc(num_history_vals + 1, sizeof(Slapi_Value *));
  1061. /* strip the timestamps */
  1062. for ( i = 0; history_vals[i] != NULL; i++ )
  1063. {
  1064. char *h_val = (char *)slapi_value_get_string(history_vals[i]);
  1065. size_t h_len = slapi_value_get_length(history_vals[i]);
  1066. /* Allocate a value and put it in the array. */
  1067. trimmed_history[i] = (Slapi_Value *)slapi_ch_calloc(1, sizeof(Slapi_Value));
  1068. if ( h_val != NULL &&
  1069. h_len >= 14 )
  1070. {
  1071. /* LP: With the new genTime, the password history format has changed */
  1072. int pos = 14;
  1073. if (h_val[pos] == 'Z')
  1074. pos++;
  1075. trimmed_history[i]->bv.bv_val = &(h_val[pos]);
  1076. trimmed_history[i]->bv.bv_len = h_len - pos;
  1077. } else {
  1078. trimmed_history[i]->bv.bv_val = NULL;
  1079. trimmed_history[i]->bv.bv_len = 0;
  1080. }
  1081. }
  1082. /* NULL terminate the array. */
  1083. trimmed_history[i] = NULL;
  1084. /* Check if the new password is in the trimmed history list. */
  1085. ret = slapi_pw_find_sv(trimmed_history, pw_val);
  1086. /* Free the trimmed values. */
  1087. for ( i = 0; trimmed_history[i] != NULL; i++ )
  1088. {
  1089. slapi_ch_free((void **)&trimmed_history[i]);
  1090. }
  1091. /* Free the array. */
  1092. slapi_ch_free((void **)&trimmed_history);
  1093. }
  1094. return ( ret );
  1095. }
  1096. int
  1097. slapi_add_pwd_control ( Slapi_PBlock *pb, char *arg, long time) {
  1098. LDAPControl new_ctrl;
  1099. char buf[12];
  1100. LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_add_pwd_control\n", 0, 0, 0 );
  1101. sprintf( buf, "%ld", time );
  1102. new_ctrl.ldctl_oid = arg;
  1103. new_ctrl.ldctl_value.bv_val = buf;
  1104. new_ctrl.ldctl_value.bv_len = strlen( buf );
  1105. new_ctrl.ldctl_iscritical = 0; /* 0 = false. */
  1106. if ( slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, &new_ctrl ) != 0 ) {
  1107. return( -1 );
  1108. }
  1109. return( 0 );
  1110. }
  1111. void
  1112. pw_mod_allowchange_aci(int pw_prohibit_change)
  1113. {
  1114. const Slapi_DN *base;
  1115. char *values_mod[2];
  1116. LDAPMod mod;
  1117. LDAPMod *mods[2];
  1118. Slapi_Backend *be;
  1119. char *cookie = NULL;
  1120. mods[0] = &mod;
  1121. mods[1] = NULL;
  1122. mod.mod_type = "aci";
  1123. mod.mod_values = values_mod;
  1124. if (pw_prohibit_change) {
  1125. mod.mod_op = LDAP_MOD_ADD;
  1126. }
  1127. else
  1128. {
  1129. /* Allow change password by default */
  1130. /* remove the aci if it is there. it is ok to fail */
  1131. mod.mod_op = LDAP_MOD_DELETE;
  1132. }
  1133. be = slapi_get_first_backend (&cookie);
  1134. /* Foreach backend... */
  1135. while (be)
  1136. {
  1137. /* Don't add aci on a chaining backend holding remote entries */
  1138. if((!be->be_private) && (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  1139. {
  1140. /* There's only One suffix per DB now. No need to loop */
  1141. base = slapi_be_getsuffix(be, 0);
  1142. if (base != NULL)
  1143. {
  1144. Slapi_PBlock pb;
  1145. int rc;
  1146. pblock_init (&pb);
  1147. values_mod[0] = DENY_PW_CHANGE_ACI;
  1148. values_mod[1] = NULL;
  1149. slapi_modify_internal_set_pb_ext(&pb, base, mods, NULL, NULL,
  1150. pw_get_componentID(), 0);
  1151. slapi_modify_internal_pb(&pb);
  1152. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1153. if (rc == LDAP_SUCCESS){
  1154. /*
  1155. ** Since we modified the acl
  1156. ** successfully, let's update the
  1157. ** in-memory acl list
  1158. */
  1159. slapi_pblock_set(&pb, SLAPI_TARGET_SDN, (void *)base);
  1160. plugin_call_acl_mods_update (&pb, LDAP_REQ_MODIFY );
  1161. }
  1162. pblock_done(&pb);
  1163. }
  1164. }
  1165. be = slapi_get_next_backend (cookie);
  1166. }
  1167. slapi_ch_free((void **) &cookie);
  1168. }
  1169. void
  1170. add_password_attrs( Slapi_PBlock *pb, Operation *op, Slapi_Entry *e )
  1171. {
  1172. struct berval bv;
  1173. struct berval *bvals[2];
  1174. Slapi_Attr **a, **next;
  1175. passwdPolicy *pwpolicy = NULL;
  1176. const char *dn = slapi_entry_get_ndn(e);
  1177. int has_allowchangetime = 0, has_expirationtime = 0;
  1178. time_t existing_exptime = 0;
  1179. LDAPDebug( LDAP_DEBUG_TRACE, "add_password_attrs\n", 0, 0, 0 );
  1180. bvals[0] = &bv;
  1181. bvals[1] = NULL;
  1182. /* If passwordexpirationtime is specified by the user, don't
  1183. try to assign the initial value */
  1184. for ( a = &e->e_attrs; *a != NULL; a = next ) {
  1185. if ( strcasecmp( (*a)->a_type,
  1186. "passwordexpirationtime" ) == 0) {
  1187. Slapi_Value *sval;
  1188. if (slapi_attr_first_value(*a, &sval) == 0) {
  1189. const struct berval *bv = slapi_value_get_berval(sval);
  1190. existing_exptime = parse_genTime(bv->bv_val);
  1191. }
  1192. has_expirationtime = 1;
  1193. } else if ( strcasecmp( (*a)->a_type,
  1194. "passwordallowchangetime" ) == 0) {
  1195. has_allowchangetime = 1;
  1196. }
  1197. next = &(*a)->a_next;
  1198. }
  1199. if ( has_allowchangetime && has_expirationtime ) {
  1200. return;
  1201. }
  1202. pwpolicy = new_passwdPolicy(pb, dn);
  1203. if ( !has_expirationtime &&
  1204. ( pwpolicy->pw_exp || pwpolicy->pw_must_change ) ) {
  1205. if ( pwpolicy->pw_must_change) {
  1206. /* must change password when first time logon */
  1207. bv.bv_val = format_genTime ( NO_TIME );
  1208. } else if ( pwpolicy->pw_exp ) {
  1209. bv.bv_val = format_genTime ( time_plus_sec ( current_time (),
  1210. pwpolicy->pw_maxage ) );
  1211. }
  1212. bv.bv_len = strlen( bv.bv_val );
  1213. slapi_entry_attr_merge( e, "passwordexpirationtime", bvals );
  1214. slapi_ch_free_string( &bv.bv_val );
  1215. }
  1216. /*
  1217. * If the password minimum age is not 0, calculate when the password
  1218. * is allowed to be changed again and store the result
  1219. * in passwordallowchangetime in the user's entry.
  1220. * If the password has expired, don't add passwordallowchangetime,
  1221. * otherwise if the user has grace logins, they can't be used to change
  1222. * the password if we set a passwordallowchangetime in the future.
  1223. */
  1224. if ( !has_allowchangetime && pwpolicy->pw_minage != 0 &&
  1225. (has_expirationtime && existing_exptime > current_time()) ) {
  1226. bv.bv_val = format_genTime ( time_plus_sec ( current_time (),
  1227. pwpolicy->pw_minage ) );
  1228. bv.bv_len = strlen( bv.bv_val );
  1229. slapi_entry_attr_merge( e, "passwordallowchangetime", bvals );
  1230. slapi_ch_free((void **) &bv.bv_val );
  1231. }
  1232. delete_passwdPolicy(&pwpolicy);
  1233. }
  1234. static int
  1235. check_trivial_words (Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Value **vals, char *attrtype,
  1236. int toklen, Slapi_Mods *smods )
  1237. {
  1238. Slapi_Attr *attr = NULL;
  1239. Slapi_Mod *smodp = NULL, *smod = NULL;
  1240. Slapi_ValueSet *vs = NULL;
  1241. Slapi_Value *valp = NULL;
  1242. struct berval *bvp = NULL;
  1243. int i, pwresponse_req = 0;
  1244. slapi_pblock_get ( pb, SLAPI_PWPOLICY, &pwresponse_req );
  1245. /* Get a list of present values for attrtype in the existing entry, if there is one */
  1246. if (e != NULL )
  1247. {
  1248. if ( (attr = attrlist_find(e->e_attrs, attrtype)) &&
  1249. (!valueset_isempty(&attr->a_present_values)) )
  1250. {
  1251. /* allocate and add present values to valueset */
  1252. slapi_attr_get_valueset( attr, &vs );
  1253. }
  1254. }
  1255. /* allocate new one if not allocated above by
  1256. slapi_attr_get_valueset */
  1257. if (!vs) {
  1258. vs = slapi_valueset_new();
  1259. }
  1260. /* Get a list of new values for attrtype from the operation */
  1261. if ( smods && (smod = slapi_mod_new()) )
  1262. {
  1263. for (smodp = slapi_mods_get_first_smod(smods, smod);
  1264. smodp != NULL; smodp = slapi_mods_get_next_smod(smods, smod) )
  1265. {
  1266. /* Operation has new values for attrtype */
  1267. if ( PL_strcasecmp(attrtype, slapi_mod_get_type(smodp)) == 0 )
  1268. {
  1269. /* iterate through smodp values and add them if they don't exist */
  1270. for ( bvp = slapi_mod_get_first_value( smodp ); bvp != NULL;
  1271. bvp = slapi_mod_get_next_value( smodp ) )
  1272. {
  1273. /* Add new value to valueset */
  1274. valp = slapi_value_new_berval( bvp );
  1275. slapi_valueset_add_value_ext( vs, valp, SLAPI_VALUE_FLAG_PASSIN );
  1276. valp = NULL;
  1277. }
  1278. }
  1279. }
  1280. /* Free smod */
  1281. slapi_mod_free(&smod);
  1282. smod = NULL;
  1283. smodp = NULL;
  1284. }
  1285. /* If valueset isn't empty, we need to check if the password contains the values */
  1286. if ( slapi_valueset_count(vs) != 0 )
  1287. {
  1288. for ( i = slapi_valueset_first_value( vs, &valp);
  1289. (i != -1) && (valp != NULL);
  1290. i = slapi_valueset_next_value( vs, i, &valp) )
  1291. {
  1292. /* If the value is smaller than the max token length,
  1293. * we don't need to check the password */
  1294. if ( ldap_utf8characters(slapi_value_get_string( valp )) < toklen )
  1295. continue;
  1296. /* See if the password contains the value */
  1297. if ( PL_strcasestr( slapi_value_get_string( vals[0] ),
  1298. slapi_value_get_string( valp ) ) )
  1299. {
  1300. if ( pwresponse_req == 1 )
  1301. {
  1302. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  1303. LDAP_PWPOLICY_INVALIDPWDSYNTAX );
  1304. }
  1305. pw_send_ldap_result ( pb,
  1306. LDAP_CONSTRAINT_VIOLATION, NULL,
  1307. "invalid password syntax - password based off of user entry", 0, NULL );
  1308. /* Free valueset */
  1309. slapi_valueset_free( vs );
  1310. return ( 1 );
  1311. }
  1312. }
  1313. }
  1314. /* Free valueset */
  1315. slapi_valueset_free( vs );
  1316. return ( 0 );
  1317. }
  1318. void
  1319. pw_add_allowchange_aci(Slapi_Entry *e, int pw_prohibit_change) {
  1320. char *aci_pw = NULL;
  1321. const char *aciattr = "aci";
  1322. aci_pw = slapi_ch_strdup(DENY_PW_CHANGE_ACI);
  1323. if (pw_prohibit_change) {
  1324. /* Add ACI */
  1325. slapi_entry_add_string(e, aciattr, aci_pw);
  1326. } else {
  1327. /* Remove ACI */
  1328. slapi_entry_delete_string(e, aciattr, aci_pw);
  1329. }
  1330. slapi_ch_free((void **) &aci_pw);
  1331. }
  1332. /* This function creates a passwdPolicy structure, loads it from either
  1333. * slapdFrontendconfig or the entry pointed by pwdpolicysubentry and
  1334. * returns the structure.
  1335. */
  1336. passwdPolicy *
  1337. new_passwdPolicy(Slapi_PBlock *pb, const char *dn)
  1338. {
  1339. Slapi_ValueSet *values = NULL;
  1340. Slapi_Entry *e = NULL, *pw_entry = NULL;
  1341. int type_name_disposition = 0;
  1342. char *actual_type_name = NULL;
  1343. int attr_free_flags = 0;
  1344. int rc=0;
  1345. passwdPolicy *pwdpolicy = NULL;
  1346. struct pw_scheme *pwdscheme = NULL;
  1347. Slapi_Attr *attr;
  1348. char *attr_name;
  1349. Slapi_Value **sval;
  1350. slapdFrontendConfig_t *slapdFrontendConfig;
  1351. char ebuf[ BUFSIZ ];
  1352. int optype = -1;
  1353. /* RFE - is there a way to make this work for non-existent entries
  1354. * when we don't pass in pb? We'll need to do this if we add support
  1355. * for password policy plug-ins. */
  1356. if (NULL == pb) {
  1357. LDAPDebug0Args(LDAP_DEBUG_ANY,
  1358. "new_passwdPolicy: NULL pblock was passed.\n");
  1359. return NULL;
  1360. }
  1361. slapdFrontendConfig = getFrontendConfig();
  1362. pwdpolicy = (passwdPolicy *)slapi_ch_calloc(1, sizeof(passwdPolicy));
  1363. slapi_pblock_get( pb, SLAPI_OPERATION_TYPE, &optype );
  1364. if (dn && (slapdFrontendConfig->pwpolicy_local == 1)) {
  1365. /* If we're doing an add, COS does not apply yet so we check
  1366. parents for the pwdpolicysubentry. We look only for virtual
  1367. attributes, because real ones are for single-target policy. */
  1368. if (optype == SLAPI_OPERATION_ADD) {
  1369. char *parentdn = slapi_ch_strdup(dn);
  1370. char *nextdn = NULL;
  1371. while ((nextdn = slapi_dn_parent( parentdn )) != NULL) {
  1372. if (((e = get_entry( pb, nextdn )) != NULL)) {
  1373. if ((slapi_vattr_values_get(e, "pwdpolicysubentry",
  1374. &values, &type_name_disposition, &actual_type_name,
  1375. SLAPI_VIRTUALATTRS_REQUEST_POINTERS |
  1376. SLAPI_VIRTUALATTRS_ONLY,
  1377. &attr_free_flags)) == 0) {
  1378. /* pwdpolicysubentry found! */
  1379. break;
  1380. } else {
  1381. /* Parent didn't have it, check grandparent... */
  1382. slapi_ch_free_string( &parentdn );
  1383. parentdn = nextdn;
  1384. slapi_entry_free( e );
  1385. e = NULL;
  1386. }
  1387. } else {
  1388. /* Reached the top without finding a pwdpolicysubentry. */
  1389. break;
  1390. }
  1391. }
  1392. slapi_ch_free_string( &parentdn );
  1393. slapi_ch_free_string( &nextdn );
  1394. /* If we're not doing an add, we look for the pwdpolicysubentry
  1395. attribute in the target entry itself. */
  1396. } else {
  1397. if ( (e = get_entry( pb, dn )) != NULL ) {
  1398. rc = slapi_vattr_values_get(e, "pwdpolicysubentry", &values,
  1399. &type_name_disposition, &actual_type_name,
  1400. SLAPI_VIRTUALATTRS_REQUEST_POINTERS, &attr_free_flags);
  1401. if (rc) {
  1402. values = NULL;
  1403. }
  1404. }
  1405. }
  1406. if (values != NULL) {
  1407. Slapi_Value *v = NULL;
  1408. const struct berval *bvp = NULL;
  1409. if ( ((rc = slapi_valueset_first_value( values, &v )) != -1) &&
  1410. ( bvp = slapi_value_get_berval( v )) != NULL ) {
  1411. if ( bvp != NULL ) {
  1412. /* we got the pwdpolicysubentry value */
  1413. pw_entry = get_entry ( pb, bvp->bv_val);
  1414. }
  1415. }
  1416. slapi_vattr_values_free(&values, &actual_type_name, attr_free_flags);
  1417. slapi_entry_free( e );
  1418. if ( pw_entry == NULL ) {
  1419. LDAPDebug(LDAP_DEBUG_ANY, "loading global password policy for %s"
  1420. "--local policy entry not found\n", escape_string(dn, ebuf),0,0);
  1421. goto done;
  1422. }
  1423. for (slapi_entry_first_attr(pw_entry, &attr); attr;
  1424. slapi_entry_next_attr(pw_entry, attr, &attr))
  1425. {
  1426. slapi_attr_get_type(attr, &attr_name);
  1427. if (!strcasecmp(attr_name, "passwordminage")) {
  1428. if ((sval = attr_get_present_values(attr))) {
  1429. pwdpolicy->pw_minage = slapi_value_get_timelong(*sval);
  1430. if (-1 == pwdpolicy->pw_minage) {
  1431. LDAPDebug2Args(LDAP_DEBUG_ANY,
  1432. "Password Policy Entry%s: Invalid passwordMinAge: %s\n",
  1433. slapi_entry_get_dn_const(pw_entry),
  1434. slapi_value_get_string(*sval));
  1435. }
  1436. }
  1437. }
  1438. else
  1439. if (!strcasecmp(attr_name, "passwordmaxage")) {
  1440. if ((sval = attr_get_present_values(attr))) {
  1441. pwdpolicy->pw_maxage = slapi_value_get_timelong(*sval);
  1442. if (-1 == pwdpolicy->pw_maxage) {
  1443. LDAPDebug2Args(LDAP_DEBUG_ANY,
  1444. "Password Policy Entry%s: Invalid passwordMaxAge: %s\n",
  1445. slapi_entry_get_dn_const(pw_entry),
  1446. slapi_value_get_string(*sval));
  1447. }
  1448. }
  1449. }
  1450. else
  1451. if (!strcasecmp(attr_name, "passwordwarning")) {
  1452. if ((sval = attr_get_present_values(attr))) {
  1453. pwdpolicy->pw_warning = slapi_value_get_timelong(*sval);
  1454. if (-1 == pwdpolicy->pw_warning) {
  1455. LDAPDebug2Args(LDAP_DEBUG_ANY,
  1456. "Password Policy Entry%s: Invalid passwordWarning: %s\n",
  1457. slapi_entry_get_dn_const(pw_entry),
  1458. slapi_value_get_string(*sval));
  1459. }
  1460. }
  1461. }
  1462. else
  1463. if (!strcasecmp(attr_name, "passwordhistory")) {
  1464. if ((sval = attr_get_present_values(attr))) {
  1465. pwdpolicy->pw_history =
  1466. pw_boolean_str2value(slapi_value_get_string(*sval));
  1467. }
  1468. }
  1469. else
  1470. if (!strcasecmp(attr_name, "passwordinhistory")) {
  1471. if ((sval = attr_get_present_values(attr))) {
  1472. pwdpolicy->pw_inhistory = slapi_value_get_int(*sval);
  1473. }
  1474. }
  1475. else
  1476. if (!strcasecmp(attr_name, "passwordlockout")) {
  1477. if ((sval = attr_get_present_values(attr))) {
  1478. pwdpolicy->pw_lockout =
  1479. pw_boolean_str2value(slapi_value_get_string(*sval));
  1480. }
  1481. }
  1482. else
  1483. if (!strcasecmp(attr_name, "passwordmaxfailure")) {
  1484. if ((sval = attr_get_present_values(attr))) {
  1485. pwdpolicy->pw_maxfailure = slapi_value_get_int(*sval);
  1486. }
  1487. }
  1488. else
  1489. if (!strcasecmp(attr_name, "passwordunlock")) {
  1490. if ((sval = attr_get_present_values(attr))) {
  1491. pwdpolicy->pw_unlock =
  1492. pw_boolean_str2value(slapi_value_get_string(*sval));
  1493. }
  1494. }
  1495. else
  1496. if (!strcasecmp(attr_name, "passwordlockoutduration")) {
  1497. if ((sval = attr_get_present_values(attr))) {
  1498. pwdpolicy->pw_lockduration = slapi_value_get_timelong(*sval);
  1499. }
  1500. }
  1501. else
  1502. if (!strcasecmp(attr_name, "passwordresetfailurecount")) {
  1503. if ((sval = attr_get_present_values(attr))) {
  1504. pwdpolicy->pw_resetfailurecount = slapi_value_get_long(*sval);
  1505. }
  1506. }
  1507. else
  1508. if (!strcasecmp(attr_name, "passwordchange")) {
  1509. if ((sval = attr_get_present_values(attr))) {
  1510. pwdpolicy->pw_change =
  1511. pw_boolean_str2value(slapi_value_get_string(*sval));
  1512. }
  1513. }
  1514. else
  1515. if (!strcasecmp(attr_name, "passwordmustchange")) {
  1516. if ((sval = attr_get_present_values(attr))) {
  1517. pwdpolicy->pw_must_change =
  1518. pw_boolean_str2value(slapi_value_get_string(*sval));
  1519. }
  1520. }
  1521. else
  1522. if (!strcasecmp(attr_name, "passwordchecksyntax")) {
  1523. if ((sval = attr_get_present_values(attr))) {
  1524. pwdpolicy->pw_syntax =
  1525. pw_boolean_str2value(slapi_value_get_string(*sval));
  1526. }
  1527. }
  1528. else
  1529. if (!strcasecmp(attr_name, "passwordminlength")) {
  1530. if ((sval = attr_get_present_values(attr))) {
  1531. pwdpolicy->pw_minlength = slapi_value_get_int(*sval);
  1532. }
  1533. }
  1534. else
  1535. if (!strcasecmp(attr_name, "passwordmindigits")) {
  1536. if ((sval = attr_get_present_values(attr))) {
  1537. pwdpolicy->pw_mindigits = slapi_value_get_int(*sval);
  1538. }
  1539. }
  1540. else
  1541. if (!strcasecmp(attr_name, "passwordminalphas")) {
  1542. if ((sval = attr_get_present_values(attr))) {
  1543. pwdpolicy->pw_minalphas = slapi_value_get_int(*sval);
  1544. }
  1545. }
  1546. else
  1547. if (!strcasecmp(attr_name, "passwordminuppers")) {
  1548. if ((sval = attr_get_present_values(attr))) {
  1549. pwdpolicy->pw_minuppers = slapi_value_get_int(*sval);
  1550. }
  1551. }
  1552. else
  1553. if (!strcasecmp(attr_name, "passwordminlowers")) {
  1554. if ((sval = attr_get_present_values(attr))) {
  1555. pwdpolicy->pw_minlowers = slapi_value_get_int(*sval);
  1556. }
  1557. }
  1558. else
  1559. if (!strcasecmp(attr_name, "passwordminspecials")) {
  1560. if ((sval = attr_get_present_values(attr))) {
  1561. pwdpolicy->pw_minspecials = slapi_value_get_int(*sval);
  1562. }
  1563. }
  1564. else
  1565. if (!strcasecmp(attr_name, "passwordmin8bit")) {
  1566. if ((sval = attr_get_present_values(attr))) {
  1567. pwdpolicy->pw_min8bit = slapi_value_get_int(*sval);
  1568. }
  1569. }
  1570. else
  1571. if (!strcasecmp(attr_name, "passwordmaxrepeats")) {
  1572. if ((sval = attr_get_present_values(attr))) {
  1573. pwdpolicy->pw_maxrepeats = slapi_value_get_int(*sval);
  1574. }
  1575. }
  1576. else
  1577. if (!strcasecmp(attr_name, "passwordmincategories")) {
  1578. if ((sval = attr_get_present_values(attr))) {
  1579. pwdpolicy->pw_mincategories = slapi_value_get_int(*sval);
  1580. }
  1581. }
  1582. else
  1583. if (!strcasecmp(attr_name, "passwordmintokenlength")) {
  1584. if ((sval = attr_get_present_values(attr))) {
  1585. pwdpolicy->pw_mintokenlength = slapi_value_get_int(*sval);
  1586. }
  1587. }
  1588. else
  1589. if (!strcasecmp(attr_name, "passwordexp")) {
  1590. if ((sval = attr_get_present_values(attr))) {
  1591. pwdpolicy->pw_exp =
  1592. pw_boolean_str2value(slapi_value_get_string(*sval));
  1593. }
  1594. }
  1595. else
  1596. if (!strcasecmp(attr_name, "passwordgracelimit")) {
  1597. if ((sval = attr_get_present_values(attr))) {
  1598. pwdpolicy->pw_gracelimit = slapi_value_get_int(*sval);
  1599. }
  1600. }
  1601. else
  1602. if (!strcasecmp(attr_name, "passwordstoragescheme")) {
  1603. if ((sval = attr_get_present_values(attr))) {
  1604. pwdpolicy->pw_storagescheme =
  1605. pw_name2scheme((char*)slapi_value_get_string(*sval));
  1606. }
  1607. }
  1608. } /* end of for() loop */
  1609. if (pw_entry) {
  1610. slapi_entry_free(pw_entry);
  1611. }
  1612. return pwdpolicy;
  1613. } else if ( e ) {
  1614. slapi_entry_free( e );
  1615. }
  1616. }
  1617. done:
  1618. /* If we are here, that means we need to load the passwdPolicy
  1619. * structure from slapdFrontendconfig
  1620. */
  1621. *pwdpolicy = slapdFrontendConfig->pw_policy;
  1622. pwdscheme = (struct pw_scheme *)slapi_ch_calloc(1, sizeof(struct pw_scheme));
  1623. *pwdscheme = *slapdFrontendConfig->pw_storagescheme;
  1624. pwdscheme->pws_name = strdup( slapdFrontendConfig->pw_storagescheme->pws_name );
  1625. pwdpolicy->pw_storagescheme = pwdscheme;
  1626. return pwdpolicy;
  1627. } /* End of new_passwdPolicy() */
  1628. void
  1629. delete_passwdPolicy( passwdPolicy **pwpolicy)
  1630. {
  1631. if (pwpolicy && *pwpolicy) {
  1632. free_pw_scheme( (*(*pwpolicy)).pw_storagescheme );
  1633. slapi_ch_free((void **)pwpolicy);
  1634. }
  1635. }
  1636. /*
  1637. * Encode the PWPOLICY RESPONSE control.
  1638. *
  1639. * Create a password policy response control,
  1640. * and add it to the PBlock to be returned to the client.
  1641. *
  1642. * Returns:
  1643. * success ( 0 )
  1644. * operationsError (1),
  1645. */
  1646. int
  1647. slapi_pwpolicy_make_response_control (Slapi_PBlock *pb, int seconds, int logins, ber_int_t error)
  1648. {
  1649. BerElement *ber= NULL;
  1650. struct berval *bvp = NULL;
  1651. int rc = -1;
  1652. /*
  1653. PasswordPolicyResponseValue ::= SEQUENCE {
  1654. warning [0] CHOICE OPTIONAL {
  1655. timeBeforeExpiration [0] INTEGER (0 .. maxInt),
  1656. graceLoginsRemaining [1] INTEGER (0 .. maxInt) }
  1657. error [1] ENUMERATED OPTIONAL {
  1658. passwordExpired (0),
  1659. accountLocked (1),
  1660. changeAfterReset (2),
  1661. passwordModNotAllowed (3),
  1662. mustSupplyOldPassword (4),
  1663. invalidPasswordSyntax (5),
  1664. passwordTooShort (6),
  1665. passwordTooYoung (7),
  1666. passwordInHistory (8) } }
  1667. */
  1668. LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_pwpolicy_make_response_control", 0, 0, 0 );
  1669. if ( ( ber = ber_alloc()) == NULL )
  1670. {
  1671. return rc;
  1672. }
  1673. rc = ber_printf( ber, "{" );
  1674. if ( seconds >= 0 || logins >= 0 ) {
  1675. if ( seconds >= 0 ) {
  1676. rc = ber_printf( ber, "t{ti}", LDAP_TAG_PWP_WARNING,
  1677. LDAP_TAG_PWP_SECSLEFT,
  1678. seconds );
  1679. }
  1680. else {
  1681. rc = ber_printf( ber, "t{ti}", LDAP_TAG_PWP_WARNING,
  1682. LDAP_TAG_PWP_GRCLOGINS,
  1683. logins );
  1684. }
  1685. }
  1686. if ( error >= 0 ) {
  1687. rc = ber_printf( ber, "te", LDAP_TAG_PWP_ERROR, error );
  1688. }
  1689. rc = ber_printf( ber, "}" );
  1690. if ( rc != -1 )
  1691. {
  1692. rc = ber_flatten( ber, &bvp );
  1693. }
  1694. ber_free( ber, 1 );
  1695. if ( rc != -1 )
  1696. {
  1697. LDAPControl new_ctrl = {0};
  1698. new_ctrl.ldctl_oid = LDAP_X_CONTROL_PWPOLICY_RESPONSE;
  1699. new_ctrl.ldctl_value = *bvp;
  1700. new_ctrl.ldctl_iscritical = 0;
  1701. rc= slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, &new_ctrl );
  1702. ber_bvfree(bvp);
  1703. }
  1704. LDAPDebug( LDAP_DEBUG_TRACE, "<= slapi_pwpolicy_make_response_control", 0, 0, 0 );
  1705. return (rc==-1?LDAP_OPERATIONS_ERROR:LDAP_SUCCESS);
  1706. }
  1707. static int
  1708. pw_boolean_str2value (const char *str)
  1709. {
  1710. if ( !strcasecmp(str, "true") ||
  1711. !strcasecmp(str, "on") ||
  1712. !strcasecmp(str, "1") ) {
  1713. return ( LDAP_ON );
  1714. }
  1715. if ( !strcasecmp(str, "false") ||
  1716. !strcasecmp(str, "off") ||
  1717. !strcasecmp(str, "0") ) {
  1718. return ( LDAP_OFF );
  1719. }
  1720. return (-1);
  1721. }
  1722. int
  1723. check_pw_duration_value( const char *attr_name, char *value,
  1724. long minval, long maxval, char *errorbuf )
  1725. {
  1726. int retVal = LDAP_SUCCESS;
  1727. long age;
  1728. age = parse_duration(value);
  1729. if (-1 == age) {
  1730. PR_snprintf ( errorbuf, BUFSIZ,
  1731. "password minimum age \"%s\" is invalid. ", value );
  1732. retVal = LDAP_CONSTRAINT_VIOLATION;
  1733. } else if (0 == strcasecmp(CONFIG_PW_LOCKDURATION_ATTRIBUTE, attr_name)) {
  1734. if ( (age <= 0) ||
  1735. (age > (MAX_ALLOWED_TIME_IN_SECS - current_time())) ||
  1736. ((-1 != minval) && (age < minval)) ||
  1737. ((-1 != maxval) && (age > maxval))) {
  1738. PR_snprintf ( errorbuf, BUFSIZ, "%s: \"%s\" seconds is invalid. ",
  1739. attr_name, value );
  1740. retVal = LDAP_CONSTRAINT_VIOLATION;
  1741. }
  1742. } else {
  1743. if ( (age < 0) ||
  1744. (age > (MAX_ALLOWED_TIME_IN_SECS - current_time())) ||
  1745. ((-1 != minval) && (age < minval)) ||
  1746. ((-1 != maxval) && (age > maxval))) {
  1747. PR_snprintf ( errorbuf, BUFSIZ, "%s: \"%s\" seconds is invalid. ",
  1748. attr_name, value );
  1749. retVal = LDAP_CONSTRAINT_VIOLATION;
  1750. }
  1751. }
  1752. return retVal;
  1753. }
  1754. int
  1755. check_pw_resetfailurecount_value( const char *attr_name, char *value, long minval, long maxval, char *errorbuf )
  1756. {
  1757. int retVal = LDAP_SUCCESS;
  1758. long duration = 0; /* in minutes */
  1759. /* in seconds */
  1760. duration = strtol (value, NULL, 0);
  1761. if ( duration < 0 || duration > (MAX_ALLOWED_TIME_IN_SECS - current_time()) ) {
  1762. PR_snprintf ( errorbuf, BUFSIZ,
  1763. "password reset count duration \"%s\" seconds is invalid. ",
  1764. value );
  1765. retVal = LDAP_CONSTRAINT_VIOLATION;
  1766. }
  1767. return retVal;
  1768. }
  1769. int
  1770. check_pw_storagescheme_value( const char *attr_name, char *value, long minval, long maxval, char *errorbuf )
  1771. {
  1772. int retVal = LDAP_SUCCESS;
  1773. struct pw_scheme *new_scheme = NULL;
  1774. char * scheme_list = NULL;
  1775. scheme_list = plugin_get_pwd_storage_scheme_list(PLUGIN_LIST_PWD_STORAGE_SCHEME);
  1776. new_scheme = pw_name2scheme(value);
  1777. if ( new_scheme == NULL) {
  1778. if ( scheme_list != NULL ) {
  1779. PR_snprintf ( errorbuf, BUFSIZ,
  1780. "%s: invalid scheme - %s. Valid schemes are: %s",
  1781. CONFIG_PW_STORAGESCHEME_ATTRIBUTE, value, scheme_list );
  1782. } else {
  1783. PR_snprintf ( errorbuf, BUFSIZ,
  1784. "%s: invalid scheme - %s (no pwdstorage scheme"
  1785. " plugin loaded)",
  1786. CONFIG_PW_STORAGESCHEME_ATTRIBUTE, value);
  1787. }
  1788. retVal = LDAP_CONSTRAINT_VIOLATION;
  1789. }
  1790. else if ( new_scheme->pws_enc == NULL )
  1791. {
  1792. /* For example: the NS-MTA-MD5 password scheme is for comparision only
  1793. and for backward compatibility with an Old Messaging Server that was
  1794. setting passwords in the directory already encrypted. The scheme cannot
  1795. and won't encrypt passwords if they are in clear. We don't take it
  1796. */
  1797. if ( scheme_list != NULL ) {
  1798. PR_snprintf ( errorbuf, BUFSIZ,
  1799. "%s: invalid encoding scheme - %s\nValid values are: %s\n",
  1800. CONFIG_PW_STORAGESCHEME_ATTRIBUTE, value, scheme_list );
  1801. }
  1802. retVal = LDAP_CONSTRAINT_VIOLATION;
  1803. }
  1804. free_pw_scheme(new_scheme);
  1805. slapi_ch_free_string(&scheme_list);
  1806. return retVal;
  1807. }
  1808. /* check_account_lock is called before bind opeation; this could be a pre-op. */
  1809. int
  1810. slapi_check_account_lock ( Slapi_PBlock *pb, Slapi_Entry * bind_target_entry, int pwresponse_req, int check_password_policy, int send_result) {
  1811. time_t unlock_time;
  1812. time_t cur_time;
  1813. char *cur_time_str = NULL;
  1814. char *accountUnlockTime;
  1815. passwdPolicy *pwpolicy = NULL;
  1816. char *dn = NULL;
  1817. /* kexcoff: account inactivation */
  1818. int rc = 0;
  1819. Slapi_ValueSet *values = NULL;
  1820. int type_name_disposition = 0;
  1821. char *actual_type_name = NULL;
  1822. int attr_free_flags = 0;
  1823. /* kexcoff - end */
  1824. if ( bind_target_entry == NULL )
  1825. return -1;
  1826. if(check_password_policy)
  1827. {
  1828. dn = slapi_entry_get_ndn(bind_target_entry);
  1829. pwpolicy = new_passwdPolicy(pb, dn);
  1830. }
  1831. /* kexcoff: account inactivation */
  1832. /* check if the entry is locked by nsAccountLock attribute - account inactivation feature */
  1833. rc = slapi_vattr_values_get(bind_target_entry, "nsAccountLock",
  1834. &values,
  1835. &type_name_disposition, &actual_type_name,
  1836. SLAPI_VIRTUALATTRS_REQUEST_POINTERS,
  1837. &attr_free_flags);
  1838. if ( rc == 0 && NULL != values )
  1839. {
  1840. Slapi_Value *v = NULL;
  1841. const struct berval *bvp = NULL;
  1842. if ( (slapi_valueset_first_value( values, &v ) != -1) &&
  1843. ( bvp = slapi_value_get_berval( v )) != NULL )
  1844. {
  1845. if ( (bvp != NULL) && (strcasecmp(bvp->bv_val, "true") == 0) )
  1846. {
  1847. /* account inactivated */
  1848. if (check_password_policy && pwresponse_req) {
  1849. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  1850. LDAP_PWPOLICY_ACCTLOCKED );
  1851. }
  1852. if (send_result)
  1853. send_ldap_result ( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  1854. "Account inactivated. Contact system administrator.",
  1855. 0, NULL );
  1856. slapi_vattr_values_free(&values, &actual_type_name, attr_free_flags);
  1857. goto locked;
  1858. }
  1859. } /* else, account "activated", keep on the process */
  1860. }
  1861. if ( values != NULL )
  1862. slapi_vattr_values_free(&values, &actual_type_name, attr_free_flags);
  1863. /* kexcoff - end */
  1864. /*
  1865. * Check if the password policy has to be checked or not
  1866. */
  1867. if ( !check_password_policy || pwpolicy->pw_lockout == 0 ) {
  1868. goto notlocked;
  1869. }
  1870. /*
  1871. * Check the attribute of the password policy
  1872. */
  1873. /* check if account is locked out. If so, send result and return 1 */
  1874. {
  1875. unsigned int maxfailure= pwpolicy->pw_maxfailure;
  1876. /* It's locked if passwordRetryCount >= maxfailure */
  1877. if ( slapi_entry_attr_get_uint(bind_target_entry,"passwordRetryCount") < maxfailure )
  1878. {
  1879. /* Not locked */
  1880. goto notlocked;
  1881. }
  1882. }
  1883. /* locked but maybe it's time to unlock it */
  1884. accountUnlockTime= slapi_entry_attr_get_charptr(bind_target_entry, "accountUnlockTime");
  1885. if (accountUnlockTime != NULL)
  1886. {
  1887. unlock_time = parse_genTime(accountUnlockTime);
  1888. slapi_ch_free((void **) &accountUnlockTime );
  1889. if ( pwpolicy->pw_unlock == 0 &&
  1890. unlock_time == NO_TIME ) {
  1891. /* account is locked forever. contact admin to reset */
  1892. if (pwresponse_req) {
  1893. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  1894. LDAP_PWPOLICY_ACCTLOCKED );
  1895. }
  1896. if (send_result)
  1897. send_ldap_result ( pb, LDAP_CONSTRAINT_VIOLATION, NULL,
  1898. "Exceed password retry limit. Contact system administrator to reset.",
  1899. 0, NULL );
  1900. goto locked;
  1901. }
  1902. cur_time = current_time();
  1903. cur_time_str = format_genTime( cur_time);
  1904. if ( difftime ( parse_genTime( cur_time_str ), unlock_time ) < 0 ) {
  1905. /* account is locked, cannot do anything */
  1906. if (pwresponse_req) {
  1907. slapi_pwpolicy_make_response_control ( pb, -1, -1,
  1908. LDAP_PWPOLICY_ACCTLOCKED );
  1909. }
  1910. if (send_result)
  1911. send_ldap_result ( pb, LDAP_CONSTRAINT_VIOLATION, NULL,
  1912. "Exceed password retry limit. Please try later.",
  1913. 0, NULL );
  1914. slapi_ch_free((void **) &cur_time_str );
  1915. goto locked;
  1916. }
  1917. slapi_ch_free((void **) &cur_time_str );
  1918. }
  1919. notlocked:
  1920. /* account is not locked. */
  1921. if(check_password_policy)
  1922. delete_passwdPolicy(&pwpolicy);
  1923. return ( 0 );
  1924. locked:
  1925. if(check_password_policy)
  1926. delete_passwdPolicy(&pwpolicy);
  1927. return (1);
  1928. }
  1929. /* The idea here is that these functions could allow us to have password
  1930. * policy plugins in the future. The plugins would register callbacks for these
  1931. * slapi functions that would be used here if any pwpolicy plugin is configured to
  1932. * be used. Right now, we just use the normal server password policy code since
  1933. * we don't have a pwpolicy plugin type. */
  1934. Slapi_PWPolicy *
  1935. slapi_get_pwpolicy(Slapi_DN *dn)
  1936. {
  1937. return ((Slapi_PWPolicy *)new_passwdPolicy(NULL, slapi_sdn_get_ndn(dn)));
  1938. }
  1939. void
  1940. slapi_pwpolicy_free(Slapi_PWPolicy *pwpolicy)
  1941. {
  1942. delete_passwdPolicy((passwdPolicy **)&pwpolicy);
  1943. }
  1944. int
  1945. slapi_pwpolicy_is_expired(Slapi_PWPolicy *pwpolicy, Slapi_Entry *e, time_t *expire_time, int *remaining_grace)
  1946. {
  1947. int is_expired = 0;
  1948. if (pwpolicy && e) {
  1949. /* If password expiration is enabled in the policy,
  1950. * check if the password has expired. */
  1951. if (pwpolicy->pw_exp == 1) {
  1952. char *expiration_val = NULL;
  1953. time_t _expire_time;
  1954. double diff_t = 0;
  1955. char *cur_time_str = NULL;
  1956. time_t cur_time;
  1957. expiration_val = slapi_entry_attr_get_charptr(e, "passwordExpirationTime");
  1958. if (expiration_val) {
  1959. _expire_time = parse_genTime(expiration_val);
  1960. cur_time = current_time();
  1961. cur_time_str = format_genTime(cur_time);
  1962. if ((_expire_time != NO_TIME) && (_expire_time != NOT_FIRST_TIME) &&
  1963. ((diff_t = difftime (_expire_time, parse_genTime(cur_time_str))) <= 0)) {
  1964. is_expired = 1;
  1965. }
  1966. if (is_expired) {
  1967. if (remaining_grace) {
  1968. /* Fill in the number of remaining grace logins */
  1969. int grace_attempts = 0;
  1970. grace_attempts = slapi_entry_attr_get_int(e, "passwordGraceUserTime");
  1971. if (pwpolicy->pw_gracelimit > grace_attempts) {
  1972. *remaining_grace = pwpolicy->pw_gracelimit - grace_attempts;
  1973. } else {
  1974. *remaining_grace = 0;
  1975. }
  1976. }
  1977. } else if (expire_time) {
  1978. /* Fill in the expiration time */
  1979. if ((_expire_time != NO_TIME) && (_expire_time != NOT_FIRST_TIME)) {
  1980. *expire_time = _expire_time;
  1981. } else {
  1982. *expire_time = (time_t)0;
  1983. }
  1984. }
  1985. slapi_ch_free_string(&cur_time_str);
  1986. }
  1987. } else if (expire_time) {
  1988. /* Passwords never expire */
  1989. *expire_time = (time_t)0;
  1990. }
  1991. }
  1992. return is_expired;
  1993. }
  1994. int
  1995. slapi_pwpolicy_is_locked(Slapi_PWPolicy *pwpolicy, Slapi_Entry *e, time_t *unlock_time)
  1996. {
  1997. int is_locked = 0;
  1998. if (pwpolicy && e) {
  1999. /* Check if account is locked */
  2000. if ( pwpolicy->pw_lockout == 1) {
  2001. if (slapi_entry_attr_get_uint(e, "passwordRetryCount") >= pwpolicy->pw_maxfailure) {
  2002. is_locked = 1;
  2003. }
  2004. }
  2005. if (is_locked) {
  2006. /* See if it's time for the account to be unlocked */
  2007. char *unlock_time_str = NULL;
  2008. char *cur_time_str = NULL;
  2009. time_t _unlock_time = (time_t)0;
  2010. time_t cur_time;
  2011. unlock_time_str = slapi_entry_attr_get_charptr(e, "accountUnlockTime");
  2012. if (unlock_time_str) {
  2013. _unlock_time = parse_genTime(unlock_time_str);
  2014. }
  2015. if ((pwpolicy->pw_unlock == 0) && (_unlock_time == NO_TIME)) {
  2016. /* Account is locked forever */
  2017. if (unlock_time) {
  2018. *unlock_time = (time_t)0;
  2019. }
  2020. } else {
  2021. cur_time = current_time();
  2022. cur_time_str = format_genTime(cur_time);
  2023. if (difftime(parse_genTime(cur_time_str), _unlock_time) < 0) {
  2024. /* Account is not due to be unlocked yet.
  2025. * Fill in the unlock time. */
  2026. if (unlock_time) {
  2027. *unlock_time = _unlock_time;
  2028. }
  2029. } else {
  2030. /* Account is due to be unlocked */
  2031. is_locked = 0;
  2032. }
  2033. slapi_ch_free_string(&cur_time_str);
  2034. }
  2035. }
  2036. }
  2037. return is_locked;
  2038. }
  2039. int
  2040. slapi_pwpolicy_is_reset(Slapi_PWPolicy *pwpolicy, Slapi_Entry *e)
  2041. {
  2042. int is_reset = 0;
  2043. if (pwpolicy && e) {
  2044. /* Check if password was reset and needs to be changed */
  2045. if (pwpolicy->pw_must_change) {
  2046. char *expiration_val = 0;
  2047. time_t expire_time = (time_t)0;
  2048. expiration_val = slapi_entry_attr_get_charptr(e, "passwordExpirationTime");
  2049. if (expiration_val) {
  2050. expire_time = parse_genTime(expiration_val);
  2051. if (expire_time == NO_TIME) {
  2052. is_reset = 1;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. return is_reset;
  2058. }