]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Added "dbnotxn" config keyword. If present, back-bdb uses DB_INIT_CDB
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         struct berval   *** refs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         int rc;
31         Entry *e = NULL;
32         struct berval *hash = 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 = NULL;
39         struct berval *new = NULL;
40
41         char *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 ? id->bv_val : "", 0, 0 );
51
52         if( rc != LDAP_SUCCESS ) {
53                 goto done;
54         }
55
56         if( new == NULL || new->bv_len == 0 ) {
57                 new = slap_passwd_generate();
58
59                 if( new == NULL || 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         hash = slap_passwd_hash( new );
69
70         if( hash == NULL || hash->bv_len == 0 ) {
71                 *text = "password hash failed";
72                 rc = LDAP_OTHER;
73                 goto done;
74         }
75
76         dn = id ? id->bv_val : op->o_dn;
77
78         Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
79                 dn, id ? " (proxy)" : "", 0 );
80
81         if( dn == NULL || dn[0] == '\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         }
99
100         if (bdb->bi_txn) {
101             /* begin transaction */
102             rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
103             *text = NULL;
104             if( rc != 0 ) {
105                 Debug( LDAP_DEBUG_TRACE,
106                         "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
107                         db_strerror(rc), rc, 0 );
108                 rc = LDAP_OTHER;
109                 *text = "internal error";
110                 goto done;
111             }
112         }
113
114         opinfo.boi_bdb = be;
115         opinfo.boi_txn = ltid;
116         opinfo.boi_err = 0;
117         op->o_private = &opinfo;
118
119         /* get entry */
120         rc = bdb_dn2entry( be, ltid, dn, &e, NULL, 0 );
121
122         switch(rc) {
123         case DB_LOCK_DEADLOCK:
124         case DB_LOCK_NOTGRANTED:
125                 goto retry;
126         case DB_NOTFOUND:
127         case 0:
128                 break;
129         default:
130                 rc = LDAP_OTHER;
131                 *text = "internal error";
132                 goto done;
133         }
134
135         if( e == NULL ) {
136                 *text = "could not locate authorization entry";
137                 rc = LDAP_NO_SUCH_OBJECT;
138                 goto done;
139         }
140
141         if( is_entry_alias( e ) ) {
142                 /* entry is an alias, don't allow operation */
143                 *text = "authorization entry is alias";
144                 rc = LDAP_ALIAS_PROBLEM;
145                 goto done;
146         }
147
148
149         if( is_entry_referral( e ) ) {
150                 /* entry is an referral, don't allow operation */
151                 *text = "authorization entry is referral";
152                 rc = LDAP_OPERATIONS_ERROR;
153                 goto done;
154         }
155
156         {
157                 Modifications ml;
158                 struct berval *vals[2];
159
160                 vals[0] = hash;
161                 vals[1] = NULL;
162
163                 ml.sml_desc = slap_schema.si_ad_userPassword;
164                 ml.sml_bvalues = vals;
165                 ml.sml_op = LDAP_MOD_REPLACE;
166                 ml.sml_next = NULL;
167
168                 rc = bdb_modify_internal( be, conn, op, ltid,
169                         &ml, e, text, textbuf, textlen );
170
171                 switch(rc) {
172                 case DB_LOCK_DEADLOCK:
173                 case DB_LOCK_NOTGRANTED:
174                         *text = NULL;
175                         bdb_entry_return( be, e );
176                         e = NULL;
177                         goto retry;
178                 case 0:
179                         break;
180                 default:
181                         rc = LDAP_OTHER;
182                         *text = "entry modify failed";
183                         goto done;
184                 }
185
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         if (bdb->bi_txn && rc == 0) {
202                 rc = txn_commit( ltid, 0 );
203                 ltid = NULL;
204         }
205         op->o_private = NULL;
206
207 done:
208         if( e != NULL ) {
209                 bdb_entry_return( be, e );
210         }
211
212         if( id != NULL ) {
213                 ber_bvfree( id );
214         }
215
216         if( new != NULL ) {
217                 ber_bvfree( new );
218         }
219
220         if( hash != NULL ) {
221                 ber_bvfree( hash );
222         }
223
224         if( ltid != NULL ) {
225                 txn_abort( ltid );
226                 op->o_private = NULL;
227         }
228
229         return rc;
230 }