pw.c 54 KB

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