• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/samba/packaging/PHT/TurboLinux/
1#!/bin/sh 
2# name:			mount.smb  --  interface between mount and smbmount
3# author:		Ch. L. Spiel (cspiel@physik.tu-muenchen.de)
4
5myname=`basename $0`
6passwd_filename="smb-pass"		# name of user smb-password file
7lock_file="/var/lock/$myname"
8log_file="/tmp/mount.smb.log"
9
10# check for an existing lock-file quickly(!)
11if [ -e "$lock_file" ]; then
12	# exit, but don�t touch lock-file
13	exit 0
14fi
15# set up new lock-file
16echo > $lock_file
17
18# initialise log-file
19echo "logging of $myname started at `date`" > $log_file
20chmod --silent 600 $log_file
21echo "called with: $@" >> $log_file
22exec >> $log_file 2>&1
23
24
25
26# set default and initial values
27verbose=false				# be silent
28fake=false				# really do the mount
29fmode="-f 600"				# default file mode
30dmode="-d 700"				# default dir mode
31
32#uid="-u `id | sed 's/^uid=\([0-9]*\).*$/\1/'`"
33uid="-u 0"
34#gid="-g `id | sed 's/^.*gid=\([0-9]*\).*$/\1/'`"
35gid="-g 0"
36
37
38#
39# functions
40#
41
42# exitproc(int exit_code)
43function exit_proc
44{
45	if [ -n "$lock_file" ]; then
46		# remove current lock-file
47		rm "$lock_file"
48	fi
49	# update log-file
50	echo "" >> $log_file
51	echo "$myname�s return value is $1." >> $log_file
52	echo "logging of $myname ended at `date`." >> $log_file
53	# done.
54	exit $1
55}
56
57
58# split_arg(arg)
59# arg ::= id '=' val
60# set id and val on return
61function split_arg
62{
63	id="$1"
64	val="$2"
65	extra="$3"
66} # end of split_arg
67
68
69# split_passwdline(uline)
70function split_passwdline
71{
72	user_name=$1
73	real_password=$2
74	user_id=$3
75	group_id=$4
76	full_name=$5
77	home_dir=$6
78	shell_name=$7
79}
80
81
82# get_homedir(username)
83function get_homedir
84{
85	local temp_ifs
86
87	temp_ifs="$IFS"
88	uline=`grep "^$1" /etc/passwd`
89	if [ -z "$uline" ]; then
90		echo "$myname: unknown user \"$1\""
91		exit_proc 1
92	fi
93	IFS=":"
94	split_passwdline $uline
95	if [ -z "$home_dir" ]; then
96		echo "$myname: user \"$1\" has no home directory"
97		exit_proc 1
98	fi
99	echo "$home_dir"
100	IFS="$temp_ifs"
101}
102
103
104# get_uid(username)
105function get_uid
106{
107	local temp_ifs
108
109	temp_ifs="$IFS"
110	uline=`grep "^$1" /etc/passwd`
111	if [ -z "$uline" ]; then
112		echo "$myname: unknown user \"$1\""
113		exit_proc 1
114	fi
115	IFS=":"
116	split_passwdline $uline
117	echo "$user_id"
118	IFS="$temp_ifs"
119}
120
121
122# get_gid(username)
123function get_gid
124{
125	local temp_ifs
126
127	temp_ifs="$IFS"
128	uline=`grep "^$1" /etc/passwd`
129	if [ -z "$uline" ]; then
130		echo "$myname: unknown user \"$1\""
131		exit_proc 1
132	fi
133	IFS=":"
134	split_passwdline $uline
135	echo "$group_id"
136	IFS="$temp_ifs"
137}
138
139
140# read_passwd_file(sharename)
141function read_passwd_file
142{
143	local pwd_filename pwd_entry temp_ifs share_name fmod
144
145	pwd_filename=`get_homedir $uuname`/$passwd_filename
146	# use uid and gid of user�s /etc/password entry
147	uid="-u `get_uid $uuname`"
148	gid="-g `get_gid $uuname`"
149	# check existence of password file
150	if [ ! -f "$pwd_filename" -o ! -r "$pwd_filename" ]; then
151		echo "$myname: cannot read from user password file \"$pwd_filename\""
152		exit_proc 1
153	fi
154	# check file permissions
155	for f in $pwd_filename{,~,%,.BAK,.bak,.new,.old,.orig,.sav}; do
156		if [ ! -f $f ]; then continue; fi
157		/bin/ls -l $f | grep -q -- "^-r\(w\|-\)------"
158		if [ $? = 1 ]; then
159			echo "$myname: Found security hole: mode of file \"$f\""
160			echo "$myname: Password file must have permission 400 or 600."
161			echo "$myname: Please fix the file�s mode."
162			exit_proc 1
163		fi
164	done
165
166	share_name="$1"		# sharename in smb-format!
167	pwd_entry=`grep -v '^#' "$pwd_filename" | grep -i "^$share_name"`
168	if [ -z "$pwd_entry" ]; then
169		# try uni*-like sharename
170		share_name=`echo $share_name | sed -e 's,^//,,' -e 's,/,:/,'`
171		pwd_entry=`grep -v '^#' "$pwd_filename" | grep -i "^$share_name"`
172	fi
173	if [ -z "$pwd_entry" ]; then
174		# sharename was not found in user�s password file
175		echo "$myname: cannot authentify share named \"$1\" via file \"$pwd_filename\""
176		exit_proc 1
177	fi
178	
179	# pwd_entry has the form:
180	# sharename username password
181	temp_ifs="$IFS"
182	IFS="	 "		# <tab> and <space>
183	split_arg $pwd_entry
184	options="$options -U $val"
185	password="$extra"
186	IFS="$temp_ifs"
187}
188
189
190# process_options(opt1, opt2, ..., optN)
191function process_options
192{
193	local temp_ifs
194
195	for j; do
196		temp_ifs="$IFS"	# save current internal-field separator
197		IFS="="		# set new separator
198		split_arg $j	# split argument into identifier and value
199		IFS="$temp_ifs"	# reset old separator
200		case "$id" in
201			port)
202				options="$options -p $val"
203				;;
204			debug)
205				options="$options -d $val"
206				;;
207			log)
208				options="$options -l $val"
209				;;
210			nbname)
211				options="$options -n $val"
212				;;
213			nopwd)
214				options="$options -N"
215				;;
216			maxproto)
217				options="$options -m $val"
218				;;
219			ip)
220				options="$options -I $val"
221				;;
222			uname)
223				options="$options -U $val"
224				;;
225			wrkgrp)
226				options="$options -W $val"
227				;;
228			term)
229				options="$options -t $val"
230				;;
231			sdir)
232				options="$options -D $val"
233				;;
234			pwd)
235				# DO NOT USE THIS OPTION!  It is a severe scurity hole.
236				password="$val"
237				;;
238			uuname)
239				# consult user�s smb-password file
240				uuname="$val"	# uni* user name
241				read_passwd_file "$server_service"
242				;;
243				
244			# ignored options
245			async)
246				# do nothing
247				;;
248			atime)
249				# do nothing
250				;;
251			auto)
252				# do nothing
253				;;
254			defaults)
255				# do nothing
256				;;
257			dev)
258				# do nothing
259				;;
260			exec)
261				# do nothing
262				;;
263			noatime)
264				# do nothing
265				;;
266			noauto)
267				# do nothing
268				;;
269			nodev)
270				# do nothing
271				;;
272			noexec)
273				# do nothing
274				;;
275			nosuid)
276				# do nothing
277				;;
278			nouser)
279				# do nothing
280				;;
281			ro)
282				# do nothing
283				;;
284			rw)
285				# do nothing
286				;;
287			suid)
288				# do nothing
289				;;
290			sync)
291				# do nothing
292				;;
293			user)
294				# do nothing
295				;;
296				
297			# fs options
298			fmod)
299				fmode="-f $val"
300				;;
301			dmod)
302				dmode="-d $val"
303				;;
304			uid)
305			        uid="-u $val"
306				;;
307			gid)
308				gid="-g $val"
309				;;
310			
311			# fallthrough
312			*)
313				echo "$myname: unrecognized option $id"
314				exit_proc 1
315				;;
316		esac
317	done
318} # end of split_options
319
320
321
322#
323# main
324#
325
326
327
328if [ "$verbose" != "false" ]; then
329	# show how we have been called
330	echo "$myname: $*"
331fi
332
333# some checks of the input parameters
334if [ "$#" -lt 2 ]; then
335	echo "$myname: need at least service and mountpoint"
336	exit_proc 1
337fi
338
339if `echo "$2" | grep -vq "^/"`; then
340	echo "$myname: mount point must be an absolut path"
341	exit_proc 1
342fi
343
344
345# copy arguments
346if `echo "$1" | grep -q ":/"`; then
347	# non--standard format, i.e., server:/service
348	server_service=`echo "//$1" | sed -e "sx:/x/x"`
349else
350	# standard format, i.e, //server/service
351	server_service="$1"
352fi
353mntpt="$2"
354
355# copy options
356shift 2		# skip arguments: //server/service and /mnt-point
357for i; do
358	case "$i" in
359		-f | --fake)
360			fake=true
361			;;
362		-h | --help)
363			echo "usage: mount.smb service [password] mountpoint [options]"
364			exit_proc 0
365			;;
366		-v | --verbose)
367			verbose=true
368			;;
369		-V | --version)
370		        echo "$myname: mount.smb-0.1.0"
371			exit_proc 0
372			;;
373		-o)
374			shift			# skip leading -o
375			temp_ifs="$IFS"		# save current internal-field separator
376			IFS=","			# set new separator
377			process_options $*
378			IFS="$temp_ifs"		# reset old separator
379			break			# mount places options at the end -> we are done
380			;;
381		*)
382			echo "$myname: unrecognized option $i"
383			exit_proc 1
384			;;
385	esac
386	shift
387done
388IFS=' '
389
390
391#
392# be careful...
393#
394
395
396# nmblookup server: is node up and running?
397srv=`echo $server_service | sed 's,^//\(.*\)/.*$,\1,'`	# server�s name
398nmblookup "$srv" | grep -q "failed to find name"
399if [ "$?" = 0 ]; then
400	echo "$myname: failed to find server \"$srv\"."
401	exit_proc 1
402fi
403
404
405#
406# perform mount
407#
408
409
410fs_options="$fmode $dmode $uid $gid"	# all options concerning the mounted fs
411if [ "$verbose" = "true" ]; then
412	# display what we would do.  Do not show the password, only show "xxx".
413	echo -n "smbmount $server_service "
414	if [ -n "$password" ]; then	# password is set
415		echo -n "xxx "		# ... but we don�t show it ;-)
416	fi
417	echo "-c \"mount $mntpt $fs_options\" $options"
418#else
419	# supress further messages
420#	exec > /dev/null 2>&1
421#:
422fi
423	
424if [ "$fake" != "true" ]; then
425	smbmount $server_service $password -c "mount $mntpt $fs_options" $options
426	echo "smbmount�s exit code was $?."
427fi
428
429# clean up and exit
430exit_proc 0
431
432