1 /* $NetBSD: cancel.c,v 1.2 2020/08/11 13:15:39 christos Exp $ */ 2 3 /* cancel.c - LDAP cancel extended operation */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2020 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 19 #include <sys/cdefs.h> 20 __RCSID("$NetBSD: cancel.c,v 1.2 2020/08/11 13:15:39 christos Exp $"); 21 22 #include "portable.h" 23 24 #include <stdio.h> 25 26 #include <ac/socket.h> 27 #include <ac/string.h> 28 #include <ac/unistd.h> 29 30 #include "slap.h" 31 32 #include <lber_pvt.h> 33 #include <lutil.h> 34 35 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_CANCEL); 36 37 int cancel_extop( Operation *op, SlapReply *rs ) 38 { 39 Operation *o; 40 int rc; 41 int opid; 42 BerElementBuffer berbuf; 43 BerElement *ber = (BerElement *)&berbuf; 44 45 assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 ); 46 47 if ( op->ore_reqdata == NULL ) { 48 rs->sr_text = "no message ID supplied"; 49 return LDAP_PROTOCOL_ERROR; 50 } 51 52 if ( op->ore_reqdata->bv_len == 0 ) { 53 rs->sr_text = "empty request data field"; 54 return LDAP_PROTOCOL_ERROR; 55 } 56 57 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */ 58 ber_init2( ber, op->ore_reqdata, 0 ); 59 60 if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) { 61 rs->sr_text = "message ID parse failed"; 62 return LDAP_PROTOCOL_ERROR; 63 } 64 65 Statslog( LDAP_DEBUG_STATS, "%s CANCEL msg=%d\n", 66 op->o_log_prefix, opid, 0, 0, 0 ); 67 68 if ( opid < 0 ) { 69 rs->sr_text = "message ID invalid"; 70 return LDAP_PROTOCOL_ERROR; 71 } 72 73 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex ); 74 75 if ( op->o_abandon ) { 76 /* FIXME: Should instead reject the cancel/abandon of this op, but 77 * it seems unsafe to reset op->o_abandon once it is set. ITS#6138. 78 */ 79 rc = LDAP_OPERATIONS_ERROR; 80 rs->sr_text = "tried to abandon or cancel this operation"; 81 goto out; 82 } 83 84 LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) { 85 if ( o->o_msgid == opid ) { 86 /* TODO: We could instead remove the cancelled operation 87 * from c_pending_ops like Abandon does, and send its 88 * response here. Not if it is pending because of a 89 * congested connection though. 90 */ 91 rc = LDAP_CANNOT_CANCEL; 92 rs->sr_text = "too busy for Cancel, try Abandon instead"; 93 goto out; 94 } 95 } 96 97 LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) { 98 if ( o->o_msgid == opid ) { 99 break; 100 } 101 } 102 103 if ( o == NULL ) { 104 rc = LDAP_NO_SUCH_OPERATION; 105 rs->sr_text = "message ID not found"; 106 107 } else if ( o->o_tag == LDAP_REQ_BIND 108 || o->o_tag == LDAP_REQ_UNBIND 109 || o->o_tag == LDAP_REQ_ABANDON ) { 110 rc = LDAP_CANNOT_CANCEL; 111 112 } else if ( o->o_cancel != SLAP_CANCEL_NONE ) { 113 rc = LDAP_OPERATIONS_ERROR; 114 rs->sr_text = "message ID already being cancelled"; 115 116 #if 0 117 } else if ( o->o_abandon ) { 118 /* TODO: Would this break something when 119 * o_abandon="suppress response"? (ITS#6138) 120 */ 121 rc = LDAP_TOO_LATE; 122 #endif 123 124 } else { 125 rc = LDAP_SUCCESS; 126 o->o_cancel = SLAP_CANCEL_REQ; 127 o->o_abandon = 1; 128 } 129 130 out: 131 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex ); 132 133 if ( rc == LDAP_SUCCESS ) { 134 LDAP_STAILQ_FOREACH( op->o_bd, &backendDB, be_next ) { 135 if( !op->o_bd->be_cancel ) continue; 136 137 op->oq_cancel.rs_msgid = opid; 138 if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) { 139 return LDAP_SUCCESS; 140 } 141 } 142 143 do { 144 /* Fake a cond_wait with thread_yield, then 145 * verify the result properly mutex-protected. 146 */ 147 while ( o->o_cancel == SLAP_CANCEL_REQ ) 148 ldap_pvt_thread_yield(); 149 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex ); 150 rc = o->o_cancel; 151 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex ); 152 } while ( rc == SLAP_CANCEL_REQ ); 153 154 if ( rc == SLAP_CANCEL_ACK ) { 155 rc = LDAP_SUCCESS; 156 } 157 158 o->o_cancel = SLAP_CANCEL_DONE; 159 } 160 161 return rc; 162 } 163