New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Passwd.pm in vendors/FCM-2017.10.0/lib/FCM/Admin/Users – NEMO

source: vendors/FCM-2017.10.0/lib/FCM/Admin/Users/Passwd.pm @ 10672

Last change on this file since 10672 was 10672, checked in by nicolasmartin, 5 years ago

Reimport latest FCM release

File size: 4.2 KB
Line 
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
20use strict;
21use warnings;
22
23package FCM::Admin::Users::Passwd;
24use base qw{FCM::Class::CODE};
25
26use FCM::Admin::Config;
27use FCM::Admin::User;
28use Text::ParseWords qw{shellwords};
29
30my %ACTION_OF = (
31    get_users_info => \&_get_users_info,
32    verify_users   => \&_verify_users,
33);
34
35__PACKAGE__->class({}, {action_of => {%ACTION_OF}});
36
37my $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>, ...)
41sub _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>, ...)
77sub _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
94sub _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.
103sub _verify_users {
104    my ($attrib_ref, @users) = @_;
105    grep {!getpwnam($_)} @users;
106}
107
1081;
109__END__
110
111=head1 NAME
112
113FCM::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
123Utility for obtaining user information from passwd information.
124
125=head1 METHODS
126
127=over 4
128
129=item $util->get_users_info()
130
131Return 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
133in the passwd database that appear to be real users.
134
135=item $util->verify_users(@users)
136
137Return a list of bad users in @users.
138
139=back
140
141=head1 COPYRIGHT
142
143E<169> Crown copyright Met Office. All rights reserved.
144
145=cut
Note: See TracBrowser for help on using the repository browser.