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