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