pw_retry.c 8.4 KB

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