]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
ITS#1716 is_entry_subentr/ies/y/
[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         default:
163                 rc = LDAP_OTHER;
164                 *text = "internal error";
165                 goto done;
166         }
167
168         if( e == NULL ) {
169                 *text = "could not locate authorization entry";
170                 rc = LDAP_NO_SUCH_OBJECT;
171                 goto done;
172         }
173
174 #ifdef BDB_SUBENTRIES
175         if( is_entry_subentry( e ) ) {
176                 /* entry is an alias, don't allow operation */
177                 *text = "authorization entry is subentry";
178                 rc = LDAP_OTHER;
179                 goto done;
180         }
181 #endif
182 #ifdef BDB_ALIASES
183         if( is_entry_alias( e ) ) {
184                 /* entry is an alias, don't allow operation */
185                 *text = "authorization entry is alias";
186                 rc = LDAP_ALIAS_PROBLEM;
187                 goto done;
188         }
189 #endif
190
191         if( is_entry_referral( e ) ) {
192                 /* entry is an referral, don't allow operation */
193                 *text = "authorization entry is referral";
194                 rc = LDAP_OTHER;
195                 goto done;
196         }
197
198         {
199                 Modifications ml;
200                 struct berval vals[2];
201
202                 vals[0] = hash;
203                 vals[1].bv_val = NULL;
204
205                 ml.sml_desc = slap_schema.si_ad_userPassword;
206                 ml.sml_bvalues = vals;
207                 ml.sml_op = LDAP_MOD_REPLACE;
208                 ml.sml_next = NULL;
209
210                 rc = bdb_modify_internal( be, conn, op, ltid,
211                         &ml, e, text, textbuf, textlen );
212
213                 switch(rc) {
214                 case DB_LOCK_DEADLOCK:
215                 case DB_LOCK_NOTGRANTED:
216                         *text = NULL;
217                         goto retry;
218                 case 0:
219                         *text = NULL;
220                         break;
221                 default:
222                         rc = LDAP_OTHER;
223                         *text = "entry modify failed";
224                         goto done;
225                 }
226
227                 /* change the entry itself */
228                 rc = bdb_id2entry_update( be, ltid, e );
229                 if( rc != 0 ) {
230                         switch(rc) {
231                         case DB_LOCK_DEADLOCK:
232                         case DB_LOCK_NOTGRANTED:
233                                 goto retry;
234                         }
235                         *text = "entry update failed";
236                         rc = LDAP_OTHER;
237                 }
238
239                 if( rc == 0 ) {
240                         if( op->o_noop ) {
241                                 rc = TXN_ABORT( ltid );
242                         } else {
243                                 rc = TXN_COMMIT( ltid, 0 );
244                         }
245                         ltid = NULL;
246                 }
247                 op->o_private = NULL;
248
249                 if( rc == LDAP_SUCCESS ) {
250                         replog( be, op, &e->e_name, &e->e_nname, &ml );
251                 }
252         }
253
254 done:
255         if( e != NULL ) {
256                 bdb_cache_return_entry_w( &bdb->bi_cache, e );
257         }
258                 
259         if( hash.bv_val != NULL ) {
260                 free( hash.bv_val );
261         }
262
263         if( ltid != NULL ) {
264                 TXN_ABORT( ltid );
265                 op->o_private = NULL;
266         }
267
268         return rc;
269 }