]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Delete CDB (no transactions) support
[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_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                 if ( e != NULL ) {
90                         bdb_cache_delete_entry(&bdb->bi_cache, e);
91                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
92                 }
93                 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
94                 rc = txn_abort( ltid );
95                 ltid = NULL;
96                 op->o_private = NULL;
97                 if( rc != 0 ) {
98                         rc = LDAP_OTHER;
99                         *text = "internal error";
100                         goto done;
101                 }
102                 ldap_pvt_thread_yield();
103         }
104
105         /* begin transaction */
106         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
107                 bdb->bi_db_opflags );
108         *text = NULL;
109         if( rc != 0 ) {
110                 Debug( LDAP_DEBUG_TRACE,
111                         "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
112                         db_strerror(rc), rc, 0 );
113                 rc = LDAP_OTHER;
114                 *text = "internal error";
115                 goto done;
116         }
117
118         opinfo.boi_bdb = be;
119         opinfo.boi_txn = ltid;
120         opinfo.boi_err = 0;
121         op->o_private = &opinfo;
122
123         /* get entry */
124         rc = bdb_dn2entry_w( be, ltid, dn, &e, NULL, 0 );
125
126         switch(rc) {
127         case DB_LOCK_DEADLOCK:
128         case DB_LOCK_NOTGRANTED:
129                 goto retry;
130         case DB_NOTFOUND:
131         case 0:
132                 break;
133         default:
134                 rc = LDAP_OTHER;
135                 *text = "internal error";
136                 goto done;
137         }
138
139         if( e == NULL ) {
140                 *text = "could not locate authorization entry";
141                 rc = LDAP_NO_SUCH_OBJECT;
142                 goto done;
143         }
144
145         if( is_entry_alias( e ) ) {
146                 /* entry is an alias, don't allow operation */
147                 *text = "authorization entry is alias";
148                 rc = LDAP_ALIAS_PROBLEM;
149                 goto done;
150         }
151
152
153         if( is_entry_referral( e ) ) {
154                 /* entry is an referral, don't allow operation */
155                 *text = "authorization entry is referral";
156                 rc = LDAP_OPERATIONS_ERROR;
157                 goto done;
158         }
159
160         {
161                 Modifications ml;
162                 struct berval vals[2];
163
164                 vals[0] = hash;
165                 vals[1].bv_val = NULL;
166
167                 ml.sml_desc = slap_schema.si_ad_userPassword;
168                 ml.sml_bvalues = vals;
169                 ml.sml_op = LDAP_MOD_REPLACE;
170                 ml.sml_next = NULL;
171
172                 rc = bdb_modify_internal( be, conn, op, ltid,
173                         &ml, e, text, textbuf, textlen );
174
175                 switch(rc) {
176                 case DB_LOCK_DEADLOCK:
177                 case DB_LOCK_NOTGRANTED:
178                         *text = 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                                 goto retry;
195                         }
196                         *text = "entry update failed";
197                         rc = LDAP_OTHER;
198                 }
199
200                 if( rc == 0 ) {
201                         rc = txn_commit( ltid, 0 );
202                         ltid = NULL;
203                 }
204                 op->o_private = NULL;
205
206                 if( rc == LDAP_SUCCESS ) {
207                         replog( be, op, &e->e_name, &e->e_nname, &ml );
208                 }
209         }
210
211 done:
212         if( e != NULL ) {
213                 bdb_cache_return_entry_w( &bdb->bi_cache, e );
214         }
215                 
216         if( hash.bv_val != NULL ) {
217                 free( hash.bv_val );
218         }
219
220         if( ltid != NULL ) {
221                 txn_abort( ltid );
222                 op->o_private = NULL;
223         }
224
225         return rc;
226 }