@@ -16,37 +16,75 @@ trap "{ rm -r $IMGDIRECTORY; }" EXIT
1616
1717DIFFOSCOPE=" diffoscope"
1818
19+ function get_subguids() {
20+ local user=$( id -u)
21+ local subuids
22+ local subgids
23+ while IFS=: read uid start count ; do
24+ if [[ $user == $( id -u $uid ) ]] ; then
25+ subuids=" 1:$start :$count "
26+ break
27+ fi
28+ done < /etc/subuid
29+ while IFS=: read uid start count ; do
30+ if [[ $user == $( id -u $uid ) ]] ; then
31+ subgids=" 1:$start :$count "
32+ break
33+ fi
34+ done < /etc/subgid
35+ [[ $subuids && $subgids ]] || return 1
36+ printf " --uid_mapping %s --gid_mapping %s " " $subuids " " $subgids "
37+ }
38+
39+ # Desc: Enter a user namespace with virtual privileges
40+ function become_rootless() {
41+ (( rootless_userns)) || return
42+ (( __REPRO_NSJAIL == 1 )) && return
43+ local subguids=$( get_subguids)
44+ if (( $? )) ; then
45+ error " Your user has no subuids or subgids"
46+ exit 1
47+ fi
48+ exec nsjail -Mo --quiet --skip_setsid \
49+ --disable_clone_newnet --disable_clone_newpid \
50+ --disable_rlimit --disable_proc --keep_caps \
51+ --chroot / --cwd " $( pwd) " --rw \
52+ --uid 0 --gid 0 $subguids \
53+ --keep_env -E ' __REPRO_NSJAIL=1' -- " ${orig_argv[@]} "
54+ # exec become-root unshare --mount "${orig_argv[@]}"
55+ }
56+
1957# Desc: Escalates privileges
2058orig_argv=(" $0 " " $@ " )
2159src_owner=${SUDO_USER:- $USER }
2260function check_root() {
2361 (( EUID == 0 )) && return
24- if (( rootless_userns)) ; then
25- exec become-root unshare --mount " ${orig_argv[@]} "
26- elif type -P sudo > /dev/null; then
62+ if type -P sudo > /dev/null; then
2763 exec sudo -- " ${orig_argv[@]} "
2864 else
2965 exec su root -c " $( printf ' %q' " ${orig_argv[@]} " ) "
3066 fi
3167}
3268
3369function require_userns_tools() {
34- if command -v become-root > /dev/null \
70+ # if command -v become-root >/dev/null \
71+ if command -v unshare > /dev/null \
3572 && command -v nsjail > /dev/null \
3673 && command -v fuse-overlayfs > /dev/null
3774 then
3875 return 0
3976 fi
40- warning " nsjail, fuse-overlayfs and become-root are necessary for rootless operation"
41- warning " https://github.com/giuseppe/become-root"
77+ warning " nsjail, fuse-overlayfs and unshare (util-linux) are necessary for rootless operation"
78+ # warning "nsjail, fuse-overlayfs and become-root are necessary for rootless operation"
79+ # warning "https://github.com/giuseppe/become-root"
4280 warning " https://github.com/containers/fuse-overlayfs"
4381 warning " https://github.com/google/nsjail"
4482 return 1
4583}
4684
4785function mountoverlay() {
4886 if (( rootless_userns)) ; then
49- fuse-overlayfs " $@ "
87+ ~ /Projekte/fuse-overlayfs/ fuse-overlayfs " $@ "
5088 else
5189 mount -t overlayfs overlayfs " $@ "
5290 fi
@@ -61,7 +99,11 @@ function umountoverlay() {
6199
62100# Use a private gpg keyring
63101function gpg() {
102+ local res
64103 command gpg --homedir=" $BUILDDIRECTORY /gnupg" " $@ "
104+ res=$?
105+ gpgconf --homedir=" $BUILDDIRECTORY /gnupg" --kill gpg-agent
106+ return $res
65107}
66108
67109function init_gnupg() {
@@ -257,14 +299,19 @@ function init_chroot(){
257299 exec_container root pacman -R arch-install-scripts --noconfirm
258300 exec_container root locale-gen
259301
260- printf ' builduser ALL = NOPASSWD: /usr/bin/pacman\n' > " $BUILDDIRECTORY " /root/etc/sudoers.d/builduser-pacman
302+ printf ' %s\n\n' ' Defaults preserve_groups' \
303+ ' builduser ALL = NOPASSWD: /usr/bin/pacman' \
304+ > " $BUILDDIRECTORY " /root/etc/sudoers.d/builduser-pacman
261305 exec_container root useradd -m -G wheel -s /bin/bash -d /build builduser
262306 echo " keyserver-options auto-key-retrieve" | install -Dm644 /dev/stdin " $BUILDDIRECTORY /root" /build/.gnupg/gpg.conf
263307 exec_container root chown -R builduser /build/.gnupg
264308 else
265309 printf ' Server = %s\n' " $HOSTMIRROR " > " $BUILDDIRECTORY " /root/etc/pacman.d/mirrorlist
266310 exec_container root pacman -Syu --noconfirm
267311 fi
312+ exec_container root gpgconf --homedir=" /etc/pacman.d/gnupg" --kill gpg-agent
313+ # FIXME: Why is this necessary?
314+ rm -f " $BUILDDIRECTORY " /root/etc/pacman.d/gnupg/S.gpg-agent{,.browser,.extra,.ssh}
268315
269316 trap - ERR INT
270317}
@@ -390,6 +437,7 @@ Usage:
390437General Options:
391438 -h Print this help message
392439 -d Run diffoscope if packages are not reproducible
440+ -r Run without root privileges in nsjail containers
393441__END__
394442}
395443
@@ -420,6 +468,7 @@ while getopts :hdorC:P:M: arg; do
420468 d) run_diffoscope=1;;
421469 r) rootless_userns=1;
422470 require_userns_tools || exit 1
471+ become_rootless
423472 # TODO: better detection for valid writable build directory
424473 [[ $BUILDDIRECTORY == /var/lib/repro ]] && BUILDDIRECTORY=" ${XDG_CACHE_HOME:- $HOME / .cache} /archlinux-repro"
425474 ;;
0 commit comments