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