summaryrefslogtreecommitdiff
path: root/scripts/show-users.pl
blob: d47a535a9f5f12d791fb81bdb26784a025a261bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/perl

# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Stephen Hemminger
# Date: Sept 2009
# Description: Show password accounts
# 
# **** End License ****

use lib "/opt/vyatta/share/perl5";
use Term::ANSIColor;
use Vyatta::Config;

use strict;
use warnings;

sub usage {
    print "Usage: $0 {type}\n";
    print " type := all | vyatta | locked | other | color\n";
    exit 1;
}

my %pw;
setpwent();
while ( my ($u, $p) = getpwent()) {
    $pw{$u} = $p;
}
endpwent();

my $cfg = new Vyatta::Config;
$cfg->setLevel('system login user');
$cfg->{_active_dir_base} = '/opt/vyatta/config/active/';
my %vuser = map { $_ => 1 } $cfg->listOrigNodes();

sub locked {
    return grep { length($pw{$_}) == 1 } @_;
}

sub nopasswd {
    return grep { length($pw{$_}) == 0 } @_;
}

sub all {
    return @_;
}

sub vyatta {
    return grep { $vuser{$_} } @_;
}

sub other {
    return grep { length($pw{$_}) > 1 && ! defined($vuser{$_}) } @_;
}

sub login_color {
    my $u = shift;
    my $p = $pw{$u};
    my $c;

    if (length($p) == 0) {
	$c = 'blink red';	# open no password!
    } elsif ($vuser{$u}) {
	$c = 'green';		# vyatta user
    } elsif (length($p) == 1) {
	$c = 'blue';		# locked account
    } else {
	$c = 'yellow';		# non vyatta account
    }
    return color($c) . $u . color('reset');
}

# show non-locked accounts in color
sub colorize {
    return map { login_color($_) } grep { length($pw{$_}) != 1 } @_;
}

my %filters = (
    'all'	=> \&all,
    'vyatta'	=> \&vyatta,
    'locked'	=> \&locked,
    'open'	=> \&nopasswd,
    'other'	=> \&other,
    'color'	=> \&colorize,
);

for (@ARGV) {
    my $func = $filters{$_};
    unless ($func) {
	warn "Unknown type $_\n";
	usage();
    }
    print join("\n", sort($func->(keys %pw))), "\n";
}