]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Sync with HEAD
[openldap] / servers / slapd / back-ldbm / passwd.c
1 /* passwd.c - ldbm backend password routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27 #include "lber_pvt.h"
28
29 int
30 ldbm_back_exop_passwd(
31         Operation       *op,
32         SlapReply       *rs )
33 {
34         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
35         int rc;
36         Entry *e = NULL;
37         struct berval hash = { 0, NULL };
38
39         struct berval id = { 0, NULL };
40         struct berval new = { 0, NULL };
41
42         struct berval dn = { 0, NULL };
43         struct berval ndn = { 0, NULL };
44
45         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->oq_extended.rs_reqoid ) == 0 );
46
47         rc = slap_passwd_parse( op->oq_extended.rs_reqdata,
48                 &id, NULL, &new, &rs->sr_text );
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG( BACK_LDBM, ENTRY,
52                 "ldbm_back_exop_passwd: \"%s\"\n", id.bv_val ? id.bv_val : "", 0,0 );
53 #else
54         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_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                         rs->sr_text = "password generation failed.";
67                         rc = LDAP_OTHER;
68                         goto done;
69                 }
70                 
71                 rs->sr_rspdata = slap_passwd_return( &new );
72         }
73
74         slap_passwd_hash( &new, &hash, &rs->sr_text );
75
76         if( hash.bv_len == 0 ) {
77                 if( !rs->sr_text ) rs->sr_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( BACK_LDBM, DETAIL1,
90                 "ldbm_back_exop_passwd: \"%s\"%s\n",
91                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
92 #else
93         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
94                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
95 #endif
96
97         if( dn.bv_len == 0 ) {
98                 rs->sr_text = "No password is associated with the Root DSE";
99                 rc = LDAP_UNWILLING_TO_PERFORM;
100                 goto done;
101         }
102
103         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
104         if( rc != LDAP_SUCCESS ) {
105                 rs->sr_text = "Invalid DN";
106                 goto done;
107         }
108
109         /* grab giant lock for writing */
110         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
111
112         e = dn2entry_w( op->o_bd, &ndn, NULL );
113
114         if ( e == NULL || is_entry_glue( e )) {
115                 /* FIXME : dn2entry() should return non-glue entry */
116                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
117                 rs->sr_text = "could not locate authorization entry";
118                 rc = LDAP_NO_SUCH_OBJECT;
119                 goto done;
120         }
121
122 #ifdef LDBM_SUBENTRIES
123         if( is_entry_subentry( e ) ) {
124                 /* entry is a subentry, don't allow operation */
125                 rs->sr_text = "authorization entry is subentry";
126                 rc = LDAP_OTHER;
127                 goto done;
128         }
129 #endif
130
131         if( is_entry_alias( e ) ) {
132                 /* entry is an alias, don't allow operation */
133                 rs->sr_text = "authorization entry is alias";
134                 rc = LDAP_ALIAS_PROBLEM;
135                 goto done;
136         }
137
138         rc = LDAP_OTHER;
139
140         if( is_entry_referral( e ) ) {
141                 /* entry is an referral, don't allow operation */
142                 rs->sr_text = "authorization entry is referral";
143                 goto done;
144         }
145
146         {
147                 Modifications ml;
148                 struct berval vals[2];
149                 char textbuf[SLAP_TEXT_BUFLEN]; /* non-returnable */
150
151                 vals[0] = hash;
152                 vals[1].bv_val = NULL;
153
154                 ml.sml_desc = slap_schema.si_ad_userPassword;
155                 ml.sml_values = vals;
156                 ml.sml_nvalues = vals;
157                 ml.sml_op = LDAP_MOD_REPLACE;
158                 ml.sml_next = NULL;
159
160                 rc = ldbm_modify_internal( op,
161                         &ml, e, &rs->sr_text, textbuf, 
162                         sizeof( textbuf ) );
163
164                 /* FIXME: ldbm_modify_internal may set *text = textbuf,
165                  * which is BAD */
166                 if ( rs->sr_text == textbuf ) {
167                         rs->sr_text = NULL;
168                 }
169
170                 if( rc ) {
171                         /* cannot return textbuf */
172                         rs->sr_text = "entry modify failed";
173                         goto done;
174                 }
175
176                 /* change the entry itself */
177                 if( id2entry_add( op->o_bd, e ) != 0 ) {
178                         rs->sr_text = "entry update failed";
179                         rc = LDAP_OTHER;
180                 }
181
182                 if( rc == LDAP_SUCCESS ) {
183                         replog( op );
184                 }
185         }
186
187 done:
188         if( e != NULL ) {
189                 cache_return_entry_w( &li->li_cache, e );
190                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
191         }
192
193         if( hash.bv_val != NULL ) {
194                 free( hash.bv_val );
195         }
196
197         if( ndn.bv_val != NULL ) {
198                 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
199         }
200
201         return rc;
202 }