1
0

pw.c 62 KB

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