summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/operation.c
blob: 85a19a45814f155f4fc6f6ff11ba43c11a7a4265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* operation.c - routines to deal with pending ldap operations */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include "slap.h"

int
slapi_op_abandoned( Slapi_PBlock *pb )
{
	int	op_status;

	if (pb && pb->pb_op) {
		op_status = pb->pb_op->o_status;
		return( op_status == SLAPI_OP_STATUS_ABANDONED );
	}
	return 0;
}

void
operation_out_of_disk_space()
{
    LDAPDebug(LDAP_DEBUG_ANY, "*** DISK FULL ***\n", 0, 0, 0);
    LDAPDebug(LDAP_DEBUG_ANY, "Attempting to shut down gracefully.\n", 0, 0, 0);
    g_set_shutdown( SLAPI_SHUTDOWN_DISKFULL );
}

/* Setting the flags on the operation allows more control to the plugin
 * to disable and enable checks
 * Flags that we support  for setting in the operation  from the plugin are
 * SLAPI_OP_FLAG_NO_ACCESS_CHECK - do not check for access control
 * This function can be extended to support other flag setting as well
 */

void slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
{

	operation_set_flag(op, flag);

}

void slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
{
	operation_clear_flag(op, flag);

}

int slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
{
	return operation_is_flag_set(op, flag);
}



static int operation_type = -1; /* The type number assigned by the Factory for 'Operation' */

int
get_operation_object_type()
{
    if(operation_type==-1)
	{
	    /* The factory is given the name of the object type, in
		 * return for a type handle. Whenever the object is created
		 * or destroyed the factory is called with the handle so
		 * that it may call the constructors or destructors registered
		 * with it.
		 */
        operation_type= factory_register_type(SLAPI_EXT_OPERATION,offsetof(Operation,o_extension));
	}
    return operation_type;
}

/*
 * Allocate a new Slapi_Operation.
 * The flag parameter indicates whether the the operation is
 * external (from an LDAP Client), or internal (from a plugin).
 */
Slapi_Operation *
operation_new(int flags)
{
	/* To improve performance, we allocate the Operation, BerElement and
	 * ber buffer in one block, instead of a separate malloc() for each.
	 * Subsequently, ber_special_free() frees them all; we're careful
	 * not to free the Operation separately, and the ber software knows
	 * not to free the buffer separately.
	 */
	BerElement *ber = NULL;
	Slapi_Operation *o;
	if(flags & OP_FLAG_INTERNAL)
	{
	   	o = (Slapi_Operation *) slapi_ch_malloc(sizeof(Slapi_Operation));
	}
	else
	{
		o= (Slapi_Operation *) ber_special_alloc( sizeof(Slapi_Operation), &ber );
	}
	if (NULL != o)
	{
		memset(o,0,sizeof(Slapi_Operation));
		o->o_ber = ber;
		o->o_msgid = -1;
		o->o_tag = LBER_DEFAULT;
		o->o_status = SLAPI_OP_STATUS_PROCESSING;
		slapi_sdn_init(&(o->o_sdn));
		o->o_authtype = NULL;
		o->o_isroot = 0;
		o->o_time = current_time();
		o->o_opid = 0;
		o->o_connid = 0;
		o->o_next = NULL;
		o->o_flags= flags;
		if ( config_get_accesslog_level() & LDAP_DEBUG_TIMING ) {
                    o->o_interval = PR_IntervalNow();
                } else {
                    o->o_interval = (PRIntervalTime)0;
                }
	}
	return o;
}

void
operation_free( Slapi_Operation **op, Connection *conn )
{
	if(op!=NULL && *op!=NULL)
	{
		/* Call the plugin extension destructors */
	    factory_destroy_extension(get_operation_object_type(),*op,conn,&((*op)->o_extension));
		slapi_sdn_done(&(*op)->o_sdn);
		slapi_sdn_free(&(*op)->o_target_spec);
		slapi_ch_free_string( &(*op)->o_authtype );
		if ( (*op)->o_searchattrs != NULL ) {
			cool_charray_free( (*op)->o_searchattrs );
		}
		if ( NULL != (*op)->o_params.request_controls ) {
			ldap_controls_free( (*op)->o_params.request_controls );
		}
		if ( NULL != (*op)->o_results.result_controls ) {
			ldap_controls_free( (*op)->o_results.result_controls );
		}
		if(operation_is_flag_set(*op, OP_FLAG_INTERNAL))
		{
			slapi_ch_free((void**)op);
		}
		else
		{
			ber_special_free( *op , (*op)->o_ber);
		}
		/* counters_to_errors_log("after operation"); */
	}
}

void
slapi_operation_set_csngen_handler ( Slapi_Operation *op, void *callback )
{
	op->o_csngen_handler = (csngen_handler) callback;
}

void
slapi_operation_set_replica_attr_handler ( Slapi_Operation *op, void *callback )
{
	op->o_replica_attr_handler = (replica_attr_handler) callback;
}

int
slapi_operation_get_replica_attr ( Slapi_PBlock *pb, Slapi_Operation *op, const char *type, void *value )
{
	int rc = -1;

	if (op->o_replica_attr_handler)
	{
		rc = op->o_replica_attr_handler ( pb, type, value );
	}

	return rc;
}

CSN *
operation_get_csn(Slapi_Operation *op)
{
	return op->o_params.csn;
}

void
operation_set_csn(Slapi_Operation *op,CSN *csn)
{
	op->o_params.csn= csn;
}

unsigned long
slapi_op_get_type(Slapi_Operation *op)
{
	return op->o_params.operation_type;
}

char *
slapi_op_type_to_string(unsigned long type)
{
	switch (type) 
	{
		case SLAPI_OPERATION_ADD:
			return "add";
		case SLAPI_OPERATION_DELETE:
			return "delete";
		case SLAPI_OPERATION_MODIFY:
			return "modify";
		case SLAPI_OPERATION_MODRDN:
			return "modrdn";
		case SLAPI_OPERATION_BIND:
			return "bind";
		case SLAPI_OPERATION_COMPARE:
			return "compare";
		case SLAPI_OPERATION_SEARCH:
			return "search";
		default:
			return "unknown operation type";
	}
}

/* DEPRECATED : USE FUNCTION ABOVE FOR NEW DVLPT */
unsigned long
operation_get_type(Slapi_Operation *op)
{
	return op->o_params.operation_type;
}

void
operation_set_type(Slapi_Operation *op, unsigned long type)
{
	op->o_params.operation_type= type;
}

void
operation_set_flag(Slapi_Operation *op, int flag)
{
	op->o_flags|= flag;
}

void
operation_clear_flag(Slapi_Operation *op, int flag)
{
	op->o_flags &= ~flag;
}

int
operation_is_flag_set(Slapi_Operation *op, int flag)
{
	return op->o_flags & flag;
}

Slapi_DN* 
operation_get_target_spec (Slapi_Operation *op)
{
	return op->o_target_spec;
}

void 
operation_set_target_spec (Slapi_Operation *op, const Slapi_DN *target_spec)
{
	PR_ASSERT (op);
	PR_ASSERT (target_spec);

	op->o_target_spec = slapi_sdn_dup(target_spec);
}

void 
operation_set_target_spec_str (Slapi_Operation *op, const char *target_spec)
{
	PR_ASSERT (op);

	op->o_target_spec = slapi_sdn_new_dn_byval (target_spec);
}

unsigned long operation_get_abandoned_op (const Slapi_Operation *op)
{
	PR_ASSERT (op);

	return op->o_abandoned_op;
}

void operation_set_abandoned_op (Slapi_Operation *op, unsigned long abandoned_op)
{
	 PR_ASSERT (op);

	 op->o_abandoned_op = abandoned_op;
}

/* slapi_operation_parameters manipulation functions */

struct slapi_operation_parameters *operation_parameters_new()
{
	return (slapi_operation_parameters *)slapi_ch_calloc (1, sizeof (slapi_operation_parameters));
}

static LDAPMod **
copy_mods(LDAPMod **orig_mods)
{
	LDAPMod **new_mods = NULL;
	LDAPMod *mod;
	Slapi_Mods smods_old;
	Slapi_Mods smods_new;
	slapi_mods_init_byref(&smods_old,orig_mods);
	slapi_mods_init_passin(&smods_new,new_mods);
	mod= slapi_mods_get_first_mod(&smods_old);
	while(mod!=NULL)
	{
		slapi_mods_add_modbvps(&smods_new,mod->mod_op,mod->mod_type,mod->mod_bvalues);
		mod= slapi_mods_get_next_mod(&smods_old);
	}
	new_mods= slapi_mods_get_ldapmods_passout(&smods_new);
	slapi_mods_done(&smods_old);
	slapi_mods_done(&smods_new);
	return new_mods;
}

struct slapi_operation_parameters *
operation_parameters_dup(struct slapi_operation_parameters *sop)
{
	struct slapi_operation_parameters *sop_new= (struct slapi_operation_parameters *)
		slapi_ch_malloc(sizeof(struct slapi_operation_parameters));
	memcpy(sop_new,sop,sizeof(struct slapi_operation_parameters));
	if(sop->target_address.dn!=NULL)
	{
		sop_new->target_address.dn= slapi_ch_strdup(sop->target_address.dn);
	}
	if(sop->target_address.uniqueid!=NULL)
	{
		sop_new->target_address.uniqueid= slapi_ch_strdup(sop->target_address.uniqueid); 
	}
  
	sop_new->csn= csn_dup(sop->csn);
	switch(sop->operation_type)
	{
	case SLAPI_OPERATION_ADD:
		sop_new->p.p_add.target_entry= slapi_entry_dup(sop->p.p_add.target_entry);
		sop_new->p.p_add.parentuniqueid = slapi_ch_strdup(sop->p.p_add.parentuniqueid);
		break;
	case SLAPI_OPERATION_MODIFY:
		sop_new->p.p_modify.modify_mods= NULL;
		if (sop->p.p_modify.modify_mods!=NULL)
		{
			sop_new->p.p_modify.modify_mods = copy_mods(sop->p.p_modify.modify_mods);
		}
		break;
	case SLAPI_OPERATION_MODRDN:
		if(sop->p.p_modrdn.modrdn_newrdn!=NULL)
		{
			sop_new->p.p_modrdn.modrdn_newrdn= slapi_ch_strdup(sop->p.p_modrdn.modrdn_newrdn);
		}
		if(sop->p.p_modrdn.modrdn_newsuperior_address.dn!=NULL)
		{
			sop_new->p.p_modrdn.modrdn_newsuperior_address.dn = 
				slapi_ch_strdup(sop->p.p_modrdn.modrdn_newsuperior_address.dn);
		}
		if(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid!=NULL)
		{
			sop_new->p.p_modrdn.modrdn_newsuperior_address.uniqueid = 
				slapi_ch_strdup(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid);
		}
		sop_new->p.p_modrdn.modrdn_mods= NULL;
		if (sop->p.p_modrdn.modrdn_mods!=NULL)
		{
			sop_new->p.p_modrdn.modrdn_mods = copy_mods(sop->p.p_modrdn.modrdn_mods);
		}
		break;
	case SLAPI_OPERATION_DELETE:
		/* Has no extra parameters. */
	case SLAPI_OPERATION_BIND:
	case SLAPI_OPERATION_COMPARE:
	case SLAPI_OPERATION_SEARCH:
	default:
		/* We are not interested in these. */
		break;
	}
	return sop_new;
}

void
operation_parameters_done (struct slapi_operation_parameters *sop)
{
	if(sop!=NULL)
	{
		slapi_ch_free((void **)&sop->target_address.dn);
		slapi_ch_free((void **)&sop->target_address.uniqueid);
		csn_free(&sop->csn);
		
		switch(sop->operation_type)
		{
		case SLAPI_OPERATION_ADD:
			slapi_entry_free(sop->p.p_add.target_entry);
			sop->p.p_add.target_entry= NULL;
			slapi_ch_free((void **)&(sop->p.p_add.parentuniqueid));
			break;
		case SLAPI_OPERATION_MODIFY:
			ldap_mods_free(sop->p.p_modify.modify_mods, 1 /* Free the Array and the Elements */);
			sop->p.p_modify.modify_mods= NULL;
			break;
		case SLAPI_OPERATION_MODRDN:
			slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newrdn));
			slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newsuperior_address.dn));
			slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid));
			ldap_mods_free(sop->p.p_modrdn.modrdn_mods, 1 /* Free the Array and the Elements */);
			sop->p.p_modrdn.modrdn_mods= NULL;
			break;
		case SLAPI_OPERATION_DELETE:
			/* Has no extra parameters. */
		case SLAPI_OPERATION_BIND:
		case SLAPI_OPERATION_COMPARE:
		case SLAPI_OPERATION_SEARCH:
		default:
			/* We are not interested in these */
			break;
		}
	}
}

void operation_parameters_free(struct slapi_operation_parameters **sop)
{
	if (sop)
	{
		operation_parameters_done (*sop);	
		slapi_ch_free ((void**)sop);
	}
}