]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Sync with HEAD
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "external.h"
24 #include "lber_pvt.h"
25
26 int
27 bdb_exop_passwd( Operation *op, SlapReply *rs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
30         int rc;
31         Entry *e = NULL;
32         EntryInfo *ei;
33         struct berval hash = { 0, NULL };
34         DB_TXN *ltid = NULL, *lt2;
35         struct bdb_op_info opinfo;
36         char textbuf[SLAP_TEXT_BUFLEN];
37         size_t textlen = sizeof textbuf;
38
39         struct berval id = { 0, NULL };
40         struct berval new = { 0, NULL };
41
42         struct berval dn = { 0, NULL };
43         struct berval ndn = { 0, NULL };
44
45         u_int32_t       locker = 0;
46         DB_LOCK         lock;
47
48         int             num_retries = 0;
49
50         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->oq_extended.rs_reqoid ) == 0 );
51
52         rc = slap_passwd_parse( op->oq_extended.rs_reqdata,
53                 &id, NULL, &new, &rs->sr_text );
54
55 #ifdef NEW_LOGGING
56         LDAP_LOG ( ACL, ENTRY, 
57                 "==>bdb_exop_passwd: \"%s\"\n", id.bv_val ? id.bv_val : "", 0, 0  );
58 #else
59         Debug( LDAP_DEBUG_ARGS, "==> bdb_exop_passwd: \"%s\"\n",
60                 id.bv_val ? id.bv_val : "", 0, 0 );
61 #endif
62
63         if( rc != LDAP_SUCCESS ) {
64                 goto done;
65         }
66
67         if( new.bv_len == 0 ) {
68                 slap_passwd_generate(&new);
69
70                 if( new.bv_len == 0 ) {
71                         rs->sr_text = "password generation failed.";
72                         rc = LDAP_OTHER;
73                         goto done;
74                 }
75                 
76                 rs->sr_rspdata = slap_passwd_return( &new );
77         }
78
79         slap_passwd_hash( &new, &hash, &rs->sr_text );
80
81         if( hash.bv_len == 0 ) {
82                 if( !rs->sr_text ) rs->sr_text = "password hash failed";
83                 rc = LDAP_OTHER;
84                 goto done;
85         }
86
87         if( id.bv_len ) {
88                 dn = id;
89         } else {
90                 dn = op->o_dn;
91         }
92
93 #ifdef NEW_LOGGING
94         LDAP_LOG ( ACL, DETAIL1, "bdb_exop_passwd: \"%s\"%s\"\n",
95                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
96 #else
97         Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
98                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
99 #endif
100
101         if( dn.bv_len == 0 ) {
102                 rs->sr_text = "No password is associated with the Root DSE";
103                 rc = LDAP_UNWILLING_TO_PERFORM;
104                 goto done;
105         }
106
107         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
108         if( rc != LDAP_SUCCESS ) {
109                 rs->sr_text = "Invalid DN";
110                 goto done;
111         }
112
113         if( 0 ) {
114 retry:  /* transaction retry */
115                 if ( e != NULL ) {
116                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
117                 }
118 #ifdef NEW_LOGGING
119                 LDAP_LOG ( ACL, DETAIL1, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
120 #else
121                 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
122 #endif
123                 rc = TXN_ABORT( ltid );
124                 ltid = NULL;
125                 op->o_private = NULL;
126                 op->o_do_not_cache = opinfo.boi_acl_cache;
127                 if( rc != 0 ) {
128                         rc = LDAP_OTHER;
129                         rs->sr_text = "internal error";
130                         goto done;
131                 }
132                 ldap_pvt_thread_yield();
133                 bdb_trans_backoff( ++num_retries );
134         }
135
136         /* begin transaction */
137         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
138                 bdb->bi_db_opflags );
139         rs->sr_text = NULL;
140         if( rc != 0 ) {
141 #ifdef NEW_LOGGING
142                 LDAP_LOG ( ACL, ERR, 
143                         "bdb_exop_passwd: txn_begin failed: %s (%d)\n", 
144                         db_strerror(rc), rc, 0 );
145 #else
146                 Debug( LDAP_DEBUG_TRACE,
147                         "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
148                         db_strerror(rc), rc, 0 );
149 #endif
150                 rc = LDAP_OTHER;
151                 rs->sr_text = "internal error";
152                 goto done;
153         }
154
155         locker = TXN_ID ( ltid );
156
157         opinfo.boi_bdb = op->o_bd;
158         opinfo.boi_txn = ltid;
159         opinfo.boi_locker = locker;
160         opinfo.boi_err = 0;
161         opinfo.boi_acl_cache = op->o_do_not_cache;
162         op->o_private = &opinfo;
163
164         /* get entry */
165         rc = bdb_dn2entry( op, ltid, &ndn, &ei, 0 , locker, &lock );
166
167         switch(rc) {
168         case DB_LOCK_DEADLOCK:
169         case DB_LOCK_NOTGRANTED:
170                 goto retry;
171         case DB_NOTFOUND:
172         case 0:
173                 break;
174         case LDAP_BUSY:
175                 rs->sr_text = "ldap server busy";
176                 goto done;
177         default:
178                 rc = LDAP_OTHER;
179                 rs->sr_text = "internal error";
180                 goto done;
181         }
182
183         if ( ei ) e = ei->bei_e;
184
185         if ( e == NULL || is_entry_glue( e )) {
186                         /* FIXME: dn2entry() should return non-glue entry */
187                 rs->sr_text = "could not locate authorization entry";
188                 rc = LDAP_NO_SUCH_OBJECT;
189                 goto done;
190         }
191
192 #ifdef BDB_SUBENTRIES
193         if( is_entry_subentry( e ) ) {
194                 /* entry is a subentry, don't allow operation */
195                 rs->sr_text = "authorization entry is subentry";
196                 rc = LDAP_OTHER;
197                 goto done;
198         }
199 #endif
200
201 #ifdef BDB_ALIASES
202         if( is_entry_alias( e ) ) {
203                 /* entry is an alias, don't allow operation */
204                 rs->sr_text = "authorization entry is alias";
205                 rc = LDAP_ALIAS_PROBLEM;
206                 goto done;
207         }
208 #endif
209
210         if( is_entry_referral( e ) ) {
211                 /* entry is an referral, don't allow operation */
212                 rs->sr_text = "authorization entry is referral";
213                 rc = LDAP_OTHER;
214                 goto done;
215         }
216
217         /* nested transaction */
218         rc = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
219                 bdb->bi_db_opflags );
220         rs->sr_text = NULL;
221         if( rc != 0 ) {
222 #ifdef NEW_LOGGING
223                 LDAP_LOG ( OPERATION, ERR, 
224                         "bdb_exop_passwd: txn_begin(2) failed: %s (%d)\n",
225                         db_strerror(rs->sr_err), rs->sr_err, 0 );
226 #else
227                 Debug( LDAP_DEBUG_TRACE,
228                         "bdb_exop_passwd: txn_begin(2) failed: %s (%d)\n",
229                         db_strerror(rs->sr_err), rs->sr_err, 0 );
230 #endif
231                 rc = LDAP_OTHER;
232                 rs->sr_text = "internal error";
233                 goto done;
234         }
235
236         {
237                 Modifications ml;
238                 struct berval vals[2];
239                 Entry dummy, *save;
240
241                 save = e;
242                 dummy = *e;
243                 e = &dummy;
244
245                 vals[0] = hash;
246                 vals[1].bv_val = NULL;
247
248                 ml.sml_desc = slap_schema.si_ad_userPassword;
249                 ml.sml_values = vals;
250                 ml.sml_nvalues = NULL;
251                 ml.sml_op = LDAP_MOD_REPLACE;
252                 ml.sml_next = NULL;
253
254                 rc = bdb_modify_internal( op, lt2,
255                         &ml, e, &rs->sr_text, textbuf, textlen );
256
257                 if ( (rc == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
258                         rc = opinfo.boi_err;
259                 }
260                 switch(rc) {
261                 case DB_LOCK_DEADLOCK:
262                 case DB_LOCK_NOTGRANTED:
263                         rs->sr_text = NULL;
264                         goto retry;
265                 case 0:
266                         rs->sr_text = NULL;
267                         break;
268                 default:
269                         rc = LDAP_OTHER;
270                         rs->sr_text = "entry modify failed";
271                         goto done;
272                 }
273
274                 /* change the entry itself */
275                 rc = bdb_id2entry_update( op->o_bd, lt2, e );
276                 if( rc != 0 ) {
277                         switch(rc) {
278                         case DB_LOCK_DEADLOCK:
279                         case DB_LOCK_NOTGRANTED:
280                                 goto retry;
281                         }
282                         rs->sr_text = "entry update failed";
283                         rc = LDAP_OTHER;
284                 }
285                 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
286                         rc = LDAP_OTHER;
287                         rs->sr_text = "txn_commit(2) failed";
288                 }
289
290                 if( rc == 0 ) {
291                         if( op->o_noop ) {
292                                 rc = TXN_ABORT( ltid );
293                         } else {
294                                 bdb_cache_modify( save, e->e_attrs,
295                                         bdb->bi_dbenv, locker, &lock );
296                                 rc = TXN_COMMIT( ltid, 0 );
297                         }
298                         ltid = NULL;
299                 }
300                 op->o_private = NULL;
301
302                 if( rc == LDAP_SUCCESS ) {
303                         op->o_req_dn = e->e_name;
304                         op->o_req_ndn = e->e_nname;
305                         op->oq_modify.rs_modlist = &ml;
306                         replog( op );
307                         op->oq_extended.rs_reqoid = slap_EXOP_MODIFY_PASSWD;
308                 }
309         }
310
311 done:
312         if( e != NULL ) {
313                 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e );
314         }
315                 
316         if( hash.bv_val != NULL ) {
317                 free( hash.bv_val );
318         }
319
320         if( ndn.bv_val != NULL ) {
321                 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
322         }
323
324         if( ltid != NULL ) {
325                 TXN_ABORT( ltid );
326                 op->o_private = NULL;
327         }
328
329         return rc;
330 }