]> git.sur5r.net Git - openldap/blob - libraries/libldap/passwd.c
7fbc0cf900be4a40e581b44a0a66f2ac225e6f91
[openldap] / libraries / libldap / passwd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22
23 #include "ldap-int.h"
24
25 /*
26  * LDAP Password Modify (Extended) Operation <RFC 3062>
27  */
28
29 int ldap_parse_passwd(
30         LDAP *ld,
31         LDAPMessage *res,
32         struct berval *newpasswd )
33 {
34         int rc;
35         char *retoid = NULL;
36         struct berval *retdata;
37
38         assert( ld != NULL );
39         assert( LDAP_VALID( ld ) );
40         assert( res != NULL );
41         assert( newpasswd != NULL );
42
43         newpasswd->bv_val = NULL;
44         newpasswd->bv_len = 0;
45
46         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 );
47
48         if( rc != LDAP_SUCCESS ) {
49                 return rc;
50         }
51
52         if( retdata != NULL ) {
53                 ber_tag_t tag;
54                 BerElement *ber = ber_init( retdata );
55
56                 if( ber == NULL ) {
57                         ld->ld_errno = LDAP_NO_MEMORY;
58                         return ld->ld_errno;
59                 }
60
61                 /* we should check the tag */
62                 tag = ber_scanf( ber, "{o}", newpasswd );
63                 ber_free( ber, 1 );
64
65                 if( tag == LBER_ERROR ) {
66                         rc = ld->ld_errno = LDAP_DECODING_ERROR;
67                 }
68         }
69
70         ber_memfree( retoid );
71         return rc;
72 }
73
74 int
75 ldap_passwd( LDAP *ld,
76         struct berval   *user,
77         struct berval   *oldpw,
78         struct berval   *newpw,
79         LDAPControl             **sctrls,
80         LDAPControl             **cctrls,
81         int                             *msgidp )
82 {
83         int rc;
84         struct berval bv = {0, NULL};
85         BerElement *ber = NULL;
86
87         assert( ld != NULL );
88         assert( LDAP_VALID( ld ) );
89         assert( msgidp != NULL );
90
91         if( user != NULL || oldpw != NULL || newpw != NULL ) {
92                 /* build change password control */
93                 ber = ber_alloc_t( LBER_USE_DER );
94
95                 if( ber == NULL ) {
96                         ld->ld_errno = LDAP_NO_MEMORY;
97                         return ld->ld_errno;
98                 }
99
100                 ber_printf( ber, "{" /*}*/ );
101
102                 if( user != NULL ) {
103                         ber_printf( ber, "tO",
104                                 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
105                 }
106
107                 if( oldpw != NULL ) {
108                         ber_printf( ber, "tO",
109                                 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
110                 }
111
112                 if( newpw != NULL ) {
113                         ber_printf( ber, "tO",
114                                 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
115                 }
116
117                 ber_printf( ber, /*{*/ "N}" );
118
119                 rc = ber_flatten2( ber, &bv, 0 );
120
121                 if( rc < 0 ) {
122                         ld->ld_errno = LDAP_ENCODING_ERROR;
123                         return ld->ld_errno;
124                 }
125
126         }
127         
128         rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
129                 bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
130
131         ber_free( ber, 1 );
132
133         return rc;
134 }
135
136 int
137 ldap_passwd_s(
138         LDAP *ld,
139         struct berval   *user,
140         struct berval   *oldpw,
141         struct berval   *newpw,
142         struct berval *newpasswd,
143         LDAPControl **sctrls,
144         LDAPControl **cctrls )
145 {
146         int             rc;
147         int             msgid;
148         LDAPMessage     *res;
149
150         rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
151         if ( rc != LDAP_SUCCESS ) {
152                 return rc;
153         }
154
155         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
156                 return ld->ld_errno;
157         }
158
159         rc = ldap_parse_passwd( ld, res, newpasswd );
160         if( rc != LDAP_SUCCESS ) {
161                 ldap_msgfree( res );
162                 return rc;
163         }
164
165         return( ldap_result2error( ld, res, 1 ) );
166 }