Debian Codegraph @

test/gvfs-testbed

raw
1 #!/bin/bash
2 # Build an "unshared tmpfs" sandbox for gvfs-test to safely run tests which
3 # need root privileges.
4 #
5 # (C) 2012-2013 Canonical Ltd.
6 # Author: Martin Pitt <[email protected]>
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2 of the License, or (at your option) any later version.
12
13 set -e
14 if [ "`id -u`" != 0 ]; then
15 echo "Error: this test suite wrapper needs to be called as root" >&2
16 exit 1
17 fi
18
19 if ! type smbd >/dev/null 2>&1; then
20 echo "Error: this test suite wrapper needs samba installed" >&2
21 exit 1
22 fi
23
24 # find out the user who calls us
25 pid=$$
26 while [ "`stat -c '%u' /proc/$pid`" = "0" ]; do
27 pid=`awk '/^PPid:/ {print $2}' /proc/$pid/status`
28 if [ -z "$pid" -o "$pid" = "1" ]; then
29 echo "Error: Did not find a parent process that runs as non-root" >&2
30 exit 1
31 fi
32 done
33 CALLING_UID="`stat -c '%u' /proc/$pid`"
34 CALLING_USER="`stat -c '%U' /proc/$pid`"
35
36 # sanity check
37 [ "$CALLING_UID" -gt 0 ] && [ -n "$CALLING_USER" ]
38 CALLING_GROUP="`id -gn $CALLING_USER`"
39
40 # find udisks daemon
41 UDISKSD=`sed -n '/^Exec=/ { s/^[^=]*=//; p }' /usr/share/dbus-1/system-services/*UDisks2*.service | cut -f1 -d' '`
42 if [ ! -x "$UDISKSD" ]; then
43 echo "Error: Did not find udisksd path" >&2
44 exit 1
45 fi
46
47 # smbd needs to be restarted in the sandbox
48 (service smbd stop || service smb stop) && smbd_running=1 || :
49 (service nmbd stop || service nmb stop) && nmbd_running=1 || :
50
51 MNT=`mktemp -d`
52
53 # work around scsi_debug not implementing CD-ROM SCSI commands
54 # see https://launchpad.net/bugs/1043182 for details
55 if [ -d /run/udev/ -a ! -e /run/udev/rules.d/60-persistent-storage-scsi_debug.rules ]; then
56 mkdir -p /run/udev/rules.d
57 cat <<EOF > /run/udev/rules.d/60-persistent-storage-scsi_debug.rules
58 KERNEL=="sr*", ENV{DISK_EJECT_REQUEST}!="?*", ATTRS{model}=="scsi_debug*", ENV{ID_CDROM_MEDIA}=="?*", IMPORT{program}="/sbin/blkid -o udev -p -u noraid \$tempnode"
59 EOF
60 sync
61 pkill -HUP udevd || pkill -HUP systemd-udevd
62 fi
63
64 # prevent nautilus popups for temporary drives in running sessions
65 pkill -STOP -f gvfs-udisks2-volume-monitor || :
66
67 cat <<EOF | unshare -m sh
68 set -e
69 mount --make-rprivate /
70 mount -n -t tmpfs tmpfs $MNT
71
72 # prepare overlay directories and copy essential configuration
73 mkdir -p $MNT/etc/samba $MNT/var/lib/samba/private $MNT/var/cache/samba $MNT/var/log/samba $MNT/home/$CALLING_USER/run $MNT/run_samba $MNT/media
74 touch $MNT/etc/fstab $MNT/home/gvfs_sandbox_marker
75 cp -a /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/pam* /etc/nsswitch.conf /etc/security/ /etc/init.d /etc/systemd /etc/login.defs /etc/dbus-1 /etc/polkit-1 $MNT/etc/
76 if [ -d /etc/selinux ]; then
77 cp -a /etc/selinux $MNT/etc/
78 fi
79 if [ -d /etc/init ]; then
80 cp -a /etc/init $MNT/etc/
81 fi
82 chown -R $CALLING_USER:$CALLING_GROUP $MNT/home/$CALLING_USER
83 # ensure we can resolve our hostname
84 echo "127.0.0.1 `uname -n`" >> $MNT/etc/hosts
85
86 # copy our local mock polkitd into testbed
87 cp `dirname $0`/test_polkitd.py $MNT/home/
88
89 # Debianisms
90 if [ -d /etc/alternatives ]; then
91 cp -a /etc/alternatives $MNT/etc/
92 fi
93 if [ -L /var/run ]; then
94 cp -a /var/run $MNT/var
95 fi
96
97 # if we run a script, we need to copy it into the sandbox as it might be in
98 # a directory that we overlay
99 if [ -f "$1" ]; then
100 cp -a "$1" $MNT/home/gvfs-testbed-script
101 ARGS="/home/gvfs-testbed-script ${@:2}"
102
103 # we need to copy our test files as well, if we run gvfs-test
104 if [ -d "`dirname $1`/files" ]; then
105 cp -a "`dirname $1`/files" $MNT/home
106 fi
107 else
108 ARGS="$@"
109 fi
110
111 # realize our overlays
112 mount -n --bind $MNT/etc/ /etc/
113 mount -n --bind $MNT/home/ /home/
114 mount -n --bind $MNT/var/ /var/
115 mount -n --bind $MNT/media/ /media
116 mkdir -p /run/samba
117 mount -n --bind $MNT/run_samba/ /run/samba
118
119 # run Samba with local configuration/state
120 cat <<SMBEOF >/etc/samba/smb.conf
121 [global]
122 workgroup = TESTGROUP
123 interfaces = 127.0.0.0/8
124 map to guest = Bad User
125
126 [public]
127 path = /home/$CALLING_USER/public
128 guest ok = yes
129
130 [private]
131 path = /home/$CALLING_USER/private
132 read only = no
133 SMBEOF
134 nmbd -D -l /var/log/samba
135 smbd -D -l /var/log/samba
136
137 # we need a predictable password for the smb:// authenticated test, so change
138 # it to "foo" in the sandbox
139 /bin/echo -e 'foo\\nfoo\\n' | smbpasswd -a $CALLING_USER -s
140
141 # set up SSH key for test user
142 su -lc "mkdir ~/.ssh; ssh-keygen -q -f ~/.ssh/id_rsa -N ''" $CALLING_USER
143
144 # create a root shell that the user can call to control scsi_debug and
145 # similar
146 cp /bin/sh /home/$CALLING_USER/rootsh
147 chown root:$CALLING_USER /home/$CALLING_USER/rootsh
148 chmod 4550 /home/$CALLING_USER/rootsh
149
150 # we must start udisksd in our private mount environment, so that gvfs and
151 # udisks agree to the same view of mounts
152 $UDISKSD --replace &
153 UDISKS_PID=\$!
154
155 echo "Running commmand in testbed: \$ARGS"
156 su -lc "export PATH=$PATH; export XDG_RUNTIME_DIR=/home/$CALLING_USER/run; \$ARGS" $CALLING_USER || {
157 RC=\$?
158 echo "=== command failed, showing Samba log files ==="
159 for f in /var/log/samba/log.*; do
160 echo "--- \$f ---"
161 cat \$f
162 done
163 }
164 (cat /var/run/samba/*.pid | xargs kill ) || ( cat /var/run/[sn]mbd.pid | xargs kill )
165 kill \$UDISKS_PID || :
166 exit \$RC
167 EOF
168 RC=$?
169
170 pkill -CONT -f gvfs-udisks2-volume-monitor || :
171
172 [ -n "$smbd_running" ] && service smbd start || service smb start || :
173 [ -n "$nmbd_running" ] && service nmbd start || service nmb start || :
174
175 rmdir "$MNT" || :
176 exit $RC