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
// This file is part of environment-sanity. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/environment-sanity/master/COPYRIGHT. No part of environment-sanity, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of environment-sanity. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/environment-sanity/master/COPYRIGHT.


#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![feature(command_envs)]


extern crate memchr;


use ::std::collections::HashMap;
use ::std::collections::HashSet;
use ::std::env::args_os;
use ::std::env::vars_os;
use ::std::ffi::OsString;
use ::std::fs::File;
use ::std::io::BufReader;
use ::std::io::prelude::*;
#[cfg(unix)] use ::std::os::unix::ffi::OsStringExt;
#[cfg(unix)] use ::std::os::unix::ffi::OsStrExt;
#[cfg(unix)] use ::std::os::unix::process::CommandExt;
use ::std::path::Path;
use ::std::process::Command;
use ::std::process::Stdio;


macro_rules! warn
{
	($message:tt, $($arg:tt)*) =>
	{
		{
			use ::std::io::Write;
			let result = writeln!(&mut ::std::io::stderr(), concat!("environment-sanity:WARN:", $message), $($arg)*);
			result.expect("Could not write line to stderr");
		}
	}
}

#[macro_export]
macro_rules! fatalExit
{
	($message:tt, $($arg:tt)*) =>
	{
		{
			use ::std::io::Write;
			let result = writeln!(&mut ::std::io::stderr(), concat!("environment-sanity:EXIT:", $message), $($arg)*);
			result.expect("Could not write line to stderr");
			::std::process::exit(1);
		}
	}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EnvironmentVariable(OsString);

const AsciiNul: u8 = 0x00;

fn osStringFromRawBytesWithoutADelimiter(mut environmentVariableRawBytes: Vec<u8>) -> OsString
{
	environmentVariableRawBytes.push(AsciiNul);
	OsString::from_vec(environmentVariableRawBytes)
}

impl EnvironmentVariable
{
	pub fn fromRawBytesWithoutADelimiter(environmentVariableRawBytes: Vec<u8>) -> Self
	{
		EnvironmentVariable(osStringFromRawBytesWithoutADelimiter(environmentVariableRawBytes))
	}
	
	pub fn to_os_string(self) -> OsString
	{
		self.0
	}
}

impl<'a> From<&'a str> for EnvironmentVariable
{
	fn from(string: &'a str) -> Self
	{
		EnvironmentVariable(OsString::from(string))
	}
}

#[derive(Debug, Clone)]
pub struct BlackList(HashSet<EnvironmentVariable>);

impl BlackList
{
	pub fn new(defaultBlackList: Vec<EnvironmentVariable>) -> Self
	{
		let mut blackList = BlackList(HashSet::with_capacity(defaultBlackList.len() + 8));
		for environmentVariableName in defaultBlackList
		{
			blackList.0.insert(environmentVariableName);
		}
		blackList
	}
	
	pub fn addToFromFile(&mut self, filePath: &Path)
	{
		addEnvironmentVariablesFromLinesListedInFile("Black", filePath, |environmentVariableName, _, _|
		{
			self.0.insert(environmentVariableName);
		});
	}
	
	pub fn isBlackListed(&self, environmentVariableName: &EnvironmentVariable) -> bool
	{
		self.0.contains(environmentVariableName)
	}
	
	pub fn isNotBlackListed(&self, environmentVariableName: &EnvironmentVariable) -> bool
	{
		!self.isBlackListed(environmentVariableName)
	}
}

#[derive(Debug, Clone)]
pub struct WhiteList<'a>(HashSet<EnvironmentVariable>, &'a BlackList);

impl<'a> WhiteList<'a>
{
	pub fn new(blackList: &'a BlackList, defaultWhiteList: Vec<EnvironmentVariable>) -> Self
	{
		let mut whiteList = WhiteList(HashSet::with_capacity(defaultWhiteList.len() + 64), blackList);
		for environmentVariableName in defaultWhiteList
		{
			if whiteList.1.isBlackListed(&environmentVariableName)
			{
				fatalExit!("Environment variable '{:?}' occurs in the defaults for the black list AND the white list", environmentVariableName);
			}
			whiteList.0.insert(environmentVariableName);
		}
		whiteList
	}
	
	pub fn addToFromFile(&mut self, filePath: &Path)
	{
		addEnvironmentVariablesFromLinesListedInFile("White", filePath, |environmentVariableName, filePath, line|
		{
			if self.1.isBlackListed(&environmentVariableName)
			{
				warn!("Black list contains environment variable '{:?}' white listed in file '{:?}' at line '{}' (all offsets are zero-based)", environmentVariableName, filePath, line);
			}
			else
			{
				self.0.insert(environmentVariableName);
			}
		});
	}
	
	pub fn isWhiteListed(&self, environmentVariableName: &EnvironmentVariable) -> bool
	{
		self.0.contains(environmentVariableName)
	}
	
	pub fn filterEnvironment(&self) -> HashMap<OsString, OsString>
	{
		let blackList = self.1;
		vars_os()
		.filter(|&(ref environmentVariableName, _)| blackList.isNotBlackListed(&EnvironmentVariable(environmentVariableName.to_os_string())))
		.filter(|&(ref environmentVariableName, _)| self.isWhiteListed(&EnvironmentVariable(environmentVariableName.to_os_string())))
		.collect()
	}
}

#[derive(Debug, Clone)]
pub struct SettingsList(HashMap<EnvironmentVariable, OsString>);

impl SettingsList
{
	pub fn new(defaultSettingsList: HashMap<EnvironmentVariable, OsString>) -> Self
	{
		SettingsList(defaultSettingsList)
	}
	
	pub fn addSettingsToEnvironment(self, mut environment: HashMap<OsString, OsString>) -> HashMap<OsString, OsString>
	{
		for (environmentVariableName, value) in self.0
		{
			environment.insert(environmentVariableName.to_os_string(), value);
		}
		environment
	}
	
	pub fn addToFromFile(&mut self, filePath: &Path)
	{
		addFromLinesListedInFile("Settings", filePath, |environmentVariableRawBytesExcludingDelimiter, fileKind, filePath, line|
		{
			const Tab: u8 = b'\t';
			match memchr::memchr(Tab, environmentVariableRawBytesExcludingDelimiter.as_slice())
			{
				None => fatalExit!("There is no tab delimiter in {} list file '{:?}' at line '{}' (all offsets are zero-based)", fileKind, filePath, line),
				Some(index) =>
				{
					let name = EnvironmentVariable::fromRawBytesWithoutADelimiter(Vec::from(&environmentVariableRawBytesExcludingDelimiter[0..index]));
					let value = osStringFromRawBytesWithoutADelimiter(Vec::from(&environmentVariableRawBytesExcludingDelimiter[index + 1..]));
					
					self.0.insert(name, value);
				}
			}
		})
	}
}

/// This logic only works if there are not any LineFeed characters EMBEDDED within a line
/// This logic does not play nice on Windows with NotePad (which insists on using CRLF to end lines), but it does allow commonality of definitions
fn addFromLinesListedInFile<A: FnMut(Vec<u8>, &'static str, &Path, u64)>(fileKind: &'static str, filePath: &Path, mut add: A)
{
	let file = match File::open(filePath)
	{
		Ok(file) => file,
		Err(_) => fatalExit!("Could not open {} list file '{:?}' for reading", fileKind.to_lowercase(), filePath),
	};
	
	let bufferedReader = BufReader::with_capacity(4096, file);
	
	const LineFeed: u8 = 0x0A;
	let mut line = 0;
	for environmentVariableRawBytesExcludingDelimiter in bufferedReader.split(LineFeed)
	{
		match environmentVariableRawBytesExcludingDelimiter
		{
			Err(_) => fatalExit!("Could not read line '{}' in {} list file '{:?}' (all offsets are zero-based)", line, fileKind.to_lowercase(), filePath),
			Ok(environmentVariableRawBytes) =>
			{
				if let Some(column) = memchr::memchr(AsciiNul, environmentVariableRawBytes.as_slice())
				{
					fatalExit!("{} list file '{:?}' at line '{}' contains an ASCII NUL at column '{}' (all offsets are zero-based)", fileKind, filePath, line, column)
				}
				
				add(environmentVariableRawBytes, fileKind, filePath, line)
			},
		}
		line += 1;
	}
}

fn addEnvironmentVariablesFromLinesListedInFile<A: FnMut(EnvironmentVariable, &Path, u64)>(fileKind: &'static str, filePath: &Path, mut add: A)
{
	addFromLinesListedInFile(fileKind, filePath, |environmentVariableRawBytesExcludingDelimiter, _, filePath, line|
	{
		let environmentVariableName = EnvironmentVariable::fromRawBytesWithoutADelimiter(environmentVariableRawBytesExcludingDelimiter);
		add(environmentVariableName, filePath, line);
	});
}

pub fn parseCommandLineArguments() -> (OsString, Vec<OsString>)
{
	// This logic is designed to work with sha-bang paths, eg
	// /usr/bin/environment-sanity program-to-invoke <any> <other> <arguments>
	// sha-bang paths as used as a command interpreter may not support <any> <other> <arguments>
	
	// Skip the first argument, which is 'us'
	let mut inputArguments = args_os().skip(1);
	
	// Take the second argument, which is the program to invoke
	let programName = match inputArguments.next()
	{
		None => fatalExit!("Please provide at least one argument, which is the program to {}", "invoke"),
		Some(programName) =>
		{
			if programName.is_empty()
			{
				fatalExit!("{}", "First argument can not be empty");
			}
			
			const Slash: u8 = b'/';
			if memchr::memchr(Slash, programName.as_os_str().as_bytes()).is_some()
			{
				fatalExit!("First argument is the program name to invoke. It must be a file, not a path like '{:?}'", programName);
			}
			
			programName
		}
	};
	
	let outputArguments = inputArguments.collect();
	
	(programName, outputArguments)
}

pub fn execute(programName: OsString, arguments: Vec<OsString>, filteredEnvironment: HashMap<OsString, OsString>) -> !
{
	let error = Command::new(&programName)
	.stdin(Stdio::inherit())
	.stdout(Stdio::inherit())
	.stderr(Stdio::inherit())
	.args(&arguments)
	.env_clear().envs(&filteredEnvironment)
	.exec();
	
	fatalExit!("Could not execute '{:?}' because '{:?}'", programName, error);
}