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