File: Auth.pm

package info (click to toggle)
libx11-protocol-perl 0.53-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 556 kB
  • ctags: 869
  • sloc: perl: 8,298; makefile: 595; ansic: 580
file content (195 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/perl

package X11::Auth;

# Copyright (C) 1997, 1999 Stephen McCamant. All rights reserved. This
# program is free software; you can redistribute and/or modify it
# under the same terms as Perl itself.

use Carp;
use strict;
use vars '$VERSION';
use FileHandle;
require 5.000;

$VERSION = 0.03;

sub new {
    my($class, $fname) = @_;
    $fname = $fname || $main::ENV{"XAUTHORITY"} || 
	"$main::ENV{HOME}/.Xauthority";
    return 0 unless -e $fname;
    my $self = bless {}, $class;
    $self->{filename} = $fname;
    my($fh) = new FileHandle;
    $fh->open("<$fname") or croak "Can't open $fname: $!";
    $self->{filehandle} = $fh;
    return $self;
}

sub open { new(@_) }

sub read {
    my $self = shift;
    my($buf);
    read $self->{filehandle}, $buf, $_[0] or croak "Can't read: $!";
    return $buf;
}

sub get_one {
    my $self = shift;
    my(@a, $x);

    if ($self->{filehandle}->eof) {
	close $self->{filehandle};
	return ();
    }
    $x = unpack("n", $self->read(2)); # Family
    my $type = {256 => 'Local', 65535 => 'Wild', 254 => 'Netname',
		253 => 'Krb5Principal', 252 => 'LocalHost', 0 => 'Internet',
		1 => 'DECnet', 2 => 'Chaos'}->{$x};
    if (not defined($type)) {
	warn "Error in $self->{filename}: unknown address type $x";
	return ();
    }
    push @a, $type;

    $x = unpack("n", $self->read(2)); # Address
    push @a, $self->read($x);

    $x = unpack("n", $self->read(2)); # Display `number'
    push @a, $self->read($x);

    $x = unpack("n", $self->read(2)); # Authorization name
    push @a, $self->read($x);

    $x = unpack("n", $self->read(2)); # Authorization data
    push @a, $self->read($x);

    return @a;
}

sub get_all {
    my $self = shift;
    return @{$self->{data}} if $self->{data};
    my(@a, @x);
    while (@x = $self->get_one) {
	push @a, [@x];
    }
    $self->{data} = [@a];
    return @a;
}

sub get_by_host {
    my $self = shift;
    my($host, $fam, $dpy) = @_;
    if ($host eq "localhost" or $host eq "127.0.0.1") {
	require Sys::Hostname;
	$host = Sys::Hostname::hostname();
    }
    my($addr);
    $addr = gethostbyname($host) if $fam eq "Internet";
    #print "host $host, addr $addr\n";
    my($d);
    for $d ($self->get_all) {
	next unless $dpy eq $d->[2];
	next unless $fam eq $d->[0] or ($fam eq "Internet"
					and $d->[0] eq "Local");
	if ($fam eq "Internet" or $fam eq "Local") {
	    if ($addr && $d->[1] eq $addr or $d->[1] eq $host) {
		return ($d->[3], $d->[4]);
	    }
	}
    }
    return ();
}

1;

__END__

=head1 NAME

X11::Auth - Perl module to read X11 authority files

=head1 SYNOPSIS

  require X11::Auth;
  $a = new X11::Auth;
  ($auth_type, $auth_data) = $a->get_by_host($host, $disp_num);

=head1 DESCRIPTION

This module is an approximate perl 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.

=head1 METHODS

=head2 new

  $auth = X11::Auth->new;
  $auth = X11::Auth->open($filename);

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. 'open' may be used as a synonym.

=head2 get_one

  ($family, $host_addr, $display_num, $auth_name, $auth_data)
     = $auth->get_one;

Read one entry from the file. Returns a null list at end of file.
$family is usually 'Internet' or 'Local', and $display_num can
be any string.

=head2 get_all

  @auth_data = $auth->get_all;

Read all of the entries in the file. Each member of the array returned
is an array ref similar to the list returned by get_one().

=head2 get_by_host

  ($auth_name, $auth_data)
     = $auth->get_by_host($host, $family, $display_num);

Get authentication data for a connection of type $family to display
$display_num on $host. If $family is 'Internet', the host will be
translated into an appropriate address by gethostbyname(). If no data
is found, returns an empty list.

=head1 COMPATIBILITY

The following table shows the (rough) correspondence between libXau
calls and X11::Auth methods:

  libXau                     X11::Auth
  ------                     ---------
  XauFileName                $ENV{XAUTHORITY}
                             || "$ENV{HOME}/.Xauthority"
  fopen(XauFileName(), "rb") $auth = new X11::Auth
  XauReadAuth                $auth->get_one
  XauWriteAuth
  XauGetAuthByAddr           $auth->get_by_host
  XauGetBestAuthByAddr 
  XauLockAuth
  XauUnlockAuth
  XauDisposeAuth

=head1 AUTHOR

Stephen McCamant <SMCCAM@cpan.org>

=head1 SEE ALSO

L<perl(1)>, L<X11::Protocol>, L<Xau(3)>, L<xauth(1)>,
lib/Xau/README in the X11 source distribution.

=cut