]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Quick patch to support struct berval dn2entry changes
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_exop_passwd(
18         Backend         *be,
19         Connection              *conn,
20         Operation               *op,
21         const char              *reqoid,
22         struct berval   *reqdata,
23         char                    **rspoid,
24         struct berval   **rspdata,
25         LDAPControl             *** rspctrls,
26         const char              **text,
27         struct berval   *** refs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         int rc;
31         Entry *e = NULL;
32         struct berval *hash = NULL;
33         DB_TXN *ltid = NULL;
34         struct bdb_op_info opinfo;
35         char textbuf[SLAP_TEXT_BUFLEN];
36         size_t textlen = sizeof textbuf;
37
38         struct berval *id = NULL;
39         struct berval *new = NULL;
40
41         struct berval *dn;
42
43         assert( reqoid != NULL );
44         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
45
46         rc = slap_passwd_parse( reqdata,
47                 &id, NULL, &new, text );
48
49         Debug( LDAP_DEBUG_ARGS, "==> bdb_exop_passwd: \"%s\"\n",
50                 id ? id->bv_val : "", 0, 0 );
51
52         if( rc != LDAP_SUCCESS ) {
53                 goto done;
54         }
55
56         if( new == NULL || new->bv_len == 0 ) {
57                 new = slap_passwd_generate();
58
59                 if( new == NULL || new->bv_len == 0 ) {
60                         *text = "password generation failed.";
61                         rc = LDAP_OTHER;
62                         goto done;
63                 }
64                 
65                 *rspdata = slap_passwd_return( new );
66         }
67
68         hash = slap_passwd_hash( new );
69
70         if( hash == NULL || hash->bv_len == 0 ) {
71                 *text = "password hash failed";
72                 rc = LDAP_OTHER;
73                 goto done;
74         }
75
76         dn = id ? id : &op->o_dn;
77
78         Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
79                 dn->bv_val, id ? " (proxy)" : "", 0 );
80
81         if( dn->bv_len == 0 ) {
82                 *text = "No password is associated with the Root DSE";
83                 rc = LDAP_OPERATIONS_ERROR;
84                 goto done;
85         }
86
87         if( 0 ) {
88 retry:  /* transaction retry */
89                 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
90                 rc = txn_abort( ltid );
91                 ltid = NULL;
92                 op->o_private = NULL;
93                 if( rc != 0 ) {
94                         rc = LDAP_OTHER;
95                         *text = "internal error";
96                         goto done;
97                 }
98         }
99
100         if( bdb->bi_txn ) {
101                 /* begin transaction */
102                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
103                         bdb->bi_db_opflags );
104                 *text = NULL;
105                 if( rc != 0 ) {
106                         Debug( LDAP_DEBUG_TRACE,
107                                 "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
108                                 db_strerror(rc), rc, 0 );
109                         rc = LDAP_OTHER;
110                         *text = "internal error";
111                         goto done;
112                 }
113         }
114
115         opinfo.boi_bdb = be;
116         opinfo.boi_txn = ltid;
117         opinfo.boi_err = 0;
118         op->o_private = &opinfo;
119
120         /* get entry */
121         rc = bdb_dn2entry( be, ltid, dn, &e, NULL, 0 );
122
123         switch(rc) {
124         case DB_LOCK_DEADLOCK:
125         case DB_LOCK_NOTGRANTED:
126                 goto retry;
127         case DB_NOTFOUND:
128         case 0:
129                 break;
130         default:
131                 rc = LDAP_OTHER;
132                 *text = "internal error";
133                 goto done;
134         }
135
136         if( e == NULL ) {
137                 *text = "could not locate authorization entry";
138                 rc = LDAP_NO_SUCH_OBJECT;
139                 goto done;
140         }
141
142         if( is_entry_alias( e ) ) {
143                 /* entry is an alias, don't allow operation */
144                 *text = "authorization entry is alias";
145                 rc = LDAP_ALIAS_PROBLEM;
146                 goto done;
147         }
148
149
150         if( is_entry_referral( e ) ) {
151                 /* entry is an referral, don't allow operation */
152                 *text = "authorization entry is referral";
153                 rc = LDAP_OPERATIONS_ERROR;
154                 goto done;
155         }
156
157         {
158                 Modifications ml;
159                 struct berval *vals[2];
160
161                 vals[0] = hash;
162                 vals[1] = NULL;
163
164                 ml.sml_desc = slap_schema.si_ad_userPassword;
165                 ml.sml_bvalues = vals;
166                 ml.sml_op = LDAP_MOD_REPLACE;
167                 ml.sml_next = NULL;
168
169                 rc = bdb_modify_internal( be, conn, op, ltid,
170                         &ml, e, text, textbuf, textlen );
171
172                 switch(rc) {
173                 case DB_LOCK_DEADLOCK:
174                 case DB_LOCK_NOTGRANTED:
175                         *text = NULL;
176                         bdb_entry_return( be, e );
177                         e = NULL;
178                         goto retry;
179                 case 0:
180                         break;
181                 default:
182                         rc = LDAP_OTHER;
183                         *text = "entry modify failed";
184                         goto done;
185                 }
186
187                 /* change the entry itself */
188                 rc = bdb_id2entry_update( be, ltid, e );
189                 if( rc != 0 ) {
190                         switch(rc) {
191                         case DB_LOCK_DEADLOCK:
192                         case DB_LOCK_NOTGRANTED:
193                                 bdb_entry_return( be, e );
194                                 e = NULL;
195                                 goto retry;
196                         }
197                         *text = "entry update failed";
198                         rc = LDAP_OTHER;
199                 }
200
201                 if( bdb->bi_txn && rc == 0 ) {
202                         rc = txn_commit( ltid, 0 );
203                         ltid = NULL;
204                 }
205                 op->o_private = NULL;
206
207                 if( rc == LDAP_SUCCESS ) {
208                         replog( be, op, &e->e_name, &e->e_nname, &ml );
209                 }
210         }
211
212 done:
213         if( e != NULL ) {
214                 bdb_entry_return( be, e );
215         }
216
217         if( id != NULL ) {
218                 ber_bvfree( id );
219         }
220
221         if( new != NULL ) {
222                 ber_bvfree( new );
223         }
224
225         if( hash != NULL ) {
226                 ber_bvfree( hash );
227         }
228
229         if( ltid != NULL ) {
230                 txn_abort( ltid );
231                 op->o_private = NULL;
232         }
233
234         return rc;
235 }