]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/passwd.c
Rough in passwd and referral routines
[openldap] / servers / slapd / back-bdb / passwd.c
1 /* passwd.c - bdb backend password 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 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int
16 bdb_exop_passwd(
17     Backend             *be,
18     Connection          *conn,
19     Operation           *op,
20         const char              *reqoid,
21     struct berval       *reqdata,
22         char                    **rspoid,
23     struct berval       **rspdata,
24         LDAPControl             *** rspctrls,
25         const char              **text,
26     struct berval       *** refs
27 )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         int rc;
31         Entry *e = NULL;
32         struct berval *hash = NULL;
33         DB_TXN *ltid;
34
35         struct berval *id = NULL;
36         struct berval *new = NULL;
37
38         char *dn;
39
40         assert( reqoid != NULL );
41         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
42
43         rc = slap_passwd_parse( reqdata,
44                 &id, NULL, &new, text );
45
46         Debug( LDAP_DEBUG_ARGS, "==> bdb_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                 new = slap_passwd_generate();
55
56                 if( new == NULL || new->bv_len == 0 ) {
57                         *text = "password generation failed.";
58                         rc = LDAP_OTHER;
59                         goto done;
60                 }
61                 
62                 *rspdata = slap_passwd_return( new );
63         }
64
65         hash = slap_passwd_hash( new );
66
67         if( hash == NULL || hash->bv_len == 0 ) {
68                 *text = "password hash failed";
69                 rc = LDAP_OTHER;
70                 goto done;
71         }
72
73         dn = id ? id->bv_val : op->o_dn;
74
75         Debug( LDAP_DEBUG_TRACE, "passwd: \"%s\"%s\n",
76                 dn, id ? " (proxy)" : "", 0 );
77
78         if( dn == NULL || dn[0] == '\0' ) {
79                 *text = "No password is associated with the Root DSE";
80                 rc = LDAP_OPERATIONS_ERROR;
81                 goto done;
82         }
83
84         /* fetch entry */
85         rc = dn2entry_w( be, NULL, dn, &e, NULL );
86
87         switch(rc) {
88         case DB_NOTFOUND:
89         case 0:
90                 break;
91         default:
92                 send_ldap_result( conn, op, rc=LDAP_OTHER,
93                     NULL, "internal error", NULL, NULL );
94                 return rc;
95         }
96
97         if( e == NULL ) {
98                 *text = "could not locate authorization entry";
99                 rc = LDAP_OPERATIONS_ERROR;
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 = bdb_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( bdb_id2entry_add( be, ltid, e ) != 0 ) {
138                         *text = "entry update failed";
139                         rc = LDAP_OTHER;
140                 }
141         }
142         
143 done:
144         if( e != NULL ) {
145                 bdb_entry_return( be, 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 }