pw_retry.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /* pw_retry.c
  39. */
  40. #include <time.h>
  41. #include "slap.h"
  42. /****************************************************************************/
  43. /* prototypes */
  44. /****************************************************************************/
  45. /* Slapi_Entry *get_entry ( Slapi_PBlock *pb, const char *dn ); */
  46. static void set_retry_cnt ( Slapi_PBlock *pb, int count);
  47. static void set_retry_cnt_and_time ( Slapi_PBlock *pb, int count, time_t cur_time);
  48. /*
  49. * update_pw_retry() is called when bind operation fails
  50. * with LDAP_INVALID_CREDENTIALS (in backend bind.c ).
  51. * It checks to see if the retry count can be reset,
  52. * increments retry count, and then check if need to lock the acount.
  53. * To have a global password policy, these mods should be chained to the
  54. * master, and not applied locally. If they are applied locally, they should
  55. * not get replicated from master...
  56. */
  57. int update_pw_retry ( Slapi_PBlock *pb )
  58. {
  59. Slapi_Entry *e;
  60. int retry_cnt=0;
  61. time_t reset_time;
  62. time_t cur_time;
  63. char *cur_time_str = NULL;
  64. char *retryCountResetTime;
  65. int passwordRetryCount;
  66. /* get the entry */
  67. e = get_entry ( pb, NULL );
  68. if ( e == NULL ) {
  69. return ( 1 );
  70. }
  71. cur_time = current_time();
  72. /* check if the retry count can be reset. */
  73. retryCountResetTime= slapi_entry_attr_get_charptr(e, "retryCountResetTime");
  74. if(retryCountResetTime!=NULL)
  75. {
  76. reset_time = parse_genTime (retryCountResetTime);
  77. slapi_ch_free((void **) &retryCountResetTime );
  78. cur_time_str = format_genTime ( cur_time );
  79. if ( difftime ( parse_genTime( cur_time_str ), reset_time) >= 0 )
  80. {
  81. /* set passwordRetryCount to 1 */
  82. /* reset retryCountResetTime */
  83. set_retry_cnt_and_time ( pb, 1, cur_time );
  84. slapi_ch_free((void **) &cur_time_str );
  85. slapi_entry_free( e );
  86. return ( 0 ); /* success */
  87. } else {
  88. slapi_ch_free((void **) &cur_time_str );
  89. }
  90. } else {
  91. /* initialize passwordRetryCount and retryCountResetTime */
  92. set_retry_cnt_and_time ( pb, 1, cur_time );
  93. slapi_entry_free( e );
  94. return ( 0 ); /* success */
  95. }
  96. passwordRetryCount = slapi_entry_attr_get_int(e, "passwordRetryCount");
  97. if (passwordRetryCount >= 0)
  98. {
  99. retry_cnt = passwordRetryCount + 1;
  100. if ( retry_cnt == 1 ) {
  101. /* set retryCountResetTime */
  102. set_retry_cnt_and_time ( pb, retry_cnt, cur_time );
  103. } else {
  104. /* set passwordRetryCount to retry_cnt */
  105. set_retry_cnt ( pb, retry_cnt );
  106. }
  107. }
  108. slapi_entry_free( e );
  109. return 0; /* success */
  110. }
  111. static
  112. void set_retry_cnt_and_time ( Slapi_PBlock *pb, int count, time_t cur_time ) {
  113. char *dn;
  114. Slapi_Mods smods;
  115. time_t reset_time;
  116. char *timestr;
  117. passwdPolicy *pwpolicy = NULL;
  118. slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  119. pwpolicy = new_passwdPolicy(pb, dn);
  120. slapi_mods_init(&smods, 0);
  121. reset_time = time_plus_sec ( cur_time,
  122. pwpolicy->pw_resetfailurecount );
  123. timestr = format_genTime ( reset_time );
  124. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "retryCountResetTime", timestr);
  125. slapi_ch_free((void **)&timestr);
  126. set_retry_cnt_mods(pb, &smods, count);
  127. pw_apply_mods(dn, &smods);
  128. slapi_mods_done(&smods);
  129. delete_passwdPolicy(&pwpolicy);
  130. }
  131. void set_retry_cnt_mods(Slapi_PBlock *pb, Slapi_Mods *smods, int count)
  132. {
  133. char *timestr;
  134. time_t unlock_time;
  135. char retry_cnt[8]; /* 1-65535 */
  136. char *dn = NULL;
  137. passwdPolicy *pwpolicy = NULL;
  138. slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  139. pwpolicy = new_passwdPolicy(pb, dn);
  140. if (smods) {
  141. sprintf ( retry_cnt, "%d", count );
  142. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, "passwordRetryCount", retry_cnt);
  143. /* lock account if reache retry limit */
  144. if ( count >= pwpolicy->pw_maxfailure ) {
  145. /* Remove lock_account function to perform all mods at once */
  146. /* lock_account ( pb ); */
  147. /* reach the retry limit, lock the account */
  148. if ( pwpolicy->pw_unlock == 0 ) {
  149. /* lock until admin reset password */
  150. unlock_time = NO_TIME;
  151. } else {
  152. unlock_time = time_plus_sec ( current_time(),
  153. pwpolicy->pw_lockduration );
  154. }
  155. timestr= format_genTime ( unlock_time );
  156. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, "accountUnlockTime", timestr);
  157. slapi_ch_free((void **)&timestr);
  158. }
  159. }
  160. delete_passwdPolicy(&pwpolicy);
  161. return;
  162. }
  163. static
  164. void set_retry_cnt ( Slapi_PBlock *pb, int count) {
  165. char *dn;
  166. Slapi_Mods smods;
  167. slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  168. slapi_mods_init(&smods, 0);
  169. set_retry_cnt_mods(pb, &smods, count);
  170. pw_apply_mods(dn, &smods);
  171. slapi_mods_done(&smods);
  172. }
  173. Slapi_Entry *get_entry ( Slapi_PBlock *pb, const char *dn)
  174. {
  175. int search_result = 0;
  176. Slapi_Entry *retentry = NULL;
  177. Slapi_DN sdn;
  178. if ( dn == NULL ) {
  179. char *t;
  180. slapi_pblock_get( pb, SLAPI_TARGET_DN, &t );
  181. dn= t;
  182. }
  183. slapi_sdn_init_dn_byref(&sdn, dn);
  184. if ((search_result = slapi_search_internal_get_entry(&sdn, NULL, &retentry, pw_get_componentID())) != LDAP_SUCCESS){
  185. LDAPDebug (LDAP_DEBUG_TRACE, "WARNING: 'get_entry' can't find entry '%s', err %d\n", dn, search_result, 0);
  186. }
  187. slapi_sdn_done(&sdn);
  188. return retentry;
  189. }
  190. void pw_apply_mods(const char *dn, Slapi_Mods *mods)
  191. {
  192. Slapi_PBlock pb;
  193. int res;
  194. if (mods && (slapi_mods_get_num_mods(mods) > 0))
  195. {
  196. pblock_init(&pb);
  197. slapi_modify_internal_set_pb (&pb, dn,
  198. slapi_mods_get_ldapmods_byref(mods),
  199. NULL, /* Controls */
  200. NULL, /* UniqueID */
  201. pw_get_componentID(), /* PluginID */
  202. 0); /* Flags */
  203. slapi_modify_internal_pb (&pb);
  204. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  205. if (res != LDAP_SUCCESS){
  206. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: passwordPolicy modify error %d on entry '%s'\n",
  207. res, dn, 0);
  208. }
  209. pblock_done(&pb);
  210. }
  211. return;
  212. }
  213. /* Handle the component ID for the password policy */
  214. static struct slapi_componentid * pw_componentid = NULL;
  215. void pw_set_componentID(struct slapi_componentid *cid)
  216. {
  217. pw_componentid = cid;
  218. }
  219. struct slapi_componentid * pw_get_componentID()
  220. {
  221. return pw_componentid;
  222. }