]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
cleanup bind
[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     Backend             *be,
23     Connection          *conn,
24     Operation           *op,
25         struct berval           *reqoid,
26     struct berval       *reqdata,
27         char                    **rspoid,
28     struct berval       **rspdata,
29         LDAPControl             *** rspctrls,
30         const char              **text,
31     BerVarray *refs
32 )
33 {
34         struct ldbminfo *li = (struct ldbminfo *) be->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;
43         struct berval ndn;
44
45         assert( reqoid != NULL );
46         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
47
48         rc = slap_passwd_parse( reqdata,
49                 &id, NULL, &new, text );
50
51 #ifdef NEW_LOGGING
52         LDAP_LOG( BACK_LDBM, ENTRY,
53                    "ldbm_back_exop_passwd: \"%s\"\n", id.bv_val ? id.bv_val : "", 0,0 );
54 #else
55         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
56                 id.bv_val ? id.bv_val : "", 0, 0 );
57 #endif
58
59
60         if( rc != LDAP_SUCCESS ) {
61                 goto done;
62         }
63
64         if( new.bv_len == 0 ) {
65                 slap_passwd_generate(&new);
66
67                 if( new.bv_len == 0 ) {
68                         *text = "password generation failed.";
69                         rc = LDAP_OTHER;
70                         goto done;
71                 }
72                 
73                 *rspdata = slap_passwd_return( &new );
74         }
75
76         slap_passwd_hash( &new, &hash );
77
78         if( hash.bv_len == 0 ) {
79                 *text = "password hash failed";
80                 rc = LDAP_OTHER;
81                 goto done;
82         }
83
84         if( id.bv_len ) {
85                 dn = id;
86         } else {
87                 dn = op->o_dn;
88         }
89
90 #ifdef NEW_LOGGING
91         LDAP_LOG( BACK_LDBM, DETAIL1,
92                 "ldbm_back_exop_passwd: \"%s\"%s\n",
93                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
94 #else
95         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
96                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
97 #endif
98
99         if( dn.bv_len == 0 ) {
100                 *text = "No password is associated with the Root DSE";
101                 rc = LDAP_UNWILLING_TO_PERFORM;
102                 goto done;
103         }
104
105         rc = dnNormalize2( NULL, &dn, &ndn );
106         if( rc != LDAP_SUCCESS ) {
107                 *text = "Invalid DN";
108                 goto done;
109         }
110
111         /* grab giant lock for writing */
112         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
113
114         e = dn2entry_w( be, &ndn, NULL );
115         if( e == NULL ) {
116                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
117                 *text = "could not locate authorization entry";
118                 rc = LDAP_NO_SUCH_OBJECT;
119                 goto done;
120         }
121
122         if( is_entry_alias( e ) ) {
123                 /* entry is an alias, don't allow operation */
124                 *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                 *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 #ifdef SLAP_NVALUES
148                 ml.sml_nvalues = vals;
149 #endif
150                 ml.sml_op = LDAP_MOD_REPLACE;
151                 ml.sml_next = NULL;
152
153                 rc = ldbm_modify_internal( be,
154                         conn, op, op->o_ndn.bv_val, &ml, e, text, textbuf, 
155                         sizeof( textbuf ) );
156
157                 /* FIXME: ldbm_modify_internal may set *text = textbuf,
158                  * which is BAD */
159                 if ( *text == textbuf ) {
160                         *text = NULL;
161                 }
162
163                 if( rc ) {
164                         /* cannot return textbuf */
165                         *text = "entry modify failed";
166                         goto done;
167                 }
168
169                 /* change the entry itself */
170                 if( id2entry_add( be, e ) != 0 ) {
171                         *text = "entry update failed";
172                         rc = LDAP_OTHER;
173                 }
174
175                 if( rc == LDAP_SUCCESS ) {
176                         replog( be, op, &e->e_name, &e->e_nname, &ml );
177                 }
178         }
179
180 done:
181         if( e != NULL ) {
182                 cache_return_entry_w( &li->li_cache, e );
183                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
184         }
185
186         if( hash.bv_val != NULL ) {
187                 free( hash.bv_val );
188         }
189
190         if( ndn.bv_val != NULL ) {
191                 free( ndn.bv_val );
192         }
193
194         return rc;
195 }