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