-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathedit.rs
More file actions
351 lines (310 loc) · 10.5 KB
/
edit.rs
File metadata and controls
351 lines (310 loc) · 10.5 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#![allow(unsafe_code)]
use std::ffi::OsString;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::net::Shutdown;
use std::os::unix::{fs::OpenOptionsExt, net::UnixStream, process::ExitStatusExt};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{io, process};
use crate::common::SudoPath;
use crate::exec::ExitReason;
use crate::log::{user_error, user_info};
use crate::system::file::{FileLock, create_temporary_dir};
use crate::system::wait::{Wait, WaitError, WaitOptions};
use crate::system::{ForkResult, audit, fork, mark_fds_as_cloexec};
struct ParentFileInfo<'a> {
path: &'a Path,
file: File,
lock: FileLock,
old_data: Vec<u8>,
new_data_rx: UnixStream,
new_data: Option<Vec<u8>>,
}
struct ChildFileInfo<'a> {
path: &'a Path,
old_data: Vec<u8>,
tempfile_path: Option<PathBuf>,
new_data_tx: UnixStream,
}
pub(super) fn edit_files(
editor: (PathBuf, Vec<OsString>),
selected_files: Vec<(&SudoPath, File)>,
) -> io::Result<ExitReason> {
let mut files = vec![];
let mut child_files = vec![];
for (path, mut file) in selected_files {
// Error for special files
let metadata = file.metadata().map_err(|e| {
io::Error::new(
e.kind(),
xlat!(
"failed to read metadata for {path}: {error}",
path = path.display(),
error = e
),
)
})?;
if !metadata.is_file() {
return Err(io::Error::other(xlat!(
"file {path} is not a regular file",
path = path.display()
)));
}
// Take file lock
let lock = FileLock::exclusive(&file, true).map_err(|e| {
io::Error::new(
e.kind(),
xlat!(
"failed to lock {path}: {error}",
path = path.display(),
error = e
),
)
})?;
// Read file
let mut old_data = Vec::new();
file.read_to_end(&mut old_data).map_err(|e| {
io::Error::new(
e.kind(),
xlat!(
"failed to read {path}: {error}",
path = path.display(),
error = e
),
)
})?;
// Create socket
let (parent_socket, child_socket) = UnixStream::pair()?;
files.push(ParentFileInfo {
path,
file,
lock,
old_data: old_data.clone(),
new_data_rx: parent_socket,
new_data: None,
});
child_files.push(ChildFileInfo {
path,
old_data,
tempfile_path: None,
new_data_tx: child_socket,
});
}
// Spawn child
// SAFETY: There should be no other threads at this point.
let ForkResult::Parent(command_pid) = unsafe { fork() }? else {
drop(files);
handle_child(editor, child_files)
};
drop(child_files);
for file in &mut files {
// Read from socket
file.new_data =
Some(read_stream(&mut file.new_data_rx).map_err(|e| {
io::Error::new(e.kind(), format!("Failed to read from socket: {e}"))
})?);
}
// If child has error, exit with non-zero exit code
let status = loop {
match command_pid.wait(WaitOptions::new()) {
Ok((_, status)) => break status,
Err(WaitError::Io(err)) if err.kind() == io::ErrorKind::Interrupted => {}
Err(err) => panic!("{err:?}"),
}
};
assert!(status.did_exit());
if let Some(signal) = status.term_signal() {
return Ok(ExitReason::Signal(signal));
} else if let Some(code) = status.exit_status() {
if code != 0 {
return Ok(ExitReason::Code(code));
}
} else {
return Ok(ExitReason::Code(1));
}
for mut file in files {
let data = file.new_data.expect("filled in above");
if data == file.old_data {
// File unchanged. No need to write it again.
user_info!("{path} unchanged", path = file.path.display());
continue;
}
// FIXME check if modified since reading and if so ask user what to do
// Write file
(move || {
file.file.rewind()?;
file.file.write_all(&data)?;
file.file.set_len(
data.len()
.try_into()
.expect("more than 18 exabyte of data???"),
)
})()
.map_err(|e| {
io::Error::new(
e.kind(),
xlat!(
"failed to write {path}: {error}",
path = file.path.display(),
error = e
),
)
})?;
drop(file.lock);
}
Ok(ExitReason::Code(0))
}
struct TempDirDropGuard(PathBuf);
impl Drop for TempDirDropGuard {
fn drop(&mut self) {
if let Err(e) = std::fs::remove_dir_all(&self.0) {
user_error!(
"failed to remove temporary directory {path}: {error}",
path = self.0.display(),
error = e
);
};
}
}
fn handle_child(editor: (PathBuf, Vec<OsString>), file: Vec<ChildFileInfo<'_>>) -> ! {
match handle_child_inner(editor, file) {
Ok(()) => process::exit(0),
Err(err) => {
user_error!("{error}", error = err);
process::exit(1);
}
}
}
// FIXME maybe use pipes once std::io::pipe has been stabilized long enough.
fn handle_child_inner(
editor: (PathBuf, Vec<OsString>),
mut files: Vec<ChildFileInfo<'_>>,
) -> Result<(), String> {
mark_fds_as_cloexec().map_err(|e| format!("Failed to mark fds as CLOEXEC: {e}"))?;
// root privileges are dangerous after this point, since we are about to manipulate the
// file system and execute a command under control of the user, so drop them
audit::irrevocably_drop_privileges();
let tempdir = TempDirDropGuard(
create_temporary_dir()
.map_err(|e| xlat!("failed to create temporary directory: {error}", error = e))?,
);
for (i, file) in files.iter_mut().enumerate() {
// Create temp file
let dir = tempdir.0.join(format!("{i}"));
std::fs::create_dir(&dir).map_err(|e| {
xlat!(
"failed to create temporary directory {path}: {error}",
path = dir.display(),
error = e
)
})?;
let tempfile_path = dir.join(file.path.file_name().expect("file must have filename"));
let mut tempfile = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.mode(0o600)
.open(&tempfile_path)
.map_err(|e| {
xlat!(
"failed to create temporary file {path}: {error}",
path = tempfile_path.display(),
error = e
)
})?;
// Write to temp file
tempfile.write_all(&file.old_data).map_err(|e| {
xlat!(
"failed to write to temporary file {path}: {error}",
path = tempfile_path.display(),
error = e
)
})?;
drop(tempfile);
file.tempfile_path = Some(tempfile_path);
}
// Spawn editor
let status = Command::new(&editor.0)
.args(editor.1)
.args(
files
.iter()
.map(|file| file.tempfile_path.as_ref().expect("filled in above")),
)
.status()
.map_err(|e| {
xlat!(
"failed to run editor {path}: {error}",
path = editor.0.display(),
error = e
)
})?;
if !status.success() {
drop(tempdir);
if let Some(signal) = status.signal() {
process::exit(128 + signal);
}
process::exit(status.code().unwrap_or(1));
}
for mut file in files {
let tempfile_path = file.tempfile_path.as_ref().expect("filled in above");
// Read from temp file
let new_data = std::fs::read(tempfile_path).map_err(|e| {
xlat!(
"failed to read from temporary file {path}: {error}",
path = tempfile_path.display(),
error = e
)
})?;
// FIXME preserve temporary file if the original couldn't be written to
std::fs::remove_file(tempfile_path).map_err(|e| {
xlat!(
"failed to remove temporary file {path}: {error}",
path = tempfile_path.display(),
error = e
)
})?;
// If the file has been changed to be empty, ask the user what to do.
if new_data.is_empty() && new_data != file.old_data {
// TRANSLATORS: the initial letters of 'yes' and 'no' responses, in that order
let answers = xlat!("yn");
match crate::visudo::ask_response(
&xlat!(
"sudoedit: truncate {path} to zero? (y/n) [n] ",
path = file.path.display()
),
answers,
answers
.chars()
.last()
.expect("translation files are corrupted"),
) {
Ok(val) if answers.starts_with(val) => {}
// a fallback: also accept 'yes' based on Debian's apt behaviour
Ok('y') if !answers.contains('y') => {}
_ => {
user_info!("not overwriting {path}", path = file.path.display());
// Parent ignores write when new data matches old data
write_stream(&mut file.new_data_tx, &file.old_data)
.map_err(|e| xlat!("failed to write data to parent: {error}", error = e))?;
continue;
}
}
}
// Write to socket
write_stream(&mut file.new_data_tx, &new_data)
.map_err(|e| xlat!("failed to write data to parent: {error}", error = e))?;
}
process::exit(0);
}
fn write_stream(socket: &mut UnixStream, data: &[u8]) -> io::Result<()> {
socket.write_all(data)?;
socket.shutdown(Shutdown::Both)?;
Ok(())
}
fn read_stream(socket: &mut UnixStream) -> io::Result<Vec<u8>> {
let mut new_data = Vec::new();
socket.read_to_end(&mut new_data)?;
Ok(new_data)
}