Debian Codegraph @

debian/tests/smb-mount

raw
1 #!/bin/sh
2
3 set -x
4
5 add_smb_share() {
6 local share="$1"
7 local sharepath="$2"
8 local public="$3"
9
10 if ! testparm -s 2>&1 | grep -qE "^\[${share}\]"; then
11 echo "Adding [${share}] share"
12 cat >> /etc/samba/smb.conf <<EOFEOF
13 [${share}]
14 path = ${sharepath}
15 read only = no
16 EOFEOF
17 if [ "${public}" = "yes" ]; then
18 cat >> /etc/samba/smb.conf <<EOFEOF
19 guest ok = yes
20 EOFEOF
21 else
22 cat >> /etc/samba/smb.conf <<EOFEOF
23 guest ok = no
24 EOFEOF
25 fi
26 systemctl reload smbd.service
27 else
28 echo "No need to add private [${share}] share, continuing."
29 fi
30 }
31
32 create_user() {
33 local username="$1"
34 local password="$2"
35
36 useradd -m "$username"
37 echo "Setting samba password for the ${username} user"
38 echo "${password}\n${password}" | smbpasswd -s -a ${username}
39 }
40
41
42 now=$(date --utc)
43 result=0
44 # Remount everything in case someone is running this interactively
45 # and mounts from previous runs and previous usernames are still lingering
46 systemctl restart autofs
47
48 echo "Setting up a public samba share in /pub"
49 mkdir -p /pub
50 echo "${now} - This is the public samba share." > /pub/hello-public.txt
51 add_smb_share pub /pub yes
52
53 echo "Setting up a private samba share in /private"
54 mkdir -p /private
55 echo "${now} - This is the private samba share." > /private/hello-private.txt
56 add_smb_share private /private no
57
58 username="smbtest$$"
59 password="$$"
60 echo "Creating a local test user called ${username}"
61 create_user "${username}" "${password}"
62
63 echo "Setting up autofs credentials for the private share"
64 mkdir -m 0700 -p /etc/creds
65 cat > /etc/creds/localhost <<EOFEOF
66 username=${username}
67 password=${password}
68 domain=WORKGROUP
69 EOFEOF
70 chmod 0600 /etc/creds/localhost
71
72 if ! grep -qE "^/cifs" /etc/auto.master; then
73 echo "Configuring autofs for the /cifs mountpoint"
74 echo "/cifs /etc/auto.smb --timeout=180" >> /etc/auto.master
75 systemctl restart autofs
76 fi
77
78 # We use a trick here. By storing the credentials in /etc/creds/localhost,
79 # that means the autofs key is "localhost", and the credentials will be used
80 # when accessing /cifs/localhost.
81 # If instead we access /cifs/127.0.0.1, since there is no corresponding entry in
82 # /etc/creds, that access will be as a guest, i.e., with no credentials
83 echo "Testing authenticated share automount"
84 timeout 30s grep -qE "${now}.*private" /cifs/localhost/private/hello-private.txt || result=$?
85 if [ "$result" -ne "0" ]; then
86 echo "test failed"
87 exit ${result}
88 fi
89
90 echo "Confirming with smbstatus that an authenticated connection was used"
91 output=$(smbstatus 2>&1)
92 echo "${output}" | grep -q "${username}" || result=$?
93 if [ "${result}" -ne "0" ]; then
94 echo "test failed"
95 echo "smbstatus output:"
96 echo "${smbstatus}"
97 exit "${result}"
98 fi
99
100 echo "Confirming with mount -t cifs that the filesystem is mounted with authentication"
101 output=$(mount -t cifs 2>&1)
102 echo "${output}" | grep -qE "on /cifs/localhost/private.*username=${username}" || result=$?
103 if [ "${result}" -ne "0" ]; then
104 echo "test failed"
105 echo "mount -t cifs output:"
106 echo "${output}"
107 exit ${result}
108 fi
109
110 echo "Testing unauthenticated share automount"
111 timeout 30s grep -q "${now}.*public" /cifs/127.0.0.1/pub/hello-public.txt || result=$?
112 if [ "$result" -ne "0" ]; then
113 echo "test failed"
114 exit ${result}
115 fi
116
117 echo "Confirming with smbstatus that a guest connection was used"
118 output=$(smbstatus 2>&1)
119 echo "${output}" | grep -q nobody || result=$?
120 if [ "${result}" -ne "0" ]; then
121 echo "test failed"
122 echo "smbstatus output:"
123 echo "${smbstatus}"
124 exit ${result}
125 fi
126
127 echo "Confirming with mount -t cifs that the filesystem is mounted without authentication"
128 output=$(mount -t cifs 2>&1)
129 echo "${output}" | grep -qE "on /cifs/127.0.0.1/pub.*sec=none" || result=$?
130 if [ "${result}" -ne "0" ]; then
131 echo "test failed"
132 echo "mount -t cifs output:"
133 echo "${output}"
134 exit ${result}
135 fi