]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/extended.c
Sync with HEAD
[openldap] / servers / slapd / back-ldap / extended.c
1 /* extended.c - ldap backend extended routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Howard Chu for inclusion
18  * in OpenLDAP Software and subsequently enhanced by Pierangelo
19  * Masarati. 
20  */
21 /* This is an altered version */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <ac/string.h>
27
28 #include "slap.h"
29 #include "back-ldap.h"
30 #include "lber_pvt.h"
31
32 BI_op_extended ldap_back_exop_passwd;
33
34 static struct exop {
35         struct berval *oid;
36         BI_op_extended  *extended;
37 } exop_table[] = {
38         { (struct berval *)&slap_EXOP_MODIFY_PASSWD, ldap_back_exop_passwd },
39         { NULL, NULL }
40 };
41
42 int
43 ldap_back_extended(
44         Operation               *op,
45         SlapReply               *rs )
46 {
47         int i;
48
49         for( i=0; exop_table[i].extended != NULL; i++ ) {
50                 if( ber_bvcmp( exop_table[i].oid, &op->oq_extended.rs_reqoid ) == 0 ) {
51                         return (exop_table[i].extended)( op, rs );
52                 }
53         }
54
55         rs->sr_text = "not supported within naming context";
56         return LDAP_UNWILLING_TO_PERFORM;
57 }
58
59 int
60 ldap_back_exop_passwd(
61         Operation               *op,
62         SlapReply               *rs )
63 {
64         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
65         struct ldapconn *lc;
66         struct berval id = { 0, NULL };
67         struct berval old = { 0, NULL };
68         struct berval new = { 0, NULL };
69         struct berval dn, mdn = { 0, NULL }, newpw;
70         LDAPMessage *res;
71         ber_int_t msgid;
72         int rc;
73         dncookie dc;
74
75         lc = ldap_back_getconn(op, rs);
76         if (!lc || !ldap_back_dobind(lc, op, rs) ) {
77                 return -1;
78         }
79
80         rc = slap_passwd_parse( op->oq_extended.rs_reqdata, &id, &old, &new, &rs->sr_text );
81         if (rc != LDAP_SUCCESS)
82                 return rc;
83         
84         if (id.bv_len) {
85                 dn = id;
86         } else {
87                 dn = op->o_dn;
88         }
89
90 #ifdef NEW_LOGGING
91         LDAP_LOG ( ACL, DETAIL1, "ldap_back_exop_passwd: \"%s\"%s\"\n",
92                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
93 #else
94         Debug( LDAP_DEBUG_TRACE, "ldap_back_exop_passwd: \"%s\"%s\n",
95                 dn.bv_val, id.bv_len ? " (proxy)" : "", 0 );
96 #endif
97
98         if (dn.bv_len == 0) {
99                 rs->sr_text = "No password is associated with the Root DSE";
100                 return LDAP_UNWILLING_TO_PERFORM;
101         }
102         if (id.bv_len) {
103                 dc.rwmap = &li->rwmap;
104 #ifdef ENABLE_REWRITE
105                 dc.conn = op->o_conn;
106                 dc.rs = rs;
107                 dc.ctx = "modifyPwd";
108 #else
109                 dc.tofrom = 1;
110                 dc.normalized = 0;
111 #endif
112                 if ( ldap_back_dn_massage( &dc, &dn, &mdn ) ) {
113                         send_ldap_result( op, rs );
114                         return -1;
115                 }
116         }
117
118         rc = ldap_passwd(lc->ld, id.bv_len ? &mdn : NULL, old.bv_len ? &old : NULL,
119                 new.bv_len ? &new : NULL, op->o_ctrls, NULL, &msgid);
120
121         if (mdn.bv_val != dn.bv_val) {
122                 free(mdn.bv_val);
123         }
124
125         if (rc == LDAP_SUCCESS) {
126                 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
127                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &rc);
128                 } else {
129                         /* sigh. parse twice, because parse_passwd doesn't give
130                          * us the err / match / msg info.
131                          */
132                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text,
133                                 NULL, NULL, 0);
134                         if (rc == LDAP_SUCCESS) {
135                                 if (rs->sr_err == LDAP_SUCCESS) {
136                                         rc = ldap_parse_passwd(lc->ld, res, &newpw);
137                                         if (rc == LDAP_SUCCESS && newpw.bv_val) {
138                                                 rs->sr_type = REP_EXTENDED;
139                                                 rs->sr_rspdata = slap_passwd_return(&newpw);
140                                                 free(newpw.bv_val);
141                                         }
142                                 } else {
143                                         rc = rs->sr_err;
144                                 }
145                         }
146                         ldap_msgfree(res);
147                 }
148         }
149         if (rc != LDAP_SUCCESS) {
150                 rs->sr_err = ldap_back_map_result(rs);
151                 send_ldap_result(op, rs);
152                 if (rs->sr_matched) free((char *)rs->sr_matched);
153                 if (rs->sr_text) free((char *)rs->sr_text);
154                 rs->sr_matched = NULL;
155                 rs->sr_text = NULL;
156                 rc = -1;
157         }
158         return rc;
159 }