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