ppolicy.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 2004-2022 The OpenLDAP Foundation.
  5. * Portions Copyright 2004 Hewlett-Packard Company.
  6. * Portions Copyright 2004 Howard Chu, Symas Corp.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted only as authorized by the OpenLDAP
  11. * Public License.
  12. *
  13. * A copy of this license is available in the file LICENSE in the
  14. * top-level directory of the distribution or, alternatively, at
  15. * <http://www.OpenLDAP.org/license.html>.
  16. */
  17. /* ACKNOWLEDGEMENTS:
  18. * This work was developed by Howard Chu for inclusion in
  19. * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
  20. * This work was sponsored by the Hewlett-Packard Company.
  21. */
  22. #include "portable.h"
  23. #include <stdio.h>
  24. #include <ac/stdlib.h>
  25. #include <ac/string.h>
  26. #include <ac/time.h>
  27. #include "ldap-int.h"
  28. #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
  29. /* IMPLICIT TAGS, all context-specific */
  30. #define PPOLICY_WARNING 0xa0L /* constructed + 0 */
  31. #define PPOLICY_ERROR 0x81L /* primitive + 1 */
  32. #define PPOLICY_EXPIRE 0x80L /* primitive + 0 */
  33. #define PPOLICY_GRACE 0x81L /* primitive + 1 */
  34. /*---
  35. ldap_create_passwordpolicy_control
  36. Create and encode the Password Policy Request
  37. ld (IN) An LDAP session handle, as obtained from a call to
  38. ldap_init().
  39. ctrlp (OUT) A result parameter that will be assigned the address
  40. of an LDAPControl structure that contains the
  41. passwordPolicyRequest control created by this function.
  42. The memory occupied by the LDAPControl structure
  43. SHOULD be freed when it is no longer in use by
  44. calling ldap_control_free().
  45. There is no control value for a password policy request
  46. ---*/
  47. int
  48. ldap_create_passwordpolicy_control( LDAP *ld,
  49. LDAPControl **ctrlp )
  50. {
  51. assert( ld != NULL );
  52. assert( LDAP_VALID( ld ) );
  53. assert( ctrlp != NULL );
  54. ld->ld_errno = ldap_control_create( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
  55. 0, NULL, 0, ctrlp );
  56. return ld->ld_errno;
  57. }
  58. /*---
  59. ldap_parse_passwordpolicy_control
  60. Decode the passwordPolicyResponse control and return information.
  61. ld (IN) An LDAP session handle.
  62. ctrl (IN) The address of an
  63. LDAPControl structure, either obtained
  64. by running through the list of response controls or
  65. by a call to ldap_control_find().
  66. exptimep (OUT) This result parameter is filled in with the number of seconds before
  67. the password will expire, if expiration is imminent
  68. (imminency defined by the password policy). If expiration
  69. is not imminent, the value is set to -1.
  70. gracep (OUT) This result parameter is filled in with the number of grace logins after
  71. the password has expired, before no further login attempts
  72. will be allowed.
  73. errorcodep (OUT) This result parameter is filled in with the error code of the password operation
  74. If no error was detected, this error is set to PP_noError.
  75. Ber encoding
  76. PasswordPolicyResponseValue ::= SEQUENCE {
  77. warning [0] CHOICE {
  78. timeBeforeExpiration [0] INTEGER (0 .. maxInt),
  79. graceLoginsRemaining [1] INTEGER (0 .. maxInt) } OPTIONAL
  80. error [1] ENUMERATED {
  81. passwordExpired (0),
  82. accountLocked (1),
  83. changeAfterReset (2),
  84. passwordModNotAllowed (3),
  85. mustSupplyOldPassword (4),
  86. invalidPasswordSyntax (5),
  87. passwordTooShort (6),
  88. passwordTooYoung (7),
  89. passwordInHistory (8) } OPTIONAL }
  90. ---*/
  91. int
  92. ldap_parse_passwordpolicy_control(
  93. LDAP *ld,
  94. LDAPControl *ctrl,
  95. ber_int_t *expirep,
  96. ber_int_t *gracep,
  97. LDAPPasswordPolicyError *errorp )
  98. {
  99. BerElement *ber;
  100. int exp = -1, grace = -1;
  101. ber_tag_t tag;
  102. ber_len_t berLen;
  103. char *last;
  104. int err = PP_noError;
  105. assert( ld != NULL );
  106. assert( LDAP_VALID( ld ) );
  107. assert( ctrl != NULL );
  108. if ( !ctrl->ldctl_value.bv_val ) {
  109. ld->ld_errno = LDAP_DECODING_ERROR;
  110. return(ld->ld_errno);
  111. }
  112. /* Create a BerElement from the berval returned in the control. */
  113. ber = ber_init(&ctrl->ldctl_value);
  114. if (ber == NULL) {
  115. ld->ld_errno = LDAP_NO_MEMORY;
  116. return(ld->ld_errno);
  117. }
  118. tag = ber_peek_tag( ber, &berLen );
  119. if (tag != LBER_SEQUENCE) goto exit;
  120. for( tag = ber_first_element( ber, &berLen, &last );
  121. tag != LBER_DEFAULT;
  122. tag = ber_next_element( ber, &berLen, last ) )
  123. {
  124. switch (tag) {
  125. case PPOLICY_WARNING:
  126. ber_skip_tag(ber, &berLen );
  127. tag = ber_peek_tag( ber, &berLen );
  128. switch( tag ) {
  129. case PPOLICY_EXPIRE:
  130. if (ber_get_int( ber, &exp ) == LBER_DEFAULT) goto exit;
  131. break;
  132. case PPOLICY_GRACE:
  133. if (ber_get_int( ber, &grace ) == LBER_DEFAULT) goto exit;
  134. break;
  135. default:
  136. goto exit;
  137. }
  138. break;
  139. case PPOLICY_ERROR:
  140. if (ber_get_enum( ber, &err ) == LBER_DEFAULT) goto exit;
  141. break;
  142. default:
  143. goto exit;
  144. }
  145. }
  146. ber_free(ber, 1);
  147. /* Return data to the caller for items that were requested. */
  148. if (expirep) *expirep = exp;
  149. if (gracep) *gracep = grace;
  150. if (errorp) *errorp = err;
  151. ld->ld_errno = LDAP_SUCCESS;
  152. return(ld->ld_errno);
  153. exit:
  154. ber_free(ber, 1);
  155. ld->ld_errno = LDAP_DECODING_ERROR;
  156. return(ld->ld_errno);
  157. }
  158. const char *
  159. ldap_passwordpolicy_err2txt( LDAPPasswordPolicyError err )
  160. {
  161. switch(err) {
  162. case PP_passwordExpired: return "Password expired";
  163. case PP_accountLocked: return "Account locked";
  164. case PP_changeAfterReset: return "Password must be changed";
  165. case PP_passwordModNotAllowed: return "Policy prevents password modification";
  166. case PP_mustSupplyOldPassword: return "Policy requires old password in order to change password";
  167. case PP_insufficientPasswordQuality: return "Password fails quality checks";
  168. case PP_passwordTooShort: return "Password is too short for policy";
  169. case PP_passwordTooYoung: return "Password has been changed too recently";
  170. case PP_passwordInHistory: return "New password is in list of old passwords";
  171. case PP_passwordTooLong: return "Password is too long for policy";
  172. case PP_noError: return "No error";
  173. default: return "Unknown error code";
  174. }
  175. }
  176. #endif /* LDAP_CONTROL_PASSWORDPOLICYREQUEST */
  177. #ifdef LDAP_CONTROL_X_PASSWORD_EXPIRING
  178. int
  179. ldap_parse_password_expiring_control(
  180. LDAP *ld,
  181. LDAPControl *ctrl,
  182. long *secondsp )
  183. {
  184. long seconds = 0;
  185. char buf[sizeof("-2147483648")];
  186. char *next;
  187. assert( ld != NULL );
  188. assert( LDAP_VALID( ld ) );
  189. assert( ctrl != NULL );
  190. if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ||
  191. ctrl->ldctl_value.bv_len >= sizeof(buf) ) {
  192. ld->ld_errno = LDAP_DECODING_ERROR;
  193. return(ld->ld_errno);
  194. }
  195. memcpy( buf, ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len );
  196. buf[ctrl->ldctl_value.bv_len] = '\0';
  197. seconds = strtol( buf, &next, 10 );
  198. if ( next == buf || next[0] != '\0' ) goto exit;
  199. if ( secondsp != NULL ) {
  200. *secondsp = seconds;
  201. }
  202. ld->ld_errno = LDAP_SUCCESS;
  203. return(ld->ld_errno);
  204. exit:
  205. ld->ld_errno = LDAP_DECODING_ERROR;
  206. return(ld->ld_errno);
  207. }
  208. #endif /* LDAP_CONTROL_X_PASSWORD_EXPIRING */