Search code examples
xorgcxcb

Parsing X server authorization file


Is there any API's or documentation to facilitate in parsing the authorization file for X server?

I am using xcb to connect to a display. It accepts a xcb_auth_info_t struct for authorization info. However, I cannot find any information on how to build this structure. There doesn't seem to be any documentation on the format of the X server authorization files.

Solution I settled with:

It turns out, for MIT-MAGIC-COOKIE-1 type X authority files, the Xauth structure (from X11/Xauth.h) members map directly to the xcb_auth_info_t members. So, just read an Xauth structure from your X authority file using XauReadAuth. Then copy over the name, name_length, data, and data_length members.

If you want a more portable way to parse the X authority file, you can refer to xcb's source code. It is pretty messy, but it shouldn't be too difficult to adapt their source code for your own purposes. Refer to xcb_util.c for details on how to open a socket to a display. Once you have the socket, you can use methods from xcb_auth.c to create the xcb_auth_info_t struct (see the methods _xcb_get_auth_info, get_auth_ptr, and compute_auth).

I only needed to connect through unix sockets (AF_UNIX), so the code I ported over was fairly minimal. I mostly just used the compute_auth method (and its dependencies).


Solution

  • After some searches, it is like you doesn't have to build this structure yourself. Looks at this discussion:

    What's the right way to call xcb_connect_to_display_with_auth_info() given a Xauthority file

    The Xauthory file is specified in the XAUTHORITY environement variable. This file is generated by the program that start the X server (xdm, startx or xauth itself for example according to the doc in man xauth)

    A classical connection with auth file specified via the XAUTHORITY variable works like this :

    • xcb_connect call xcb_connect_with_auth_info() with a auth info set as NULL
    • xcb_connect_with_auth_info() call _xcb_get_auth_info() in order to get auth information from the default xauthority file.

    If you really want to see how this function get the auth info:

    git clone git://anongit.freedesktop.org/xcb/libxcb
    
    • Look at the file ./libxcb/src/xcb_util.c lines 478 to end
    • Look at the file ./libxcb/src/xcb_auth.c lines 312 to 379 for _xcb_get_auth_info()