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