mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
Make dc_create_setup_code() safe (#239)
Make dc_create_setup_code() safe
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use std::ffi::CString;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use deltachat::chatlist::*;
|
use deltachat::chatlist::*;
|
||||||
@@ -568,13 +569,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
|||||||
dc_imex(context, 2, context.get_blobdir(), 0 as *const libc::c_char);
|
dc_imex(context, 2, context.get_blobdir(), 0 as *const libc::c_char);
|
||||||
}
|
}
|
||||||
"export-setup" => {
|
"export-setup" => {
|
||||||
let setup_code: *mut libc::c_char = dc_create_setup_code(context);
|
let setup_code = dc_create_setup_code(context);
|
||||||
|
let setup_code_c = CString::new(setup_code.clone()).unwrap();
|
||||||
let file_name: *mut libc::c_char = dc_mprintf(
|
let file_name: *mut libc::c_char = dc_mprintf(
|
||||||
b"%s/autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char,
|
b"%s/autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char,
|
||||||
context.get_blobdir(),
|
context.get_blobdir(),
|
||||||
);
|
);
|
||||||
let file_content: *mut libc::c_char;
|
let file_content: *mut libc::c_char;
|
||||||
file_content = dc_render_setup_file(context, setup_code);
|
file_content = dc_render_setup_file(context, setup_code_c.as_ptr());
|
||||||
if !file_content.is_null()
|
if !file_content.is_null()
|
||||||
&& 0 != dc_write_file(
|
&& 0 != dc_write_file(
|
||||||
context,
|
context,
|
||||||
@@ -586,14 +588,13 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
|||||||
println!(
|
println!(
|
||||||
"Setup message written to: {}\nSetup code: {}",
|
"Setup message written to: {}\nSetup code: {}",
|
||||||
as_str(file_name),
|
as_str(file_name),
|
||||||
as_str(setup_code),
|
&setup_code,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
bail!("");
|
bail!("");
|
||||||
}
|
}
|
||||||
free(file_content as *mut libc::c_void);
|
free(file_content as *mut libc::c_void);
|
||||||
free(file_name as *mut libc::c_void);
|
free(file_name as *mut libc::c_void);
|
||||||
free(setup_code as *mut libc::c_void);
|
|
||||||
}
|
}
|
||||||
"poke" => {
|
"poke" => {
|
||||||
ensure!(0 != poke_spec(context, arg1_c), "Poke failed");
|
ensure!(0 != poke_spec(context, arg1_c), "Poke failed");
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
|
|||||||
if 0 == dc_alloc_ongoing(context) {
|
if 0 == dc_alloc_ongoing(context) {
|
||||||
return 0 as *mut libc::c_char;
|
return 0 as *mut libc::c_char;
|
||||||
}
|
}
|
||||||
setup_code = dc_create_setup_code(context);
|
setup_code = to_cstring(dc_create_setup_code(context));
|
||||||
if !setup_code.is_null() {
|
if !setup_code.is_null() {
|
||||||
/* this may require a keypair to be created. this may take a second ... */
|
/* this may require a keypair to be created. this may take a second ... */
|
||||||
if !context
|
if !context
|
||||||
@@ -282,7 +282,7 @@ pub unsafe extern "C" fn dc_render_setup_file(
|
|||||||
ret_setupfilecontent
|
ret_setupfilecontent
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
|
pub fn dc_create_setup_code(_context: &Context) -> String {
|
||||||
let mut random_val: uint16_t;
|
let mut random_val: uint16_t;
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let mut ret = String::new();
|
let mut ret = String::new();
|
||||||
@@ -302,7 +302,7 @@ pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
to_cstring(ret)
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO should return bool /rtn
|
// TODO should return bool /rtn
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! Stress some functions for testing; if used as a lib, this file is obsolete.
|
//! Stress some functions for testing; if used as a lib, this file is obsolete.
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
use mmime::mailimf_types::*;
|
use mmime::mailimf_types::*;
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
@@ -527,11 +528,11 @@ unsafe fn stress_functions(context: &Context) {
|
|||||||
);
|
);
|
||||||
free(buf_1 as *mut libc::c_void);
|
free(buf_1 as *mut libc::c_void);
|
||||||
if 0 != dc_is_configured(context) {
|
if 0 != dc_is_configured(context) {
|
||||||
let setupcode: *mut libc::c_char;
|
|
||||||
let setupfile: *mut libc::c_char;
|
let setupfile: *mut libc::c_char;
|
||||||
setupcode = dc_create_setup_code(context);
|
let setupcode_c =
|
||||||
assert!(!setupcode.is_null());
|
CString::new(dc_create_setup_code(context)).expect("invalid string converted");
|
||||||
assert_eq!(strlen(setupcode), 44);
|
assert_eq!(setupcode_c.to_bytes().len(), 44);
|
||||||
|
let setupcode = setupcode_c.as_ptr();
|
||||||
assert!(
|
assert!(
|
||||||
0 != !(*setupcode.offset(4isize) as libc::c_int == '-' as i32
|
0 != !(*setupcode.offset(4isize) as libc::c_int == '-' as i32
|
||||||
&& *setupcode.offset(9isize) as libc::c_int == '-' as i32
|
&& *setupcode.offset(9isize) as libc::c_int == '-' as i32
|
||||||
@@ -596,7 +597,6 @@ unsafe fn stress_functions(context: &Context) {
|
|||||||
);
|
);
|
||||||
free(payload as *mut libc::c_void);
|
free(payload as *mut libc::c_void);
|
||||||
free(setupfile as *mut libc::c_void);
|
free(setupfile as *mut libc::c_void);
|
||||||
free(setupcode as *mut libc::c_void);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if 0 != dc_is_configured(context) {
|
if 0 != dc_is_configured(context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user