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.

533 lines
12 KiB

27 years ago
12 years ago
27 years ago
  1. # $OpenBSD: rc,v 1.407 2013/08/09 16:24:54 ajacoutot Exp $
  2. # System startup script run by init on autoboot
  3. # or after single-user.
  4. # Output and error are redirected to console by init,
  5. # and the console is the controlling terminal.
  6. # Subroutines (have to come first).
  7. # Strip comments (and leading/trailing whitespace if IFS is set)
  8. # from a file and spew to stdout
  9. stripcom() {
  10. local _file="$1"
  11. local _line
  12. {
  13. while read _line ; do
  14. _line=${_line%%#*} # strip comments
  15. test -z "$_line" && continue
  16. echo $_line
  17. done
  18. } < $_file
  19. }
  20. # Update resource limits when sysctl changes
  21. # Usage: update_limit -X loginconf_name
  22. update_limit() {
  23. local _fl="$1" # ulimit flag
  24. local _lc="$2" # login.conf name
  25. local _new _suf
  26. for _suf in "" -cur -max; do
  27. _new=`getcap -f /etc/login.conf -s ${_lc}${_suf} daemon 2>/dev/null`
  28. if [ X"$_new" != X"" ]; then
  29. if [ X"$_new" = X"infinity" ]; then
  30. _new=unlimited
  31. fi
  32. case "$_suf" in
  33. -cur)
  34. ulimit -S $_fl $_new
  35. ;;
  36. -max)
  37. ulimit -H $_fl $_new
  38. ;;
  39. *)
  40. ulimit $_fl $_new
  41. return
  42. ;;
  43. esac
  44. fi
  45. done
  46. }
  47. sysctl_conf() {
  48. test -s /etc/sysctl.conf || return
  49. # delete comments and blank lines
  50. set -- `stripcom /etc/sysctl.conf`
  51. while [ $# -ge 1 ] ; do
  52. sysctl $1
  53. # update limits if needed
  54. case $1 in
  55. kern.maxproc=*)
  56. update_limit -p maxproc
  57. ;;
  58. kern.maxfiles=*)
  59. update_limit -n openfiles
  60. ;;
  61. esac
  62. shift
  63. done
  64. }
  65. mixerctl_conf()
  66. {
  67. test -s /etc/mixerctl.conf || return
  68. # delete comments and blank lines
  69. set -- `stripcom /etc/mixerctl.conf`
  70. while [ $# -ge 1 ] ; do
  71. mixerctl -q $1 > /dev/null 2>&1
  72. shift
  73. done
  74. }
  75. wsconsctl_conf()
  76. {
  77. local save_IFS="$IFS"
  78. test -x /sbin/wsconsctl -a -s /etc/wsconsctl.conf || return
  79. # delete comments and blank lines
  80. IFS="
  81. "
  82. set -- `stripcom /etc/wsconsctl.conf`
  83. IFS="$save_IFS"
  84. while [ $# -ge 1 ] ; do
  85. eval wsconsctl $1
  86. shift
  87. done
  88. }
  89. random_seed()
  90. {
  91. if [ -f /var/db/host.random ]; then
  92. dd if=/var/db/host.random of=/dev/arandom bs=65536 count=1 \
  93. > /dev/null 2>&1
  94. chmod 600 /var/db/host.random >/dev/null 2>&1
  95. # reset seed file, so that if a shutdown-less reboot occurs,
  96. # the next seed is not a repeat
  97. dd if=/dev/arandom of=/var/db/host.random bs=65536 count=1 \
  98. > /dev/null 2>&1
  99. fi
  100. }
  101. fill_baddynamic()
  102. {
  103. local _service=$1
  104. local _sysctl="net.inet.${_service}.baddynamic"
  105. stripcom /etc/services |
  106. {
  107. # Variables are local
  108. while IFS=" /" read _name _port _srv _junk; do
  109. [ "x${_srv}" = "x${_service}" ] || continue
  110. _ban="${_ban:+${_ban},}+${_port}"
  111. # Flush before argv gets too long
  112. if [ ${#_ban} -gt 1024 ]; then
  113. sysctl -q ${_sysctl}=${_ban}
  114. _ban=""
  115. fi
  116. done
  117. [ "${_ban}" ] && sysctl -q ${_sysctl}=${_ban}
  118. }
  119. }
  120. start_daemon()
  121. {
  122. local _n
  123. for _n; do
  124. eval _do=\${${_n}_flags}
  125. if [ X"${_do}" != X"NO" ]; then
  126. /etc/rc.d/${_n} start
  127. fi
  128. done
  129. }
  130. make_keys()
  131. {
  132. if [ X"${named_flags}" != X"NO" ]; then
  133. if ! cmp -s /etc/rndc.key /var/named/etc/rndc.key ; then
  134. echo -n "rndc-confgen: generating shared secret... "
  135. if rndc-confgen -a -t /var/named >/dev/null 2>&1; then
  136. chmod 0640 /var/named/etc/rndc.key \
  137. >/dev/null 2>&1
  138. echo done.
  139. else
  140. echo failed.
  141. fi
  142. fi
  143. fi
  144. if [ ! -f /etc/isakmpd/private/local.key ]; then
  145. echo -n "openssl: generating isakmpd/iked RSA key... "
  146. if openssl genrsa -out /etc/isakmpd/private/local.key 2048 \
  147. >/dev/null 2>&1; then
  148. chmod 600 /etc/isakmpd/private/local.key
  149. openssl rsa -out /etc/isakmpd/local.pub -in \
  150. /etc/isakmpd/private/local.key -pubout \
  151. >/dev/null 2>&1
  152. echo done.
  153. else
  154. echo failed.
  155. fi
  156. fi
  157. if [ ! -f /etc/iked/private/local.key ]; then
  158. # Just copy the generated isakmpd key
  159. cp /etc/isakmpd/private/local.key /etc/iked/private/local.key
  160. chmod 600 /etc/iked/private/local.key
  161. cp /etc/isakmpd/local.pub /etc/iked/local.pub
  162. fi
  163. ssh-keygen -A
  164. }
  165. # create Unix sockets directories for X if needed and make sure they have
  166. # correct permissions
  167. setup_X_sockets()
  168. {
  169. if [ -d /usr/X11R6/lib ]; then
  170. for d in /tmp/.X11-unix /tmp/.ICE-unix ; do
  171. if [ -d $d ]; then
  172. if [ `ls -ld $d | cut -d' ' -f4` \
  173. != root ]; then
  174. chown root $d
  175. fi
  176. if [ `ls -ld $d | cut -d' ' -f1` \
  177. != drwxrwxrwt ]; then
  178. chmod 1777 $d
  179. fi
  180. elif [ -e $d ]; then
  181. echo "Error: $d exists and isn't a directory."
  182. else
  183. mkdir -m 1777 $d
  184. fi
  185. done
  186. fi
  187. }
  188. # End subroutines
  189. stty status '^T'
  190. # Set shell to ignore SIGINT (2), but not children;
  191. # shell catches SIGQUIT (3) and returns to single user after fsck.
  192. trap : 2
  193. trap : 3 # shouldn't be needed
  194. HOME=/; export HOME
  195. INRC=1; export INRC
  196. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  197. export PATH
  198. # must set the domainname before rc.conf, so YP startup choices can be made
  199. if [ -f /etc/defaultdomain ]; then
  200. domainname `stripcom /etc/defaultdomain`
  201. fi
  202. # pick up option configuration
  203. . /etc/rc.conf
  204. if [ X"$1" = X"shutdown" ]; then
  205. dd if=/dev/arandom of=/var/db/host.random bs=65536 count=1 >/dev/null 2>&1
  206. chmod 600 /var/db/host.random >/dev/null 2>&1
  207. _c=$?
  208. if [ ${_c} -eq 0 -a -n "${pkg_scripts}" ]; then
  209. echo -n 'stopping package daemons:'
  210. while [ -n "${pkg_scripts}" ]; do
  211. _r=${pkg_scripts##* }
  212. pkg_scripts=${pkg_scripts%%*( )${_r}}
  213. [ -x /etc/rc.d/${_r} ] && /etc/rc.d/${_r} stop
  214. done
  215. echo '.'
  216. fi
  217. if [ ${_c} -eq 0 -a -f /etc/rc.shutdown ]; then
  218. echo /etc/rc.shutdown in progress...
  219. . /etc/rc.shutdown
  220. echo /etc/rc.shutdown complete.
  221. # bring carp interfaces down gracefully
  222. ifconfig | while read a b; do
  223. case $a in
  224. carp+([0-9]):) ifconfig ${a%:} down ;;
  225. esac
  226. done
  227. if [ X"${powerdown}" = X"YES" ]; then
  228. exit 2
  229. fi
  230. else
  231. echo single user: not running /etc/rc.shutdown
  232. fi
  233. exit 0
  234. fi
  235. swapctl -A -t blk
  236. if [ -e /fastboot ]; then
  237. echo "Fast boot: skipping disk checks."
  238. elif [ X"$1" = X"autoboot" ]; then
  239. echo "Automatic boot in progress: starting file system checks."
  240. fsck -p
  241. case $? in
  242. 0)
  243. ;;
  244. 2)
  245. exit 1
  246. ;;
  247. 4)
  248. echo "Rebooting..."
  249. reboot
  250. echo "Reboot failed; help!"
  251. exit 1
  252. ;;
  253. 8)
  254. echo "Automatic file system check failed; help!"
  255. exit 1
  256. ;;
  257. 12)
  258. echo "Boot interrupted."
  259. exit 1
  260. ;;
  261. 130)
  262. # interrupt before catcher installed
  263. exit 1
  264. ;;
  265. *)
  266. echo "Unknown error; help!"
  267. exit 1
  268. ;;
  269. esac
  270. fi
  271. trap "echo 'Boot interrupted.'; exit 1" 3
  272. umount -a >/dev/null 2>&1
  273. mount -a -t nonfs,vnd
  274. mount -uw / # root on nfs requires this, others aren't hurt
  275. rm -f /fastboot # XXX (root now writeable)
  276. # set flags on ttys. (do early, in case they use tty for SLIP in netstart)
  277. echo 'setting tty flags'
  278. ttyflags -a
  279. if [ -f /sbin/kbd -a -f /etc/kbdtype ]; then
  280. kbd `cat /etc/kbdtype`
  281. fi
  282. wsconsctl_conf
  283. if [ X"${pf}" != X"NO" ]; then
  284. RULES="block all"
  285. RULES="$RULES\npass on lo0"
  286. RULES="$RULES\npass in proto tcp from any to any port 22 keep state"
  287. RULES="$RULES\npass out proto { tcp, udp } from any to any port 53 keep state"
  288. RULES="$RULES\npass out inet proto icmp all icmp-type echoreq keep state"
  289. if ifconfig lo0 inet6 >/dev/null 2>&1; then
  290. RULES="$RULES\npass out inet6 proto icmp6 all icmp6-type neighbrsol"
  291. RULES="$RULES\npass in inet6 proto icmp6 all icmp6-type neighbradv"
  292. RULES="$RULES\npass out inet6 proto icmp6 all icmp6-type routersol"
  293. RULES="$RULES\npass in inet6 proto icmp6 all icmp6-type routeradv"
  294. fi
  295. RULES="$RULES\npass proto carp keep state (no-sync)"
  296. case `sysctl vfs.mounts.nfs 2>/dev/null` in
  297. *[1-9]*)
  298. # don't kill NFS
  299. RULES="set reassemble yes no-df\n$RULES"
  300. RULES="$RULES\npass in proto { tcp, udp } from any port { 111, 2049 } to any"
  301. RULES="$RULES\npass out proto { tcp, udp } from any to any port { 111, 2049 }"
  302. ;;
  303. esac
  304. echo $RULES | pfctl -f -
  305. pfctl -e
  306. fi
  307. # Fill net.inet.(tcp|udp).baddynamic lists from /etc/services
  308. fill_baddynamic udp
  309. fill_baddynamic tcp
  310. sysctl_conf
  311. # set hostname, turn on network
  312. echo 'starting network'
  313. ifconfig -g carp carpdemote 128
  314. if [ -f /etc/resolv.conf.save ]; then
  315. mv -f /etc/resolv.conf.save /etc/resolv.conf
  316. touch /etc/resolv.conf
  317. fi
  318. . /etc/netstart
  319. echo rekey > /dev/arandom # any write triggers an RC4 rekey
  320. if [ X"${pf}" != X"NO" ]; then
  321. if [ -f ${pf_rules} ]; then
  322. pfctl -f ${pf_rules}
  323. fi
  324. # bring up pfsync after the working ruleset has been loaded
  325. if [ -f /etc/hostname.pfsync0 ]; then
  326. . /etc/netstart pfsync0
  327. fi
  328. fi
  329. mount -s /usr >/dev/null 2>&1
  330. mount -s /var >/dev/null 2>&1
  331. random_seed
  332. # clean up left-over files
  333. rm -f /etc/nologin /var/spool/lock/LCK.* /var/spool/uucp/STST/*
  334. (cd /var/run && { rm -rf -- *; install -c -m 664 -g utmp /dev/null utmp; })
  335. (cd /var/authpf && rm -rf -- *)
  336. # save a copy of the boot messages
  337. dmesg >/var/run/dmesg.boot
  338. make_keys
  339. echo -n 'starting early daemons:'
  340. start_daemon syslogd ldattach pflogd named nsd ntpd isakmpd iked sasyncd
  341. start_daemon ldapd npppd
  342. echo '.'
  343. if [ X"${ipsec}" != X"NO" ]; then
  344. if [ -f ${ipsec_rules} ]; then
  345. ipsecctl -f ${ipsec_rules}
  346. fi
  347. fi
  348. echo -n 'starting RPC daemons:'
  349. start_daemon portmap ypldap
  350. if [ X"`domainname`" != X"" ]; then
  351. start_daemon ypserv ypbind yppasswdd
  352. fi
  353. start_daemon mountd nfsd lockd statd amd
  354. echo '.'
  355. mount -a
  356. swapctl -A -t noblk
  357. # /var/crash should be a directory or a symbolic link
  358. # to the crash directory if core dumps are to be saved.
  359. if [ -d /var/crash ]; then
  360. savecore ${savecore_flags} /var/crash
  361. fi
  362. if [ X"${check_quotas}" = X"YES" ]; then
  363. echo -n 'checking quotas:'
  364. quotacheck -a
  365. echo ' done.'
  366. quotaon -a
  367. fi
  368. kvm_mkdb # build kvm(3) databases
  369. dev_mkdb
  370. chmod 666 /dev/tty[pqrstuvwxyzPQRST]*
  371. chown root:wheel /dev/tty[pqrstuvwxyzPQRST]*
  372. # check the password temp/lock file
  373. if [ -f /etc/ptmp ]; then
  374. logger -s -p auth.err \
  375. 'password file may be incorrect -- /etc/ptmp exists'
  376. fi
  377. echo clearing /tmp
  378. # prune quickly with one rm, then use find to clean up /tmp/[lq]*
  379. # (not needed with mfs /tmp, but doesn't hurt there...)
  380. (cd /tmp && rm -rf [a-km-pr-zA-Z]*)
  381. (cd /tmp &&
  382. find . ! -name . ! -name lost+found ! -name quota.user \
  383. ! -name quota.group -execdir rm -rf -- {} \; -type d -prune)
  384. setup_X_sockets
  385. [ -f /etc/rc.securelevel ] && . /etc/rc.securelevel
  386. if [ X"${securelevel}" != X"" ]; then
  387. echo -n 'setting kernel security level: '
  388. sysctl kern.securelevel=${securelevel}
  389. fi
  390. # patch /etc/motd
  391. if [ ! -f /etc/motd ]; then
  392. install -c -o root -g wheel -m 664 /dev/null /etc/motd
  393. fi
  394. if T=`mktemp /tmp/_motd.XXXXXXXXXX`; then
  395. sysctl -n kern.version | sed 1q > $T
  396. echo "" >> $T
  397. sed '1,/^$/d' < /etc/motd >> $T
  398. cmp -s $T /etc/motd || cp $T /etc/motd
  399. rm -f $T
  400. fi
  401. if [ X"${accounting}" = X"YES" ]; then
  402. if [ ! -f /var/account/acct ]; then
  403. touch /var/account/acct
  404. fi
  405. echo 'turning on accounting'; accton /var/account/acct
  406. fi
  407. if [ -f /sbin/ldconfig ]; then
  408. echo 'creating runtime link editor directory cache.'
  409. if [ -d /usr/local/lib ]; then
  410. shlib_dirs="/usr/local/lib $shlib_dirs"
  411. fi
  412. if [ -d /usr/X11R6/lib ]; then
  413. shlib_dirs="/usr/X11R6/lib $shlib_dirs"
  414. fi
  415. ldconfig $shlib_dirs
  416. fi
  417. echo 'preserving editor files.'; /usr/libexec/vi.recover
  418. echo -n 'starting network daemons:'
  419. start_daemon ldomd sshd snmpd ldpd ripd ospfd ospf6d bgpd ifstated
  420. start_daemon relayd dhcpd dhcrelay mrouted dvmrpd
  421. if ifconfig lo0 inet6 >/dev/null 2>&1; then
  422. fw=`sysctl -n net.inet6.ip6.forwarding`
  423. if [ X"${fw}" = X"0" ]; then
  424. start_daemon rtsold
  425. else
  426. start_daemon route6d rtadvd
  427. fi
  428. fi
  429. start_daemon hostapd rwhod lpd sendmail smtpd httpd slowcgi nginx ftpd
  430. start_daemon ftpproxy tftpd tftpproxy identd inetd rarpd bootparamd
  431. start_daemon rbootd mopd popa3d spamd spamlogd kdc kadmind kpasswdd
  432. start_daemon ipropd_master ipropd_slave sndiod
  433. echo '.'
  434. if [ X"${spamd_flags}" != X"NO" ]; then
  435. /usr/libexec/spamd-setup -D
  436. fi
  437. # If rc.firstime exists, run it just once, and make sure it is deleted
  438. if [ -f /etc/rc.firsttime ]; then
  439. mv /etc/rc.firsttime /etc/rc.firsttime.run
  440. . /etc/rc.firsttime.run 2>&1 | tee /dev/tty |
  441. mail -Es "`hostname` rc.firsttime output" root >/dev/null
  442. fi
  443. rm -f /etc/rc.firsttime.run
  444. # Run rc.d(8) scripts from packages
  445. if [ -n "${pkg_scripts}" ]; then
  446. echo -n 'starting package daemons:'
  447. for _r in $pkg_scripts; do
  448. [ -x /etc/rc.d/${_r} ] && start_daemon ${_r}
  449. done
  450. echo '.'
  451. fi
  452. [ -f /etc/rc.local ] && . /etc/rc.local
  453. ifconfig -g carp -carpdemote 128 # disable carp interlock
  454. mixerctl_conf
  455. echo -n 'starting local daemons:'
  456. start_daemon apmd sensorsd hotplugd watchdogd cron wsmoused xdm
  457. echo '.'
  458. date
  459. exit 0