Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try detecting sudo when running rustup-init #617

Merged
merged 3 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/rustup-cli/setup_mode.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
use std::env;
use std::process;
use self_update::{self, InstallOpts};
use errors::*;
use clap::{App, Arg, AppSettings};
use rustup_dist::dist::TargetTriple;
use common;

mod sys_check {
#[cfg(unix)]
pub fn home_mismatch() -> bool {
extern crate libc as c;

use std::env;
use std::ffi::CStr;
use std::mem;
use std::ops::Deref;
use std::ptr;

// test runner should set this, nothing else
if env::var("RUSTUP_INIT_SKIP_SUDO_CHECK").as_ref().map(Deref::deref).ok() == Some("yes") {
return false;
}
let mut pwd = unsafe { mem::uninitialized::<c::passwd>() };
let mut pwdp: *mut c::passwd = ptr::null_mut();
let mut buf = [0u8; 1024];
let rv = unsafe { c::getpwuid_r(c::geteuid(), &mut pwd, mem::transmute(&mut buf), buf.len(), &mut pwdp) };
if rv != 0 || pwdp == ptr::null_mut() {
warn!("getpwuid_r: couldn't get user data");
return false;
}
let pw_dir = unsafe { CStr::from_ptr(pwd.pw_dir) }.to_str().ok();
let env_home = env::var_os("HOME");
let env_home = env_home.as_ref().map(Deref::deref);
match (env_home, pw_dir) {
(None, _) | (_, None) => false,
(Some(ref eh), Some(ref pd)) => eh != pd
}
}

#[cfg(not(unix))]
pub fn home_mismatch() -> bool {
false
}
}

pub fn main() -> Result<()> {
let args: Vec<_> = env::args().collect();
let arg1 = args.get(1).map(|a| &**a);
Expand Down Expand Up @@ -39,6 +78,17 @@ pub fn main() -> Result<()> {

let matches = cli.get_matches();
let no_prompt = matches.is_present("no-prompt");
match (self::sys_check::home_mismatch(), no_prompt) {
(false, _) => (),
(true, false) => {
err!("$HOME differs from euid-obtained home directory: you may be using sudo");
err!("if this is what you want, restart the installation with `-y'");
process::exit(1);
},
(true, true) => {
warn!("$HOME differs from euid-obtained home directory: you may be using sudo");
}
}
let verbose = matches.is_present("verbose");
let default_host = matches.value_of("default-host").map(|s| s.to_owned()).unwrap_or_else(|| {
TargetTriple::from_host_or_build().to_string()
Expand Down
3 changes: 3 additions & 0 deletions src/rustup-mock/src/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ pub fn env(config: &Config, cmd: &mut Command) {
// This is only used for some installation tests on unix where CARGO_HOME
// above is unset
cmd.env("HOME", config.homedir.to_string_lossy().to_string());

// Setting HOME will confuse the sudo check for rustup-init. Override it
cmd.env("RUSTUP_INIT_SKIP_SUDO_CHECK", "yes");
}

pub fn run(config: &Config, name: &str, args: &[&str], env: &[(&str, &str)]) -> SanitizedOutput {
Expand Down