1 | #------------------------------------------------------------------------------- |
---|
2 | # (C) British Crown Copyright 2006-17 Met Office. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | #------------------------------------------------------------------------------- |
---|
19 | |
---|
20 | use strict; |
---|
21 | use warnings; |
---|
22 | |
---|
23 | package FCM::Admin::Users::Passwd; |
---|
24 | use base qw{FCM::Class::CODE}; |
---|
25 | |
---|
26 | use FCM::Admin::Config; |
---|
27 | use FCM::Admin::User; |
---|
28 | use Text::ParseWords qw{shellwords}; |
---|
29 | |
---|
30 | my %ACTION_OF = ( |
---|
31 | get_users_info => \&_get_users_info, |
---|
32 | verify_users => \&_verify_users, |
---|
33 | ); |
---|
34 | |
---|
35 | __PACKAGE__->class({}, {action_of => {%ACTION_OF}}); |
---|
36 | |
---|
37 | my $CONFIG = FCM::Admin::Config->instance(); |
---|
38 | |
---|
39 | # Gets a HASH of users using the POSIX password DB. |
---|
40 | # %user_of = ($name => <FCM::Admin::User instance>, ...) |
---|
41 | sub _get_users_info { |
---|
42 | my ($attrib_ref, @only_users) = @_; |
---|
43 | if (@only_users) { |
---|
44 | return _get_only_users_info($attrib_ref, @only_users); |
---|
45 | } |
---|
46 | my $gid_max = $CONFIG->get_passwd_gid_max(); |
---|
47 | my $uid_max = $CONFIG->get_passwd_uid_max(); |
---|
48 | my $gid_min = $CONFIG->get_passwd_gid_min(); |
---|
49 | my $uid_min = $CONFIG->get_passwd_uid_min(); |
---|
50 | my %user_of; |
---|
51 | USER: |
---|
52 | while (my ($name, $uid, $gid, $gecos) = (getpwent())[0, 2, 3, 6]) { |
---|
53 | if ( exists($user_of{$name}) |
---|
54 | || defined($uid_max) && $uid > $uid_max || $uid < $uid_min |
---|
55 | || defined($gid_max) && $gid > $gid_max || $gid < $gid_min |
---|
56 | || !$gecos |
---|
57 | || (@only_users && grep {$_ eq $name} @only_users) |
---|
58 | ) { |
---|
59 | next USER; |
---|
60 | } |
---|
61 | my $email = _guess_email_from_gecos($attrib_ref, $gecos); |
---|
62 | if (!$email) { |
---|
63 | next USER; |
---|
64 | } |
---|
65 | $user_of{$name} = FCM::Admin::User->new({ |
---|
66 | name => $name, |
---|
67 | display_name => (split(q{,}, $gecos))[0], |
---|
68 | email => $email, |
---|
69 | }); |
---|
70 | } |
---|
71 | endpwent(); |
---|
72 | return \%user_of; |
---|
73 | } |
---|
74 | |
---|
75 | # Gets a HASH of users matching @only_users using the POSIX password DB. |
---|
76 | # %user_of = ($name => <FCM::Admin::User instance>, ...) |
---|
77 | sub _get_only_users_info { |
---|
78 | my ($attrib_ref, @only_users) = @_; |
---|
79 | my %user_of; |
---|
80 | for my $user (@only_users) { |
---|
81 | my ($name, $gecos) = (getpwnam($user))[0, 6]; |
---|
82 | if ($name && $gecos) { |
---|
83 | $user_of{$name} = FCM::Admin::User->new({ |
---|
84 | name => $name, |
---|
85 | display_name => (split(q{,}, $gecos))[0], |
---|
86 | email => _guess_email_from_gecos($attrib_ref, $gecos), |
---|
87 | }); |
---|
88 | } |
---|
89 | } |
---|
90 | return (wantarray() ? %user_of : \%user_of); |
---|
91 | } |
---|
92 | |
---|
93 | # Guess a user's email from gecos information |
---|
94 | sub _guess_email_from_gecos { |
---|
95 | my ($attrib_ref, $gecos) = @_; |
---|
96 | my $domain = $CONFIG->get_passwd_email_domain(); |
---|
97 | my $name = index($gecos, q{,}) > 0 ? (split(q{,}, $gecos))[0] : $gecos; |
---|
98 | return ($name =~ qr{\s}msx) |
---|
99 | ? undef : lc($name) . ($domain ? '@' . $domain : q{}); |
---|
100 | } |
---|
101 | |
---|
102 | # Return a list of bad users in @users. |
---|
103 | sub _verify_users { |
---|
104 | my ($attrib_ref, @users) = @_; |
---|
105 | grep {!getpwnam($_)} @users; |
---|
106 | } |
---|
107 | |
---|
108 | 1; |
---|
109 | __END__ |
---|
110 | |
---|
111 | =head1 NAME |
---|
112 | |
---|
113 | FCM::Admin::Users::Passwd |
---|
114 | |
---|
115 | =head1 SYNOPSIS |
---|
116 | |
---|
117 | use FCM::Admin::Users::Passwd; |
---|
118 | my $users_info_util = FCM::Admin::Users::Passwd->new(); |
---|
119 | $users_info_util->get_users(); |
---|
120 | |
---|
121 | =head1 DESCRIPTION |
---|
122 | |
---|
123 | Utility for obtaining user information from passwd information. |
---|
124 | |
---|
125 | =head1 METHODS |
---|
126 | |
---|
127 | =over 4 |
---|
128 | |
---|
129 | =item $util->get_users_info() |
---|
130 | |
---|
131 | Return a HASH (in list context) or a reference to a HASH (in scalar context) |
---|
132 | {name => <FCM::Admin::User instance>, ...}. The HASH should contain all entries |
---|
133 | in the passwd database that appear to be real users. |
---|
134 | |
---|
135 | =item $util->verify_users(@users) |
---|
136 | |
---|
137 | Return a list of bad users in @users. |
---|
138 | |
---|
139 | =back |
---|
140 | |
---|
141 | =head1 COPYRIGHT |
---|
142 | |
---|
143 | E<169> Crown copyright Met Office. All rights reserved. |
---|
144 | |
---|
145 | =cut |
---|