haven/nexgb/auth.go

110 lines
2.3 KiB
Go
Raw Normal View History

package xgb
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.
*/
import (
2012-05-29 00:04:02 +02:00
"encoding/binary"
"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).
2012-05-29 00:04:02 +02:00
b := make([]byte, 256)
// As per /usr/include/X11/Xauth.h.
const familyLocal = 256
const familyWild = 65535
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 {
return "", nil, err
}
2012-05-29 00:04:02 +02:00
addr, err := getString(r, b)
if err != nil {
return "", nil, err
}
2012-05-29 00:04:02 +02:00
disp, err := getString(r, b)
if err != nil {
return "", nil, err
}
2012-05-29 00:04:02 +02:00
name0, err := getString(r, b)
if err != nil {
return "", nil, err
}
2012-05-29 00:04:02 +02:00
data0, err := getBytes(r, b)
if err != nil {
return "", nil, err
}
addrmatch := (family == familyWild) || (family == familyLocal && addr == hostname)
dispmatch := (disp == "") || (disp == display)
if addrmatch && dispmatch {
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
}