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