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