]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Last changes should have been #ifdef
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         struct berval   *** refs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         int rc;
31         Entry *e = NULL;
32         struct berval *hash = 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 = NULL;
39         struct berval *new = NULL;
40
41         char *dn;
42
43         assert( reqoid != NULL );
44         assert( strcmp( LDAP_EXOP_X_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 ? id->bv_val : "", 0, 0 );
51
52         if( rc != LDAP_SUCCESS ) {
53                 goto done;
54         }
55
56         if( new == NULL || new->bv_len == 0 ) {
57                 new = slap_passwd_generate();
58
59                 if( new == NULL || 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         hash = slap_passwd_hash( new );
69
70         if( hash == NULL || hash->bv_len == 0 ) {
71                 *text = "password hash failed";
72                 rc = LDAP_OTHER;
73                 goto done;
74         }
75
76         dn = id ? id->bv_val : op->o_dn;
77
78         Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: \"%s\"%s\n",
79                 dn, id ? " (proxy)" : "", 0 );
80
81         if( dn == NULL || dn[0] == '\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                 Debug( LDAP_DEBUG_TRACE, "bdb_exop_passwd: retrying...\n", 0, 0, 0 );
90                 rc = txn_abort( ltid );
91                 ltid = NULL;
92                 op->o_private = NULL;
93                 if( rc != 0 ) {
94                         rc = LDAP_OTHER;
95                         *text = "internal error";
96                         goto done;
97                 }
98         }
99
100         /* begin transaction */
101         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
102         *text = NULL;
103         if( rc != 0 ) {
104                 Debug( LDAP_DEBUG_TRACE,
105                         "bdb_exop_passwd: txn_begin failed: %s (%d)\n",
106                         db_strerror(rc), rc, 0 );
107                 rc = LDAP_OTHER;
108                 *text = "internal error";
109                 goto done;
110         }
111
112         opinfo.boi_bdb = be;
113         opinfo.boi_txn = ltid;
114         opinfo.boi_err = 0;
115         op->o_private = &opinfo;
116
117         /* get entry */
118         rc = bdb_dn2entry( be, ltid, dn, &e, NULL, 0 );
119
120         switch(rc) {
121         case DB_LOCK_DEADLOCK:
122         case DB_LOCK_NOTGRANTED:
123                 goto retry;
124         case DB_NOTFOUND:
125         case 0:
126                 break;
127         default:
128                 rc = LDAP_OTHER;
129                 *text = "internal error";
130                 goto done;
131         }
132
133         if( e == NULL ) {
134                 *text = "could not locate authorization entry";
135                 rc = LDAP_NO_SUCH_OBJECT;
136                 goto done;
137         }
138
139         if( is_entry_alias( e ) ) {
140                 /* entry is an alias, don't allow operation */
141                 *text = "authorization entry is alias";
142                 rc = LDAP_ALIAS_PROBLEM;
143                 goto done;
144         }
145
146
147         if( is_entry_referral( e ) ) {
148                 /* entry is an referral, don't allow operation */
149                 *text = "authorization entry is referral";
150                 rc = LDAP_OPERATIONS_ERROR;
151                 goto done;
152         }
153
154         {
155                 Modifications ml;
156                 struct berval *vals[2];
157
158                 vals[0] = hash;
159                 vals[1] = NULL;
160
161                 ml.sml_desc = slap_schema.si_ad_userPassword;
162                 ml.sml_bvalues = vals;
163                 ml.sml_op = LDAP_MOD_REPLACE;
164                 ml.sml_next = NULL;
165
166                 rc = bdb_modify_internal( be, conn, op, ltid,
167                         &ml, e, text, textbuf, textlen );
168
169                 switch(rc) {
170                 case DB_LOCK_DEADLOCK:
171                 case DB_LOCK_NOTGRANTED:
172                         *text = NULL;
173                         bdb_entry_return( be, e );
174                         e = NULL;
175                         goto retry;
176                 case 0:
177                         break;
178                 default:
179                         rc = LDAP_OTHER;
180                         *text = "entry modify failed";
181                         goto done;
182                 }
183
184         }
185
186         /* change the entry itself */
187         rc = bdb_id2entry_update( be, ltid, e );
188         if( rc != 0 ) {
189                 switch(rc) {
190                 case DB_LOCK_DEADLOCK:
191                 case DB_LOCK_NOTGRANTED:
192                         bdb_entry_return( be, e );
193                         e = NULL;
194                         goto retry;
195                 }
196                 *text = "entry update failed";
197                 rc = LDAP_OTHER;
198         }
199
200 done:
201         if( e != NULL ) {
202                 bdb_entry_return( be, e );
203         }
204
205         if( id != NULL ) {
206                 ber_bvfree( id );
207         }
208
209         if( new != NULL ) {
210                 ber_bvfree( new );
211         }
212
213         if( hash != NULL ) {
214                 ber_bvfree( hash );
215         }
216
217         if( ltid != NULL ) {
218                 txn_abort( ltid );
219                 op->o_private = NULL;
220         }
221
222         return rc;
223 }