| 192 |
- |
1 |
#!/bin/bash
|
|
|
2 |
|
|
|
3 |
# This file must be executable to work! chmod 755!
|
|
|
4 |
|
|
|
5 |
# Automagically mount CIFS shares in the network, similar to
|
|
|
6 |
# what autofs -hosts does for NFS.
|
|
|
7 |
|
|
|
8 |
# Put a line like the following in /etc/auto.master:
|
|
|
9 |
# /cifs /etc/auto.smb --timeout=300
|
|
|
10 |
# You'll be able to access Windows and Samba shares in your network
|
|
|
11 |
# under /cifs/host.domain/share
|
|
|
12 |
|
|
|
13 |
# "smbclient -L" is used to obtain a list of shares from the given host.
|
|
|
14 |
# In some environments, this requires valid credentials.
|
|
|
15 |
|
|
|
16 |
# This script knows 2 methods to obtain credentials:
|
|
|
17 |
# 1) if a credentials file (see mount.cifs(8)) is present
|
|
|
18 |
# under /etc/creds/$key, use it.
|
|
|
19 |
# 2) Otherwise, try to find a usable kerberos credentials cache
|
|
|
20 |
# for the uid of the user that was first to trigger the mount
|
|
|
21 |
# and use that.
|
|
|
22 |
# If both methods fail, the script will try to obtain the list
|
|
|
23 |
# of shares anonymously.
|
|
|
24 |
|
|
|
25 |
get_krb5_cache() {
|
|
|
26 |
cache=
|
|
|
27 |
uid=${UID}
|
|
|
28 |
for x in $(ls -d /run/user/$uid/krb5cc_* 2>/dev/null); do
|
|
|
29 |
if [ -d "$x" ] && klist -s DIR:"$x"; then
|
|
|
30 |
cache=DIR:$x
|
|
|
31 |
return
|
|
|
32 |
fi
|
|
|
33 |
done
|
|
|
34 |
if [ -f /tmp/krb5cc_$uid ] && klist -s /tmp/krb5cc_$uid; then
|
|
|
35 |
cache=/tmp/krb5cc_$uid
|
|
|
36 |
return
|
|
|
37 |
fi
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
key="$1"
|
|
|
41 |
opts="-fstype=cifs"
|
|
|
42 |
|
|
|
43 |
for P in /bin /sbin /usr/bin /usr/sbin
|
|
|
44 |
do
|
|
|
45 |
if [ -x $P/smbclient ]
|
|
|
46 |
then
|
|
|
47 |
SMBCLIENT=$P/smbclient
|
|
|
48 |
break
|
|
|
49 |
fi
|
|
|
50 |
done
|
|
|
51 |
|
|
|
52 |
[ -x $SMBCLIENT ] || exit 1
|
|
|
53 |
|
|
|
54 |
creds=/etc/creds/$key
|
|
|
55 |
if [ -f "$creds" ]; then
|
|
|
56 |
opts="$opts"',uid=$UID,gid=$GID,credentials='"$creds"
|
|
|
57 |
smbopts="-A $creds"
|
|
|
58 |
else
|
|
|
59 |
get_krb5_cache
|
|
|
60 |
if [ -n "$cache" ]; then
|
|
|
61 |
opts="$opts"',multiuser,cruid=$UID,sec=krb5i'
|
|
|
62 |
smbopts="-k"
|
|
|
63 |
export KRB5CCNAME=$cache
|
|
|
64 |
else
|
|
|
65 |
opts="$opts"',guest'
|
|
|
66 |
smbopts="-N"
|
|
|
67 |
fi
|
|
|
68 |
fi
|
|
|
69 |
|
|
|
70 |
$SMBCLIENT $smbopts -gL "$key" 2>/dev/null| awk -v "key=$key" -v "opts=$opts" -F '|' -- '
|
|
|
71 |
BEGIN { ORS=""; first=1 }
|
|
|
72 |
/Disk/ {
|
|
|
73 |
if (first)
|
|
|
74 |
print opts; first=0
|
|
|
75 |
dir = $2
|
|
|
76 |
loc = $2
|
|
|
77 |
# Enclose mount dir and location in quotes
|
|
|
78 |
# Double quote "$" in location as it is special
|
|
|
79 |
gsub(/\$$/, "\\$", loc);
|
|
|
80 |
gsub(/\&/,"\\\\&",loc)
|
|
|
81 |
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
|
|
|
82 |
}
|
|
|
83 |
END { if (!first) print "\n"; else exit 1 }
|
|
|
84 |
'
|
|
|
85 |
|