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