| 1 | #!/usr/bin/perl -Tw | ||||||||||
| 2 | #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free | ||||||||||
| 3 | #software; you can redistribute it and/or modify it under the same terms as | ||||||||||
| 4 | #PERL itself. | ||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | package CfgTie::TieHost; | ||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | C<CfgTie::TieHost> -- This accesses the hosts tables. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | This is an associative array that allows the hosts tables to be configured | ||||||||||
| 16 | easily. | ||||||||||
| 17 | |||||||||||
| 18 | tie %host,'CfgTie::TieHost'; | ||||||||||
| 19 | |||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||
| 21 | |||||||||||
| 22 | This is a straightforward hash tie that allows us to access the host database | ||||||||||
| 23 | sanely. | ||||||||||
| 24 | |||||||||||
| 25 | =head2 Ties | ||||||||||
| 26 | |||||||||||
| 27 | There are two ties available for programmers: | ||||||||||
| 28 | |||||||||||
| 29 | =over 1 | ||||||||||
| 30 | |||||||||||
| 31 | =item C<tie %host,'CfgTie::TieHost'> | ||||||||||
| 32 | |||||||||||
| 33 | C<$host{$name}> will return a hash reference of the named host information. | ||||||||||
| 34 | |||||||||||
| 35 | =item C<tie %host_addr,'CfgTie::TieHost_addr'> | ||||||||||
| 36 | |||||||||||
| 37 | C<$host_addr{$addr}> will return a hash reference for the specified host. | ||||||||||
| 38 | |||||||||||
| 39 | =back | ||||||||||
| 40 | |||||||||||
| 41 | =head2 Structure of hash | ||||||||||
| 42 | |||||||||||
| 43 | Any given host entry has the following information assoicated with it: | ||||||||||
| 44 | |||||||||||
| 45 | =over 1 | ||||||||||
| 46 | |||||||||||
| 47 | =item C<Name> | ||||||||||
| 48 | |||||||||||
| 49 | Host name | ||||||||||
| 50 | |||||||||||
| 51 | =item C<Aliases> | ||||||||||
| 52 | |||||||||||
| 53 | Other names for this host | ||||||||||
| 54 | |||||||||||
| 55 | =item C<AddrType> | ||||||||||
| 56 | |||||||||||
| 57 | The type of address | ||||||||||
| 58 | |||||||||||
| 59 | =item C<Length> | ||||||||||
| 60 | |||||||||||
| 61 | =item C<Addrs> | ||||||||||
| 62 | |||||||||||
| 63 | A list reference of addresses. You will need something like | ||||||||||
| 64 | |||||||||||
| 65 | ($a,$b,$c,$d) = unpack('C4',$Addr); | ||||||||||
| 66 | |||||||||||
| 67 | to get the address out sanely. | ||||||||||
| 68 | |||||||||||
| 69 | =back | ||||||||||
| 70 | |||||||||||
| 71 | Additionally, the programmer can set any other associated key, but this | ||||||||||
| 72 | information will only be available to a running Perl script. | ||||||||||
| 73 | |||||||||||
| 74 | =head1 See Also | ||||||||||
| 75 | |||||||||||
| 76 | L<CfgTie::TieAliases>, L<CfgTie::TieGeneric>, L<CfgTie::TieGroup>, | ||||||||||
| 77 | L<CfgTie::TieMTab>, L<CfgTie::TieNamed>, L<CfgTie::TieNet>, | ||||||||||
| 78 | L<CfgTie::TiePh>, L<CfgTie::TieProto>, L<CfgTie::TieRCService>, | ||||||||||
| 79 | L<CfgTie::TieRsrc>, L<CfgTie::TieServ>, L<CfgTie::TieShadow>, | ||||||||||
| 80 | L<CfgTie::TieUser> | ||||||||||
| 81 | |||||||||||
| 82 | L<host(5)> | ||||||||||
| 83 | |||||||||||
| 84 | =head1 Caveats | ||||||||||
| 85 | |||||||||||
| 86 | The current version does cache some host information. | ||||||||||
| 87 | |||||||||||
| 88 | =head1 Author | ||||||||||
| 89 | |||||||||||
| 90 | Randall Maas (L<mailto:randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>) | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | sub TIEHASH | ||||||||||
| 95 | 0 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | 0 | my $node = {}; | |||||||||
| 98 | 0 | return bless $node, $self; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | sub FIRSTKEY | ||||||||||
| 102 | 0 | ▶ | { | ||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||
| 103 | 0 | my $self = shift; | |||||||||
| 104 | |||||||||||
| 105 | #Rewind outselves to the beginning. | ||||||||||
| 106 | 0 | sethostent 1; | |||||||||
| 107 | |||||||||||
| 108 | 0 | &NEXTKEY($self); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | sub NEXTKEY | ||||||||||
| 112 | 0 | ▶ | { | ||||||||
|
Subroutine
NEXTKEY: not called CC=2 SCAR=17.9
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | |||||||||||
| 115 | #Get the next host id in the database. | ||||||||||
| 116 | # Get the information from the system and store it for later | ||||||||||
| 117 | 0 | my @x = gethostent; | |||||||||
| 118 | 0 | ▶ | if (! scalar @x) {return;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 0 | &CfgTie::TieHost_rec'TIEHASH(0,@x); | |||||||||
| 121 | 0 | return $x[0]; #Corresponds to the name | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | sub EXISTS | ||||||||||
| 125 | 0 | ▶ | { | ||||||||
|
Subroutine
EXISTS: not called CC=3 SCAR=24.8
| |||||||||||
| 126 | 0 | my ($self,$name) = shift; | |||||||||
| 127 | 0 | ▶ | if (exists $CfgTie::TieHost_rec'by_name{$name}) {return 1;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | |||||||||||
| 129 | # Get the information from the system and store it for later | ||||||||||
| 130 | 0 | my @x = gethostbyname $name; | |||||||||
| 131 | 0 | ▶ | if (! scalar @x) {return 0;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | |||||||||||
| 133 | 0 | &CfgTie::TieHost_rec'TIEHASH(0,@x); | |||||||||
| 134 | 0 | return 1; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | sub FETCH | ||||||||||
| 138 | 0 | ▶ | { | ||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||
| 139 | 0 | my ($self, $name) = @_; | |||||||||
| 140 | |||||||||||
| 141 | #check out our cache first | ||||||||||
| 142 | 0 | ▶ | if (exists $CfgTie::TieHost_rec'by_name{$name}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | {return $CfgTie::TieHost_rec'by_name{$name};} | |||||||||
| 144 | |||||||||||
| 145 | 0 | my %X; | |||||||||
| 146 | 0 | tie %X, 'CfgTie::TieHost_rec', gethostbyname $name; | |||||||||
| 147 | 0 | return bless %X; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 1 | 1; | |||||||||
| 151 | #Bug creating, deleting hosts is not supported yet. | ||||||||||
| 152 | |||||||||||
| 153 | package CfgTie::TieHost_addr; | ||||||||||
| 154 | |||||||||||
| 155 | sub TIEHASH | ||||||||||
| 156 | 0 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: not called CC=1 SCAR=6.9
| |||||||||||
| 157 | 0 | my $self = shift; | |||||||||
| 158 | 0 | my $node = {}; | |||||||||
| 159 | 0 | return bless $node, $self; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | sub FIRSTKEY | ||||||||||
| 163 | 0 | ▶ | { | ||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||
| 164 | 0 | my $self = shift; | |||||||||
| 165 | |||||||||||
| 166 | #Rewind outselves to the beginning. | ||||||||||
| 167 | 0 | sethostent 1; | |||||||||
| 168 | |||||||||||
| 169 | 0 | &NEXTKEY($self); | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | sub NEXTKEY | ||||||||||
| 173 | 0 | ▶ | { | ||||||||
|
Subroutine
NEXTKEY: not called CC=2 SCAR=17.9
| |||||||||||
| 174 | 0 | my $self = shift; | |||||||||
| 175 | |||||||||||
| 176 | #Get the next host id in the database. | ||||||||||
| 177 | # Get the information from the system and store it for later | ||||||||||
| 178 | 0 | my @x = gethostent; | |||||||||
| 179 | 0 | ▶ | if (! scalar @x) {return;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | |||||||||||
| 181 | 0 | &CfgTie::TieHost_rec'TIEHASH(0,@x); | |||||||||
| 182 | 0 | return $x[5]; #Corresponds to the id | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | sub EXISTS | ||||||||||
| 186 | 0 | ▶ | { | ||||||||
|
Subroutine
EXISTS: not called CC=3 SCAR=24.8
| |||||||||||
| 187 | 0 | my ($self,$addr) = shift; | |||||||||
| 188 | 0 | ▶ | if (exists $CfgTie::TieHost_rec'by_addr{$addr}) {return 1;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | |||||||||||
| 190 | # Get the information from the system and store it for later | ||||||||||
| 191 | 0 | my @x = gethostbyaddr $addr, AF_INET; | |||||||||
| 192 | 0 | ▶ | if (! scalar @x) {return 0;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | |||||||||||
| 194 | 0 | &CfgTie::TieHost_rec'TIEHASH(0,@x); | |||||||||
| 195 | 0 | return 1; | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | sub FETCH | ||||||||||
| 199 | 0 | ▶ | { | ||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||
| 200 | 0 | my ($self,$addr) = @_; | |||||||||
| 201 | |||||||||||
| 202 | #check out our cache first | ||||||||||
| 203 | 0 | ▶ | if (exists $CfgTie::TieHost_rec'by_addr{$addr}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | 0 | {return $CfgTie::TieHost_rec'by_addr{$addr};} | |||||||||
| 205 | |||||||||||
| 206 | 0 | my %X; | |||||||||
| 207 | 0 | tie %X, 'CfgTie::TieHost_rec', gethostbyaddr($addr, AF_INET); | |||||||||
| 208 | 0 | return bless %X; | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | #Bug creating and modifying hosts is not supported yet. | ||||||||||
| 212 | |||||||||||
| 213 | package CfgTie::TieHost_rec; | ||||||||||
| 214 | # A package used by both host_addr and host to retain record information about | ||||||||||
| 215 | # a hoost. This is the only way to access hostmod. | ||||||||||
| 216 | |||||||||||
| 217 | #Two hashes are used for look up | ||||||||||
| 218 | # $by_name{$name} | ||||||||||
| 219 | # $by_addr{$addr} | ||||||||||
| 220 | |||||||||||
| 221 | sub TIEHASH | ||||||||||
| 222 | 0 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: not called CC=4 SCAR=30.0
| |||||||||||
| 223 | # Ties a single host to a register... | ||||||||||
| 224 | 0 | my $self = shift; | |||||||||
| 225 | 0 | my $Name = shift; | |||||||||
| 226 | 0 | my $Node; | |||||||||
| 227 | |||||||||||
| 228 | 0 | ▶ | if (exists $by_name{$Name}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 229 | { | ||||||||||
| 230 | #Just update our record... | ||||||||||
| 231 | 0 | $Node = $by_name{$Name}; | |||||||||
| 232 | } | ||||||||||
| 233 | # else create it | ||||||||||
| 234 | |||||||||||
| 235 | 0 | my ($Aliases,@Addrs); | |||||||||
| 236 | 0 | $Node->{Name} = $Name; | |||||||||
| 237 | 0 | ($Aliases, $Node->{AddrType}, @Addrs) = @_; | |||||||||
| 238 | 0 | $Node->{Aliases} = [split ',',$Aliases]; | |||||||||
| 239 | 0 | $Node->{Addrs} = \@Addrs; | |||||||||
| 240 | |||||||||||
| 241 | #Cross reference the names | ||||||||||
| 242 | 0 | my $I; | |||||||||
| 243 | 0 | foreach $I ($Name, $Node->{Aliases}) | |||||||||
| 244 | 0 | {$by_name{$I} = $Node;} | |||||||||
| 245 | |||||||||||
| 246 | #Cross reference the addresses | ||||||||||
| 247 | 0 | foreach $I (@Addrs) | |||||||||
| 248 | 0 | {$by_addr{$I} = $Node;} | |||||||||
| 249 | |||||||||||
| 250 | 0 | return bless $Node, $self; | |||||||||
| 251 | } | ||||||||||
| 252 | |||||||||||
| 253 | sub FIRSTKEY | ||||||||||
| 254 | 0 | ▶ | { | ||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||
| 255 | 0 | my $self = shift; | |||||||||
| 256 | 0 | my $a = keys %self; | |||||||||
| 257 | 0 | return scalar each %self; | |||||||||
| 258 | } | ||||||||||
| 259 | |||||||||||
| 260 | sub NEXTKEY | ||||||||||
| 261 | 0 | ▶ | { | ||||||||
|
Subroutine
NEXTKEY: not called CC=1 SCAR=6.9
| |||||||||||
| 262 | 0 | my $self = shift; | |||||||||
| 263 | 0 | return scalar each %self; | |||||||||
| 264 | } | ||||||||||
| 265 | |||||||||||
| 266 | sub EXISTS | ||||||||||
| 267 | 0 | ▶ | { | ||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||
| 268 | 0 | my ($self,$key) = shift; | |||||||||
| 269 | 0 | return exists $self{$key}; | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | sub FETCH | ||||||||||
| 273 | 0 | ▶ | { | ||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||
| 274 | 0 | my $self = shift; | |||||||||
| 275 | 0 | my $key = shift; | |||||||||
| 276 | |||||||||||
| 277 | 0 | ▶ | if (exists $self{$key}) {return $self{$key};} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 278 | } | ||||||||||
| 279 | |||||||||||
| 280 | sub STORE | ||||||||||
| 281 | 0 | ▶ | { | ||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||
| 282 | # Changes a setting for the specified host... we basically call hostmod | ||||||||||
| 283 | 0 | my ($self,$key,$val) = @_; | |||||||||
| 284 | |||||||||||
| 285 | #Extra setting that will be lost... 8( | ||||||||||
| 286 | 0 | $self{$key}=$val; | |||||||||
| 287 | } | ||||||||||
| 288 | |||||||||||
| 289 | sub DELETE | ||||||||||
| 290 | 0 | ▶ | { | ||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||
| 291 | #Deletes a host setting | ||||||||||
| 292 | 0 | my ($self, $key) = @_; | |||||||||
| 293 | |||||||||||
| 294 | #Just remove our local copy | ||||||||||
| 295 | 0 | delete $self{$key}; | |||||||||
| 296 | } | ||||||||||
| 297 | 1 | 1; | |||||||||