127702724Smrg
227702724Smrg
327702724Smrg		     A Sample Authorization Protocol for X
427702724Smrg
527702724Smrg
627702724SmrgOverview
727702724Smrg
827702724SmrgThe following note describes a very simple mechanism for providing individual
927702724Smrgaccess to an X Window System display.  It uses existing core protocol and
1027702724Smrglibrary hooks for specifying authorization data in the connection setup block
1127702724Smrgto restrict use of the display to only those clients that show that they
1227702724Smrgknow a server-specific key called a "magic cookie".  This mechanism is *not*
1327702724Smrgbeing proposed as an addition to the Xlib standard; among other reasons, a
1427702724Smrgprotocol extension is needed to support more flexible mechanisms.  We have
1527702724Smrgimplemented this mechanism already; if you have comments, please send them
1627702724Smrgto us.
1727702724Smrg
1827702724SmrgThis scheme involves changes to the following parts of the sample release:
1927702724Smrg
2027702724Smrg    o  xdm
2127702724Smrg	-  generate random magic cookie and store in protected file
2227702724Smrg	-  pass name of magic cookie file to server
2327702724Smrg	-  when user logs in, add magic cookie to user's auth file
2427702724Smrg	-  when user logs out, generate a new cookie for server
2527702724Smrg
2627702724Smrg    o  server
2727702724Smrg	-  a new command line option to specify cookie file
2827702724Smrg	-  check client authorization data against magic cookie
2927702724Smrg	-  read in cookie whenever the server resets
3027702724Smrg	-  do not add local machine to host list if magic cookie given
3127702724Smrg
3227702724Smrg    o  Xlib
3327702724Smrg	-  read in authorization data from file
3427702724Smrg	-  find data for appropriate server
3527702724Smrg	-  send authorization data if found
3627702724Smrg
3727702724Smrg    o  xauth [new program to manage user auth file]
3827702724Smrg	-  add entries to user's auth file
3927702724Smrg	-  remove entries from user's auth file
4027702724Smrg
41313a12fdSmrgThis mechanism assumes that the superuser and the transport layer between
42e19dfac4Smrgthe client and the server is secure.
4327702724Smrg
4427702724Smrg
4527702724SmrgDescription
4627702724Smrg
4727702724SmrgThe sample implementation will use the xdm Display Manager to set up and
4827702724Smrgcontrol the server's authorization file.  Sites that do not run xdm will
49313a12fdSmrgneed to build their own mechanisms.
5027702724Smrg
5127702724SmrgXdm uses a random key (seeded by the system time and check sum of /dev/kmem)
5227702724Smrgto generate a unique sequence of characters at 16 bytes long.  This sequence
5327702724Smrgwill be written to a file which is made readable only by the server.  The
5427702724Smrgserver will then be started with a command line option instructing it to use
5527702724Smrgthe contents of the file as the magic cookie for connections that include
5627702724Smrgauthorization data.  This will also disable the server from adding the local
5727702724Smrgmachine's address to the initial host list.  Note that the actual cookie must
5827702724Smrgnot be stored on the command line or in an environment variable, to prevent
5927702724Smrgit from being publicly obtainable by the "ps" command.
6027702724Smrg
6127702724SmrgIf a client presents an authorization name of "MIT-MAGIC-COOKIE-1" and
6227702724Smrgauthorization data that matches the magic cookie, that client is allowed
6327702724Smrgaccess.  If the name or data does not match and the host list is empty,
6427702724Smrgthat client will be denied access.  Otherwise, the existing host-based access
6527702724Smrgcontrol will be used.  Since any client that is making a connection from a
6627702724Smrgmachine on the host list will be granted access even if their authorization
6727702724Smrgdata is incorrect, sites are strongly urged not to set up any default hosts
6827702724Smrgusing the /etc/X*.hosts files.  Granting access to other machines should be
6927702724Smrgdone by the user's session manager instead.
7027702724Smrg
7127702724SmrgAssuming the server is configured with an empty host list, the existence of the
7227702724Smrgcookie is sufficient to ensure there will be no unauthorized access to the
7327702724Smrgdisplay.  However, xdm will (continue to) work to minimize the chances of
7427702724Smrgspoofing on servers that do not support this authorization mechanism.  This
7527702724Smrgwill be done by grabbing the server and the keyboard after opening the display.
7627702724SmrgThis action will be surrounded by a timer which will kill the server if the
7727702724Smrggrabs cannot be done within several seconds.  [This level of security is now
7827702724Smrgimplemented in patches already sent out.]
7927702724Smrg
8027702724SmrgAfter the user logs in, xdm will add authorization entries for each of the
8127702724Smrgserver machine's network addresses to the user's authorization file (the format
8227702724Smrgof which is described below).  This file will usually be named .Xauthority in
8327702724Smrgthe users's home directory; will be owned by the user (as specified by the
8427702724Smrgpw_uid and pw_gid fields in the user's password entry), and will be accessible
8527702724Smrgonly to the user (no group access).  This file will contain authorization data
8627702724Smrgfor all of the displays opened by the user.
8727702724Smrg
8827702724SmrgWhen the session terminates, xdm will generate and store a new magic cookie
8927702724Smrgfor the server.  Then, xdm will shutdown its own connection and send a
9027702724SmrgSIGHUP to the server process, which should cause the server to reset.  The
9127702724Smrgserver will then read in the new magic cookie.
9227702724Smrg
9327702724SmrgTo support accesses (both read and write) from multiple machines (for use in
9427702724Smrgenvironments that use distributed file systems), file locking is done using
9527702724Smrghard links.  This is done by creat'ing (sic) a lock file and then linking it
9627702724Smrgto another name in the same directory.  If the link-target already exists,
9727702724Smrgthe link will fail, indicating failure to obtain the lock.  Linking is used
9827702724Smrginstead of just creating the file read-only since link will fail even for
9927702724Smrgthe superuser.
10027702724Smrg
10127702724SmrgProblems and Solutions
10227702724Smrg
10327702724SmrgThere are a few problems with .Xauthority as described.  If no home directory
10427702724Smrgexists, or if xdm cannot create a file there (disk full), xdm stores the
10527702724Smrgcookie in a file in a resource-specified back-up directory, and sets an
10627702724Smrgenvironment variable in the user's session (called XAUTHORITY) naming this
10727702724Smrgfile.  There is also the problem that the locking attempts will need to be
10827702724Smrgtimed out, due to a leftover lock.  Xdm, again, creates a file and set an
10927702724Smrgenvironment variable.  Finally, the back-up directory might be full.  Xdm,
11027702724Smrgas a last resort, provides a function key binding that allows a user to log
11127702724Smrgin without having the authorization data stored, and with host-based access
11227702724Smrgcontrol disabled.
11327702724Smrg
11427702724SmrgXlib
11527702724Smrg
11627702724SmrgXOpenDisplay in Xlib was enhanced to allow specification of authorization
11727702724Smrginformation.  As implied above, Xlib looks for the data in the
11827702724Smrg.Xauthority file of the home directory, or in the file pointed at by the
11927702724SmrgXAUTHORITY environment variable instead if that is defined.  This required
12027702724Smrgno programmatic interface change to Xlib.  In addition, a new Xlib routine
12127702724Smrgis provided to explicitly specify authorization.
12227702724Smrg
12327702724Smrg	XSetAuthorization(name, namelen, data, datalen)
12427702724Smrg		int namelen, datalen;
12527702724Smrg		char *name, *data;
12627702724Smrg
12727702724SmrgThere are three types of input:
12827702724Smrg
12927702724Smrg	name NULL, data don't care	- use default authorization mechanism.
13027702724Smrg	name non-NULL, data NULL	- use the named authorization; get
13127702724Smrg					  data from that mechanism's default.
13227702724Smrg	name non-NULL, data non-NULL	- use the given authorization and data.
133313a12fdSmrg
13427702724SmrgThis interface is used by xdm and might also be used by any other
13527702724Smrgapplications that wish to explicitly set the authorization information.
13627702724Smrg
13727702724SmrgAuthorization File
13827702724Smrg
13927702724SmrgThe .Xauthority file is a binary file consisting of a sequence of entries
14027702724Smrgin the following format:
14127702724Smrg
14227702724Smrg	2 bytes		Family value (second byte is as in protocol HOST)
14327702724Smrg	2 bytes		address length (always MSB first)
14427702724Smrg	A bytes		host address (as in protocol HOST)
14527702724Smrg	2 bytes		display "number" length (always MSB first)
14627702724Smrg	S bytes		display "number" string
14727702724Smrg	2 bytes		name length (always MSB first)
14827702724Smrg	N bytes		authorization name string
14927702724Smrg	2 bytes		data length (always MSB first)
15027702724Smrg	D bytes		authorization data string
15127702724Smrg
15227702724SmrgThe format is binary for easy processing, since authorization information
15327702724Smrgusually consists of arbitrary data.  Host addresses are used instead of
15427702724Smrgnames to eliminate potentially time-consuming name resolutions in
15527702724SmrgXOpenDisplay.  Programs, such as xdm, that initialize the user's
15627702724Smrgauthorization file will have to do the same work as the server in finding
15727702724Smrgaddresses for all network interfaces.  If more than one entry matches the
15827702724Smrgdesired address, the entry that is chosen is implementation-dependent.  In
15927702724Smrgour implementation, it is always the first in the file.
16027702724Smrg
16127702724SmrgThe Family is specified in two bytes to allow out-of-band values
16227702724Smrg(i.e. values not in the Protocol) to be used.  In particular,
16327702724Smrgtwo new values "FamilyLocal" and "FamilyWild" are defined.  FamilyLocal
16495b7a5c8Smrgrefers to any connections using a non-network method of connection from the
16527702724Smrglocal machine (Unix domain sockets, shared memory, loopback serial line).
16627702724SmrgIn this case the host address is specified by the data returned from
16727702724Smrggethostname() and better be unique in a collection of machines
16827702724Smrgwhich share NFS directories.  FamilyWild is currently used only
16927702724Smrgby xdm to communicate authorization data to the server.  It matches
17027702724Smrgany family/host address pair.
17127702724Smrg
17227702724SmrgFor FamilyInternet, the host address is the 4 byte internet address, for
17327702724SmrgFamilyDecnet, the host address is the byte decnet address, for FamilyChaos
17427702724Smrgthe address is also two bytes.
17527702724Smrg
17627702724SmrgThe Display Number is the ascii representation of the display number
17727702724Smrgportion of the display name.  It is in ascii to allow future expansion
17827702724Smrgto PseudoRoots or anything else that might happen.
17927702724Smrg
18027702724SmrgA utility called "xauth" will be provided for editing and viewing the
18127702724Smrgcontents of authorization files.  Note that the user's authorization file is
18227702724Smrgnot the same as the server's magic cookie file.
183