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.

211 lines
5.4 KiB

11 years ago
11 years ago
11 years ago
  1. #
  2. # $OpenBSD: daily,v 1.78 2013/12/12 22:02:43 brad 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 ./portslocks \) \
  46. -prune -o -type f -atime +3 -execdir rm -f -- {} \; 2>/dev/null
  47. find -x . -type d -mtime +1 ! -path ./vi.recover ! -path ./.X11-unix \
  48. ! -path ./.ICE-unix ! -path ./portslocks ! -name . \
  49. -execdir rmdir -- {} \; >/dev/null 2>&1; }
  50. fi
  51. if [ -d /var/tmp -a ! -L /var/tmp ]; then
  52. cd /var/tmp && {
  53. find -x . \
  54. \( -path './ssh-*' -o -path ./.X11-unix -o -path ./.ICE-unix \
  55. -o -path ./portslocks \) \
  56. -prune -o ! -type d -atime +7 -execdir rm -f -- {} \; 2>/dev/null
  57. find -x . -type d -mtime +1 ! -path ./vi.recover ! -path ./.X11-unix \
  58. ! -path ./.ICE-unix ! -path ./portslocks ! -name . \
  59. -execdir rmdir -- {} \; >/dev/null 2>&1; }
  60. fi
  61. # Additional junk directory cleanup would go like this:
  62. #if [ -d /scratch -a ! -L /scratch ]; then
  63. # cd /scratch && {
  64. # find . ! -name . -atime +1 -execdir rm -f -- {} \;
  65. # find . ! -name . -type d -mtime +1 -execdir rmdir -- {} \; \
  66. # >/dev/null 2>&1; }
  67. #fi
  68. if [ -d /var/rwho -a ! -L /var/rwho ] ; then
  69. cd /var/rwho && {
  70. find . ! -name . -mtime +7 -execdir rm -f -- {} \; ; }
  71. fi
  72. next_part "Purging accounting records:"
  73. if [ -f /var/account/acct ]; then
  74. mv -f /var/account/acct.2 /var/account/acct.3
  75. mv -f /var/account/acct.1 /var/account/acct.2
  76. mv -f /var/account/acct.0 /var/account/acct.1
  77. cp -f /var/account/acct /var/account/acct.0
  78. sa -sq
  79. fi
  80. # If ROOTBACKUP is set to 1 in the environment, and
  81. # if filesystem named /altroot is type ffs and mounted "xx",
  82. # use it as a backup root filesystem to be updated daily.
  83. next_part "Backing up root filesystem:"
  84. while [ "X$ROOTBACKUP" = X1 ]; do
  85. rootbak=`awk '$1 !~ /^#/ && $2 == "/altroot" && $3 == "ffs" && \
  86. $4 ~ /xx/ { print $1 }' < /etc/fstab`
  87. if [ -z "$rootbak" ]; then
  88. echo "No xx ffs /altroot device found in the fstab(5)."
  89. break
  90. fi
  91. rootbak=${rootbak#/dev/}
  92. bakdisk=${rootbak%%?(.)[a-p]}
  93. sysctl -n hw.disknames | grep -Fqw $bakdisk || break
  94. bakpart=${rootbak##$bakdisk?(.)}
  95. OLDIFS=$IFS
  96. IFS=,
  97. for d in `sysctl -n hw.disknames`; do
  98. # If the provided disk name is a duid, substitute the device.
  99. if [ X$bakdisk = X${d#*:} ]; then
  100. bakdisk=${d%:*}
  101. rootbak=$bakdisk$bakpart
  102. fi
  103. done
  104. IFS=$OLDIFS
  105. baksize=`disklabel $bakdisk 2>/dev/null | \
  106. awk -v "part=$bakpart:" '$1 == part { print $2 }'`
  107. rootdev=`mount | awk '$3 == "/" && $1 ~ /^\/dev\// && $5 == "ffs" \
  108. { print substr($1, 6) }'`
  109. if [ -z "$rootdev" ]; then
  110. echo "The root filesystem is not local or not ffs."
  111. break
  112. fi
  113. if [ X$rootdev = X$rootbak ]; then
  114. echo "The device $rootdev holds both root and /altroot."
  115. break
  116. fi
  117. rootdisk=${rootdev%[a-p]}
  118. rootpart=${rootdev#$rootdisk}
  119. rootsize=`disklabel $rootdisk 2>/dev/null | \
  120. awk -v "part=$rootpart:" '$1 == part { print $2 }'`
  121. if [ $rootsize -gt $baksize ]; then
  122. echo "Root ($rootsize) is larger than /altroot ($baksize)."
  123. break
  124. fi
  125. next_part "Backing up root=/dev/r$rootdev to /dev/r$rootbak:"
  126. sync
  127. dd if=/dev/r$rootdev of=/dev/r$rootbak bs=16b seek=1 skip=1 \
  128. conv=noerror
  129. fsck -y /dev/r$rootbak
  130. break
  131. done
  132. next_part "Checking subsystem status:"
  133. if [ "X$VERBOSESTATUS" != X0 ]; then
  134. echo ""
  135. echo "disks:"
  136. df -ikl
  137. echo ""
  138. dump W
  139. fi
  140. # The first two regular expressions handle sendmail, the third postfix.
  141. # When the queue is empty, smtpd(8) and exim -bp keep silent.
  142. next_part "mail:"
  143. mailq | grep -v -e "^/var/spool/mqueue is empty$" \
  144. -e "^[[:blank:]]*Total requests: 0$" \
  145. -e "^Mail queue is empty$"
  146. next_part "network:"
  147. if [ "X$VERBOSESTATUS" != X0 ]; then
  148. netstat -ivn
  149. t=/var/rwho/*
  150. if [ "$t" != '/var/rwho/*' ]; then
  151. echo ""
  152. ruptime
  153. fi
  154. fi
  155. next_part "Running calendar in the background:"
  156. if [ "X$CALENDAR" != X0 -a \
  157. \( -d /var/yp/`domainname` -o ! -d /var/yp/binding \) ]; then
  158. calendar -a &
  159. fi
  160. # If CHECKFILESYSTEMS is set to 1 in the environment, run fsck
  161. # with the no-write flag.
  162. next_part "Checking filesystems:"
  163. [ "X$CHECKFILESYSTEMS" = X1 ] && {
  164. fsck -n | grep -v '^\*\* Phase'
  165. }
  166. next_part "Running rdist:"
  167. if [ -f /etc/Distfile ]; then
  168. if [ -d /var/log/rdist ]; then
  169. rdist -f /etc/Distfile 2>&1 | tee /var/log/rdist/`date +%F`
  170. else
  171. rdist -f /etc/Distfile
  172. fi
  173. fi
  174. end_part
  175. [ -s $MAINOUT ] && {
  176. sysctl -n kern.version
  177. uptime
  178. cat $MAINOUT
  179. } 2>&1 | mail -s "`hostname` daily output" root
  180. MAINOUT=/var/log/security.out
  181. install -o 0 -g 0 -m 600 -b /dev/null $MAINOUT
  182. start_part "Running security(8):"
  183. export SUIDSKIP
  184. /usr/libexec/security
  185. end_part
  186. rm -f $PARTOUT
  187. [ -s $MAINOUT ] && mail -s "`hostname` daily insecurity output" root < $MAINOUT