Annotation of src/etc/security, Revision 1.100
1.1 cgd 1: #!/bin/sh -
2: #
1.100 ! tron 3: # $NetBSD: security,v 1.99 2006/09/23 04:07:01 jmcneill Exp $
1.9 cgd 4: # from: @(#)security 8.1 (Berkeley) 6/9/93
1.1 cgd 5: #
6:
1.9 cgd 7: PATH=/sbin:/usr/sbin:/bin:/usr/bin
1.1 cgd 8:
1.89 jmmv 9: rcvar_manpage='security.conf(5)'
10:
1.31 lukem 11: if [ -f /etc/rc.subr ]; then
12: . /etc/rc.subr
13: else
14: echo "Can't read /etc/rc.subr; aborting."
15: exit 1;
16: fi
17:
1.9 cgd 18: umask 077
1.64 cjs 19: TZ=UTC; export TZ
1.1 cgd 20:
1.15 mrg 21: if [ -s /etc/security.conf ]; then
22: . /etc/security.conf
23: fi
24:
1.67 lukem 25: # Set reasonable defaults (if they're not set in security.conf)
26: #
27: backup_dir=${backup_dir:-/var/backups}
28: pkgdb_dir=${pkgdb_dir:-/var/db/pkg}
29: max_loginlen=${max_loginlen:-8}
30: max_grouplen=${max_grouplen:-8}
31:
32: # Other configurable variables
33: #
34: special_files="/etc/mtree/special /etc/mtree/special.local"
35: MP=/etc/master.passwd
36: CHANGELIST=""
37: work_dir=$backup_dir/work
38:
39: if [ ! -d "$work_dir" ]; then
40: mkdir -p "$work_dir"
41: fi
42:
1.56 lukem 43: SECUREDIR=`mktemp -d /tmp/_securedir.XXXXXX` || exit 1
44:
1.67 lukem 45: trap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE
1.15 mrg 46:
1.56 lukem 47: if ! cd "$SECUREDIR"; then
48: echo "Can not cd to $SECUREDIR".
1.15 mrg 49: exit 1
50: fi
51:
1.91 lukem 52: ERR=err.$$
53: TMP1=tmp1.$$
54: TMP2=tmp2.$$
55: MPBYUID=mpbyuid.$$
56: MPBYPATH=mpbypath.$$
57: LIST=list.$$
58: OUTPUT=output.$$
59: LABELS=labels.$$
60: PKGS=pkgs.$$
61: CHANGEFILES=changefiles.$$
62: SPECIALSPEC=specialspec.$$
1.67 lukem 63:
1.15 mrg 64:
1.67 lukem 65: # migrate_file old new
66: # Determine if the "${old}" path name needs to be migrated to the
67: # "${new}" path. Also checks if "${old}.current" needs migrating,
68: # and if so, migrate it and possibly "${old}.current,v" and
69: # "${old}.backup".
70: #
71: migrate_file()
72: {
73: _old=$1
74: _new=$2
75: if [ -z "$_old" -o -z "$_new" ]; then
76: err 3 "USAGE: migrate_file old new"
77: fi
78: if [ ! -d "${_new%/*}" ]; then
79: mkdir -p "${_new%/*}"
80: fi
81: if [ -f "${_old}" -a ! -f "${_new}" ]; then
82: echo "==> migrating ${_old}"
83: echo " to ${_new}"
84: mv "${_old}" "${_new}"
85: fi
86: if [ -f "${_old}.current" -a ! -f "${_new}.current" ]; then
87: echo "==> migrating ${_old}.current"
88: echo " to ${_new}.current"
89: mv "${_old}.current" "${_new}.current"
90: if [ -f "${_old}.current,v" -a ! -f "${_new}.current,v" ]; then
91: echo "==> migrating ${_old}.current,v"
92: echo " to ${_new}.current,v"
93: mv "${_old}.current,v" "${_new}.current,v"
94: fi
95: if [ -f "${_old}.backup" -a ! -f "${_new}.backup" ]; then
96: echo "==> migrating ${_old}.backup"
97: echo " to ${_new}.backup"
98: mv "${_old}.backup" "${_new}.backup"
99: fi
100: fi
101: }
102:
103:
104: # backup_and_diff file printdiff
105: # Determine if file needs backing up, and if so, do it.
106: # If printdiff is yes, display the diffs, otherwise
107: # just print a message saying "[changes omitted]".
108: #
109: backup_and_diff()
110: {
111: _file=$1
112: _printdiff=$2
113: if [ -z "$_file" -o -z "$_printdiff" ]; then
114: err 3 "USAGE: backup_and_diff file printdiff"
115: fi
116: ! checkyesno _printdiff
117: _printdiff=$?
118:
119: _old=$backup_dir/${_file##*/}
120: case "$_file" in
121: $work_dir/*)
122: _new=$_file
123: migrate_file "$backup_dir/$_old" "$_new"
124: migrate_file "$_old" "$_new"
125: ;;
126: *)
127: _new=$backup_dir/$_file
128: migrate_file "$_old" "$_new"
129: ;;
130: esac
131: CUR=${_new}.current
132: BACK=${_new}.backup
133: if [ -f $_file ]; then
134: if [ -f $CUR ] ; then
135: if [ "$_printdiff" -ne 0 ]; then
1.83 jhawk 136: diff ${diff_options} $CUR $_file > $OUTPUT
1.67 lukem 137: else
138: if ! cmp -s $CUR $_file; then
139: echo "[changes omitted]"
140: fi > $OUTPUT
141: fi
142: if [ -s $OUTPUT ] ; then
143: printf \
144: "\n======\n%s diffs (OLD < > NEW)\n======\n" $_file
145: cat $OUTPUT
146: backup_file update $_file $CUR $BACK
147: fi
148: else
149: printf "\n======\n%s added\n======\n" $_file
150: if [ "$_printdiff" -ne 0 ]; then
1.83 jhawk 151: diff ${diff_options} /dev/null $_file
1.67 lukem 152: else
153: echo "[changes omitted]"
154: fi
155: backup_file add $_file $CUR $BACK
156: fi
157: else
158: if [ -f $CUR ]; then
159: printf "\n======\n%s removed\n======\n" $_file
160: if [ "$_printdiff" -ne 0 ]; then
1.83 jhawk 161: diff ${diff_options} $CUR /dev/null
1.67 lukem 162: else
163: echo "[changes omitted]"
164: fi
165: backup_file remove $_file $CUR $BACK
166: fi
167: fi
168: }
1.48 abs 169:
1.9 cgd 170:
1.67 lukem 171: # These are used several times.
172: #
1.91 lukem 173: awk -F: '!/^\+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
1.29 lukem 174: awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
1.91 lukem 175: for file in $special_files; do
176: [ -s $file ] && cat $file
177: done | mtree -CM -k all > $SPECIALSPEC || exit 1
1.9 cgd 178:
1.67 lukem 179:
1.9 cgd 180: # Check the master password file syntax.
1.32 lukem 181: #
1.31 lukem 182: if checkyesno check_passwd; then
1.85 jhawk 183: # XXX: the sense of permit_star is reversed; the code works as
184: # implemented, but usage needs to be negated.
1.81 jhawk 185: checkyesno check_passwd_permit_star && permit_star=0 || permit_star=1
1.94 jdolecek 186: checkyesno check_passwd_permit_nonalpha \
187: && permit_nonalpha=1 || permit_nonalpha=0
188:
1.81 jhawk 189: awk -v "len=$max_loginlen" \
190: -v "nowarn_shells_list=$check_passwd_nowarn_shells" \
191: -v "nowarn_users_list=$check_passwd_nowarn_users" \
1.94 jdolecek 192: -v "permit_star=$permit_star" \
193: -v "permit_nonalpha=$permit_nonalpha" \
194: '
1.25 lukem 195: BEGIN {
196: while ( getline < "/etc/shells" > 0 ) {
1.39 hubertf 197: if ($0 ~ /^\#/ || $0 ~ /^$/ )
1.25 lukem 198: continue;
199: shells[$1]++;
200: }
1.81 jhawk 201: split(nowarn_shells_list, a);
202: for (i in a) nowarn_shells[a[i]]++;
203: split(nowarn_users_list, a);
204: for (i in a) nowarn_users[a[i]]++;
205: uid0_users_list="root toor"
206: split(uid0_users_list, a);
207: for (i in a) uid0_users[a[i]]++;
1.25 lukem 208: FS=":";
209: }
210:
211: {
1.15 mrg 212: if ($0 ~ /^[ ]*$/) {
1.25 lukem 213: printf "Line %d is a blank line.\n", NR;
1.15 mrg 214: next;
215: }
1.34 abs 216: if (NF != 10 && ($1 != "+" || NF != 1))
1.25 lukem 217: printf "Line %d has the wrong number of fields.\n", NR;
1.34 abs 218: if ($1 == "+" ) {
219: if (NF != 1 && $3 == 0)
1.81 jhawk 220: printf "Line %d includes entries with uid 0.\n",
221: NR;
1.34 abs 222: next;
223: }
1.94 jdolecek 224: if (!permit_nonalpha &&
1.95 peter 225: $1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
1.25 lukem 226: printf "Login %s has non-alphanumeric characters.\n",
227: $1;
1.34 abs 228: if (length($1) > len)
1.81 jhawk 229: printf "Login %s has more than "len" characters.\n",
230: $1;
231: if ($2 == "" && !nowarn_users[$1])
232: printf "Login %s has no password.\n", $1;
233: if (!nowarn_shells[$10] && !nowarn_users[$1]) {
234: if (length($2) != 13 &&
235: length($2) != 20 &&
236: $2 !~ /^\$1/ &&
237: $2 !~ /^\$2/ &&
1.99 jmcneill 238: $2 !~ /^\$sha1/ &&
1.81 jhawk 239: $2 != "" &&
240: (permit_star || $2 != "*") &&
241: $2 !~ /^\*[A-z-]+$/ &&
242: $1 != "toor") {
243: if ($10 == "" || shells[$10])
244: printf "Login %s is off but still has "\
245: "a valid shell (%s)\n", $1, $10;
246: } else if (! shells[$10])
247: printf "Login %s does not have a valid "\
248: "shell (%s)\n", $1, $10;
249: }
250: if ($3 == 0 && !uid0_users[$1] && !nowarn_users[$1])
1.25 lukem 251: printf "Login %s has a user id of 0.\n", $1;
1.15 mrg 252: if ($3 < 0)
1.25 lukem 253: printf "Login %s has a negative user id.\n", $1;
1.15 mrg 254: if ($4 < 0)
1.25 lukem 255: printf "Login %s has a negative group id.\n", $1;
1.15 mrg 256: }' < $MP > $OUTPUT
257: if [ -s $OUTPUT ] ; then
258: printf "\nChecking the $MP file:\n"
259: cat $OUTPUT
260: fi
261:
262: awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
263: if [ -s $OUTPUT ] ; then
264: printf "\n$MP has duplicate user names.\n"
265: column $OUTPUT
266: fi
267:
1.37 wrstuden 268: # To not exclude 'toor', a standard duplicate root account, from the duplicate
269: # account test, uncomment the line below (without egrep in it)and comment
270: # out the line (with egrep in it) below it.
271: #
272: # < $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
1.36 wrstuden 273: < $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
1.15 mrg 274: if [ -s $TMP2 ] ; then
275: printf "\n$MP has duplicate user id's.\n"
276: while read uid; do
1.28 lukem 277: grep -w $uid $MPBYUID
1.15 mrg 278: done < $TMP2 | column
279: fi
1.9 cgd 280: fi
281:
282: # Check the group file syntax.
1.32 lukem 283: #
1.31 lukem 284: if checkyesno check_group; then
1.15 mrg 285: GRP=/etc/group
1.49 jdolecek 286: awk -F: -v "len=$max_grouplen" '{
1.15 mrg 287: if ($0 ~ /^[ ]*$/) {
1.25 lukem 288: printf "Line %d is a blank line.\n", NR;
1.15 mrg 289: next;
290: }
1.34 abs 291: if (NF != 4 && ($1 != "+" || NF != 1))
1.25 lukem 292: printf "Line %d has the wrong number of fields.\n", NR;
1.34 abs 293: if ($1 == "+" ) {
294: next;
295: }
1.95 peter 296: if ($1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
1.25 lukem 297: printf "Group %s has non-alphanumeric characters.\n",
298: $1;
1.49 jdolecek 299: if (length($1) > len)
300: printf "Group %s has more than "len" characters.\n", $1;
1.15 mrg 301: if ($3 !~ /[0-9]*/)
1.25 lukem 302: printf "Login %s has a negative group id.\n", $1;
1.15 mrg 303: }' < $GRP > $OUTPUT
304: if [ -s $OUTPUT ] ; then
305: printf "\nChecking the $GRP file:\n"
306: cat $OUTPUT
307: fi
308:
309: awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
310: if [ -s $OUTPUT ] ; then
311: printf "\n$GRP has duplicate group names.\n"
312: column $OUTPUT
313: fi
1.9 cgd 314: fi
315:
316: # Check for root paths, umask values in startup files.
317: # The check for the root paths is problematical -- it's likely to fail
318: # in other environments. Once the shells have been modified to warn
319: # of '.' in the path, the path tests should go away.
1.32 lukem 320: #
1.31 lukem 321: if checkyesno check_rootdotfiles; then
1.67 lukem 322: rhome=~root
1.15 mrg 323: umaskset=no
324: list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
325: for i in $list ; do
326: if [ -f $i ] ; then
1.67 lukem 327: if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
328: then
1.15 mrg 329: umaskset=yes
330: fi
1.63 lukem 331: # Double check the umask value itself; ensure that
1.67 lukem 332: # both the group and other write bits are set.
333: #
1.45 sommerfe 334: egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
1.63 lukem 335: awk '{
1.67 lukem 336: if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
1.80 wiz 337: print "\tRoot umask is group writable"
1.63 lukem 338: }
1.67 lukem 339: if ($2 ~ /[^2367]$/) {
1.80 wiz 340: print "\tRoot umask is other writable"
1.63 lukem 341: }
1.67 lukem 342: }' | sort -u
1.26 lukem 343: SAVE_PATH=$PATH
344: unset PATH
1.15 mrg 345: /bin/csh -f -s << end-of-csh > /dev/null 2>&1
346: source $i
347: /bin/ls -ldgT \$path > $TMP1
1.9 cgd 348: end-of-csh
1.76 atatat 349: export PATH=$SAVE_PATH
1.15 mrg 350: awk '{
351: if ($10 ~ /^\.$/) {
1.27 lukem 352: print "\tThe root path includes .";
1.15 mrg 353: next;
354: }
355: }
356: $1 ~ /^d....w/ \
1.80 wiz 357: { print "\tRoot path directory " $10 " is group writable." } \
1.15 mrg 358: $1 ~ /^d.......w/ \
1.80 wiz 359: { print "\tRoot path directory " $10 " is other writable." }' \
1.67 lukem 360: < $TMP1
1.15 mrg 361: fi
1.67 lukem 362: done > $OUTPUT
1.15 mrg 363: if [ $umaskset = "no" -o -s $OUTPUT ] ; then
1.27 lukem 364: printf "\nChecking root csh paths, umask values:\n$list\n\n"
1.15 mrg 365: if [ -s $OUTPUT ]; then
366: cat $OUTPUT
367: fi
368: if [ $umaskset = "no" ] ; then
1.27 lukem 369: printf "\tRoot csh startup files do not set the umask.\n"
1.15 mrg 370: fi
1.9 cgd 371: fi
372:
1.15 mrg 373: umaskset=no
1.23 lukem 374: list="/etc/profile ${rhome}/.profile"
1.15 mrg 375: for i in $list; do
376: if [ -f $i ] ; then
377: if egrep umask $i > /dev/null ; then
378: umaskset=yes
379: fi
380: egrep umask $i |
1.67 lukem 381: awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
1.80 wiz 382: { print "\tRoot umask is group writable" } \
1.67 lukem 383: $2 ~ /[^2367]$/ \
1.80 wiz 384: { print "\tRoot umask is other writable" }'
1.26 lukem 385: SAVE_PATH=$PATH
386: unset PATH
1.15 mrg 387: /bin/sh << end-of-sh > /dev/null 2>&1
388: . $i
1.26 lukem 389: list=\`echo \$PATH | /usr/bin/sed -e \
390: 's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
1.15 mrg 391: /bin/ls -ldgT \$list > $TMP1
1.9 cgd 392: end-of-sh
1.76 atatat 393: export PATH=$SAVE_PATH
1.15 mrg 394: awk '{
395: if ($10 ~ /^\.$/) {
1.27 lukem 396: print "\tThe root path includes .";
1.15 mrg 397: next;
398: }
399: }
400: $1 ~ /^d....w/ \
1.80 wiz 401: { print "\tRoot path directory " $10 " is group writable." } \
1.15 mrg 402: $1 ~ /^d.......w/ \
1.80 wiz 403: { print "\tRoot path directory " $10 " is other writable." }' \
1.67 lukem 404: < $TMP1
1.9 cgd 405:
1.15 mrg 406: fi
1.67 lukem 407: done > $OUTPUT
1.15 mrg 408: if [ $umaskset = "no" -o -s $OUTPUT ] ; then
409: printf "\nChecking root sh paths, umask values:\n$list\n"
410: if [ -s $OUTPUT ]; then
411: cat $OUTPUT
412: fi
413: if [ $umaskset = "no" ] ; then
1.27 lukem 414: printf "\tRoot sh startup files do not set the umask.\n"
1.15 mrg 415: fi
1.9 cgd 416: fi
417: fi
418:
419: # Root and uucp should both be in /etc/ftpusers.
1.32 lukem 420: #
1.31 lukem 421: if checkyesno check_ftpusers; then
1.28 lukem 422: list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
1.27 lukem 423: for i in $list; do
1.29 lukem 424: if /usr/libexec/ftpd -C $i ; then
1.67 lukem 425: printf "\t$i is not denied\n"
1.27 lukem 426: fi
1.67 lukem 427: done > $OUTPUT
1.28 lukem 428: if [ -s $OUTPUT ]; then
429: printf "\nChecking the /etc/ftpusers configuration:\n"
430: cat $OUTPUT
431: fi
1.9 cgd 432: fi
433:
1.43 itojun 434: # Uudecode should not be in the /etc/mail/aliases file.
1.32 lukem 435: #
1.31 lukem 436: if checkyesno check_aliases; then
1.43 itojun 437: for f in /etc/mail/aliases /etc/aliases; do
438: if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
439: printf "\nEntry for uudecode in $f file.\n"
440: fi
441: done
1.9 cgd 442: fi
443:
444: # Files that should not have + signs.
1.32 lukem 445: #
1.31 lukem 446: if checkyesno check_rhosts; then
1.15 mrg 447: list="/etc/hosts.equiv /etc/hosts.lpd"
448: for f in $list ; do
449: if [ -f $f ] && egrep '\+' $f > /dev/null ; then
450: printf "\nPlus sign in $f file.\n"
451: fi
452: done
453:
454: # Check for special users with .rhosts files. Only root and toor should
1.16 mikel 455: # have .rhosts files. Also, .rhosts files should not have plus signs.
1.15 mrg 456: awk -F: '$1 != "root" && $1 != "toor" && \
457: ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
1.20 mycroft 458: { print $1 " " $9 }' $MP |
1.19 mycroft 459: sort -k2 |
1.15 mrg 460: while read uid homedir; do
461: if [ -f ${homedir}/.rhosts ] ; then
462: rhost=`ls -ldgT ${homedir}/.rhosts`
1.46 christos 463: printf -- "$uid: $rhost\n"
1.15 mrg 464: fi
465: done > $OUTPUT
466: if [ -s $OUTPUT ] ; then
467: printf "\nChecking for special users with .rhosts files.\n"
468: cat $OUTPUT
469: fi
470:
471: while read uid homedir; do
1.35 fair 472: if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
1.41 christos 473: cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
1.46 christos 474: printf -- "$uid: + in .rhosts file.\n"
1.15 mrg 475: fi
1.29 lukem 476: done < $MPBYPATH > $OUTPUT
1.15 mrg 477: if [ -s $OUTPUT ] ; then
478: printf "\nChecking .rhosts files syntax.\n"
479: cat $OUTPUT
480: fi
1.9 cgd 481: fi
482:
483: # Check home directories. Directories should not be owned by someone else
1.80 wiz 484: # or writable.
1.32 lukem 485: #
1.31 lukem 486: if checkyesno check_homes; then
1.85 jhawk 487: checkyesno check_homes_permit_usergroups && \
488: permit_usergroups=1 || permit_usergroups=0
1.15 mrg 489: while read uid homedir; do
490: if [ -d ${homedir}/ ] ; then
491: file=`ls -ldgT ${homedir}`
1.46 christos 492: printf -- "$uid $file\n"
1.9 cgd 493: fi
1.29 lukem 494: done < $MPBYPATH |
1.85 jhawk 495: awk -v "usergroups=$permit_usergroups" '
496: $1 != $4 && $4 != "root" \
1.15 mrg 497: { print "user " $1 " home directory is owned by " $4 }
1.88 jdolecek 498: $2 ~ /^-....w/ && (!usergroups || $5 != $1) \
1.80 wiz 499: { print "user " $1 " home directory is group writable" }
1.15 mrg 500: $2 ~ /^-.......w/ \
1.80 wiz 501: { print "user " $1 " home directory is other writable" }' \
1.27 lukem 502: > $OUTPUT
1.15 mrg 503: if [ -s $OUTPUT ] ; then
504: printf "\nChecking home directories.\n"
505: cat $OUTPUT
506: fi
507:
508: # Files that should not be owned by someone else or readable.
1.67 lukem 509: list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
1.15 mrg 510: while read uid homedir; do
511: for f in $list ; do
512: file=${homedir}/${f}
513: if [ -f $file ] ; then
1.46 christos 514: printf -- "$uid $f `ls -ldgT $file`\n"
1.15 mrg 515: fi
516: done
1.29 lukem 517: done < $MPBYPATH |
1.85 jhawk 518: awk -v "usergroups=$permit_usergroups" '
519: $1 != $5 && $5 != "root" \
1.15 mrg 520: { print "user " $1 " " $2 " file is owned by " $5 }
1.85 jhawk 521: $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
1.15 mrg 522: { print "user " $1 " " $2 " file is group readable" }
523: $3 ~ /^-......r/ \
524: { print "user " $1 " " $2 " file is other readable" }
1.85 jhawk 525: $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
1.80 wiz 526: { print "user " $1 " " $2 " file is group writable" }
1.15 mrg 527: $3 ~ /^-.......w/ \
1.80 wiz 528: { print "user " $1 " " $2 " file is other writable" }' \
1.27 lukem 529: > $OUTPUT
1.15 mrg 530:
1.80 wiz 531: # Files that should not be owned by someone else or writable.
1.19 mycroft 532: list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
1.79 elric 533: .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
534: .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
535: .twmrc .xinitrc .xsession .ssh/authorized_keys \
536: .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
537: .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
538: .ssh/known_hosts2"
1.15 mrg 539: while read uid homedir; do
540: for f in $list ; do
541: file=${homedir}/${f}
542: if [ -f $file ] ; then
1.46 christos 543: printf -- "$uid $f `ls -ldgT $file`\n"
1.15 mrg 544: fi
545: done
1.29 lukem 546: done < $MPBYPATH |
1.85 jhawk 547: awk -v "usergroups=$permit_usergroups" '
548: $1 != $5 && $5 != "root" \
1.15 mrg 549: { print "user " $1 " " $2 " file is owned by " $5 }
1.85 jhawk 550: $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
1.80 wiz 551: { print "user " $1 " " $2 " file is group writable" }
1.15 mrg 552: $3 ~ /^-.......w/ \
1.80 wiz 553: { print "user " $1 " " $2 " file is other writable" }' \
1.27 lukem 554: >> $OUTPUT
1.15 mrg 555: if [ -s $OUTPUT ] ; then
556: printf "\nChecking dot files.\n"
557: cat $OUTPUT
558: fi
1.9 cgd 559: fi
560:
561: # Mailboxes should be owned by user and unreadable.
1.32 lukem 562: #
1.31 lukem 563: if checkyesno check_varmail; then
1.86 jhawk 564: ls -lA /var/mail | \
1.63 lukem 565: awk ' NR == 1 { next; }
1.86 jhawk 566: $9 ~ /^\./ {next; }
1.63 lukem 567: $3 != $9 {
568: print "user " $9 " mailbox is owned by " $3
569: }
570: $1 != "-rw-------" {
571: print "user " $9 " mailbox is " $1 ", group " $4
572: }' > $OUTPUT
1.15 mrg 573: if [ -s $OUTPUT ] ; then
574: printf "\nChecking mailbox ownership.\n"
575: cat $OUTPUT
576: fi
577: fi
578:
1.32 lukem 579: # NFS exports shouldn't be globally exported
580: #
581: if checkyesno check_nfs && [ -f /etc/exports ]; then
582: awk '{
1.22 lukem 583: # ignore comments and blank lines
1.39 hubertf 584: if ($0 ~ /^\#/ || $0 ~ /^$/ )
1.22 lukem 585: next;
1.100 ! tron 586: # manage line continuation
! 587: while ($NF ~ /^\\$/) {
! 588: $NF = "";
! 589: line = $0 "";
! 590: getline;
! 591: $0 = line $0 "";
! 592: }
1.22 lukem 593:
1.100 ! tron 594: delete dir;
! 595: readonly = ndir = 0;
! 596: for (i = 1; i <= NF; ++i) {
! 597: if ($i ~ /^\//) dir[ndir++] = $i;
! 598: else if ($i ~ /^-/) {
! 599: if ($i ~ /^-(ro|o)$/) readonly = 1;
! 600: if ($i ~ /^-network/) next;
! 601: }
! 602: else next;
1.15 mrg 603: }
604: if (readonly)
1.100 ! tron 605: for (item in dir)
! 606: rodir[nrodir++] = dir[item];
1.15 mrg 607: else
1.100 ! tron 608: for (item in dir)
! 609: rwdir[nrwdir++] = dir[item];
! 610:
! 611: }
! 612:
! 613: END {
! 614: if (nrodir) {
! 615: printf("Globally exported file system%s, read-only:\n",
! 616: nrodir > 1 ? "s" : "");
! 617: for (item in rodir)
! 618: printf("\t%s\n", rodir[item]);
! 619: }
! 620: if (nrwdir) {
! 621: printf("Globally exported file system%s, read-write:\n",
! 622: nrwdir > 1 ? "s" : "");
! 623: for (item in rwdir)
! 624: printf("\t%s\n", rwdir[item]);
! 625: }
1.32 lukem 626: }' < /etc/exports > $OUTPUT
627: if [ -s $OUTPUT ] ; then
1.15 mrg 628: printf "\nChecking for globally exported file systems.\n"
629: cat $OUTPUT
630: fi
1.9 cgd 631: fi
632:
633: # Display any changes in setuid files and devices.
1.32 lukem 634: #
1.31 lukem 635: if checkyesno check_devices; then
1.28 lukem 636: > $ERR
1.92 erh 637: (
1.98 lukem 638:
639: # Convert check_devices_ignore_fstypes="foo !bar bax"
640: # into "-fstype foo -o ! -fstype bar -o -fstype bax"
641: # and check_devices_ignore_paths="/foo !/bar /bax"
642: # into " -path /foo -o ! -path /bar -o -path /bax"
643: #
644: ignexpr=$(\
645: echo $check_devices_ignore_fstypes | \
646: sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
647: echo $check_devices_ignore_paths | \
648: sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
649: )
650:
651: # Massage the expression into ( $ignexpr ) -a -prune -o
652: if [ -n "${ignexpr}" ]; then
653: ignexpr=$(\
654: echo $ignexpr | \
655: sed -e 's/^-o /( /' \
656: -e 's/$/ ) -a -prune -o/' \
657: )
658: fi
659:
660: find / $ignexpr \
1.21 mycroft 661: \( \( -perm -u+s -a ! -type d \) -o \
662: \( -perm -g+s -a ! -type d \) -o \
1.24 lukem 663: -type b -o -type c \) -print0 | \
1.98 lukem 664: xargs -0 ls -ldgTq | sort +9 > $LIST
665:
666: ) 2> $OUTPUT
1.15 mrg 667:
668: # Display any errors that occurred during system file walk.
669: if [ -s $OUTPUT ] ; then
1.28 lukem 670: printf "Setuid/device find errors:\n" >> $ERR
671: cat $OUTPUT >> $ERR
672: printf "\n" >> $ERR
1.15 mrg 673: fi
674:
675: # Display any changes in the setuid file list.
676: egrep -v '^[bc]' $LIST > $TMP1
677: if [ -s $TMP1 ] ; then
678: # Check to make sure uudecode isn't setuid.
679: if grep -w uudecode $TMP1 > /dev/null ; then
1.28 lukem 680: printf "\nUudecode is setuid.\n" >> $ERR
1.15 mrg 681: fi
682:
1.67 lukem 683: file=$work_dir/setuid
684: migrate_file "$backup_dir/setuid" "$file"
685: CUR=${file}.current
686: BACK=${file}.backup
1.15 mrg 687: if [ -s $CUR ] ; then
688: if cmp -s $CUR $TMP1 ; then
689: :
690: else
691: > $TMP2
692: join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
693: if [ -s $OUTPUT ] ; then
1.28 lukem 694: printf "Setuid additions:\n" >> $ERR
695: tee -a $TMP2 < $OUTPUT >> $ERR
696: printf "\n" >> $ERR
1.15 mrg 697: fi
698:
699: join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
700: if [ -s $OUTPUT ] ; then
1.28 lukem 701: printf "Setuid deletions:\n" >> $ERR
702: tee -a $TMP2 < $OUTPUT >> $ERR
703: printf "\n" >> $ERR
1.15 mrg 704: fi
705:
1.20 mycroft 706: sort -k10 $TMP2 $CUR $TMP1 | \
1.27 lukem 707: sed -e 's/[ ][ ]*/ /g' | \
708: uniq -u > $OUTPUT
1.15 mrg 709: if [ -s $OUTPUT ] ; then
1.28 lukem 710: printf "Setuid changes:\n" >> $ERR
711: column -t $OUTPUT >> $ERR
712: printf "\n" >> $ERR
1.15 mrg 713: fi
1.9 cgd 714:
1.52 atatat 715: backup_file update $TMP1 $CUR $BACK
1.9 cgd 716: fi
1.15 mrg 717: else
1.28 lukem 718: printf "Setuid additions:\n" >> $ERR
719: column -t $TMP1 >> $ERR
720: printf "\n" >> $ERR
1.52 atatat 721: backup_file add $TMP1 $CUR $BACK
1.9 cgd 722: fi
1.15 mrg 723: fi
724:
1.27 lukem 725: # Check for block and character disk devices that are readable or
1.80 wiz 726: # writable or not owned by root.operator.
1.15 mrg 727: >$TMP1
1.61 lukem 728: DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
1.57 simonb 729: sd se ss uk up vnd wd xd xy"
1.27 lukem 730: # DISKLIST="$DISKLIST ct mt st wt"
1.15 mrg 731: for i in $DISKLIST; do
732: egrep "^b.*/${i}[0-9][0-9]*[a-p]$" $LIST >> $TMP1
733: egrep "^c.*/r${i}[0-9][0-9]*[a-p]$" $LIST >> $TMP1
734: done
735:
736: awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
1.25 lukem 737: { printf "Disk %s is user %s, group %s, permissions %s.\n", \
738: $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
1.15 mrg 739: if [ -s $OUTPUT ] ; then
1.28 lukem 740: printf "\nChecking disk ownership and permissions.\n" >> $ERR
741: cat $OUTPUT >> $ERR
742: printf "\n" >> $ERR
1.9 cgd 743: fi
744:
1.15 mrg 745: # Display any changes in the device file list.
1.20 mycroft 746: egrep '^[bc]' $LIST | sort -k11 > $TMP1
1.15 mrg 747: if [ -s $TMP1 ] ; then
1.67 lukem 748: file=$work_dir/device
749: migrate_file "$backup_dir/device" "$file"
750: CUR=${file}.current
751: BACK=${file}.backup
1.15 mrg 752:
753: if [ -s $CUR ] ; then
754: if cmp -s $CUR $TMP1 ; then
755: :
756: else
757: > $TMP2
758: join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
759: if [ -s $OUTPUT ] ; then
1.28 lukem 760: printf "Device additions:\n" >> $ERR
761: tee -a $TMP2 < $OUTPUT >> $ERR
762: printf "\n" >> $ERR
1.15 mrg 763: fi
764:
765: join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
766: if [ -s $OUTPUT ] ; then
1.28 lukem 767: printf "Device deletions:\n" >> $ERR
768: tee -a $TMP2 < $OUTPUT >> $ERR
769: printf "\n" >> $ERR
1.15 mrg 770: fi
771:
1.27 lukem 772: # Report any block device change. Ignore
773: # character devices, only the name is
774: # significant.
1.15 mrg 775: cat $TMP2 $CUR $TMP1 | \
1.27 lukem 776: sed -e '/^c/d' | \
777: sort -k11 | \
778: sed -e 's/[ ][ ]*/ /g' | \
779: uniq -u > $OUTPUT
1.15 mrg 780: if [ -s $OUTPUT ] ; then
1.28 lukem 781: printf "Block device changes:\n" >> $ERR
782: column -t $OUTPUT >> $ERR
783: printf "\n" >> $ERR
1.15 mrg 784: fi
1.9 cgd 785:
1.52 atatat 786: backup_file update $TMP1 $CUR $BACK
1.9 cgd 787: fi
1.15 mrg 788: else
1.28 lukem 789: printf "Device additions:\n" >> $ERR
790: column -t $TMP1 >> $ERR
791: printf "\n" >> $ERR
1.52 atatat 792: backup_file add $TMP1 $CUR $BACK >> $ERR
1.9 cgd 793: fi
1.28 lukem 794: fi
795: if [ -s $ERR ] ; then
796: printf "\nChecking setuid files and devices:\n"
797: cat $ERR
798: printf "\n"
1.9 cgd 799: fi
800: fi
801:
802: # Check special files.
803: # Check system binaries.
804: #
805: # Create the mtree tree specifications using:
1.67 lukem 806: # mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
1.38 kleink 807: # chown root:wheel DIR.secure
1.67 lukem 808: # chmod u+r,go= DIR.secure
1.9 cgd 809: #
810: # Note, this is not complete protection against Trojan horsed binaries, as
811: # the hacker can modify the tree specification to match the replaced binary.
812: # For details on really protecting yourself against modified binaries, see
813: # the mtree(8) manual page.
1.32 lukem 814: #
1.31 lukem 815: if checkyesno check_mtree; then
1.82 jhawk 816: if checkyesno check_mtree_follow_symlinks; then
817: check_mtree_flags="-L"
818: else
819: check_mtree_flags=""
820: fi
1.91 lukem 821: mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
1.87 jhawk 822: grep -v '^mtree: dev/tty: Device not configured$' >&2
1.15 mrg 823: if [ -s $OUTPUT ]; then
1.9 cgd 824: printf "\nChecking special files and directories.\n"
825: cat $OUTPUT
826: fi
827:
1.16 mikel 828: for file in /etc/mtree/*.secure; do
829: [ $file = '/etc/mtree/*.secure' ] && continue
1.9 cgd 830: tree=`sed -n -e '3s/.* //p' -e 3q $file`
1.82 jhawk 831: mtree $check_mtree_flags -f $file -p $tree > $TMP1
1.9 cgd 832: if [ -s $TMP1 ]; then
1.67 lukem 833: printf "\nChecking $tree:\n"
834: cat $TMP1
1.9 cgd 835: fi
1.67 lukem 836: done > $OUTPUT
1.15 mrg 837: if [ -s $OUTPUT ]; then
1.9 cgd 838: printf "\nChecking system binaries:\n"
839: cat $OUTPUT
840: fi
841: fi
842:
1.32 lukem 843: # Backup disklabels of available disks
844: #
845: if checkyesno check_disklabels; then
1.67 lukem 846: # migrate old disklabels
847: for file in `ls -1d $backup_dir/$backup_dir/disklabel.* \
848: $backup_dir/disklabel.* 2>/dev/null`; do
849: migrate_file "$file" "$work_dir/${file##*/}"
850: done
851:
852: # generate list of old disklabels & fdisks and remove them
853: ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
1.52 atatat 854: egrep -v '\.(backup|current)(,v)?$' > $LABELS
1.32 lukem 855: xargs rm < $LABELS
856:
1.96 rpaulo 857: # generate disklabels of all disks excluding: cd fd md st
1.97 veego 858: disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d|st|nfs/ { print $1; }'`
1.32 lukem 859: for i in $disks; do
1.67 lukem 860: disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
1.32 lukem 861: done
862:
1.67 lukem 863: # if fdisk is available, generate fdisks for: ed ld sd wd
864: if [ -x /sbin/fdisk ]; then
865: disks=`iostat -x| awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }'`
866: for i in $disks; do
867: /sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
868: done
869: fi
870:
871: # append list of new disklabels and fdisks
872: ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
1.52 atatat 873: egrep -v '\.(backup|current)(,v)?$' >> $LABELS
1.62 atatat 874: CHANGELIST="$LABELS $CHANGELIST"
875: fi
876:
877: # Check for changes in the list of installed pkgs
878: #
1.65 lukem 879: if checkyesno check_pkgs && [ -d $pkgdb_dir ]; then
1.67 lukem 880: pkgs=$work_dir/pkgs
881: migrate_file "$backup_dir/pkgs" "$pkgs"
1.65 lukem 882: ( cd $pkgdb_dir
1.62 atatat 883: pkg_info | sort
884: echo ""
885: find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
1.72 lukem 886: xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
1.62 atatat 887: ) > $pkgs
1.67 lukem 888: echo "$pkgs" > $PKGS
1.62 atatat 889: CHANGELIST="$PKGS $CHANGELIST"
1.32 lukem 890: fi
891:
1.67 lukem 892: # List of files that get backed up and checked for any modifications.
1.9 cgd 893: # Any changes cause the files to rotate.
1.32 lukem 894: #
1.67 lukem 895: if checkyesno check_changelist ; then
1.91 lukem 896: mtree -D -k type -f $SPECIALSPEC -E exclude |
897: sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
1.67 lukem 898:
1.75 lukem 899: (
1.68 lukem 900: # Add other files which might dynamically exist:
1.67 lukem 901: # /etc/ifconfig.*
902: # /etc/raid*.conf
1.68 lukem 903: # /etc/rc.d/*
1.67 lukem 904: # /etc/rc.conf.d/*
1.68 lukem 905: #
1.75 lukem 906: echo "/etc/ifconfig.*"
907: echo "/etc/raid*.conf"
908: echo "/etc/rc.d/*"
909: echo "/etc/rc.conf.d/*"
1.67 lukem 910:
1.68 lukem 911: # Add /etc/changelist
912: #
1.75 lukem 913: if [ -s /etc/changelist ]; then
914: grep -v '^#' /etc/changelist
915: fi
916: ) | while read file; do
917: case "$file" in
918: *[\*\?\[]*) # If changelist line is a glob ...
919: # ... expand possible backup files
920: #
921: ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
922: | sed "s,^$backup_dir/,, ; s,\.current$,,"
923:
924: # ... expand possible files
925: #
926: ls -1d $(echo $file) 2>/dev/null
927: ;;
928: *)
929: # Otherwise, just print the filename
930: echo $file
931: ;;
932: esac
933: done >> $CHANGEFILES
1.67 lukem 934: CHANGELIST="$CHANGEFILES $CHANGELIST"
935: fi
936:
937: # Special case backups, including the master password file and
938: # ssh private host keys. The normal backup mechanisms for
939: # $check_changelist (see below) also print out the actual file
940: # differences and we don't want to do that for these files
941: #
942: echo $MP > $TMP1 # always add /etc/master.passwd
1.91 lukem 943: mtree -D -k type -f $SPECIALSPEC -I nodiff |
944: sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
1.73 lukem 945: grep -v '^$' $TMP1 | sort -u > $TMP2
1.68 lukem 946:
1.69 lukem 947: while read file; do
1.67 lukem 948: backup_and_diff "$file" no
1.69 lukem 949: done < $TMP2
1.67 lukem 950:
1.32 lukem 951:
952: if [ -n "$CHANGELIST" ]; then
1.73 lukem 953: grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
1.68 lukem 954: comm -23 $TMP1 $TMP2 | while read file; do
1.67 lukem 955: backup_and_diff "$file" yes
1.9 cgd 956: done
1.44 ad 957: fi
958:
959: if [ -f /etc/security.local ]; then
1.90 kim 960: . /etc/security.local > $OUTPUT 2>&1
1.84 jhawk 961: if [ -s $OUTPUT ] ; then
962: printf "\nRunning /etc/security.local:\n"
963: cat $OUTPUT
964: fi
1.9 cgd 965: fi
CVSweb <webmaster@jp.NetBSD.org>