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