1 /* extended.c - ldap backend extended routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
25 #include <ac/string.h>
28 #include "back-ldap.h"
31 typedef int (ldap_back_exop_f)( Operation *op, SlapReply *rs, ldapconn_t **lc );
33 static ldap_back_exop_f ldap_back_exop_passwd;
34 static ldap_back_exop_f ldap_back_exop_generic;
38 ldap_back_exop_f *extended;
40 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), ldap_back_exop_passwd },
45 ldap_back_extended_one( Operation *op, SlapReply *rs, ldap_back_exop_f exop )
47 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
49 ldapconn_t *lc = NULL;
50 LDAPControl **oldctrls = NULL;
53 /* FIXME: this needs to be called here, so it is
54 * called twice; maybe we could avoid the
55 * ldap_back_dobind() call inside each extended()
57 if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
61 oldctrls = op->o_ctrls;
62 if ( ldap_back_proxy_authz_ctrl( &lc->lc_bound_ndn,
63 li->li_version, &li->li_idassert, op, rs, &op->o_ctrls ) )
65 op->o_ctrls = oldctrls;
66 send_ldap_extended( op, rs );
68 /* otherwise frontend resends result */
69 rc = rs->sr_err = SLAPD_ABANDON;
73 rc = exop( op, rs, &lc );
75 if ( op->o_ctrls && op->o_ctrls != oldctrls ) {
76 free( op->o_ctrls[ 0 ] );
79 op->o_ctrls = oldctrls;
83 ldap_back_release_conn( li, lc );
96 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
97 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
99 return ldap_back_extended_one( op, rs, exop_table[i].extended );
103 /* if we get here, the exop is known; the best that we can do
104 * is pass it thru as is */
105 /* FIXME: maybe a list of OIDs to pass thru would be safer */
106 return ldap_back_extended_one( op, rs, ldap_back_exop_generic );
110 ldap_back_exop_passwd(
115 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
117 ldapconn_t *lc = *lcp;
118 req_pwdexop_s *qpw = &op->oq_pwdexop;
121 int rc, isproxy, freedn = 0;
124 struct berval dn = op->o_req_dn,
127 assert( lc != NULL );
129 if ( BER_BVISNULL( &ndn ) && op->ore_reqdata != NULL ) {
130 /* NOTE: most of this code is mutuated
131 * from slap_passwd_parse(); we can't call
132 * that function since now the request data
133 * has been destroyed by NULL-terminating
134 * the bervals. Luckily enough, we only need
135 * the first berval... */
139 BerElementBuffer berbuf;
140 BerElement *ber = (BerElement *)&berbuf;
142 struct berval tmpid = BER_BVNULL;
144 if ( op->ore_reqdata->bv_len == 0 ) {
145 return LDAP_PROTOCOL_ERROR;
148 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
149 ber_init2( ber, op->ore_reqdata, 0 );
151 tag = ber_scanf( ber, "{" /*}*/ );
153 if ( tag == LBER_ERROR ) {
154 return LDAP_PROTOCOL_ERROR;
157 tag = ber_peek_tag( ber, &len );
158 if ( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
159 tag = ber_scanf( ber, "m", &tmpid );
161 if ( tag == LBER_ERROR ) {
162 return LDAP_PROTOCOL_ERROR;
166 if ( !BER_BVISEMPTY( &tmpid ) ) {
167 rs->sr_err = dnPrettyNormal( NULL, &tmpid, &dn,
168 &ndn, op->o_tmpmemctx );
169 if ( rs->sr_err != LDAP_SUCCESS ) {
170 /* should have been successfully parsed earlier! */
181 isproxy = ber_bvcmp( &ndn, &op->o_ndn );
183 Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
184 dn.bv_val, isproxy ? " (proxy)" : "", 0 );
187 rc = ldap_passwd( lc->lc_ld, isproxy ? &dn : NULL,
188 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
189 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
190 op->o_ctrls, NULL, &msgid );
192 if ( rc == LDAP_SUCCESS ) {
193 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
194 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
198 /* only touch when activity actually took place... */
199 if ( li->li_idle_timeout && lc ) {
200 lc->lc_time = op->o_time;
203 /* sigh. parse twice, because parse_passwd
204 * doesn't give us the err / match / msg info.
206 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
207 (char **)&rs->sr_matched,
211 if ( rc == LDAP_SUCCESS ) {
212 if ( rs->sr_err == LDAP_SUCCESS ) {
215 /* this never happens because
216 * the frontend is generating
217 * the new password, so when
218 * the passwd exop is proxied,
219 * it never delegates password
220 * generation to the remote server
222 rc = ldap_parse_passwd( lc->lc_ld, res,
224 if ( rc == LDAP_SUCCESS &&
225 !BER_BVISNULL( &newpw ) )
227 rs->sr_type = REP_EXTENDED;
228 rs->sr_rspdata = slap_passwd_return( &newpw );
229 free( newpw.bv_val );
240 if ( rc != LDAP_SUCCESS ) {
241 rs->sr_err = slap_map_api2result( rs );
242 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
244 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
249 if ( LDAP_BACK_QUARANTINE( li ) ) {
250 ldap_back_quarantine( op, rs );
253 if ( text ) rs->sr_text = text;
254 send_ldap_extended( op, rs );
255 /* otherwise frontend resends result */
256 rc = rs->sr_err = SLAPD_ABANDON;
258 } else if ( LDAP_BACK_QUARANTINE( li ) ) {
259 ldap_back_quarantine( op, rs );
263 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
264 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
267 /* these have to be freed anyway... */
268 if ( rs->sr_matched ) {
269 free( (char *)rs->sr_matched );
270 rs->sr_matched = NULL;
278 /* in case, cleanup handler */
287 ldap_back_exop_generic(
292 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
294 ldapconn_t *lc = *lcp;
301 assert( lc != NULL );
303 Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_generic(%s, \"%s\")\n",
304 op->ore_reqoid.bv_val, op->o_req_dn.bv_val, 0 );
307 rc = ldap_extended_operation( lc->lc_ld,
308 op->ore_reqoid.bv_val, op->ore_reqdata,
309 op->o_ctrls, NULL, &msgid );
311 if ( rc == LDAP_SUCCESS ) {
312 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
313 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
317 /* only touch when activity actually took place... */
318 if ( li->li_idle_timeout && lc ) {
319 lc->lc_time = op->o_time;
322 /* sigh. parse twice, because parse_passwd
323 * doesn't give us the err / match / msg info.
325 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
326 (char **)&rs->sr_matched,
329 if ( rc == LDAP_SUCCESS ) {
330 if ( rs->sr_err == LDAP_SUCCESS ) {
331 rc = ldap_parse_extended_result( lc->lc_ld, res,
332 (char **)&rs->sr_rspoid, &rs->sr_rspdata, 0 );
333 if ( rc == LDAP_SUCCESS ) {
334 rs->sr_type = REP_EXTENDED;
345 if ( rc != LDAP_SUCCESS ) {
346 rs->sr_err = slap_map_api2result( rs );
347 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
349 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
354 if ( LDAP_BACK_QUARANTINE( li ) ) {
355 ldap_back_quarantine( op, rs );
358 if ( text ) rs->sr_text = text;
359 send_ldap_extended( op, rs );
360 /* otherwise frontend resends result */
361 rc = rs->sr_err = SLAPD_ABANDON;
363 } else if ( LDAP_BACK_QUARANTINE( li ) ) {
364 ldap_back_quarantine( op, rs );
367 /* these have to be freed anyway... */
368 if ( rs->sr_matched ) {
369 free( (char *)rs->sr_matched );
370 rs->sr_matched = NULL;
378 /* in case, cleanup handler */