16.1. Getting Password and Group Information

The information that the UNIX system keeps about your username and user ID is fairly public. In fact, nearly everything but your unencrypted password is available for perusal by any program that cares to scan the /etc/passwd file. This file has a particular format, defined in passwd (5), which looks something like this:

name:passwd:uid:gid:gcos:dir:shell

The fields are defined as follows:

name

The login name of the user

passwd

The encrypted password, or something simple if a shadow password file is being used

uid

The user ID number (0 for root, nonzero for normal users)

gid

The default login group (group 0 may be privileged, but not necessarily)

gcos

The GCOS field, which typically contains the user's full name followed by a comma and some other information

dir

The home directory (where you go when you type cd without any arguments and where most of your "dot-files" are kept)

shell

Your login shell, typically /bin/sh or /bin/csh (or maybe even /usr/bin/perl, if you're crazy)

A typical portion of the password file looks like this:

fred:*:123:15:Fred Flintstone,,,:/home/fred:/bin/csh
barney:*:125:15:Barney Rubble,,,:/home/barney:/bin/csh

Now, Perl has enough tools to parse this kind of line easily (using split, for example), without drawing on special purpose routines. But the UNIX programing library does have a set of special routines: getpwent (3), getpwuid (3), getpwnam (3), and so on. These routines are available in Perl ...

Get Learning Perl, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.