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.

194 lines
4.8 KiB

10 years ago
  1. #
  2. # $OpenBSD: daily,v 1.92 2019/07/25 13:13:53 bluhm Exp $
  3. # From: @(#)daily 8.2 (Berkeley) 1/25/94
  4. #
  5. # For local additions, create the file /etc/daily.local.
  6. # To get section headers, use the function next_part in daily.local.
  7. #
  8. umask 022
  9. PARTOUT=/var/log/daily.part
  10. MAINOUT=/var/log/daily.out
  11. install -o 0 -g 0 -m 600 /dev/null $PARTOUT
  12. install -o 0 -g 0 -m 600 -b /dev/null $MAINOUT
  13. start_part() {
  14. TITLE=$1
  15. exec > $PARTOUT 2>&1
  16. }
  17. end_part() {
  18. exec >> $MAINOUT 2>&1
  19. test -s $PARTOUT || return
  20. echo ""
  21. echo "$TITLE"
  22. cat $PARTOUT
  23. }
  24. next_part() {
  25. end_part
  26. start_part "$1"
  27. }
  28. run_script() {
  29. f=/etc/$1
  30. test -e $f || return
  31. if [ `stat -f '%Sp%u' $f | cut -b1,6,9,11-` != '---0' ]; then
  32. echo "$f has insecure permissions, skipping:"
  33. ls -l $f
  34. return
  35. fi
  36. . $f
  37. }
  38. start_part "Running daily.local:"
  39. run_script "daily.local"
  40. next_part "Removing scratch and junk files:"
  41. if [ -d /tmp -a ! -L /tmp ]; then
  42. cd /tmp && {
  43. find -x . \
  44. \( -path './ssh-*' -o -path ./.X11-unix -o -path ./.ICE-unix \
  45. -o -path './tmux-*' \) \
  46. -prune -o -type f -atime +7 -execdir rm -f -- {} \; 2>/dev/null
  47. find -x . -type d -mtime +1 ! -path ./vi.recover ! -path ./.X11-unix \
  48. ! -path ./.ICE-unix ! -name . \
  49. -execdir rmdir -- {} \; >/dev/null 2>&1; }
  50. fi
  51. # Additional junk directory cleanup would go like this:
  52. #if [ -d /scratch -a ! -L /scratch ]; then
  53. # cd /scratch && {
  54. # find . ! -name . -atime +1 -execdir rm -f -- {} \;
  55. # find . ! -name . -type d -mtime +1 -execdir rmdir -- {} \; \
  56. # >/dev/null 2>&1; }
  57. #fi
  58. next_part "Purging accounting records:"
  59. if [ -f /var/account/acct ]; then
  60. test -f /var/account/acct.2 && \
  61. mv -f /var/account/acct.2 /var/account/acct.3
  62. test -f /var/account/acct.1 && \
  63. mv -f /var/account/acct.1 /var/account/acct.2
  64. test -f /var/account/acct.0 && \
  65. mv -f /var/account/acct.0 /var/account/acct.1
  66. cp -f /var/account/acct /var/account/acct.0
  67. sa -sq
  68. lastcomm -f /var/account/acct.0 | grep -e ' -[A-Z]*[PTU]'
  69. fi
  70. # If ROOTBACKUP is set to 1 in the environment, and
  71. # if filesystem named /altroot is type ffs and mounted "xx",
  72. # use it as a backup root filesystem to be updated daily.
  73. next_part "Backing up root filesystem:"
  74. while [ "X$ROOTBACKUP" = X1 ]; do
  75. rootbak=`awk '$1 !~ /^#/ && $2 == "/altroot" && $3 == "ffs" && \
  76. $4 ~ /xx/ { print $1 }' < /etc/fstab`
  77. if [ -z "$rootbak" ]; then
  78. echo "No xx ffs /altroot device found in the fstab(5)."
  79. break
  80. fi
  81. rootbak=${rootbak#/dev/}
  82. bakdisk=${rootbak%%?(.)[a-p]}
  83. if ! sysctl -n hw.disknames | grep -Fqw $bakdisk; then
  84. echo "Backup disk '$bakdisk' not present in hw.disknames."
  85. break
  86. fi
  87. bakpart=${rootbak##$bakdisk?(.)}
  88. OLDIFS=$IFS
  89. IFS=,
  90. for d in `sysctl -n hw.disknames`; do
  91. # If the provided disk name is a duid, substitute the device.
  92. if [ X$bakdisk = X${d#*:} ]; then
  93. bakdisk=${d%:*}
  94. rootbak=$bakdisk$bakpart
  95. fi
  96. done
  97. IFS=$OLDIFS
  98. baksize=`disklabel $bakdisk 2>/dev/null | \
  99. awk -v "part=$bakpart:" '$1 == part { print $2 }'`
  100. rootdev=`mount | awk '$3 == "/" && $1 ~ /^\/dev\// && $5 == "ffs" \
  101. { print substr($1, 6) }'`
  102. if [ -z "$rootdev" ]; then
  103. echo "The root filesystem is not local or not ffs."
  104. break
  105. fi
  106. if [ X$rootdev = X$rootbak ]; then
  107. echo "The device $rootdev holds both root and /altroot."
  108. break
  109. fi
  110. rootdisk=${rootdev%[a-p]}
  111. rootpart=${rootdev#$rootdisk}
  112. rootsize=`disklabel $rootdisk 2>/dev/null | \
  113. awk -v "part=$rootpart:" '$1 == part { print $2 }'`
  114. if [ $rootsize -gt $baksize ]; then
  115. echo "Root ($rootsize) is larger than /altroot ($baksize)."
  116. break
  117. fi
  118. next_part "Backing up root=/dev/r$rootdev to /dev/r$rootbak:"
  119. sync
  120. dd if=/dev/r$rootdev of=/dev/r$rootbak bs=16b seek=1 skip=1 \
  121. conv=noerror
  122. fsck -y /dev/r$rootbak
  123. break
  124. done
  125. next_part "Services that should be running but aren't:"
  126. rcctl ls failed
  127. next_part "Checking subsystem status:"
  128. if [ "X$VERBOSESTATUS" != X0 ]; then
  129. echo ""
  130. echo "disks:"
  131. df -ikl
  132. echo ""
  133. dump W
  134. else
  135. dump w | grep -vB1 ^Dump
  136. fi
  137. next_part "network:"
  138. if [ "X$VERBOSESTATUS" != X0 ]; then
  139. netstat -ivn
  140. fi
  141. next_part "Running calendar in the background:"
  142. if [ "X$CALENDAR" != X0 -a \
  143. \( -d /var/yp/`domainname` -o ! -d /var/yp/binding \) ]; then
  144. calendar -a &
  145. fi
  146. # If CHECKFILESYSTEMS is set to 1 in the environment, run fsck
  147. # with the no-write flag.
  148. next_part "Checking filesystems:"
  149. [ "X$CHECKFILESYSTEMS" = X1 ] && {
  150. fsck -n | grep -v '^\*\* Phase'
  151. }
  152. next_part "Running rdist:"
  153. if [ -f /etc/Distfile ]; then
  154. if [ -d /var/log/rdist ]; then
  155. rdist -f /etc/Distfile 2>&1 | tee /var/log/rdist/`date +%F`
  156. else
  157. rdist -f /etc/Distfile
  158. fi
  159. fi
  160. end_part
  161. [ -s $MAINOUT ] && {
  162. sysctl -n kern.version
  163. uptime
  164. cat $MAINOUT
  165. } 2>&1 | mail -s "`hostname` daily output" root
  166. MAINOUT=/var/log/security.out
  167. install -o 0 -g 0 -m 600 -b /dev/null $MAINOUT
  168. start_part "Running security(8):"
  169. export SUIDSKIP
  170. /usr/libexec/security
  171. end_part
  172. rm -f $PARTOUT
  173. [ -s $MAINOUT ] && mail -s "`hostname` daily insecurity output" root < $MAINOUT