]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Set lock detector to DEFAULT, not NORUN.
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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
16 int
17 bdb_exop_passwd(
18         Backend         *be,
19         Connection              *conn,
20         Operation               *op,
21         const char              *reqoid,
22         struct berval   *reqdata,
23         char                    **rspoid,
24         struct berval   **rspdata,
25         LDAPControl             *** rspctrls,
26         const char              **text,
27         BerVarray *refs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         int rc;
31         Entry *e = NULL;
32         struct berval hash = { 0, NULL };
33         DB_TXN *ltid = NULL;
34         struct bdb_op_info opinfo;
35         char textbuf[SLAP_TEXT_BUFLEN];
36         size_t textlen = sizeof textbuf;
37
38         struct berval id = { 0, NULL };
39         struct berval new = { 0, NULL };
40
41         struct berval *dn;
42
43         assert( reqoid != NULL );
44         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
45
46         rc = slap_passwd_parse( reqdata,
47                 &id, NULL, &new, text );
48
49         Debug( LDAP_DEBUG_ARGS, "==> bdb_exop_passwd: \"%s\"\n",
50                 id.bv_val ? id.bv_val : "", 0, 0 );
51
52         if( rc != LDAP_SUCCESS ) {
53                 goto done;
54         }
55
56         if( new.bv_len == 0 ) {
57                 slap_passwd_generate(&new);
58
59                 if( new.bv_len == 0 ) {
60                         *text = "password generation failed.";
61                         rc = LDAP_OTHER;
62                         goto done;
63                 }
64                 
65                 *rspdata = slap_passwd_return( &new );
66         }
67
68         slap_passwd_hash( &new, &hash );
69
70         if( hash.bv_len == 0 ) {
71                 *text = "password hash failed";
72                 rc = LDAP_OTHER;
73                 goto done;
74         }
75
76         dn = id.bv_val ? &id : &op->o_dn;
77
78         Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
79                 dn->bv_val, id.bv_val ? " (proxy)" : "", 0 );
80
81         if( dn->bv_len == 0 ) {
82                 *text = "No password is associated with the Root DSE";
83                 rc = LDAP_OPERATIONS_ERROR;
84                 goto done;
85         }
86
87         if( 0 ) {
88 retry:  /* transaction retry */
89                 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
90                 rc = txn_abort( ltid );
91                 ltid = NULL;
92                 op->o_private = NULL;
93                 if( rc != 0 ) {
94                         rc = LDAP_OTHER;
95                         *text = "internal error";
96                         goto done;
97                 }
98                 ldap_pvt_thread_yield();
99         }
100
101         if( bdb->bi_txn ) {
102                 /* begin transaction */
103                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
104                         bdb->bi_db_opflags );
105                 *text = NULL;
106                 if( rc != 0 ) {
107                         Debug( LDAP_DEBUG_TRACE,
108                                 "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
109                                 db_strerror(rc), rc, 0 );
110                         rc = LDAP_OTHER;
111                         *text = "internal error";
112                         goto done;
113                 }
114         }
115
116         opinfo.boi_bdb = be;
117         opinfo.boi_txn = ltid;
118         opinfo.boi_err = 0;
119         op->o_private = &opinfo;
120
121         /* get entry */
122         rc = bdb_dn2entry( be, ltid, dn, &e, NULL, 0 );
123
124         switch(rc) {
125         case DB_LOCK_DEADLOCK:
126         case DB_LOCK_NOTGRANTED:
127                 goto retry;
128         case DB_NOTFOUND:
129         case 0:
130                 break;
131         default:
132                 rc = LDAP_OTHER;
133                 *text = "internal error";
134                 goto done;
135         }
136
137         if( e == NULL ) {
138                 *text = "could not locate authorization entry";
139                 rc = LDAP_NO_SUCH_OBJECT;
140                 goto done;
141         }
142
143         if( is_entry_alias( e ) ) {
144                 /* entry is an alias, don't allow operation */
145                 *text = "authorization entry is alias";
146                 rc = LDAP_ALIAS_PROBLEM;
147                 goto done;
148         }
149
150
151         if( is_entry_referral( e ) ) {
152                 /* entry is an referral, don't allow operation */
153                 *text = "authorization entry is referral";
154                 rc = LDAP_OPERATIONS_ERROR;
155                 goto done;
156         }
157
158         {
159                 Modifications ml;
160                 struct berval vals[2];
161
162                 vals[0] = hash;
163                 vals[1].bv_val = NULL;
164
165                 ml.sml_desc = slap_schema.si_ad_userPassword;
166                 ml.sml_bvalues = vals;
167                 ml.sml_op = LDAP_MOD_REPLACE;
168                 ml.sml_next = NULL;
169
170                 rc = bdb_modify_internal( be, conn, op, ltid,
171                         &ml, e, text, textbuf, textlen );
172
173                 switch(rc) {
174                 case DB_LOCK_DEADLOCK:
175                 case DB_LOCK_NOTGRANTED:
176                         *text = NULL;
177                         bdb_entry_return( be, e );
178                         e = NULL;
179                         goto retry;
180                 case 0:
181                         break;
182                 default:
183                         rc = LDAP_OTHER;
184                         *text = "entry modify failed";
185                         goto done;
186                 }
187
188                 /* change the entry itself */
189                 rc = bdb_id2entry_update( be, ltid, e );
190                 if( rc != 0 ) {
191                         switch(rc) {
192                         case DB_LOCK_DEADLOCK:
193                         case DB_LOCK_NOTGRANTED:
194                                 bdb_entry_return( be, e );
195                                 e = NULL;
196                                 goto retry;
197                         }
198                         *text = "entry update failed";
199                         rc = LDAP_OTHER;
200                 }
201
202                 if( bdb->bi_txn && rc == 0 ) {
203                         rc = txn_commit( ltid, 0 );
204                         ltid = NULL;
205                 }
206                 op->o_private = NULL;
207
208                 if( rc == LDAP_SUCCESS ) {
209                         replog( be, op, &e->e_name, &e->e_nname, &ml );
210                 }
211         }
212
213 done:
214         if( e != NULL ) {
215                 bdb_entry_return( be, e );
216         }
217
218         if( hash.bv_val != NULL ) {
219                 free( hash.bv_val );
220         }
221
222         if( ltid != NULL ) {
223                 txn_abort( ltid );
224                 op->o_private = NULL;
225         }
226
227         return rc;
228 }