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