Script to mount nas-share using generated credentials (mount EC 13,32)

Heyas

At home i have 1 nas with 3 shares, of which i used to mount 2 of them using a script with hardcoded password and username in it.

EDIT: Turns out, its not the script, but 'how i access' the nas share.. (-o user=XY,password=... VS. -o credentials=...).

Figured about credential files, on a home computer, i tried to match this task into a script.
Idea is to provide all infos at the first time, like:

[sea@localhost ~]$ mount_nas 

mount_nas (0.1)
Usage:		mount_nas //IP/SHARE /mount/point [username password [domain]]
Example:	mount_nas //192.168.10.4/Public /home/sea/net/pub sea ABCDEFG localdomain

Once configured, simply call:
		mount_nas SHARENAME
		Where SHARENAME will be the credential filename, and was 'generated' from //IP/SHARENAME...

In practice, it mounts the public one, but wont the private one (error codes: 13 & 32).
The 2 handling scripts are these:
umount-nas:

LC_ALL=C
sudo umount /home/sea/priv/nas  /home/sea/net/pub
mount|grep 192

mnt-nas:

#!/bin/sh
export LC_ALL=C

source ~/.config/user-dirs.dirs
IP=192.168.10.110
USR=MYNAME
PW=THEPASSWORD
DOM=THEDOMAIN

# The creation and usage of the credentials work fine...
#mount_nas //$IP/Public $XDG_PUBLICSHARE_DIR $USR $PW $DOM
#mount_nas //$IP/priv $XDG_CLOUD_DIR $USR $PW $DOM
#exit

# Thus, use the benefit of the script
echo;echo; mount_nas Public
echo;echo; mount_nas priv

IP="";USR="";PW="";DOM=""

During the 'tests' both shares have the same password, but for some reason, one gets mounted the other not.

umount: /home/sea/priv/nas: nicht eingeh�ngt


Loaded settings: Public (//192.168.10.110/Public)
0 Mounted: /home/sea/net/pub (//192.168.10.110/Public)


Loaded settings: priv (//192.168.10.110/priv)
mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
32 Failed: /home/sea/priv/nas (//192.168.10.110/priv)

//192.168.10.110/Public on /home/sea/net/pub type cifs (rw,relatime,vers=1.0,cache=strict,username="MYUSERNAME",domain="MYDOMAIN",uid=0,noforceuid,gid=0,noforcegid,addr=192.168.10.110,unix,posixpaths,serverino,acl,rsize=1048576,wsize=65536,actimeo=1)

[sea@localhost ~]$ clear ; umnt-nas ; mnt-nas ; echo ; mount|grep 192 ; echo

The actual script is: mount_nas

#!/bin/sh
export LC_ALL=C
#
#	Author: 	sea (Simon Arjuna Erat)
#	Contact:	erat.simon@gmail.com
#	Created:	2014.17.11
#	Description:	If all required variables (2-3) are provided, it creates
#			a credentials file which it'll be using at 2nd usage,
#			if you just pass the sharename of an already used NAS entry.
#		NOTE:	There must be no identical names on diffrent NAS!
#
#	Variables
#
	ME=$(basename $0)
	source ~/.config/user-dirs.dirs
	script_version=0.1
	hlp_txt="\n$ME ($script_version)\nUsage:\t\t$ME //IP/SHARE /mount/point [username password [domain]]
\rExample:\t$ME //$(for STR in [a-z];do ip addr|awk '{print $2}'|grep ^$STR -A2|grep -iv ::|grep -iv 127|grep [0-9].[0-9].[0-9]|sed s,"/"," ",g|awk '{print $1}';done)/Public $XDG_PUBLICSHARE_DIR $USER ABCDEFG $(hostname|sed s,'\.',' ',g|awk '{print $2}')\n\nOnce configured, simply call:\n\t\t$ME SHARENAME\n\t\tWhere SHARENAME will be the credential filename, and was the full //IP/SHARENAME...\n"
	NAS_CFG_DIR="$HOME/.config/script-tools/nas"
	[ ! -d "$NAS_CFG_DIR" ] && mkdir -p "$NAS_CFG_DIR
	NAS="$1" ; MP="$2" ; CREDS=""
	
	
#
#	Arguments : pre-check
#
	
	[ -f "$NAS_CFG_DIR/$1" ] && doneConfig=true || doneConfig=false
#
#	Error messages
#	
	[ -z $1 ] && echo -e "$hlp_txt" && exit 1
	NAS_NAME="$(basename $1)"
	export this_cred="$NAS_CFG_DIR/$NAS_NAME"
	[ -z $2 ] && ( [ -f "$NAS_CFG_DIR/$1" ] && doneConfig=true || doneConfig=false )
	[ ! -z $3 ] && [ -z $4 ] && echo -e "$hlp_txt" && exit 1
	[ ! $doneConfig ] && [ -z $2 ] && echo -e "$hlp_txt" && exit 1
#
#	Arguments : prepare credentials
#
	if [ ! $doneConfig ]
	then	# Configuration is missing
		touch "$this_cred"
		printf "# NAS ($NAS) Credentials file, created by sea ($(date +'%Y-%m-%d'))\nusername=\"$3\"\npassword=\"$4\"\ndomain=\"$5\"\nadress=\"$1\"\nmountpoint=\"$2\"" > "$this_cred"
		export CREDS="-o credentials=$this_cred"
	elif [ $doneConfig ]
	then	# Configuration exists
		export CREDS="-o credentials=$this_cred"	
	else	# Use plain text, not credentials file
		[ ! -z $3 ] && CREDS="username=$3"
		[ ! -z $4 ] && CREDS="$CREDS,password=$4"
		[ ! -z $5 ] && CREDS="$CREDS,domain=$5"
		[ ! -z $4 ] && CREDS="-o $CREDS"
		export CREDS
	fi
	if echo $CREDS|grep credentials > /dev/zero
	then	printf "Loading settings: $1\r"
		source "$this_cred" && \
			NAS="$adress" && \
			MP="$mountpoint"
		echo "Loaded settings: $1 ($NAS)"
	fi
#
#	Action
#
 	if sudo mount -t cifs $NAS $MP $CREDS
	then 	echo "$? Mounted: $MP ($NAS)"
	else 	echo "$? Failed: $MP ($NAS)"
	fi
#
#	Reset variables
#
	MP="";CREDS="";NAS="";ANSWER="";NAS_NAME=""
	doneConfig="";this_cred=""
	export MP CREDS NAS ANSWER doneConfig this_cred NAS_NAME

Trying to mount the 'missing' share manualy and the regarding credential file is looking like:

[sea@localhost ~]$ CREDS="/path/to/creds";sudo mount //192.168.10.110/priv -t cifs /mnt/sysimage/ -o credentials="$CREDS" ; echo $? ; cat $CREDS
mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
32
# NAS (//192.168.10.110/priv) Credentials file, created by sea (2014-02-11)
username="simon"
password="PASSWORD"
domain="DOMAIN"
adress="//192.168.10.110/priv"
mountpoint="/home/sea/priv/nas"
[sea@localhost ~]$ sudo mount //192.168.10.110/priv -t cifs /mnt/sysimage/ -o credentials="$CREDS" ; echo $? ; cat $CREDS

Any ideas apreciated.
Thank you in advance

  • Simon

---------- Post updated at 10:28 ---------- Previous update was at 06:35 ----------

Just to be sure i'm trying to mount an existing share...

[sea@localhost net]$ smbclient -L 192.168.10.110 -N
Domain=[OCEAN] OS=[Unix] Server=[Samba 3.5.6]

	Sharename       Type      Comment
	---------       ----      -------
	IPC$            IPC       IPC Service (Terra Nova)
	Public          Disk      
	priv            Disk      
	TimeMachine     Disk      
Domain=[OCEAN] OS=[Unix] Server=[Samba 3.5.6]

But its kidding with me...

sudo mount -t cifs //192.168.10.110/priv /mnt/nas/ -o user=USERNAME,password=PASSWORD,domain=DOMAIN

Works like a charm, while

sudo mount -t cifs //192.168.10.110/priv /mnt/nas/ -o credentials="/home/sea/.config/script-tools/nas/priv"

results in the 'plain and known error':

mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

How the credentials file looks like is shown in the first post...
Of couse, password, username and domain are verified.

Anything i missed?
Any ideas please?

---------- Post updated at 11:15 ---------- Previous update was at 10:28 ----------

Also tried:
chmod [644|400] /path/of/cred

As well as:
...,sec=[ntlmssp|ntlmv2,ntlm]

Digging deeper than google's 2nd page, reading the repeating solutions (if even) is quite disencouraging.

EDIT:

dmesg|tail
[37395.259096] SELinux: initialized (dev cifs, type cifs), uses genfs_contexts
[37395.368390] CIFS VFS: cifs_mount failed w/return code = -13

EDIT2:

[sea@localhost nas]$ mnt-nas 


0 Loaded settings: Public (//192.168.10.110/Public)
Credential formatted incorrectly: (null)
domain="ocean"
Credential formatted incorrectly: "//192.168.10.110/Public"
Credential formatted incorrectly: "/home/sea/net/pub"
mount.cifs kernel mount options: ip=192.168.10.110,unc=\\192.168.10.110\Public,iocharset=utf8,file_mode=0777,dir_mode=0777,sec=ntlm,user="simon",,domain="ocean",pass=********
0 Mounted: /home/sea/net/pub (//192.168.10.110/Public)


0 Loaded settings: priv (//192.168.10.110/priv)
domain="ocean"
Credential formatted incorrectly: //192.168.10.110/priv
Credential formatted incorrectly: /home/sea/priv/nas
mount.cifs kernel mount options: ip=192.168.10.110,unc=\\192.168.10.110\priv,iocharset=utf8,file_mode=0777,dir_mode=0777,sec=ntlm,user="simon",,domain="ocean",pass=********
mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
32 Failed: /home/sea/priv/nas (//192.168.10.110/priv)

I really dont get it :frowning:
Both shares share the same uid/pw combination, they just differ by the Sharename.
Ok, the credential file is formatted incorrectly.. both times, but only one doesnt work.

If both would not work, i'd sure say its because i stored the full-adress (//IP/sharename) as well as the mountpoint (/home/sea/priv/nas).
But since 1 of 2 work, me back at the beginning... :clueless: