]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Switch to openldap-data
[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 #ifdef BDB_SUBENTRIES
146         if( is_entry_subentries( e ) ) {
147                 /* entry is an alias, don't allow operation */
148                 *text = "authorization entry is subentry";
149                 rc = LDAP_OTHER;
150                 goto done;
151         }
152 #endif
153 #ifdef BDB_ALIASES
154         if( is_entry_alias( e ) ) {
155                 /* entry is an alias, don't allow operation */
156                 *text = "authorization entry is alias";
157                 rc = LDAP_ALIAS_PROBLEM;
158                 goto done;
159         }
160 #endif
161
162         if( is_entry_referral( e ) ) {
163                 /* entry is an referral, don't allow operation */
164                 *text = "authorization entry is referral";
165                 rc = LDAP_OTHER;
166                 goto done;
167         }
168
169         {
170                 Modifications ml;
171                 struct berval vals[2];
172
173                 vals[0] = hash;
174                 vals[1].bv_val = NULL;
175
176                 ml.sml_desc = slap_schema.si_ad_userPassword;
177                 ml.sml_bvalues = vals;
178                 ml.sml_op = LDAP_MOD_REPLACE;
179                 ml.sml_next = NULL;
180
181                 rc = bdb_modify_internal( be, conn, op, ltid,
182                         &ml, e, text, textbuf, textlen );
183
184                 switch(rc) {
185                 case DB_LOCK_DEADLOCK:
186                 case DB_LOCK_NOTGRANTED:
187                         *text = NULL;
188                         goto retry;
189                 case 0:
190                         break;
191                 default:
192                         rc = LDAP_OTHER;
193                         *text = "entry modify failed";
194                         goto done;
195                 }
196
197                 /* change the entry itself */
198                 rc = bdb_id2entry_update( be, ltid, e );
199                 if( rc != 0 ) {
200                         switch(rc) {
201                         case DB_LOCK_DEADLOCK:
202                         case DB_LOCK_NOTGRANTED:
203                                 goto retry;
204                         }
205                         *text = "entry update failed";
206                         rc = LDAP_OTHER;
207                 }
208
209                 if( rc == 0 ) {
210                         if( op->o_noop ) {
211                                 rc = TXN_ABORT( ltid );
212                         } else {
213                                 rc = TXN_COMMIT( ltid, 0 );
214                         }
215                         ltid = NULL;
216                 }
217                 op->o_private = NULL;
218
219                 if( rc == LDAP_SUCCESS ) {
220                         replog( be, op, &e->e_name, &e->e_nname, &ml );
221                 }
222         }
223
224 done:
225         if( e != NULL ) {
226                 bdb_cache_return_entry_w( &bdb->bi_cache, e );
227         }
228                 
229         if( hash.bv_val != NULL ) {
230                 free( hash.bv_val );
231         }
232
233         if( ltid != NULL ) {
234                 TXN_ABORT( ltid );
235                 op->o_private = NULL;
236         }
237
238         return rc;
239 }