The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

PTools::SDF::File::Passwd - Get list of users via "/etc/passwd" or "ypcat passwd"

VERSION

This document describes version 0.05, released Nov 12, 2002.

SYNOPSIS

      use PTools::SDF::File::Passwd;
or    use PTools::SDF::File::Passwd qw( passwd );  # default: from /etc/passwd
or    use PTools::SDF::File::Passwd qw( NIS );     # alt: via "ypcat passwd"

Note: Only when the NIS format of "use PTools::SDF::File::Passwd" is employed is it possible to instantiate from an array of passwd entries. When this is done, the NIS password file is not loaded.

      $pwObj = new PTools::SDF::File::Passwd;
or    $pwObj = new PTools::SDF::File::Passwd( @arrayOfPasswdEntries );

The list of system users is, at this point, sorted in "uname" order. To process each entry, for example, use something like the following. Pick "$fieldName" from the "@PasswdFieldNames" list, defined below.

foreach $uname ( $pwObj->getUserList ) {

          (@passwdEnt) = $pwObj->getPwent( $uname );
  (or)    $passwdField = $pwObj->getPwent( $uname, $fieldName );

          (@gcosEntry) = $pwObj->getGcos( $uname );
          $gcosHashRef = $pwObj->parseGcos( @gcosEntry );

  (or)    $gcosHashRef = $pwObj->parseGcos( $uname );
}

Different installations define the "gcos" field in different ways. To redefine how the "gcos" field in the passwd entry is parsed, simply subclass this module and override the "parseGcos" method.

(@gcosFields) = $pwObj->getGcosFieldNames;

To determine field names currently defined, use the above method.

To re-sort, simply invoke the normal PTools::SDF::SDF "sort" method. This is probably not necessary. See the PTools::SDF::SDF module and the modules SDF::Sort::Quick and PTools::SDF::Sort::Shell for further details on sorting. (Note that "$mode" may or may not work, depending on the sorter used.) Pick "$keyFieldName" from the "@PasswdFieldNames" list, defined below.

$pwObj->sort( $mode, $keyFieldName );

Note that even after resorting the "getUserList" method will STILL return a list sorted in 'uname' order. To process using the new sort order, walk the list sequentially. For example,

foreach $rec ( 0 .. $pwObj->param ) {
     (@passwdEnt) = $pwObj->param($rec);
     (@gcosEntry) = $pwObj->param($rec,'gcos');
     $gcosHashRef = $pwObj->parseGcos( @gcosEntry );
}

DESCRIPTION

Constructor

new

Collect a list of Unix user entries from /etc/passwd, an NIS passwd map or an array of entries in passwd format.

      use PTools::SDF::File::Passwd;
or    use PTools::SDF::File::Passwd qw( passwd );  # default: from /etc/passwd
or    use PTools::SDF::File::Passwd qw( NIS );     # alt: via "ypcat passwd"

Note: Only when the NIS format of "use PTools::SDF::File::Passwd" is employed is it possible to instantiate from an array of passwd entries. When this is done, the NIS password file is not loaded.

      $pwObj = new PTools::SDF::File::Passwd;
or    $pwObj = new PTools::SDF::File::Passwd( @arrayOfPasswdEntries );

Methods

getUserList

Return a list (array) of each Uname defined in the current object. This is useful for iterating through the entire list of passwd entries.

foreach $uname ( $pwObj->getUserList ) { . . .  }
getPwent ( Uname, FieldName )

Search for a particular entry from the list of passwd entries and return the given FieldName portion of the entry (if the Uname lookup was successful.

    (@passwdEnt) = $pwObj->getPwent( $uname );
or  $passwdField = $pwObj->getPwent( $uname, $fieldName );
getAcctStat ( Uname )
acctActive ( Uname )
acctDisabled ( Uname )

Determine the status of a given user's passwd entry. This assumes that the first character of the passwd field will be set to an asterisk ("*") character to signify a disabled account.

$acctStat = $pwObj->getAcctStat( $uname );   # returns "Active" or "Disabled"

if ($pwObj->acctActive( $uname ))   { ... }

if ($pwObj->acctDisabled( $uname )) { ... }

WARN: If additional characters are used in the local passwd file to indicate a disabled account, this module must be subclassed and the necessary methods overridden.

getGcos ( Uname )

Search for a particular entry from the list of passwd entries and return the GCOS portion (if an entry is found).

(@gcosEntry) = $pwObj->getGcos( $uname );
getGcosFieldNames ( [ HashRef ] )

Different installations define the "gcos" field in different ways. Use this method to obtain a list of the field names that this module (or any subclasses thereof) uses to store the various components within the GCOS field.

(@gcosFields) = $pwObj->getGcosFieldNames;
parseGcos ( { Uname | GcosEntry } )

Return a hash reference containing data parsed from the GCOS field within a passwd entry.

Different installations define the "gcos" field in different ways. To redefine how the "gcos" field in the passwd entry is parsed, simply subclass this module and override the "parseGcos" method.

Examples:

$gcosHashRef = $pwObj->parseGcos( $uname );

(@gcosEntry) = $pwObj->getGcos( $uname );
$gcosHashRef = $pwObj->parseGcos( @gcosEntry );
sort ( Params )

The sort method defined in the PTools::SDF::SDF class is overriden here to ensure the uname index is recreated after a sort.

The Params here will vary, based on the currently loaded sort module.

save

The save method defined in the PTools::SDF::SDF class is overriden here to prevent rewriting the passwd file. This module implements read only access to passwd data.

INHERITANCE

This class inherits from both PTools::SDF::IDX and PTools::SDF::ARRAY. The PTools::SDF::ARRAY class inherits from PTools::SDF::SDF which, in turn, inherits from PTools::SDF::File.

SEE ALSO

For additional methods see PTools::SDF::IDX, PTools::SDF::ARRAY, PTools::SDF::SDF and PTools::SDF::File.

For documentation on the various sorting classes available see PTools::SDF::Sort::Bubble, PTools::SDF::Sort::Quick and PTools::SDF::Sort::Shell.

AUTHOR

Chris Cobb, <nospamplease@ccobb.net>

COPYRIGHT

Copyright (c) 2002-2007 by Chris Cobb. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.