]> git.sur5r.net Git - openldap/blob - libraries/libldap/passwd.c
Insert missing initializers, to silence gcc warnings.
[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 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 == LBER_ERROR ) {
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 = {0, NULL};
75         BerElement *ber = NULL;
76
77         assert( ld != NULL );
78         assert( LDAP_VALID( ld ) );
79         assert( msgidp != NULL );
80
81         if( user != NULL || oldpw != NULL || newpw != NULL ) {
82                 /* build change password control */
83                 ber = ber_alloc_t( LBER_USE_DER );
84
85                 if( ber == NULL ) {
86                         ld->ld_errno = LDAP_NO_MEMORY;
87                         return ld->ld_errno;
88                 }
89
90                 ber_printf( ber, "{" /*}*/ );
91
92                 if( user != NULL ) {
93                         ber_printf( ber, "ts",
94                                 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
95                 }
96
97                 if( oldpw != NULL ) {
98                         ber_printf( ber, "ts",
99                                 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
100                 }
101
102                 if( newpw != NULL ) {
103                         ber_printf( ber, "ts",
104                                 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
105                 }
106
107                 ber_printf( ber, /*{*/ "N}" );
108
109                 rc = ber_flatten2( ber, &bv, 0 );
110
111                 if( rc < 0 ) {
112                         ld->ld_errno = LDAP_ENCODING_ERROR;
113                         return ld->ld_errno;
114                 }
115
116         }
117         
118         rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
119                 bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
120
121         ber_free( ber, 1 );
122
123         return rc;
124 }
125
126 int
127 ldap_passwd_s(
128         LDAP *ld,
129         struct berval   *user,
130         struct berval   *oldpw,
131         struct berval   *newpw,
132         struct berval **newpasswd,
133         LDAPControl **sctrls,
134         LDAPControl **cctrls )
135 {
136         int             rc;
137         int             msgid;
138         LDAPMessage     *res;
139
140         rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
141         if ( rc != LDAP_SUCCESS ) {
142                 return rc;
143         }
144
145         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
146                 return ld->ld_errno;
147         }
148
149         rc = ldap_parse_passwd( ld, res, newpasswd );
150         if( rc != LDAP_SUCCESS ) {
151                 ldap_msgfree( res );
152                 return rc;
153         }
154
155         return( ldap_result2error( ld, res, 1 ) );
156 }