]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/passwd.c
Reengineered ldappasswd(1). Uses extended operation to set
[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 = LDAP_OPERATIONS_ERROR;
32         Entry *e;
33         struct berval *cred = NULL;
34
35         assert( oid != NULL );
36         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, oid ) == 0 );
37
38         Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: dn: %s\n",
39                 op->o_dn, 0, 0 );
40
41
42         cred = slap_passwd_generate( reqdata );
43         if( cred == NULL || cred->bv_len == 0 ) {
44                 *text = ch_strdup("password generation failed");
45                 return LDAP_OPERATIONS_ERROR;
46         }
47
48         Debug( LDAP_DEBUG_TRACE, "passwd: %s\n", cred->bv_val, 0, 0 );
49
50         e = dn2entry_w( be, op->o_ndn, NULL );
51
52         if( e == NULL ) {
53                 *text = ch_strdup("could not locate authorization entry");
54                 return LDAP_OPERATIONS_ERROR;
55         }
56
57         if( ! access_allowed( be, conn, op, e, "entry", NULL, ACL_WRITE ) ) {
58                 *text = ch_strdup("access to authorization entry denied");
59                 rc = LDAP_INSUFFICIENT_ACCESS;
60                 goto done;
61         }
62
63         if( is_entry_alias( e ) ) {
64                 /* entry is an alias, don't allow operation */
65                 *text = ch_strdup("authorization entry is alias");
66                 rc = LDAP_ALIAS_PROBLEM;
67                 goto done;
68         }
69
70         if( is_entry_referral( e ) ) {
71                 /* entry is an referral, don't allow operation */
72                 *text = ch_strdup("authorization entry is referral");
73                 goto done;
74         }
75
76         {
77                 LDAPModList ml;
78                 struct berval *vals[2];
79
80                 vals[0] = cred;
81                 vals[1] = NULL;
82
83                 ml.ml_type = ch_strdup("userPassword");
84                 ml.ml_bvalues = vals;
85                 ml.ml_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
86                 ml.ml_next = NULL;
87
88                 rc = ldbm_modify_internal( be,
89                         conn, op, op->o_ndn, &ml, e );
90
91                 ch_free(ml.ml_type);
92         }
93
94         if( rc == LDAP_SUCCESS ) {
95                 /* change the entry itself */
96                 if( id2entry_add( be, e ) != 0 ) {
97                         rc = LDAP_OPERATIONS_ERROR;
98                 }
99         }
100         
101 done:
102         cache_return_entry_w( &li->li_cache, e );
103
104         if( cred != NULL ) {
105                 ber_bvfree( cred );
106         }
107
108         return rc;
109 }