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