#!/usr/local/bin/perl -w
use AcctInfo;
my $acct = new AcctInfo;
$acct->SetUser("root"); $acct->cache; $acct->dump; exit;
That script would dump some info about the root account. The amount and
quality of info would depend on how many features your operating system
reports. By default, the cache()
method tries to get as much
info as possible and store it for easy access.
Example output from the program above:
Username : root Password : H7axm7Qd0ezas UID : 0 GID : 1 Name : 0000-Admin(0000) Home Dir : / Shell : /sbin/sh File Quota: File Usage: Disk Quota: Disk Usage: Exipration: none Exp Date : Wed Dec 31 19:00:00 1969 Change : 10161 Today : 10216 Today Date: Wed Dec 31 21:50:16 1969 DaysLeft : infinity Min : Max : Warn : Inactive : Flag : MyStatus : MyTime : Forward : "|/usr/local/bin/procmail"
Not all that interesting, but read on.
Of course, if you develop a script to report on, say, account expiration dates, and you try to run it on a system that doesn't have any sense of account expiration, you will encounter trouble. So, your mileage may vary.
In this early version, the module is read-only by default. You shouldn't be able to damage anything by running it. That's
mostly because the code for manipulating /etc/passwd
and /etc/shadow
is still experimental, and must be manually turned on. See NOTES for more information.
Carp.pm is required (it's standard in Perl 5.003 and up, anyway).
Root access to the system on which you want to use this library may be necessary. Without root access, you will only have access to a subset of the information that would normally be available. Specifically, you cannot read the shadow password file (if there is one) or the disk quotas without it.
You MUST read the README file which is included in the distribtuion. It may contain information which ammends, corrects, or otherwise impacts what you will read in the remainder of the documentation.
% perl Makefile.PL % make % make test % make install
As a convention, you may notice that most accessor functions begin with ``Get'' and most functions which change or set things begin with ``Set''. Of course, there are exceptions.
These are listed in no particular order. I should probably either group them by logical function or alphabetize them.
Example: my $acct = new AcctInfo;
debug()
toggles debugging messages. By default, debugging is
turned
off. Turn it on if you are developing AcctInfo or having trouble with it.
debug()
understands all of the folloing:
Enable Disable ------ ------- 1 0 'yes' 'no' 'on' 'off'
If you contact me for help, I'll likely ask you for some debugging output.
Example: $acct->debug;
cache()
attempts to gather as much info about the current
account as possible. You must have set the username via a call to
SetUser() before calling cache().
cache()
should give you a performance boost. You should be
aware that cache()
may be called automatically by some
methods.
Example: $acct->cache;
reset()
will clear all of the current object's internal data
structures. Rather than creating lots of AcctInfo objects and using each
only once, you may want to recycle them by calling reset()
and
then cache() after you've done a SetUser(). It depends on your needs.
Example: $acct->reset; $acct->SetUser("jzawodn"); $acct->cache;
dump()
exists for debugging purposes. When invoked, the
current object will print out all the current account info in a simple
format.
Example: $acct->dump;
Commit()
must be called if you change any user attributes and
want to make them really happen. Generally, you'll need to call
Commit()
after using any of the Set* functions (except
SetUser()).
Commit()
is generally smart enough to only bother updating
files that require updating. For example, if you change a user's .forward
using
SetForward(), Commit()
won't bother to re-write /etc/passwd and/or
/etc/shadow, since those files don't care about mail forwarding.
Example: $acct->SetForward("/dev/null"); $acct->Commit();
SetUser()
is what you call to tell the current instance of
AccInfo which account you'd like to do stuff with. Just pass a username. If
the account does not exist, it will return a false value.
Example: $acct->SetUser("root)";
GetAllUsers()
returns a list of all the usernames. They are in
the order that your system returns them, which will either mean (a) the
order they appear in /etc/passwd, or (b) the semi-random order from
NIS/Yellow Pages. If you only want ``real'' users, see
GetNonSystemUsers().
Example: my @users = $acct->GetAllUsers;
The default values for loUid and hiUid are 100 and 60,000 respectively.
Example: my @realusers = $acct->GetNonSystemUsers;
If there is demand, I can implement some of that processing in this module.
See also: passwd
Example: print "$acct->Uname is really $acct->GetRealName";
Example: $acct ->-SetUser("porky"); $acct->cache; $acct->SetRealName("Porky the Pig");)
Example: print "Your shell is $acct->GetShell";
Example: print "Your uid is $acct->GetUid";
Example: print "Your primary gid is $acct->GetGid";
Example: my $homedir = $acct->GetHomeDir;
Example: if ($acct->VerifyPassword("PaSsWoRd!")) { print "You guessed it!\n"; }
Example: my $encpasswd = $acct->GetPassword;
Example: my $lastchange = $acct->GetLastChange;
Used to parse the entries in the password file.
Used to parse the entries in the shadow password file.
A version of getpwnam()
that works on the shadow password
file.
Example: print "You have $acct->GetDaysLeft days left.";
Example: print "Your account is set to exipire on $acct->GetExpireDate";
Example: print "Your mail is forwarded to $acct->GetFoward";
Example: $acct->SetForward("/dev/null");
Example: if($acct->HasShadow) { print "Yeay! You have a shadow password file.\n"; }
Used to chop decimals off of numbers after division.
All of these are accessed like $acct->variable
where
variable comes from the list below.
Variable Description ======== =========== Uname username Uid uid Gid gid Passwd Encrypted passwordn Homedir Home directory Name real name Shell shell Fquota file quota Fusage file usage Dquota disk quota Dusage disk usage Change last change Expire when the account expires (numeric) ExpireDate date when the account expires Today what today is (for comparing Expire) TodayDate what today is (for comparing with ExpireDate) DaysLeft how many days the user has left (Expire - Today) Min the min field from /etc/shadow Max the max field from /etc/shadow Warn the warn field from /etc/shadow Inactive the inactive field from /etc/shadow Flag the flag field from /etc/shadow Forward the contents of the user's .forward file (if exists)
You should always free to browse the source and try to figure it out own your own. I didn't try to hide anything (much).
This controls how many sanity/safety checks occur when critical files are
being updated. For the puposes of this discussion, critical files are /etc/passwd
and /etc/shadow
. That may change to include more files/features in the future.
Any feedback on this measure is greatly appreciated.
This controls weather or not AcctInfo will let you do things which are currently considered experimental. The exact meaning of ``experimental'' will change in subsequent releases as the code sees more testing and has proven itself a bit.
In this release, the following items are experimental:
Updating /etc/passwd when Commit()
is called.
Thanks to Eric Johansson <esj@harvee.billerica.ma.us> for some ideas about the Get/Set/Commit implementation. Eric may be working on a FreeBSD version as well.