1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // readU16BE reads a big-endian uint16 from r, using b as a scratch buffer.
15 func readU16BE(r io.Reader, b []byte) (uint16, error) {
16 _, err := io.ReadFull(r, b[0:2])
20 return uint16(b[0])<<8 + uint16(b[1]), nil
23 // readStr reads a length-prefixed string from r, using b as a scratch buffer.
24 func readStr(r io.Reader, b []byte) (string, error) {
25 n, err := readU16BE(r, b)
30 return "", errors.New("Xauthority entry too long for buffer")
32 _, err = io.ReadFull(r, b[0:n])
36 return string(b[0:n]), nil
39 // readAuth reads the X authority file and returns the name/data pair for the display.
40 // displayStr is the "12" out of a $DISPLAY like ":12.0".
41 func readAuth(displayStr string) (name, data string, err error) {
42 // b is a scratch buffer to use and should be at least 256 bytes long
43 // (i.e. it should be able to hold a hostname).
45 // As per /usr/include/X11/Xauth.h.
46 const familyLocal = 256
48 fn := os.Getenv("XAUTHORITY")
50 home := os.Getenv("HOME")
52 err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set")
55 fn = home + "/.Xauthority"
62 br := bufio.NewReader(r)
64 hostname, err := os.Hostname()
70 var addr, disp, name0, data0 string
71 family, err = readU16BE(br, b[0:2])
75 addr, err = readStr(br, b[0:])
79 disp, err = readStr(br, b[0:])
83 name0, err = readStr(br, b[0:])
87 data0, err = readStr(br, b[0:])
91 if family == familyLocal && addr == hostname && disp == displayStr {
92 return name0, data0, nil