Tue, 22 Sep 2009 20:26:20 +0200
Allow underscore in account names
5 | 1 | #!/bin/bash |
2 | # | |
3 | ############################################################################# | |
7 | 4 | # Copyright (C) 2005-2009 |
5 | 5 | # |
7 | 6 | # Michiel Broek <mbse@mbse.eu> |
5 | 7 | # Beekmansbos 10 |
8 | # 1971 BV IJmuiden | |
9 | # the Netherlands | |
10 | # | |
11 | # This file is part of SlackSecCheckSripts. | |
12 | # | |
13 | # This package is free software; you can redistribute it and/or modify it | |
14 | # under the terms of the GNU General Public License as published by the | |
15 | # Free Software Foundation; either version 2, or (at your option) any | |
16 | # later version. | |
17 | # | |
18 | # SlackSecCheckSripts is distributed in the hope that it will be useful, but | |
19 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | # General Public License for more details. | |
22 | # | |
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with MBSE BBS; see the file COPYING. If not, write to the Free | |
25 | # Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
26 | ############################################################################# | |
27 | ||
28 | ||
29 | PATH=/sbin:/usr/sbin:/bin:/usr/bin | |
30 | ||
31 | umask 077 | |
32 | TZ=UTC; export TZ | |
33 | LANG=C; export LANG | |
34 | ||
35 | max_loginlen=${max_loginlen:-32} | |
36 | ||
37 | MP=/etc/passwd | |
38 | SP=/etc/shadow | |
39 | ||
40 | ||
41 | SECUREDIR=`mktemp -d /tmp/_securedir.XXXXXX` || exit 1 | |
42 | ||
43 | trap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE | |
44 | ||
45 | if ! cd "$SECUREDIR"; then | |
46 | echo "Can not cd to $SECUREDIR". | |
47 | exit 1 | |
48 | fi | |
49 | ||
50 | TMP2=secure1.$$ | |
51 | MPBYUID=secure2.$$ | |
52 | COMBINED=secure3.$$ | |
53 | OUTPUT=secure4.$$ | |
54 | ||
9 | 55 | |
5 | 56 | # Combine passwd and shadow files. |
57 | # | |
9 | 58 | JOPT=$( join --help | grep "\--nocheck-order") |
59 | if [ -z "$JOPT" ]; then | |
60 | join -t : -j 1 $MP $SP > $COMBINED | |
61 | else | |
62 | join --nocheck-order -t : -j 1 $MP $SP > $COMBINED | |
63 | fi | |
5 | 64 | |
65 | ||
66 | # These are used several times. | |
67 | # | |
68 | awk -F: '!/^+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID | |
69 | ||
70 | ||
71 | # Check the master password file syntax. | |
72 | # Usernames may have a $ character at the end for Samba | |
73 | # machine and trust accounts. | |
74 | # | |
75 | awk -v "len=$max_loginlen" ' | |
76 | BEGIN { | |
77 | while ( getline < "/etc/shells" > 0 ) { | |
78 | if ($0 ~ /^\#/ || $0 ~ /^$/ ) | |
79 | continue; | |
80 | shells[$1]++; | |
81 | } | |
82 | FS=":"; | |
83 | } | |
84 | ||
85 | { | |
86 | if ($0 ~ /^[ ]*$/) { | |
87 | printf "\tLine %d is a blank line.\n", NR; | |
88 | next; | |
89 | } | |
90 | if (NF != 15 && ($1 != "+" || NF != 1)) | |
91 | printf "\tLine %d has the wrong number of fields.\n", NR; | |
92 | if ($1 == "+" ) { | |
93 | if (NF != 1 && $3 == 0) | |
94 | printf "\tLine %d includes entries with uid 0.\n", NR; | |
95 | next; | |
96 | } | |
13 | 97 | if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9_]*[A-Za-z0-9\$])*$/) |
5 | 98 | printf "\tLogin %s has non-alphanumeric characters.\n", $1; |
99 | if (length($1) > len) | |
100 | printf "\tLogin %s has more than "len" characters.\n", $1; | |
101 | if ($7 == "" && $8 !~ /!/ && $8 != "*") | |
102 | printf "\tLogin %s does not have a shell\n", $1; | |
103 | if ($7 != "" && ! shells[$7] && $8 !~ /!/ && $8 != "*") | |
104 | printf "\tLogin %s does not have a valid shell (%s)\n", $1, $7; | |
105 | if ($7 != "" && shells[$7] && ($8 ~ /!/ && $8 = "*")) | |
106 | printf "\tLogin %s account is locked.\n", $1; | |
107 | if ($8 == "") | |
108 | printf "\tLogin %s has no password.\n", $1; | |
109 | if ($9 == "0") | |
110 | printf "\tLogin %s password is expired.\n", $1; | |
111 | if ($3 == 0 && $1 != "root" && $1 != "toor") | |
112 | printf "\tLogin %s has a user id of 0.\n", $1; | |
113 | if ($3 < 0) | |
114 | printf "\tLogin %s has a negative user id.\n", $1; | |
115 | if ($4 < 0) | |
116 | printf "\tLogin %s has a negative group id.\n", $1; | |
117 | }' < $COMBINED > $OUTPUT | |
118 | if [ -s $OUTPUT ] ; then | |
119 | printf "\nChecking the $MP and $SP files:\n" | |
120 | cat $OUTPUT | |
121 | fi | |
122 | ||
123 | awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT | |
124 | if [ -s $OUTPUT ] ; then | |
125 | printf "\n$MP has duplicate user names.\n" | |
126 | column $OUTPUT | |
127 | fi | |
128 | ||
129 | # To not exclude 'toor', a standard duplicate root account, from the duplicate | |
130 | # account test, uncomment the line below (without egrep in it)and comment | |
131 | # out the line (with egrep in it) below it. | |
132 | # | |
133 | #< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2 | |
134 | < $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2 | |
135 | if [ -s $TMP2 ] ; then | |
136 | printf "\n$MP has duplicate user id's.\n" | |
137 | while read uid; do | |
138 | grep -w $uid $MPBYUID | |
139 | done < $TMP2 | column | |
140 | fi | |
141 | ||
142 |