12 our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
15 @EXPORT = qw(getpwent getpwuid getpwnam getpw);
19 $pw_name $pw_passwd $pw_uid $pw_gid
20 $pw_gecos $pw_dir $pw_shell
21 $pw_expire $pw_change $pw_class
28 FIELDS => [ grep(/^\$pw_/, @EXPORT_OK), @EXPORT ],
29 ALL => [ @EXPORT, @EXPORT_OK ],
32 use vars grep /^\$pw_/, @EXPORT_OK;
35 # XXX: these mean somebody hacked this module's source
36 # without understanding the underlying assumptions.
38 my $IE = "[INTERNAL ERROR]";
40 # Class::Struct forbids use of @ISA
41 sub import { goto &Exporter::import }
43 use Class::Struct qw(struct);
44 struct 'User::pwent' => [
45 name => '$', # pwent[0]
46 passwd => '$', # pwent[1]
47 uid => '$', # pwent[2]
48 gid => '$', # pwent[3]
50 # you'll only have one/none of these three
51 change => '$', # pwent[4]
52 age => '$', # pwent[4]
53 quota => '$', # pwent[4]
55 # you'll only have one/none of these two
56 comment => '$', # pwent[5]
57 class => '$', # pwent[5]
59 # you might not have this one
60 gecos => '$', # pwent[6]
62 dir => '$', # pwent[7]
63 shell => '$', # pwent[8]
65 # you might not have this one
66 expire => '$', # pwent[9]
71 # init our groks hash to be true if the built platform knew how
72 # to do each struct pwd field that perl can ever under any circumstances
73 # know about. we do not use /^pw_?/, but just the tails.
75 our %Groks; # whether build system knew how to do this feature
77 pwage pwchange pwclass pwcomment
78 pwexpire pwgecos pwpasswd pwquota
82 my $short = $feep =~ /^pw(.*)/
85 # not cluck, as we know we called ourselves,
86 # and a confession is probably imminent anyway
87 warn("$IE $feep is a funny struct pwd field");
91 exists $Config{ "d_" . $feep }
92 || confess("$IE Configure doesn't d_$feep");
93 $Groks{$short} = defined $Config{ "d_" . $feep };
95 # assume that any that are left are always there
96 for my $feep (grep /^\$pw_/s, @EXPORT_OK) {
97 $feep =~ /^\$pw_(.*)/;
98 $Groks{$1} = 1 unless defined $Groks{$1};
102 # With arguments, reports whether one or more fields are all implemented
103 # in the build machine's struct pwd pw_*. May be whitespace separated.
104 # We do not use /^pw_?/, just the tails.
106 # Without arguments, returns the list of fields implemented on build
107 # machine, space separated in scalar context.
109 # Takes exception to being asked whether this machine's struct pwd has
110 # a field that Perl never knows how to provide under any circumstances.
111 # If the module does this idiocy to itself, the explosion is noisier.
114 our %Groks; # whether build system knew how to do this feature
116 my $sploder = caller() ne __PACKAGE__
118 : sub { confess("$IE @_") };
120 my @valid = sort grep { $Groks{$_} } keys %Groks;
121 return wantarray ? @valid : "@valid";
123 for my $feep (map { split } @_) {
124 defined $Groks{$feep}
125 || $sploder->("$feep is never a valid struct pwd field");
126 $cando &&= $Groks{$feep};
135 # Any that haven't been pw_had are assumed on "all" platforms of
136 # course, this may not be so, but you can't get here otherwise,
137 # since the underlying core call already took exception to your
140 $pw_name = $pwob->name ( $_[0] );
141 $pw_passwd = $pwob->passwd ( $_[1] ) if pw_has("passwd");
142 $pw_uid = $pwob->uid ( $_[2] );
143 $pw_gid = $pwob->gid ( $_[3] );
145 if (pw_has("change")) {
146 $pw_change = $pwob->change ( $_[4] );
148 elsif (pw_has("age")) {
149 $pw_age = $pwob->age ( $_[4] );
151 elsif (pw_has("quota")) {
152 $pw_quota = $pwob->quota ( $_[4] );
155 if (pw_has("class")) {
156 $pw_class = $pwob->class ( $_[5] );
158 elsif (pw_has("comment")) {
159 $pw_comment = $pwob->comment( $_[5] );
162 $pw_gecos = $pwob->gecos ( $_[6] ) if pw_has("gecos");
164 $pw_dir = $pwob->dir ( $_[7] );
165 $pw_shell = $pwob->shell ( $_[8] );
167 $pw_expire = $pwob->expire ( $_[9] ) if pw_has("expire");
172 sub getpwent ( ) { _populate(CORE::getpwent()) }
173 sub getpwnam ($) { _populate(CORE::getpwnam(shift)) }
174 sub getpwuid ($) { _populate(CORE::getpwuid(shift)) }
175 sub getpw ($) { ($_[0] =~ /^\d+\z/s) ? &getpwuid : &getpwnam }
184 User::pwent - by-name interface to Perl's built-in getpw*() functions
189 $pw = getpwnam('daemon') || die "No daemon user";
190 if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?\z#s ) {
191 print "gid 1 on root dir";
194 $real_shell = $pw->shell || '/bin/sh';
196 for (($fullname, $office, $workphone, $homephone) =
197 split /\s*,\s*/, $pw->gecos)
199 s/&/ucfirst(lc($pw->name))/ge;
202 use User::pwent qw(:FIELDS);
203 getpwnam('daemon') || die "No daemon user";
204 if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?\z#s ) {
205 print "gid 1 on root dir";
208 $pw = getpw($whoever);
210 use User::pwent qw/:DEFAULT pw_has/;
211 if (pw_has(qw[gecos expire quota])) { .... }
212 if (pw_has("name uid gid passwd")) { .... }
213 print "Your struct pwd has: ", scalar pw_has(), "\n";
217 This module's default exports override the core getpwent(), getpwuid(),
218 and getpwnam() functions, replacing them with versions that return
219 C<User::pwent> objects. This object has methods that return the
220 similarly named structure field name from the C's passwd structure
221 from F<pwd.h>, stripped of their leading "pw_" parts, namely C<name>,
222 C<passwd>, C<uid>, C<gid>, C<change>, C<age>, C<quota>, C<comment>,
223 C<class>, C<gecos>, C<dir>, C<shell>, and C<expire>. The C<passwd>,
224 C<gecos>, and C<shell> fields are tainted when running in taint mode.
226 You may also import all the structure fields directly into your
227 namespace as regular variables using the :FIELDS import tag. (Note
228 that this still overrides your core functions.) Access these fields
229 as variables named with a preceding C<pw_> in front their method
230 names. Thus, C<< $passwd_obj->shell >> corresponds to $pw_shell
231 if you import the fields.
233 The getpw() function is a simple front-end that forwards
234 a numeric argument to getpwuid() and the rest to getpwnam().
236 To access this functionality without the core overrides, pass the
237 C<use> an empty import list, and then access function functions
238 with their full qualified names. The built-ins are always still
239 available via the C<CORE::> pseudo-package.
241 =head2 System Specifics
243 Perl believes that no machine ever has more than one of C<change>,
244 C<age>, or C<quota> implemented, nor more than one of either
245 C<comment> or C<class>. Some machines do not support C<expire>,
246 C<gecos>, or allegedly, C<passwd>. You may call these methods
247 no matter what machine you're on, but they return C<undef> if
250 You may ask whether one of these was implemented on the system Perl
251 was built on by asking the importable C<pw_has> function about them.
252 This function returns true if all parameters are supported fields
253 on the build platform, false if one or more were not, and raises
254 an exception if you asked about a field that Perl never knows how
255 to provide. Parameters may be in a space-separated string, or as
256 separate arguments. If you pass no parameters, the function returns
257 the list of C<struct pwd> fields supported by your build platform's
258 C library, as a list in list context, or a space-separated string
259 in scalar context. Note that just because your C library had
260 a field doesn't necessarily mean that it's fully implemented on
263 Interpretation of the C<gecos> field varies between systems, but
264 traditionally holds 4 comma-separated fields containing the user's
265 full name, office location, work phone number, and home phone number.
266 An C<&> in the gecos field should be replaced by the user's properly
267 capitalized login C<name>. The C<shell> field, if blank, must be
268 assumed to be F</bin/sh>. Perl does not do this for you. The
269 C<passwd> is one-way hashed garble, not clear text, and may not be
270 unhashed save by brute-force guessing. Secure systems use more a
271 more secure hashing than DES. On systems supporting shadow password
272 systems, Perl automatically returns the shadow password entry when
273 called by a suitably empowered user, even if your underlying
274 vendor-provided C library was too short-sighted to realize it should
277 See passwd(5) and getpwent(3) for details.
281 While this class is currently implemented using the Class::Struct
282 module to build a struct-like class, you shouldn't rely upon this.
292 =item March 18th, 2000
294 Reworked internals to support better interface to dodgey fields
295 than normal Perl function provides. Added pw_has() field. Improved