-
Notifications
You must be signed in to change notification settings - Fork 80
/
pblock.rs
323 lines (277 loc) · 9.92 KB
/
pblock.rs
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
use libc;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_char;
use std::ptr;
use crate::backend::BackendRef;
use crate::constants::{PblockType, PluginFnType, PluginVersion};
use crate::entry::EntryRef;
pub use crate::log::{log_error, ErrorLevel};
extern "C" {
fn slapi_pblock_set(pb: *const libc::c_void, arg: i32, value: *const libc::c_void) -> i32;
fn slapi_pblock_get(pb: *const libc::c_void, arg: i32, value: *const libc::c_void) -> i32;
fn slapi_pblock_destroy(pb: *const libc::c_void);
fn slapi_pblock_new() -> *const libc::c_void;
}
pub struct Pblock {
value: PblockRef,
}
impl Pblock {
pub fn new() -> Pblock {
let raw_pb = unsafe { slapi_pblock_new() };
Pblock {
value: PblockRef { raw_pb },
}
}
}
impl Deref for Pblock {
type Target = PblockRef;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl DerefMut for Pblock {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
impl Drop for Pblock {
fn drop(&mut self) {
unsafe { slapi_pblock_destroy(self.value.raw_pb) }
}
}
pub struct PblockRef {
raw_pb: *const libc::c_void,
}
impl PblockRef {
pub fn new(raw_pb: *const libc::c_void) -> Self {
PblockRef { raw_pb }
}
pub unsafe fn as_ptr(&self) -> *const libc::c_void {
self.raw_pb
}
fn set_pb_char_arr_ptr(&mut self, req_type: PblockType, ptr: *const *const c_char) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, req_type as i32, value_ptr) }
}
fn set_pb_char_ptr(&mut self, req_type: PblockType, ptr: *const c_char) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, req_type as i32, value_ptr) }
}
fn set_pb_fn_ptr(
&mut self,
fn_type: PluginFnType,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, fn_type as i32, value_ptr) }
}
fn get_value_ptr(&mut self, req_type: PblockType) -> Result<*const libc::c_void, ()> {
let mut value: *mut libc::c_void = ptr::null::<libc::c_void>() as *mut libc::c_void;
let value_ptr: *const libc::c_void = &mut value as *const _ as *const libc::c_void;
match unsafe { slapi_pblock_get(self.raw_pb, req_type as i32, value_ptr) } {
0 => Ok(value),
e => {
log_error!(ErrorLevel::Error, "enable to get from pblock -> {:?}", e);
Err(())
}
}
}
fn get_value_i32(&mut self, req_type: PblockType) -> Result<i32, ()> {
let mut value: i32 = 0;
let value_ptr: *const libc::c_void = &mut value as *const _ as *const libc::c_void;
match unsafe { slapi_pblock_get(self.raw_pb, req_type as i32, value_ptr) } {
0 => Ok(value),
e => {
log_error!(ErrorLevel::Error, "enable to get from pblock -> {:?}", e);
Err(())
}
}
}
pub fn register_start_fn(&mut self, ptr: extern "C" fn(*const libc::c_void) -> i32) -> i32 {
self.set_pb_fn_ptr(PluginFnType::Start, ptr)
}
pub fn register_close_fn(&mut self, ptr: extern "C" fn(*const libc::c_void) -> i32) -> i32 {
self.set_pb_fn_ptr(PluginFnType::Close, ptr)
}
pub fn register_betxn_pre_add_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::BeTxnPreAdd, ptr)
}
pub fn register_betxn_pre_modify_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::BeTxnPreModify, ptr)
}
pub fn register_syntax_filter_ava_fn(
&mut self,
ptr: extern "C" fn(
*const core::ffi::c_void,
*const core::ffi::c_void,
*const core::ffi::c_void,
i32,
*mut core::ffi::c_void,
) -> i32,
) -> i32 {
// We can't use self.set_pb_fn_ptr here as the fn type sig is different.
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, PluginFnType::SyntaxFilterAva as i32, value_ptr) }
}
pub fn register_syntax_values2keys_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::SyntaxValuesToKeys, ptr)
}
pub fn register_syntax_assertion2keys_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::SyntaxAssertion2KeysAva, ptr)
}
pub fn register_syntax_flags_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::SyntaxFlags, ptr)
}
pub fn register_syntax_oid(&mut self, ptr: *const c_char) -> i32 {
self.set_pb_char_ptr(PblockType::SyntaxOid, ptr)
}
pub fn register_syntax_compare_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::SyntaxCompare, ptr)
}
pub fn register_syntax_validate_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::SyntaxValidate, ptr)
}
pub fn register_syntax_names(&mut self, arr_ptr: *const *const c_char) -> i32 {
self.set_pb_char_arr_ptr(PblockType::SyntaxNames, arr_ptr)
}
pub fn register_mr_filter_create_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::MRFilterCreate, ptr)
}
pub fn register_mr_indexer_create_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::MRIndexerCreate, ptr)
}
pub fn register_mr_filter_ava_fn(
&mut self,
ptr: extern "C" fn(
*const core::ffi::c_void,
*const core::ffi::c_void,
*const core::ffi::c_void,
i32,
*mut core::ffi::c_void,
) -> i32,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, PluginFnType::MRFilterAva as i32, value_ptr) }
}
pub fn register_mr_filter_sub_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::MRFilterSub, ptr)
}
pub fn register_mr_values2keys_fn(
&mut self,
ptr: extern "C" fn(
*const core::ffi::c_void,
*const core::ffi::c_void,
*mut core::ffi::c_void,
i32,
) -> i32,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, PluginFnType::MRValuesToKeys as i32, value_ptr) }
}
pub fn register_mr_assertion2keys_ava_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::MRAssertionToKeysAva, ptr)
}
pub fn register_mr_assertion2keys_sub_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void) -> i32,
) -> i32 {
self.set_pb_fn_ptr(PluginFnType::MRAssertionToKeysSub, ptr)
}
pub fn register_mr_compare_fn(
&mut self,
ptr: extern "C" fn(*const libc::c_void, *const libc::c_void) -> i32,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe { slapi_pblock_set(self.raw_pb, PluginFnType::MRCompare as i32, value_ptr) }
}
pub fn register_mr_names(&mut self, arr_ptr: *const *const c_char) -> i32 {
self.set_pb_char_arr_ptr(PblockType::MRNames, arr_ptr)
}
pub fn register_pwd_storage_encrypt_fn(
&mut self,
ptr: extern "C" fn(*const c_char) -> *const c_char,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe {
slapi_pblock_set(
self.raw_pb,
PluginFnType::PwdStorageEncrypt as i32,
value_ptr,
)
}
}
pub fn register_pwd_storage_compare_fn(
&mut self,
ptr: extern "C" fn(*const c_char, *const c_char) -> i32,
) -> i32 {
let value_ptr: *const libc::c_void = ptr as *const libc::c_void;
unsafe {
slapi_pblock_set(
self.raw_pb,
PluginFnType::PwdStorageCompare as i32,
value_ptr,
)
}
}
pub fn register_pwd_storage_scheme_name(&mut self, ptr: *const c_char) -> i32 {
self.set_pb_char_ptr(PblockType::PwdStorageSchemeName, ptr)
}
pub fn get_op_add_entryref(&mut self) -> Result<EntryRef, ()> {
self.get_value_ptr(PblockType::AddEntry)
.map(|ptr| EntryRef::new(ptr))
}
pub fn set_plugin_version(&mut self, vers: PluginVersion) -> i32 {
self.set_pb_char_ptr(PblockType::Version, vers.to_char_ptr())
}
pub fn set_op_backend(&mut self, be: &BackendRef) -> i32 {
unsafe { slapi_pblock_set(self.raw_pb, PblockType::Backend as i32, be.as_ptr()) }
}
pub fn get_plugin_identity(&mut self) -> *const libc::c_void {
self.get_value_ptr(PblockType::Identity)
.unwrap_or(std::ptr::null())
}
pub fn get_op_result(&mut self) -> i32 {
self.get_value_i32(PblockType::OpResult).unwrap_or(-1)
}
pub fn get_is_replicated_operation(&mut self) -> bool {
let i = self
.get_value_i32(PblockType::IsReplicationOperation)
.unwrap_or(0);
// Because rust returns the result of the last evaluation, we can
// just return if not equal 0.
i != 0
}
}