]> git.sur5r.net Git - openldap/blob - libraries/libldap/passwd.c
a186abe0402c6c35c385c057b1a73fd1d12a6f75
[openldap] / libraries / libldap / passwd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 3???>
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 = NULL;
35
36         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 );
37
38         if( rc != LDAP_SUCCESS ) {
39                 return rc;
40         }
41
42         if( retdata != NULL ) {
43                 ber_tag_t tag;
44                 BerElement *ber = ber_init( retdata );
45
46                 if( ber == NULL ) {
47                         ld->ld_errno = LDAP_NO_MEMORY;
48                         return ld->ld_errno;
49                 }
50
51                 /* we should check the tag */
52                 tag = ber_scanf( ber, "{o}", newpasswd );
53                 ber_free( ber, 1 );
54
55                 if( tag == -1 ) {
56                         rc = ld->ld_errno = LDAP_DECODING_ERROR;
57                 }
58         }
59
60         ber_memfree( retoid );
61         return rc;
62 }
63
64 int
65 ldap_passwd( LDAP *ld,
66         struct berval   *user,
67         struct berval   *oldpw,
68         struct berval   *newpw,
69         LDAPControl             **sctrls,
70         LDAPControl             **cctrls,
71         int                             *msgidp )
72 {
73         int rc;
74         struct berval *bv = NULL;
75
76         assert( ld != NULL );
77         assert( LDAP_VALID( ld ) );
78         assert( msgidp != NULL );
79
80         if( user != NULL || oldpw != NULL || newpw != NULL ) {
81                 /* build change password control */
82                 BerElement *ber = ber_alloc_t( LBER_USE_DER );
83
84                 if( ber == NULL ) {
85                         ld->ld_errno = LDAP_NO_MEMORY;
86                         return ld->ld_errno;
87                 }
88
89                 ber_printf( ber, "{" /*}*/ );
90
91                 if( user != NULL ) {
92                         ber_printf( ber, "ts",
93                                 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
94                 }
95
96                 if( oldpw != NULL ) {
97                         ber_printf( ber, "ts",
98                                 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
99                 }
100
101                 if( newpw != NULL ) {
102                         ber_printf( ber, "ts",
103                                 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
104                 }
105
106                 ber_printf( ber, /*{*/ "N}" );
107
108                 rc = ber_flatten( ber, &bv );
109
110                 ber_free( ber, 1 );
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, sctrls, cctrls, msgidp );
121
122         return rc;
123 }
124
125 int
126 ldap_passwd_s(
127         LDAP *ld,
128         struct berval   *user,
129         struct berval   *oldpw,
130         struct berval   *newpw,
131         struct berval **newpasswd,
132         LDAPControl **sctrls,
133         LDAPControl **cctrls )
134 {
135         int             rc;
136         int             msgid;
137         LDAPMessage     *res;
138
139         rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
140         if ( rc != LDAP_SUCCESS ) {
141                 return rc;
142         }
143
144         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
145                 return ld->ld_errno;
146         }
147
148         rc = ldap_parse_passwd( ld, res, newpasswd );
149         if( rc != LDAP_SUCCESS ) {
150                 ldap_msgfree( res );
151                 return rc;
152         }
153
154         return( ldap_result2error( ld, res, 1 ) );
155 }