2011-08-01 06:51:22 +02:00
|
|
|
# This module is an approximate ruby replacement for the libXau C library and the
|
|
|
|
# xauth(1) program. It reads and interprets the files (usually '~/.Xauthority') that
|
|
|
|
# hold authorization data used in connecting to X servers. Since it was written mainly
|
|
|
|
# for the use of X11::Protocol, its functionality is currently restricted to reading,
|
|
|
|
# not writing, of these files.
|
|
|
|
|
|
|
|
module X11
|
|
|
|
class Auth
|
|
|
|
FAILED = 0
|
2012-03-11 22:18:30 +01:00
|
|
|
SUCCESS = 1
|
|
|
|
AUTHENTICATE = 2
|
2011-08-01 06:51:22 +02:00
|
|
|
|
|
|
|
ADDRESS_TYPES = {
|
2024-01-11 20:17:35 +01:00
|
|
|
0 => :Internet,
|
|
|
|
1 => :DECnet,
|
|
|
|
2 => :Chaos,
|
|
|
|
252 => :LocalHost,
|
|
|
|
253 => :Krb5Principal,
|
|
|
|
254 => :Netname,
|
|
|
|
256 => :Local,
|
2011-08-01 06:51:22 +02:00
|
|
|
65535 => :Wild,
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthInfo = Struct.new :family, :address, :display, :auth_name, :auth_data
|
2012-03-09 06:52:42 +01:00
|
|
|
|
|
|
|
# Open an authority file, and create an object to handle it.
|
|
|
|
# The filename will be taken from the XAUTHORITY environment variable,
|
|
|
|
# if present, or '.Xauthority' in the user's home directory, or it may be overridden
|
|
|
|
# by an argument.
|
2021-09-23 20:23:34 +02:00
|
|
|
def initialize(path = ENV['XAUTHORITY'] || ENV['HOME'] + "/.Xauthority")
|
2011-08-01 06:51:22 +02:00
|
|
|
@file = File.open(path)
|
|
|
|
end
|
|
|
|
|
2012-03-09 06:52:42 +01:00
|
|
|
# Get authentication data for a connection of type family to display display_id on host.
|
|
|
|
# If family is 'Internet', the host will be translated into an appropriate address by gethostbyname().
|
2011-08-01 06:51:22 +02:00
|
|
|
# If no data is found, returns nil
|
|
|
|
def get_by_hostname(host, family, display_id)
|
2021-09-23 20:23:34 +02:00
|
|
|
host = `hostname`.chomp if host == 'localhost' or host == '127.0.0.1' or host.nil?
|
2011-08-01 06:51:22 +02:00
|
|
|
address = TCPSocket.gethostbyname(host) if family == :Internet # this line does nothing for now
|
2012-03-09 06:52:42 +01:00
|
|
|
|
2011-08-01 06:51:22 +02:00
|
|
|
auth_data = nil
|
2012-03-07 23:13:51 +01:00
|
|
|
|
|
|
|
# with each entry from XAuthority file
|
|
|
|
until @file.eof?
|
|
|
|
r = read()
|
2023-08-01 21:14:05 +02:00
|
|
|
auth_data = r if r.display.empty? || display_id.to_i == r.display.to_i
|
2011-08-01 06:51:22 +02:00
|
|
|
end
|
2012-03-07 23:13:51 +01:00
|
|
|
|
2011-08-01 06:51:22 +02:00
|
|
|
reset
|
2012-03-09 06:52:42 +01:00
|
|
|
return auth_data
|
2011-08-01 06:51:22 +02:00
|
|
|
end
|
2012-03-09 06:52:42 +01:00
|
|
|
|
2011-08-01 06:51:22 +02:00
|
|
|
# returns one entry from Xauthority file
|
|
|
|
def read
|
2024-01-11 20:17:35 +01:00
|
|
|
auth_info = [] << ADDRESS_TYPES[ @file.read(2).unpack1('n') ]
|
2012-03-11 22:18:30 +01:00
|
|
|
|
|
|
|
4.times do
|
2024-01-11 20:17:35 +01:00
|
|
|
length = @file.read(2).unpack1('n')
|
2012-03-11 22:18:30 +01:00
|
|
|
auth_info << @file.read(length)
|
|
|
|
end
|
2011-08-01 06:51:22 +02:00
|
|
|
AuthInfo[*auth_info]
|
|
|
|
end
|
|
|
|
|
2024-01-11 20:17:35 +01:00
|
|
|
def reset = @file.seek(0, IO::SEEK_SET)
|
2011-08-01 06:51:22 +02:00
|
|
|
end
|
|
|
|
end
|