summaryrefslogtreecommitdiff
path: root/src/cmd/init.rs
blob: 167d028e6ea37ac7190e1f819bc3c14840eac93a (plain)
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
use std::ffi::CString;

use crate::cmd::MailStorage;
use crate::de_jmhoffmann_jwebmail_mailstorage::Call_Init;

pub fn init(
    ms: &MailStorage,
    call: &mut dyn Call_Init,
    unix_user: String,
    mailbox_path: String,
) -> varlink::Result<()> {
    unsafe {
        *libc::__errno_location() = 0;
    }
    if let Ok(c_sys_user) = CString::new(unix_user.clone()) {
        let user_info: *const libc::passwd = unsafe { libc::getpwnam(c_sys_user.as_ptr()) };
        let err = unsafe { *libc::__errno_location() };
        if err != 0 {
            return call.reply_invalid_user(unix_user);
        }
        if user_info.is_null() {
            return call.reply_invalid_user(unix_user);
        }
        let rc = unsafe { libc::setuid((*user_info).pw_uid) };
        if rc != 0 {
            return call.reply_invalid_user(unix_user);
        }

        *ms.maildir_path.write().unwrap() = Some(mailbox_path.into());

        call.reply()
    } else {
        call.reply_invalid_user(unix_user)
    }
}