2018-09-08 19:39:28 +02:00
|
|
|
package nexgb
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-13 03:36:31 +02:00
|
|
|
/*
|
|
|
|
auth.go contains functions to facilitate the parsing of .Xauthority files.
|
|
|
|
|
|
|
|
It is largely unmodified from the original XGB package that I forked.
|
|
|
|
*/
|
|
|
|
|
2012-04-29 05:25:57 +02:00
|
|
|
import (
|
2012-05-29 00:04:02 +02:00
|
|
|
"encoding/binary"
|
2012-04-29 05:25:57 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// readAuthority reads the X authority file for the DISPLAY.
|
|
|
|
// If hostname == "" or hostname == "localhost",
|
2012-05-04 04:47:50 +02:00
|
|
|
// then use the system's hostname (as returned by os.Hostname) instead.
|
|
|
|
func readAuthority(hostname, display string) (
|
|
|
|
name string, data []byte, err error) {
|
|
|
|
|
2012-04-29 05:25:57 +02:00
|
|
|
// 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).
|
2012-05-29 00:04:02 +02:00
|
|
|
b := make([]byte, 256)
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// As per /usr/include/X11/Xauth.h.
|
|
|
|
const familyLocal = 256
|
2016-03-21 18:50:49 +01:00
|
|
|
const familyWild = 65535
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
for {
|
2012-05-29 00:04:02 +02:00
|
|
|
var family uint16
|
|
|
|
if err := binary.Read(r, binary.BigEndian, &family); err != nil {
|
2012-04-29 05:25:57 +02:00
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:04:02 +02:00
|
|
|
addr, err := getString(r, b)
|
2012-04-29 05:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:04:02 +02:00
|
|
|
disp, err := getString(r, b)
|
2012-04-29 05:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:04:02 +02:00
|
|
|
name0, err := getString(r, b)
|
2012-04-29 05:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:04:02 +02:00
|
|
|
data0, err := getBytes(r, b)
|
2012-04-29 05:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:23:18 +02:00
|
|
|
addrmatch := (family == familyWild) ||
|
|
|
|
(family == familyLocal && addr == hostname)
|
2016-03-21 18:50:49 +01:00
|
|
|
dispmatch := (disp == "") || (disp == display)
|
|
|
|
|
|
|
|
if addrmatch && dispmatch {
|
2012-04-29 05:25:57 +02:00
|
|
|
return name0, data0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
2012-05-07 10:17:11 +02:00
|
|
|
|
|
|
|
func getBytes(r io.Reader, b []byte) ([]byte, error) {
|
2012-05-29 00:04:02 +02:00
|
|
|
var n uint16
|
|
|
|
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
|
2012-05-07 10:17:11 +02:00
|
|
|
return nil, err
|
2012-05-29 00:04:02 +02:00
|
|
|
} else if n > uint16(len(b)) {
|
2012-05-07 10:17:11 +02:00
|
|
|
return nil, errors.New("bytes too long for buffer")
|
|
|
|
}
|
2012-05-29 00:04:02 +02:00
|
|
|
|
|
|
|
if _, err := io.ReadFull(r, b[0:n]); err != nil {
|
2012-05-07 10:17:11 +02:00
|
|
|
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
|
|
|
|
}
|