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