]> git.sur5r.net Git - openldap/blob - libraries/libldap/ppolicy.c
cleanup
[openldap] / libraries / libldap / ppolicy.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004-2006 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
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31
32 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
33
34 #define PPOLICY_WARNING 0xa0L
35 #define PPOLICY_ERROR 0xa1L
36
37 #define PPOLICY_EXPIRE 0xa0L
38 #define PPOLICY_GRACE  0xa1L
39
40 /*---
41    ldap_create_passwordpolicy_control
42    
43    Create and encode the Password Policy Request
44
45    ld        (IN)  An LDAP session handle, as obtained from a call to
46                                    ldap_init().
47    
48    ctrlp     (OUT) A result parameter that will be assigned the address
49                                    of an LDAPControl structure that contains the 
50                                    passwordPolicyRequest control created by this function.
51                                    The memory occupied by the LDAPControl structure
52                                    SHOULD be freed when it is no longer in use by
53                                    calling ldap_control_free().
54                                           
55    
56    There is no control value for a password policy request
57  ---*/
58
59 int
60 ldap_create_passwordpolicy_control( LDAP *ld,
61                                     LDAPControl **ctrlp )
62 {
63         BerElement *ber;
64
65         assert( ld != NULL );
66         assert( LDAP_VALID( ld ) );
67         assert( ctrlp != NULL );
68
69         if ((ber = ldap_alloc_ber_with_options(ld)) == NULL) {
70                 ld->ld_errno = LDAP_NO_MEMORY;
71                 return(LDAP_NO_MEMORY);
72         }
73
74         ld->ld_errno = ldap_create_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
75                 ber, 0, ctrlp);
76
77         ber_free(ber, 1);
78         return(ld->ld_errno);
79 }
80
81
82 /*---
83    ldap_parse_passwordpolicy_control
84    
85    Decode the passwordPolicyResponse control and return information.
86
87    ld           (IN)   An LDAP session handle.
88    
89    ctrl         (IN)   The address of an
90                                            LDAPControl structure, typically obtained 
91                                            by a call to ldap_find_control().
92
93    exptimep     (OUT)  This result parameter is filled in with the number of seconds before
94                                            the password will expire, if expiration is imminent
95                                            (imminency defined by the password policy). If expiration
96                                            is not imminent, the value is set to -1.
97
98    gracep       (OUT)  This result parameter is filled in with the number of grace logins after
99                                            the password has expired, before no further login attempts
100                                            will be allowed.
101
102    errorcodep   (OUT)  This result parameter is filled in with the error code of the password operation
103                                            If no error was detected, this error is set to PP_noError.
104    
105    Ber encoding
106    
107    PasswordPolicyResponseValue ::= SEQUENCE {
108        warning [0] CHOICE {
109            timeBeforeExpiration [0] INTEGER (0 .. maxInt),
110            graceLoginsRemaining [1] INTEGER (0 .. maxInt) } OPTIONAL
111        error [1] ENUMERATED {
112            passwordExpired        (0),
113            accountLocked          (1),
114            changeAfterReset       (2),
115            passwordModNotAllowed  (3),
116            mustSupplyOldPassword  (4),
117            invalidPasswordSyntax  (5),
118            passwordTooShort       (6),
119            passwordTooYoung       (7),
120            passwordInHistory      (8) } OPTIONAL }
121            
122 ---*/
123
124 int
125 ldap_parse_passwordpolicy_control(
126         LDAP           *ld,
127         LDAPControl    *ctrl,
128         ber_int_t      *expirep,
129         ber_int_t      *gracep,
130         LDAPPasswordPolicyError *errorp )
131 {
132         BerElement  *ber;
133         int exp = -1, grace = -1;
134         ber_tag_t tag;
135         ber_len_t berLen;
136         char *last;
137         int err = PP_noError;
138         
139         assert( ld != NULL );
140         assert( LDAP_VALID( ld ) );
141         assert( ctrl != NULL );
142
143         /* Create a BerElement from the berval returned in the control. */
144         ber = ber_init(&ctrl->ldctl_value);
145
146         if (ber == NULL) {
147                 ld->ld_errno = LDAP_NO_MEMORY;
148                 return(ld->ld_errno);
149         }
150
151         tag = ber_peek_tag( ber, &berLen );
152         if (tag != LBER_SEQUENCE) goto exit;
153
154         for( tag = ber_first_element( ber, &berLen, &last );
155                 tag != LBER_DEFAULT;
156                 tag = ber_next_element( ber, &berLen, last ) )
157         {
158                 switch (tag) {
159                 case PPOLICY_WARNING:
160                         ber_skip_tag(ber, &berLen );
161                         tag = ber_peek_tag( ber, &berLen );
162                         switch( tag ) {
163                         case PPOLICY_EXPIRE:
164                                 if (ber_get_int( ber, &exp ) == LBER_DEFAULT) goto exit;
165                                 break;
166                         case PPOLICY_GRACE:
167                                 if (ber_get_int( ber, &grace ) == LBER_DEFAULT) goto exit;
168                                 break;
169                         default:
170                                 goto exit;
171                         }
172                         break;
173                 case PPOLICY_ERROR:
174                         if (ber_get_enum( ber, &err ) == LBER_DEFAULT) goto exit;
175                         break;
176                 default:
177                         goto exit;
178                 }
179         }
180
181         ber_free(ber, 1);
182
183         /* Return data to the caller for items that were requested. */
184         if (expirep) *expirep = exp;
185         if (gracep) *gracep = grace;
186         if (errorp) *errorp = err;
187         
188         ld->ld_errno = LDAP_SUCCESS;
189         return(ld->ld_errno);
190
191   exit:
192         ber_free(ber, 1);
193         ld->ld_errno = LDAP_DECODING_ERROR;
194         return(ld->ld_errno);
195 }
196
197 const char *
198 ldap_passwordpolicy_err2txt( LDAPPasswordPolicyError err )
199 {
200         switch(err) {
201         case PP_passwordExpired: return "Password expired";
202         case PP_accountLocked: return "Account locked";
203         case PP_changeAfterReset: return "Password must be changed";
204         case PP_passwordModNotAllowed: return "Policy prevents password modification";
205         case PP_mustSupplyOldPassword: return "Policy requires old password in order to change password";
206         case PP_insufficientPasswordQuality: return "Password fails quality checks";
207         case PP_passwordTooShort: return "Password is too short for policy";
208         case PP_passwordTooYoung: return "Password has been changed too recently";
209         case PP_passwordInHistory: return "New password is in list of old passwords";
210         case PP_noError: return "No error";
211         default: return "Unknown error code";
212         }
213 }
214
215 #endif /* LDAP_CONTROL_PASSWORDPOLICYREQUEST */