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
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

//! FFI for mutable data permissions and permission sets.

use App;
use errors::AppError;
use ffi::helper::send_sync;
use ffi::mutable_data::helper;
use ffi_utils::{FfiResult, OpaqueCtx, ReprC, catch_unwind_cb};
use ffi_utils::callback::CallbackArgs;
use object_cache::{MDataPermissionSetHandle, MDataPermissionsHandle, SignKeyHandle};
use routing::{Action, PermissionSet, User};
use std::os::raw::c_void;

/// Special value that represents `User::Anyone` in permission sets.
#[no_mangle]
pub static USER_ANYONE: u64 = 0;

/// Permission actions.
#[repr(C)]
pub enum MDataAction {
    /// Permission to insert new entries.
    Insert,
    /// Permission to update existing entries.
    Update,
    /// Permission to delete existing entries.
    Delete,
    /// Permission to manage permissions.
    ManagePermissions,
}

/// State of action in the permission set
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(C)]
pub enum PermissionValue {
    /// Explicit permission is not set
    NotSet,
    /// Permission is allowed
    Allowed,
    /// Permission is denied
    Denied,
}

impl Into<Action> for MDataAction {
    fn into(self) -> Action {
        match self {
            MDataAction::Insert => Action::Insert,
            MDataAction::Update => Action::Update,
            MDataAction::Delete => Action::Delete,
            MDataAction::ManagePermissions => Action::ManagePermissions,
        }
    }
}

/// Create new permission set.
///
/// Callback parameters: user data, error code, permission set handle
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_new(
    app: *const App,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        perm_set_h: MDataPermissionSetHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, |_, context| {
            Ok(context.object_cache().insert_mdata_permission_set(
                PermissionSet::new(),
            ))
        })
    })
}

/// Allow the action in the permission set.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_allow(
    app: *const App,
    set_h: MDataPermissionSetHandle,
    action: MDataAction,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let mut set = context.object_cache().get_mdata_permission_set(set_h)?;
            *set = set.allow(action.into());
            Ok(())
        })
    })
}

/// Deny the action in the permission set.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_deny(
    app: *const App,
    set_h: MDataPermissionSetHandle,
    action: MDataAction,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let mut set = context.object_cache().get_mdata_permission_set(set_h)?;
            *set = set.deny(action.into());
            Ok(())
        })
    })
}

/// Clear the actions in the permission set.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_clear(
    app: *const App,
    set_h: MDataPermissionSetHandle,
    action: MDataAction,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let mut set = context.object_cache().get_mdata_permission_set(set_h)?;
            *set = set.clear(action.into());
            Ok(())
        })
    })
}

/// Read the permission set.
///
/// Callback parameters: user data, error code, permission value
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_is_allowed(
    app: *const App,
    set_h: MDataPermissionSetHandle,
    action: MDataAction,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        perm_value: PermissionValue),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let set = context.object_cache().get_mdata_permission_set(set_h)?;
            let perm = match action {
                MDataAction::Insert => set.is_allowed(Action::Insert),
                MDataAction::Update => set.is_allowed(Action::Update),
                MDataAction::Delete => set.is_allowed(Action::Delete),
                MDataAction::ManagePermissions => set.is_allowed(Action::ManagePermissions),
            };
            Ok(permission_set_into_permission_value(&perm))
        })
    })
}

/// Free the permission set from memory.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permission_set_free(
    app: *const App,
    set_h: MDataPermissionSetHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_permission_set(set_h)?;
            Ok(())
        })
    })
}

/// Create new permissions.
///
/// Callback parameters: user data, error code, permissions handle
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_new(
    app: *const App,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        perm_h: MDataPermissionsHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, |_, context| {
            Ok(context.object_cache().insert_mdata_permissions(
                Default::default(),
            ))
        })
    })
}

/// Get the number of entries in the permissions.
///
/// Callback parameters: user data, error code, size
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_len(
    app: *const App,
    permissions_h: MDataPermissionsHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult, size: usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let permissions = context.object_cache().get_mdata_permissions(permissions_h)?;
            Ok(permissions.len())
        })
    })
}

/// Get the permission set corresponding to the given user.
/// Use a constant `USER_ANYONE` for anyone.
///
/// Callback parameters: user data, error code, permission set handle
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_get(
    app: *const App,
    permissions_h: MDataPermissionsHandle,
    user_h: SignKeyHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        perm_set_h: MDataPermissionSetHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let permissions = context.object_cache().get_mdata_permissions(permissions_h)?;
            let handle = *permissions
                .get(&helper::get_user(context.object_cache(), user_h)?)
                .ok_or(AppError::InvalidSignKeyHandle)?;

            Ok(handle)
        })
    })
}

/// Iterate over the permissions.
/// The `o_each_cb` is called for each (user, permission set) pair in the permissions.
/// The `o_done_cb` is called after the iterations is over, or in case of error.
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_for_each(
    app: *const App,
    permissions_h: MDataPermissionsHandle,
    user_data: *mut c_void,
    o_each_cb: extern "C" fn(user_data: *mut c_void,
                             sign_key_h: SignKeyHandle,
                             perm_set_h: MDataPermissionSetHandle),
    o_done_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_done_cb, || {
        let user_data = OpaqueCtx(user_data);

        send_sync(app, user_data.0, o_done_cb, move |_, context| {
            let permissions = context.object_cache().get_mdata_permissions(permissions_h)?;
            for (user_key, permission_set_h) in &*permissions {
                let user_h = match *user_key {
                    User::Key(key) => context.object_cache().insert_sign_key(key),
                    User::Anyone => USER_ANYONE,
                };
                o_each_cb(user_data.0, user_h, *permission_set_h);
            }

            Ok(())
        })
    })
}

/// Insert permission set for the given user to the permissions.
///
/// To insert permissions for "Anyone", pass `USER_ANYONE` as the user handle.
///
/// Note: the permission sets are stored by reference, which means they must
/// remain alive (not be disposed of with `mdata_permission_set_free`) until
/// the whole permissions collection is no longer needed. The users, on the
/// other hand, are stored by value (copied).
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_insert(
    app: *const App,
    permissions_h: MDataPermissionsHandle,
    user_h: SignKeyHandle,
    permission_set_h: MDataPermissionSetHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let mut permissions = context.object_cache().get_mdata_permissions(permissions_h)?;
            let _ = permissions.insert(
                helper::get_user(context.object_cache(), user_h)?,
                permission_set_h,
            );

            Ok(())
        })
    })
}

/// Free the permissions from memory.
///
/// Note: this doesn't free the individual permission sets. Those have to be
/// disposed of manually by calling `mdata_permission_set_free`.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_permissions_free(
    app: *const App,
    permissions_h: MDataPermissionsHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_permissions(
                permissions_h,
            )?;
            Ok(())
        })
    })
}

/// Converts the permission set state into a `PermissionValue` variant
fn permission_set_into_permission_value(val: &Option<bool>) -> PermissionValue {
    match *val {
        Some(true) => PermissionValue::Allowed,
        Some(false) => PermissionValue::Denied,
        None => PermissionValue::NotSet,
    }
}

/// Returns default `PermissionValue` in an error case
impl CallbackArgs for PermissionValue {
    fn default() -> Self {
        PermissionValue::NotSet
    }
}

impl ReprC for PermissionValue {
    type C = PermissionValue;
    type Error = ();

    unsafe fn clone_from_repr_c(c_repr: PermissionValue) -> Result<PermissionValue, ()> {
        Ok(c_repr)
    }
}