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