]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Another round of changes behind -DSLAPD_SCHEMA_NOT_COMPAT
[openldap] / servers / slapd / back-ldbm / passwd.c
1 /* extended.c - ldbm backend extended routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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         char            *reqoid,
25     struct berval       *reqdata,
26         char            **rspoid,
27     struct berval       **rspdata,
28         LDAPControl *** rspctrls,
29         char**  text,
30     struct berval       *** refs
31 )
32 {
33         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
34         int rc;
35         Entry *e = NULL;
36         struct berval *hash = NULL;
37
38         struct berval *id = NULL;
39         struct berval *new = NULL;
40
41         char *dn;
42
43 #ifdef SLAPD_SCHEMA_NOT_COMPAT
44         static AttributeDescription *entry = NULL;
45 #else
46         static const char *entry = "entry";
47 #endif
48
49         assert( reqoid != NULL );
50         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
51
52         rc = slap_passwd_parse( reqdata,
53                 &id, NULL, &new, text );
54
55         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
56                 id ? id->bv_val : "", 0, 0 );
57
58         if( rc != LDAP_SUCCESS ) {
59                 goto done;
60         }
61
62         if( new == NULL || new->bv_len == 0 ) {
63                 new = slap_passwd_generate();
64
65                 if( new == NULL || new->bv_len == 0 ) {
66                         *text = ch_strdup("password generation failed.");
67                         rc = LDAP_OPERATIONS_ERROR;
68                         goto done;
69                 }
70                 
71                 *rspdata = slap_passwd_return( new );
72         }
73
74         hash = slap_passwd_hash( new );
75
76         if( hash == NULL || hash->bv_len == 0 ) {
77                 *text = ch_strdup("password hash failed");
78                 rc = LDAP_OPERATIONS_ERROR;
79                 goto done;
80         }
81
82         dn = id ? id->bv_val : op->o_dn;
83
84         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
85                 dn, id ? " (proxy)" : "", 0 );
86
87         if( dn == NULL || dn[0] == '\0' ) {
88                 *text = ch_strdup("No password is associated with the Root DSE");
89                 rc = LDAP_OPERATIONS_ERROR;
90                 goto done;
91         }
92
93         e = dn2entry_w( be, dn, NULL );
94
95         if( e == NULL ) {
96                 *text = ch_strdup("could not locate authorization entry");
97                 rc = LDAP_OPERATIONS_ERROR;
98                 goto done;
99         }
100
101         if( ! access_allowed( be, conn, op, e, entry, NULL, ACL_WRITE ) ) {
102                 *text = ch_strdup("access to authorization entry denied");
103                 rc = LDAP_INSUFFICIENT_ACCESS;
104                 goto done;
105         }
106
107         if( is_entry_alias( e ) ) {
108                 /* entry is an alias, don't allow operation */
109                 *text = ch_strdup("authorization entry is alias");
110                 rc = LDAP_ALIAS_PROBLEM;
111                 goto done;
112         }
113
114         rc = LDAP_OPERATIONS_ERROR;
115
116         if( is_entry_referral( e ) ) {
117                 /* entry is an referral, don't allow operation */
118                 *text = ch_strdup("authorization entry is referral");
119                 goto done;
120         }
121
122         {
123                 Modifications ml;
124                 struct berval *vals[2];
125
126                 vals[0] = hash;
127                 vals[1] = NULL;
128
129 #ifdef SLAPD_SCHEMA_NOT_COMPAT
130                 /* not yet implemented */
131 #else
132                 ml.ml_type = ch_strdup("userPassword");
133                 ml.ml_bvalues = vals;
134                 ml.ml_op = LDAP_MOD_REPLACE;
135                 ml.ml_next = NULL;
136 #endif
137
138                 rc = ldbm_modify_internal( be,
139                         conn, op, op->o_ndn, &ml, e );
140
141 #ifdef SLAPD_SCHEMA_NOT_COMPAT
142                 /* not yet implemented */
143 #else
144                 ch_free(ml.ml_type);
145 #endif
146         }
147
148         if( rc == LDAP_SUCCESS ) {
149                 /* change the entry itself */
150                 if( id2entry_add( be, e ) != 0 ) {
151                         rc = LDAP_OPERATIONS_ERROR;
152                 }
153         }
154         
155 done:
156         if( e != NULL ) {
157                 cache_return_entry_w( &li->li_cache, e );
158         }
159
160         if( id != NULL ) {
161                 ber_bvfree( id );
162         }
163
164         if( new != NULL ) {
165                 ber_bvfree( new );
166         }
167
168         if( hash != NULL ) {
169                 ber_bvfree( hash );
170         }
171
172         return rc;
173 }