]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Return noSuchObject when target entry doesn't exist
[openldap] / servers / slapd / back-ldbm / passwd.c
1 /* extended.c - ldbm backend extended routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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     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         assert( reqoid != NULL );
44         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
45
46         rc = slap_passwd_parse( reqdata,
47                 &id, NULL, &new, text );
48
49         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
50                 id ? id->bv_val : "", 0, 0 );
51
52         if( rc != LDAP_SUCCESS ) {
53                 goto done;
54         }
55
56         if( new == NULL || new->bv_len == 0 ) {
57                 new = slap_passwd_generate();
58
59                 if( new == NULL || new->bv_len == 0 ) {
60                         *text = "password generation failed.";
61                         rc = LDAP_OTHER;
62                         goto done;
63                 }
64                 
65                 *rspdata = slap_passwd_return( new );
66         }
67
68         hash = slap_passwd_hash( new );
69
70         if( hash == NULL || hash->bv_len == 0 ) {
71                 *text = "password hash failed";
72                 rc = LDAP_OTHER;
73                 goto done;
74         }
75
76         dn = id ? id->bv_val : op->o_dn;
77
78         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
79                 dn, id ? " (proxy)" : "", 0 );
80
81         if( dn == NULL || dn[0] == '\0' ) {
82                 *text = "No password is associated with the Root DSE";
83                 rc = LDAP_OPERATIONS_ERROR;
84                 goto done;
85         }
86
87         e = dn2entry_w( be, dn, NULL );
88
89         if( e == NULL ) {
90                 *text = "could not locate authorization entry";
91                 rc = LDAP_NO_SUCH_OBJECT;
92                 goto done;
93         }
94
95         if( is_entry_alias( e ) ) {
96                 /* entry is an alias, don't allow operation */
97                 *text = "authorization entry is alias";
98                 rc = LDAP_ALIAS_PROBLEM;
99                 goto done;
100         }
101
102         rc = LDAP_OPERATIONS_ERROR;
103
104         if( is_entry_referral( e ) ) {
105                 /* entry is an referral, don't allow operation */
106                 *text = "authorization entry is referral";
107                 goto done;
108         }
109
110         {
111                 Modifications ml;
112                 struct berval *vals[2];
113
114                 vals[0] = hash;
115                 vals[1] = NULL;
116
117                 ml.sml_desc = slap_schema.si_ad_userPassword;
118                 ml.sml_bvalues = vals;
119                 ml.sml_op = LDAP_MOD_REPLACE;
120                 ml.sml_next = NULL;
121
122                 rc = ldbm_modify_internal( be,
123                         conn, op, op->o_ndn, &ml, e, text );
124
125         }
126
127         if( rc == LDAP_SUCCESS ) {
128                 /* change the entry itself */
129                 if( id2entry_add( be, e ) != 0 ) {
130                         *text = "entry update failed";
131                         rc = LDAP_OTHER;
132                 }
133         }
134         
135 done:
136         if( e != NULL ) {
137                 cache_return_entry_w( &li->li_cache, e );
138         }
139
140         if( id != NULL ) {
141                 ber_bvfree( id );
142         }
143
144         if( new != NULL ) {
145                 ber_bvfree( new );
146         }
147
148         if( hash != NULL ) {
149                 ber_bvfree( hash );
150         }
151
152         return rc;
153 }