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