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
#![deny(warnings, clippy::all)]
#![warn(missing_docs)]
//! A simple library to detect whether you are root/admin or not
//! ## Usage
//! ```no_run
//! use is_root::is_root;
//!
//! if is_root() {
//!     println!("Doing something dangerous")
//! } else {
//!     eprintln!("Run me as root")
//! }
//! ```

/// Returns `true` if user is root; `false` otherwise
/// ```no_run
/// use is_root::is_root;
///
/// if is_root() {
///     println!("Doing something dangerous")
/// } else {
///     eprintln!("Run me as root")
/// }
/// ```
#[must_use]
pub fn is_root() -> bool {
    is_root_internal()
}

#[cfg(windows)]
#[must_use]
fn is_root_internal() -> bool {
    use std::mem;
    use winapi::{
        ctypes::c_void,
        shared::minwindef::{DWORD, TRUE},
        um::{
            handleapi::{CloseHandle, INVALID_HANDLE_VALUE},
            processthreadsapi::{GetCurrentProcess, OpenProcessToken},
            securitybaseapi::GetTokenInformation,
            winnt::{TokenElevation, TOKEN_ELEVATION, TOKEN_QUERY},
        },
    };
    let mut token = INVALID_HANDLE_VALUE;
    let mut elevated = false;
    unsafe {
        if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == TRUE {
            let mut elevation: TOKEN_ELEVATION = mem::zeroed();
            let mut size = mem::size_of::<TOKEN_ELEVATION>() as DWORD;
            if GetTokenInformation(
                token,
                TokenElevation,
                &mut elevation as *mut TOKEN_ELEVATION as *mut c_void,
                size,
                &mut size,
            ) == TRUE
            {
                elevated = elevation.TokenIsElevated != 0;
            }
        }
        if token != INVALID_HANDLE_VALUE {
            CloseHandle(token);
        }
    }
    elevated
}
#[cfg(unix)]
#[must_use]
fn is_root_internal() -> bool {
    users::get_current_uid() == 0
}