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