O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required