fix build: update SOURCE0 consists of name and version macro
[pkgs/xorg/util/xorg-x11-xauth.git] / packaging / mkxauth
1 #!/bin/sh
2 #
3 # mkxauth: script to make per-user Xauthority database
4 # formerly 'newcookie' script; modified 18-Jul-1996 jim knoble
5 #
6 ########################################################################
7 # $Log: mkxauth,v $
8 # Revision 1.1  2004/03/10 20:21:41  mharris
9 # automated commit of xorg-x11-0.0.6.6-0.0.2004_03_09.0
10 #
11 # Revision 1.8mh  2004/02/23 mharris
12 # changed chown to use foo:bar instead of foo.bar as the latter has been
13 # deprecated.  This entry is added by hand as mkxauth isn't in CVS
14 #
15 # Revision 1.7  1996/10/23 21:34:23  jmknoble
16 # take path of least surprise if no command specified;
17 # if hostnames specified with -c, don't assume local host.
18 #
19 # Revision 1.6  1996/10/02 20:34:12  jmknoble
20 # updated help text again
21 #
22 # Revision 1.5  1996/10/02 20:10:03  jmknoble
23 # updated help text
24 #
25 # Revision 1.4  1996/10/02 20:03:26  jmknoble
26 # fixed quoting problem in key generation
27 #
28 # Revision 1.3  1996/08/20 16:31:30  jmknoble
29 # refined random key generation (using mcookie if available)
30 #
31 # Revision 1.2  1996/08/20 15:49:33  jmknoble
32 # replaced key generation using perl with method using md5sum
33 #
34 # Revision 1.1  1996/08/05 16:40:20  jmknoble
35 # Initial revision
36 #
37 ########################################################################
38
39 #set -x
40
41 ## default values for some variables
42 usr_umask=0077
43 # eventual exit status
44 sts=0
45 # verbose operation if blank
46 opt_vrbopr=''
47 # eventual string of non-option arguments
48 cmd_args=''
49 # filename for per-user Xauthority database
50 usrauth=.Xauthority
51 # username for whom to make per-user database
52 lclusr=`whoami`
53 # mode for making database; 
54 # valid values are 'create', 'merge-local', 
55 # 'merge-ftp', 'merge-rsh', 'merge-rzip',
56 # and 'none'
57 xauth_mode='none'
58 # actual path to target database
59 dstauth=''
60 # user to login as for rsh/rzip modes
61 rmtusr=`whoami`
62 # host to contact for remote Xauthority databases
63 rmthst=''
64 # local user to grab Xauthority from in merge mode
65 srcusr=''
66
67 ########################################################################
68 # help message
69 function prthlp() {
70     echo ""
71     echo "  usage:  $0 [-q] [-u <login>] -m <login>"
72     echo "          $0 [-q] [-u <login>] -f <host>"
73     echo "          $0 [-q] [-u <login>] -r <host> [-l <login>]"
74     echo "          $0 [-q] [-u <login>] -z <host> [-l <login>]"
75     echo "          $0 [-q] [-u <login>] -c [<host> [<host> ... ]]"
76     echo ""
77     echo "  create or update an Xauthority database containing authentication"
78     echo "  keys for the current user or a specified user on the local host."
79     echo ""
80     echo "  commands:"
81     echo ""
82     echo "  -m <login>    merge the Xauthority database from local user <login>"
83     echo "                (if readable) with the target .Xauthority"
84     echo ""
85     echo "  -f <host>     merge a remote Xauthority database with the target"
86     echo "                .Xauthority, using ncftp"
87     echo ""
88     echo "  -r <host>     merge a remote Xauthority database with the target"
89     echo "                .Xauthority, using rsh"
90     echo ""
91     echo "  -z <host>     merge a remote Xauthority database with the target"
92     echo "                .Xauthority, using rsh and gzip"
93     echo ""
94     echo "  -c <host>...  create a local Xauthority database, or add keys to an"
95     echo "                existing one, for all hosts listed (uses md5sum).  if"
96     echo "                no hosts are listed, assume the local host."
97     echo ""
98     echo "  options:"
99     echo ""
100     echo "  -q            quiet operation"
101     echo ""
102     echo "  -u <login>    create/merge .Xauthority for user <login>"
103     echo ""
104     echo "  -l <login>    for '-f', '-r' and '-z' modes, use <login> for the"
105     echo "                remote login"
106     echo ""
107
108     exit 0
109 }
110
111 # check that current user is root
112 function chkroot() {
113     if [ `whoami` != root ]; then
114         echo "sorry---you need to be root" "$*"
115         exit 1
116     fi
117 }
118
119 # write a message to stdout iff verbose mode on
120 function msg() {
121     if [ -z "$opt_vrbopr" ]; then
122         echo "$@"
123     fi
124 }
125
126 # check that a command exists
127 function chkcmdexs() {
128     for i in $*; do
129         if [ -z `type -p $i` ]; then
130             echo "`basename $0`: error: can't find command '$i'"
131             exit 1
132         fi
133     done
134 }
135
136 # check that a file exists, and create it if it doesn't
137 # *and* if we have write permissions to its parent dir
138 function chkfilexs() {
139     for i in $*; do
140         if [ ! -f "$i" ]; then
141             if [ -w `dirname $i` ]; then
142                 msg -n "creating file $i ... "
143                 touch $i
144                 msg "done"
145             fi
146         fi
147     done
148 }
149
150 # check if a file is readable
151 function redabl() {
152     local srcfil=$1
153     if [ -r "$srcfil" ]; then
154         sts=0
155     else
156         echo "`basename $0`: error: cannot read file $srcfil"
157         sts=1
158     fi
159     return $sts
160 }
161
162 # check if a file is writable
163 function wrtabl() {
164     local dstfil=$1
165     if [ -w "$dstfil" ]; then
166         sts=0
167     else
168         echo "`basename $0`: error: cannot write to file $dstfil"
169         sts=1
170     fi
171     return $sts
172 }
173
174 # set the correct ownership for a file
175 function givusr() {
176     local lststs=$1
177     local usrnam=$2
178     local dstfil=$3
179     if [ $lststs = 0 ]; then
180         chown $usrnam:$usrnam $dstfil
181         sts=0
182     else
183         msg ""
184         echo "`basename $0`: error writing to file $dstfil"
185         sts=1
186     fi
187     return $sts
188 }
189
190 ########################################################################
191 # set our umask so that no one else can read our files
192 umask $usr_umask
193
194 # test some command-line args
195 while [ "$*" ]; do
196     case $1 in
197         -h | --help)
198             shift
199             prthlp
200             ;;
201         -q | --quiet)
202             shift
203             opt_vrbopr='-q'
204             ;;
205         -u | --user)
206             shift
207             lclusr="$1"
208             shift
209             ;;
210         -l | --login)
211             shift
212             rmtusr="$1"
213             shift
214             ;;
215         -c | --create)
216             shift
217             xauth_mode='create'
218             ;;
219         -m | --merge)
220             shift
221             xauth_mode='merge-local'
222             srcusr="$1"
223             shift
224             ;;
225         -f | --ftp)
226             shift
227             xauth_mode='merge-ftp'
228             rmthst="$1"
229             shift
230             ;;
231         -r | --rsh)
232             shift
233             xauth_mode='merge-rsh'
234             rmthst="$1"
235             shift
236             ;;
237         -z | --rzip)
238             shift
239             xauth_mode='merge-rzip'
240             rmthst="$1"
241             shift
242             ;;
243         -*)
244             echo "`basename $0`: invalid option '$1'"
245             shift
246             prthlp
247             ;;
248         *)
249             cmd_args="$cmd_args $1"
250             shift
251             ;;
252     esac
253 done
254
255 # if called without a valid command, follow path of least surprise
256 if [ "$xauth_mode" = "none" ]; then
257     prthlp
258 fi
259
260 # figure out if we're allowed to do what we said we wanted to
261 if [ `whoami` != $lclusr ]; then
262     chkroot "to change another user's .Xauthority."
263 fi
264
265 # make sure xauth is available
266 chkcmdexs xauth
267
268 # set name for target Xauthority database
269 dstauth=`eval echo ~$lclusr/$usrauth`
270
271 # figure out what action to take
272 case $xauth_mode in
273     create)
274         # create an Xauthority database for user 'userid'.
275         # (requires md5sum, xauth)
276         chkcmdexs uptime dd md5sum cut
277         # create an empty database if one doesn't exist
278         chkfilexs $dstauth
279         # generate a random key -- depends on md5sum, among others
280         key=`(
281                 whoami
282                 uptime
283                 [ \`type -p mcookie\` ] && mcookie
284                 [ -f /proc/meminfo ] && cat /proc/meminfo
285                 [ -f /dev/urandom ] && dd if=/dev/urandom bs=16 count=1
286             ) 2>&1 | md5sum | cut -f 1 -d ' '`
287         # add all hosts specified on command line;
288         # if none specified, assume local host.
289         authhosts=`hostname`
290         if [ "$cmd_args" ]; then
291             authhosts="$cmd_args"
292         fi
293         if wrtabl $dstauth; then
294             for i in $authhosts; do 
295                 msg -n "adding key for $i to $dstauth ... "
296                 xauth -f $dstauth add $i/unix:0 . $key
297                 xauth -f $dstauth add $i:0      . $key
298                 if [ $? != 0 ]; then
299                     break
300                 fi
301                 msg "done"
302             done
303             # make sure the user owns the file
304             givusr $? $lclusr $dstauth
305         fi
306         ;;
307     merge-local)
308         # merge a local Xauthority database (if readable) 
309         # from a specified user with the database for local user.
310         # (requires xauth)
311         srcauth=`eval echo ~$srcusr/$usrauth`
312         if redabl $srcauth; then
313             mrgcmd="xauth -f $dstauth merge $srcauth"
314             mrgmsg="merging $srcauth into $dstauth"
315         else
316             exit $sts
317         fi
318         ;;
319     merge-ftp)
320         # merge a remote Xauthority database with the local one
321         # for local user, using ncftp.
322         # (requires ncftp, xauth)
323         chkcmdexs ncftp
324         srcauth="$rmtusr@$rmthst:$usrauth"
325         if [ -z "$opt_vrbopr" ]; then
326             ftp_vrbopr="-V quiet"
327         else
328             ftp_vrbopr="-V quiet"
329         fi
330         mrgcmd='ncftp $ftp_vrbopr <<-ENDFTPCMD
331                 open -ui $rmthst
332                 $rmtusr
333                 get $usrauth "|xauth -f $dstauth merge -"
334                 quit
335                 ENDFTPCMD'
336         mrgmsg="merging $srcauth into $dstauth"
337         ;;
338     merge-rsh)
339         # merge a remote Xauthority database with the local one
340         # for local user, using rsh
341         # (requires rsh, xauth)
342         chkcmdexs rsh
343         srcauth="$rmtusr@$rmthst:$usrauth"
344         mrgcmd="{ rsh -l $rmtusr $rmthst cat $usrauth } \
345             | { xauth -f $dstauth merge - }"
346         mrgmsg="merging $srcauth into $dstauth"
347         ;;
348     merge-rzip)
349         # merge a remote Xauthority database with the local one
350         # for local user, using rsh and gzip.
351         # (requires rsh, gzip, xauth)
352         chkcmdexs rsh gzip
353         srcauth="$rmtusr@$rmthst:$usrauth"
354         mrgcmd="{ rsh -l $rmtusr $rmthst gzip -c $usrauth } \
355             | { gzip -dc } \
356             | { xauth -f $dstauth merge - }"
357         mrgmsg="merging $srcauth into $dstauth"
358         ;;
359     *)
360         # something's hosed
361         echo "oops!  xauth_mode = '$xauth_mode' - this shouldn't happen."
362         sts=1
363         ;;
364 esac
365
366 # actually perform merge, if requested
367 case $xauth_mode in
368     merge-*)
369         # create an empty database if one doesn't exist
370         chkfilexs $dstauth
371         # perform the requested merge, if the target database is writable
372         if wrtabl $dstauth; then
373             msg "$mrgmsg ... "
374             eval "$mrgcmd"
375             # if successful, make sure the user owns the file
376             if givusr $? $lclusr $dstauth; then
377                 msg "done"
378             fi
379         fi
380         ;;
381 esac
382
383 exit $sts
384