]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Various changes
[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 #ifdef LDAP_SYNCREPL
107         if ( e == NULL || is_entry_glue( e )) {
108                         /* FIXME : dn2entry() should return non-glue entry */
109 #else
110         if( e == NULL ) {
111 #endif
112                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
113                 rs->sr_text = "could not locate authorization entry";
114                 rc = LDAP_NO_SUCH_OBJECT;
115                 goto done;
116         }
117
118 #ifdef LDBM_SUBENTRIES
119         if( is_entry_subentry( e ) ) {
120                 /* entry is a subentry, don't allow operation */
121                 rs->sr_text = "authorization entry is subentry";
122                 rc = LDAP_OTHER;
123                 goto done;
124         }
125 #endif
126
127         if( is_entry_alias( e ) ) {
128                 /* entry is an alias, don't allow operation */
129                 rs->sr_text = "authorization entry is alias";
130                 rc = LDAP_ALIAS_PROBLEM;
131                 goto done;
132         }
133
134         rc = LDAP_OTHER;
135
136         if( is_entry_referral( e ) ) {
137                 /* entry is an referral, don't allow operation */
138                 rs->sr_text = "authorization entry is referral";
139                 goto done;
140         }
141
142         {
143                 Modifications ml;
144                 struct berval vals[2];
145                 char textbuf[SLAP_TEXT_BUFLEN]; /* non-returnable */
146
147                 vals[0] = hash;
148                 vals[1].bv_val = NULL;
149
150                 ml.sml_desc = slap_schema.si_ad_userPassword;
151                 ml.sml_values = vals;
152                 ml.sml_nvalues = vals;
153                 ml.sml_op = LDAP_MOD_REPLACE;
154                 ml.sml_next = NULL;
155
156                 rc = ldbm_modify_internal( op,
157                         &ml, e, &rs->sr_text, textbuf, 
158                         sizeof( textbuf ) );
159
160                 /* FIXME: ldbm_modify_internal may set *text = textbuf,
161                  * which is BAD */
162                 if ( rs->sr_text == textbuf ) {
163                         rs->sr_text = NULL;
164                 }
165
166                 if( rc ) {
167                         /* cannot return textbuf */
168                         rs->sr_text = "entry modify failed";
169                         goto done;
170                 }
171
172                 /* change the entry itself */
173                 if( id2entry_add( op->o_bd, e ) != 0 ) {
174                         rs->sr_text = "entry update failed";
175                         rc = LDAP_OTHER;
176                 }
177
178                 if( rc == LDAP_SUCCESS ) {
179                         replog( op );
180                 }
181         }
182
183 done:
184         if( e != NULL ) {
185                 cache_return_entry_w( &li->li_cache, e );
186                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
187         }
188
189         if( hash.bv_val != NULL ) {
190                 free( hash.bv_val );
191         }
192
193         if( ndn.bv_val != NULL ) {
194                 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
195         }
196
197         return rc;
198 }