Unix::PasswdFileOps - Operations on Unix Passwd file
use Unix::PasswdFileOps; my $pass = Unix::PasswdFileOps->new('passwd' => '/etc/passwd'); print $pass->passwd(); $pass->verbose(1); $pass->protect_zero(1); if($pass->sort_passwd()) { print $pass->error; }
This module will perform sorting on a standard UNIX passwd file, the sort is performed against the UID by default although this can be altered using the sort_field() function.
Additionally it can populate an internal hash of arrays with line information this provides a nice interface to find information about user accounts.
my $passwd = Unix::PasswdFileOps->new('passwd' => '/etc/passwd');
You can pass in the optional parameters 'passwd', 'protect_zero' 'verbose' and 'sort_field'
my $passwd_file = $pass->passwd(); # or $pass->passwd('/etc/passwd');
This function will allow you to view or set the passwd file parameter, if you pass a parameter it will set the passwd file to that parameter, if you do not it will just return the currently set file.
my $zero = $pass->protect_zero(); # or $pass->protect_zero(1);
This function will allow you to view or set the protect_zero parameter, if you pass a parameter it will set the protect_zero option to 1, if you do not it will just return the currently set parameter.
If the protect_zero option is set all 0 uids will be unsorted and left at the top of the file, this is useful if you have more than one user with UID 0.
my $zero = $pass-unprotect_zero(); # or $pass->unprotect_zero(1);
This function will allow you to view or unset the protect_zero parameter, if you pass a parameter it will set the protect_zero option to undef, if you do not it will just return the currently set parameter.
my $verbose = $pass->verbose(); # or $pass->verbose(1);
This function will allow you to view or set the verbose parameter, if you pass a parameter it will set the verbose option to that parameter, if you do not it will just return the currently set parameter.
This is only useful during the passwd_sort function output will be printed to screen if this is enabled
my $sort_field = $pass->sort_field(); # or $pass->sort_field(1);
This function will allow you to view or set the sort_field parameter, if you pass a parameter it will set the sort_field option to that parameter, if you do not it will just return the currently set parameter.
The sort_field option determines which field to sort the passwd file by, the default is field 2 the UID field.
my $err = $pass->error();
This function will allow you to view any error messages
my $zero = $pass->sort_passwd(); # or $pass->sort_passwd();
This function performs a sort on the current passwd file, technically this is a safe process the sort type is determined by the sort_field option or the default field 2 (array element 2) this has been tested on Linux, Solaris 8, and almost 200 System V R 4.3 machines. It should be safe on any standard unix passwd file.
The file is read into the function and the sorted output is written back to the file, it is suggested that you create a backup of the passwd file before running this function.
$pass->populate_stats();
This function will populate an internal hash of arrays with the contents of the passwd file. You can access these using the following:
$pass->{'file_stats'}->{'username'}->{'list'}[0];
You can substitute 'username' with 'uid', 'gid', 'fullname', 'homedir', and 'shell'.
man passwd
Ben Maynard, <cpan@geekserv.com> <http://www.benmaynard.com>
Copyright (C) 2006-2011 by Ben Maynard, <cpan@geekserv.com> <http://www.benmaynard.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
To install Unix::PasswdFileOps, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Unix::PasswdFileOps
CPAN shell
perl -MCPAN -e shell install Unix::PasswdFileOps
For more information on module installation, please visit the detailed CPAN module installation guide.