1 /* passwd.c - bdb backend password routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
27 bdb_exop_passwd( Operation *op, SlapReply *rs )
29 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
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;
39 struct berval id = { 0, NULL };
40 struct berval new = { 0, NULL };
42 struct berval dn = { 0, NULL };
43 struct berval ndn = { 0, NULL };
50 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->oq_extended.rs_reqoid ) == 0 );
52 rc = slap_passwd_parse( op->oq_extended.rs_reqdata,
53 &id, NULL, &new, &rs->sr_text );
56 LDAP_LOG ( ACL, ENTRY,
57 "==>bdb_exop_passwd: \"%s\"\n", id.bv_val ? id.bv_val : "", 0, 0 );
59 Debug( LDAP_DEBUG_ARGS, "==> bdb_exop_passwd: \"%s\"\n",
60 id.bv_val ? id.bv_val : "", 0, 0 );
63 if( rc != LDAP_SUCCESS ) {
67 if( new.bv_len == 0 ) {
68 slap_passwd_generate(&new);
70 if( new.bv_len == 0 ) {
71 rs->sr_text = "password generation failed.";
76 rs->sr_rspdata = slap_passwd_return( &new );
79 slap_passwd_hash( &new, &hash, &rs->sr_text );
81 if( hash.bv_len == 0 ) {
82 if( !rs->sr_text ) rs->sr_text = "password hash failed";
94 LDAP_LOG ( ACL, DETAIL1, "bdb_exop_passwd: \"%s\"%s\"\n",
95 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
97 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
98 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
101 if( dn.bv_len == 0 ) {
102 rs->sr_text = "No password is associated with the Root DSE";
103 rc = LDAP_UNWILLING_TO_PERFORM;
107 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
108 if( rc != LDAP_SUCCESS ) {
109 rs->sr_text = "Invalid DN";
114 retry: /* transaction retry */
116 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
119 LDAP_LOG ( ACL, DETAIL1, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
121 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
123 rc = TXN_ABORT( ltid );
125 op->o_private = NULL;
126 op->o_do_not_cache = opinfo.boi_acl_cache;
129 rs->sr_text = "internal error";
132 ldap_pvt_thread_yield();
133 bdb_trans_backoff( ++num_retries );
136 /* begin transaction */
137 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
138 bdb->bi_db_opflags );
143 "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
144 db_strerror(rc), rc, 0 );
146 Debug( LDAP_DEBUG_TRACE,
147 "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
148 db_strerror(rc), rc, 0 );
151 rs->sr_text = "internal error";
155 locker = TXN_ID ( ltid );
157 opinfo.boi_bdb = op->o_bd;
158 opinfo.boi_txn = ltid;
159 opinfo.boi_locker = locker;
161 opinfo.boi_acl_cache = op->o_do_not_cache;
162 op->o_private = &opinfo;
165 rc = bdb_dn2entry( op, ltid, &ndn, &ei, 0 , locker, &lock );
168 case DB_LOCK_DEADLOCK:
169 case DB_LOCK_NOTGRANTED:
175 rs->sr_text = "ldap server busy";
179 rs->sr_text = "internal error";
183 if ( ei ) e = ei->bei_e;
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;
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";
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;
210 if( is_entry_referral( e ) ) {
211 /* entry is an referral, don't allow operation */
212 rs->sr_text = "authorization entry is referral";
217 /* nested transaction */
218 rc = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
219 bdb->bi_db_opflags );
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 );
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 );
232 rs->sr_text = "internal error";
238 struct berval vals[2];
246 vals[1].bv_val = NULL;
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;
254 rc = bdb_modify_internal( op, lt2,
255 &ml, e, &rs->sr_text, textbuf, textlen );
257 if ( (rc == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
261 case DB_LOCK_DEADLOCK:
262 case DB_LOCK_NOTGRANTED:
270 rs->sr_text = "entry modify failed";
274 /* change the entry itself */
275 rc = bdb_id2entry_update( op->o_bd, lt2, e );
278 case DB_LOCK_DEADLOCK:
279 case DB_LOCK_NOTGRANTED:
282 rs->sr_text = "entry update failed";
285 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
287 rs->sr_text = "txn_commit(2) failed";
292 rc = TXN_ABORT( ltid );
294 bdb_cache_modify( save, e->e_attrs,
295 bdb->bi_dbenv, locker, &lock );
296 rc = TXN_COMMIT( ltid, 0 );
300 op->o_private = NULL;
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;
307 op->oq_extended.rs_reqoid = slap_EXOP_MODIFY_PASSWD;
313 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e );
316 if( hash.bv_val != NULL ) {
320 if( ndn.bv_val != NULL ) {
321 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
326 op->o_private = NULL;