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