]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
unifdef LDAP_SYNC and LDAP_SYNCREPL
[openldap] / servers / slapd / back-ldbm / passwd.c
1 /* passwd.c - ldbm 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
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18 #include "lber_pvt.h"
19
20 int
21 ldbm_back_exop_passwd(
22         Operation       *op,
23         SlapReply       *rs )
24 {
25         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
26         int rc;
27         Entry *e = NULL;
28         struct berval hash = { 0, NULL };
29
30         struct berval id = { 0, NULL };
31         struct berval new = { 0, NULL };
32
33         struct berval dn = { 0, NULL };
34         struct berval ndn = { 0, NULL };
35
36         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->oq_extended.rs_reqoid ) == 0 );
37
38         rc = slap_passwd_parse( op->oq_extended.rs_reqdata,
39                 &id, NULL, &new, &rs->sr_text );
40
41 #ifdef NEW_LOGGING
42         LDAP_LOG( BACK_LDBM, ENTRY,
43                    "ldbm_back_exop_passwd: \"%s\"\n", id.bv_val ? id.bv_val : "", 0,0 );
44 #else
45         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
46                 id.bv_val ? id.bv_val : "", 0, 0 );
47 #endif
48
49
50         if( rc != LDAP_SUCCESS ) {
51                 goto done;
52         }
53
54         if( new.bv_len == 0 ) {
55                 slap_passwd_generate(&new);
56
57                 if( new.bv_len == 0 ) {
58                         rs->sr_text = "password generation failed.";
59                         rc = LDAP_OTHER;
60                         goto done;
61                 }
62                 
63                 rs->sr_rspdata = slap_passwd_return( &new );
64         }
65
66         slap_passwd_hash( &new, &hash, &rs->sr_text );
67
68         if( hash.bv_len == 0 ) {
69                 if( !rs->sr_text ) rs->sr_text = "password hash failed";
70                 rc = LDAP_OTHER;
71                 goto done;
72         }
73
74         if( id.bv_len ) {
75                 dn = id;
76         } else {
77                 dn = op->o_dn;
78         }
79
80 #ifdef NEW_LOGGING
81         LDAP_LOG( BACK_LDBM, DETAIL1,
82                 "ldbm_back_exop_passwd: \"%s\"%s\n",
83                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
84 #else
85         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
86                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
87 #endif
88
89         if( dn.bv_len == 0 ) {
90                 rs->sr_text = "No password is associated with the Root DSE";
91                 rc = LDAP_UNWILLING_TO_PERFORM;
92                 goto done;
93         }
94
95         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
96         if( rc != LDAP_SUCCESS ) {
97                 rs->sr_text = "Invalid DN";
98                 goto done;
99         }
100
101         /* grab giant lock for writing */
102         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
103
104         e = dn2entry_w( op->o_bd, &ndn, NULL );
105
106         if ( e == NULL || is_entry_glue( e )) {
107                 /* FIXME : dn2entry() should return non-glue entry */
108                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
109                 rs->sr_text = "could not locate authorization entry";
110                 rc = LDAP_NO_SUCH_OBJECT;
111                 goto done;
112         }
113
114 #ifdef LDBM_SUBENTRIES
115         if( is_entry_subentry( e ) ) {
116                 /* entry is a subentry, don't allow operation */
117                 rs->sr_text = "authorization entry is subentry";
118                 rc = LDAP_OTHER;
119                 goto done;
120         }
121 #endif
122
123         if( is_entry_alias( e ) ) {
124                 /* entry is an alias, don't allow operation */
125                 rs->sr_text = "authorization entry is alias";
126                 rc = LDAP_ALIAS_PROBLEM;
127                 goto done;
128         }
129
130         rc = LDAP_OTHER;
131
132         if( is_entry_referral( e ) ) {
133                 /* entry is an referral, don't allow operation */
134                 rs->sr_text = "authorization entry is referral";
135                 goto done;
136         }
137
138         {
139                 Modifications ml;
140                 struct berval vals[2];
141                 char textbuf[SLAP_TEXT_BUFLEN]; /* non-returnable */
142
143                 vals[0] = hash;
144                 vals[1].bv_val = NULL;
145
146                 ml.sml_desc = slap_schema.si_ad_userPassword;
147                 ml.sml_values = vals;
148                 ml.sml_nvalues = vals;
149                 ml.sml_op = LDAP_MOD_REPLACE;
150                 ml.sml_next = NULL;
151
152                 rc = ldbm_modify_internal( op,
153                         &ml, e, &rs->sr_text, textbuf, 
154                         sizeof( textbuf ) );
155
156                 /* FIXME: ldbm_modify_internal may set *text = textbuf,
157                  * which is BAD */
158                 if ( rs->sr_text == textbuf ) {
159                         rs->sr_text = NULL;
160                 }
161
162                 if( rc ) {
163                         /* cannot return textbuf */
164                         rs->sr_text = "entry modify failed";
165                         goto done;
166                 }
167
168                 /* change the entry itself */
169                 if( id2entry_add( op->o_bd, e ) != 0 ) {
170                         rs->sr_text = "entry update failed";
171                         rc = LDAP_OTHER;
172                 }
173
174                 if( rc == LDAP_SUCCESS ) {
175                         replog( op );
176                 }
177         }
178
179 done:
180         if( e != NULL ) {
181                 cache_return_entry_w( &li->li_cache, e );
182                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
183         }
184
185         if( hash.bv_val != NULL ) {
186                 free( hash.bv_val );
187         }
188
189         if( ndn.bv_val != NULL ) {
190                 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
191         }
192
193         return rc;
194 }