nops.c 3.67 KB
Newer Older
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
/* $OpenLDAP$ */
/* nops.c - Overlay to filter idempotent operations */
/* 
 * Copyright 2008 Emmanuel Dreyfus
 * 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>.
 */
#include "portable.h"

#ifdef SLAPD_OVER_NOPS

#include <stdio.h>

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

#include "lutil.h"
#include "slap.h"
#include "config.h"

static ConfigDriver nops_cf_gen;

static int nops_cf_gen( ConfigArgs *c ) { return 0; }

static void
nops_rm_mod( Modifications **mods, Modifications *mod ) {
	Modifications *next, *m;

	next = mod->sml_next;
	if (*mods == mod) {
		*mods = next;
	} else {
		Modifications *m;

		for (m = *mods; m; m = m->sml_next) {
			if (m->sml_next == mod) {
				m->sml_next = next;
				break;
			}
		}
	}

	mod->sml_next = NULL;
	slap_mods_free(mod, 1);

	return;
}

static int
nops_modify( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
	Backend *be = op->o_bd;
	Entry *target_entry = NULL;
	Modifications *m;
	int rc;
	
	if ((m = op->orm_modlist) == NULL) {
		op->o_bd->bd_info = (BackendInfo *)(on->on_info);
		send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
				"nops() got null orm_modlist");
		return(rs->sr_err);
	}

	op->o_bd = on->on_info->oi_origdb;
	rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0,  &target_entry);
	op->o_bd = be;

	if (rc != 0 || target_entry == NULL)
		return 0;
        
	/* 
	 * For each attribute modification, check if the 
	 * modification and the old entry are the same.
	 */
	while (m) {
		int i, j;
		int found;
		Attribute *a;
		BerVarray bm;
		BerVarray bt;
		Modifications *mc;

		mc = m;
		m = m->sml_next;

		/* Check only replace sub-operations */
		if ((mc->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)
			continue;

		/* If there is no values, skip */
		if (((bm = mc->sml_values ) == NULL ) || (bm[0].bv_val == NULL))
			continue;

		/* If the attribute does not exist in old entry, skip */
		if ((a = attr_find(target_entry->e_attrs, mc->sml_desc)) == NULL)
			continue;
		if ((bt = a->a_vals) == NULL)
			continue;

		/* For each value replaced, do we find it in old entry? */
		found = 0;
		for (i = 0; bm[i].bv_val; i++) {
			for (j = 0; bt[j].bv_val; j++) {
				if (bm[i].bv_len != bt[j].bv_len)
					continue;
				if (memcmp(bm[i].bv_val, bt[j].bv_val, bt[j].bv_len) != 0)
					continue;

				found++;
				break;
			}
		}

		/* Did we find as many values as we had in old entry? */
		if (i != a->a_numvals || found != a->a_numvals)
			continue;

		/* This is a nop, remove it */
		Debug(LDAP_DEBUG_TRACE, "removing nop on %s%s%s",
			a->a_desc->ad_cname.bv_val, "", "");

		nops_rm_mod(&op->orm_modlist, mc);
	}
	if (target_entry) {
		op->o_bd = on->on_info->oi_origdb;
		be_entry_release_r(op, target_entry);
		op->o_bd = be;
	}

	if ((m = op->orm_modlist) == NULL) {
139
140
		slap_callback *cb = op->o_callback;

141
		op->o_bd->bd_info = (BackendInfo *)(on->on_info);
142
143
144
145
		op->o_callback = NULL;
                send_ldap_error(op, rs, LDAP_SUCCESS, "");
		op->o_callback = cb;

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
		return (rs->sr_err);
	}

	return SLAP_CB_CONTINUE;
}

static slap_overinst nops_ovl;

#if SLAPD_OVER_NOPS == SLAPD_MOD_DYNAMIC
static
#endif
int
nops_initialize( void ) {
	nops_ovl.on_bi.bi_type = "nops";
	nops_ovl.on_bi.bi_op_modify = nops_modify;
	return overlay_register( &nops_ovl );
}

#if SLAPD_OVER_NOPS == SLAPD_MOD_DYNAMIC
int init_module(int argc, char *argv[]) {
	return nops_initialize();
}
#endif

#endif /* defined(SLAPD_OVER_NOPS) */