]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/extended.c
Free IDL_CACHE locks
[openldap] / servers / slapd / back-ldap / extended.c
1 /* extended.c - ldap backend extended routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 "slap.h"
14 #include "back-ldap.h"
15 #include "lber_pvt.h"
16
17 BI_op_extended ldap_back_exop_passwd;
18
19 static struct exop {
20         struct berval *oid;
21         BI_op_extended  *extended;
22 } exop_table[] = {
23         { (struct berval *)&slap_EXOP_MODIFY_PASSWD, ldap_back_exop_passwd },
24         { NULL, NULL }
25 };
26
27 int
28 ldap_back_extended(
29         Operation               *op,
30         SlapReply               *rs )
31 {
32         int i;
33
34         for( i=0; exop_table[i].extended != NULL; i++ ) {
35                 if( ber_bvcmp( exop_table[i].oid, &op->oq_extended.rs_reqoid ) == 0 ) {
36                         return (exop_table[i].extended)( op, rs );
37                 }
38         }
39
40         rs->sr_text = "not supported within naming context";
41         return LDAP_UNWILLING_TO_PERFORM;
42 }
43
44 int
45 ldap_back_exop_passwd(
46         Operation               *op,
47         SlapReply               *rs )
48 {
49         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
50         struct ldapconn *lc;
51         struct berval id = { 0, NULL };
52         struct berval old = { 0, NULL };
53         struct berval new = { 0, NULL };
54         struct berval dn, mdn = { 0, NULL }, newpw;
55         LDAPMessage *res;
56         ber_int_t msgid;
57         int rc;
58         dncookie dc;
59
60         lc = ldap_back_getconn(op, rs);
61         if (!lc || !ldap_back_dobind(lc, op, rs) ) {
62                 return -1;
63         }
64
65         rc = slap_passwd_parse( op->oq_extended.rs_reqdata, &id, &old, &new, &rs->sr_text );
66         if (rc != LDAP_SUCCESS)
67                 return rc;
68         
69         if (id.bv_len) {
70                 dn = id;
71         } else {
72                 dn = op->o_dn;
73         }
74
75 #ifdef NEW_LOGGING
76         LDAP_LOG ( ACL, DETAIL1, "ldap_back_exop_passwd: \"%s\"%s\"\n",
77                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
78 #else
79         Debug( LDAP_DEBUG_TRACE, "ldap_back_exop_passwd: \"%s\"%s\n",
80                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
81 #endif
82
83         if (dn.bv_len == 0) {
84                 rs->sr_text = "No password is associated with the Root DSE";
85                 return LDAP_UNWILLING_TO_PERFORM;
86         }
87         if (id.bv_len) {
88                 dc.rwmap = &li->rwmap;
89 #ifdef ENABLE_REWRITE
90                 dc.conn = op->o_conn;
91                 dc.rs = rs;
92                 dc.ctx = "modifyPwd";
93 #else
94                 dc.tofrom = 1;
95                 dc.normalized = 0;
96 #endif
97                 if ( ldap_back_dn_massage( &dc, &dn, &mdn ) ) {
98                         send_ldap_result( op, rs );
99                         return -1;
100                 }
101         }
102
103         rc = ldap_passwd(lc->ld, id.bv_len ? &mdn : NULL, old.bv_len ? &old : NULL,
104                 new.bv_len ? &new : NULL, op->o_ctrls, NULL, &msgid);
105
106         if (mdn.bv_val != dn.bv_val) {
107                 free(mdn.bv_val);
108         }
109
110         if (rc == LDAP_SUCCESS) {
111                 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
112                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &rc);
113                 } else {
114                         /* sigh. parse twice, because parse_passwd doesn't give
115                          * us the err / match / msg info.
116                          */
117                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text,
118                                 NULL, NULL, 0);
119                         if (rc == LDAP_SUCCESS) {
120                                 if (rs->sr_err == LDAP_SUCCESS) {
121                                         rc = ldap_parse_passwd(lc->ld, res, &newpw);
122                                         if (rc == LDAP_SUCCESS && newpw.bv_val) {
123                                                 rs->sr_type = REP_EXTENDED;
124                                                 rs->sr_rspdata = slap_passwd_return(&newpw);
125                                                 free(newpw.bv_val);
126                                         }
127                                 } else {
128                                         rc = rs->sr_err;
129                                 }
130                         }
131                         ldap_msgfree(res);
132                 }
133         }
134         if (rc != LDAP_SUCCESS) {
135                 rs->sr_err = ldap_back_map_result(rs);
136                 send_ldap_result(op, rs);
137                 if (rs->sr_matched) free((char *)rs->sr_matched);
138                 if (rs->sr_text) free((char *)rs->sr_text);
139                 rs->sr_matched = NULL;
140                 rs->sr_text = NULL;
141                 rc = -1;
142         }
143         return rc;
144 }