aboutsummaryrefslogtreecommitdiff
path: root/nexgb/auth.go
blob: 85e2d56c9d2e213b7fac73880a85e133e724794b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2009 The XGB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xgb

import (
	"bufio"
	"errors"
	"io"
	"os"
)

// readAuthority reads the X authority file for the DISPLAY.
// If hostname == "" or hostname == "localhost",
// then use the system's hostname (as returned by os.Hostname) instead.
func readAuthority(hostname, display string) (
	name string, data []byte, err error) {

	// b is a scratch buffer to use and should be at least 256 bytes long
	// (i.e. it should be able to hold a hostname).
	var b [256]byte

	// As per /usr/include/X11/Xauth.h.
	const familyLocal = 256

	if len(hostname) == 0 || hostname == "localhost" {
		hostname, err = os.Hostname()
		if err != nil {
			return "", nil, err
		}
	}

	fname := os.Getenv("XAUTHORITY")
	if len(fname) == 0 {
		home := os.Getenv("HOME")
		if len(home) == 0 {
			err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set")
			return "", nil, err
		}
		fname = home + "/.Xauthority"
	}

	r, err := os.Open(fname)
	if err != nil {
		return "", nil, err
	}
	defer r.Close()

	br := bufio.NewReader(r)
	for {
		family, err := getU16BE(br, b[0:2])
		if err != nil {
			return "", nil, err
		}

		addr, err := getString(br, b[0:])
		if err != nil {
			return "", nil, err
		}

		disp, err := getString(br, b[0:])
		if err != nil {
			return "", nil, err
		}

		name0, err := getString(br, b[0:])
		if err != nil {
			return "", nil, err
		}

		data0, err := getBytes(br, b[0:])
		if err != nil {
			return "", nil, err
		}

		if family == familyLocal && addr == hostname && disp == display {
			return name0, data0, nil
		}
	}
	panic("unreachable")
}

func getU16BE(r io.Reader, b []byte) (uint16, error) {
	_, err := io.ReadFull(r, b[0:2])
	if err != nil {
		return 0, err
	}
	return uint16(b[0])<<8 + uint16(b[1]), nil
}

func getBytes(r io.Reader, b []byte) ([]byte, error) {
	n, err := getU16BE(r, b)
	if err != nil {
		return nil, err
	}
	if int(n) > len(b) {
		return nil, errors.New("bytes too long for buffer")
	}
	_, err = io.ReadFull(r, b[0:n])
	if err != nil {
		return nil, err
	}
	return b[0:n], nil
}

func getString(r io.Reader, b []byte) (string, error) {
	b, err := getBytes(r, b)
	if err != nil {
		return "", err
	}
	return string(b), nil
}