cancel.c 3.91 KB
Newer Older
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
1
/* cancel.c - LDAP cancel extended operation */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 1998-2020 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
#include <ac/unistd.h>

#include "slap.h"

#include <lber_pvt.h>
#include <lutil.h>

30
31
const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_CANCEL);

32
int cancel_extop( Operation *op, SlapReply *rs )
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
33
{
34
	Operation *o;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
35
36
	int rc;
	int opid;
37
38
	BerElementBuffer berbuf;
	BerElement *ber = (BerElement *)&berbuf;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
39

Howard Chu's avatar
Howard Chu committed
40
	assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
41

Howard Chu's avatar
Howard Chu committed
42
	if ( op->ore_reqdata == NULL ) {
43
		rs->sr_text = "no message ID supplied";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
44
45
46
		return LDAP_PROTOCOL_ERROR;
	}

47
48
49
	if ( op->ore_reqdata->bv_len == 0 ) {
		rs->sr_text = "empty request data field";
		return LDAP_PROTOCOL_ERROR;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
50
51
	}

52
53
54
	/* ber_init2 uses reqdata directly, doesn't allocate new buffers */
	ber_init2( ber, op->ore_reqdata, 0 );

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
55
	if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
56
		rs->sr_text = "message ID parse failed";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
57
58
59
		return LDAP_PROTOCOL_ERROR;
	}

60
61
62
	Statslog( LDAP_DEBUG_STATS, "%s CANCEL msg=%d\n",
		op->o_log_prefix, opid, 0, 0, 0 );

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
63
	if ( opid < 0 ) {
64
		rs->sr_text = "message ID invalid";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
65
66
67
		return LDAP_PROTOCOL_ERROR;
	}

Howard Chu's avatar
Howard Chu committed
68
69
70
71
72
	if ( opid == op->o_msgid ) {
		op->o_cancel = SLAP_CANCEL_DONE;
		return LDAP_SUCCESS;
	}

73
	ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
74

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
75
76
77
78
79
80
81
82
83
	if ( op->o_abandon ) {
		/* FIXME: Should instead reject the cancel/abandon of this op, but
		 * it seems unsafe to reset op->o_abandon once it is set. ITS#6138.
		 */
		rc = LDAP_OPERATIONS_ERROR;
		rs->sr_text = "tried to abandon or cancel this operation";
		goto out;
	}

84
85
	LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
		if ( o->o_msgid == opid ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
86
87
88
89
90
			/* TODO: We could instead remove the cancelled operation
			 * from c_pending_ops like Abandon does, and send its
			 * response here.  Not if it is pending because of a
			 * congested connection though.
			 */
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
91
			rc = LDAP_CANNOT_CANCEL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
92
			rs->sr_text = "too busy for Cancel, try Abandon instead";
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
93
			goto out;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
94
95
96
		}
	}

97
98
	LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
		if ( o->o_msgid == opid ) {
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
99
100
101
102
			break;
		}
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
103
104
105
	if ( o == NULL ) {
	 	rc = LDAP_NO_SUCH_OPERATION;
		rs->sr_text = "message ID not found";
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
106
107
108
109
110
111

	} else if ( o->o_tag == LDAP_REQ_BIND
			|| o->o_tag == LDAP_REQ_UNBIND
			|| o->o_tag == LDAP_REQ_ABANDON ) {
		rc = LDAP_CANNOT_CANCEL;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
112
	} else if ( o->o_cancel != SLAP_CANCEL_NONE ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
113
		rc = LDAP_OPERATIONS_ERROR;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
114
		rs->sr_text = "message ID already being cancelled";
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
115
116
117
118
119
120
121
122
123

#if 0
	} else if ( o->o_abandon ) {
		/* TODO: Would this break something when
		 * o_abandon="suppress response"? (ITS#6138)
		 */
		rc = LDAP_TOO_LATE;
#endif

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
124
125
	} else {
		rc = LDAP_SUCCESS;
126
		o->o_cancel = SLAP_CANCEL_REQ;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
127
128
129
		o->o_abandon = 1;
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
130
 out:
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
131
	ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
132

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
138
			op->oq_cancel.rs_msgid = opid;
			if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
139
140
141
				return LDAP_SUCCESS;
			}
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
142

143
144
145
146
147
148
149
150
151
152
		do {
			/* Fake a cond_wait with thread_yield, then
			 * verify the result properly mutex-protected.
			 */
			while ( o->o_cancel == SLAP_CANCEL_REQ )
				ldap_pvt_thread_yield();
			ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
			rc = o->o_cancel;
			ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
		} while ( rc == SLAP_CANCEL_REQ );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
153

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
154
		if ( rc == SLAP_CANCEL_ACK ) {
155
156
			rc = LDAP_SUCCESS;
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
157

158
		o->o_cancel = SLAP_CANCEL_DONE;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
159
160
161
162
	}

	return rc;
}