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