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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// 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.

use crate::ffi::errors::FfiError;
use ffi_utils::{call_result_cb, ffi_error};
use ffi_utils::{
    catch_unwind_cb, FfiResult, NativeResult, OpaqueCtx, ReprC, SafePtr, FFI_RESULT_OK,
};

use log::debug;
use safe_authenticator::access_container;
use safe_authenticator::app_auth;
use safe_authenticator::config;
use safe_authenticator::ipc::{decode_ipc_msg, decode_share_mdata_req, encode_response};
use safe_authenticator::revocation::{flush_app_revocation_queue, revoke_app};
use safe_authenticator::{AuthError, Authenticator};
use safe_core::client::Client;
use safe_core::ffi::ipc::req::{AuthReq, ContainersReq, ShareMDataRequest};
use safe_core::ffi::ipc::resp::MetadataResponse;
use safe_core::ipc::req::{
    AuthReq as NativeAuthReq, ContainersReq as NativeContainersReq, IpcReq,
    ShareMDataReq as NativeShareMDataReq,
};
use safe_core::ipc::resp::IpcResp;
use safe_core::ipc::{decode_msg, IpcError, IpcMsg};
use safe_core::{client, CoreError};
use safe_nd::MDataAddress;
use std::ffi::CStr;

use std::os::raw::{c_char, c_void};

/// Decodes a given encoded IPC message without requiring an authorised account.
#[no_mangle]
pub unsafe extern "C" fn auth_unregistered_decode_ipc_msg(
    msg: *const c_char,
    user_data: *mut c_void,
    o_unregistered: extern "C" fn(
        user_data: *mut c_void,
        req_id: u32,
        extra_data: *const u8,
        extra_data_len: usize,
    ),
    o_err: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_err, || -> Result<_, FfiError> {
        let msg_raw = CStr::from_ptr(msg).to_str()?;
        let msg = decode_msg(msg_raw)?;

        match msg {
            IpcMsg::Req {
                request: IpcReq::Unregistered(extra_data),
                req_id,
            } => {
                o_unregistered(
                    user_data.0,
                    req_id,
                    extra_data.as_safe_ptr(),
                    extra_data.len(),
                );
            }
            _ => {
                call_result_cb!(
                    Err::<(), _>(FfiError::from(CoreError::OperationForbidden)),
                    user_data,
                    o_err
                );
            }
        }

        Ok(())
    })
}

/// Decodes a given encoded IPC message and calls a corresponding callback.
#[no_mangle]
pub unsafe extern "C" fn auth_decode_ipc_msg(
    auth: *const Authenticator,
    msg: *const c_char,
    user_data: *mut c_void,
    o_auth: extern "C" fn(user_data: *mut c_void, req_id: u32, req: *const AuthReq),
    o_containers: extern "C" fn(user_data: *mut c_void, req_id: u32, req: *const ContainersReq),
    o_unregistered: extern "C" fn(
        user_data: *mut c_void,
        req_id: u32,
        extra_data: *const u8,
        extra_data_len: usize,
    ),
    o_share_mdata: extern "C" fn(
        user_data: *mut c_void,
        req_id: u32,
        req: *const ShareMDataRequest,
        metadata: *const MetadataResponse,
        metadata_len: usize,
    ),
    o_err: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_err, || -> Result<_, FfiError> {
        let msg_raw = CStr::from_ptr(msg).to_str()?;
        let msg = decode_msg(msg_raw)?;

        let client = &(*auth).client;
        let c1 = client.clone();
        let msg = futures::executor::block_on(decode_ipc_msg(client, msg))?;
        let _res = match msg {
            Ok(IpcMsg::Req {
                request: IpcReq::Auth(auth_req),
                req_id,
            }) => {
                let repr_c = auth_req.into_repr_c().map_err(AuthError::IpcError)?;
                o_auth(user_data.0, req_id, &repr_c);
                Ok(())
            }
            Ok(IpcMsg::Req {
                request: IpcReq::Containers(cont_req),
                req_id,
            }) => {
                let repr_c = cont_req.into_repr_c().map_err(AuthError::IpcError)?;
                o_containers(user_data.0, req_id, &repr_c);
                Ok(())
            }
            Ok(IpcMsg::Req {
                request: IpcReq::Unregistered(extra_data),
                req_id,
            }) => {
                o_unregistered(
                    user_data.0,
                    req_id,
                    extra_data.as_safe_ptr(),
                    extra_data.len(),
                );
                Ok(())
            }
            Ok(IpcMsg::Req {
                request: IpcReq::ShareMData(share_mdata_req),
                req_id,
            }) => {
                let metadata_cont =
                    futures::executor::block_on(decode_share_mdata_req(&c1, &share_mdata_req))?;
                let share_mdata_req_repr_c = share_mdata_req.into_repr_c()?;

                let mut ffi_metadata_cont = Vec::with_capacity(metadata_cont.len());
                for metadata in metadata_cont {
                    if let Some(metadata) = metadata {
                        ffi_metadata_cont.push(metadata);
                    } else {
                        ffi_metadata_cont.push(MetadataResponse::invalid());
                    }
                }

                o_share_mdata(
                    user_data.0,
                    req_id,
                    &share_mdata_req_repr_c,
                    ffi_metadata_cont.as_ptr(),
                    ffi_metadata_cont.len(),
                );

                Ok(())
            }
            Err((error_code, description, err)) => {
                let res = NativeResult {
                    error_code,
                    description: Some(description),
                }
                .into_repr_c()?;
                o_err(user_data.0, &res, err.as_ptr());
                Ok(())
            }
            Ok(IpcMsg::Resp { .. }) | Ok(IpcMsg::Revoked { .. }) | Ok(IpcMsg::Err(..)) => {
                let err = FfiError::from(AuthError::Unexpected("Unexpected msg type".to_owned()));
                call_result_cb!(Err::<(), _>(err), user_data, o_err);
                Ok(())
            }
        }
        .map_err(move |err: AuthError| {
            call_result_cb!(Err::<(), _>(FfiError::from(err)), user_data, o_err);
        });
        Ok(())
    })
}

/// Revoke app access.
#[no_mangle]
pub unsafe extern "C" fn auth_revoke_app(
    auth: *const Authenticator,
    app_id: *const c_char,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<_, FfiError> {
        let app_id = String::clone_from_repr_c(app_id)?;

        let client = &(*auth).client;
        let _ = futures::executor::block_on(revoke_app(client, &app_id)).map_err(move |e| {
            call_result_cb!(Err::<(), _>(FfiError::from(e)), user_data, o_cb);
        });

        let resp = encode_response(&IpcMsg::Revoked { app_id })?;
        o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());

        Ok(())
    });
}

/// Flush the revocation queue.
#[no_mangle]
pub unsafe extern "C" fn auth_flush_app_revocation_queue(
    auth: *const Authenticator,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<_, AuthError> {
        let client = &(*auth).client;
        let res = futures::executor::block_on(flush_app_revocation_queue(client));
        call_result_cb!(res.map_err(FfiError::from), user_data, o_cb);
        Ok(())
    })
}

/// Encodes a response to unregistered client authentication request.
#[no_mangle]
pub unsafe extern "C" fn encode_unregistered_resp(
    req_id: u32,
    is_granted: bool,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<(), FfiError> {
        if is_granted {
            let bootstrap_cfg = client::bootstrap_config()?;

            let resp = encode_response(&IpcMsg::Resp {
                req_id,
                response: IpcResp::Unregistered(Ok(bootstrap_cfg)),
            })?;

            o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());
        } else {
            let resp = encode_response(&IpcMsg::Resp {
                req_id,
                response: IpcResp::Unregistered(Err(IpcError::AuthDenied)),
            })?;

            o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());
        }
        Ok(())
    })
}

/// Provides and encodes an Authenticator response.
#[no_mangle]
pub unsafe extern "C" fn encode_auth_resp(
    auth: *const Authenticator,
    req: *const AuthReq,
    req_id: u32,
    is_granted: bool,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<(), FfiError> {
        let auth_req = NativeAuthReq::clone_from_repr_c(req)?;

        if is_granted {
            let client = &(*auth).client;
            let auth_granted =
                futures::executor::block_on(app_auth::authenticate(client, auth_req))
                    .map_err(FfiError::from)?;

            let res = {
                let resp = encode_response(&IpcMsg::Resp {
                    req_id,
                    response: IpcResp::Auth(Ok(auth_granted)),
                })?;

                o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());
                Ok(())
            };
            let _ = res
                .or_else(move |e: FfiError| -> Result<(), FfiError> {
                    let (error_code, description) = ffi_error!(e);
                    let resp = encode_response(&IpcMsg::Resp {
                        req_id,
                        response: IpcResp::Auth(Err(e.into())),
                    })?;
                    let result = NativeResult {
                        error_code,
                        description: Some(description),
                    }
                    .into_repr_c()?;
                    o_cb(user_data.0, &result, resp.as_ptr());
                    Ok(())
                })
                .map_err(move |e| {
                    call_result_cb!(Err::<(), _>(e), user_data, o_cb);
                });
        } else {
            let response = encode_response(&IpcMsg::Resp {
                req_id,
                response: IpcResp::Auth(Err(IpcError::AuthDenied)),
            })?;

            o_cb(user_data.0, FFI_RESULT_OK, response.as_ptr());
        }

        Ok(())
    })
}

/// Update containers permissions for an App.
#[no_mangle]
pub unsafe extern "C" fn encode_containers_resp(
    auth: *const Authenticator,
    req: *const ContainersReq,
    req_id: u32,
    is_granted: bool,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<(), FfiError> {
        let cont_req = NativeContainersReq::clone_from_repr_c(req)?;

        if is_granted {
            let permissions = cont_req.containers.clone();
            let app_id = cont_req.app.id;

            let client = &(*auth).client;
            let c2 = client.clone();
            let c3 = client.clone();
            let c4 = client.clone();

            let app = futures::executor::block_on(config::get_app(client, &app_id))?;
            let app_pk = app.keys.public_key();
            let mut perms = futures::executor::block_on(access_container::update_container_perms(
                &c2,
                permissions,
                app_pk,
            ))?;
            let app_keys = app.keys;

            let res = futures::executor::block_on(access_container::fetch_entry(
                c3,
                app_id.clone(),
                app_keys.clone(),
            ));

            let version = match res {
                // Updating an existing entry
                Ok((version, Some(mut existing_perms))) => {
                    for (key, val) in perms {
                        let _ = existing_perms.insert(key, val);
                    }
                    perms = existing_perms;

                    version + 1
                }

                // Adding a new access container entry
                Ok((_, None)) => 0,

                // Error has occurred while trying to get an
                // existing entry
                Err(e) => return Err(FfiError::from(e)),
            };

            futures::executor::block_on(access_container::put_entry(
                &c4, &app_id, &app_keys, &perms, version,
            ))?;

            let res: Result<(), AuthError> = {
                let resp = encode_response(&IpcMsg::Resp {
                    req_id,
                    response: IpcResp::Containers(Ok(())),
                })
                .map_err(FfiError::from)?;
                o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());
                Ok(())
            }
            .or_else(move |e: AuthError| -> Result<(), AuthError> {
                let (error_code, description) = ffi_error!(e);
                let resp = encode_response(&IpcMsg::Resp {
                    req_id,
                    response: IpcResp::Containers(Err(e.into())),
                })?;
                let result = NativeResult {
                    error_code,
                    description: Some(description),
                }
                .into_repr_c()?;
                o_cb(user_data.0, &result, resp.as_ptr());
                Ok(())
            });

            match res {
                Ok(()) => {
                    // do nothing
                }
                Err(e) => {
                    debug!("Unexpected error: {:?}", e);
                    return Err(FfiError::from(e));
                }
            }
        } else {
            let response = encode_response(&IpcMsg::Resp {
                req_id,
                response: IpcResp::Containers(Err(IpcError::AuthDenied)),
            })?;

            o_cb(user_data.0, FFI_RESULT_OK, response.as_ptr());
        }

        Ok(())
    });
}

/// Encode share mutable data response.
#[no_mangle]
pub unsafe extern "C" fn encode_share_mdata_resp(
    auth: *const Authenticator,
    req: *const ShareMDataRequest,
    req_id: u32,
    is_granted: bool,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, response: *const c_char),
) {
    let user_data = OpaqueCtx(user_data);

    catch_unwind_cb(user_data.0, o_cb, || -> Result<(), FfiError> {
        let share_mdata_req = NativeShareMDataReq::clone_from_repr_c(req)?;

        if is_granted {
            let client = &(*auth).client;

            let client_cloned0 = client.clone();
            let client_cloned1 = client.clone();
            let user_data = user_data.0;

            let app_info =
                futures::executor::block_on(config::get_app(client, &share_mdata_req.app.id))?;
            let user = app_info.keys.public_key();
            let _num_mdata = share_mdata_req.mdata.len();

            let encode_result: Result<(), AuthError> = {
                for mdata in share_mdata_req.mdata.iter() {
                    let md = futures::executor::block_on(
                        client_cloned0.get_seq_mdata_shell(mdata.name, mdata.type_tag),
                    )?;
                    let version = md.version();
                    futures::executor::block_on(client_cloned1.set_mdata_user_permissions(
                        MDataAddress::Seq {
                            name: mdata.name,
                            tag: mdata.type_tag,
                        },
                        user,
                        mdata.perms.clone(),
                        version + 1,
                    ))?;
                    let resp = encode_response(&IpcMsg::Resp {
                        req_id,
                        response: IpcResp::ShareMData(Ok(())),
                    })
                    .map_err(AuthError::IpcError)?;
                    o_cb(user_data, FFI_RESULT_OK, resp.as_ptr());
                }

                Ok(())
            };

            match encode_result {
                Ok(()) => {
                    // do nothing
                }
                Err(e) => {
                    call_result_cb!(Err::<(), _>(FfiError::from(e)), user_data, o_cb);
                }
            }
        } else {
            let resp = encode_response(&IpcMsg::Resp {
                req_id,
                response: IpcResp::ShareMData(Err(IpcError::ShareMDataDenied)),
            })?;

            o_cb(user_data.0, FFI_RESULT_OK, resp.as_ptr());
        }

        Ok(())
    })
}