/usr/share/perl5/Unix/AliasFile.pm is in libunix-configfile-perl 0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | package Unix::AliasFile;
# $Id: AliasFile.pm,v 1.5 2000/05/02 15:50:11 ssnodgra Exp $
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use Unix::ConfigFile;
use Text::ParseWords;
require Exporter;
@ISA = qw(Unix::ConfigFile Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);
$VERSION = '0.06';
# Implementation Notes
#
# This module adds one field to the basic ConfigFile object. The field
# is called 'alias' and is a hash of hashes. The key is the alias name and
# the subhash contains members of the alias as keys. The values of those
# keys are normally just '1', but may be object references in the case of
# :include: aliases. The module also makes use of the file sequencing
# facility provided by ConfigFile to preserve comments and keep the file in
# its original order.
# Preloaded methods go here.
# Read in the data structures from the supplied file
sub read {
my ($this, $fh) = @_;
my $alias = ""; # Current alias being processed
while (<$fh>) {
if (/^#/ || /^$/) { # Comments/Blank Lines
$this->seq_append($_);
}
elsif (/^[^\s]/) { # Alias start
s/,?\s*$//;
($alias, my $rhs) = split /:\s*/, $_, 2;
# I use the parse_line routine from Text::ParseWords here because
# a simple split would hose program aliases with embedded commas.
# Note that this routine does not exist prior to 5.005, so older
# perl versions will have the comma bug.
my @members;
if ($] >= 5.005) {
@members = parse_line(',\s*', 1, $rhs);
}
else {
@members = split /,\s*/, $rhs;
}
# This weird little hack fixes a bug that caused empty aliases
# to be deleted when the file was read, since the alias method
# won't actually create an empty alias.
$this->alias($alias, "empty");
$this->remove_user($alias, "empty");
$this->alias($alias, @members);
}
elsif (/^\s+$/) { # Junk whitespace
$this->seq_append("\n");
}
elsif (/^\s+/ && $alias) { # Alias continuation
s/,?\s*$//;
s/^\s+//;
if ($] >= 5.005) {
$this->add_user($alias, parse_line(',\s*', 1, $_));
}
else {
$this->add_user($alias, split /,\s*/);
}
}
else { # What's this?
die "Bogus line: $_";
}
}
return 1;
}
# Add, modify or get an alias
sub alias {
my $this = shift;
my $name = shift;
# If no more parameters, we return alias members
unless (@_) {
return undef unless defined $this->{alias}{$name};
return keys %{$this->{alias}{$name}} unless wantarray;
return sort keys %{$this->{alias}{$name}};
}
# Create or modify an alias
$this->seq_append("_ALIAS_ $name") unless defined $this->{alias}{$name};
$this->{alias}{$name} = {};
$this->add_user($name, @_);
return keys %{$this->{alias}{$name}} unless wantarray;
return sort keys %{$this->{alias}{$name}};
}
# Delete an alias
sub delete {
my ($this, $name) = @_;
return 0 unless defined $this->{alias}{$name};
$this->seq_remove("_ALIAS_ $name");
delete $this->{alias}{$name};
return 1;
}
# Delete aliases with no members
sub delempty {
my $this = shift;
my $count = 0;
foreach my $name ($this->aliases) {
unless ($this->alias($name)) {
$this->delete($name);
$count++;
}
}
return $count;
}
# Add users to an existing alias
sub add_user {
my $this = shift;
my $name = shift;
my @aliases = ($name eq "*") ? $this->aliases : ($name);
foreach (@aliases) {
return 0 unless defined $this->{alias}{$_};
foreach my $user (@_) {
$this->{alias}{$_}{$user} = 1;
}
}
return 1;
}
# Remove users from an existing alias
sub remove_user {
my $this = shift;
my $name = shift;
my @aliases = ($name eq "*") ? $this->aliases : ($name);
foreach (@aliases) {
return 0 unless defined $this->{alias}{$_};
foreach my $user (@_) {
delete $this->{alias}{$_}{$user};
}
}
return 1;
}
# Rename a user
sub rename_user {
my ($this, $oldname, $newname) = @_;
my $count = 0;
foreach ($this->aliases) {
if (exists $this->{alias}{$_}{$oldname}) {
delete $this->{alias}{$_}{$oldname};
$this->{alias}{$_}{$newname} = 1;
$count++;
}
}
return $count;
}
# Return the list of aliases
sub aliases {
my $this = shift;
wantarray ? sort keys %{$this->{alias}} : keys %{$this->{alias}};
}
# Add a comment before an alias
sub comment {
my ($this, $name, @cmnt) = @_;
grep { chomp; s/$/\n/; } @cmnt;
return $this->seq_insert("_ALIAS_ $name", @cmnt);
}
# Remove a comment
sub uncomment {
my ($this, $cmnt) = @_;
chomp $cmnt;
$cmnt =~ s/$/\n/;
return $this->seq_remove($cmnt);
}
# Output file to disk
sub write {
my ($this, $fh) = @_;
foreach my $seq ($this->sequence) {
unless ($seq =~ /^_ALIAS_ ([^\s]+)$/) {
print $fh $seq or return 0;
next;
}
my $name = $1;
my @users = $this->alias($name);
next if !defined @users;
print $fh $this->joinwrap(80, "$name: ", "\t", ",", ",", @users), "\n"
or return 0;
}
return 1;
}
# Autoload methods go after =cut, and are processed by the autosplit program.
1;
__END__
# Below is the stub of documentation for your module. You better edit it!
=head1 NAME
Unix::AliasFile - Perl interface to /etc/aliases format files
=head1 SYNOPSIS
use Unix::AliasFile;
$al = new Unix::AliasFile "/etc/aliases";
$al->alias("bozos", @members);
$al->delete("deadlist");
$al->remove_user("coolmail", "bgates", "badguy");
$al->add_user("coolmail", "joecool", "goodguy");
$al->remove_user("*", "deadguy");
$al->commit();
undef $al;
=head1 DESCRIPTION
The Unix::AliasFile module provides an abstract interface to Unix alias files.
It automatically handles file locking, getting colons and commas in the right
places, and all the other niggling details.
Unlike some of the other Unix::*File modules, this module will preserve the
order of your alias file, with a few exceptions. Comments and aliases will
appear in the file in the same order that they started in, unless you have
comment lines interspersed between the beginning of an alias and continuation
lines for that same alias. In this case, those comments will appear after the
alias that contains them.
=head1 METHODS
=head2 add_user( ALIAS, @USERS )
This method will add the list of users to an existing alias. Users that are
already members of the alias are silently ignored. The special alias name *
will add the users to every alias. Returns 1 on success or 0 on failure.
=head2 alias( ALIAS [,@USERS] )
This method can add, modify, or return information about an alias. Supplied
with a single alias parameter, it will return a list consisting of the members
of that alias, or undef if no such alias exists. If you supply more
parameters, the named alias will be created or modified if it already exists.
The member list is also returned to you in this case.
=head2 aliases( )
This method returns a list of all existing aliases. The list will be sorted
in alphabetical order. In scalar context, this method returns the total
number of aliases.
=head2 comment( ALIAS, COMMENT )
This method inserts a comment line before the specified alias. You must
supply your own comment marker (#) but a newline will be automatically
appended to the comment unless it already has one. Returns 1 on success
and 0 on failure.
=head2 commit( [BACKUPEXT] )
See the Unix::ConfigFile documentation for a description of this method.
=head2 delempty( )
This method will delete all existing aliases that have no members. It returns
a count of how many aliases were deleted.
=head2 delete( ALIAS )
This method will delete the named alias. It has no effect if the supplied
alias does not exist.
=head2 new( FILENAME [,OPTIONS] )
See the Unix::ConfigFile documentation for a description of this method.
=head2 remove_user( ALIAS, @USERS )
This method will remove the list of users from an existing alias. Users that
are not members of the alias are silently ignored. The special alias name *
will remove the users from every alias. Returns 1 on success or 0 on failure.
=head2 rename_user( OLDNAME, NEWNAME )
This method will change one username to another in every alias. Returns the
number of aliases affected.
=head2 uncomment( COMMENT )
Remove the comment from the file that matches the supplied text. The match
must be exact. Returns 1 on success and 0 on failure.
=head1 BUGS
While the Unix::AliasFile module will work with Perl versions prior to 5.005,
it may exhibit a minor bug under those versions. The bug will cause program
aliases with embedded comma characters to be broken apart. This will not
happen under 5.005 and up, due to the use of the Text::ParseWords module,
which changed significantly with the 5.005 release.
=head1 AUTHOR
Steve Snodgrass, ssnodgra@fore.com
=head1 SEE ALSO
Unix::AutomountFile, Unix::ConfigFile, Unix::GroupFile, Unix::PasswdFile
=cut
|