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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// jkcoxson
use std::os::raw::{c_char, c_int, c_uint};
use crate::{
bindings as unsafe_bindings,
error::{MobileBackup2Error, MobileBackupError},
idevice::Device,
services::lockdownd::LockdowndService,
};
use plist_plus::Plist;
/// Manages backups on older devices
/// This is only for old versions of iOS, you are probably looking for MobileBackup2
pub struct MobileBackupClient<'a> {
pub(crate) pointer: unsafe_bindings::mobilebackup_client_t,
phantom: std::marker::PhantomData<&'a Device>,
}
/// Manages backups on new devices
pub struct MobileBackup2Client<'a> {
pub(crate) pointer: unsafe_bindings::mobilebackup2_client_t,
phantom: std::marker::PhantomData<&'a Device>,
}
impl MobileBackupClient<'_> {
/// Creates a new mobile backup service connection to the device
/// The use of this function is unknown
/// # Arguments
/// * `device` - The device to create the service with
/// # Returns
/// The lockdownd service
///
/// ***Verified:*** False
pub fn new(device: &Device, service: LockdowndService) -> Result<Self, MobileBackupError> {
let mut client = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup_client_new(device.pointer, service.pointer, &mut client)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(MobileBackupClient {
pointer: client,
phantom: std::marker::PhantomData,
})
}
/// Starts an afc service connection to the device
/// # Arguments
/// * `device` - The device to create the service with
/// * `service_name` - The name of the service to start
/// # Returns
/// An afc service connection
///
/// ***Verified:*** False
pub fn start_service(
device: &Device,
label: impl Into<String>,
) -> Result<Self, MobileBackupError> {
let mut client = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup_client_start_service(
device.pointer,
&mut client,
label.into().as_ptr() as *const std::os::raw::c_char,
)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(MobileBackupClient {
pointer: client,
phantom: std::marker::PhantomData,
})
}
/// Receives a plist from the service
/// Blocks until a full plist is received
/// # Arguments
/// *none*
/// # Returns
/// A plist containing the message
///
/// ***Verified:*** False
pub fn receive(&self) -> Result<Plist, MobileBackupError> {
let mut plist = unsafe { std::mem::zeroed() };
let result =
unsafe { unsafe_bindings::mobilebackup_receive(self.pointer, &mut plist) }.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(plist.into())
}
/// Sends a message to the service
/// # Arguments
/// * `plist` - The message to send as a plist
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send(&self, message: Plist) -> Result<(), MobileBackupError> {
let result =
unsafe { unsafe_bindings::mobilebackup_send(self.pointer, message.get_pointer()) }
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
/// Request a backup from the device
/// # Arguments
/// * `manifest` - The backup manifest containing the backup state and last backup time. For a first backup, pass None.
/// * `base_path` - The path to use as the backup's base path, usually '/'.
/// * `backup_verion` - The version of backup to use. The latest version is 1.6.
pub fn request_backup(
&self,
manifest: Option<Plist>,
base_path: impl Into<String>,
backup_version: impl Into<String>,
) -> Result<(), MobileBackupError> {
let ptr = if let Some(manifest) = manifest {
manifest.get_pointer()
} else {
std::ptr::null_mut()
};
let result = unsafe {
unsafe_bindings::mobilebackup_request_backup(
self.pointer,
ptr,
base_path.into().as_ptr() as *const std::os::raw::c_char,
backup_version.into().as_ptr() as *const std::os::raw::c_char,
)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
/// Sends a confirmation that the backup file was received
/// # Arguments
/// *none*
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send_backup_file_received(&self) -> Result<(), MobileBackupError> {
let result =
unsafe { unsafe_bindings::mobilebackup_send_backup_file_received(self.pointer) }.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
/// Request the device restore a backup
/// # Arguments
/// * `manifest` - The backup manifest containing the backup version
/// * `flags` - The flag to choose for restoring
/// * `backup_version` - The backup version to use. The latest known version is 1.6.
pub fn request_restore(
&self,
manifest: Plist,
flags: MobileBackupRestoreFlags,
backup_version: impl Into<String>,
) -> Result<(), MobileBackupError> {
let result = unsafe {
unsafe_bindings::mobilebackup_request_restore(
self.pointer,
manifest.get_pointer(),
flags.into(),
backup_version.into().as_ptr() as *const std::os::raw::c_char,
)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
/// Receive a confirmation that the restore file was received
/// Blocks until the full plist is received
/// # Arguments
/// *none*
/// # Returns
/// A plist with the confirmation
///
/// ***Verified:*** False
pub fn receive_restore_file_received(&self) -> Result<Plist, MobileBackupError> {
let mut plist = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup_receive_restore_file_received(self.pointer, &mut plist)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(plist.into())
}
/// Receive a confirmation that the restore file was received
/// Blocks until the full plist is received
/// # Arguments
/// *none*
/// # Returns
/// A plist with the confirmation
///
/// ***Verified:*** False
pub fn receive_restore_application_received(&self) -> Result<Plist, MobileBackupError> {
let mut plist = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup_receive_restore_application_received(
self.pointer,
&mut plist,
)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(plist.into())
}
/// Tells the device that the restore is complete.
/// The device will close the connection and reboot.
/// # Arguments
/// *none*
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send_restore_complete(&self) -> Result<(), MobileBackupError> {
let result =
unsafe { unsafe_bindings::mobilebackup_send_restore_complete(self.pointer) }.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
/// Sends an error message to the device
/// # Arguments
/// * `error` - The error message to show on the device
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send_error(&self, error: impl Into<String>) -> Result<(), MobileBackupError> {
let result = unsafe {
unsafe_bindings::mobilebackup_send_error(
self.pointer,
error.into().as_ptr() as *const std::os::raw::c_char,
)
}
.into();
if result != MobileBackupError::Success {
return Err(result);
}
Ok(())
}
}
impl MobileBackup2Client<'_> {
/// Creates a new mobile backup service connection to the device
/// The use of this function is unknown
/// # Arguments
/// * `device` - The device to create the service with
/// # Returns
/// The lockdownd service
///
/// ***Verified:*** False
pub fn new(device: &Device, service: LockdowndService) -> Result<Self, MobileBackup2Error> {
let mut client = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup2_client_new(device.pointer, service.pointer, &mut client)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(MobileBackup2Client {
pointer: client,
phantom: std::marker::PhantomData,
})
}
/// Starts an afc service connection to the device
/// # Arguments
/// * `device` - The device to create the service with
/// * `service_name` - The name of the service to start
/// # Returns
/// An afc service connection
///
/// ***Verified:*** False
pub fn start_service(
device: &Device,
label: impl Into<String>,
) -> Result<Self, MobileBackup2Error> {
let mut client = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup2_client_start_service(
device.pointer,
&mut client,
label.into().as_ptr() as *const std::os::raw::c_char,
)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(MobileBackup2Client {
pointer: client,
phantom: std::marker::PhantomData,
})
}
/// Sends a message to the service
/// # Arguments
/// * `message` - The message to send
/// * `options` - The options to send the message with
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send_message(
&self,
message: Option<String>,
options: Plist,
) -> Result<(), MobileBackup2Error> {
let ptr = if let Some(message) = message {
message.as_ptr() as *const c_char
} else {
std::ptr::null()
};
let result = unsafe {
unsafe_bindings::mobilebackup2_send_message(self.pointer, ptr, options.get_pointer())
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(())
}
/// Receives a message from the service
/// # Arguments
/// *none*
/// # Returns
/// Receives the DL* string and the message
///
/// ***Verified:*** False
pub fn receive_message(&self) -> Result<(String, Plist), MobileBackup2Error> {
let mut message = unsafe { std::mem::zeroed() };
let mut options = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::mobilebackup2_receive_message(self.pointer, &mut options, &mut message)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok((
unsafe { std::ffi::CStr::from_ptr(message) }
.to_string_lossy()
.into_owned(),
options.into(),
))
}
/// Sends raw data through the service connection
/// # Arguments
/// * `data` - A vector of bytes to send
/// # Returns
/// The bytes sent
///
/// ***Verified:*** False
pub fn send_raw(&self, data: Vec<u8>) -> Result<u32, MobileBackup2Error> {
let mut sent = 0;
let result = unsafe {
unsafe_bindings::mobilebackup2_send_raw(
self.pointer,
data.as_ptr() as *const c_char,
data.len() as u32,
&mut sent,
)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(sent)
}
/// Receives raw data from the connection
/// # Arguments
/// * `len` - How many bytes to receive
/// # Returns
/// A vector of bytes containing the received data
///
/// ***Verified:*** False
pub fn recieve_raw(&self, len: u32) -> Result<Vec<c_char>, MobileBackup2Error> {
let mut data = unsafe { std::mem::zeroed() };
let mut received = 0;
let result = unsafe {
unsafe_bindings::mobilebackup2_receive_raw(self.pointer, &mut data, len, &mut received)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(unsafe {
std::slice::from_raw_parts(&data as *const c_char, received as usize).to_vec()
})
}
/// Exchanges version with the service
/// # Arguments
/// * `versions` - The versions to exchange
/// # Returns
/// * The version of the iOS device
///
/// ***Verified:*** False
pub fn version_exchange(&self, mut versions: Vec<f64>) -> Result<f64, MobileBackup2Error> {
let mut version = 0.0;
let result = unsafe {
unsafe_bindings::mobilebackup2_version_exchange(
self.pointer,
versions.as_mut_ptr(),
versions.len() as c_char,
&mut version,
)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(version)
}
/// Sends a request to the service
/// # Arguments
/// * `request` - The type of request to send
/// * `target` - The UDID of the target device
/// * `source` - The UDID of the source device
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send_request(
&self,
request: MobileBackupRequest,
target: impl Into<String>,
source: impl Into<String>,
options: Plist,
) -> Result<(), MobileBackup2Error> {
let result = unsafe {
unsafe_bindings::mobilebackup2_send_request(
self.pointer,
request.into(),
target.into().as_ptr() as *const std::os::raw::c_char,
source.into().as_ptr() as *const std::os::raw::c_char,
options.get_pointer(),
)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(())
}
/// Sends a status response to the service
/// # Arguments
/// * `code` - The status code to send
/// * `status_string` - The string for the status
/// * `status_plist` - The plist containing status data
pub fn send_status_response(
&self,
code: c_int,
status_string: Option<String>,
status_plist: Option<Plist>,
) -> Result<(), MobileBackup2Error> {
let result = unsafe {
unsafe_bindings::mobilebackup2_send_status_response(
self.pointer,
code,
status_string
.map(|s| s.as_ptr() as *const std::os::raw::c_char)
.unwrap_or(std::ptr::null()),
status_plist
.map(|p| p.get_pointer())
.unwrap_or(std::ptr::null_mut::<std::os::raw::c_void>()), // idk
)
}
.into();
if result != MobileBackup2Error::Success {
return Err(result);
}
Ok(())
}
}
pub enum MobileBackupRequest {
Backup,
Restore,
Info,
List,
}
/// Choose what to restore
pub enum MobileBackupRestoreFlags {
/// Show a restore screen on the device
Springboard,
/// Don't overwrite any settings
Settings,
/// Don't overwrite the cameraroll
CameraRoll,
}
impl From<MobileBackupRestoreFlags> for c_uint {
fn from(flag: MobileBackupRestoreFlags) -> Self {
match flag {
MobileBackupRestoreFlags::Springboard => 1,
MobileBackupRestoreFlags::Settings => 2,
MobileBackupRestoreFlags::CameraRoll => 4,
}
}
}
impl From<MobileBackupRequest> for *const c_char {
fn from(request: MobileBackupRequest) -> Self {
match request {
MobileBackupRequest::Backup => "Backup".as_ptr() as *const c_char,
MobileBackupRequest::Restore => "Restore".as_ptr() as *const c_char,
MobileBackupRequest::Info => "Info".as_ptr() as *const c_char,
MobileBackupRequest::List => "List".as_ptr() as *const c_char,
}
}
}
impl Drop for MobileBackupClient<'_> {
fn drop(&mut self) {
unsafe {
unsafe_bindings::mobilebackup_client_free(self.pointer);
}
}
}
impl Drop for MobileBackup2Client<'_> {
fn drop(&mut self) {
unsafe {
unsafe_bindings::mobilebackup2_client_free(self.pointer);
}
}
}