From 52a21b415ad95b2c4649254447388cb329cee1a4 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sat, 28 Apr 2012 23:25:57 -0400 Subject: initial commit. not currently in a working state. --- nexgb/auth.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 nexgb/auth.go (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go new file mode 100644 index 0000000..355afeb --- /dev/null +++ b/nexgb/auth.go @@ -0,0 +1,111 @@ +// 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" +) + +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 +} + +// readAuthority reads the X authority file for the DISPLAY. +// If hostname == "" or hostname == "localhost", +// readAuthority uses 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") +} -- cgit v1.2.3 From a5d4ad6c9d763b3d3f797075038023756c38bb28 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Thu, 3 May 2012 22:47:50 -0400 Subject: reworking xgb. cleaned up connection stuff a little. making new xid generation cleaner and use goroutines for it. --- nexgb/auth.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index 355afeb..ac43e07 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -44,8 +44,10 @@ func getString(r io.Reader, b []byte) (string, error) { // readAuthority reads the X authority file for the DISPLAY. // If hostname == "" or hostname == "localhost", -// readAuthority uses the system's hostname (as returned by os.Hostname) instead. -func readAuthority(hostname, display string) (name string, data []byte, err error) { +// 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 -- cgit v1.2.3 From daad54a5e114dcff9ef62abbbd18ea52929d01e5 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Mon, 7 May 2012 04:17:11 -0400 Subject: important stuff first please --- nexgb/auth.go | 62 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index ac43e07..85e2d56 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -11,37 +11,6 @@ import ( "os" ) -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 -} - // 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. @@ -111,3 +80,34 @@ func readAuthority(hostname, display string) ( } 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 +} -- cgit v1.2.3 From 24fef4062ad441c935f5a87e908c5f293d8a2f42 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sat, 12 May 2012 21:36:31 -0400 Subject: docs --- nexgb/auth.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index 85e2d56..396e832 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -1,9 +1,11 @@ -// 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 +/* +auth.go contains functions to facilitate the parsing of .Xauthority files. + +It is largely unmodified from the original XGB package that I forked. +*/ + import ( "bufio" "errors" -- cgit v1.2.3 From cd22f99b20b3520f578bd146847ad5c8b0603d57 Mon Sep 17 00:00:00 2001 From: Paul Sbarra Date: Mon, 28 May 2012 17:04:02 -0500 Subject: auth: use encoding.binary --- nexgb/auth.go | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index 396e832..a6fad90 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -7,7 +7,7 @@ It is largely unmodified from the original XGB package that I forked. */ import ( - "bufio" + "encoding/binary" "errors" "io" "os" @@ -21,7 +21,7 @@ func readAuthority(hostname, display string) ( // 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 + b := make([]byte, 256) // As per /usr/include/X11/Xauth.h. const familyLocal = 256 @@ -49,29 +49,28 @@ func readAuthority(hostname, display string) ( } defer r.Close() - br := bufio.NewReader(r) for { - family, err := getU16BE(br, b[0:2]) - if err != nil { + var family uint16 + if err := binary.Read(r, binary.BigEndian, &family); err != nil { return "", nil, err } - addr, err := getString(br, b[0:]) + addr, err := getString(r, b) if err != nil { return "", nil, err } - disp, err := getString(br, b[0:]) + disp, err := getString(r, b) if err != nil { return "", nil, err } - name0, err := getString(br, b[0:]) + name0, err := getString(r, b) if err != nil { return "", nil, err } - data0, err := getBytes(br, b[0:]) + data0, err := getBytes(r, b) if err != nil { return "", nil, err } @@ -83,24 +82,15 @@ func readAuthority(hostname, display string) ( 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 { + var n uint16 + if err := binary.Read(r, binary.BigEndian, &n); err != nil { return nil, err - } - if int(n) > len(b) { + } else if n > uint16(len(b)) { return nil, errors.New("bytes too long for buffer") } - _, err = io.ReadFull(r, b[0:n]) - if err != nil { + + if _, err := io.ReadFull(r, b[0:n]); err != nil { return nil, err } return b[0:n], nil -- cgit v1.2.3 From 8d343cfd3a12d910d6a663f2d5f2cd4a41d88ce2 Mon Sep 17 00:00:00 2001 From: aarzilli Date: Mon, 21 Mar 2016 18:50:49 +0100 Subject: Handle wildcard values in Xauthority file Some field values in the Xauthority file have special meanings: - a value of 65535 in the 'family' field means that the entry will match a connection of any family on any address - an empty string in the 'display number' field means that the entry will match a connection on any display number This behaviour is documented at: https://cgit.freedesktop.org/xorg/lib/libXau/tree/AuGetBest.c#n109 --- nexgb/auth.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index a6fad90..62a9b35 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -25,6 +25,7 @@ func readAuthority(hostname, display string) ( // As per /usr/include/X11/Xauth.h. const familyLocal = 256 + const familyWild = 65535 if len(hostname) == 0 || hostname == "localhost" { hostname, err = os.Hostname() @@ -75,7 +76,10 @@ func readAuthority(hostname, display string) ( return "", nil, err } - if family == familyLocal && addr == hostname && disp == display { + addrmatch := (family == familyWild) || (family == familyLocal && addr == hostname) + dispmatch := (disp == "") || (disp == display) + + if addrmatch && dispmatch { return name0, data0, nil } } -- cgit v1.2.3 From 1614b58c420f7875f92a5469c77fd7aeccc7a106 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Tue, 29 Mar 2016 16:23:18 -0400 Subject: fix structs with field name of 'Bytes' (it conflict with a method of the same name that is generated for all such structs) --- nexgb/auth.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'nexgb/auth.go') diff --git a/nexgb/auth.go b/nexgb/auth.go index 62a9b35..ec51d10 100644 --- a/nexgb/auth.go +++ b/nexgb/auth.go @@ -76,7 +76,8 @@ func readAuthority(hostname, display string) ( return "", nil, err } - addrmatch := (family == familyWild) || (family == familyLocal && addr == hostname) + addrmatch := (family == familyWild) || + (family == familyLocal && addr == hostname) dispmatch := (disp == "") || (disp == display) if addrmatch && dispmatch { -- cgit v1.2.3