]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Modify password code such that backend end routine calls into
[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            *oid,
25     struct berval       *reqdata,
26     struct berval       **rspdata,
27         char**  text
28 )
29 {
30         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31         int rc;
32         Entry *e = NULL;
33         struct berval *hash = NULL;
34
35         struct berval *id = NULL;
36         struct berval *new = NULL;
37
38         char *dn;
39
40         assert( oid != NULL );
41         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, oid ) == 0 );
42
43         rc = slap_passwd_parse( reqdata,
44                 &id, NULL, &new, text );
45
46         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
47                 id ? id->bv_val : "", 0, 0 );
48
49         if( rc != LDAP_SUCCESS ) {
50                 goto done;
51         }
52
53         if( new == NULL || new->bv_len == 0 ) {
54                 *text = ch_strdup("no password provided");
55                 rc = LDAP_OPERATIONS_ERROR;
56                 goto done;
57         }
58
59         hash = slap_passwd_generate( new );
60
61         if( hash == NULL || hash->bv_len == 0 ) {
62                 *text = ch_strdup("password generation failed");
63                 rc = LDAP_OPERATIONS_ERROR;
64                 goto done;
65         }
66
67         dn = id ? id->bv_val : op->o_dn;
68
69         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
70                 dn, id ? " (proxy)" : "", 0 );
71
72         if( dn == NULL || dn[0] == NULL ) {
73                 *text = ch_strdup("No password is associated with the Root DSE");
74                 rc = LDAP_OPERATIONS_ERROR;
75                 goto done;
76         }
77
78         e = dn2entry_w( be,
79                 id ? id->bv_val : op->o_dn,
80                 NULL );
81
82         if( e == NULL ) {
83                 *text = ch_strdup("could not locate authorization entry");
84                 rc = LDAP_OPERATIONS_ERROR;
85                 goto done;
86         }
87
88         if( ! access_allowed( be, conn, op, e, "entry", NULL, ACL_WRITE ) ) {
89                 *text = ch_strdup("access to authorization entry denied");
90                 rc = LDAP_INSUFFICIENT_ACCESS;
91                 goto done;
92         }
93
94         if( is_entry_alias( e ) ) {
95                 /* entry is an alias, don't allow operation */
96                 *text = ch_strdup("authorization entry is alias");
97                 rc = LDAP_ALIAS_PROBLEM;
98                 goto done;
99         }
100
101         rc = LDAP_OPERATIONS_ERROR;
102
103         if( is_entry_referral( e ) ) {
104                 /* entry is an referral, don't allow operation */
105                 *text = ch_strdup("authorization entry is referral");
106                 goto done;
107         }
108
109         {
110                 LDAPModList ml;
111                 struct berval *vals[2];
112
113                 vals[0] = hash;
114                 vals[1] = NULL;
115
116                 ml.ml_type = ch_strdup("userPassword");
117                 ml.ml_bvalues = vals;
118                 ml.ml_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
119                 ml.ml_next = NULL;
120
121                 rc = ldbm_modify_internal( be,
122                         conn, op, op->o_ndn, &ml, e );
123
124                 ch_free(ml.ml_type);
125         }
126
127         if( rc == LDAP_SUCCESS ) {
128                 /* change the entry itself */
129                 if( id2entry_add( be, e ) != 0 ) {
130                         rc = LDAP_OPERATIONS_ERROR;
131                 }
132         }
133         
134 done:
135         if( e != NULL ) {
136                 cache_return_entry_w( &li->li_cache, e );
137         }
138
139         if( id != NULL ) {
140                 ber_bvfree( id );
141         }
142
143         if( new != NULL ) {
144                 ber_bvfree( new );
145         }
146
147         if( hash != NULL ) {
148                 ber_bvfree( hash );
149         }
150
151         return rc;
152 }