Ejemplo n.º 1
0
func TestUidshift(t *testing.T) {
	if os.Getuid() != 0 {
		t.Fatal("Tests needs to be run as root")
	}
	idmap := fuidshift.IdmapSet{}
	idmap, err := idmap.Append("b:0:100000:65536")
	ok(t, err)

	tempdir, err := ioutil.TempDir(os.TempDir(), "fuidshift")
	ok(t, err)
	defer os.Remove(tempdir)

	dir := path.Join(tempdir, "dir")
	ok(t, os.Mkdir(dir, 0700))
	ok(t, os.Chown(dir, 1, 1))

	file := path.Join(tempdir, "file")
	ok(t, ioutil.WriteFile(file, []byte("hello\ngo\n"), 0700))
	ok(t, os.Chown(file, 0, 0))

	ok(t, idmap.UidshiftIntoContainer(tempdir, false))
	assertOwnership(t, dir, 100001, 100001)
	assertOwnership(t, file, 100000, 100000)
	ok(t, idmap.UidshiftFromContainer(tempdir, false))
	assertOwnership(t, dir, 1, 1)
	assertOwnership(t, file, 0, 0)
}
Ejemplo n.º 2
0
func run() error {
	if len(os.Args) < 3 {
		if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "help") {
			help(os.Args[0], 0)
		} else {
			help(os.Args[0], 1)
		}
	}

	directory := os.Args[1]
	idmap := fuidshift.IdmapSet{}
	testmode := false
	reverse := false

	for pos := 2; pos < len(os.Args); pos++ {

		switch os.Args[pos] {
		case "-r", "--reverse":
			reverse = true
		case "t", "-t", "--test", "test":
			testmode = true
		default:
			var err error
			idmap, err = idmap.Append(os.Args[pos])
			if err != nil {
				return err
			}
		}
	}

	if idmap.Len() == 0 {
		fmt.Printf("No idmaps given\n")
		help(os.Args[0], 1)
	}

	if !testmode && os.Geteuid() != 0 {
		fmt.Printf("This must be run as root\n")
		os.Exit(1)
	}

	if reverse {
		return idmap.UidshiftFromContainer(directory, testmode)
	}
	return idmap.UidshiftIntoContainer(directory, testmode)
}