Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

715 lines
19 KiB

28 years ago
  1. #!/bin/sh -
  2. #
  3. # $OpenBSD: security,v 1.68 2004/08/25 19:59:29 millert Exp $
  4. # from: @(#)security 8.1 (Berkeley) 6/9/93
  5. #
  6. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  7. umask 077
  8. DIR=`mktemp -d /tmp/_secure.XXXXXXXXXX` || exit 1
  9. ERR=$DIR/_secure1
  10. TMP1=$DIR/_secure2
  11. TMP2=$DIR/_secure3
  12. TMP3=$DIR/_secure4
  13. LIST=$DIR/_secure5
  14. OUTPUT=$DIR/_secure6
  15. trap 'rm -rf $DIR; exit 1' 0 1 2 3 13 15
  16. # Check the master password file syntax.
  17. MP=/etc/master.passwd
  18. awk -F: '{
  19. if ($0 ~ /^[ ]*$/) {
  20. printf("Line %d is a blank line.\n", NR);
  21. next;
  22. }
  23. if (NF != 10)
  24. printf("Line %d has the wrong number of fields:\n%s\n", NR, $0);
  25. if ($1 ~ /^[+-]/)
  26. next;
  27. if ($1 == "")
  28. printf("Line %d has an empty login field:\n%s\n", NR, $0);
  29. else if ($1 !~ /^[A-Za-z0-9_][A-Za-z0-9_\-\.]*\$?$/)
  30. printf("Login %s has non-alphanumeric characters.\n", $1);
  31. if (length($1) > 31)
  32. printf("Login %s has more than 31 characters.\n", $1);
  33. if ($2 == "")
  34. printf("Login %s has no password.\n", $1);
  35. if ($2 != "" && length($2) != 13 && ($10 ~ /.*sh$/ || $10 == "") &&
  36. ($2 !~ /^\$[0-9a-f]+\$/) && ($2 != "skey")) {
  37. if (system("test -s /etc/skey/"$1"") == 0)
  38. printf("Login %s is off but still has a valid shell and an entry in /etc/skey.\n", $1);
  39. if (system("test -d "$9" -a ! -r "$9"") == 0)
  40. printf("Login %s is off but still has valid shell and home directory is unreadable\n\t by root; cannot check for existence of alternate access files.\n", $1);
  41. else if (system("for file in .ssh .rhosts .shosts .klogin; do if test -e "$9"/$file; then if ((ls -ld "$9"/$file | cut -b 2-10 | grep -q r) && (test ! -O "$9"/$file)) ; then exit 1; fi; fi; done"))
  42. printf("Login %s is off but still has a valid shell and alternate access files in\n\t home directory are still readable.\n",$1);
  43. }
  44. if ($3 == 0 && $1 != "root")
  45. printf("Login %s has a user ID of 0.\n", $1);
  46. if ($3 < 0)
  47. printf("Login %s has a negative user ID.\n", $1);
  48. if ($4 < 0)
  49. printf("Login %s has a negative group ID.\n", $1);
  50. if (int($7) != 0 && system("test "$7" -lt `date +%s`") == 0)
  51. printf("Login %s has expired.\n", $1);
  52. }' < $MP > $OUTPUT
  53. if [ -s $OUTPUT ] ; then
  54. echo "\nChecking the ${MP} file:"
  55. cat $OUTPUT
  56. fi
  57. awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
  58. if [ -s $OUTPUT ] ; then
  59. echo "\n${MP} has duplicate user names."
  60. column $OUTPUT
  61. fi
  62. awk -F: '/^[^\+]/ { print $1 " " $3 }' $MP | sort -n +1 | tee $TMP1 |
  63. uniq -d -f 1 | awk '{ print $2 }' > $TMP2
  64. if [ -s $TMP2 ] ; then
  65. echo "\n${MP} has duplicate user ID's."
  66. while read uid; do
  67. grep -w $uid $TMP1
  68. done < $TMP2 | column
  69. fi
  70. # Backup the master password file; a special case, the normal backup
  71. # mechanisms also print out file differences and we don't want to do
  72. # that because this file has encrypted passwords in it.
  73. if [ ! -d /var/backups ] ; then
  74. mkdir /var/backups
  75. chmod 700 /var/backups
  76. fi
  77. CUR=/var/backups/`basename $MP`.current
  78. BACK=/var/backups/`basename $MP`.backup
  79. if [ -s $CUR ] ; then
  80. if cmp -s $CUR $MP; then
  81. :
  82. else
  83. cp -p $CUR $BACK
  84. cp -p $MP $CUR
  85. chown root:wheel $CUR
  86. fi
  87. else
  88. cp -p $MP $CUR
  89. chown root:wheel $CUR
  90. fi
  91. # Check the group file syntax.
  92. GRP=/etc/group
  93. awk -F: '{
  94. if ($0 ~ /^[ ]*$/) {
  95. printf("Line %d is a blank line.\n", NR);
  96. next;
  97. }
  98. if ($1 ~ /^[+-].*$/)
  99. next;
  100. if (NF != 4)
  101. printf("Line %d has the wrong number of fields:\n%s\n", NR, $0);
  102. if ($1 !~ /^[A-Za-z0-9_][A-Za-z0-9_\-\.]*$/)
  103. printf("Group %s has non-alphanumeric characters.\n", $1);
  104. if (length($1) > 31)
  105. printf("Group %s has more than 31 characters.\n", $1);
  106. if ($3 !~ /[0-9]*/)
  107. printf("Login %s has a negative group ID.\n", $1);
  108. }' < $GRP > $OUTPUT
  109. if [ -s $OUTPUT ] ; then
  110. echo "\nChecking the ${GRP} file:"
  111. cat $OUTPUT
  112. fi
  113. awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
  114. if [ -s $OUTPUT ] ; then
  115. echo "\n${GRP} has duplicate group names."
  116. column $OUTPUT
  117. fi
  118. # Check for root paths, umask values in startup files.
  119. # The check for the root paths is problematical -- it's likely to fail
  120. # in other environments. Once the shells have been modified to warn
  121. # of '.' in the path, the path tests should go away.
  122. > $OUTPUT
  123. rhome=/root
  124. umaskset=no
  125. list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
  126. for i in $list ; do
  127. if [ -s $i ] ; then
  128. if egrep -q '[[:space:]]*umask[[:space:]]' $i ; then
  129. umaskset=yes
  130. fi
  131. awk '{
  132. if ($1 == "umask") {
  133. if ($2 % 100 / 10 ~ /^[0145]/)
  134. print "Root umask is group writable";
  135. if ($2 % 10 ~ /^[0145]/)
  136. print "Root umask is other writable";
  137. }
  138. }' < $i >> $OUTPUT
  139. SAVE_PATH=$PATH
  140. unset PATH
  141. /bin/csh -f -s << end-of-csh > /dev/null 2>&1
  142. source $i
  143. if (\$?path) then
  144. /bin/ls -ldgT \$path > $TMP1
  145. else
  146. cat /dev/null > $TMP1
  147. endif
  148. end-of-csh
  149. PATH=$SAVE_PATH
  150. awk '{
  151. if ($10 ~ /^\.$/) {
  152. print "The root path includes .";
  153. next;
  154. }
  155. }
  156. $1 ~ /^d....w/ \
  157. { print "Root path directory " $10 " is group writable." } \
  158. $1 ~ /^d.......w/ \
  159. { print "Root path directory " $10 " is other writable." }' \
  160. < $TMP1 >> $OUTPUT
  161. fi
  162. done
  163. if [ $umaskset = "no" -o -s $OUTPUT ] ; then
  164. echo "\nChecking root csh paths, umask values:\n${list}"
  165. if [ -s $OUTPUT ] ; then
  166. cat $OUTPUT
  167. fi
  168. if [ $umaskset = "no" ] ; then
  169. echo "\nRoot csh startup files do not set the umask."
  170. fi
  171. fi
  172. > $OUTPUT
  173. > $TMP2
  174. rhome=/root
  175. umaskset=no
  176. list="/etc/profile ${rhome}/.profile"
  177. for i in $list; do
  178. if [ -s $i ] ; then
  179. if egrep umask $i > /dev/null ; then
  180. umaskset=yes
  181. fi
  182. egrep umask $i |
  183. awk '$2 % 100 < 20 \
  184. { print "Root umask is group writable" } \
  185. $2 % 10 < 2 \
  186. { print "Root umask is other writable" }' >> $OUTPUT
  187. SAVE_PATH=$PATH
  188. SAVE_ENV=$ENV
  189. unset PATH ENV
  190. /bin/sh << end-of-sh > /dev/null 2>&1
  191. . $i
  192. if [ X"\$PATH" != "X" ]; then
  193. list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
  194. /bin/ls -ldgT \$list > $TMP1
  195. else
  196. > $TMP1
  197. fi
  198. echo \$ENV >> $TMP2
  199. end-of-sh
  200. PATH=$SAVE_PATH
  201. ENV=$SAVE_ENV
  202. awk '{
  203. if ($10 ~ /^\.$/) {
  204. print "The root path includes .";
  205. next;
  206. }
  207. }
  208. $1 ~ /^d....w/ \
  209. { print "Root path directory " $10 " is group writable." } \
  210. $1 ~ /^d.......w/ \
  211. { print "Root path directory " $10 " is other writable." }' \
  212. < $TMP1 >> $OUTPUT
  213. fi
  214. done
  215. if [ $umaskset = "no" -o -s $OUTPUT ] ; then
  216. echo "\nChecking root sh paths, umask values:\n${list}"
  217. if [ -s $OUTPUT ] ; then
  218. cat $OUTPUT
  219. fi
  220. if [ $umaskset = "no" ] ; then
  221. echo "\nRoot sh startup files do not set the umask."
  222. fi
  223. fi
  224. # A good .kshrc will not have a umask or path, that being set in .profile
  225. # check anyway.
  226. > $OUTPUT
  227. rhome=/root
  228. list="/etc/ksh.kshrc `cat $TMP2`"
  229. (cd $rhome
  230. for i in $list; do
  231. if [ -s $i ] ; then
  232. egrep umask $i |
  233. awk '$2 % 100 < 20 \
  234. { print "Root umask is group writable" } \
  235. $2 % 10 < 2 \
  236. { print "Root umask is other writable" }' >> $OUTPUT
  237. if egrep PATH= $i > /dev/null ; then
  238. SAVE_PATH=$PATH
  239. unset PATH
  240. /bin/ksh << end-of-sh > /dev/null 2>&1
  241. . $i
  242. if [ X"\$PATH" != "X" ]; then
  243. list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
  244. /bin/ls -ldgT \$list > $TMP1
  245. else
  246. > $TMP1
  247. fi
  248. end-of-sh
  249. PATH=$SAVE_PATH
  250. awk '{
  251. if ($10 ~ /^\.$/) {
  252. print "The root path includes .";
  253. next;
  254. }
  255. }
  256. $1 ~ /^d....w/ \
  257. { print "Root path directory " $10 " is group writable." } \
  258. $1 ~ /^d.......w/ \
  259. { print "Root path directory " $10 " is other writable." }' \
  260. < $TMP1 >> $OUTPUT
  261. fi
  262. fi
  263. done
  264. )
  265. if [ -s $OUTPUT ] ; then
  266. echo "\nChecking root ksh paths, umask values:\n${list}"
  267. cat $OUTPUT
  268. fi
  269. # Root and uucp should both be in /etc/ftpusers.
  270. if egrep root /etc/ftpusers > /dev/null ; then
  271. :
  272. else
  273. echo "\nRoot not listed in /etc/ftpusers file."
  274. fi
  275. if egrep uucp /etc/ftpusers > /dev/null ; then
  276. :
  277. else
  278. echo "\nUucp not listed in /etc/ftpusers file."
  279. fi
  280. # Uudecode should not be in the /etc/mail/aliases file.
  281. if egrep 'uudecode|decode' /etc/mail/aliases; then
  282. echo "\nThere is an entry for uudecode in the /etc/mail/aliases file."
  283. fi
  284. # Files that should not have + signs.
  285. list="/etc/hosts.equiv /etc/shosts.equiv /etc/hosts.lpd"
  286. for f in $list ; do
  287. if [ -s $f ] ; then
  288. awk '{
  289. if ($0 ~ /^\+@.*$/)
  290. next;
  291. if ($0 ~ /^\+.*$/)
  292. printf("\nPlus sign in %s file.\n", FILENAME);
  293. }' $f
  294. fi
  295. done
  296. # Check for special users with .rhosts/.shosts files. Only root
  297. # should have .rhosts/.shosts files. Also, .rhosts/.shosts
  298. # files should not have plus signs.
  299. awk -F: '$1 != "root" && $1 !~ /^[+-]/ && \
  300. ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
  301. { print $1 " " $6 }' /etc/passwd |
  302. while read uid homedir; do
  303. for j in .rhosts .shosts; do
  304. # Root owned .rhosts/.shosts files are ok.
  305. if [ -s ${homedir}/$j -a ! -O ${homedir}/$j ] ; then
  306. rhost=`ls -ldgT ${homedir}/$j`
  307. echo "${uid}: ${rhost}"
  308. fi
  309. done
  310. done > $OUTPUT
  311. if [ -s $OUTPUT ] ; then
  312. echo "\nChecking for special users with .rhosts/.shosts files."
  313. cat $OUTPUT
  314. fi
  315. awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
  316. while read uid homedir; do
  317. for j in .rhosts .shosts; do
  318. if [ -s ${homedir}/$j ] ; then
  319. awk '{
  320. if ($0 ~ /^+@.*$/ )
  321. next;
  322. if ($0 ~ /^\+[ ]*$/ )
  323. printf("%s has + sign in it.\n",
  324. FILENAME);
  325. }' ${homedir}/$j
  326. fi
  327. done
  328. done > $OUTPUT
  329. if [ -s $OUTPUT ] ; then
  330. echo "\nChecking .rhosts/.shosts files syntax."
  331. cat $OUTPUT
  332. fi
  333. # Check home directories. Directories should not be owned by someone else
  334. # or writeable.
  335. awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
  336. while read uid homedir; do
  337. if [ -d ${homedir}/ ] ; then
  338. file=`ls -ldgT ${homedir}`
  339. echo "${uid} ${file}"
  340. fi
  341. done |
  342. awk '$1 != $4 && $4 != "root" \
  343. { print "user " $1 " home directory is owned by " $4 }
  344. $2 ~ /^-....w/ \
  345. { print "user " $1 " home directory is group writable" }
  346. $2 ~ /^-.......w/ \
  347. { print "user " $1 " home directory is other writable" }' > $OUTPUT
  348. if [ -s $OUTPUT ] ; then
  349. echo "\nChecking home directories."
  350. cat $OUTPUT
  351. fi
  352. # Files that should not be owned by someone else or readable.
  353. list=".netrc .rhosts .gnupg/secring.gpg .gnupg/random_seed \
  354. .pgp/secring.pgp .shosts .ssh/identity .ssh/id_dsa .ssh/id_rsa"
  355. awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
  356. while read uid homedir; do
  357. for f in $list ; do
  358. file=${homedir}/${f}
  359. if [ -f $file ] ; then
  360. echo "${uid} ${f} `ls -ldgT ${file}`"
  361. fi
  362. done
  363. done |
  364. awk '$1 != $5 && $5 != "root" \
  365. { print "user " $1 " " $2 " file is owned by " $5 }
  366. $3 ~ /^-...r/ \
  367. { print "user " $1 " " $2 " file is group readable" }
  368. $3 ~ /^-......r/ \
  369. { print "user " $1 " " $2 " file is other readable" }
  370. $3 ~ /^-....w/ \
  371. { print "user " $1 " " $2 " file is group writable" }
  372. $3 ~ /^-.......w/ \
  373. { print "user " $1 " " $2 " file is other writable" }' > $OUTPUT
  374. # Files that should not be owned by someone else or writeable.
  375. list=".bashrc .bash_profile .bash_login .bash_logout .cshrc \
  376. .emacs .exrc .forward .fvwmrc .inputrc .klogin .kshrc .login \
  377. .logout .nexrc .profile .screenrc .ssh .ssh/config \
  378. .ssh/authorized_keys .ssh/authorized_keys2 .ssh/environment \
  379. .ssh/known_hosts .ssh/rc .tcshrc .twmrc .xsession .xinitrc \
  380. .Xdefaults .Xauthority"
  381. awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
  382. while read uid homedir; do
  383. for f in $list ; do
  384. file=${homedir}/${f}
  385. if [ -f $file ] ; then
  386. echo "${uid} ${f} `ls -ldgT ${file}`"
  387. fi
  388. done
  389. done |
  390. awk '$1 != $5 && $5 != "root" \
  391. { print "user " $1 " " $2 " file is owned by " $5 }
  392. $3 ~ /^-....w/ \
  393. { print "user " $1 " " $2 " file is group writable" }
  394. $3 ~ /^-.......w/ \
  395. { print "user " $1 " " $2 " file is other writable" }' >> $OUTPUT
  396. if [ -s $OUTPUT ] ; then
  397. echo "\nChecking dot files."
  398. cat $OUTPUT
  399. fi
  400. # Mailboxes should be owned by user and unreadable.
  401. ls -l /var/mail | sed 1d | \
  402. awk '$3 != $9 \
  403. { print "user " $9 " mailbox is owned by " $3 }
  404. $1 != "-rw-------" \
  405. { print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
  406. if [ -s $OUTPUT ] ; then
  407. echo "\nChecking mailbox ownership."
  408. cat $OUTPUT
  409. fi
  410. # File systems should not be globally exported.
  411. if [ -s /etc/exports ] ; then
  412. awk '{
  413. if (($1 ~ /^#/) || ($1 ~ /^$/))
  414. next;
  415. readonly = 0;
  416. for (i = 2; i <= NF; ++i) {
  417. if ($i ~ /^-ro$/)
  418. readonly = 1;
  419. else if ($i !~ /^-/)
  420. next;
  421. }
  422. if (readonly)
  423. print "File system " $1 " globally exported, read-only."
  424. else
  425. print "File system " $1 " globally exported, read-write."
  426. }' < /etc/exports > $OUTPUT
  427. if [ -s $OUTPUT ] ; then
  428. echo "\nChecking for globally exported file systems."
  429. cat $OUTPUT
  430. fi
  431. fi
  432. # Display any changes in setuid/setgid files and devices.
  433. pending="\nChecking setuid/setgid files and devices:\n"
  434. (find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
  435. -o -fstype procfs \) -a -prune -o \
  436. -type f -a \( -perm -u+s -o -perm -g+s \) -print0 -o \
  437. ! -type d -a ! -type f -a ! -type l -a ! -type s -a ! -type p \
  438. -print0 | xargs -0 ls -ldgT | sort +9 > $LIST) 2> $OUTPUT
  439. # Display any errors that occurred during system file walk.
  440. if [ -s $OUTPUT ] ; then
  441. echo "${pending}Setuid/device find errors:"
  442. pending=
  443. cat $OUTPUT
  444. echo ""
  445. fi
  446. # Display any changes in the setuid/setgid file list.
  447. FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,0
  448. FIELDS2=2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,0
  449. egrep -av '^[bc]' $LIST | join -o $FIELDS2 -110 -210 -v2 /dev/null - > $TMP1
  450. if [ -s $TMP1 ] ; then
  451. # Check to make sure uudecode isn't setuid.
  452. if grep -aw uudecode $TMP1 > /dev/null ; then
  453. echo "${pending}\nUudecode is setuid."
  454. pending=
  455. fi
  456. CUR=/var/backups/setuid.current
  457. BACK=/var/backups/setuid.backup
  458. if [ -s $CUR ] ; then
  459. if cmp -s $CUR $TMP1 ; then
  460. :
  461. else
  462. > $TMP2
  463. join -o $FIELDS2 -110 -210 -v2 $CUR $TMP1 > $OUTPUT
  464. if [ -s $OUTPUT ] ; then
  465. echo "${pending}Setuid additions:"
  466. pending=
  467. tee -a $TMP2 < $OUTPUT | column -t
  468. echo ""
  469. fi
  470. join -o $FIELDS1 -110 -210 -v1 $CUR $TMP1 > $OUTPUT
  471. if [ -s $OUTPUT ] ; then
  472. echo "${pending}Setuid deletions:"
  473. pending=
  474. tee -a $TMP2 < $OUTPUT | column -t
  475. echo ""
  476. fi
  477. sort +9 $TMP2 $CUR $TMP1 | \
  478. sed -e 's/[ ][ ]*/ /g' | uniq -u > $OUTPUT
  479. if [ -s $OUTPUT ] ; then
  480. echo "${pending}Setuid changes:"
  481. pending=
  482. column -t $OUTPUT
  483. echo ""
  484. fi
  485. cp $CUR $BACK
  486. cp $TMP1 $CUR
  487. fi
  488. else
  489. echo "${pending}Setuid additions:"
  490. pending=
  491. column -t $TMP1
  492. echo ""
  493. cp $TMP1 $CUR
  494. fi
  495. fi
  496. # Check for block and character disk devices that are readable or writeable
  497. # or not owned by root.operator.
  498. >$TMP1
  499. DISKLIST="ccd dk fd hd hk hp jb kra ra rb rd rl rx rz sd up vnd wd xd"
  500. for i in $DISKLIST; do
  501. egrep "^b.*/${i}[0-9][0-9]*[B-H]?[a-p]$" $LIST >> $TMP1
  502. egrep "^c.*/r${i}[0-9][0-9]*[B-H]?[a-p]$" $LIST >> $TMP1
  503. done
  504. awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
  505. { printf("Disk %s is user %s, group %s, permissions %s.\n", \
  506. $11, $3, $4, $1); }' < $TMP1 > $OUTPUT
  507. if [ -s $OUTPUT ] ; then
  508. echo "\nChecking disk ownership and permissions."
  509. cat $OUTPUT
  510. echo ""
  511. fi
  512. FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,0
  513. FIELDS2=2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,0
  514. # Display any changes in the device file list.
  515. egrep -a '^[bc]' $LIST | sort +10 | \
  516. join -o $FIELDS2 -111 -211 -v2 /dev/null - > $TMP1
  517. if [ -s $TMP1 ] ; then
  518. CUR=/var/backups/device.current
  519. BACK=/var/backups/device.backup
  520. if [ -s $CUR ] ; then
  521. if cmp -s $CUR $TMP1 ; then
  522. :
  523. else
  524. > $TMP2
  525. join -o $FIELDS2 -111 -211 -v2 $CUR $TMP1 > $OUTPUT
  526. if [ -s $OUTPUT ] ; then
  527. echo "Device additions:"
  528. tee -a $TMP2 < $OUTPUT | column -t
  529. echo ""
  530. fi
  531. join -o $FIELDS1 -111 -211 -v1 $CUR $TMP1 > $OUTPUT
  532. if [ -s $OUTPUT ] ; then
  533. echo "Device deletions:"
  534. tee -a $TMP2 < $OUTPUT | column -t
  535. echo ""
  536. fi
  537. # Report any block device change. Ignore character
  538. # devices, only the name is significant.
  539. cat $TMP2 $CUR $TMP1 | \
  540. sed -e '/^c/d' | \
  541. sort +10 | \
  542. sed -e 's/[ ][ ]*/ /g' | \
  543. uniq -u > $OUTPUT
  544. if [ -s $OUTPUT ] ; then
  545. echo "Block device changes:"
  546. column -t $OUTPUT
  547. echo ""
  548. fi
  549. cp $CUR $BACK
  550. cp $TMP1 $CUR
  551. fi
  552. else
  553. echo "Device additions:"
  554. column -t $TMP1
  555. echo ""
  556. cp $TMP1 $CUR
  557. fi
  558. fi
  559. # Check special files.
  560. # Check system binaries.
  561. #
  562. # Create the mtree tree specifications using:
  563. #
  564. # mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
  565. # chown root:wheel DIR.secure
  566. # chmod 600 DIR.secure
  567. #
  568. # Note, this is not complete protection against Trojan horsed binaries, as
  569. # the hacker can modify the tree specification to match the replaced binary.
  570. # For details on really protecting yourself against modified binaries, see
  571. # the mtree(8) manual page.
  572. if [ -d /etc/mtree ] ; then
  573. cd /etc/mtree
  574. mtree -e -l -p / -f /etc/mtree/special > $OUTPUT
  575. if [ -s $OUTPUT ] ; then
  576. echo "\nChecking special files and directories."
  577. echo "Output format is:\n\tfilename:"
  578. echo "\t\tcriteria (shouldbe, reallyis)"
  579. cat $OUTPUT
  580. fi
  581. > $OUTPUT
  582. for file in *.secure; do
  583. [ $file = '*.secure' ] && continue
  584. tree=`sed -n -e '3s/.* //p' -e 3q $file`
  585. mtree -f $file -p $tree > $TMP1
  586. if [ -s $TMP1 ] ; then
  587. echo "\nChecking ${tree}:" >> $OUTPUT
  588. cat $TMP1 >> $OUTPUT
  589. fi
  590. done
  591. if [ -s $OUTPUT ] ; then
  592. echo "\nChecking system binaries:"
  593. cat $OUTPUT
  594. fi
  595. else
  596. echo /etc/mtree is missing
  597. fi
  598. # List of files that get backed up and checked for any modifications. Each
  599. # file is expected to have two backups, /var/backups/file.{current,backup}.
  600. # Any changes cause the files to rotate.
  601. _fnchg() {
  602. echo "$1" | sed 's/^\///;s/\//_/g'
  603. }
  604. if [ -s /etc/changelist ] ; then
  605. for file in `egrep -v "^(#|\+|$MP)" /etc/changelist`; do
  606. CUR=/var/backups/$(_fnchg "$file").current
  607. BACK=/var/backups/$(_fnchg "$file").backup
  608. if [ -s $file -a ! -d $file ] ; then
  609. if [ -s $CUR ] ; then
  610. diff -u $CUR $file > $OUTPUT
  611. if [ -s $OUTPUT ] ; then
  612. echo "\n======\n${file} diffs (-OLD +NEW)\n======"
  613. cat $OUTPUT
  614. cp -p $CUR $BACK
  615. cp -p $file $CUR
  616. chown root:wheel $CUR $BACK
  617. fi
  618. else
  619. cp -p $file $CUR
  620. chown root:wheel $CUR
  621. fi
  622. fi
  623. done
  624. for file in `egrep "^\+" /etc/changelist`; do
  625. file="${file#+}"
  626. CUR=/var/backups/$(_fnchg "$file").current.md5
  627. BACK=/var/backups/$(_fnchg "$file").backup.md5
  628. if [ -s $file -a ! -d $file ] ; then
  629. MD5_NEW=`md5 $file | sed 's/^.* //'`
  630. if [ -s $CUR ] ; then
  631. MD5_OLD="`cat $CUR`"
  632. if [ "$MD5_NEW" != "$MD5_OLD" ]; then
  633. echo "\n======\n${file} MD5 checksums\n======"
  634. echo "OLD: $MD5_OLD"
  635. echo "NEW: $MD5_NEW"
  636. cp -p $CUR $BACK
  637. echo $MD5_NEW > $CUR
  638. chown root:wheel $CUR $BACK
  639. chmod 600 $CUR
  640. fi
  641. else
  642. echo $MD5_NEW > $CUR
  643. chown root:wheel $CUR
  644. chmod 600 $CUR
  645. fi
  646. fi
  647. done
  648. fi
  649. # Make backups of the labels for any mounted disks and produce diffs
  650. # when they change.
  651. for d in `df -ln | sed -n 's:^/dev/\([a-z]*[0-9]*\)[a-p].*$:\1:p' | sort -u`; do
  652. file=/var/backups/disklabel.$d
  653. CUR=$file.current
  654. BACK=$file.backup
  655. if disklabel $d > $file 2>&1 ; then
  656. if [ -s $CUR ] ; then
  657. diff -u $CUR $file > $OUTPUT
  658. if [ -s $OUTPUT ] ; then
  659. echo "\n======\n${d} diffs (-OLD +NEW)\n======"
  660. cat $OUTPUT
  661. cp -p $CUR $BACK
  662. cp -p $file $CUR
  663. chown root:wheel $CUR $BACK
  664. fi
  665. else
  666. cp -p $file $CUR
  667. chown root:wheel $CUR
  668. fi
  669. fi
  670. rm -f $file
  671. done