viz-shell 0.1.0

One shell for every repo
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! The host user, recreated inside the container with the same name, uid,
//! gid, group and home, so that tools which look the user up — ssh first
//! among them — find the same account as on the host.

use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, ensure};

use crate::constants::{GID_ENV, GROUP_ENV, HOME_ARG, HOME_ENV, UID_ENV, USER_ENV};

#[derive(Debug, Clone, PartialEq)]
pub struct User {
    pub name: String,
    pub uid: u32,
    pub gid: u32,
    pub group: String,
    pub home: PathBuf,
}

impl User {
    /// The user running vz. Names come from `id`, which resolves them the
    /// way the host does, directory services included.
    pub fn of_host() -> anyhow::Result<Self> {
        let home = std::env::var_os(HOME_ENV).context("HOME is not set")?;
        Ok(Self {
            name: id("-un")?,
            uid: id("-u")?.parse().context("parsing `id -u`")?,
            gid: id("-g")?.parse().context("parsing `id -g`")?,
            group: id("-gn")?,
            home: home.into(),
        })
    }

    /// Inside the container: the user the launcher passed in.
    pub fn from_env() -> anyhow::Result<Self> {
        Ok(Self {
            name: env(USER_ENV)?,
            uid: env(UID_ENV)?.parse().context(UID_ENV)?,
            gid: env(GID_ENV)?.parse().context(GID_ENV)?,
            group: env(GROUP_ENV)?,
            home: env(HOME_ENV)?.into(),
        })
    }

    /// The variables that carry this user into the container.
    pub fn env(&self) -> [(&'static str, String); 5] {
        [
            (USER_ENV, self.name.clone()),
            (UID_ENV, self.uid.to_string()),
            (GID_ENV, self.gid.to_string()),
            (GROUP_ENV, self.group.clone()),
            (HOME_ENV, self.home.to_string_lossy().into_owned()),
        ]
    }

    /// The build args a Dockerfile may declare to bake this user into the
    /// image: the runtime variables, with the home as `VZ_HOME`.
    pub fn build_args(&self) -> [(&'static str, String); 5] {
        [
            (USER_ENV, self.name.clone()),
            (UID_ENV, self.uid.to_string()),
            (GID_ENV, self.gid.to_string()),
            (GROUP_ENV, self.group.clone()),
            (HOME_ARG, self.home.to_string_lossy().into_owned()),
        ]
    }

    /// No password (`*`), rather than `x` for one in /etc/shadow: the user has
    /// no shadow entry, and PAM would refuse the account, sudo included.
    pub fn passwd_line(&self, shell: &Path) -> String {
        format!(
            "{}:*:{}:{}::{}:{}",
            self.name,
            self.uid,
            self.gid,
            self.home.display(),
            shell.display()
        )
    }

    pub fn group_line(&self) -> String {
        format!("{}:x:{}:", self.group, self.gid)
    }

    /// Whether the user has to be added. The image may already have it with
    /// the same name, uid and home; any other clash is refused.
    pub fn needs_user(&self, passwd: &str) -> anyhow::Result<bool> {
        let home = self.home.to_string_lossy();
        for entry in entries(passwd) {
            let same_name = entry.name == self.name;
            let same_uid = entry.id == Some(self.uid);
            if same_name && same_uid {
                ensure!(
                    entry.home == Some(&*home),
                    "the image's user `{}` has home {}, the host's is {}; \
                     ssh finds ~/.ssh through the image's",
                    self.name,
                    entry.home.unwrap_or_default(),
                    home
                );
                return Ok(false);
            }
            ensure!(
                !same_uid,
                "the image has user `{}` with uid {}, which clashes with the host's `{}`",
                entry.name,
                self.uid,
                self.name
            );
            ensure!(
                !same_name,
                "the image has user `{}` with uid {}, which clashes with the host's uid {}",
                self.name,
                entry.id.map(|id| id.to_string()).unwrap_or_default(),
                self.uid
            );
        }
        Ok(true)
    }

    /// Whether the group has to be added. The image may already have it with
    /// the same name and gid; any other clash is refused.
    pub fn needs_group(&self, group_file: &str) -> anyhow::Result<bool> {
        for entry in entries(group_file) {
            let same_name = entry.name == self.group;
            let same_gid = entry.id == Some(self.gid);
            if same_name && same_gid {
                return Ok(false);
            }
            ensure!(
                !same_name && !same_gid,
                "the image has group `{}` with gid {}, which clashes with the host's `{}` (gid {})",
                entry.name,
                entry.id.map(|id| id.to_string()).unwrap_or_default(),
                self.group,
                self.gid
            );
        }
        Ok(true)
    }
}

/// A group the user joins besides their own, such as the docker socket's.
#[derive(Debug, Clone, PartialEq)]
pub struct ExtraGroup {
    pub name: String,
    pub gid: u32,
}

impl ExtraGroup {
    /// `name:gid`, comma-separated; empty for none.
    pub fn parse_list(text: &str) -> anyhow::Result<Vec<Self>> {
        text.split(',')
            .filter(|item| !item.is_empty())
            .map(|item| {
                let (name, gid) = item
                    .split_once(':')
                    .with_context(|| format!("extra group `{item}` is not `name:gid`"))?;
                let gid = gid
                    .parse()
                    .with_context(|| format!("extra group `{item}` has no numeric gid"))?;
                Ok(Self {
                    name: name.to_owned(),
                    gid,
                })
            })
            .collect()
    }

    /// The `/etc/group` line that gives the gid a name, with `user` as a
    /// member: none if the image already has the gid. The host's name when
    /// the image does not use it, `host-<name>` when it does.
    pub fn line_to_add(&self, group_file: &str, user: &str) -> Option<String> {
        if entries(group_file).any(|entry| entry.id == Some(self.gid)) {
            return None;
        }
        let name_taken = entries(group_file).any(|entry| entry.name == self.name);
        let name = if name_taken {
            format!("host-{}", self.name)
        } else {
            self.name.clone()
        };
        Some(format!("{name}:x:{}:{user}", self.gid))
    }
}

/// `text` with `line` appended on a line of its own.
pub fn with_line(text: &str, line: &str) -> String {
    let separator = if text.is_empty() || text.ends_with('\n') {
        ""
    } else {
        "\n"
    };
    format!("{text}{separator}{line}\n")
}

/// The name and numeric id of each line in `/etc/passwd` or `/etc/group`,
/// and the home of a `/etc/passwd` line.
struct Entry<'a> {
    name: &'a str,
    id: Option<u32>,
    home: Option<&'a str>,
}

fn entries(text: &str) -> impl Iterator<Item = Entry<'_>> {
    text.lines()
        .filter(|line| !line.trim().is_empty() && !line.starts_with('#'))
        .map(|line| {
            let mut fields = line.split(':');
            let name = fields.next().unwrap_or_default();
            let id = fields.nth(1).and_then(|id| id.parse().ok());
            let home = fields.nth(2);
            Entry { name, id, home }
        })
}

fn id(flag: &str) -> anyhow::Result<String> {
    let output = Command::new("id")
        .arg(flag)
        .output()
        .with_context(|| format!("running `id {flag}`"))?;
    ensure!(output.status.success(), "`id {flag}` failed");
    Ok(String::from_utf8(output.stdout)
        .with_context(|| format!("`id {flag}` printed non-UTF-8"))?
        .trim_end()
        .to_owned())
}

fn env(name: &str) -> anyhow::Result<String> {
    std::env::var(name).with_context(|| {
        format!("{name} is not set; `vz entrypoint` runs only in a container vz started")
    })
}

#[cfg(test)]
#[allow(non_snake_case)] // unit__scenario__expected test names
mod tests {
    use super::*;

    const PASSWD: &str = "root:x:0:0:root:/root:/bin/bash\n\
                          daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n";
    const GROUP: &str = "root:x:0:\ndaemon:x:1:\n";

    fn sally() -> User {
        User {
            name: "sally".to_owned(),
            uid: 1000,
            gid: 1000,
            group: "sally".to_owned(),
            home: PathBuf::from("/home/sally"),
        }
    }

    #[test]
    fn passwd_line__user_and_shell__is_passwd_format() {
        let line = sally().passwd_line(Path::new("/bin/bash"));

        assert_eq!(line, "sally:*:1000:1000::/home/sally:/bin/bash");
    }

    #[test]
    fn group_line__user__is_group_format() {
        let line = sally().group_line();

        assert_eq!(line, "sally:x:1000:");
    }

    #[test]
    fn needs_user__absent__is_true() {
        let needed = sally().needs_user(PASSWD).unwrap();

        assert!(needed);
    }

    #[test]
    fn needs_user__same_name_uid_and_home__is_false() {
        let passwd = format!("{PASSWD}sally:x:1000:1000::/home/sally:/bin/bash\n");

        let needed = sally().needs_user(&passwd).unwrap();

        assert!(!needed);
    }

    #[test]
    fn needs_user__same_user_other_home__refuses_naming_both_homes() {
        let passwd = format!("{PASSWD}sally:x:1000:1000::/home/other:/bin/bash\n");

        let error = sally().needs_user(&passwd).unwrap_err();

        assert!(
            error.to_string().contains(
                "the image's user `sally` has home /home/other, the host's is /home/sally"
            ),
            "{error}"
        );
    }

    #[test]
    fn needs_user__uid_taken_by_another__refuses_naming_the_user() {
        let passwd = format!("{PASSWD}ubuntu:x:1000:1000::/home/ubuntu:/bin/bash\n");

        let error = sally().needs_user(&passwd).unwrap_err();

        assert!(
            error.to_string().contains(
                "the image has user `ubuntu` with uid 1000, which clashes with the host's `sally`"
            ),
            "{error}"
        );
    }

    #[test]
    fn needs_user__name_taken_with_another_uid__refuses() {
        let passwd = format!("{PASSWD}sally:x:1001:1001::/home/sally:/bin/sh\n");

        let error = sally().needs_user(&passwd).unwrap_err();

        assert!(
            error.to_string().contains(
                "the image has user `sally` with uid 1001, which clashes with the host's uid 1000"
            ),
            "{error}"
        );
    }

    #[test]
    fn needs_group__absent__is_true() {
        let needed = sally().needs_group(GROUP).unwrap();

        assert!(needed);
    }

    #[test]
    fn needs_group__same_name_and_gid__is_false() {
        let group = format!("{GROUP}sally:x:1000:\n");

        let needed = sally().needs_group(&group).unwrap();

        assert!(!needed);
    }

    #[test]
    fn needs_group__gid_taken_by_another__refuses() {
        let group = format!("{GROUP}ubuntu:x:1000:\n");

        let error = sally().needs_group(&group).unwrap_err();

        assert!(
            error.to_string().contains(
                "the image has group `ubuntu` with gid 1000, which clashes with the host's `sally`"
            ),
            "{error}"
        );
    }

    #[test]
    fn needs_group__name_taken_with_another_gid__refuses() {
        let group = format!("{GROUP}sally:x:1001:\n");

        let error = sally().needs_group(&group).unwrap_err();

        assert!(
            error.to_string().contains(
                "the image has group `sally` with gid 1001, which clashes with the host's `sally` (gid 1000)"
            ),
            "{error}"
        );
    }

    fn group(name: &str, gid: u32) -> ExtraGroup {
        ExtraGroup {
            name: name.to_owned(),
            gid,
        }
    }

    #[test]
    fn parse_list__none_one_or_several__each_name_and_gid() {
        let cases = [
            ("", vec![]),
            ("docker:969", vec![group("docker", 969)]),
            (
                "docker:969,audio:29",
                vec![group("docker", 969), group("audio", 29)],
            ),
        ];
        for (text, expected) in cases {
            let groups = ExtraGroup::parse_list(text).unwrap();

            assert_eq!(groups, expected, "{text:?}");
        }
    }

    #[test]
    fn parse_list__malformed__is_refused_naming_the_item() {
        let cases = [
            ("docker", "extra group `docker` is not `name:gid`"),
            ("docker:x", "extra group `docker:x` has no numeric gid"),
        ];
        for (text, expected) in cases {
            let error = ExtraGroup::parse_list(text).unwrap_err().to_string();

            assert!(error.contains(expected), "{error}");
        }
    }

    #[test]
    fn line_to_add__gid_new_to_the_image__a_line_naming_it() {
        let cases = [
            ("the name free", GROUP, "docker:x:969:sally"),
            (
                "the name taken: host- before it",
                "root:x:0:\ndocker:x:101:\n",
                "host-docker:x:969:sally",
            ),
        ];
        for (case, group_file, expected) in cases {
            let line = group("docker", 969).line_to_add(group_file, "sally");

            assert_eq!(line.as_deref(), Some(expected), "{case}");
        }
    }

    #[test]
    fn line_to_add__gid_the_image_has__none() {
        let group_file = "root:x:0:\nsomething:x:969:\n";

        let line = group("docker", 969).line_to_add(group_file, "sally");

        assert_eq!(line, None);
    }

    #[test]
    fn with_line__with_or_without_a_trailing_newline__the_line_on_its_own() {
        let cases = [("", "a\n"), ("root\n", "root\na\n"), ("root", "root\na\n")];
        for (text, expected) in cases {
            let appended = with_line(text, "a");

            assert_eq!(appended, expected, "text: {text:?}");
        }
    }
}