]> 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-2005 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
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "back-ldap.h"
29 #include "lber_pvt.h"
30
31 BI_op_extended ldap_back_exop_passwd;
32
33 static struct exop {
34         struct berval   *oid;
35         BI_op_extended  *extended;
36 } exop_table[] = {
37         { (struct berval *)&slap_EXOP_MODIFY_PASSWD, ldap_back_exop_passwd },
38         { NULL, NULL }
39 };
40
41 int
42 ldap_back_extended(
43                 Operation       *op,
44                 SlapReply       *rs )
45 {
46         int     i;
47
48         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
49                 if ( bvmatch( exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
50                 {
51                         struct ldapconn *lc;
52                         LDAPControl     **oldctrls = NULL;
53                         int             rc;
54
55                         /* FIXME: this needs to be called here, so it is
56                          * called twice; maybe we could avoid the 
57                          * ldap_back_dobind() call inside each extended()
58                          * call ... */
59                         lc = ldap_back_getconn( op, rs );
60                         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
61                                 return -1;
62                         }
63
64                         oldctrls = op->o_ctrls;
65                         if ( ldap_back_proxy_authz_ctrl( lc, op, rs,
66                                                 &op->o_ctrls ) )
67                         {
68                                 op->o_ctrls = oldctrls;
69                                 send_ldap_result( op, rs );
70                                 rs->sr_text = NULL;
71                                 return rs->sr_err;
72                         }
73
74                         rc = ( *exop_table[i].extended )( op, rs );
75
76                         if ( op->o_ctrls && op->o_ctrls != oldctrls ) {
77                                 free( op->o_ctrls[ 0 ] );
78                                 free( op->o_ctrls );
79                         }
80                         op->o_ctrls = oldctrls;
81
82                         return rc;
83                 }
84         }
85
86         rs->sr_text = "not supported within naming context";
87         return LDAP_UNWILLING_TO_PERFORM;
88 }
89
90 int
91 ldap_back_exop_passwd(
92                 Operation       *op,
93                 SlapReply       *rs )
94 {
95         struct ldapconn *lc;
96         req_pwdexop_s   *qpw = &op->oq_pwdexop;
97         LDAPMessage     *res;
98         ber_int_t       msgid;
99         int             rc, isproxy;
100         int             do_retry = 1;
101
102         lc = ldap_back_getconn( op, rs );
103         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
104                 return -1;
105         }
106
107         isproxy = ber_bvcmp( &op->o_req_ndn, &op->o_ndn );
108
109         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
110                 op->o_req_dn.bv_val, isproxy ? " (proxy)" : "", 0 );
111
112 retry:
113         rc = ldap_passwd( lc->lc_ld, isproxy ? &op->o_req_dn : NULL,
114                 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
115                 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
116                 op->o_ctrls, NULL, &msgid );
117
118         if ( rc == LDAP_SUCCESS ) {
119                 if ( ldap_result( lc->lc_ld, msgid, 1, NULL, &res ) == -1 ) {
120                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
121                         ldap_back_freeconn( op, lc );
122                         lc = NULL;
123
124                 } else {
125                         /* sigh. parse twice, because parse_passwd
126                          * doesn't give us the err / match / msg info.
127                          */
128                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
129                                         (char **)&rs->sr_matched,
130                                         (char **)&rs->sr_text,
131                                         NULL, NULL, 0 );
132                         if ( rc == LDAP_SUCCESS ) {
133                                 if ( rs->sr_err == LDAP_SUCCESS ) {
134                                         struct berval   newpw;
135                                         
136                                         rc = ldap_parse_passwd( lc->lc_ld, res,
137                                                         &newpw);
138                                         if ( rc == LDAP_SUCCESS &&
139                                                         !BER_BVISNULL( &newpw ) )
140                                         {
141                                                 rs->sr_type = REP_EXTENDED;
142                                                 rs->sr_rspdata = slap_passwd_return( &newpw );
143                                                 free( newpw.bv_val );
144                                         }
145
146                                 } else {
147                                         rc = rs->sr_err;
148                                 }
149                         }
150                         ldap_msgfree( res );
151                 }
152         }
153         if ( rc != LDAP_SUCCESS ) {
154                 rs->sr_err = slap_map_api2result( rs );
155                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
156                         do_retry = 0;
157                         if ( ldap_back_retry(lc, op, rs ) ) {
158                                 goto retry;
159                         }
160                 }
161                 send_ldap_result( op, rs );
162                 if ( rs->sr_matched ) {
163                         free( (char *)rs->sr_matched );
164                 }
165                 if ( rs->sr_text ) {
166                         free( (char *)rs->sr_text );
167                 }
168                 rs->sr_matched = NULL;
169                 rs->sr_text = NULL;
170                 rc = -1;
171         }
172
173         return rc;
174 }