| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package X11::Auth; | ||
| 4 | |||
| 5 | # Copyright (C) 1997, 1999, 2005 Stephen McCamant. All rights | ||
| 6 | # reserved. This program is free software; you can redistribute and/or | ||
| 7 | # modify it under the same terms as Perl itself. | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use strict; | ||
| 11 | use vars '$VERSION'; | ||
| 12 | use FileHandle; | ||
| 13 | require 5.000; | ||
| 14 | |||
| 15 | $VERSION = 0.05; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my($class, $fname) = @_; | ||
| 19 | $fname = $fname || $main::ENV{"XAUTHORITY"} || | ||
| 20 | "$main::ENV{HOME}/.Xauthority"; | ||
| 21 | return 0 unless -e $fname; | ||
| 22 | my $self = bless {}, $class; | ||
| 23 | $self->{filename} = $fname; | ||
| 24 | my($fh) = new FileHandle; | ||
| 25 | $fh->open("<$fname") or croak "Can't open $fname: $!"; | ||
| 26 | $self->{filehandle} = $fh; | ||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub open { new(@_) } | ||
| 31 | |||
| 32 | sub read { | ||
| 33 | my($self, $len) = @_; | ||
| 34 | my($buf); | ||
| 35 | my $ret = read $self->{filehandle}, $buf, $len; | ||
| 36 | if (not defined $ret) { | ||
| 37 | croak "Can't read authority file " . $self->{filename} . ": $!"; | ||
| 38 | } elsif ($ret < $len) { | ||
| 39 | warn "Expecting $len bytes, got $ret at " . tell($self->{filename}); | ||
| 40 | croak "Unexpected short read from authority file" . $self->{filename}; | ||
| 41 | } | ||
| 42 | return $buf; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub get_one { | ||
| 46 | my $self = shift; | ||
| 47 | my(@a, $x); | ||
| 48 | my $warned_nulls = 0; | ||
| 49 | RETRY: { | ||
| 50 | if ($self->{filehandle}->eof) { | ||
| 51 | close $self->{filehandle}; | ||
| 52 | return (); | ||
| 53 | } | ||
| 54 | $x = unpack("n", $self->read(2)); # Family | ||
| 55 | my $type = {256 => 'Local', 65535 => 'Wild', 254 => 'Netname', | ||
| 56 | 253 => 'Krb5Principal', 252 => 'LocalHost', | ||
| 57 | 0 => 'Internet', 1 => 'DECnet', 2 => 'Chaos', | ||
| 58 | 5 => 'ServerInterpreted', 6 => 'InternetV6'}->{$x}; | ||
| 59 | if (not defined($type)) { | ||
| 60 | warn "Error in $self->{filename}: unknown address type $x"; | ||
| 61 | } | ||
| 62 | push @a, $type; | ||
| 63 | |||
| 64 | $x = unpack("n", $self->read(2)); # Address | ||
| 65 | push @a, $self->read($x); | ||
| 66 | |||
| 67 | $x = unpack("n", $self->read(2)); # Display `number' | ||
| 68 | push @a, $self->read($x); | ||
| 69 | |||
| 70 | $x = unpack("n", $self->read(2)); # Authorization name | ||
| 71 | push @a, $self->read($x); | ||
| 72 | |||
| 73 | $x = unpack("n", $self->read(2)); # Authorization data | ||
| 74 | push @a, $self->read($x); | ||
| 75 | |||
| 76 | if ($type eq "Internet" and $a[1] eq "" and $a[2] eq "" | ||
| 77 | and $a[3] eq "" and $a[4] eq "") { | ||
| 78 | warn "Error in $self->{filename}: unexpected null bytes" | ||
| 79 | unless $warned_nulls; | ||
| 80 | $warned_nulls = 1; | ||
| 81 | @a = (); | ||
| 82 | next RETRY; | ||
| 83 | } | ||
| 84 | |||
| 85 | return @a; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_all { | ||
| 90 | my $self = shift; | ||
| 91 | return @{$self->{data}} if $self->{data}; | ||
| 92 | my(@a, @x); | ||
| 93 | while (@x = $self->get_one) { | ||
| 94 | push @a, [@x]; | ||
| 95 | } | ||
| 96 | $self->{data} = [@a]; | ||
| 97 | return @a; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub get_by_host { | ||
| 101 | my $self = shift; | ||
| 102 | my($host, $fam, $dpy) = @_; | ||
| 103 | if ($host eq "localhost" or $host eq "127.0.0.1") { | ||
| 104 | require Sys::Hostname; | ||
| 105 | $host = Sys::Hostname::hostname(); | ||
| 106 | } | ||
| 107 | my($addr); | ||
| 108 | $addr = gethostbyname($host) if $fam eq "Internet"; | ||
| 109 | #print "host $host, addr $addr\n"; | ||
| 110 | my($d); | ||
| 111 | for $d ($self->get_all) { | ||
| 112 | next unless $dpy eq $d->[2]; | ||
| 113 | next unless $fam eq $d->[0] or ($fam eq "Internet" | ||
| 114 | and $d->[0] eq "Local"); | ||
| 115 | if ($fam eq "Internet" or $fam eq "Local") { | ||
| 116 | if ($addr && $d->[1] eq $addr or $d->[1] eq $host) { | ||
| 117 | return ($d->[3], $d->[4]); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return (); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ |