]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Update copyright statements
[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     BVarray *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_X_MODIFY_PASSWD, reqoid ) == 0 );
46
47         rc = slap_passwd_parse( reqdata,
48                 &id, NULL, &new, text );
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
52                    "ldbm_back_exop_passwd: \"%s\"\n",
53                    id.bv_val ? id.bv_val : "" ));
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(( "backend", LDAP_LEVEL_DETAIL1,
92                 "ldbm_back_exop_passwd: \"%s\"%s\n",
93                 dn.bv_val, id.bv_len ? " (proxy)" : "" ));
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_OPERATIONS_ERROR;
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         e = dn2entry_w( be, &ndn, NULL );
112         if( e == NULL ) {
113                 *text = "could not locate authorization entry";
114                 rc = LDAP_NO_SUCH_OBJECT;
115                 goto done;
116         }
117
118         if( is_entry_alias( e ) ) {
119                 /* entry is an alias, don't allow operation */
120                 *text = "authorization entry is alias";
121                 rc = LDAP_ALIAS_PROBLEM;
122                 goto done;
123         }
124
125         rc = LDAP_OPERATIONS_ERROR;
126
127         if( is_entry_referral( e ) ) {
128                 /* entry is an referral, don't allow operation */
129                 *text = "authorization entry is referral";
130                 goto done;
131         }
132
133         {
134                 Modifications ml;
135                 struct berval vals[2];
136                 char textbuf[SLAP_TEXT_BUFLEN]; /* non-returnable */
137
138                 vals[0] = hash;
139                 vals[1].bv_val = NULL;
140
141                 ml.sml_desc = slap_schema.si_ad_userPassword;
142                 ml.sml_bvalues = vals;
143                 ml.sml_op = LDAP_MOD_REPLACE;
144                 ml.sml_next = NULL;
145
146                 rc = ldbm_modify_internal( be,
147                         conn, op, op->o_ndn.bv_val, &ml, e, text, textbuf, 
148                         sizeof( textbuf ) );
149
150                 /* FIXME: ldbm_modify_internal may set *text = textbuf,
151                  * which is BAD */
152                 if ( *text == textbuf ) {
153                         *text = NULL;
154                 }
155
156                 if( rc ) {
157                         /* cannot return textbuf */
158                         *text = "entry modify failed";
159                         goto done;
160                 }
161
162                 /* change the entry itself */
163                 if( id2entry_add( be, e ) != 0 ) {
164                         *text = "entry update failed";
165                         rc = LDAP_OTHER;
166                 }
167
168                 if( rc == LDAP_SUCCESS ) {
169                         replog( be, op, &e->e_name, &e->e_nname, &ml );
170                 }
171         }
172
173 done:
174         if( e != NULL ) {
175                 cache_return_entry_w( &li->li_cache, e );
176         }
177
178         if( id.bv_val != NULL ) {
179                 free( id.bv_val );
180         }
181
182         if( new.bv_val != NULL ) {
183                 free( new.bv_val );
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 }