Handle hostnames with upper-case letters
[webmin.git] / useradmin / linux-lib.pl
1 # linux-lib.pl
2 # Functions for reading linux format last output
3
4 # passfiles_type()
5 # Returns 0 for old-style passwords (/etc/passwd only), 1 for FreeBSD-style
6 # (/etc/master.passwd) and 2 for SysV (/etc/passwd & /etc/shadow)
7 sub passfiles_type
8 {
9 return &password_file($config{'shadow_file'}) ? 2 : 0;
10 }
11
12 # groupfiles_type()
13 # Returns 0 for normal group file (/etc/group only) and 2 for shadowed
14 # (/etc/group and /etc/gshadow)
15 sub groupfiles_type
16 {
17 return &password_file($config{'gshadow_file'}) ? 2 : 0;
18 }
19
20 # open_last_command(handle, user)
21 sub open_last_command
22 {
23 local ($fh, $user) = @_;
24 open($fh, "last $user |");
25 }
26
27 # read_last_line(handle)
28 # Parses a line of output from last into an array of
29 #  user, tty, host, login, logout, period
30 sub read_last_line
31 {
32 $fh = $_[0];
33 while(1) {
34         chop($line = <$fh>);
35         if (!$line) { return (); }
36         if ($line =~ /system boot/) { next; }
37         if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)?\s+(\S+\s+\S+\s+\d+\s+\d+:\d+)\s+\-\s+(\S+)\s+\((\d+:\d+)\)/) {
38                 return ($1, $2, $3, $4, $5 eq "down" ? "Shutdown" : $5, $6);
39                 }
40         elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)?\s+(\S+\s+\S+\s+\d+\s+\d+:\d+)\s+still/) {
41                 return ($1, $2, $3, $4);
42                 }
43         }
44 }
45
46 # os_most_recent_logins()
47 # Returns hash ref from username to the most recent login as time string
48 sub os_most_recent_logins
49 {
50 my %rv;
51 &clean_language();
52 open(LASTLOG, "lastlog |");
53 while(<LASTLOG>) {
54         s/\r|\n//g;
55         if (/^(\S+)/) {
56                 my $user = $1;
57                 if (/((\S+)\s+(\S+)\s+\d+\s+(\d+):(\d+):(\d+)\s+([\-\+]\d+)\s+(\d+))/) {
58                         # Have a date to parse
59                         $rv{$user} = $1;
60                         }
61                 else {
62                         $rv{$user} = undef;
63                         }
64                 }
65         }
66 close(LASTLOG);
67 &reset_environment();
68 return \%rv;
69 }
70
71 # logged_in_users()
72 # Returns a list of hashes containing details of logged-in users
73 sub logged_in_users
74 {
75 local @rv;
76 open(WHO, "who |");
77 while(<WHO>) {
78         if (/^(\S+)\s+(\S+)\s+(\S+\s+\d+\s+\d+:\d+)\s+(\((\S+)\))?/) {
79                 push(@rv, { 'user' => $1, 'tty' => $2,
80                             'when' => $3, 'from' => $5 });
81                 }
82         }
83 close(WHO);
84 return @rv;
85 }
86
87 # use_md5()
88 # Returns 1 if pam is set up to use MD5 encryption, 2 for blowfish, 3 for SHA512
89 sub use_md5
90 {
91 if (defined($use_md5_cache)) {
92         # Don't re-look this up
93         return $use_md5_cache;
94         }
95 local $md5 = 0;
96 if (&foreign_check("pam")) {
97         # Use the PAM module if we can
98         &foreign_require("pam", "pam-lib.pl");
99         local @conf = &foreign_call("pam", "get_pam_config");
100         local ($svc) = grep { $_->{'name'} eq 'passwd' } @conf;
101         LOOP:
102         foreach my $m (@{$svc->{'mods'}}) {
103                 if ($m->{'type'} eq 'password') {
104                         if ($m->{'args'} =~ /md5/) {
105                                 $md5 = 1;
106                                 }
107                         elsif ($m->{'args'} =~ /sha512/) {
108                                 $md5 = 3;
109                                 }
110                         elsif ($m->{'args'} =~ /blowfish/) {
111                                 $md5 = 2;
112                                 }
113                         elsif ($m->{'module'} =~ /pam_stack\.so/ &&
114                                $m->{'args'} =~ /service=(\S+)/) {
115                                 # Referred to another service!
116                                 ($svc) = grep { $_->{'name'} eq $1 } @conf;
117                                 if ($svc) { goto LOOP }
118                                 else { last; }
119                                 }
120                         elsif ($m->{'control'} eq 'include') {
121                                 # Include another section
122                                 ($svc) = grep { $_->{'name'} eq $m->{'module'} }
123                                               @conf;
124                                 if ($svc) { goto LOOP }
125                                 else { last; }
126                                 }
127                         }
128                 elsif ($m->{'include'}) {
129                         # Include another section, with @ syntax
130                         ($svc) = grep { $_->{'name'} eq $m->{'include'} } @conf;
131                         if ($svc) { goto LOOP }
132                         else { last; }
133                         }
134                 }
135         }
136 if (!$md5 && &open_readfile(PAM, "/etc/pam.d/passwd")) {
137         # Otherwise try to check the PAM file directly
138         while(<PAM>) {
139                 s/#.*$//g;
140                 if (/^password.*md5/) { $md5 = 1; }
141                 elsif (/^password.*blowfish/) { $md5 = 2; }
142                 elsif (/^password.*sha512/) { $md5 = 3; }
143                 }
144         close(PAM);
145         }
146 if (!$md5 && (&open_readfile(PAM, "/etc/pam.d/common-password") ||
147               &open_readfile(PAM, "/etc/pam.d/system-auth"))) {
148         # Then try reading common password config file
149         while(<PAM>) {
150                 s/#.*$//g;
151                 if (/^password.*md5/) { $md5 = 1; }
152                 elsif (/^password.*blowfish/) { $md5 = 2; }
153                 elsif (/^password.*sha512/) { $md5 = 3; }
154                 }
155         close(PAM);
156         }
157 if (&open_readfile(DEFS, "/etc/login.defs")) {
158         # The login.defs file is used on debian sometimes
159         while(<DEFS>) {
160                 s/#.*$//g;
161                 $md5 = 1 if (/MD5_CRYPT_ENAB\s+yes/i);
162                 }
163         close(DEFS);
164         }
165 $use_md5_cache = $md5;
166 return $md5;
167 }
168
169 1;