summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/replication/repl_ops.c
blob: aaffe507bf1acad9e7543abdc4268848f69b37ff (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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include "slapi-plugin.h"
#include "repl.h"
#include "repl5.h"

int
legacy_postop( Slapi_PBlock *pb, const char *caller, int operation_type)
{
	int rc = 0;
    Object *r_obj;
    Replica *r;
	
    r_obj = replica_get_replica_for_op (pb);
    if (r_obj == NULL)  /* there is no replica configured for this operations */
        return 0;
    else
    {
        /* check if this replica is 4.0 consumer */
        r = (Replica*)object_get_data (r_obj);
        PR_ASSERT (r);

        /* this replica is not a 4.0 consumer - so we don't need to do any processing */
        if (!replica_is_legacy_consumer (r))
        {
            object_release (r_obj);
            return 0;
        }
    
        object_release (r_obj);
    }

	slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &rc);
	if (0 == rc)
	{
		if (OP_ADD == operation_type || OP_MODIFY == operation_type)
		{
			void *op;
			consumer_operation_extension *opext = NULL;

			/* Optimise out traversal of mods/entry if no cop{ied|ying}From present */
			slapi_pblock_get(pb, SLAPI_OPERATION, &op);
			opext = (consumer_operation_extension*) repl_con_get_ext (REPL_CON_EXT_OP, op);
			if (NULL != opext && opext->has_cf)
			{
                process_legacy_cf( pb );
			}
		}
	}

	return 0;
}



static char *not_replicationdn_errmsg =
	"An operation was submitted that contained copiedFrom or "
	"copyingFrom attributes, but the connection was not bound "
	"as the replicationdn.";

int
legacy_preop(Slapi_PBlock *pb, const char *caller, int operation_type)
{
	int rc = 0;
	Slapi_Operation *operation = NULL;
	consumer_operation_extension *opext = NULL;
	int has_cf = 0;
    Object *r_obj;
    Replica *r;
	int is_legacy_op = 0;

	slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
	is_legacy_op = operation_is_flag_set(operation,OP_FLAG_LEGACY_REPLICATION_DN);
    r_obj = replica_get_replica_for_op (pb);

    if (r_obj == NULL) {  /* there is no replica configured for this operations */
		if (is_legacy_op){
			/* This is a legacy replication operation but there are NO replica defined
			   Just refuse it */
			slapi_send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, 
				"Replication operation refused because the consumer is not defined as a replica", 0, NULL);
			slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
				"Incoming replication operation was refused because "
				"there's no replica defined for this operation\n");
			return -1;
		}
		else {
			return 0;
		}
	}
    else
    {
        /* check if this replica is 4.0 consumer */
        r = (Replica*)object_get_data (r_obj);
        PR_ASSERT (r);

        if (!replica_is_legacy_consumer (r))
        {
            object_release (r_obj);
			if (is_legacy_op) {
				/* This is a legacy replication operation 
				   but the replica is doesn't accept from legacy
				   Just refuse it */
				slapi_send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, 
					"Replication operation refused because "
					"the consumer is not defined as a legacy replica", 0, NULL);
				slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
					"Incoming replication operation was refused because "
					"there's no legacy replica defined for this operation\n");
				return -1;
			} else {
				return 0;
			}
        }

        object_release (r_obj);
    }

	opext = (consumer_operation_extension*) repl_con_get_ext (REPL_CON_EXT_OP, operation);
	
	switch (operation_type) {
	case OP_ADD:
		{
			Slapi_Entry *e = NULL;
			Slapi_Attr *attr;
			/*
			 * Check if the entry being added has copiedFrom/copyingFrom
			 * attributes. 
			 */
			slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
			if (NULL != e)
			{
				if (slapi_entry_attr_find(e, type_copiedFrom, &attr) == 0)
				{
					has_cf = 1;
				}
				else
				if (slapi_entry_attr_find(e, type_copyingFrom, &attr) == 0)
				{
					has_cf = 1;
				}
			}
			/* JCMREPL - If this is a replicated operation then the baggage control also contains the Unique Identifier of the superior entry. */
		}
		break;
	case OP_MODIFY:
		{
			LDAPMod **mods = NULL;
			int i;
	
			/*
			 * Check if the modification contains copiedFrom/copyingFrom
			 * attributes. 
			 */
			slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
			for (i = 0; NULL != mods && NULL != mods[i]; i++)
			{
				if ((strcasecmp(mods[i]->mod_type, type_copiedFrom) == 0) ||
						(strcasecmp(mods[i]->mod_type, type_copyingFrom) == 0))
				{
					has_cf = 1;
				}
			}
		}
		break;
	case OP_DELETE:
		break;
	case OP_MODDN:
		break;
	}

	/* Squirrel away an optimization hint for the postop plugin */
	opext->has_cf = has_cf;

	return rc;
}