3 # mkxauth: script to make per-user Xauthority database
4 # formerly 'newcookie' script; modified 18-Jul-1996 jim knoble
6 ########################################################################
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
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
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.
19 # Revision 1.6 1996/10/02 20:34:12 jmknoble
20 # updated help text again
22 # Revision 1.5 1996/10/02 20:10:03 jmknoble
25 # Revision 1.4 1996/10/02 20:03:26 jmknoble
26 # fixed quoting problem in key generation
28 # Revision 1.3 1996/08/20 16:31:30 jmknoble
29 # refined random key generation (using mcookie if available)
31 # Revision 1.2 1996/08/20 15:49:33 jmknoble
32 # replaced key generation using perl with method using md5sum
34 # Revision 1.1 1996/08/05 16:40:20 jmknoble
37 ########################################################################
41 ## default values for some variables
43 # eventual exit status
45 # verbose operation if blank
47 # eventual string of non-option arguments
49 # filename for per-user Xauthority database
51 # username for whom to make per-user database
53 # mode for making database;
54 # valid values are 'create', 'merge-local',
55 # 'merge-ftp', 'merge-rsh', 'merge-rzip',
58 # actual path to target database
60 # user to login as for rsh/rzip modes
62 # host to contact for remote Xauthority databases
64 # local user to grab Xauthority from in merge mode
67 ########################################################################
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> ... ]]"
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."
82 echo " -m <login> merge the Xauthority database from local user <login>"
83 echo " (if readable) with the target .Xauthority"
85 echo " -f <host> merge a remote Xauthority database with the target"
86 echo " .Xauthority, using ncftp"
88 echo " -r <host> merge a remote Xauthority database with the target"
89 echo " .Xauthority, using rsh"
91 echo " -z <host> merge a remote Xauthority database with the target"
92 echo " .Xauthority, using rsh and gzip"
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."
100 echo " -q quiet operation"
102 echo " -u <login> create/merge .Xauthority for user <login>"
104 echo " -l <login> for '-f', '-r' and '-z' modes, use <login> for the"
111 # check that current user is root
113 if [ `whoami` != root ]; then
114 echo "sorry---you need to be root" "$*"
119 # write a message to stdout iff verbose mode on
121 if [ -z "$opt_vrbopr" ]; then
126 # check that a command exists
127 function chkcmdexs() {
129 if [ -z `type -p $i` ]; then
130 echo "`basename $0`: error: can't find command '$i'"
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() {
140 if [ ! -f "$i" ]; then
141 if [ -w `dirname $i` ]; then
142 msg -n "creating file $i ... "
150 # check if a file is readable
153 if [ -r "$srcfil" ]; then
156 echo "`basename $0`: error: cannot read file $srcfil"
162 # check if a file is writable
165 if [ -w "$dstfil" ]; then
168 echo "`basename $0`: error: cannot write to file $dstfil"
174 # set the correct ownership for a file
179 if [ $lststs = 0 ]; then
180 chown $usrnam:$usrnam $dstfil
184 echo "`basename $0`: error writing to file $dstfil"
190 ########################################################################
191 # set our umask so that no one else can read our files
194 # test some command-line args
221 xauth_mode='merge-local'
227 xauth_mode='merge-ftp'
233 xauth_mode='merge-rsh'
239 xauth_mode='merge-rzip'
244 echo "`basename $0`: invalid option '$1'"
249 cmd_args="$cmd_args $1"
255 # if called without a valid command, follow path of least surprise
256 if [ "$xauth_mode" = "none" ]; then
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."
265 # make sure xauth is available
268 # set name for target Xauthority database
269 dstauth=`eval echo ~$lclusr/$usrauth`
271 # figure out what action to take
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
279 # generate a random key -- depends on md5sum, among others
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.
290 if [ "$cmd_args" ]; then
291 authhosts="$cmd_args"
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
303 # make sure the user owns the file
304 givusr $? $lclusr $dstauth
308 # merge a local Xauthority database (if readable)
309 # from a specified user with the database for local user.
311 srcauth=`eval echo ~$srcusr/$usrauth`
312 if redabl $srcauth; then
313 mrgcmd="xauth -f $dstauth merge $srcauth"
314 mrgmsg="merging $srcauth into $dstauth"
320 # merge a remote Xauthority database with the local one
321 # for local user, using ncftp.
322 # (requires ncftp, xauth)
324 srcauth="$rmtusr@$rmthst:$usrauth"
325 if [ -z "$opt_vrbopr" ]; then
326 ftp_vrbopr="-V quiet"
328 ftp_vrbopr="-V quiet"
330 mrgcmd='ncftp $ftp_vrbopr <<-ENDFTPCMD
333 get $usrauth "|xauth -f $dstauth merge -"
336 mrgmsg="merging $srcauth into $dstauth"
339 # merge a remote Xauthority database with the local one
340 # for local user, using rsh
341 # (requires rsh, xauth)
343 srcauth="$rmtusr@$rmthst:$usrauth"
344 mrgcmd="{ rsh -l $rmtusr $rmthst cat $usrauth } \
345 | { xauth -f $dstauth merge - }"
346 mrgmsg="merging $srcauth into $dstauth"
349 # merge a remote Xauthority database with the local one
350 # for local user, using rsh and gzip.
351 # (requires rsh, gzip, xauth)
353 srcauth="$rmtusr@$rmthst:$usrauth"
354 mrgcmd="{ rsh -l $rmtusr $rmthst gzip -c $usrauth } \
356 | { xauth -f $dstauth merge - }"
357 mrgmsg="merging $srcauth into $dstauth"
361 echo "oops! xauth_mode = '$xauth_mode' - this shouldn't happen."
366 # actually perform merge, if requested
369 # create an empty database if one doesn't exist
371 # perform the requested merge, if the target database is writable
372 if wrtabl $dstauth; then
375 # if successful, make sure the user owns the file
376 if givusr $? $lclusr $dstauth; then