1 /* extended.c - ldap backend extended routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2011 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 **ctrls = NULL, **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 ) ) {
62 if ( ldap_back_controls_add( op, rs, lc, &ctrls ) )
64 op->o_ctrls = oldctrls;
65 send_ldap_extended( op, rs );
67 /* otherwise frontend resends result */
68 rc = rs->sr_err = SLAPD_ABANDON;
73 rc = exop( op, rs, &lc );
75 op->o_ctrls = oldctrls;
76 (void)ldap_back_controls_free( op, rs, &ctrls );
80 ldap_back_release_conn( li, lc );
93 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
94 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
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 );
128 assert( rs->sr_ctrls == NULL );
130 if ( BER_BVISNULL( &ndn ) && op->ore_reqdata != NULL ) {
131 /* NOTE: most of this code is mutated
132 * from slap_passwd_parse();
133 * But here we only need
134 * the first berval... */
138 BerElementBuffer berbuf;
139 BerElement *ber = (BerElement *)&berbuf;
141 struct berval tmpid = BER_BVNULL;
143 if ( op->ore_reqdata->bv_len == 0 ) {
144 return LDAP_PROTOCOL_ERROR;
147 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
148 ber_init2( ber, op->ore_reqdata, 0 );
150 tag = ber_scanf( ber, "{" /*}*/ );
152 if ( tag == LBER_ERROR ) {
153 return LDAP_PROTOCOL_ERROR;
156 tag = ber_peek_tag( ber, &len );
157 if ( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
158 tag = ber_get_stringbv( ber, &tmpid, LBER_BV_NOTERM );
160 if ( tag == LBER_ERROR ) {
161 return LDAP_PROTOCOL_ERROR;
165 if ( !BER_BVISEMPTY( &tmpid ) ) {
166 char idNull = tmpid.bv_val[tmpid.bv_len];
167 tmpid.bv_val[tmpid.bv_len] = '\0';
168 rs->sr_err = dnPrettyNormal( NULL, &tmpid, &dn,
169 &ndn, op->o_tmpmemctx );
170 tmpid.bv_val[tmpid.bv_len] = idNull;
171 if ( rs->sr_err != LDAP_SUCCESS ) {
172 /* should have been successfully parsed earlier! */
183 isproxy = ber_bvcmp( &ndn, &op->o_ndn );
185 Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
186 dn.bv_val, isproxy ? " (proxy)" : "", 0 );
189 rc = ldap_passwd( lc->lc_ld, isproxy ? &dn : NULL,
190 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
191 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
192 op->o_ctrls, NULL, &msgid );
194 if ( rc == LDAP_SUCCESS ) {
195 /* TODO: set timeout? */
196 /* by now, make sure no timeout is used (ITS#6282) */
197 struct timeval tv = { -1, 0 };
198 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) == -1 ) {
199 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
203 /* only touch when activity actually took place... */
204 if ( li->li_idle_timeout && lc ) {
205 lc->lc_time = op->o_time;
208 /* sigh. parse twice, because parse_passwd
209 * doesn't give us the err / match / msg info.
211 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
212 (char **)&rs->sr_matched,
214 NULL, &rs->sr_ctrls, 0 );
216 if ( rc == LDAP_SUCCESS ) {
217 if ( rs->sr_err == LDAP_SUCCESS ) {
220 /* this never happens because
221 * the frontend is generating
222 * the new password, so when
223 * the passwd exop is proxied,
224 * it never delegates password
225 * generation to the remote server
227 rc = ldap_parse_passwd( lc->lc_ld, res,
229 if ( rc == LDAP_SUCCESS &&
230 !BER_BVISNULL( &newpw ) )
232 rs->sr_type = REP_EXTENDED;
233 rs->sr_rspdata = slap_passwd_return( &newpw );
234 free( newpw.bv_val );
245 if ( rc != LDAP_SUCCESS ) {
246 rs->sr_err = slap_map_api2result( rs );
247 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
249 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
254 if ( LDAP_BACK_QUARANTINE( li ) ) {
255 ldap_back_quarantine( op, rs );
258 if ( text ) rs->sr_text = text;
259 send_ldap_extended( op, rs );
260 /* otherwise frontend resends result */
261 rc = rs->sr_err = SLAPD_ABANDON;
263 } else if ( LDAP_BACK_QUARANTINE( li ) ) {
264 ldap_back_quarantine( op, rs );
268 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
269 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
272 /* these have to be freed anyway... */
273 if ( rs->sr_matched ) {
274 free( (char *)rs->sr_matched );
275 rs->sr_matched = NULL;
278 if ( rs->sr_ctrls ) {
279 ldap_controls_free( rs->sr_ctrls );
288 /* in case, cleanup handler */
297 ldap_back_exop_generic(
302 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
304 ldapconn_t *lc = *lcp;
311 Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_generic(%s, \"%s\")\n",
312 op->ore_reqoid.bv_val, op->o_req_dn.bv_val, 0 );
313 assert( lc != NULL );
314 assert( rs->sr_ctrls == NULL );
317 rc = ldap_extended_operation( lc->lc_ld,
318 op->ore_reqoid.bv_val, op->ore_reqdata,
319 op->o_ctrls, NULL, &msgid );
321 if ( rc == LDAP_SUCCESS ) {
322 /* TODO: set timeout? */
323 /* by now, make sure no timeout is used (ITS#6282) */
324 struct timeval tv = { -1, 0 };
325 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) == -1 ) {
326 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
330 /* only touch when activity actually took place... */
331 if ( li->li_idle_timeout && lc ) {
332 lc->lc_time = op->o_time;
335 /* sigh. parse twice, because parse_passwd
336 * doesn't give us the err / match / msg info.
338 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
339 (char **)&rs->sr_matched,
341 NULL, &rs->sr_ctrls, 0 );
342 if ( rc == LDAP_SUCCESS ) {
343 if ( rs->sr_err == LDAP_SUCCESS ) {
344 rc = ldap_parse_extended_result( lc->lc_ld, res,
345 (char **)&rs->sr_rspoid, &rs->sr_rspdata, 0 );
346 if ( rc == LDAP_SUCCESS ) {
347 rs->sr_type = REP_EXTENDED;
358 if ( rc != LDAP_SUCCESS ) {
359 rs->sr_err = slap_map_api2result( rs );
360 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
362 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
367 if ( LDAP_BACK_QUARANTINE( li ) ) {
368 ldap_back_quarantine( op, rs );
371 if ( text ) rs->sr_text = text;
372 send_ldap_extended( op, rs );
373 /* otherwise frontend resends result */
374 rc = rs->sr_err = SLAPD_ABANDON;
376 } else if ( LDAP_BACK_QUARANTINE( li ) ) {
377 ldap_back_quarantine( op, rs );
380 /* these have to be freed anyway... */
381 if ( rs->sr_matched ) {
382 free( (char *)rs->sr_matched );
383 rs->sr_matched = NULL;
386 if ( rs->sr_ctrls ) {
387 ldap_controls_free( rs->sr_ctrls );
396 /* in case, cleanup handler */