title: Parsing X server authorization file
body: <p>Is there any API's or documentation to facilitate in parsing the authorization file for X server?</p>
  
  <p>I am using <code>xcb</code> to connect to a display. It accepts a <code>xcb_auth_info_t</code> 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.</p>
  
  <p><strong>Solution I settled with:</strong></p>
  
  <p>It turns out, for <code>MIT-MAGIC-COOKIE-1</code> type X authority files, the <code>Xauth</code> structure (from <code>X11/Xauth.h</code>) members map directly to the <code>xcb_auth_info_t</code> members. So, just read an <code>Xauth</code> structure from your X authority file using <code>XauReadAuth</code>. Then copy over the <code>name</code>, <code>name_length</code>, <code>data</code>, and <code>data_length</code> members.</p>
  
  <p>If you want a more portable way to parse the X authority file, you can refer to <code>xcb</code>'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 <code>xcb_util.c</code> for details on how to open a socket to a display. Once you have the socket, you can use methods from <code>xcb_auth.c</code> to create the <code>xcb_auth_info_t</code> struct (see the methods <code>_xcb_get_auth_info</code>, <code>get_auth_ptr</code>, and <code>compute_auth</code>).</p>
  
  <p>I only needed to connect through unix sockets (<code>AF_UNIX</code>), so the code I ported over was fairly minimal. I mostly just used the <code>compute_auth</code> method (and its dependencies).</p>