Blame samples/auto.smb

Packit 8480eb
#!/bin/bash
Packit 8480eb
Packit 8480eb
# This file must be executable to work! chmod 755!
Packit 8480eb
Packit 8480eb
# Automagically mount CIFS shares in the network, similar to
Packit 8480eb
# what autofs -hosts does for NFS.
Packit 8480eb
Packit 8480eb
# Put a line like the following in /etc/auto.master:
Packit 8480eb
# /cifs  /etc/auto.smb --timeout=300
Packit 8480eb
# You'll be able to access Windows and Samba shares in your network
Packit 8480eb
# under /cifs/host.domain/share
Packit 8480eb
Packit 8480eb
# "smbclient -L" is used to obtain a list of shares from the given host.
Packit 8480eb
# In some environments, this requires valid credentials.
Packit 8480eb
Packit 8480eb
# This script knows 2 methods to obtain credentials:
Packit 8480eb
# 1) if a credentials file (see mount.cifs(8)) is present
Packit 8480eb
#    under /etc/creds/$key, use it.
Packit 8480eb
# 2) Otherwise, try to find a usable kerberos credentials cache
Packit 8480eb
#    for the uid of the user that was first to trigger the mount
Packit 8480eb
#    and use that.
Packit 8480eb
# If both methods fail, the script will try to obtain the list
Packit 8480eb
# of shares anonymously.
Packit 8480eb
Packit 8480eb
get_krb5_cache() {
Packit 8480eb
    cache=
Packit 8480eb
    uid=${UID}
Packit 8480eb
    for x in $(ls -d /run/user/$uid/krb5cc_* 2>/dev/null); do
Packit 8480eb
        if [ -d "$x" ] && klist -s DIR:"$x"; then
Packit 8480eb
	    cache=DIR:$x
Packit 8480eb
            return
Packit 8480eb
        fi
Packit 8480eb
    done
Packit 8480eb
    if [ -f /tmp/krb5cc_$uid ] && klist -s /tmp/krb5cc_$uid; then
Packit 8480eb
	    cache=/tmp/krb5cc_$uid
Packit 8480eb
	    return
Packit 8480eb
    fi
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
key="$1"
Packit 8480eb
opts="-fstype=cifs"
Packit 8480eb
Packit 8480eb
for P in /bin /sbin /usr/bin /usr/sbin
Packit 8480eb
do
Packit 8480eb
	if [ -x $P/smbclient ]
Packit 8480eb
	then
Packit 8480eb
		SMBCLIENT=$P/smbclient
Packit 8480eb
		break
Packit 8480eb
	fi
Packit 8480eb
done
Packit 8480eb
Packit 8480eb
[ -x $SMBCLIENT ] || exit 1
Packit 8480eb
Packit 8480eb
creds=/etc/creds/$key
Packit 8480eb
if [ -f "$creds" ]; then
Packit 8480eb
    opts="$opts"',uid=$UID,gid=$GID,credentials='"$creds"
Packit 8480eb
    smbopts="-A $creds"
Packit 8480eb
else
Packit 8480eb
    get_krb5_cache
Packit 8480eb
    if [ -n "$cache" ]; then
Packit 8480eb
        opts="$opts"',multiuser,cruid=$UID,sec=krb5i'
Packit 8480eb
        smbopts="-k"
Packit 8480eb
        export KRB5CCNAME=$cache
Packit 8480eb
    else
Packit 8480eb
        opts="$opts"',guest'
Packit 8480eb
        smbopts="-N"
Packit 8480eb
    fi
Packit 8480eb
fi
Packit 8480eb
Packit 8480eb
$SMBCLIENT $smbopts -gL "$key" 2>/dev/null| awk -v "key=$key" -v "opts=$opts" -F '|' -- '
Packit 8480eb
	BEGIN	{ ORS=""; first=1 }
Packit 8480eb
	/Disk/	{
Packit 8480eb
		  if (first)
Packit 8480eb
			print opts; first=0
Packit 8480eb
		  dir = $2
Packit 8480eb
		  loc = $2
Packit 8480eb
		  # Enclose mount dir and location in quotes
Packit 8480eb
		  # Double quote "$" in location as it is special
Packit 8480eb
		  gsub(/\$$/, "\\$", loc);
Packit 8480eb
		  gsub(/\&/,"\\\\&",loc)
Packit 8480eb
		  print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
Packit 8480eb
		}
Packit 8480eb
	END 	{ if (!first) print "\n"; else exit 1 }
Packit 8480eb
	'
Packit 8480eb